Subversion Repositories f9daq

Rev

Rev 172 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
146 f9daq 1
#include "../include/sipmscan.h"
2
#include "../include/workstation.h"
3
 
4
#include <stdio.h>
5
#include <stdlib.h>
6
 
7
// Separate functions -----------------------------------------
8
void GetTime(int intime, char *outtime)
9
{
10
   time_t rawtime;
11
   struct tm * timeinfo;
12
   if(intime < 0)
13
      time(&rawtime);
14
   else
15
      rawtime = (time_t)intime;
16
   timeinfo = localtime(&rawtime);
17
   sprintf(outtime, "%s", asctime(timeinfo));
18
   int len = strlen(outtime);
19
   if(len) outtime[len-1] = 0;
20
}
21
 
22
void remove_ext(char *inname, char *outname)
23
{
24
  char ctemp[256];
25
  for(int i = 0; i < (int)strlen(inname); i++)
26
  {
27
    if( (inname[i] == '.') && (i > (int)(strlen(inname)-6)) )
28
    {
29
      ctemp[i] = '\0';
30
      sprintf(outname, "%s", ctemp);
31
      break;
32
    }
33
    else
34
      ctemp[i] = inname[i];
35
  }
36
 
37
  if(DBGSIG)
38
    printf("remove_ext(): Outfile = %s\n", outname);
39
}
40
 
41
void remove_from_last(char *inname, char search, char *outname)
42
{
43
   char ctemp[256];
44
   int searchpos = -1;
45
   for(int i = (int)strlen(inname); i >= 0; i--)
46
   {
47
      if(inname[i] == search)
48
      {
49
         searchpos = i;
50
         break;
51
      }
52
   }
53
 
54
   for(int i = 0; i < searchpos; i++)
55
      ctemp[i] = inname[i];
56
 
57
   ctemp[searchpos] = '\0';
58
   sprintf(outname, "%s", ctemp);
59
 
60
   if(DBGSIG)
61
      printf("remove_from_last(): Outfile = %s\n", outname);
62
}
63
 
64
void remove_before_last(char *inname, char search, char *outname)
65
{
66
   char ctemp[256];
67
   int searchpos = -1;
68
   for(int i = (int)strlen(inname); i >= 0; i--)
69
   {
70
      if(inname[i] == search)
71
      {
72
         searchpos = i;
73
         break;
74
      }
75
   }
76
 
77
   int k = 0;
78
   for(int i = searchpos+1; i < (int)strlen(inname); i++)
79
   {
80
      ctemp[k] = inname[i];
81
      k++;
82
   }
83
 
84
   ctemp[k] = '\0';
85
   sprintf(outname, "%s", ctemp);
86
 
87
   if(DBGSIG)
88
      printf("remove_before_last(): Outfile = %s\n", outname);
89
}
90
 
91
void SeqNumber(int innum, int maxnum, char *outstr)
92
{
93
   int zeronum = 5;
94
 
95
   // Check how many zeroes we need to add to get sequential numbers
96
   if( (maxnum > 0) && (maxnum < 1000) )
97
      zeronum = 2;
98
   else if( (maxnum >= 1000) && (maxnum < 10000) )
99
      zeronum = 3;
100
   else if( (maxnum >= 10000) && (maxnum < 100000) )
101
      zeronum = 4;
102
   else if( (maxnum >= 100000) && (maxnum < 1000000) )
103
      zeronum = 5;
104
 
105
   // Make the sequence number depending on the number of zeroes
106
   if(zeronum == 2)
107
   {
108
      if(innum < 10)
109
         sprintf(outstr, "00%d", innum);
110
      else if( (innum >= 10) && (innum < 100) )
111
         sprintf(outstr, "0%d", innum);
112
      else if( (innum >= 100) && (innum < 1000) )
113
         sprintf(outstr, "%d", innum);
114
   }
115
   else if(zeronum == 3)
116
   {
117
      if(innum < 10)
118
         sprintf(outstr, "000%d", innum);
119
      else if( (innum >= 10) && (innum < 100) )
120
         sprintf(outstr, "00%d", innum);
121
      else if( (innum >= 100) && (innum < 1000) )
122
         sprintf(outstr, "0%d", innum);
123
      else if( (innum >= 1000) && (innum < 10000) )
124
         sprintf(outstr, "%d", innum);
125
   }
126
   else if(zeronum == 4)
127
   {
128
      if(innum < 10)
129
         sprintf(outstr, "0000%d", innum);
130
      else if( (innum >= 10) && (innum < 100) )
131
         sprintf(outstr, "000%d", innum);
132
      else if( (innum >= 100) && (innum < 1000) )
133
         sprintf(outstr, "00%d", innum);
134
      else if( (innum >= 1000) && (innum < 10000) )
135
         sprintf(outstr, "0%d", innum);
136
      else if( (innum >= 10000) && (innum < 100000) )
137
         sprintf(outstr, "%d", innum);
138
   }
139
   else if(zeronum == 5)
140
   {
141
      if(innum < 10)
142
         sprintf(outstr, "00000%d", innum);
143
      else if( (innum >= 10) && (innum < 100) )
144
         sprintf(outstr, "0000%d", innum);
145
      else if( (innum >= 100) && (innum < 1000) )
146
         sprintf(outstr, "000%d", innum);
147
      else if( (innum >= 1000) && (innum < 10000) )
148
         sprintf(outstr, "00%d", innum);
149
      else if( (innum >= 10000) && (innum < 100000) )
150
         sprintf(outstr, "0%d", innum);
151
      else if( (innum >= 100000) && (innum < 1000000) )
152
         sprintf(outstr, "%d", innum);
153
   }
154
}
155
 
