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 | #include <TObjString.h>
|
---|
55 |
|
---|
56 | #include <MTime.h>
|
---|
57 | #include <MDirIter.h>
|
---|
58 |
|
---|
59 | using namespace std;
|
---|
60 |
|
---|
61 | int debug = 0;
|
---|
62 |
|
---|
63 | Bool_t DeleteSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t sequ, Bool_t dummy)
|
---|
64 | {
|
---|
65 | TString query1(Form("DELETE FROM Calibration WHERE fSequenceFirst=%d", sequ));
|
---|
66 | TString query2(Form("DELETE FROM Star WHERE fSequenceFirst=%d", sequ));
|
---|
67 | TString query3(Form("DELETE FROM SequenceProcessStatus WHERE fSequenceFirst=%d", sequ));
|
---|
68 | TString query4(Form("UPDATE RunData SET fSequenceFirst=0 WHERE fSequenceFirst=%d", sequ));
|
---|
69 | TString query5(Form("DELETE FROM Sequences WHERE fSequenceFirst=%d AND fManuallyChangedKEY=1", sequ));
|
---|
70 |
|
---|
71 | TString fname(Form("%s/%04d/sequence%08d.txt", sequpath.Data(),sequ/10000, sequ));
|
---|
72 | TString command(Form("rm -r %s/callisto/%04d/%08d/", datapath.Data(), sequ/10000, sequ));
|
---|
73 | TString command2(Form("rm -r %s/star/%04d/%08d/", datapath.Data(), sequ/10000, sequ));
|
---|
74 |
|
---|
75 | if (dummy)
|
---|
76 | {
|
---|
77 | cout << "not using dummy=kTRUE the following commands would be executed: " << endl;
|
---|
78 | cout << "queries: " << endl;
|
---|
79 | cout << query1 << endl;
|
---|
80 | cout << query2 << endl;
|
---|
81 | cout << query3 << endl;
|
---|
82 | cout << query4 << endl;
|
---|
83 | cout << query5 << endl;
|
---|
84 | cout << "removing files:" << endl;
|
---|
85 | cout << "unlink " << fname << endl;
|
---|
86 | cout << command << endl;
|
---|
87 | cout << command2 << endl;
|
---|
88 | return kTRUE;
|
---|
89 | }
|
---|
90 |
|
---|
91 | TSQLResult *res = serv.Query(query1);
|
---|
92 | if (!res)
|
---|
93 | return kFALSE;
|
---|
94 | delete res;
|
---|
95 |
|
---|
96 | res = serv.Query(query2);
|
---|
97 | if (!res)
|
---|
98 | return kFALSE;
|
---|
99 | delete res;
|
---|
100 |
|
---|
101 | res = serv.Query(query3);
|
---|
102 | if (!res)
|
---|
103 | return kFALSE;
|
---|
104 | delete res;
|
---|
105 |
|
---|
106 | res = serv.Query(query4);
|
---|
107 | if (!res)
|
---|
108 | return kFALSE;
|
---|
109 | delete res;
|
---|
110 |
|
---|
111 | res = serv.Query(query5);
|
---|
112 | if (!res)
|
---|
113 | return kFALSE;
|
---|
114 | delete res;
|
---|
115 |
|
---|
116 | gSystem->Unlink(fname);
|
---|
117 |
|
---|
118 | gSystem->Exec(command);
|
---|
119 | gSystem->Exec(command2);
|
---|
120 |
|
---|
121 | return kTRUE;
|
---|
122 | }
|
---|
123 |
|
---|
124 | Int_t DoCheck(TSQLResult &res)
|
---|
125 | {
|
---|
126 | TArrayI data(5);
|
---|
127 | Int_t n = 0;
|
---|
128 |
|
---|
129 | TSQLRow *row=0;
|
---|
130 | while ((row=res.Next()))
|
---|
131 | {
|
---|
132 | n++;
|
---|
133 |
|
---|
134 | if (data[0]==0)
|
---|
135 | {
|
---|
136 | for (int i=0; i<data.GetSize(); i++)
|
---|
137 | data[i] = atoi((*row)[i]);
|
---|
138 | continue;
|
---|
139 | }
|
---|
140 |
|
---|
141 | for (int i=1; i<data.GetSize(); i++)
|
---|
142 | {
|
---|
143 | if (data[i] != atoi((*row)[i]))
|
---|
144 | return i+1;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | return n==0 ? 0 : -1;
|
---|
148 | }
|
---|
149 |
|
---|
150 | Bool_t CheckRuns(MSQLServer &serv, Int_t from, Int_t to, Int_t type)
|
---|
151 | {
|
---|
152 | TString query("SELECT fRunNumber, fL1TriggerTableKEY, fL2TriggerTableKEY,"
|
---|
153 | " fCalibrationScriptKEY, fProjectKEY FROM RunData");
|
---|
154 | query += Form(" WHERE fRunTypeKEY=%d AND fExcludedFDAKEY=1 AND "
|
---|
155 | " (fRunNumber BETWEEN %d AND %d)"
|
---|
156 | " ORDER BY fRunNumber", type, from, to);
|
---|
157 |
|
---|
158 | TSQLResult *res = serv.Query(query);
|
---|
159 | if (!res)
|
---|
160 | return kFALSE;
|
---|
161 |
|
---|
162 | Int_t rc = DoCheck(*res);
|
---|
163 | delete res;
|
---|
164 |
|
---|
165 | switch (rc)
|
---|
166 | {
|
---|
167 | case 0: cout << "ERROR - No runs found for check!" << endl; break;
|
---|
168 | case 1: cout << "ERROR - fRunNumber doesn't match!" << endl; break;
|
---|
169 | case 2: cout << "ERROR - fL1TriggerTableKEY doesn't match!" << endl; break;
|
---|
170 | case 3: cout << "ERROR - fL2TriggerTableKEY doesn't match!" << endl; break;
|
---|
171 | case 4: cout << "ERROR - fCalibrationScriptKEY doesn't match!" << endl; break;
|
---|
172 | case 5: cout << "ERROR - fProjectKEY doesn't match!" << endl; break;
|
---|
173 | }
|
---|
174 |
|
---|
175 | return rc<0;
|
---|
176 | }
|
---|
177 |
|
---|
178 | Int_t CheckSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, Bool_t dummy)
|
---|
179 | {
|
---|
180 | Int_t rc=0;
|
---|
181 | //rc=0 means sequence not found -> insert
|
---|
182 | //rc=1 means deleting sequence(s) worked -> insert
|
---|
183 | //rc=2 means sequence is still the same -> insert not neccessary
|
---|
184 | //if deleting sequence doesn't work -> return -1
|
---|
185 |
|
---|
186 |
|
---|
187 | //getting # of sequence (in sequDB) between from and to
|
---|
188 | TString query("SELECT fSequenceFirst FROM Sequences WHERE (fSequenceFirst ");
|
---|
189 | query += Form("BETWEEN %d and %d OR fSequenceLast BETWEEN %d and %d) AND "
|
---|
190 | "fManuallyChangedKEY=1 ", from, to, from, to);
|
---|
191 |
|
---|
192 | TSQLResult *res = serv.Query(query);
|
---|
193 | if (!res)
|
---|
194 | return -1;
|
---|
195 |
|
---|
196 | TArrayI sequences;
|
---|
197 | Int_t numsequ=0;
|
---|
198 |
|
---|
199 | TSQLRow *row=0;
|
---|
200 | while ((row=res->Next()))
|
---|
201 | {
|
---|
202 | numsequ++;
|
---|
203 | sequences.Set(numsequ);
|
---|
204 | sequences.AddAt(atoi((*row)[0]), numsequ-1);
|
---|
205 | }
|
---|
206 | delete res;
|
---|
207 |
|
---|
208 | //if there's no sequence in the table Sequences -> check other tables
|
---|
209 | //if there's one sequence -> check if the sequence is identical
|
---|
210 | //if there are more sequences -> delete them
|
---|
211 | switch (numsequ)
|
---|
212 | {
|
---|
213 | case 0:
|
---|
214 | //FIXME: like this the check is of no use, but when doing it
|
---|
215 | // without the check for manuallychanged, all manually
|
---|
216 | // changed sequences would be deleted
|
---|
217 | cout << "found no sequence in Sequ-DB -> check other tables" << endl;
|
---|
218 | cout << " deleting every sequence found in Calibration, Star or SequenceProcessStatus between "
|
---|
219 | << from << " and " << to << endl;
|
---|
220 |
|
---|
221 | //calibration table
|
---|
222 | query=Form("SELECT Calibration.fSequenceFirst FROM Calibration "
|
---|
223 | " LEFT JOIN Sequences ON Calibration.fSequenceFirst=Sequences.fSequenceFirst "
|
---|
224 | " WHERE fManuallyChangedKEY=1 AND Calibration.fSequenceFirst BETWEEN %d and %d",
|
---|
225 | from, to);
|
---|
226 | res = serv.Query(query);
|
---|
227 | if (!res)
|
---|
228 | return -1;
|
---|
229 | row=0;
|
---|
230 | while ((row=res->Next()))
|
---|
231 | {
|
---|
232 | if (!DeleteSequence(serv, datapath, sequpath, atoi((*row)[0]), dummy))
|
---|
233 | return -1;
|
---|
234 | rc=1;
|
---|
235 | }
|
---|
236 | delete res;
|
---|
237 |
|
---|
238 | //Star table
|
---|
239 | query=Form("SELECT Star.fSequenceFirst FROM Star "
|
---|
240 | " LEFT JOIN Sequences ON Star.fSequenceFirst=Sequences.fSequenceFirst "
|
---|
241 | " WHERE fManuallyChangedKEY=1 AND Star.fSequenceFirst BETWEEN %d and %d",
|
---|
242 | from, to);
|
---|
243 | res = serv.Query(query);
|
---|
244 | if (!res)
|
---|
245 | return -1;
|
---|
246 | row=0;
|
---|
247 | while ((row=res->Next()))
|
---|
248 | {
|
---|
249 | if (!DeleteSequence(serv, datapath, sequpath, atoi((*row)[0]), dummy))
|
---|
250 | return -1;
|
---|
251 | rc=1;
|
---|
252 | }
|
---|
253 | delete res;
|
---|
254 |
|
---|
255 | //SequenceProcessStatus table
|
---|
256 | query=Form("SELECT SequenceProcessStatus.fSequenceFirst FROM SequenceProcessStatus "
|
---|
257 | " LEFT JOIN Sequences ON SequenceProcessStatus.fSequenceFirst=Sequences.fSequenceFirst "
|
---|
258 | " WHERE fManuallyChangedKEY=1 AND SequenceProcessStatus.fSequenceFirst BETWEEN %d and %d",
|
---|
259 | from, to);
|
---|
260 | res = serv.Query(query);
|
---|
261 | if (!res)
|
---|
262 | return -1;
|
---|
263 | row=0;
|
---|
264 | while ((row=res->Next()))
|
---|
265 | {
|
---|
266 | if (!DeleteSequence(serv, datapath, sequpath, atoi((*row)[0]), dummy))
|
---|
267 | return -1;
|
---|
268 | rc=1;
|
---|
269 | }
|
---|
270 | delete res;
|
---|
271 | break;
|
---|
272 |
|
---|
273 | case 1:
|
---|
274 | cout << "found 1 sequence: " << sequences.At(0) << " -> check sequ# " << endl;
|
---|
275 | if (sequences.At(0)!=from)
|
---|
276 | {
|
---|
277 | if (!DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy))
|
---|
278 | return -1;
|
---|
279 | rc=1;
|
---|
280 | }
|
---|
281 | else
|
---|
282 | {
|
---|
283 | cout << "sequence# is the same -> checking the runs " << endl;
|
---|
284 |
|
---|
285 | //getting olf runs
|
---|
286 | query=Form("SELECT fRunNumber FROM RunData WHERE fSequenceFirst=%d ", from);
|
---|
287 | res = serv.Query(query);
|
---|
288 | if (!res)
|
---|
289 | return -1;
|
---|
290 |
|
---|
291 | TArrayI oldruns;
|
---|
292 | Int_t count=0;
|
---|
293 | row=0;
|
---|
294 | while ((row=res->Next()))
|
---|
295 | {
|
---|
296 | count++;
|
---|
297 | oldruns.Set(count);
|
---|
298 | oldruns.AddAt(atoi((*row)[0]), count-1);
|
---|
299 | }
|
---|
300 | delete res;
|
---|
301 |
|
---|
302 | //getting new runs
|
---|
303 | query=Form("SELECT fRunNumber FROM RunData WHERE fRunNumber BETWEEN %d and %d AND fExcludedFDAKEY=1", from, to);
|
---|
304 | res = serv.Query(query);
|
---|
305 | if (!res)
|
---|
306 | return -1;
|
---|
307 | TArrayI newruns;
|
---|
308 | count=0;
|
---|
309 | row=0;
|
---|
310 | while ((row=res->Next()))
|
---|
311 | {
|
---|
312 | count++;
|
---|
313 | newruns.Set(count);
|
---|
314 | newruns.AddAt(atoi((*row)[0]), count-1);
|
---|
315 | }
|
---|
316 | delete res;
|
---|
317 |
|
---|
318 | //comparing old and new runs (first the # of runs, if it is the same, also the single runnumbers
|
---|
319 | if (oldruns.GetSize()!=newruns.GetSize())
|
---|
320 | {
|
---|
321 | // cout << " number of runs (" << oldruns.GetSize() << " - " << newruns.GetSize()
|
---|
322 | // << ") is not the same -> deleting sequence " << sequences.At(0) << endl;
|
---|
323 | if (!DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy))
|
---|
324 | return -1;
|
---|
325 | rc=1;
|
---|
326 | }
|
---|
327 | else
|
---|
328 | {
|
---|
329 | cout << " number of runs is the same -> checking the single runnumbers " << endl;
|
---|
330 |
|
---|
331 | for (Int_t i=0;i<newruns.GetSize();i++)
|
---|
332 | {
|
---|
333 | // cout << "i: " << i << " - new: " << newruns.At(i) << " - old: " << oldruns.At(i) << endl;
|
---|
334 | if (newruns.At(i)==oldruns.At(i))
|
---|
335 | continue;
|
---|
336 |
|
---|
337 | cout << i << ". run is not the same ( " << oldruns.At(i) << " -- " << newruns.At(i)
|
---|
338 | << ") -> deleting sequence " << sequences.At(0) << endl;
|
---|
339 | if (!DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy))
|
---|
340 | return -1;
|
---|
341 | rc=1;
|
---|
342 | break;
|
---|
343 | }
|
---|
344 | }
|
---|
345 | }
|
---|
346 | rc=2;
|
---|
347 | break;
|
---|
348 |
|
---|
349 | default:
|
---|
350 | cout << "found " << numsequ << " sequences -> deleting them " << endl;
|
---|
351 |
|
---|
352 | for (Int_t i=0;i<sequences.GetSize();i++)
|
---|
353 | {
|
---|
354 | cout << "deleting sequence " << sequences.At(i) << "... <" << i << ">" << endl;
|
---|
355 | if (!DeleteSequence(serv, datapath, sequpath, sequences.At(i), dummy))
|
---|
356 | return -1;
|
---|
357 | rc=1;
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | return rc;
|
---|
362 | }
|
---|
363 |
|
---|
364 | Bool_t InsertSequence(MSQLServer &serv, Int_t from, Int_t to)
|
---|
365 | {
|
---|
366 |
|
---|
367 | cout << "Inserting sequence " << from << " ... " << endl;
|
---|
368 |
|
---|
369 | // ========== Request number of events ==========
|
---|
370 | TString query("SELECT SUM(fNumEvents), "
|
---|
371 | " SUM(if(TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)<0,"
|
---|
372 | " TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)+24*60*60,"
|
---|
373 | " TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart))), ");
|
---|
374 | query += " MIN(fZenithDistance), MAX(fZenithDistance), ";
|
---|
375 | query += " MIN(fAzimuth), MAX(fAzimuth) ";
|
---|
376 | query += Form(" FROM RunData WHERE fRunTypeKEY=2 AND "
|
---|
377 | " (fRunNumber BETWEEN %d AND %d) AND fExcludedFDAKEY=1",
|
---|
378 | from, to);
|
---|
379 |
|
---|
380 | TSQLResult *res = serv.Query(query);
|
---|
381 | if (!res)
|
---|
382 | return kFALSE;
|
---|
383 |
|
---|
384 | TSQLRow *row = res->Next();
|
---|
385 | if (!row || !(*row)[0])
|
---|
386 | {
|
---|
387 | cout << "ERROR - No result from query: " << query << endl;
|
---|
388 | return kFALSE;
|
---|
389 | }
|
---|
390 |
|
---|
391 | TString nevts = (*row)[0];
|
---|
392 | TString secs = (*row)[1];
|
---|
393 | TString zdmin = (*row)[2];
|
---|
394 | TString zdmax = (*row)[3];
|
---|
395 | TString azmin = (*row)[4];
|
---|
396 | TString azmax = (*row)[5];
|
---|
397 |
|
---|
398 | delete res;
|
---|
399 |
|
---|
400 | // ========== Request start time of sequence ==========
|
---|
401 | query = Form("SELECT fRunStart FROM RunData WHERE fRunNumber=%d AND fExcludedFDAKEY=1", from);
|
---|
402 |
|
---|
403 | res = serv.Query(query);
|
---|
404 | if (!res)
|
---|
405 | return kFALSE;
|
---|
406 |
|
---|
407 | row = res->Next();
|
---|
408 | if (!row || !(*row)[0])
|
---|
409 | {
|
---|
410 | cout << "ERROR - No result from query: " << query << endl;
|
---|
411 | return kFALSE;
|
---|
412 | }
|
---|
413 |
|
---|
414 | TString start((*row)[0]);
|
---|
415 |
|
---|
416 | delete res;
|
---|
417 |
|
---|
418 | // ========== Request data of sequence ==========
|
---|
419 | query = Form("SELECT fSourceKEY, fProjectKEY, "
|
---|
420 | " fL1TriggerTableKEY, fL2TriggerTableKEY,"
|
---|
421 | " fHvSettingsKEY, fDiscriminatorThresholdTableKEY,"
|
---|
422 | " fTriggerDelayTableKEY, fLightConditionsKEY, fTestFlagKEY"
|
---|
423 | " FROM RunData"
|
---|
424 | " WHERE fRunTypeKEY=2 AND fExcludedFDAKEY=1 AND (fRunNumber BETWEEN %d AND %d)"
|
---|
425 | " LIMIT 1", from, to);
|
---|
426 |
|
---|
427 | res = serv.Query(query);
|
---|
428 | if (!res)
|
---|
429 | return kFALSE;
|
---|
430 |
|
---|
431 | row = res->Next();
|
---|
432 | if (!row)
|
---|
433 | {
|
---|
434 | cout << "ERROR - No result from query: " << query << endl;
|
---|
435 | return kFALSE;
|
---|
436 | }
|
---|
437 |
|
---|
438 | TString query1("INSERT Sequences SET");
|
---|
439 | query1+=Form(" fSequenceFirst=%d, fSequenceLast=%d,", from, to);
|
---|
440 | query1+=Form(" fSourceKEY=%s,", (*row)[0]);
|
---|
441 | query1+=Form(" fProjectKEY=%s,", (*row)[1]);
|
---|
442 | query1+=Form(" fNumEvents=%s,", nevts.Data());
|
---|
443 | query1+=Form(" fRunTime=%s,", secs.Data());
|
---|
444 | query1+=Form(" fRunStart=\"%s\",", start.Data());
|
---|
445 | query1+=Form(" fZenithDistanceMin=%s,", zdmin.Data());
|
---|
446 | query1+=Form(" fZenithDistanceMax=%s,", zdmax.Data());
|
---|
447 | query1+=Form(" fAzimuthMin=%s,", azmin.Data());
|
---|
448 | query1+=Form(" fAzimuthMax=%s,", azmax.Data());
|
---|
449 | query1+=Form(" fL1TriggerTableKEY=%s,", (*row)[2]);
|
---|
450 | query1+=Form(" fL2TriggerTableKEY=%s,", (*row)[3]);
|
---|
451 | query1+=Form(" fHvSettingsKEY=%s,", (*row)[4]);
|
---|
452 | query1+=Form(" fDiscriminatorThresholdTableKEY=%s,", (*row)[5]);
|
---|
453 | query1+=Form(" fTriggerDelayTableKEY=%s,", (*row)[6]);
|
---|
454 | query1+=Form(" fLightConditionsKEY=%s,", (*row)[7]);
|
---|
455 | query1+=Form(" fTestFlagKEY=%s, fManuallyChangedKEY=1", (*row)[8]);
|
---|
456 |
|
---|
457 |
|
---|
458 | TString query2 = Form("UPDATE RunData SET fSequenceFirst=%d WHERE"
|
---|
459 | " (fRunNumber BETWEEN %d AND %d) AND"
|
---|
460 | " (fRunTypeKEY BETWEEN 2 AND 4) AND"
|
---|
461 | " fSourceKEY=%s AND fHvSettingsKEY=%s AND fExcludedFDAKEY=1",
|
---|
462 | from, from, to, (*row)[0], (*row)[4]);
|
---|
463 |
|
---|
464 | TString query3 = Form("INSERT SequenceProcessStatus SET fSequenceFirst=%d ", from);
|
---|
465 |
|
---|
466 | delete res;
|
---|
467 |
|
---|
468 | cout << "q1: " << query1 << endl;
|
---|
469 | cout << "q2: " << query2 << endl;
|
---|
470 | cout << "q3: " << query3 << endl;
|
---|
471 |
|
---|
472 | res = serv.Query(query1);
|
---|
473 | if (!res)
|
---|
474 | {
|
---|
475 | cout << "ERROR - Could not insert Sequence into Sequences." << endl;
|
---|
476 | return kFALSE;
|
---|
477 | }
|
---|
478 | delete res;
|
---|
479 |
|
---|
480 | res = serv.Query(query2);
|
---|
481 | if (!res)
|
---|
482 | {
|
---|
483 | cout << "ERROR - Could not update RunData." << endl;
|
---|
484 | return kFALSE;
|
---|
485 | }
|
---|
486 | delete res;
|
---|
487 |
|
---|
488 | res = serv.Query(query3);
|
---|
489 | if (!res)
|
---|
490 | {
|
---|
491 | cout << "ERROR - Could not insert Sequence into SequenceProcessStatus." << endl;
|
---|
492 | return kFALSE;
|
---|
493 | }
|
---|
494 | delete res;
|
---|
495 |
|
---|
496 | return kTRUE;
|
---|
497 | }
|
---|
498 |
|
---|
499 | Bool_t NewSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, TList &sequlist, Bool_t dummy)
|
---|
500 | {
|
---|
501 | cout << "Found Sequence (" << from << ", " << to << ") ... checking runs..." << flush;
|
---|
502 |
|
---|
503 | if (!CheckRuns(serv, from, to, 2))
|
---|
504 | {
|
---|
505 | cout << "Warning - Found inconsistency in data-runs (" << from << ", " << to << ")" << endl;
|
---|
506 | //sequence is not built, but kTRUE is returned, to allow
|
---|
507 | //the automatic processing of the other sequences of this day
|
---|
508 | return kTRUE;
|
---|
509 | }
|
---|
510 | if (!CheckRuns(serv, from, to, 3))
|
---|
511 | {
|
---|
512 | cout << "Warning - Found inconsistency in ped-runs (" << from << ", " << to << ")" << endl;
|
---|
513 | //sequence is not built, but kTRUE is returned, to allow
|
---|
514 | //the automatic processing of the other sequences of this day
|
---|
515 | return kTRUE;
|
---|
516 | }
|
---|
517 |
|
---|
518 | cout << "ok." << endl;
|
---|
519 |
|
---|
520 |
|
---|
521 | cout << "checking Sequence..." << endl;
|
---|
522 |
|
---|
523 | TObject *sequ;
|
---|
524 | Bool_t rc=kFALSE;
|
---|
525 | switch (CheckSequence(serv, datapath, sequpath, from, to, dummy))
|
---|
526 | {
|
---|
527 | case 0:
|
---|
528 | cout << " sequence not found -> inserting " << from << flush ;
|
---|
529 | if (dummy)
|
---|
530 | {
|
---|
531 | cout << " <dummy> " << endl;
|
---|
532 | return kTRUE;
|
---|
533 | }
|
---|
534 | cout << endl;
|
---|
535 | if ((sequ=sequlist.Remove(sequlist.FindObject(Form("%d", from)))))
|
---|
536 | delete sequ;
|
---|
537 | return InsertSequence(serv, from, to);
|
---|
538 |
|
---|
539 | case 1:
|
---|
540 | cout << " deleting successfully finished -> inserting sequence " << from << flush;
|
---|
541 | if (dummy)
|
---|
542 | {
|
---|
543 | cout << " <dummy> " << endl;
|
---|
544 | return kTRUE;
|
---|
545 | }
|
---|
546 | cout << endl;
|
---|
547 | if ((sequ=sequlist.Remove(sequlist.FindObject(Form("%d", from)))))
|
---|
548 | delete sequ;
|
---|
549 | return InsertSequence(serv, from, to);
|
---|
550 |
|
---|
551 | case 2:
|
---|
552 | cout << " sequence " << from << " is already existing -> inserting not necessary" << endl;
|
---|
553 | if ((sequ=sequlist.Remove(sequlist.FindObject(Form("%d", from)))))
|
---|
554 | delete sequ;
|
---|
555 | return kTRUE;
|
---|
556 |
|
---|
557 | case -1:
|
---|
558 | cout << " deleting went wrong " << endl;
|
---|
559 | return kFALSE;
|
---|
560 | }
|
---|
561 |
|
---|
562 |
|
---|
563 | return rc;
|
---|
564 | }
|
---|
565 |
|
---|
566 | Bool_t Process(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, TList &sequlist, Bool_t dummy)
|
---|
567 | {
|
---|
568 |
|
---|
569 | TString query(Form("SELECT fRunNumber, fRunTypeKEY, fRunStart, fRunStop"
|
---|
570 | " FROM RunData"
|
---|
571 | " WHERE fRunNumber BETWEEN %d AND %d AND "
|
---|
572 | " fExcludedFDAKEY=1 AND (fRunTypeKEY BETWEEN 2 AND 4)"
|
---|
573 | " ORDER BY fRunNumber", from, to));
|
---|
574 |
|
---|
575 | TSQLResult *res = serv.Query(query);
|
---|
576 | if (!res)
|
---|
577 | return kFALSE;
|
---|
578 |
|
---|
579 | TExMap map;
|
---|
580 |
|
---|
581 | Int_t start=0;
|
---|
582 | Int_t stop=0;
|
---|
583 | Int_t last=0;
|
---|
584 | Int_t first=0;
|
---|
585 |
|
---|
586 | MTime lasttime;
|
---|
587 |
|
---|
588 | TSQLRow *row=0;
|
---|
589 |
|
---|
590 | enum { UNKNOWN, PED=3, CAL=4, DATA=2 };
|
---|
591 | Char_t status = UNKNOWN;
|
---|
592 |
|
---|
593 | Int_t nblocks = 0;
|
---|
594 |
|
---|
595 | while ((row=res->Next()))
|
---|
596 | {
|
---|
597 | if (!(*row)[1])
|
---|
598 | continue;
|
---|
599 |
|
---|
600 | if (start==0)
|
---|
601 | {
|
---|
602 | first = atoi((*row)[0]);
|
---|
603 | if (debug)
|
---|
604 | cout << "First Run: " << first << endl;
|
---|
605 | }
|
---|
606 |
|
---|
607 | switch (atoi((*row)[1]))
|
---|
608 | {
|
---|
609 | case CAL: // ---------- CALIBRATION ----------
|
---|
610 | if (status!=CAL)
|
---|
611 | {
|
---|
612 | start = stop = atoi((*row)[0]);
|
---|
613 | if (!(*row)[2])
|
---|
614 | cout << "No time available... skipped." << endl;
|
---|
615 | else
|
---|
616 | {
|
---|
617 | MTime *tm = new MTime;
|
---|
618 | tm->SetSqlDateTime((*row)[2]);
|
---|
619 | map.Add((ULong_t)map.GetSize(), (Long_t)tm, (Long_t)nblocks);
|
---|
620 | }
|
---|
621 | }
|
---|
622 | status = CAL;
|
---|
623 | break;
|
---|
624 | default:
|
---|
625 | if (status==CAL)
|
---|
626 | {
|
---|
627 | MTime *tm = new MTime(lasttime);
|
---|
628 | map.Add((ULong_t)map.GetSize(), (Long_t)tm, (Long_t)nblocks);
|
---|
629 |
|
---|
630 | stop = last;
|
---|
631 | nblocks++;
|
---|
632 | if (debug)
|
---|
633 | cout << "Cal Block #" << nblocks << " from " << start << " to " << last << endl;
|
---|
634 | }
|
---|
635 | status = UNKNOWN;
|
---|
636 | break;
|
---|
637 | }
|
---|
638 | last = atoi((*row)[0]);
|
---|
639 | lasttime.SetSqlDateTime((*row)[3]);
|
---|
640 | }
|
---|
641 | if (status==CAL)
|
---|
642 | {
|
---|
643 | stop = last;
|
---|
644 | nblocks++;
|
---|
645 | if (debug)
|
---|
646 | cout << "Cal Block #" << nblocks << " from " << start << " to " << stop << endl;
|
---|
647 | }
|
---|
648 |
|
---|
649 | if (debug)
|
---|
650 | cout << "Last Run: " << last << endl;
|
---|
651 | delete res;
|
---|
652 |
|
---|
653 | if (debug)
|
---|
654 | cout << "Found " << nblocks << " calibration blocks" << endl;
|
---|
655 |
|
---|
656 | res = serv.Query(query);
|
---|
657 | if (!res)
|
---|
658 | return kFALSE;
|
---|
659 |
|
---|
660 | Int_t n = -1;
|
---|
661 |
|
---|
662 | Bool_t rc = kTRUE;
|
---|
663 |
|
---|
664 | start = first;
|
---|
665 | while ((row=res->Next()))
|
---|
666 | {
|
---|
667 | if (!(*row)[1])
|
---|
668 | continue;
|
---|
669 |
|
---|
670 | MTime tstart, tstop;
|
---|
671 | tstart.SetSqlDateTime((*row)[2]);
|
---|
672 | tstop.SetSqlDateTime((*row)[3]);
|
---|
673 |
|
---|
674 | MTime min;
|
---|
675 | Int_t nmin = -1;
|
---|
676 | Double_t dmin = 1e35;
|
---|
677 |
|
---|
678 | Long_t key, val;
|
---|
679 | TExMapIter nmap(&map);
|
---|
680 | while (nmap.Next(key, val))
|
---|
681 | {
|
---|
682 | MTime *t = (MTime*)key;
|
---|
683 |
|
---|
684 | if (nmin==-1)
|
---|
685 | {
|
---|
686 | nmin = val;
|
---|
687 | min = *(MTime*)key;
|
---|
688 | dmin = fabs((Double_t)*t-(Double_t)tstart);
|
---|
689 | }
|
---|
690 |
|
---|
691 | if (fabs((Double_t)*t-(Double_t)tstart) < dmin)
|
---|
692 | {
|
---|
693 | nmin = val;
|
---|
694 | dmin = fabs((Double_t)*t-(Double_t)tstart);
|
---|
695 | min = *t;
|
---|
696 | }
|
---|
697 | if (fabs((Double_t)*t-(Double_t)tstop) < dmin)
|
---|
698 | {
|
---|
699 | nmin = val;
|
---|
700 | dmin = fabs((Double_t)*t-(Double_t)tstop);
|
---|
701 | min = *t;
|
---|
702 | }
|
---|
703 | }
|
---|
704 |
|
---|
705 | if (n!=nmin)
|
---|
706 | {
|
---|
707 | if (n!=-1)
|
---|
708 | {
|
---|
709 | if (!NewSequence(serv, datapath, sequpath, start, last, sequlist, dummy))
|
---|
710 | {
|
---|
711 | rc = kFALSE;
|
---|
712 | //continue;
|
---|
713 | }
|
---|
714 | }
|
---|
715 | n = nmin;
|
---|
716 | start = atoi((*row)[0]);
|
---|
717 | }
|
---|
718 | last = atoi((*row)[0]);
|
---|
719 | }
|
---|
720 |
|
---|
721 | delete res;
|
---|
722 |
|
---|
723 | if (n!=-1 && start!=last)
|
---|
724 | {
|
---|
725 | if (!NewSequence(serv, datapath, sequpath, start, last, sequlist, dummy))
|
---|
726 | rc = kFALSE;
|
---|
727 | }
|
---|
728 |
|
---|
729 | if (debug)
|
---|
730 | cout << endl;
|
---|
731 |
|
---|
732 | return rc;
|
---|
733 | }
|
---|
734 |
|
---|
735 |
|
---|
736 |
|
---|
737 | int buildsequenceentries(TString day, TString datapath, TString sequpath, Bool_t dummy=kTRUE)
|
---|
738 | {
|
---|
739 | TEnv env("sql.rc");
|
---|
740 |
|
---|
741 | MSQLServer serv(env);
|
---|
742 | if (!serv.IsConnected())
|
---|
743 | {
|
---|
744 | cout << "ERROR - Connection to database failed." << endl;
|
---|
745 | return 0;
|
---|
746 | }
|
---|
747 |
|
---|
748 | cout << "buildsequences" << endl;
|
---|
749 | cout << "--------------" << endl;
|
---|
750 | cout << endl;
|
---|
751 | cout << "Connected to " << serv.GetName() << endl;
|
---|
752 | cout << "Night of sunrise at: " << day << endl;
|
---|
753 | cout << endl;
|
---|
754 |
|
---|
755 | day += " 13:00:00";
|
---|
756 | const TString cond(Form("(fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
|
---|
757 | day.Data(), day.Data()));
|
---|
758 |
|
---|
759 | TString query(Form("SELECT fRunNumber, fSourceKEY, fProjectKEY, fHvSettingsKEY, fLightConditionsKEY, fDiscriminatorThresholdTableKEY, fTriggerDelayTableKEY FROM RunData WHERE %s AND fExcludedFDAKEY=1 order by fRunNumber", cond.Data()));
|
---|
760 |
|
---|
761 | TSQLResult *res = serv.Query(query);
|
---|
762 | if (!res)
|
---|
763 | return 0;
|
---|
764 |
|
---|
765 | TString keys[6]= { "NULL", "NULL", "NULL", "NULL", "NULL", "NULL" };
|
---|
766 | TString stop = "NULL";
|
---|
767 | TString runstart = "NULL";
|
---|
768 | TString runstop = "NULL";
|
---|
769 | Int_t count = 0;
|
---|
770 | TExMap blocks;
|
---|
771 | Int_t runbegin;
|
---|
772 | Int_t runend;
|
---|
773 |
|
---|
774 | TSQLRow *row=0;
|
---|
775 | while ((row=res->Next()))
|
---|
776 | {
|
---|
777 | if (count==0)
|
---|
778 | {
|
---|
779 | for (Int_t i=1 ; i<7 ; i++)
|
---|
780 | keys[i-1]=(*row)[i];
|
---|
781 | runstart=(*row)[0];
|
---|
782 | }
|
---|
783 |
|
---|
784 | for (Int_t i=1 ; i<7 ; i++)
|
---|
785 | {
|
---|
786 | runbegin=atoi(runstart.Data());
|
---|
787 | runend=atoi(runstop.Data());
|
---|
788 | if (i==2 && runbegin>20100 && runend<45100)
|
---|
789 | continue;
|
---|
790 |
|
---|
791 | TString value=(*row)[i];
|
---|
792 | TString key=keys[i-1];
|
---|
793 | if (!value.CompareTo(key))
|
---|
794 | continue;
|
---|
795 |
|
---|
796 | keys[i-1]=value;
|
---|
797 | //hier einfuellen
|
---|
798 | blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
|
---|
799 | runstart=(*row)[0];
|
---|
800 | for (Int_t i=1 ; i<7 ; i++)
|
---|
801 | keys[i-1]=(*row)[i];
|
---|
802 | break;
|
---|
803 | }
|
---|
804 | runstop=(*row)[0];
|
---|
805 | count++;
|
---|
806 | }
|
---|
807 |
|
---|
808 | //und hier einfuellen (letzter wert)
|
---|
809 | runbegin=atoi(runstart.Data());
|
---|
810 | runend=atoi(runstop.Data());
|
---|
811 | blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
|
---|
812 |
|
---|
813 |
|
---|
814 | TList sequlist;
|
---|
815 | query=Form("SELECT fSequenceFirst FROM Sequences WHERE fManuallyChangedKEY=1 AND %s order by fSequenceFirst",
|
---|
816 | cond.Data());
|
---|
817 |
|
---|
818 | // cout << "Q: " << query << endl;
|
---|
819 |
|
---|
820 | res = serv.Query(query);
|
---|
821 | if (!res)
|
---|
822 | return 0;
|
---|
823 |
|
---|
824 | cout << "old sequences: " << flush;
|
---|
825 | while ((row=res->Next()))
|
---|
826 | {
|
---|
827 | const char *str = (*row)[0];
|
---|
828 | cout << str << " " << flush;
|
---|
829 | sequlist.Add(new TObjString(str));
|
---|
830 | }
|
---|
831 | cout << endl;
|
---|
832 |
|
---|
833 | Bool_t rc = kTRUE;
|
---|
834 |
|
---|
835 | Long_t key, val;
|
---|
836 | TExMapIter nblocks(&blocks);
|
---|
837 | while (nblocks.Next(key, val))
|
---|
838 | {
|
---|
839 | Int_t runstart2 = (Int_t)key;
|
---|
840 | Int_t runstop2 = (Int_t)val;
|
---|
841 | cout << endl << "datablock from " << runstart2 << " to " << runstop2 << endl;
|
---|
842 |
|
---|
843 | if (!Process(serv, datapath, sequpath, runstart2, runstop2, sequlist, dummy))
|
---|
844 | rc = kFALSE;
|
---|
845 |
|
---|
846 | }
|
---|
847 |
|
---|
848 | TIter Next(&sequlist);
|
---|
849 | TObject *obj = 0;
|
---|
850 | while ((obj=Next()))
|
---|
851 | {
|
---|
852 | cout << "sequ: " << obj->GetName() << " deleting... " << endl;
|
---|
853 | if (!DeleteSequence(serv, datapath, sequpath, atoi(obj->GetName()), dummy))
|
---|
854 | return 0;
|
---|
855 | }
|
---|
856 |
|
---|
857 | return rc ? 1 : 0;
|
---|
858 | }
|
---|
859 |
|
---|
860 |
|
---|
861 | int buildsequenceentries(TString datapath, TString sequpath, Bool_t dummy=kTRUE)
|
---|
862 | {
|
---|
863 | TEnv env("sql.rc");
|
---|
864 |
|
---|
865 | MSQLServer serv(env);
|
---|
866 | if (!serv.IsConnected())
|
---|
867 | {
|
---|
868 | cout << "ERROR - Connection to database failed." << endl;
|
---|
869 | return 0;
|
---|
870 | }
|
---|
871 |
|
---|
872 | TString query="SELECT fDate FROM SequenceBuildStatus";
|
---|
873 |
|
---|
874 | TSQLResult *res = serv.Query(query);
|
---|
875 | if (!res)
|
---|
876 | return 0;
|
---|
877 |
|
---|
878 | TString date;
|
---|
879 | TSQLRow *row=0;
|
---|
880 | while ((row=res->Next()))
|
---|
881 | {
|
---|
882 | date=(*row)[0];
|
---|
883 | cout << "date: " << date << endl;
|
---|
884 | buildsequenceentries(date, datapath, sequpath, dummy);
|
---|
885 | }
|
---|
886 |
|
---|
887 | return 1;
|
---|
888 | }
|
---|