source: trunk/Mars/fact/processing/fillratescan.C@ 19266

Last change on this file since 19266 was 19266, checked in by tbretz, 6 years ago
Fixed some minor issues with the cling based interpreter - this should not have any effect at runtime maybe except stability
File size: 10.5 KB
Line 
1Int_t CheckVoltage(TString fname, Double_t beg, Double_t end)
2{
3 fname.ReplaceAll("RATE_SCAN_DATA", "BIAS_CONTROL_VOLTAGE");
4
5 fits file(fname.Data());
6 if (!file)
7 return -2;
8
9 //cout << fname << endl;
10
11 Double_t time;
12 Float_t Uout[416];
13 UShort_t Uref[416];
14 memset(Uout, 0, 416*4);
15 memset(Uref, 0, 416*2);
16
17 if (!file.SetPtrAddress("Time", &time))
18 return -1;
19
20 if (!file.SetPtrAddress("Uout", Uout) && !file.SetPtrAddress("Uref", Uref))
21 return -1;
22
23 //cout << "Search for: " << beg-15773 << " " << end-15773 << endl;
24
25 Int_t rows = 0;
26 while (file.GetNextRow())
27 {
28 if (time<beg)
29 continue;
30
31 if (time>end)
32 break;
33
34 Int_t cnt = 0;
35 for (int i=0; i<320; i++)
36 if (Uout[i]>50 || Uref[i]>2900)
37 cnt++;
38
39 if (cnt<318)
40 return kFALSE;
41
42 rows++;
43 }
44
45 return rows>0;
46}
47
48Float_t GetOffset(TString fname, Double_t beg, Double_t end)
49{
50 if (end < 15937)
51 fname.ReplaceAll("RATE_SCAN_DATA", "FEEDBACK_DEVIATION");
52 else
53 fname.ReplaceAll("RATE_SCAN_DATA", "FEEDBACK_CALIBRATED_CURRENTS");
54
55 fits file(fname.Data());
56
57 //cout << fname << endl;
58
59 Double_t time;
60 Float_t delta = -100;
61 Float_t delta_prev = -100;
62
63 if (!file.SetPtrAddress("Time", &time))
64 return -100;
65
66 if (end < 15937)
67 {
68 if (!file.SetPtrAddress("DeltaUser", &delta))
69 return -100;
70 }
71 else
72 {
73 if (!file.SetPtrAddress("U_nom", &delta))
74 return -100;
75 }
76
77 //cout << "Search for: " << beg-15773 << " " << end-15773 << endl;
78
79 Int_t rows = 0;
80 while (file.GetNextRow())
81 {
82 if (time>beg)
83 return delta_prev;
84
85 delta_prev = delta;
86 }
87
88 return -100;
89}
90
91Float_t GetCurrent(TString fname, Double_t beg, Double_t end)
92{
93 fname.ReplaceAll("RATE_SCAN_DATA", "FEEDBACK_CALIBRATED_CURRENTS");
94
95 //the next two lines are needed for ISDC and data before 11.3.2013
96 //fname.ReplaceAll("RATE_SCAN_DATA", "CALIBRATED_CURRENTS");
97 //fname = gSystem->BaseName(fname.Data());
98 //fname.Prepend("/scratch_nfs/calibrated_currents/");
99
100 fits file(fname.Data());
101 if (!file)
102 return -2;
103
104 //cout << fname << endl;
105
106 Double_t time;
107 Float_t med, dev;
108 if (!file.SetPtrAddress("Time", &time) ||
109 !file.SetPtrAddress("I_med", &med))
110 return -2;
111
112 double med_avg = 0;
113 int cnt = 0;
114
115 //cout << "Search for: " << beg-15773 << " " << end-15773 << endl;
116
117 while (file.GetNextRow())
118 {
119// if (int(time)==15773)
120// cout << time-15773 << endl;
121
122 if (time<beg)
123 continue;
124
125 if (time>end)
126 break;
127
128 med_avg += med;
129 cnt ++;
130 }
131
132 return cnt==0 ? -1 : med_avg/cnt;
133}
134
135TArrayD GetPointing(TString fname, Double_t beg, Double_t end)
136{
137 fname.ReplaceAll("RATE_SCAN_DATA", "DRIVE_CONTROL_TRACKING_POSITION");
138
139 fits file(fname.Data());
140 if (!file)
141 return TArrayD();
142
143 //cout << fname << endl;
144
145 Double_t time;
146 Double_t zd, az, ra, dec;
147 if (!file.SetPtrAddress("Time", &time) ||
148 !file.SetPtrAddress("Az", &az) ||
149 !file.SetPtrAddress("Zd", &zd) ||
150 !file.SetPtrAddress("Ra", &ra) ||
151 !file.SetPtrAddress("Dec", &dec))
152 return TArrayD();
153
154 Int_t n = 0;
155
156 TArrayD rc;
157 while (file.GetNextRow())
158 {
159 if (time<beg)
160 continue;
161
162 if (time>end)
163 break;
164
165 if (rc.GetSize()==0)
166 {
167 rc.Set(6);
168
169 rc[0] = zd;
170 rc[1] = az;
171 rc[2] = zd;
172 rc[3] = az;
173
174 rc[4] = ra;
175 rc[5] = dec;
176 continue;
177 }
178
179 if (zd<rc[0]) rc[0] = zd;
180 if (zd>rc[2]) rc[2] = zd;
181
182 if (az<rc[1]) rc[1] = az;
183 if (az>rc[3]) rc[3] = az;
184
185 rc[4] += ra;
186 rc[5] += dec;
187
188 n++;
189 }
190
191 if (rc.GetSize()>0)
192 {
193 rc[4] /= n;
194 rc[5] /= n;
195 }
196
197 return rc;
198}
199
200struct data {
201 TString name;
202 int offset;
203 double time[2];
204 UShort_t th[2];
205 double rate[2];
206 int cnt;
207 Long64_t id;
208
209 data(TString fname)
210 {
211 name = fname;
212 offset = -100;
213 time[0] = time[1] = -1;
214 th[0] = th[1] = 0;
215 rate[0] = rate[1] = -1;
216 cnt = 0;
217 id = -1;
218 }
219};
220
221int Delete(MSQLMagic &serv, TString fname)
222{
223 TRegexp rex("20[0-1][0-9]/[01][0-9]/[0-3][0-9]");
224
225 TString date = fname(rex);
226
227 date.ReplaceAll("/", "-");
228
229 MTime beg;
230 beg.SetSqlDateTime(date);
231
232 TString query;
233 query += "fTimeBegin>'";
234 query += beg.GetSqlDateTime();
235 query += "' AND ";
236 query += "fTimeEnd<'";
237 query += MTime(beg.GetMjd()+1).GetSqlDateTime();
238 query += "'";
239
240 return serv.Delete("Ratescan", query) ? 1 : 2;
241}
242
243int Fill(MSQLMagic &serv, data &d)
244{
245 if (d.time[1]<0 || d.cnt==0 || d.th[0]==d.th[1])
246 return 1;
247
248 Int_t volt = CheckVoltage(d.name, d.time[0], d.time[1]);
249 Float_t current = GetCurrent(d.name, d.time[0], d.time[1]);
250 Float_t ov = GetOffset(d.name, d.time[0], d.time[1]);
251 TArrayD point = GetPointing(d.name, d.time[0], d.time[1]);
252
253 MTime tbeg(d.time[0]+d.offset);
254 MTime tend(d.time[1]+d.offset);
255
256 TString beg = tbeg.GetSqlDateTime();
257 TString end = tend.GetSqlDateTime();
258
259 cout << "=== BEG === " << beg << '\n';
260
261 cout << " ID " << d.id << endl;
262
263 if (current>=0)
264 cout << " CUR " << current << '\n';
265
266 if (volt>=0)
267 cout << " VOLT " << volt << '\n';
268
269 if (ov>-70)
270 cout << " OV " << ov << '\n';
271
272 cout << " TH_beg " << d.th[0] << '\n';
273 cout << " TH_end " << d.th[1] << '\n';
274 cout << " RATE_beg " << d.rate[0] << '\n';
275 cout << " RATE_end " << d.rate[1] << '\n';
276 cout << " COUNT " << d.cnt << '\n';
277 if (point.GetSize()>0)
278 {
279 cout << " ZD/AZ[deg] " << point[0] << "/" << point[1] << " -- " << point[2] << "/" << point[3] <<'\n';
280 cout << " RA/DEC " << point[4] << "h/" << point[5] << "deg\n";
281 }
282 cout << "=== END === " << MTime(d.time[1]+d.offset).GetSqlDateTime() << '\n' << endl;
283
284 TString vars;
285 if (volt>=0)
286 vars += Form("fVoltageIsOn=%d, ", volt);
287 if (current>=0)
288 vars += Form("fCurrentMedMean=%f, ", current);
289 if (ov>=-70)
290 vars += Form("fOvervoltage=%f, ", ov);
291 if (point.GetSize()>0)
292 {
293 vars += Form("fZdMin=%f, ", point[0]);
294 vars += Form("fZdMax=%f, ", point[2]);
295 vars += Form("fAzMin=%f, ", point[1]);
296 vars += Form("fAzMax=%f, ", point[3]);
297 vars += Form("fRaMean=%f, ", point[4]);
298 vars += Form("fDecMean=%f, ", point[5]);
299 }
300 vars += Form("fNight=%d, ", MTime(tbeg.GetMjd()-1).GetNightAsInt());
301 vars += Form("fRatescanID=%lld, ", d.id);
302 vars += Form("fTimeBegin='%s', ", beg.Data());
303 vars += Form("fTimeEnd='%s', ", end.Data());
304 vars += Form("fRateBegin=%f, ", d.rate[0]);
305 vars += Form("fRateEnd=%f, ", d.rate[1]);
306 vars += Form("fThresholdBegin=%d, ", d.th[0]);
307 vars += Form("fThresholdEnd=%d, ", d.th[1]);
308 vars += Form("fNumPoints=%d ", d.cnt);
309
310
311 return serv.Insert("Ratescan", vars);
312}
313
314int Process(MSQLMagic &serv, TString fname)
315{
316 data d(fname);
317
318 Int_t cnt = 0;
319
320 fits file(fname.Data());
321 if (!file)
322 return 2;
323
324 d.offset = file.GetUInt("MJDREF");
325
326 bool old = file.HasColumn("Data0");
327
328 /* ================== That does not work, I don't know why ===================
329 Double_t time;
330 ULong64_t id;
331 UInt_t th;
332 Float_t trig;
333 Float_t ontime;
334
335 if (!file.SetPtrAddress(string("Time"), &time) ||
336 !file.SetPtrAddress(string(old ? "Data0" : "Id"), &id) ||
337 !file.SetPtrAddress(string(old ? "Data1" : "Threshold"), &th) ||
338 !file.SetPtrAddress(string(old ? "Data4" : "TriggerRate"), &trig) ||
339 !file.SetPtrAddress(string(old ? "Data3" : "RelOnTime"), &ontime))
340 return -1;
341 */
342 Double_t *ptime = (Double_t*) file.SetPtrAddress("Time");
343 ULong64_t *pid = (ULong64_t*)file.SetPtrAddress(old ? "Data0" : "Id");
344 UInt_t *pth = (UInt_t*) file.SetPtrAddress(old ? "Data1" : "Threshold");
345 Float_t *ptrig = (Float_t*) file.SetPtrAddress(old ? "Data4" : "TriggerRate");
346 Float_t *pontime = (Float_t*) file.SetPtrAddress(old ? "Data3" : "RelOnTime");
347
348 if (!ptime || !pid || !pth || !ptrig || !pontime)
349 return kFALSE;
350
351 Double_t &time = *ptime;
352 ULong64_t &id = *pid;
353 UInt_t &th = *pth;
354 Float_t &trig = *ptrig;
355 Float_t &ontime = *pontime;
356
357 while (file.GetNextRow())
358 {
359 if (th>3000)
360 continue;
361
362 if (id!=d.id)
363 {
364 if (!Fill(serv, d))
365 return 0;
366
367 d.rate[0] = ontime>0 ? trig/ontime : -1;
368 d.time[0] = time;
369 d.th[0] = th;
370 d.id = id;
371
372 d.cnt = 0;
373 }
374
375 d.rate[1] = ontime>0 ? trig/ontime : -1;
376 d.time[1] = time;
377 d.th[1] = th;
378
379 d.cnt++;
380 }
381
382 return Fill(serv, d);
383}
384
385int fillratescan(TString fname, Bool_t dummy=kTRUE)
386{
387 MSQLMagic serv("sql.rc");
388 if (!serv.IsConnected())
389 {
390 cout << "ERROR - Connection to database failed." << endl;
391 return 0;
392 }
393
394 cout << "fillratescan" << endl;
395 cout << "------------" << endl;
396 cout << endl;
397 cout << "Connected to " << serv.GetName() << endl;
398 cout << "File: " << fname << endl;
399 cout << endl;
400
401 serv.SetIsDummy(dummy);
402
403 if (!Delete(serv, fname))
404 return 2;
405
406 //return Process(serv, fname);
407 if (!Process(serv, fname))
408 return 2;
409
410 return 1;
411}
412
413int fillratescan(Bool_t dummy=kTRUE)
414{
415 MSQLMagic serv("sql.rc");
416 if (!serv.IsConnected())
417 {
418 cout << "ERROR - Connection to database failed." << endl;
419 return 0;
420 }
421
422 cout << "fillratescan" << endl;
423 cout << "------------" << endl;
424 cout << endl;
425 cout << "Connected to " << serv.GetName() << endl;
426 cout << endl;
427
428 serv.SetIsDummy(dummy);
429
430 MDirIter Next("/fact/aux", "*RATE_SCAN_DATA.fits", 3);
431
432 while (1)
433 {
434 TString name = Next();
435 if (name.IsNull())
436 break;
437
438 if (name==".")
439 continue;
440
441 cout << "File: " << name << endl;
442 if (!Delete(serv, name))
443 return 2;
444 if (!Process(serv, name))
445 return 2;
446 }
447
448 return 1;
449}
Note: See TracBrowser for help on using the repository browser.