Subversion Repositories f9daq

Rev

Rev 172 | Go to most recent revision | Details | 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
 
168
void NormateSet(int file, int nrpoint, double *min, double *max, double *setCount, double *setAcc)
169
{
170
   int count = 0;
171
 
172
   // Find minimal value in set and subtract the offset
173
   *min = TMath::MinElement(nrpoint, setAcc);
174
   for(int i = 0; i < nrpoint; i++)
175
      setAcc[i] -= *min;
176
 
177
   // Find maximal value in set and normate to 1
178
   *max = TMath::MaxElement(nrpoint, setAcc);
179
   for(int i = 0; i < nrpoint; i++)
180
   {
181
      count = file - nrpoint + i;
182
      setCount[count] = setAcc[i]/(*max);
183
      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));
184
   }
185
}
186
 
187
// Estimate the next point, depending on the set of points beforehand (least squares fit) and return the error
188
double PointEstimate(int nrp, double *points)
189
{
190
   double accx = 0, accy = 0, accxy = 0, accx2 = 0;
191
   double A, B;
192
   double esty;
193
 
194
   for(int i = 0; i < nrp; i++)
195
   {
196
      accx += points[2*i];
197
      accy += points[2*i+1];
198
      accxy += points[2*i]*points[2*i+1];
199
      accx2 += points[2*i]*points[2*i];
200
   }
201
 
202
   A = (accx2*accy - accx*accxy)/(nrp*accx2 - accx*accx);
203
   B = (nrp*accxy - accx*accy)/(nrp*accx2 - accx*accx);
204
 
205
   esty = A + B*points[2*nrp];
206
 
207
   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]);
208
 
209
   return abs(esty - points[2*nrp+1])/points[2*nrp+1];
210
}
211
 
212
// Separate functions -----------------------------------------