156
void TimeEstimate(clock_t stopw0, time_t time0, float progress, char *retEstim, int offset)
157
{
158
   // Calculate the remaining time
159
   clock_t clkt1;
160
   char ctemp[512];
161
 
162
   clkt1 = clock() - stopw0;
163
   if(DBGSIG) printf("TimeEstimate(): Startclock = %d, Midclock (%f) = %f (%d), starttime = %d, curtime = %d\n", (int)stopw0, progress, (float)clkt1/CLOCKS_PER_SEC, (int)clkt1, (int)time0, (int)time(NULL));
164
   GetTime((int)(100.*((float)clkt1/CLOCKS_PER_SEC)/progress+(int)time0+offset), ctemp);
165
   sprintf(retEstim, "Estimated end time: %s", ctemp);
166
}
167
 
172 f9daq 168
void TimeEstimateNew(int nr, clock_t stopw0, time_t time0, int rX, int rY, int rZ, int xWait, int yWait, int zWait, char *retEstim)
169
{
170
   clock_t clkt1;
171
   char ctemp[512];
172
   double timeSec;
173
   double averMeasTime;
174
 
175
   clkt1 = clock() - stopw0;
176
   if(nr == -1)
177
      return;
178
   else if(nr == 0)
179
      averMeasTime = (double)clkt1/CLOCKS_PER_SEC;
180
   else
181
      averMeasTime = (averMeasTime + (double)clkt1/CLOCKS_PER_SEC)/2.0;
182
 
183
   // calculate the time of one row
184
   timeSec = rX*(xWait + averMeasTime);
185
   // calculate the time of a surface scan
186
   timeSec = timeSec + rY*(timeSec + yWait);
187
   // calculate the time of a zscan
188
   if(rZ == 1)
189
      timeSec = timeSec + zWait;
190
   else
191
      timeSec = timeSec + rZ*(timeSec + zWait);
192
 
193
   GetTime((int)timeSec+(int)time0, ctemp);
194
   sprintf(retEstim, "Estimated end time: %s", ctemp);
195
 
196
   printf("TimeEstimateNew(): Average time of measurement = %lf, Measurement time = %lf, Finishing time = %s\n", averMeasTime, timeSec, ctemp);
197
}
198
 
146 f9daq 199
void NormateSet(int file, int nrpoint, double *min, double *max, double *setCount, double *setAcc)
200
{
201
   int count = 0;
202
 
203
   // Find minimal value in set and subtract the offset
204
   *min = TMath::MinElement(nrpoint, setAcc);
205
   for(int i = 0; i < nrpoint; i++)
206
      setAcc[i] -= *min;
207
 
208
   // Find maximal value in set and normate to 1
209
   *max = TMath::MaxElement(nrpoint, setAcc);
210
   for(int i = 0; i < nrpoint; i++)
211
   {
212
      count = file - nrpoint + i;
213
      setCount[count] = setAcc[i]/(*max);
214
      if(DBGSIG) printf("NormateSet(): Integral check 2 (i=%d,za=%d,j=%d): %lf\t\%lf\n", count, i, nrpoint, setCount[count], setAcc[i]/(*max));
215
   }
216
}
217
 
218
// Estimate the next point, depending on the set of points beforehand (least squares fit) and return the error
219
double PointEstimate(int nrp, double *points)
220
{
221
   double accx = 0, accy = 0, accxy = 0, accx2 = 0;
222
   double A, B;
223
   double esty;
224
 
181 f9daq 225
//   printf("PointEstimate points: ");
146 f9daq 226
   for(int i = 0; i < nrp; i++)
227
   {
181 f9daq 228
//      printf("[%lf,%lf], ", points[2*i], points[2*i+1]);
146 f9daq 229
      accx += points[2*i];
230
      accy += points[2*i+1];
231
      accxy += points[2*i]*points[2*i+1];
232
      accx2 += points[2*i]*points[2*i];
233
   }
181 f9daq 234
//   printf("\n");
146 f9daq 235
 
236
   A = (accx2*accy - accx*accxy)/(nrp*accx2 - accx*accx);
237
   B = (nrp*accxy - accx*accy)/(nrp*accx2 - accx*accx);
238
 
181 f9daq 239
//   printf("Fiting function = %lf + %lf*x\n", A, B);
240
 
146 f9daq 241
   esty = A + B*points[2*nrp];
242
 
243
   if(DBGSIG) printf("PointEstimate(): A = %lf, B = %lf, estimate = %lf, real = %lf, error = %lf\n", A, B, esty, points[2*nrp+1], abs(esty - points[2*nrp+1])/points[2*nrp+1]);
244
 
245
   return abs(esty - points[2*nrp+1])/points[2*nrp+1];
246
}
247
 
248
// Separate functions -----------------------------------------