source: branches/Mars_MC/fact/processing/fillratescan.C@ 17430

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