1 | /* ======================================================================== *\
|
---|
2 | !
|
---|
3 | ! *
|
---|
4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
---|
5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
---|
6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
---|
7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
---|
8 | ! *
|
---|
9 | ! * Permission to use, copy, modify and distribute this software and its
|
---|
10 | ! * documentation for any purpose is hereby granted without fee,
|
---|
11 | ! * provided that the above copyright notice appear in all copies and
|
---|
12 | ! * that both that copyright notice and this permission notice appear
|
---|
13 | ! * in supporting documentation. It is provided "as is" without express
|
---|
14 | ! * or implied warranty.
|
---|
15 | ! *
|
---|
16 | !
|
---|
17 | !
|
---|
18 | ! Author(s): Thomas Bretz, 08/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | ! Author(s): Daniela Dorner, 08/2004 <mailto:dorner@astro.uni-wuerzburg.de>
|
---|
20 | !
|
---|
21 | ! Copyright: MAGIC Software Development, 2000-2004
|
---|
22 | !
|
---|
23 | !
|
---|
24 | \* ======================================================================== */
|
---|
25 |
|
---|
26 | /////////////////////////////////////////////////////////////////////////////
|
---|
27 | //
|
---|
28 | // buildsequenceentries.C
|
---|
29 | // ======================
|
---|
30 | //
|
---|
31 | // to group the runs of one night into sequences, this marco:
|
---|
32 | // - reads the runinformation of one night from the database
|
---|
33 | // - group the runs into sets of following runs with the same conditions
|
---|
34 | // - groups the runs in this sets to sequences such that each run belongs
|
---|
35 | // to the nearest (in time) calibration run
|
---|
36 | // - check if the runs with the same runtype have the same calibration script
|
---|
37 | // and the same trigger tables
|
---|
38 | //
|
---|
39 | /////////////////////////////////////////////////////////////////////////////
|
---|
40 | #include <iostream>
|
---|
41 | #include <iomanip>
|
---|
42 | #include <fstream>
|
---|
43 |
|
---|
44 | #include <MSQLServer.h>
|
---|
45 | #include <TSQLRow.h>
|
---|
46 | #include <TSQLResult.h>
|
---|
47 |
|
---|
48 | #include <TEnv.h>
|
---|
49 | #include <TMath.h>
|
---|
50 | #include <TExMap.h>
|
---|
51 | #include <TArrayI.h>
|
---|
52 | #include <TRegexp.h>
|
---|
53 | #include <TSystem.h>
|
---|
54 |
|
---|
55 | #include <MTime.h>
|
---|
56 | #include <MDirIter.h>
|
---|
57 |
|
---|
58 | using namespace std;
|
---|
59 |
|
---|
60 | int debug = 0;
|
---|
61 |
|
---|
62 | Bool_t DeleteSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t sequ, Bool_t dummy)
|
---|
63 | {
|
---|
64 | TString query1(Form("DELETE FROM Calibration WHERE fSequenceFirst=%d", sequ));
|
---|
65 | TString query2(Form("DELETE FROM Star WHERE fSequenceFirst=%d", sequ));
|
---|
66 | TString query3(Form("DELETE FROM SequenceProcessStatus WHERE fSequenceFirst=%d", sequ));
|
---|
67 | TString query4(Form("UPDATE RunData SET fSequenceFirst=0 WHERE fSequenceFirst=%d", sequ));
|
---|
68 | TString query5(Form("DELETE FROM Sequences WHERE fSequenceFirst=%d AND fManuallyChangedKEY=1", sequ));
|
---|
69 |
|
---|
70 | TString fname(Form("%s/%04d/sequence%08d.txt", sequpath.Data(),sequ/10000, sequ));
|
---|
71 | TString command(Form("rm -r %s/callisto/%04d/%08d/", datapath.Data(), sequ/10000, sequ));
|
---|
72 | TString command2(Form("rm -r %s/star/%04d/%08d/", datapath.Data(), sequ/10000, sequ));
|
---|
73 |
|
---|
74 | if (dummy)
|
---|
75 | {
|
---|
76 | cout << "not using dummy=kTRUE the following commands would be executed: " << endl;
|
---|
77 | cout << "queries: " << endl;
|
---|
78 | cout << query1 << endl;
|
---|
79 | cout << query2 << endl;
|
---|
80 | cout << query3 << endl;
|
---|
81 | cout << query4 << endl;
|
---|
82 | cout << query5 << endl;
|
---|
83 | cout << "removing files:" << endl;
|
---|
84 | cout << "unlink " << fname << endl;
|
---|
85 | cout << command << endl;
|
---|
86 | cout << command2 << endl;
|
---|
87 | return kTRUE;
|
---|
88 | }
|
---|
89 |
|
---|
90 | TSQLResult *res = serv.Query(query1);
|
---|
91 | if (!res)
|
---|
92 | return kFALSE;
|
---|
93 | delete res;
|
---|
94 |
|
---|
95 | res = serv.Query(query2);
|
---|
96 | if (!res)
|
---|
97 | return kFALSE;
|
---|
98 | delete res;
|
---|
99 |
|
---|
100 | res = serv.Query(query3);
|
---|
101 | if (!res)
|
---|
102 | return kFALSE;
|
---|
103 | delete res;
|
---|
104 |
|
---|
105 | res = serv.Query(query4);
|
---|
106 | if (!res)
|
---|
107 | return kFALSE;
|
---|
108 | delete res;
|
---|
109 |
|
---|
110 | res = serv.Query(query5);
|
---|
111 | if (!res)
|
---|
112 | return kFALSE;
|
---|
113 | delete res;
|
---|
114 |
|
---|
115 | gSystem->Unlink(fname);
|
---|
116 |
|
---|
117 | gSystem->Exec(command);
|
---|
118 | gSystem->Exec(command2);
|
---|
119 |
|
---|
120 | return kTRUE;
|
---|
121 | }
|
---|
122 |
|
---|
123 | Int_t DoCheck(TSQLResult &res)
|
---|
124 | {
|
---|
125 | TArrayI data(5);
|
---|
126 | Int_t n = 0;
|
---|
127 |
|
---|
128 | TSQLRow *row=0;
|
---|
129 | while ((row=res.Next()))
|
---|
130 | {
|
---|
131 | n++;
|
---|
132 |
|
---|
133 | if (data[0]==0)
|
---|
134 | {
|
---|
135 | for (int i=0; i<data.GetSize(); i++)
|
---|
136 | data[i] = atoi((*row)[i]);
|
---|
137 | continue;
|
---|
138 | }
|
---|
139 |
|
---|
140 | for (int i=1; i<data.GetSize(); i++)
|
---|
141 | {
|
---|
142 | if (data[i] != atoi((*row)[i]))
|
---|
143 | return i+1;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | return n==0 ? 0 : -1;
|
---|
147 | }
|
---|
148 |
|
---|
149 | Bool_t CheckRuns(MSQLServer &serv, Int_t from, Int_t to, Int_t type)
|
---|
150 | {
|
---|
151 | TString query("SELECT fRunNumber, fL1TriggerTableKEY, fL2TriggerTableKEY,"
|
---|
152 | " fCalibrationScriptKEY, fProjectKEY FROM RunData");
|
---|
153 | query += Form(" WHERE fRunTypeKEY=%d AND fExcludedFDAKEY=1 AND "
|
---|
154 | " (fRunNumber BETWEEN %d AND %d)"
|
---|
155 | " ORDER BY fRunNumber", type, from, to);
|
---|
156 |
|
---|
157 | TSQLResult *res = serv.Query(query);
|
---|
158 | if (!res)
|
---|
159 | return kFALSE;
|
---|
160 |
|
---|
161 | Int_t rc = DoCheck(*res);
|
---|
162 | delete res;
|
---|
163 |
|
---|
164 | switch (rc)
|
---|
165 | {
|
---|
166 | case 0: cout << "ERROR - No runs found for check!" << endl; break;
|
---|
167 | case 1: cout << "ERROR - fRunNumber doesn't match!" << endl; break;
|
---|
168 | case 2: cout << "ERROR - fL1TriggerTableKEY doesn't match!" << endl; break;
|
---|
169 | case 3: cout << "ERROR - fL2TriggerTableKEY doesn't match!" << endl; break;
|
---|
170 | case 4: cout << "ERROR - fCalibrationScriptKEY doesn't match!" << endl; break;
|
---|
171 | case 5: cout << "ERROR - fProjectKEY doesn't match!" << endl; break;
|
---|
172 | }
|
---|
173 |
|
---|
174 | return rc<0;
|
---|
175 | }
|
---|
176 |
|
---|
177 | Bool_t CheckSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, Bool_t dummy)
|
---|
178 | {
|
---|
179 | Bool_t rc=kFALSE;
|
---|
180 |
|
---|
181 | TString query("SELECT fSequenceFirst, count(*) FROM RunData ");
|
---|
182 | query += Form(" WHERE fExcludedFDAKEY=1 AND (fRunNumber BETWEEN %d AND %d)"
|
---|
183 | " GROUP BY fSequenceFirst", from, to);
|
---|
184 |
|
---|
185 | TSQLResult *res = serv.Query(query);
|
---|
186 | if (!res)
|
---|
187 | return kFALSE;
|
---|
188 |
|
---|
189 | Int_t count=0;
|
---|
190 | Int_t nor=0;
|
---|
191 | TArrayI sequences;
|
---|
192 |
|
---|
193 | TSQLRow *row=0;
|
---|
194 | while ((row=res->Next()))
|
---|
195 | {
|
---|
196 | sequences.Set(count+1);
|
---|
197 | sequences.AddAt(atoi((*row)[0]), count);
|
---|
198 | // cout << "sequence: " << sequences.At(count) << endl;
|
---|
199 | nor=atoi((*row)[1]);
|
---|
200 | count++;
|
---|
201 | }
|
---|
202 |
|
---|
203 | delete res;
|
---|
204 |
|
---|
205 | if (sequences.GetSize()>1)
|
---|
206 | {
|
---|
207 | for (Int_t i=0;i<sequences.GetSize();i++)
|
---|
208 | {
|
---|
209 | rc=kTRUE;
|
---|
210 | if (sequences.At(i)!=0)
|
---|
211 | {
|
---|
212 | cout << "deleting sequence " << sequences.At(i) << "... <" << i << ">" << endl;
|
---|
213 | if(!DeleteSequence(serv, datapath, sequpath, sequences.At(i), dummy))
|
---|
214 | rc=kFALSE;
|
---|
215 | }
|
---|
216 | }
|
---|
217 | // return rc;
|
---|
218 | }
|
---|
219 |
|
---|
220 | if (sequences.At(0)==0)
|
---|
221 | return kTRUE;
|
---|
222 |
|
---|
223 |
|
---|
224 | query="SELECT fRunNumber FROM RunData ";
|
---|
225 | query += Form(" WHERE fExcludedFDAKEY=1 AND (fRunNumber BETWEEN %d AND %d)"
|
---|
226 | " ORDER BY fRunNumber", from, to);
|
---|
227 |
|
---|
228 | res = serv.Query(query);
|
---|
229 | if (!res)
|
---|
230 | return kFALSE;
|
---|
231 |
|
---|
232 | TArrayI runs;
|
---|
233 | count=0;
|
---|
234 | row=0;
|
---|
235 | while ((row=res->Next()))
|
---|
236 | {
|
---|
237 | runs.Set(count+1);
|
---|
238 | runs.AddAt(atoi((*row)[0]), count);
|
---|
239 | count++;
|
---|
240 | }
|
---|
241 |
|
---|
242 | delete res;
|
---|
243 |
|
---|
244 |
|
---|
245 | if (nor==runs.GetSize() && sequences.GetSize()==1)
|
---|
246 | {
|
---|
247 | query="SELECT fRunNumber FROM RunData ";
|
---|
248 | query += Form(" WHERE fSequenceFirst=%d ", sequences.At(0));
|
---|
249 |
|
---|
250 | res = serv.Query(query);
|
---|
251 | if (!res)
|
---|
252 | return kFALSE;
|
---|
253 |
|
---|
254 | TArrayI oldruns;
|
---|
255 | count=0;
|
---|
256 | row=0;
|
---|
257 | while ((row=res->Next()))
|
---|
258 | {
|
---|
259 | oldruns.Set(count+1);
|
---|
260 | oldruns.AddAt(atoi((*row)[0]), count);
|
---|
261 | count++;
|
---|
262 | }
|
---|
263 |
|
---|
264 | if (runs.GetSize()!=oldruns.GetSize())
|
---|
265 | {
|
---|
266 | cout << "different sequsizes -> deleting sequence " << sequences.At(0) << endl;
|
---|
267 | // return DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy);
|
---|
268 | rc=DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy);
|
---|
269 | }
|
---|
270 |
|
---|
271 | for (Int_t i=0; i<runs.GetSize(); i++)
|
---|
272 | {
|
---|
273 | if (runs.At(i)==oldruns.At(i))
|
---|
274 | continue;
|
---|
275 | cout << "different runs -> deleting sequence " << sequences.At(0) << endl;
|
---|
276 | rc=DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy);
|
---|
277 | // return DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy);
|
---|
278 | }
|
---|
279 |
|
---|
280 | delete res;
|
---|
281 | }
|
---|
282 |
|
---|
283 | query="SELECT fSequenceFirst FROM Sequences ";
|
---|
284 | query += Form(" WHERE fSequenceFirst BETWEEN %d AND %d "
|
---|
285 | " ORDER BY fSequenceFirst", from, to);
|
---|
286 |
|
---|
287 | cout << query << endl;
|
---|
288 |
|
---|
289 | res = serv.Query(query);
|
---|
290 | if (!res)
|
---|
291 | return kFALSE;
|
---|
292 |
|
---|
293 | Int_t sequ=0;
|
---|
294 | while ((row=res->Next()))
|
---|
295 | {
|
---|
296 | sequ=atoi((*row)[0]);
|
---|
297 | cout << "deleting sequence " << sequ << endl;
|
---|
298 | if(!DeleteSequence(serv, datapath, sequpath, sequ, dummy))
|
---|
299 | rc=kFALSE;
|
---|
300 | }
|
---|
301 |
|
---|
302 | delete res;
|
---|
303 |
|
---|
304 |
|
---|
305 | cout << "same sequence -> nothing to do..." << endl;
|
---|
306 | return rc;
|
---|
307 | }
|
---|
308 |
|
---|
309 | Bool_t InsertSequence(MSQLServer &serv, Int_t from, Int_t to)
|
---|
310 | {
|
---|
311 |
|
---|
312 | // ========== Request number of events ==========
|
---|
313 | TString query("SELECT SUM(fNumEvents), "
|
---|
314 | " SUM(if(TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)<0,"
|
---|
315 | " TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)+24*60*60,"
|
---|
316 | " TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart))), ");
|
---|
317 | query += " MIN(fZenithDistance), MAX(fZenithDistance), ";
|
---|
318 | query += " MIN(fAzimuth), MAX(fAzimuth) ";
|
---|
319 | query += Form(" FROM RunData WHERE fRunTypeKEY=2 AND "
|
---|
320 | " (fRunNumber BETWEEN %d AND %d) AND fExcludedFDAKEY=1",
|
---|
321 | from, to);
|
---|
322 |
|
---|
323 | TSQLResult *res = serv.Query(query);
|
---|
324 | if (!res)
|
---|
325 | return kFALSE;
|
---|
326 |
|
---|
327 | TSQLRow *row = res->Next();
|
---|
328 | if (!row || !(*row)[0])
|
---|
329 | {
|
---|
330 | cout << "ERROR - No result from query: " << query << endl;
|
---|
331 | return kFALSE;
|
---|
332 | }
|
---|
333 |
|
---|
334 | TString nevts = (*row)[0];
|
---|
335 | TString secs = (*row)[1];
|
---|
336 | TString zdmin = (*row)[2];
|
---|
337 | TString zdmax = (*row)[3];
|
---|
338 | TString azmin = (*row)[4];
|
---|
339 | TString azmax = (*row)[5];
|
---|
340 |
|
---|
341 | delete res;
|
---|
342 |
|
---|
343 | // ========== Request start time of sequence ==========
|
---|
344 | query = Form("SELECT fRunStart FROM RunData WHERE fRunNumber=%d AND fExcludedFDAKEY=1", from);
|
---|
345 |
|
---|
346 | res = serv.Query(query);
|
---|
347 | if (!res)
|
---|
348 | return kFALSE;
|
---|
349 |
|
---|
350 | row = res->Next();
|
---|
351 | if (!row || !(*row)[0])
|
---|
352 | {
|
---|
353 | cout << "ERROR - No result from query: " << query << endl;
|
---|
354 | return kFALSE;
|
---|
355 | }
|
---|
356 |
|
---|
357 | TString start((*row)[0]);
|
---|
358 |
|
---|
359 | delete res;
|
---|
360 |
|
---|
361 | // ========== Request data of sequence ==========
|
---|
362 | query = Form("SELECT fSourceKEY, fProjectKEY, "
|
---|
363 | " fL1TriggerTableKEY, fL1TriggerTableKEY,"
|
---|
364 | " fHvSettingsKEY, fDiscriminatorThresholdTableKEY,"
|
---|
365 | " fTriggerDelayTableKEY, fLightConditionsKEY, fTestFlagKEY"
|
---|
366 | " FROM RunData"
|
---|
367 | " WHERE fRunTypeKEY=2 AND fExcludedFDAKEY=1 AND (fRunNumber BETWEEN %d AND %d)"
|
---|
368 | " LIMIT 1", from, to);
|
---|
369 |
|
---|
370 | res = serv.Query(query);
|
---|
371 | if (!res)
|
---|
372 | return kFALSE;
|
---|
373 |
|
---|
374 | row = res->Next();
|
---|
375 | if (!row)
|
---|
376 | {
|
---|
377 | cout << "ERROR - No result from query: " << query << endl;
|
---|
378 | return kFALSE;
|
---|
379 | }
|
---|
380 |
|
---|
381 | TString query1("INSERT Sequences SET");
|
---|
382 | query1+=Form(" fSequenceFirst=%d, fSequenceLast=%d,", from, to);
|
---|
383 | query1+=Form(" fProjectKEY=%s,", (*row)[0]);
|
---|
384 | query1+=Form(" fSourceKEY=%s,", (*row)[1]);
|
---|
385 | query1+=Form(" fNumEvents=%s,", nevts.Data());
|
---|
386 | query1+=Form(" fRunTime=%s,", secs.Data());
|
---|
387 | query1+=Form(" fRunStart=\"%s\",", start.Data());
|
---|
388 | query1+=Form(" fZenithDistanceMin=%s,", zdmin.Data());
|
---|
389 | query1+=Form(" fZenithDistanceMax=%s,", zdmax.Data());
|
---|
390 | query1+=Form(" fAzimuthMin=%s,", azmin.Data());
|
---|
391 | query1+=Form(" fAzimuthMax=%s,", azmax.Data());
|
---|
392 | query1+=Form(" fL1TriggerTableKEY=%s,", (*row)[2]);
|
---|
393 | query1+=Form(" fL2TriggerTableKEY=%s,", (*row)[3]);
|
---|
394 | query1+=Form(" fHvSettingsKEY=%s,", (*row)[4]);
|
---|
395 | query1+=Form(" fDiscriminatorThresholdTableKEY=%s,", (*row)[5]);
|
---|
396 | query1+=Form(" fTriggerDelayTableKEY=%s,", (*row)[6]);
|
---|
397 | query1+=Form(" fLightConditionsKEY=%s,", (*row)[7]);
|
---|
398 | query1+=Form(" fTestFlagKEY=%s, fManuallyChangedKEY=1", (*row)[8]);
|
---|
399 |
|
---|
400 |
|
---|
401 | TString query2 = Form("UPDATE RunData SET fSequenceFirst=%d WHERE"
|
---|
402 | " (fRunNumber BETWEEN %d AND %d) AND"
|
---|
403 | " (fRunTypeKEY BETWEEN 2 AND 4) AND"
|
---|
404 | " fSourceKEY=%d AND fHvSettingsKEY=%s AND fExcludedFDAKEY=1",
|
---|
405 | from, from, to, (*row)[1], (*row)[4]);
|
---|
406 |
|
---|
407 | TString query3 = Form("INSERT SequenceProcessStatus SET fSequenceFirst=%d ", from);
|
---|
408 |
|
---|
409 | delete res;
|
---|
410 |
|
---|
411 | res = serv.Query(query1);
|
---|
412 | if (!res)
|
---|
413 | return kFALSE;
|
---|
414 | delete res;
|
---|
415 |
|
---|
416 | res = serv.Query(query2);
|
---|
417 | if (!res)
|
---|
418 | return kFALSE;
|
---|
419 | delete res;
|
---|
420 |
|
---|
421 | res = serv.Query(query3);
|
---|
422 | if (!res)
|
---|
423 | return kFALSE;
|
---|
424 | delete res;
|
---|
425 |
|
---|
426 | return kTRUE;
|
---|
427 | }
|
---|
428 |
|
---|
429 | Bool_t NewSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, Bool_t dummy)
|
---|
430 | {
|
---|
431 | cout << "Found Sequence (" << from << ", " << to << ") ... checking runs..." << flush;
|
---|
432 |
|
---|
433 | if (!CheckRuns(serv, from, to, 2))
|
---|
434 | {
|
---|
435 | cout << "Warning - Found inconsistency in data-runs (" << from << ", " << to << ")" << endl;
|
---|
436 | //sequence is not built, but kTRUE is returned, to allow
|
---|
437 | //the automatic processing of the other sequences of this day
|
---|
438 | return kTRUE;
|
---|
439 | }
|
---|
440 | if (!CheckRuns(serv, from, to, 3))
|
---|
441 | {
|
---|
442 | cout << "Warning - Found inconsistency in ped-runs (" << from << ", " << to << ")" << endl;
|
---|
443 | //sequence is not built, but kTRUE is returned, to allow
|
---|
444 | //the automatic processing of the other sequences of this day
|
---|
445 | return kTRUE;
|
---|
446 | }
|
---|
447 |
|
---|
448 | cout << "ok." << endl;
|
---|
449 |
|
---|
450 |
|
---|
451 | cout << "checking Sequence..." << endl;
|
---|
452 |
|
---|
453 | if (!CheckSequence(serv, datapath, sequpath, from, to, dummy))
|
---|
454 | {
|
---|
455 | cout << " inserting sequence not necessary" << endl;
|
---|
456 | return kTRUE;
|
---|
457 | }
|
---|
458 |
|
---|
459 |
|
---|
460 | if (dummy)
|
---|
461 | return kTRUE;
|
---|
462 |
|
---|
463 | Bool_t rc = InsertSequence(serv, from, to);
|
---|
464 | if (!rc)
|
---|
465 | cout << "InsertSequence failed!" << endl;
|
---|
466 |
|
---|
467 | return rc;
|
---|
468 | }
|
---|
469 |
|
---|
470 | Bool_t Process(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, Bool_t dummy)
|
---|
471 | {
|
---|
472 |
|
---|
473 | TString query(Form("SELECT fRunNumber, fRunTypeKEY, fRunStart, fRunStop"
|
---|
474 | " FROM RunData"
|
---|
475 | " WHERE fRunNumber BETWEEN %d AND %d AND "
|
---|
476 | " fExcludedFDAKEY=1 AND (fRunTypeKEY BETWEEN 2 AND 4)"
|
---|
477 | " ORDER BY fRunNumber", from, to));
|
---|
478 |
|
---|
479 | TSQLResult *res = serv.Query(query);
|
---|
480 | if (!res)
|
---|
481 | return kFALSE;
|
---|
482 |
|
---|
483 | TExMap map;
|
---|
484 |
|
---|
485 | Int_t start=0;
|
---|
486 | Int_t stop=0;
|
---|
487 | Int_t last=0;
|
---|
488 | Int_t first=0;
|
---|
489 |
|
---|
490 | MTime lasttime;
|
---|
491 |
|
---|
492 | TSQLRow *row=0;
|
---|
493 |
|
---|
494 | enum { UNKNOWN, PED=3, CAL=4, DATA=2 };
|
---|
495 | Char_t status = UNKNOWN;
|
---|
496 |
|
---|
497 | Int_t nblocks = 0;
|
---|
498 |
|
---|
499 | while ((row=res->Next()))
|
---|
500 | {
|
---|
501 | if (!(*row)[1])
|
---|
502 | continue;
|
---|
503 |
|
---|
504 | if (start==0)
|
---|
505 | {
|
---|
506 | first = atoi((*row)[0]);
|
---|
507 | if (debug)
|
---|
508 | cout << "First Run: " << first << endl;
|
---|
509 | }
|
---|
510 |
|
---|
511 | switch (atoi((*row)[1]))
|
---|
512 | {
|
---|
513 | case CAL: // ---------- CALIBRATION ----------
|
---|
514 | if (status!=CAL)
|
---|
515 | {
|
---|
516 | start = stop = atoi((*row)[0]);
|
---|
517 | if (!(*row)[2])
|
---|
518 | cout << "No time available... skipped." << endl;
|
---|
519 | else
|
---|
520 | {
|
---|
521 | MTime *tm = new MTime;
|
---|
522 | tm->SetSqlDateTime((*row)[2]);
|
---|
523 | map.Add((ULong_t)map.GetSize(), (Long_t)tm, (Long_t)nblocks);
|
---|
524 | }
|
---|
525 | }
|
---|
526 | status = CAL;
|
---|
527 | break;
|
---|
528 | default:
|
---|
529 | if (status==CAL)
|
---|
530 | {
|
---|
531 | MTime *tm = new MTime(lasttime);
|
---|
532 | map.Add((ULong_t)map.GetSize(), (Long_t)tm, (Long_t)nblocks);
|
---|
533 |
|
---|
534 | stop = last;
|
---|
535 | nblocks++;
|
---|
536 | if (debug)
|
---|
537 | cout << "Cal Block #" << nblocks << " from " << start << " to " << last << endl;
|
---|
538 | }
|
---|
539 | status = UNKNOWN;
|
---|
540 | break;
|
---|
541 | }
|
---|
542 | last = atoi((*row)[0]);
|
---|
543 | lasttime.SetSqlDateTime((*row)[3]);
|
---|
544 | }
|
---|
545 | if (status==CAL)
|
---|
546 | {
|
---|
547 | stop = last;
|
---|
548 | nblocks++;
|
---|
549 | if (debug)
|
---|
550 | cout << "Cal Block #" << nblocks << " from " << start << " to " << stop << endl;
|
---|
551 | }
|
---|
552 |
|
---|
553 | if (debug)
|
---|
554 | cout << "Last Run: " << last << endl;
|
---|
555 | delete res;
|
---|
556 |
|
---|
557 | if (debug)
|
---|
558 | cout << "Found " << nblocks << " calibration blocks" << endl;
|
---|
559 |
|
---|
560 | res = serv.Query(query);
|
---|
561 | if (!res)
|
---|
562 | return kFALSE;
|
---|
563 |
|
---|
564 | Int_t n = -1;
|
---|
565 |
|
---|
566 | Bool_t rc = kTRUE;
|
---|
567 |
|
---|
568 | start = first;
|
---|
569 | while ((row=res->Next()))
|
---|
570 | {
|
---|
571 | if (!(*row)[1])
|
---|
572 | continue;
|
---|
573 |
|
---|
574 | MTime tstart, tstop;
|
---|
575 | tstart.SetSqlDateTime((*row)[2]);
|
---|
576 | tstop.SetSqlDateTime((*row)[3]);
|
---|
577 |
|
---|
578 | MTime min;
|
---|
579 | Int_t nmin = -1;
|
---|
580 | Double_t dmin = 1e35;
|
---|
581 |
|
---|
582 | Long_t key, val;
|
---|
583 | TExMapIter nmap(&map);
|
---|
584 | while (nmap.Next(key, val))
|
---|
585 | {
|
---|
586 | MTime *t = (MTime*)key;
|
---|
587 |
|
---|
588 | if (nmin==-1)
|
---|
589 | {
|
---|
590 | nmin = val;
|
---|
591 | min = *(MTime*)key;
|
---|
592 | dmin = fabs((Double_t)*t-(Double_t)tstart);
|
---|
593 | }
|
---|
594 |
|
---|
595 | if (fabs((Double_t)*t-(Double_t)tstart) < dmin)
|
---|
596 | {
|
---|
597 | nmin = val;
|
---|
598 | dmin = fabs((Double_t)*t-(Double_t)tstart);
|
---|
599 | min = *t;
|
---|
600 | }
|
---|
601 | if (fabs((Double_t)*t-(Double_t)tstop) < dmin)
|
---|
602 | {
|
---|
603 | nmin = val;
|
---|
604 | dmin = fabs((Double_t)*t-(Double_t)tstop);
|
---|
605 | min = *t;
|
---|
606 | }
|
---|
607 | }
|
---|
608 |
|
---|
609 | if (n!=nmin)
|
---|
610 | {
|
---|
611 | if (n!=-1)
|
---|
612 | {
|
---|
613 | if (!NewSequence(serv, datapath, sequpath, start, last, dummy))
|
---|
614 | {
|
---|
615 | rc = kFALSE;
|
---|
616 | //continue;
|
---|
617 | }
|
---|
618 | }
|
---|
619 | n = nmin;
|
---|
620 | start = atoi((*row)[0]);
|
---|
621 | }
|
---|
622 | last = atoi((*row)[0]);
|
---|
623 | }
|
---|
624 |
|
---|
625 | delete res;
|
---|
626 |
|
---|
627 | if (n!=-1 && start!=last)
|
---|
628 | {
|
---|
629 | if (!NewSequence(serv, datapath, sequpath, start, last, dummy))
|
---|
630 | rc = kFALSE;
|
---|
631 | }
|
---|
632 |
|
---|
633 | if (debug)
|
---|
634 | cout << endl;
|
---|
635 |
|
---|
636 | return rc;
|
---|
637 | }
|
---|
638 |
|
---|
639 |
|
---|
640 |
|
---|
641 | int buildsequenceentries(TString day, TString datapath, TString sequpath, Bool_t dummy=kTRUE)
|
---|
642 | {
|
---|
643 | TEnv env("sql.rc");
|
---|
644 |
|
---|
645 | MSQLServer serv(env);
|
---|
646 | if (!serv.IsConnected())
|
---|
647 | {
|
---|
648 | cout << "ERROR - Connection to database failed." << endl;
|
---|
649 | return 0;
|
---|
650 | }
|
---|
651 |
|
---|
652 | cout << "buildsequences" << endl;
|
---|
653 | cout << "--------------" << endl;
|
---|
654 | cout << endl;
|
---|
655 | cout << "Connected to " << serv.GetName() << endl;
|
---|
656 | cout << "Night of sunrise at: " << day << endl;
|
---|
657 | cout << endl;
|
---|
658 |
|
---|
659 | day += " 13:00:00";
|
---|
660 | const TString cond(Form("(fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
|
---|
661 | day.Data(), day.Data()));
|
---|
662 |
|
---|
663 | TString query(Form("SELECT fRunNumber, fSourceKEY, fProjectKEY, fHvSettingsKEY, fLightConditionsKEY, fDiscriminatorThresholdTableKEY, fTriggerDelayTableKEY FROM RunData WHERE %s AND fExcludedFDAKEY=1 order by fRunNumber", cond.Data()));
|
---|
664 |
|
---|
665 | TSQLResult *res = serv.Query(query);
|
---|
666 | if (!res)
|
---|
667 | return 0;
|
---|
668 |
|
---|
669 | TString keys[6]= { "NULL", "NULL", "NULL", "NULL", "NULL", "NULL" };
|
---|
670 | TString stop = "NULL";
|
---|
671 | TString runstart = "NULL";
|
---|
672 | TString runstop = "NULL";
|
---|
673 | Int_t count = 0;
|
---|
674 | TExMap blocks;
|
---|
675 | Int_t runbegin;
|
---|
676 | Int_t runend;
|
---|
677 |
|
---|
678 | TSQLRow *row=0;
|
---|
679 | while ((row=res->Next()))
|
---|
680 | {
|
---|
681 | if (count==0)
|
---|
682 | {
|
---|
683 | for (Int_t i=1 ; i<7 ; i++)
|
---|
684 | keys[i-1]=(*row)[i];
|
---|
685 | runstart=(*row)[0];
|
---|
686 | }
|
---|
687 |
|
---|
688 | for (Int_t i=1 ; i<7 ; i++)
|
---|
689 | {
|
---|
690 | runbegin=atoi(runstart.Data());
|
---|
691 | runend=atoi(runstop.Data());
|
---|
692 | if (i==2 && runbegin>20100 && runend<45100)
|
---|
693 | continue;
|
---|
694 |
|
---|
695 | TString value=(*row)[i];
|
---|
696 | TString key=keys[i-1];
|
---|
697 | if (!value.CompareTo(key))
|
---|
698 | continue;
|
---|
699 |
|
---|
700 | keys[i-1]=value;
|
---|
701 | //hier einfuellen
|
---|
702 | blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
|
---|
703 | runstart=(*row)[0];
|
---|
704 | for (Int_t i=1 ; i<7 ; i++)
|
---|
705 | keys[i-1]=(*row)[i];
|
---|
706 | break;
|
---|
707 | }
|
---|
708 | runstop=(*row)[0];
|
---|
709 | count++;
|
---|
710 | }
|
---|
711 |
|
---|
712 | //und hier einfuellen (letzter wert)
|
---|
713 | runbegin=atoi(runstart.Data());
|
---|
714 | runend=atoi(runstop.Data());
|
---|
715 | blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
|
---|
716 |
|
---|
717 |
|
---|
718 | Bool_t rc = kTRUE;
|
---|
719 |
|
---|
720 | Long_t key, val;
|
---|
721 | TExMapIter nblocks(&blocks);
|
---|
722 | while (nblocks.Next(key, val))
|
---|
723 | {
|
---|
724 | Int_t runstart2 = (Int_t)key;
|
---|
725 | Int_t runstop2 = (Int_t)val;
|
---|
726 | cout << endl << "datablock from " << runstart2 << " to " << runstop2 << endl;
|
---|
727 |
|
---|
728 | if (!Process(serv, datapath, sequpath, runstart2, runstop2, dummy))
|
---|
729 | rc = kFALSE;
|
---|
730 |
|
---|
731 | }
|
---|
732 | return rc ? 1 : 0;
|
---|
733 | }
|
---|
734 |
|
---|
735 |
|
---|
736 | int buildsequenceentries(TString datapath, TString sequpath, Bool_t dummy=kTRUE)
|
---|
737 | {
|
---|
738 | TEnv env("sql.rc");
|
---|
739 |
|
---|
740 | MSQLServer serv(env);
|
---|
741 | if (!serv.IsConnected())
|
---|
742 | {
|
---|
743 | cout << "ERROR - Connection to database failed." << endl;
|
---|
744 | return 0;
|
---|
745 | }
|
---|
746 |
|
---|
747 | TString query="SELECT fDate FROM SequenceBuildStatus";
|
---|
748 |
|
---|
749 | TSQLResult *res = serv.Query(query);
|
---|
750 | if (!res)
|
---|
751 | return 0;
|
---|
752 |
|
---|
753 | TSQLRow *row=0;
|
---|
754 | while ((row=res->Next()))
|
---|
755 | {
|
---|
756 | cout << "date: " << (*row)[0] << endl;
|
---|
757 | buildsequenceentries((*row)[0], dummy);
|
---|
758 | }
|
---|
759 |
|
---|
760 | return 1;
|
---|
761 | }
|
---|