/[PAMELA software]/YodaProfiler/src/PamelaDBOperations.cpp
ViewVC logotype

Contents of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.26 - (show annotations) (download)
Wed Jan 31 16:15:03 2007 UTC (18 years, 1 month ago) by mocchiut
Branch: MAIN
CVS Tags: v3r00
Changes since 1.25: +6 -0 lines
V3R00 - add DB reconnection capability

1 //
2 #include <iomanip>
3 #include <sstream>
4 //
5 #include <iostream>
6 #include <string>
7 #include <fstream>
8 #include <list>
9 #include <errno.h>
10 //
11 #include <TFile.h>
12 #include <TSystem.h>
13 #include <TSQLResult.h>
14 #include <TSQLRow.h>
15 #include <TTree.h>
16 #include <TGraph.h>
17 #include <TTimeStamp.h>
18 #include <TF1.h>
19 //
20 #include <EventHeader.h>
21 #include <PscuHeader.h>
22 #include <mcmd/McmdEvent.h>
23 #include <mcmd/McmdRecord.h>
24 #include <RunHeaderEvent.h>
25 #include <RunTrailerEvent.h>
26 #include <CalibCalPedEvent.h>
27 #include <CalibS4Event.h>
28 #include <CalibTrk1Event.h>
29 #include <CalibTrk2Event.h>
30 #include <varDump/VarDumpEvent.h>
31 #include <varDump/VarDumpRecord.h>
32 #include <physics/S4/S4Event.h>
33 //
34 #include <sgp4.h>
35
36 #include <PamelaDBOperations.h>
37 //
38 using namespace std;
39 using namespace pamela;
40
41 // Some function to work with cTle stuff.
42 bool compTLE(cTle* tle1, cTle *tle2);
43 float getTleJulian(cTle *);
44 string getTleDatetime(cTle*);
45
46 /**
47 * Constructor.
48 * @param host hostname for the SQL connection.
49 * @param user username for the SQL connection.
50 * @param password password for the SQL connection.
51 * @param filerawname The path and name to the raw file.
52 * @param filerootname The path and name of the raw file.
53 * @param boot file BOOT number.
54 * @param obt0 file obt0.
55 * @param tsync file timesync.
56 * @param debug debug flag.
57 * @param tlefilename ascii file with TLE 3 line elements.
58 */
59 PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename){
60 //
61 //
62 SetConnection(host,user,password);
63 //
64 SetDebugFlag(debug);
65 //
66 glrun = new GL_RUN();
67 //
68 if ( !boot ) SetNOBOOT(false);
69 SetBOOTnumber(boot);
70 SetTsync(tsync);
71 SetObt0(obt0);
72 //
73 SetTLEPath(tlefilename);
74 //
75 //
76 INSERT_RAW =!filerawname.IsNull();
77 if(INSERT_RAW)SetRawName(filerawname);
78 //
79 INSERT_ROOT = !filerootname.IsNull();
80 if( INSERT_ROOT ){
81 this->SetRootName(filerootname);
82 this->SetOrbitNo();
83 file = TFile::Open(this->GetRootName().Data());
84 } else {
85 this->SetRootName("");
86 };
87 //
88 this->SetID_RAW(0);
89 this->SetID_ROOT(0);
90
91 VALIDATE = false;
92
93 //
94 };
95
96 /**
97 * Destructor
98 */
99 void PamelaDBOperations::Close(){
100 if( conn && conn->IsConnected() ) conn->Close();
101 delete clean_time;
102 delete glrun;
103 delete this;
104 };
105
106 //
107 // SETTERS
108 //
109
110 //
111 // must be out of the constructor in order to FORCE the validation of the latest runs in case you run the validation together with the latest file
112 //
113 void PamelaDBOperations::CheckValidate(Long64_t olderthan){
114 clean_time = new TDatime();
115 //
116 if(olderthan >= 0){
117 VALIDATE = true;
118 UInt_t timelim = 0;
119 timelim = (UInt_t)clean_time->Convert(true) - olderthan;
120 clean_time->Set(timelim,false);
121 };
122 };
123
124 /**
125 * Open the DB connection
126 * @param host hostname for the SQL connection.
127 * @param user username for the SQL connection.
128 * @param password password for the SQL connection.
129 */
130 void PamelaDBOperations::SetConnection(TString host, TString user, TString password){
131 if ( IsDebug() ) printf(" Connecting using host = %s user = %s password = %s \n",host.Data(),user.Data(),password.Data());
132 conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());
133 };
134
135 /**
136 * Store the ID of the ROOT file.
137 * @param idr ID of the ROOT file
138 */
139 void PamelaDBOperations::SetID_ROOT(UInt_t idr){
140 idroot=idr;
141 };
142
143 /**
144 * Store the ID of the RAW file.
145 * @param idr ID of the RAW file
146 */
147 void PamelaDBOperations::SetID_RAW(UInt_t idr){
148 id=idr;
149 };
150
151 /**
152 * Set the debug flag
153 *
154 */
155 void PamelaDBOperations::SetDebugFlag(Bool_t dbg){
156 debug = dbg;
157 };
158
159 /**
160 * Set the autoboot flag
161 *
162 */
163 void PamelaDBOperations::SetAutoBoot(Bool_t dbg){
164 AUTOBOOT = dbg;
165 };
166
167 /**
168 * Set the nofrag flag
169 *
170 */
171 void PamelaDBOperations::SetNoFrag(Bool_t nf){
172 NOFRAG = nf;
173 };
174
175 /**
176 * Store the BOOT number of the RAW file.
177 * @param boot BOOT number of the RAW file
178 */
179 void PamelaDBOperations::SetBOOTnumber(UInt_t boot){
180 BOOTNO=boot;
181 };
182
183 /**
184 * Store the time sync of the RAW file.
185 * @param boot time sync
186 */
187 void PamelaDBOperations::SetTsync(UInt_t ts){
188 tsync=ts;
189 };
190
191 /**
192 * Store the time sync of the RAW file.
193 * @param boot time sync
194 */
195 void PamelaDBOperations::SetObt0(UInt_t ts){
196 obt0=ts;
197 };
198
199 /**
200 * Store the RAW filename.
201 * @param str the RAW filename.
202 */
203 void PamelaDBOperations::SetRawName(TString str){
204 filerawname=str;
205 };
206
207 /**
208 * Store the ROOT filename.
209 * @param str the ROOT filename.
210 */
211 void PamelaDBOperations::SetRootName(TString str){
212 filerootname=str;
213 };
214
215 /**
216 * Store the downlink orbit number from filename.
217 */
218 void PamelaDBOperations::SetOrbitNo(){
219 dworbit = 0;
220 TString name = this->GetRootFile();
221 Int_t nlength = name.Length();
222 if ( nlength < 5 ) return;
223 TString dwo = 0;
224 for (Int_t i = 0; i<5; i++){
225 dwo.Append(name[i],1);
226 };
227 if ( dwo.IsDigit() ){
228 dworbit = (UInt_t)dwo.Atoi();
229 } else {
230 dwo="";
231 for (Int_t i = 8; i<13; i++){
232 dwo.Append(name[i],1);
233 };
234 if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
235 };
236 if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
237 return;
238 };
239
240
241
242 /**
243 * Store the NOBOOT flag.
244 * @param noboot true/false.
245 */
246 void PamelaDBOperations::SetNOBOOT(Bool_t noboot){
247 NOBOOT = noboot;
248 };
249
250 /**
251 * Store path to the TLE file.
252 */
253 void PamelaDBOperations::SetTLEPath(TString str){
254 tlefilename = str;
255 };
256
257 /**
258 * Store the olderthan variable
259 * @param olderthan
260 */
261 // void PamelaDBOperations::SetOlderThan(Long64_t oldthan){
262 // olderthan = oldthan;
263 // };
264
265 /**
266 * Retrieve the ID_RAW, if exists, returns NULL if does not exist.
267 */
268 Bool_t PamelaDBOperations::SetID_RAW(){
269 stringstream oss;
270 TSQLResult *result = 0;
271 TSQLRow *row = 0;
272 oss.str("");
273 oss << "SELECT ID FROM GL_RAW WHERE "
274 << " PATH = '" << this->GetRawPath().Data() << "' AND "
275 << " NAME = '" << this->GetRawFile().Data() << "' ";
276
277 result = conn->Query(oss.str().c_str());
278 if ( result == NULL ) throw -4;
279 row = result->Next();
280 if ( !row ) return(false);
281 delete result;
282 id = (UInt_t)atoll(row->GetField(0));
283 return(true);
284 }
285
286 /**
287 *
288 * Set the variables which have to be stored in the GL_RUN table and that do not depend on the RUN
289 *
290 */
291 void PamelaDBOperations::SetCommonGLRUN(UInt_t absth, UInt_t abstt){
292 glrun->SetBOOTNUMBER(BOOTNO);
293 glrun->SetRUNHEADER_TIME(absth);
294 glrun->SetRUNTRAILER_TIME(abstt);
295 glrun->SetID_ROOT_L2(0);
296 glrun->SetID_ROOT_L0(idroot);
297 glrun->SetVALIDATION(0);
298 };
299
300 /**
301 * Patch, look for upper limits to avoid processing retransmitted data
302 */
303 Int_t PamelaDBOperations::SetUpperLimits(){
304 UInt_t nevent = 0;
305 UInt_t pktlast = 0;
306 UInt_t obtlast = 0;
307 Long64_t t_pktlast = 0LL;
308 // UInt_t t_obtlast = 0;
309 Long64_t t_obtlast = 0LL;
310 Long64_t upperpkt2 = 0LL;
311 Long64_t upperobt2 = 0LL;
312 UInt_t zomp = 0;
313 UInt_t jump = 50000; // was 5000
314 EventCounter *code=0;
315 //
316 Long64_t deltapkt = 5000LL;
317 Long64_t deltaobt = 50000LL;
318 //
319 // pcksList packetsNames;
320 // pcksList::iterator Iter;
321 // getPacketsNames(packetsNames);
322 //
323 pktfirst = 0;
324 obtfirst = 0;
325 //
326 TTree *T = 0;
327 T = (TTree*)file->Get("Physics");
328 if ( !T || T->IsZombie() ) throw -16;
329 EventHeader *eh = 0;
330 PscuHeader *ph = 0;
331 T->SetBranchAddress("Header", &eh);
332 nevent = T->GetEntries();
333 //
334 T->GetEntry(0);
335 ph = eh->GetPscuHeader();
336 pktfirst = ph->GetCounter();
337 obtfirst = ph->GetOrbitalTime();
338 //
339 // code = eh->GetCounter();
340 // UInt_t en = 0;
341 // for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
342 // en = code->Get(GetPacketType(*Iter));
343 // if ( en ) printf(" Packet type is %s, entries: %i \n",*Iter,en);
344 //};
345 //
346 T->GetEntry(nevent-1);
347 ph = eh->GetPscuHeader();
348 pktlast = ph->GetCounter();
349 obtlast = ph->GetOrbitalTime();
350 //
351 upperpkt = PKT(pktlast);
352 upperobt = OBT(obtlast);
353 upperentry = nevent-1;
354 //
355 if ( IsDebug() ) printf(" First entries are: OBT %i pkt_num %i \n",obtfirst,pktfirst);
356 //
357 if ( IsDebug() ) printf(" Last entries are: OBT %lld pkt_num %lld entry %i\n",upperobt,upperpkt,upperentry);
358 //
359 if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ) return(1);
360 //
361 if ( !nevent ) return(2);
362 //
363 if ( nevent < 2 ) return(4);
364 //
365 if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) ){
366 //
367 // go back
368 zomp = nevent - 2;
369 //
370 while ( jump > 0 ){
371 //
372 t_pktlast = PKT(pktlast);
373 t_obtlast = OBT(obtlast);
374 //
375 for (UInt_t i = zomp; i>1; i-=jump){
376 //
377 if ( i >= 0 ) T->GetEntry(i);
378 ph = eh->GetPscuHeader();
379 upperpkt = PKT(ph->GetCounter());
380 upperobt = OBT(ph->GetOrbitalTime());
381 upperentry = i;
382 //
383 if ( (i-1) >= 0 ) T->GetEntry(i-1);
384 ph = eh->GetPscuHeader();
385 upperpkt2 = PKT(ph->GetCounter());
386 upperobt2 = OBT(ph->GetOrbitalTime());
387 //
388 if ( (t_pktlast < upperpkt && t_obtlast > upperobt) || (t_pktlast < upperpkt2 && t_obtlast > upperobt2) ){
389 if ( IsDebug() ) printf(" .-. upperpkt2 %lld upperobt2 %lld \n",upperpkt2,upperobt2);
390 if ( IsDebug() ) printf(" .-. upperpkt %lld t_pktlast %lld upperobt %lld t_obtlast %lld \n",upperpkt,t_pktlast,upperobt,t_obtlast);
391 if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %lld pktlast %u upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);
392 throw -13;
393 };
394 //
395 if ( t_pktlast < upperpkt && t_obtlast < upperobt && t_pktlast < upperpkt2 && t_obtlast < upperobt2 ){
396 zomp = i + jump + 1;
397 if ( zomp > nevent-2 ) zomp = nevent - 2;
398 if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %lld pktlast %i upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);
399 break;
400 };
401 //
402 t_pktlast = upperpkt;
403 t_obtlast = upperobt;
404 };
405 //
406 if ( jump == 1 ) jump = 0;
407 if ( jump == 10 ) jump = 1;
408 if ( jump == 100 ) jump = 10;
409 if ( jump == 1000 ) jump = 100;
410 if ( jump == 5000 ) jump = 1000;
411 if ( jump == 50000 ) jump = 5000;
412 //
413 };
414 //
415 };
416 //
417 // check if last runtrailer is within limits, if not extend limits (one should check for all packets but we need only runtrailer)
418 //
419 PacketType *pctp=0;
420 TTree *rh=(TTree*)file->Get("RunHeader");
421 if ( !rh || rh->IsZombie() ) throw -17;
422 TTree *rt=(TTree*)file->Get("RunTrailer");
423 if ( !rt || rt->IsZombie() ) throw -18;
424 //
425 rh->SetBranchAddress("RunHeader", &runh);
426 rh->SetBranchAddress("Header", &ehh);
427 //
428 rt->SetBranchAddress("RunTrailer", &runt);
429 rt->SetBranchAddress("Header", &eht);
430 //
431 rhev = rh->GetEntries();
432 rtev = rt->GetEntries();
433 Long64_t sobtt = 0LL;
434 Long64_t sobth = 0LL;
435 Long64_t spktt = 0LL;
436 Long64_t spkth = 0LL;
437 Long64_t pktt = 0LL;
438 Long64_t obtt = 0LL;
439 Long64_t pkth = 0LL;
440 Long64_t obth = 0LL;
441 //
442 T->GetEntry(upperentry);
443 code = eh->GetCounter();
444 Int_t lasttrail = code->Get(pctp->RunTrailer);
445 Int_t lasthead = code->Get(pctp->RunHeader);
446 if ( lasttrail < rtev ){
447 rt->GetEntry(lasttrail);
448 pht = eht->GetPscuHeader();
449 pktt = PKT(pht->GetCounter());
450 obtt = OBT(pht->GetOrbitalTime());
451 };
452 //
453 if ( lasthead < rhev ){
454 rh->GetEntry(lasthead);
455 phh = ehh->GetPscuHeader();
456 pkth = PKT(phh->GetCounter());
457 obth = OBT(phh->GetOrbitalTime());
458 };
459 //
460 if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
461 if ( pkth > upperpkt && obth > upperobt ){
462 if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
463 upperpkt = pkth;
464 upperobt = obth;
465 rhev = lasthead+1;
466 } else {
467 rhev = lasthead;
468 };
469 if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
470 //
471 if ( IsDebug() ) printf(" rtev beforev %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
472 if ( pktt > upperpkt && obtt > upperobt ){
473 if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
474 upperpkt = pktt;
475 upperobt = obtt;
476 rtev = lasttrail+1;
477 } else {
478 rtev = lasttrail;
479 };
480 if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
481 // goto kikko;
482 //
483 //
484 // Check if runtrailer/runheader are within lower limits
485 //
486 //
487 pkth = 0LL;
488 obth = 0LL;
489 spkth = 0LL;
490 sobth = 0LL;
491 for (Int_t k=0; k<rhev; k++){
492 if ( k > 0 ){
493 spkth = pkth;
494 sobth = obth;
495 };
496 rh->GetEntry(k);
497 phh = ehh->GetPscuHeader();
498 pkth = PKT(phh->GetCounter());
499 obth = OBT(phh->GetOrbitalTime());
500 //
501 // if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
502 //
503 if ( pkth < spkth && obth < sobth ){
504 if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i \n",rhev);
505 //
506 rhev = k-1;
507 rh->GetEntry(rhev);
508 pkth = spkth;
509 obth = sobth;
510 //
511 UInt_t evbefh = 0;
512 code = ehh->GetCounter();
513 evbefh = code->Get(pctp->Physics);
514 if ( evbefh >= 0 ){
515 T->GetEntry(evbefh);
516 ph = eh->GetPscuHeader();
517 t_pktlast = PKT(ph->GetCounter());
518 t_obtlast = OBT(ph->GetOrbitalTime());
519 if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
520 upperpkt = pkth;
521 upperobt = obth;
522 upperentry = evbefh-1;
523 } else {
524 while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){
525 evbefh++;
526 T->GetEntry(evbefh);
527 ph = eh->GetPscuHeader();
528 t_pktlast = PKT(ph->GetCounter());
529 t_obtlast = OBT(ph->GetOrbitalTime());
530 };
531 T->GetEntry(evbefh-1);
532 ph = eh->GetPscuHeader();
533 upperpkt = PKT(ph->GetCounter());
534 upperobt = OBT(ph->GetOrbitalTime());
535 upperentry = evbefh-1;
536 };
537 };
538 if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
539 goto kikko0;
540 };
541 };
542 kikko0:
543 //
544 //
545 //
546 pktt = 0LL;
547 obtt = 0LL;
548 spktt = 0LL;
549 sobtt = 0LL;
550 for (Int_t k=0; k<rtev; k++){
551 if ( k > 0 ){
552 spktt = pktt;
553 sobtt = obtt;
554 };
555 rt->GetEntry(k);
556 pht = eht->GetPscuHeader();
557 pktt = PKT(pht->GetCounter());
558 obtt = OBT(pht->GetOrbitalTime());
559 //
560 // if ( IsDebug() ) printf(" k %i rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
561 //
562 if ( pktt < spktt && obtt < sobtt ){
563 if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
564 //
565 rtev = k-1;
566 rt->GetEntry(rtev);
567 pktt = spktt;
568 obtt = sobtt;
569 if ( IsDebug() ) printf(" lasttrail %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
570 //
571 UInt_t evbeft = 0;
572 code = eht->GetCounter();
573 evbeft = code->Get(pctp->Physics);
574 if ( evbeft >= 0 ){
575 T->GetEntry(evbeft);
576 ph = eh->GetPscuHeader();
577 t_pktlast = PKT(ph->GetCounter());
578 t_obtlast = OBT(ph->GetOrbitalTime());
579 if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
580 upperpkt = pktt;
581 upperobt = obtt;
582 upperentry = evbeft-1;
583 } else {
584 while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){
585 evbeft++;
586 T->GetEntry(evbeft);
587 ph = eh->GetPscuHeader();
588 t_pktlast = PKT(ph->GetCounter());
589 t_obtlast = OBT(ph->GetOrbitalTime());
590 };
591 T->GetEntry(evbeft-1);
592 ph = eh->GetPscuHeader();
593 upperpkt = PKT(ph->GetCounter());
594 upperobt = OBT(ph->GetOrbitalTime());
595 upperentry = evbeft-1;
596 };
597 };
598 if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
599 goto kikko;
600 // break;
601 //
602 };
603 //
604 };
605 //
606 kikko:
607 //
608 T->GetEntry(upperentry);
609 code = eh->GetCounter();
610 lasttrail = code->Get(pctp->RunTrailer);
611 lasthead = code->Get(pctp->RunHeader);
612 if ( lasttrail < rtev ){
613 rt->GetEntry(lasttrail);
614 pht = eht->GetPscuHeader();
615 pktt = PKT(pht->GetCounter());
616 obtt = OBT(pht->GetOrbitalTime());
617 };
618 //
619 if ( lasthead < rhev ){
620 rh->GetEntry(lasthead);
621 phh = ehh->GetPscuHeader();
622 pkth = PKT(phh->GetCounter());
623 obth = OBT(phh->GetOrbitalTime());
624 };
625 //
626 if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
627 if ( pkth > upperpkt && obth > upperobt ){
628 if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
629 upperpkt = pkth;
630 upperobt = obth;
631 rhev = lasthead+1;
632 } else {
633 rhev = lasthead;
634 };
635 if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
636 //
637 if ( IsDebug() ) printf(" rtev beforev %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
638 if ( pktt > upperpkt && obtt > upperobt ){
639 if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
640 upperpkt = pktt;
641 upperobt = obtt;
642 rtev = lasttrail+1;
643 } else {
644 rtev = lasttrail;
645 };
646 if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
647 //
648 if ( IsDebug() ) printf(" Upper limits are: OBT %lld pkt_num %lld upper entry %i \n",upperobt,upperpkt,upperentry);
649 //
650 return(0);
651 }
652
653 /**
654 *
655 * Trick to have unique RUN ID numbers even when runs are deleted and mysql deamon restarted.
656 * Entries in the _RUNID_GEN table are never deleted.
657 *
658 **/
659 UInt_t PamelaDBOperations::AssignRunID(){
660 //
661 TSQLResult *result = 0;
662 TSQLRow *row = 0;
663 UInt_t runid = 0;
664 //
665 stringstream oss;
666 //
667 oss.str("");
668 oss << "INSERT INTO _RUNID_GEN VALUES (NULL);";
669 result = conn->Query(oss.str().c_str());
670 if ( !result ) throw -10;
671 oss.str("");
672 oss << "SELECT ID FROM _RUNID_GEN ORDER BY ID DESC LIMIT 1;";
673 result = conn->Query(oss.str().c_str());
674 if ( !result ) throw -10;
675 //
676 row = result->Next();
677 //
678 if ( !row ) throw -28;
679 //
680 runid = (UInt_t)atoll(row->GetField(0));
681 //
682 return(runid);
683 };
684
685 //
686 // GETTERS
687 //
688
689 /**
690 *
691 * Returns the DB absolute time needed to associate calibrations to data
692 *
693 */
694 UInt_t PamelaDBOperations::GetAbsTime(UInt_t obt){
695 //
696 return(((UInt_t)(OBT(obt)/1000)+toffset));
697 //
698 };
699
700 /**
701 *
702 * List of packet types (just to make easily the loops)
703 *
704 */
705 const PacketType* PamelaDBOperations::GetPacketType(const char* type){
706 if ( !strcmp(type,"Pscu") ) return(PacketType::Pscu);
707 if ( !strcmp(type,"PhysEndRun") ) return(PacketType::PhysEndRun);
708 if ( !strcmp(type,"CalibCalPulse1") ) return(PacketType::CalibCalPulse1);
709 if ( !strcmp(type,"CalibCalPulse2") ) return(PacketType::CalibCalPulse2);
710 if ( !strcmp(type,"Physics") ) return(PacketType::Physics);
711 if ( !strcmp(type,"CalibTrkBoth") ) return(PacketType::CalibTrkBoth);
712 if ( !strcmp(type,"CalibTrk1") ) return(PacketType::CalibTrk1);
713 if ( !strcmp(type,"CalibTrk2") ) return(PacketType::CalibTrk2);
714 if ( !strcmp(type,"CalibTof") ) return(PacketType::CalibTof);
715 if ( !strcmp(type,"CalibS4") ) return(PacketType::CalibS4);
716 if ( !strcmp(type,"CalibCalPed") ) return(PacketType::CalibCalPed);
717 if ( !strcmp(type,"Calib1_Ac1") ) return(PacketType::Calib1_Ac1);
718 if ( !strcmp(type,"Calib2_Ac1") ) return(PacketType::Calib2_Ac1);
719 if ( !strcmp(type,"Calib1_Ac2") ) return(PacketType::Calib1_Ac2);
720 if ( !strcmp(type,"Calib2_Ac2") ) return(PacketType::Calib2_Ac2);
721 if ( !strcmp(type,"CalibCal") ) return(PacketType::CalibCal);
722 if ( !strcmp(type,"RunHeader") ) return(PacketType::RunHeader);
723 if ( !strcmp(type,"RunTrailer") ) return(PacketType::RunTrailer);
724 if ( !strcmp(type,"CalibHeader") ) return(PacketType::CalibHeader);
725 if ( !strcmp(type,"CalibTrailer") ) return(PacketType::CalibTrailer);
726 if ( !strcmp(type,"InitHeader") ) return(PacketType::InitHeader);
727 if ( !strcmp(type,"InitTrailer") ) return(PacketType::InitTrailer);
728 if ( !strcmp(type,"EventTrk") ) return(PacketType::EventTrk);
729 if ( !strcmp(type,"Log") ) return(PacketType::Log);
730 if ( !strcmp(type,"VarDump") ) return(PacketType::VarDump);
731 if ( !strcmp(type,"ArrDump") ) return(PacketType::ArrDump);
732 if ( !strcmp(type,"TabDump") ) return(PacketType::TabDump);
733 if ( !strcmp(type,"Tmtc") ) return(PacketType::Tmtc);
734 if ( !strcmp(type,"Mcmd") ) return(PacketType::Mcmd);
735 if ( !strcmp(type,"ForcedFECmd") ) return(PacketType::ForcedFECmd);
736 if ( !strcmp(type,"Ac1Init") ) return(PacketType::Ac1Init);
737 if ( !strcmp(type,"CalInit") ) return(PacketType::CalInit);
738 if ( !strcmp(type,"TrkInit") ) return(PacketType::TrkInit);
739 if ( !strcmp(type,"TofInit") ) return(PacketType::TofInit);
740 if ( !strcmp(type,"TrgInit") ) return(PacketType::TrgInit);
741 if ( !strcmp(type,"NdInit") ) return(PacketType::NdInit);
742 if ( !strcmp(type,"S4Init") ) return(PacketType::S4Init);
743 if ( !strcmp(type,"Ac2Init") ) return(PacketType::Ac2Init);
744 if ( !strcmp(type,"CalAlarm") ) return(PacketType::CalAlarm);
745 if ( !strcmp(type,"Ac1Alarm") ) return(PacketType::Ac1Alarm);
746 if ( !strcmp(type,"TrkAlarm") ) return(PacketType::TrkAlarm);
747 if ( !strcmp(type,"TrgAlarm") ) return(PacketType::TrgAlarm);
748 if ( !strcmp(type,"TofAlarm") ) return(PacketType::TofAlarm);
749 if ( !strcmp(type,"S4Alarm") ) return(PacketType::S4Alarm);
750 if ( !strcmp(type,"Ac2Alarm") ) return(PacketType::Ac2Alarm);
751 if ( !strcmp(type,"TsbT") ) return(PacketType::TsbT);
752 if ( !strcmp(type,"TsbB") ) return(PacketType::TsbB);
753 return(PacketType::Invalid);
754 };
755
756 //
757 // PRIVATE FUNCTIONS
758 //
759
760 // /**
761 // * Open the ROOT filename for reading
762 // */
763 // void PamelaDBOperations::OpenFile(){
764 // file = TFile::Open(this->GetRootName().Data());
765 // //
766
767 void PamelaDBOperations::CheckFile(){
768 if ( !file ) throw -12;
769 };
770
771
772 /**
773 * Check if LEVEL0 file and DB connection have really be opened
774 */
775 void PamelaDBOperations::CheckConnection(){
776 //
777 // check connection
778 //
779 if( !conn ) throw -1;
780 bool connect = conn->IsConnected();
781 if( !connect ) throw -1;
782 //
783 if ( IsDebug() ) printf("\n DB INFORMATIONS:\n SQL: %s Version: %s Host %s Port %i \n\n",conn->GetDBMS(),conn->ServerInfo(),conn->GetHost(),conn->GetPort());
784 //
785 if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
786 //
787 // set DB timezone to UTC
788 //
789 stringstream oss;
790 //
791 oss.str("");
792 oss << "SET time_zone='+0:00';";
793 TSQLResult *result = 0;
794 result = conn->Query(oss.str().c_str());
795 if ( !result ) throw -10;
796 oss.str("");
797 oss << "SET wait_timeout=173000;";
798 conn->Query(oss.str().c_str());
799 //
800 };
801
802 /**
803 * Return the correct packet number if we went back to zero
804 */
805 Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){
806 //
807 if ( IsDebug() ) printf(" pkt conversion: pkt_num is %u pktfirst is %u (UInt_t)(16777214/2)) is %u \n",pkt_num,pktfirst,(UInt_t)(16777214/2));
808 //
809 if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2) ){
810 if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL);
811 return((Long64_t)pkt_num+16777215LL);
812 };
813 //
814 if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){
815 if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL);
816 return((Long64_t)pkt_num-16777215LL);
817 };
818 //
819 if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
820 return((Long64_t)pkt_num);
821 //
822 };
823
824 /**
825 * Return the correct On Board Time if we went back to zero
826 */
827 Long64_t PamelaDBOperations::OBT(UInt_t obt){
828 //
829 if ( obt < (obtfirst/2) && obtfirst > (numeric_limits<UInt_t>::max()/2) ) return((Long64_t)(obt+numeric_limits<UInt_t>::max()));
830 //
831 if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
832 return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
833 };
834 //
835 return((Long64_t)obt);
836 };
837
838 /**
839 *
840 * Fill the glrun class with infos about the run when we have both runtrailer and runheader
841 *
842 */
843 void PamelaDBOperations::FillClass(){
844 this->FillClass(false,false,0,0);
845 };
846
847 /**
848 *
849 * Fill the glrun class with infos about the run when we have both runtrailer and runheader
850 *
851 */
852 void PamelaDBOperations::FillClass(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
853 //
854 TTree *T = 0;
855 T = (TTree*)file->Get("Physics");
856 if ( !T || T->IsZombie() ) throw -16;
857 //
858 EventHeader *eh = 0;
859 PscuHeader *ph = 0;
860 T->SetBranchAddress("Header", &eh);
861 PacketType *pctp=0;
862 EventCounter *codt=0;
863 EventCounter *codh=0;
864 UInt_t firstObt = 0;
865 UInt_t lastObt = 0;
866 UInt_t firstPkt = 0;
867 UInt_t lastPkt = 0;
868 UInt_t rhtime = 0;
869 UInt_t rttime = 0;
870 if ( !mishead ){
871 codh = ehh->GetCounter();
872 firstev = codh->Get(pctp->Physics);
873 rhtime = this->GetAbsTime(phh->GetOrbitalTime());
874 glrun->Set_GL_RUNH(runh,phh);
875 firstObt = glrun->GetRUNHEADER_OBT();
876 firstPkt = glrun->GetRUNHEADER_PKT();
877 };
878 if ( !mistrail ){
879 codt = eht->GetCounter();
880 lastev = codt->Get(pctp->Physics)-1;
881 rttime = this->GetAbsTime(pht->GetOrbitalTime());
882 glrun->Set_GL_RUNT(runt,pht);
883 lastObt = glrun->GetRUNTRAILER_OBT();
884 lastPkt = glrun->GetRUNTRAILER_PKT();
885 };
886 //
887 if ( mishead && mistrail && lastev+1 == firstev ) throw -14; // run with no events, no runtrailer, no runheader... unsupported should never arrive here
888 //
889 if ( mishead ) {
890 glrun->Set_GL_RUNH0();
891 //
892 if ( lastev+1 == firstev ){
893 firstObt = lastObt;
894 firstPkt = lastPkt;
895 rhtime = rttime;
896 } else {
897 T->GetEntry(firstev);
898 ph = eh->GetPscuHeader();
899 firstObt = ph->GetOrbitalTime();
900 rhtime = this->GetAbsTime(firstObt);
901 firstPkt = ph->GetCounter();
902 };
903 //
904 glrun->SetRUNHEADER_PKT(firstPkt);
905 glrun->SetRUNHEADER_OBT(firstObt);
906 //
907 };
908 if ( mistrail ){
909 glrun->Set_GL_RUNT0();
910 //
911 if ( lastev+1 == firstev ){
912 lastObt = firstObt;
913 lastPkt = firstPkt;
914 rttime = rhtime;
915 } else {
916 T->GetEntry(lastev);
917 ph = eh->GetPscuHeader();
918 lastObt = ph->GetOrbitalTime();
919 rttime = this->GetAbsTime(lastObt);
920 lastPkt = ph->GetCounter();
921 };
922 //
923 glrun->SetRUNTRAILER_OBT(lastObt);
924 glrun->SetRUNTRAILER_PKT(lastPkt);
925 //
926 };
927 glrun->SetEV_FROM(firstev);
928 glrun->SetEV_TO(lastev);
929 glrun->SetNEVENTS(lastev-firstev+1);
930 //
931 this->SetCommonGLRUN(rhtime,rttime);
932 //
933 };
934
935 //
936 // PUBLIC FUNCTIONS
937 //
938
939 /**
940 * Insert a new row into GL_RAW table.
941 */
942 Int_t PamelaDBOperations::insertPamelaRawFile(){
943 //
944 stringstream oss;
945 //
946 Bool_t idr = this->SetID_RAW();
947 if ( idr ) return(1);
948 //
949 oss.str("");
950 oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
951 << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";
952 if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
953 //
954 idr = this->SetID_RAW();
955 if ( !idr ) throw -11;
956 //
957 return(0);
958 }
959
960
961 /**
962 * Look for one timesync information in the file and
963 * fill the GL_TIMESYNC table. It will look for: 1) TS-MCMD 2) TS info in the RunHeader 3) TS info in the runtrailer, if none exists exit with error
964 */
965 Int_t PamelaDBOperations::insertPamelaGL_TIMESYNC(){
966 //
967 TSQLResult *result = 0;
968 TSQLRow *row = 0;
969 UInt_t t0 = 0;
970 //
971 stringstream oss;
972 //
973 if ( this->GetID_RAW() == 0 ) throw -11;
974 //
975 oss.str("");
976 oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
977 << this->GetRawFile().Data() << "';";
978 if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
979 result = conn->Query(oss.str().c_str());
980 if ( !result ) throw -10;
981 row = result->Next();
982 //
983 if ( !row ){
984 oss.str("");
985 oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
986 << dworbit << " order by FROM_ORBIT desc limit 1;";
987 if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
988 result = conn->Query(oss.str().c_str());
989 if ( !result ) throw -10;
990 row = result->Next();
991 if ( !row ) throw -10;
992 };
993 //
994 TTimeStamp tu = TTimeStamp((UInt_t)atoi(row->GetField(0)),(UInt_t)atoi(row->GetField(1)),(UInt_t)atoi(row->GetField(2)),(UInt_t)atoi(row->GetField(3)),(UInt_t)atoi(row->GetField(4)),(UInt_t)atoi(row->GetField(5)),0,true,0);
995 t0 = (UInt_t)tu.GetSec();
996 if ( IsDebug() ) printf(" t0 is %u ti is %s %s %s %s %s %s\n",t0,row->GetField(0),row->GetField(1),row->GetField(2),row->GetField(3),row->GetField(4),row->GetField(5));
997 //
998 /*
999 * Verify that the TIMESYNC have been not already processed
1000 */
1001 oss.str("");
1002 oss << " SELECT COUNT(GL_TIMESYNC.ID),GL_TIMESYNC.OBT0,GL_TIMESYNC.TIMESYNC FROM GL_TIMESYNC "
1003 << " LEFT JOIN GL_RAW "
1004 << " ON GL_RAW.ID = GL_TIMESYNC.ID_RAW "
1005 << " WHERE GL_TIMESYNC.ID_RAW = " << this->GetID_RAW()
1006 << " GROUP BY GL_TIMESYNC.OBT0;";
1007 if ( IsDebug() ) printf(" check for old timesync: query is \n %s \n",oss.str().c_str());
1008 result = conn->Query(oss.str().c_str());
1009 if (result == NULL) throw -10;
1010 row = result->Next();
1011 if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
1012 if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1013 toffset = (UInt_t)atoll(row->GetField(2)) - (UInt_t)(this->OBT((UInt_t)atoll(row->GetField(1)))/1000) + t0;
1014 //
1015 tsync = (UInt_t)atoll(row->GetField(2));
1016 obt0 = (UInt_t)atoll(row->GetField(1));
1017 //
1018 return(1);
1019 };
1020 //
1021 TTree *T = 0;
1022 Int_t signal = 0;
1023 //
1024 UInt_t nevent = 0;
1025 UInt_t recEntries = 0;
1026 //
1027 UInt_t OBT = 0;
1028 UInt_t TYPE = 0;
1029 //
1030 Double_t minimum = 0.;
1031 Double_t maximum = 0.;
1032 Double_t minimum2 = 0.;
1033 Double_t maximum2 = 0.;
1034 //
1035 UInt_t TSYNC = 0;
1036 //
1037 pamela::McmdEvent *mc = 0;
1038 pamela::McmdRecord *mcrc = 0;
1039 TArrayC *mcmddata = 0;
1040 //
1041 minimum = numeric_limits<Double_t>::max();
1042 maximum = numeric_limits<Double_t>::min();
1043 minimum2 = numeric_limits<Double_t>::max();
1044 maximum2 = numeric_limits<Double_t>::min();
1045 //
1046 T = (TTree*)file->Get("Mcmd");
1047 if ( !T || T->IsZombie() ) throw -19;
1048 T->SetBranchAddress("Mcmd",&mc);
1049 //
1050 nevent = T->GetEntries();
1051 //
1052 // loop over events
1053 //
1054 Bool_t existsts = false;
1055 //
1056 for (UInt_t i=0; i<nevent;i++){
1057 //
1058 T->GetEntry(i);
1059 //
1060 recEntries = mc->Records->GetEntries();
1061 //
1062 for (UInt_t j = 0; j < recEntries; j++){
1063 mcrc = (pamela::McmdRecord*)mc->Records->At(j);
1064 mcmddata = mcrc->McmdData;
1065 //
1066 if (mcrc->ID1 == 0xE0){ // mcmd timesync
1067 //
1068 OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
1069 //
1070 TSYNC = (((UInt_t)mcmddata->At(0)<<24)&0xFF000000) + (((UInt_t)mcmddata->At(1)<<16)&0x00FF0000) + (((UInt_t)mcmddata->At(2)<<8)&0x0000FF00) + (((UInt_t)mcmddata->At(3))&0x000000FF);
1071 //
1072 TYPE = 55;//224;
1073 //
1074 if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
1075 //
1076 if ( TSYNC && OBT ){
1077 existsts = true;
1078 goto eout;
1079 };
1080 //
1081 };
1082 };
1083 };
1084 if ( !existsts ) { // try with runheader and runtrailer
1085 //
1086 if ( IsDebug() ) printf(" No ts mcmd \n");
1087 signal = 2;
1088 //
1089 TTree *rh=(TTree*)file->Get("RunHeader");
1090 if ( !rh || rh->IsZombie() ) throw -17;
1091 TTree *rt=(TTree*)file->Get("RunTrailer");
1092 if ( !rt || rt->IsZombie() ) throw -18;
1093 //
1094 rh->SetBranchAddress("RunHeader", &runh);
1095 //
1096 rt->SetBranchAddress("RunTrailer", &runt);
1097 //
1098 Int_t nrhev = rh->GetEntries();
1099 Int_t nrtev = rt->GetEntries();
1100 if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev);
1101 //
1102 if ( nrhev > 0 ){
1103 for (Int_t i=0; i<nrhev; i++){
1104 //
1105 rh->GetEntry(i);
1106 //
1107 TSYNC = runh->LAST_TIME_SYNC_INFO;
1108 OBT = runh->OBT_TIME_SYNC * 1000;
1109 //
1110 TYPE = 20;
1111 //
1112 if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
1113 //
1114 if ( TSYNC && OBT ){
1115 existsts = true;
1116 goto eout;
1117 };
1118 };
1119 //
1120 };
1121 if ( nrtev > 0 ){
1122 //
1123 if ( IsDebug() ) printf(" No runheader \n");
1124 signal = 6;
1125 //
1126 for (Int_t i=0; i<nrtev; i++){
1127 //
1128 rt->GetEntry(i);
1129 //
1130 TSYNC = runt->LAST_TYME_SYNC_INFO;
1131 OBT = runt->OBT_TYME_SYNC * 1000;
1132 //
1133 TYPE = 21;
1134 //
1135 if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
1136 //
1137 if ( TSYNC && OBT ){
1138 existsts = true;
1139 goto eout;
1140 };
1141 };
1142 //
1143 } else {
1144 if ( IsDebug() ) printf(" No runheader \n");
1145 };
1146 };
1147 //
1148 if ( !existsts ){ // try with inclination mcmd
1149 //
1150 if ( IsDebug() ) printf(" No runtrailer \n");
1151 signal = 14;
1152 //
1153 Double_t timesync = 0.;
1154 for (UInt_t i=0; i<nevent;i++){
1155 //
1156 T->GetEntry(i);
1157 //
1158 recEntries = mc->Records->GetEntries();
1159 // //
1160 for (UInt_t j = 0; j < recEntries; j++){
1161 mcrc = (pamela::McmdRecord*)mc->Records->At(j);
1162 mcmddata = mcrc->McmdData;
1163 //
1164 if (mcrc->ID1 == 0xE2){ // mcmd inclination
1165 timesync = 0.;
1166 timesync = (Double_t)(((((UInt_t)mcmddata->At(0) << 24) & 0xFF000000) + (((UInt_t)mcmddata->At(1) << 16) & 0x00FF0000) + (((UInt_t)mcmddata->At(2) << 8) & 0x0000FF00) + ((UInt_t)mcmddata->At(3) & 0x000000FF))/128.0);
1167 //
1168 if ( timesync > maximum2){
1169 maximum2 = timesync;
1170 OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
1171 };
1172 };
1173 //
1174 };
1175 };
1176 if ( maximum2 > numeric_limits<Double_t>::min() ){
1177 TSYNC = (UInt_t)(maximum2 + 0.5);
1178 TYPE = 666;
1179 if ( TSYNC && OBT ){
1180 existsts = true;
1181 goto eout;
1182 };
1183 };
1184 };
1185 //
1186 if ( !existsts && obt0 ){ // insert timesync by hand
1187 //
1188 if ( IsDebug() ) printf(" No incl mcmd \n");
1189 signal = 30;
1190 //
1191 OBT = obt0;
1192 TSYNC = tsync;
1193 TYPE = 999;
1194 existsts = true;
1195 goto eout;
1196 };
1197 //
1198 eout:
1199 //
1200 if ( !existsts ) throw -3;
1201 //
1202 oss.str("");
1203 oss << "INSERT INTO GL_TIMESYNC (ID_RAW,TYPE,OBT0,TIMESYNC) VALUES ('"
1204 << this->GetID_RAW() << "','"//224'"
1205 << dec << (UInt_t)TYPE << "','"
1206 << dec << (UInt_t)OBT << "','"
1207 << dec << (UInt_t)TSYNC << "');";
1208 conn->Query(oss.str().c_str());
1209 if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());
1210 //
1211 if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1212 toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1213 //
1214 tsync = TSYNC;
1215 obt0 = OBT;
1216 //
1217 delete result;
1218 return(signal);
1219 }
1220
1221 /**
1222 * Insert all the new rows into GL_ROOT.
1223 * The raw file indicates in the parameters should be already been stored in the database.
1224 */
1225 Int_t PamelaDBOperations::insertPamelaRootFile(){
1226 stringstream oss;
1227 TSQLResult *result = 0;
1228 TSQLRow *row = 0;
1229 UInt_t idtimesync = 0;
1230 //
1231 oss.str("");
1232 oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1233 << " LEFT JOIN GL_ROOT "
1234 << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1235 << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1236 << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1237 result = conn->Query(oss.str().c_str());
1238 //
1239 if ( !result ) throw -12;
1240 //
1241 row = result->Next();
1242 //
1243 if ( !row ) throw -10;
1244 if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){
1245 idroot = (UInt_t)atoll(row->GetField(2));
1246 return(1);
1247 };
1248 //
1249 // determine which timesync has to be used
1250 //
1251 oss.str("");
1252 oss << "SELECT GL_TIMESYNC.ID FROM GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW ORDER BY GL_TIMESYNC.ID DESC LIMIT 1;";
1253 result = conn->Query(oss.str().c_str());
1254 //
1255 if ( !result ) throw -3;
1256 //
1257 row = result->Next();
1258 //
1259 if ( !row ) throw -3;
1260 idtimesync = (UInt_t)atoll(row->GetField(0));
1261 //
1262 oss.str("");
1263 oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1264 << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";
1265 //
1266 if (conn->Query(oss.str().c_str()) == 0) throw -4;
1267 //
1268 delete result;
1269 //
1270 oss.str("");
1271 oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";
1272 //
1273 result = conn->Query(oss.str().c_str());
1274 if ( !result ) throw -12;
1275 row = result->Next();
1276 this->SetID_ROOT((UInt_t)atoll(row->GetField(0)));
1277 //
1278 delete result;
1279 //
1280 return(0);
1281 }
1282
1283 /**
1284 * Assign the BOOT_NUMBER to the raw file.
1285 */
1286 Int_t PamelaDBOperations::assignBOOT_NUMBER(){
1287 stringstream oss;
1288 TSQLResult *result = 0;
1289 TSQLRow *row = 0;
1290 oss.str("");
1291 oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1292 << " PATH = '" << this->GetRawPath().Data() << "' AND "
1293 << " NAME = '" << this->GetRawFile().Data() << "' ";
1294 result = conn->Query(oss.str().c_str());
1295 //
1296 if ( !result ) throw -4;;
1297 row = result->Next();
1298 if ( !row ) return(16);
1299 if ( row->GetField(1) ){
1300 this->SetBOOTnumber((UInt_t)atoll(row->GetField(1)));
1301 return(1);
1302 };
1303 if ( !row->GetField(0) ) throw -26;
1304 //
1305 UInt_t idRaw = (UInt_t)atoll(row->GetField(0));
1306 //
1307 //
1308 //
1309 TTree *trDumpEv = 0;
1310 trDumpEv = (TTree*)file->Get("VarDump");
1311 if ( !trDumpEv || trDumpEv->IsZombie() ) throw -20;
1312 //
1313 VarDumpEvent *vde = 0;
1314 VarDumpRecord *vdr = 0;
1315 //
1316 Bool_t found = false;
1317 trDumpEv->SetBranchAddress("VarDump", &vde);
1318 if ( trDumpEv->GetEntries() > 0 ){
1319 found = false;
1320 for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1321 trDumpEv->GetEntry(i);
1322 // vde->Records->GetEntries();
1323 if ( vde->Records->GetEntries()>5 ){
1324 found = true;
1325 goto fill;
1326 };
1327 };
1328 fill:
1329 if ( found ){
1330 //
1331 vdr = (VarDumpRecord*)vde->Records->At(6);
1332 //
1333 this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1334 //
1335 } else {
1336 if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4);
1337 };
1338 } else {
1339 if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2);
1340 };
1341 //
1342 UInt_t bn = 0;
1343 Bool_t afound = false;
1344 if ( !found && this->AutoBoot()){
1345 afound = true;
1346 //
1347 // Search for other files with similar timesync
1348 //
1349 if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0);
1350 UInt_t upperts = tsync-(obt0/1000)+5;
1351 UInt_t lowerts = tsync-(obt0/1000)-5;
1352 oss.str("");
1353 oss << "select GL_RAW.BOOT_NUMBER from GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW where TIMESYNC-(OBT0/1000)<"
1354 << upperts
1355 << " AND TIMESYNC-(OBT0/1000)>"
1356 << lowerts
1357 << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
1358 result = conn->Query(oss.str().c_str());
1359 if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str());
1360 //
1361 if ( !result ) throw -4;;
1362 found = true;
1363 if ( result->GetRowCount()<3 ){
1364 if ( IsDebug() ) printf(" AGH! no results!\n");
1365 found = false;
1366 } else {
1367 row = result->Next();
1368 bn = (UInt_t)atoll(row->GetField(0));
1369 for ( Int_t r=1; r<result->GetRowCount() ;r++){
1370 if ( !row ) throw -4;
1371 if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0));
1372 if ( bn != (UInt_t)atoll(row->GetField(0)) ){
1373 if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0)));
1374 found = false;
1375 };
1376 row = result->Next();
1377 };
1378 };
1379 };
1380 //
1381 Int_t sgn = 0;
1382 //
1383 if ( !found ){
1384 throw -29;
1385 } else {
1386 if ( afound ){
1387 this->SetBOOTnumber(bn);
1388 sgn = 8;
1389 };
1390 };
1391 //
1392 oss.str("");
1393 oss << " UPDATE GL_RAW "
1394 << " SET GL_RAW.BOOT_NUMBER = '" << dec << this->GetBOOTnumber() << "'"
1395 << " WHERE GL_RAW.ID = '" << idRaw << "'";
1396 conn->Query(oss.str().c_str());
1397 //
1398 delete result;
1399 return(sgn);
1400 };
1401
1402 /**
1403 * Scan runtrailer packet, fill the GL_RUN table and
1404 * check for missing and truncated runs
1405 */
1406 Int_t PamelaDBOperations::insertPamelaRUN(){
1407 Int_t signal = 0;
1408 //
1409 stringstream oss;
1410 oss.str("");
1411 //
1412 signal = this->SetUpperLimits();
1413 //
1414 // loop on runheader and runtrailer events
1415 //
1416 TTree *rh=(TTree*)file->Get("RunHeader");
1417 if ( !rh || rh->IsZombie() ) throw -17;
1418 TTree *rt=(TTree*)file->Get("RunTrailer");
1419 if ( !rt || rt->IsZombie() ) throw -18;
1420 //
1421 PacketType *pctp=0;
1422 EventCounter *cod=0;
1423 //
1424 rh->SetBranchAddress("RunHeader", &runh);
1425 rh->SetBranchAddress("Header", &ehh);
1426 //
1427 rt->SetBranchAddress("RunTrailer", &runt);
1428 rt->SetBranchAddress("Header", &eht);
1429 //
1430 UInt_t obtt = 0;
1431 UInt_t obth = 0;
1432 UInt_t pktt = 0;
1433 UInt_t pkth = 0;
1434 Int_t pth = -1;
1435 Int_t ptht = -1;
1436 Int_t evbeft = 0;
1437 Int_t evbefh = 0;
1438 //
1439 // no runtrailers in the file!
1440 //
1441 if ( !rtev ){
1442 if ( !upperentry ){
1443 if ( IsDebug() ) printf(" No physics events nor runs in the file \n");
1444 throw -8;
1445 } else {
1446 this->HandleRunFragments(true,true,0,upperentry);
1447 };
1448 } else {
1449 //
1450 for (Int_t ptt=0; ptt<rtev; ptt++){
1451 //
1452 rt->GetEntry(ptt);
1453 pht = eht->GetPscuHeader();
1454 pktt = pht->GetCounter();
1455 obtt = pht->GetOrbitalTime();
1456 //
1457 cod = eht->GetCounter();
1458 ptht = cod->Get(pctp->RunHeader) - 1;
1459 evbeft = cod->Get(pctp->Physics);
1460 //
1461 if ( !ptt && !(ptht+1) ){
1462 //
1463 if ( IsDebug() ) printf(" Piece of run at the beginning of the file %i %i %i \n",ptht,pth,ptt);
1464 //
1465 this->HandleRunFragments(true,false,0,(evbeft-1));
1466 //
1467 //
1468 } else if ( pth == ptht ){
1469 //
1470 if ( IsDebug() ) printf(" Missing header %i %i %i\n",ptht,pth,ptt);
1471 //
1472 if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1473 rt->GetEntry(ptt-1);
1474 cod = eht->GetCounter();
1475 evbefh = cod->Get(pctp->Physics);
1476 rt->GetEntry(ptt);
1477 pht = eht->GetPscuHeader();
1478 //
1479 if ( IsDebug() ) printf(" Try to find the beginning of a run which has only the runtrailer %i %i %i \n",ptht,pth,ptt);
1480 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);
1481 //
1482 this->HandleMissingHoT(true,false,evbefh,evbeft-1);
1483 //
1484 } else {
1485 //
1486 if ( IsDebug() ) printf(" Could be a good run, we have a runheader followed by a runtrailer %i %i %i\n",ptht,pth,ptt);
1487 //
1488 rh->GetEntry(ptht);
1489 phh = ehh->GetPscuHeader();
1490 pkth = phh->GetCounter();
1491 obth = phh->GetOrbitalTime();
1492 cod = ehh->GetCounter();
1493 evbefh = cod->Get(pctp->Physics);
1494 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);
1495 //
1496 // handle this run
1497 //
1498 this->HandleRun();
1499 //
1500 //
1501 //
1502 if ( PKT(pkth)>PKT(pktfirst) && OBT(obth)>OBT(obtfirst) && !ptt ){
1503 //
1504 if ( IsDebug() ) printf(" Piece of run at the beginning of the file WITH NO RUNTRAILER \n");
1505 //
1506 this->HandleRunFragments(true,true,0,(evbefh-1));
1507 //
1508 };
1509 //
1510 //
1511 if ( (ptht - pth) > 1 ){
1512 //
1513 if ( IsDebug() ) printf(" Missing runtrailers! \n");
1514 if ( IsDebug() ) printf(" Attention there is a jump in the runheader counter %i %i %i \n",ptht,pth,ptt);
1515 // is not the consecutive header
1516 while ( pth != ptht ){
1517 //
1518 // treat the header(s) in the middle and then go to the next header, repeat until you reach the correct header.
1519 //
1520 pth++;
1521 //
1522 rh->GetEntry(pth+1);
1523 phh = ehh->GetPscuHeader();
1524 pktt = phh->GetCounter();
1525 obtt = phh->GetOrbitalTime();
1526 cod = ehh->GetCounter();
1527 evbeft = cod->Get(pctp->Physics);
1528 rh->GetEntry(pth);
1529 phh = ehh->GetPscuHeader();
1530 cod = ehh->GetCounter();
1531 pkth = phh->GetCounter();
1532 obth = phh->GetOrbitalTime();
1533 evbefh = cod->Get(pctp->Physics);
1534 //
1535 if ( IsDebug() ) printf(" Try to find the end of a run which has only the runheader %i %i %i \n",ptht,pth,ptt);
1536 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);
1537 //
1538 this->HandleMissingHoT(false,true,evbefh,evbeft-1);
1539 //
1540 };
1541 //
1542 } else if ( !(ptht - pth) ){
1543 //
1544 if ( IsDebug() ) printf(" Missing runheader! \n");
1545 if ( IsDebug() ) printf(" Attention! the runheader counter did not changed %i %i %i \n",ptht,pth,ptt);
1546 if ( IsDebug() ) printf(" The run should have already been handled by HandleRun() \n");
1547 //
1548 } else {
1549 //
1550 // go on with next header
1551 //
1552 pth = ptht;
1553 };
1554 //
1555 };
1556 //
1557 if ( ptt+1 == rtev){
1558 ptht++;
1559 if ( ptht < rhev ){
1560 rh->GetEntry(ptht);
1561 phh = ehh->GetPscuHeader();
1562 pkth = phh->GetCounter();
1563 obth = phh->GetOrbitalTime();
1564 cod = ehh->GetCounter();
1565 evbefh = cod->Get(pctp->Physics);
1566 if ( IsDebug() ) printf(" Piece of run at the end of file %i %i %i \n",pkth,obth,obtt);
1567 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i %i %i \n",ptht,pth,ptt);
1568 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i \n",rhev);
1569 //
1570 this->HandleRunFragments(false,true,evbefh,upperentry);
1571 } else {
1572 //
1573 // check if we have a fragment with no header
1574 //
1575 if ( (UInt_t)evbeft < upperentry-1 ){
1576 if ( IsDebug() ) printf(" Piece of run at the end of the file with NO RUNHEADER!\n");
1577 //
1578 if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1579 rt->GetEntry(ptt-1);
1580 cod = eht->GetCounter();
1581 evbefh = cod->Get(pctp->Physics);
1582 rt->GetEntry(ptt);
1583 pht = eht->GetPscuHeader();
1584 this->HandleRunFragments(true,true,evbefh,upperentry);
1585 };
1586 };
1587 };
1588 //
1589 };
1590 };
1591 //
1592 return(signal);
1593 };
1594
1595 /**
1596 *
1597 * Check if the run has already been inserted
1598 *
1599 */
1600 Bool_t PamelaDBOperations::IsRunAlreadyInserted(){
1601 //
1602 TSQLResult *result = 0;
1603 TSQLRow *row = 0;
1604 //
1605 stringstream oss;
1606 oss.str("");
1607 //
1608 // the where clause is of the type: boot_number = _our_boot && (
1609 // ( runhead_time >= (_our_runhead_time-10) && runtrail_time <= (_our_runtrail_time+10) &&
1610 // ( runhead_obt >= _our_runheadobt || runhead_pkt >= _our_runheadpkt ) &&
1611 // ( runtrail_obt >= _our_runtrailobt || runtrail_pkt >= _our_runtrailpkt ) )
1612 // ||
1613 // ( runhead_time <= _our_runhead_time && runtrail_time >= _our_runtrail_time) &&
1614 // ( runhead_obt <= _our_runheadobt || runhead_pkt <= _our_runheadpkt ) &&
1615 // ( runtrail_obt <= _our_runtrailobt || runtrail_pkt <= _our_runtrailpkt ) )
1616 // )
1617 //
1618 oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
1619 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1620 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1621 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1622 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1623 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1624 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1625 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1626 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1627 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1628 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1629 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1630 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1631 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1632 //
1633 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1634 result = conn->Query(oss.str().c_str());
1635 //
1636 if ( !result ) throw -4;
1637 //
1638 row = result->Next();
1639 //
1640 if ( !row ){
1641 if ( IsDebug() ) printf(" The run is new \n");
1642 if ( IsDebug() ) printf(" -> fill the DB \n");
1643 return(false); // the file has not been inserted in the DB, go on.
1644 };
1645 //
1646 Bool_t signal = true;
1647 //
1648 while ( row != NULL ){
1649 if ( IsDebug() ) printf(" A run exists with runheader and runtrailer time and packets compatible with this one \n");
1650 //
1651 // the run has already been inserted
1652 //
1653 // return(true); //<<<<<<<<<<<<<<<<<<<<<<<< patch follows, uncomment here
1654 //
1655 // PATCH!
1656 // we keep the processing run if (in order of growing importance) 1) we have the runtrailer while the old run doesn't have it 2) we have the runheader
1657 // while the old run doesn't have it 3) we have more events than the old run
1658 //
1659 if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1660 //
1661 if ( IsDebug() ) printf(" The new run has more events than the old one \n");
1662 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1663 // oss.str("");
1664 // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1665 // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1666 // conn->Query(oss.str().c_str());
1667 if ( signal ) signal = false;
1668 goto gonext;
1669 //
1670 } else if ( glrun->GetNEVENTS() < (UInt_t)atoll(row->GetField(1)) ){
1671 if ( IsDebug() ) printf(" The new run has less events than the old one \n");
1672 if ( IsDebug() ) printf(" The run is already inserted \n");
1673 goto gonext;
1674 };
1675 //
1676 if ( glrun->GetTRK_CALIB() && !(UInt_t)atoll(row->GetField(2)) ){
1677 //
1678 if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");
1679 //
1680 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1681 // oss.str("");
1682 // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1683 // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1684 // conn->Query(oss.str().c_str());
1685 //
1686 if ( signal ) signal = false;
1687 goto gonext;
1688 } else if ( !glrun->GetTRK_CALIB() && (UInt_t)atoll(row->GetField(2)) ){
1689 if ( IsDebug() ) printf(" The new run has the same number of events but miss the runheader the old has the runheader \n");
1690 if ( IsDebug() ) printf(" The run is already inserted \n");
1691 goto gonext;
1692 };
1693 //
1694 if ( glrun->GetPKT_COUNTER() && !(UInt_t)atoll(row->GetField(3)) ){
1695 //
1696 if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");
1697 //
1698 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1699 // oss.str("");
1700 // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1701 // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1702 // conn->Query(oss.str().c_str());
1703 if ( signal ) signal = false;
1704 //
1705 };
1706 //
1707 gonext:
1708 // END PATCH!
1709 //
1710 row = result->Next();
1711 //
1712 };
1713 //
1714 delete result;
1715 //
1716 if ( signal && IsDebug() ) printf(" The run has already been inserted \n");
1717 if ( !signal && IsDebug() ) printf(" The run existed and was deleted, fill the DB \n");
1718 return(signal);
1719 };
1720
1721 /**
1722 * Handle runs which seems to be good ones.
1723 **/
1724 void PamelaDBOperations::HandleRun(){
1725 ULong64_t chkpkt = 0;
1726 ULong64_t pktt = (ULong64_t)PKT(pht->GetCounter());
1727 ULong64_t pkth = (ULong64_t)PKT(phh->GetCounter());
1728 //
1729 chkpkt = pkth + (ULong64_t)runt->PKT_COUNTER + 1ULL + 1ULL;
1730 //
1731 if ( labs(chkpkt-pktt)<2 ){
1732 //
1733 if ( IsDebug() ) printf(" check %llu pktt %llu \n",chkpkt,pktt);
1734 //
1735 // it must be a good run, fill the db
1736 //
1737 this->FillClass();
1738 //
1739 if ( !IsRunAlreadyInserted() ){
1740 glrun->SetID(this->AssignRunID());
1741 glrun->SetID_RUN_FRAG(0);
1742 glrun->Fill_GL_RUN(conn);
1743 };
1744 } else {
1745 //
1746 if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt);
1747 if ( IsDebug() ) printf(" try to recover run(s) without runheader and runtrailer between runheader and runtrailer\n");
1748 //
1749 this->HandleSuspiciousRun();
1750 //
1751 };
1752 //
1753 //
1754 return;
1755 };
1756
1757
1758 /**
1759 * Handle run fragments at the beginning or at the end of the file
1760 **/
1761 void PamelaDBOperations::HandleRunFragments(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
1762 //
1763 UInt_t rhfirstev = firstev;
1764 UInt_t rtlastev = lastev;
1765 Bool_t found = false;
1766 Bool_t foundinrun = false;
1767 //
1768 TSQLResult *result = 0;
1769 TSQLRow *row = 0;
1770 //
1771 stringstream oss;
1772 oss.str("");
1773 //
1774 // is the piece of run good (no other packets inside)?
1775 //
1776 if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
1777 //
1778 // if not, handle other pieces and continue with the first one
1779 //
1780 if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
1781 //
1782 };
1783 //
1784 // we have now the good first piece of a run, fill the glrun object
1785 //
1786 if ( rhfirstev != firstev && !mishead ) mishead = true;
1787 if ( rtlastev != lastev && !mistrail ) mistrail = true;
1788 //
1789 this->FillClass(mishead,mistrail,firstev,lastev);
1790 //
1791 if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
1792 //
1793 // First of all insert the run in the fragment table...
1794 //
1795 oss.str("");
1796 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1797 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1798 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1799 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1800 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1801 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1802 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1803 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1804 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1805 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1806 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1807 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1808 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1809 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1810 //
1811 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1812 result = conn->Query(oss.str().c_str());
1813 //
1814 if ( !result ) throw -4;
1815 //
1816 row = result->Next();
1817 //
1818 if ( !row ){
1819 //
1820 // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
1821 //
1822 if ( IsDebug() ) printf(" The run is new \n");
1823 if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
1824 //
1825 glrun->SetID(this->AssignRunID());
1826 glrun->SetID_RUN_FRAG(0);
1827 glrun->Fill_GL_RUN_FRAGMENTS(conn);
1828 //
1829 } else {
1830 if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
1831 return;
1832 };
1833 //
1834 //
1835 // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
1836 //
1837 if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
1838 // missing it no way we can found a piece in the frag table
1839 //
1840 oss.str("");
1841 oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1842 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1843 << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1844 << " ID != " << glrun->ID
1845 << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1846 //
1847 if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
1848 result = conn->Query(oss.str().c_str());
1849 //
1850 if ( !result ) throw -4;
1851 //
1852 row = result->Next();
1853 //
1854 if ( !row && NoFrag() ){
1855 //
1856 oss.str("");
1857 oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
1858 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1859 << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1860 << " ID != " << glrun->ID
1861 << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1862 //
1863 if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1864 result = conn->Query(oss.str().c_str());
1865 //
1866 if ( !result ) throw -4;
1867 //
1868 foundinrun = true;
1869 //
1870 row = result->Next();
1871 //
1872 };
1873 //
1874 if ( !row ){
1875 if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1876 found = false;
1877 } else {
1878 //
1879 found = false; // default value
1880 //
1881 if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
1882 //
1883 // if we have both runheader and runtrailer we can check with pkt_counter:
1884 //
1885 if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
1886 ULong64_t chkpkt = 0;
1887 ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
1888 ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
1889 //
1890 chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;
1891 //
1892 if ( labs(chkpkt-pktt)<2 ){
1893 //
1894 if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
1895 //
1896 found = true;
1897 //
1898 } else {
1899 //
1900 if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
1901 //
1902 found = false;
1903 //
1904 };
1905 };
1906 if ( !found ){
1907 //
1908 // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts
1909 //
1910 ULong64_t chkpkt1 = 0;
1911 ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
1912 ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
1913 chkpkt1 = labs(orunh1-dbrunt1);
1914 //
1915 ULong64_t chkpkt2 = 0;
1916 ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
1917 ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
1918 chkpkt2 = labs(orunh2-dbrunt2);
1919 //
1920 ULong64_t chkpkt3 = 0;
1921 ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
1922 ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
1923 chkpkt3 = labs(orunh3-dbrunt3);
1924 //
1925 if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
1926 // if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
1927 //
1928 if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
1929 //
1930 found = true;
1931 //
1932 } else {
1933 //
1934 if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
1935 //
1936 found = false;
1937 //
1938 };
1939 };
1940 };
1941 //
1942 if ( found ){
1943 //
1944 // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table
1945 //
1946 if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
1947 //
1948 if ( foundinrun ){
1949 glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
1950 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1951 };
1952 //
1953 GL_RUN *glrun1 = new GL_RUN();
1954 //
1955 // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
1956 //
1957 oss.str("");
1958 oss << " ID="<<row->GetField(0)<<";";
1959 //
1960 glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
1961 //
1962 // merge infos
1963 //
1964 UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
1965 ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
1966 UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
1967 ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
1968 if ( IsDebug() ) printf(" Check overlapping events: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
1969 TTree *T= 0;
1970 T = (TTree*)file->Get("Physics");
1971 if ( !T || T->IsZombie() ) throw -16;
1972 EventHeader *eh = 0;
1973 PscuHeader *ph = 0;
1974 T->SetBranchAddress("Header", &eh);
1975 while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
1976 T->GetEntry(firstev);
1977 ph = eh->GetPscuHeader();
1978 bpkt = PKT(ph->GetCounter());
1979 bobt = OBT(ph->GetOrbitalTime());
1980 firstev++;
1981 };
1982 if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
1983 //
1984 glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
1985 glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
1986 glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
1987 glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
1988 glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
1989 //
1990 glrun->SetEV_FROM(firstev);
1991 glrun->SetNEVENTS(lastev-firstev+1);
1992 //
1993 glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
1994 glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
1995 glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
1996 glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
1997 glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
1998 glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
1999 glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
2000 glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
2001 glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
2002 glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
2003 glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());
2004 glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());
2005 glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());
2006 glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());
2007 glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());
2008 glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());
2009 //
2010 if ( !IsRunAlreadyInserted() ){
2011 //
2012 // glrun->SetID(this->AssignRunID());
2013 glrun->SetID_RUN_FRAG(glrun1->GetID());
2014 glrun->Fill_GL_RUN(conn);
2015 //
2016 // set id number
2017 //
2018 glrun1->SetID_RUN_FRAG(glrun->GetID());
2019 glrun1->Fill_GL_RUN(conn);
2020 //
2021 };
2022 // delete old entry in fragment table
2023 //
2024 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2025 glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2026 //
2027 delete glrun1;
2028 //
2029 //
2030 return;
2031 //
2032 };
2033 //
2034 };
2035 //
2036 if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
2037 // missing it no way we can found a piece in the frag table
2038 //
2039 oss.str("");
2040 oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "
2041 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2042 << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2043 << " ID != " << glrun->ID
2044 << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2045 //
2046 if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
2047 result = conn->Query(oss.str().c_str());
2048 //
2049 if ( !result ) throw -4;
2050 //
2051 row = result->Next();
2052 //
2053 if ( !row && NoFrag() ){
2054 //
2055 oss.str("");
2056 oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
2057 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2058 << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2059 << " ID != " << glrun->ID
2060 << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2061 //
2062 if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2063 result = conn->Query(oss.str().c_str());
2064 //
2065 if ( !result ) throw -4;
2066 //
2067 foundinrun = true;
2068 row = result->Next();
2069 //
2070 };
2071 //
2072 if ( !row ){
2073 if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2074 found = false;
2075 } else {
2076 //
2077 found = false; // default value
2078 //
2079 if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
2080 //
2081 // if we have both runheader and runtrailer we can check with pkt_counter:
2082 //
2083 if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){
2084 ULong64_t chkpkt = 0;
2085 ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
2086 ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2087 //
2088 chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;
2089 //
2090 if ( labs(chkpkt-pktt)<2 ){
2091 //
2092 if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2093 //
2094 found = true;
2095 //
2096 } else {
2097 //
2098 if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
2099 //
2100 found = false;
2101 //
2102 };
2103 };
2104 if ( !found ){
2105 //
2106 // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts
2107 //
2108 ULong64_t chkpkt1 = 0;
2109 ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
2110 ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2111 chkpkt1 = labs(orunh1-dbrunt1);
2112 //
2113 ULong64_t chkpkt2 = 0;
2114 ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT());
2115 ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2116 chkpkt2 = labs(orunh2-dbrunt2);
2117 //
2118 ULong64_t chkpkt3 = 0;
2119 ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME());
2120 ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2121 chkpkt3 = labs(orunh3-dbrunt3);
2122 //
2123 if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2124 //
2125 if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2126 //
2127 found = true;
2128 //
2129 } else {
2130 //
2131 if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2132 //
2133 found = false;
2134 //
2135 };
2136 };
2137 };
2138 //
2139 if ( found ){
2140 //
2141 // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table
2142 //
2143 if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2144 //
2145 if ( foundinrun ){
2146 glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2147 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2148 };
2149 //
2150 GL_RUN *glrun1 = new GL_RUN();
2151 //
2152 // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2153 //
2154 oss.str("");
2155 oss << " ID="<<row->GetField(0)<<";";
2156 //
2157 glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runtrailer infos
2158 //
2159 // merge infos
2160 //
2161 UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT());
2162 ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
2163 UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
2164 ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
2165 if ( IsDebug() ) printf(" Check overlapping events: %i %i %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2166 TTree *T= 0;
2167 T = (TTree*)file->Get("Physics");
2168 if ( !T || T->IsZombie() ) throw -16;
2169 EventHeader *eh = 0;
2170 PscuHeader *ph = 0;
2171 T->SetBranchAddress("Header", &eh);
2172 while ( apkt > bpkt && aobt > bobt && lastev > 0 ){
2173 T->GetEntry(lastev);
2174 ph = eh->GetPscuHeader();
2175 apkt = PKT(ph->GetCounter());
2176 aobt = OBT(ph->GetOrbitalTime());
2177 lastev--;
2178 };
2179 if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2180 //
2181 glrun->SetEV_TO(lastev);
2182 glrun->SetNEVENTS(lastev-firstev+1);
2183 glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());
2184 glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER());
2185 glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME());
2186 glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
2187 glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
2188 //
2189 glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
2190 glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
2191 glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
2192 glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP());
2193 glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE());
2194 glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE());
2195 glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A());
2196 glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B());
2197 glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO());
2198 glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO());
2199 glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB());
2200 glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE());
2201 glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED());
2202 glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK());
2203 glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC());
2204 glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC());
2205 //
2206 if ( !IsRunAlreadyInserted() ){
2207 //
2208 // glrun->SetID(this->AssignRunID());
2209 //
2210 glrun->SetID_RUN_FRAG(glrun1->GetID());
2211 glrun->Fill_GL_RUN(conn);
2212 //
2213 // set id number
2214 //
2215 glrun1->SetID_RUN_FRAG(glrun->GetID());
2216 glrun1->Fill_GL_RUN(conn);
2217 //
2218 };
2219 //
2220 // delete old entries in fragment table
2221 //
2222 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2223 glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2224 //
2225 delete glrun1;
2226 //
2227 return;
2228 //
2229 };
2230 //
2231 };
2232 //
2233 if ( !found ){
2234 //
2235 if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
2236 //
2237 // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
2238 //
2239 oss.str("");
2240 oss << " SELECT ID FROM GL_RUN WHERE "
2241 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2242 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2243 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2244 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2245 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2246 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2247 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2248 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2249 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2250 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2251 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2252 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2253 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2254 //
2255 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
2256 result = conn->Query(oss.str().c_str());
2257 //
2258 if ( !result ) throw -4;
2259 //
2260 row = result->Next();
2261 //
2262 if ( row ){
2263 if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
2264 } else {
2265 if ( NoFrag() ){
2266 glrun->SetID_RUN_FRAG(glrun->GetID());
2267 glrun->Fill_GL_RUN(conn);
2268 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2269 };
2270 };
2271 };
2272 //
2273 return;
2274 };
2275
2276
2277 /**
2278 * Handle run without header or trailer
2279 **/
2280 void PamelaDBOperations::HandleMissingHoT(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
2281 //
2282 //
2283 // is the piece of run good (no other packets inside)?
2284 //
2285 if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
2286 //
2287 // if not, handle other pieces and continue with the first one
2288 //
2289 if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
2290 //
2291 } else {
2292 //
2293 this->FillClass(mishead,mistrail,firstev,lastev);
2294 //
2295 if ( !IsRunAlreadyInserted() ){
2296 glrun->SetID(this->AssignRunID());
2297 glrun->SetID_RUN_FRAG(0);
2298 glrun->Fill_GL_RUN(conn);
2299 };
2300 //
2301 };
2302 //
2303 return;
2304 };
2305
2306 /**
2307 *
2308 * check if we have non-physics packets inside the run
2309 *
2310 */
2311 Bool_t PamelaDBOperations::IsRunConsistent(Bool_t mishead, Bool_t mistrail, UInt_t &firstev, UInt_t &lastev){
2312 //
2313 EventCounter *code=0;
2314 //
2315 UInt_t nevent = 0;
2316 UInt_t checkfirst = 0;
2317 UInt_t checklast = 0;
2318 UInt_t firstentry = 0;
2319 UInt_t lastentry = 0;
2320 UInt_t firstTime = 0;
2321 UInt_t lastTime = 0;
2322 UInt_t firstPkt = 0;
2323 UInt_t lastPkt = 0;
2324 UInt_t firstObt = 0;
2325 UInt_t lastObt = 0;
2326 //
2327 pcksList packetsNames;
2328 pcksList::iterator Iter;
2329 getPacketsNames(packetsNames);
2330 //
2331 TTree *T= 0;
2332 T =(TTree*)file->Get("Physics");
2333 if ( !T || T->IsZombie() ) throw -16;
2334 EventHeader *eh = 0;
2335 PscuHeader *ph = 0;
2336 T->SetBranchAddress("Header", &eh);
2337 nevent = T->GetEntries();
2338 //
2339 //
2340 if ( firstev == lastev+1 ) { // no events inside the run!
2341 if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2342 // return true is correct
2343 return(true);
2344 //
2345 } else {
2346 //
2347 T->GetEntry(firstev);
2348 code = eh->GetCounter();
2349 checkfirst = 0;
2350 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2351 if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
2352 };
2353 if ( IsDebug() ) printf(" Check first is %i firstev is %i\n",checkfirst,firstev);
2354 //
2355 T->GetEntry(lastev);
2356 code = eh->GetCounter();
2357 checklast = 0;
2358 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2359 if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
2360 };
2361 if ( IsDebug() ) printf(" Check last is %i lastev is %i\n",checklast,lastev);
2362 //
2363 if ( checkfirst == checklast ){
2364 //
2365 if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2366 //
2367 return(true);
2368 //
2369 } else {
2370 //
2371 if ( IsDebug() ) printf(" There are no-phyics packets inside the run!\n");
2372 //
2373 // HERE WE MUST HANDLE THAT RUNS AND GO BACK
2374 //
2375 if ( IsDebug() ) printf(" Never seen this case, try to handle it anyway, it was throw -95\n");
2376 //
2377 Bool_t emptyruns = false;
2378 UInt_t check = 0;
2379 UInt_t lastevtemp = lastev;
2380 UInt_t firstevno = firstev;
2381 //
2382 for (UInt_t i=firstev; i<=lastev; i++){
2383 //
2384 T->GetEntry(i);
2385 code = eh->GetCounter();
2386 //
2387 check = 0;
2388 //
2389 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2390 if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
2391 };
2392 //
2393 if ( checkfirst < check || i == lastev ){
2394 //
2395 firstentry = firstevno;
2396 //
2397 if ( checkfirst < check ){
2398 lastentry = i-1;
2399 } else {
2400 lastentry = i;
2401 };
2402 //
2403 if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2404 //
2405 glrun->SetEV_FROM(firstentry);
2406 glrun->SetEV_TO(lastentry);
2407 if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
2408 if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
2409 lastentry--;
2410 };
2411 glrun->SetNEVENTS(lastentry-firstentry+1);
2412 //
2413 glrun->Set_GL_RUNH0();
2414 glrun->Set_GL_RUNT0();
2415 //
2416 glrun->SetLAST_TIMESYNC(0);
2417 glrun->SetOBT_TIMESYNC(0);
2418 //
2419 T->GetEntry(firstentry);
2420 ph = eh->GetPscuHeader();
2421 firstObt = ph->GetOrbitalTime();
2422 firstTime = this->GetAbsTime(firstObt);
2423 firstPkt = ph->GetCounter();
2424 //
2425 T->GetEntry(lastentry);
2426 ph = eh->GetPscuHeader();
2427 lastObt = ph->GetOrbitalTime();
2428 lastTime = this->GetAbsTime(lastObt);
2429 lastPkt = ph->GetCounter();
2430 //
2431 glrun->SetRUNHEADER_PKT(firstPkt);
2432 glrun->SetRUNTRAILER_PKT(lastPkt);
2433 //
2434 glrun->SetRUNHEADER_OBT(firstObt);
2435 glrun->SetRUNTRAILER_OBT(lastObt);
2436 //
2437 if ( firstev == firstentry && !emptyruns && !mishead ){
2438 glrun->Set_GL_RUNH(runh,phh);
2439 firstTime = this->GetAbsTime(phh->GetOrbitalTime());
2440 if ( IsDebug() ) printf(" We have the runheader \n");
2441 };
2442 if ( lastev == i && !mistrail ){
2443 glrun->Set_GL_RUNT(runt,pht);
2444 lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2445 if ( IsDebug() ) printf(" We have the runtrailer \n");
2446 };
2447 //
2448 if ( lastentry == (firstentry-2) ){ // no events in the run
2449 emptyruns = true;
2450 if ( IsDebug() ) printf(" No events in the run \n");
2451 lastTime = firstTime;
2452 if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
2453 lastObt = glrun->RUNHEADER_OBT;
2454 lastPkt = glrun->RUNHEADER_PKT;
2455 } else {
2456 lastObt = firstObt;
2457 lastPkt = firstPkt;
2458 };
2459 glrun->SetRUNTRAILER_PKT(lastPkt);
2460 glrun->SetRUNTRAILER_OBT(lastObt);
2461 lastentry++;
2462 };
2463 //
2464 this->SetCommonGLRUN(firstTime,lastTime);
2465 //
2466 if ( !IsRunAlreadyInserted() ){
2467 glrun->SetID(this->AssignRunID());
2468 glrun->SetID_RUN_FRAG(0);
2469 glrun->Fill_GL_RUN(conn);
2470 };
2471 //
2472 firstevno = lastentry + 1;
2473 //
2474 checkfirst = check;
2475 //
2476 };
2477 //
2478 if ( check == checklast && i != lastev ){
2479 lastevtemp = i - 1;
2480 i = lastev - 1;
2481 };
2482 //
2483 };
2484 //
2485 lastev = lastevtemp;
2486 //
2487 return(false);
2488 //
2489 };
2490 };
2491 //
2492 return(false); // should never arrive here
2493 };
2494
2495 /**
2496 *
2497 * we end up here when we have a runheader and a runtrailer but they seems not belonging to the same run since the number of events does not coincide with the
2498 * number of event written in the runtrailer. We try to split into different runs scanning the physics events from the runheader to the runtrailer and
2499 * looking for non-physics packets inside.
2500 *
2501 */
2502 void PamelaDBOperations::HandleSuspiciousRun(){
2503 //
2504 PacketType *pctp=0;
2505 EventCounter *codt=0;
2506 EventCounter *codh=0;
2507 EventCounter *code=0;
2508 UInt_t firstev = 0;
2509 UInt_t lastev = 0;
2510 UInt_t nevent = 0;
2511 UInt_t checkfirst = 0;
2512 UInt_t checklast = 0;
2513 UInt_t firstentry = 0;
2514 UInt_t lastentry = 0;
2515 UInt_t firstTime = 0;
2516 UInt_t lastTime = 0;
2517 UInt_t firstPkt = 0;
2518 UInt_t lastPkt = 0;
2519 UInt_t firstObt = 0;
2520 UInt_t lastObt = 0;
2521 //
2522 pcksList packetsNames;
2523 pcksList::iterator Iter;
2524 getPacketsNames(packetsNames);
2525 //
2526 TTree *rh=0;
2527 rh = (TTree*)file->Get("RunHeader");
2528 if ( !rh || rh->IsZombie() ) throw -17;
2529 TTree *T=0;
2530 T =(TTree*)file->Get("Physics");
2531 if ( !T || T->IsZombie() ) throw -16;
2532 EventHeader *eh = 0;
2533 PscuHeader *ph = 0;
2534 T->SetBranchAddress("Header", &eh);
2535 nevent = T->GetEntries();
2536 //
2537 codt = eht->GetCounter();
2538 codh = ehh->GetCounter();
2539 firstev = codh->Get(pctp->Physics);
2540 lastev = codt->Get(pctp->Physics)-1;
2541 //
2542 if ( firstev == lastev+1 ) { // no events inside the run!
2543 if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2544 //
2545 this->FillClass();
2546 if ( !IsRunAlreadyInserted() ){
2547 glrun->SetID(this->AssignRunID());
2548 glrun->SetID_RUN_FRAG(0);
2549 glrun->Fill_GL_RUN(conn);
2550 };
2551 //
2552 } else {
2553 //
2554 UInt_t nrunh = 0;
2555 UInt_t nrunh1 = 0;
2556 T->GetEntry(firstev);
2557 code = eh->GetCounter();
2558 checkfirst = 0;
2559 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2560 if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
2561 if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;
2562 };
2563 if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);
2564 //
2565 T->GetEntry(lastev);
2566 code = eh->GetCounter();
2567 checklast = 0;
2568 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2569 if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
2570 };
2571 if ( IsDebug() ) printf(" Check last is %i \n",checklast);
2572 //
2573 if ( checkfirst == checklast ){
2574 //
2575 if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2576 //
2577 this->FillClass();
2578 if ( !IsRunAlreadyInserted() ){
2579 glrun->SetID(this->AssignRunID());
2580 glrun->SetID_RUN_FRAG(0);
2581 glrun->Fill_GL_RUN(conn);
2582 };
2583 //
2584 } else {
2585 //
2586 if ( IsDebug() ) printf(" There are no-physics packets inside the run, try to separate runs \n");
2587 //
2588 Bool_t emptyruns = false;
2589 UInt_t check = 0;
2590 UInt_t firstevno = firstev;
2591 //
2592 for (UInt_t i=firstev; i<=lastev; i++){
2593 //
2594 T->GetEntry(i);
2595 code = eh->GetCounter();
2596 //
2597 check = 0;
2598 //
2599 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2600 if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
2601 if ( !strcmp(*Iter,"RunHeader") ) nrunh++;
2602 };
2603 //
2604 if ( checkfirst < check || i == lastev ){
2605 //
2606 firstentry = firstevno;
2607 //
2608 if ( checkfirst < check ){
2609 lastentry = i-1;
2610 } else {
2611 lastentry = i;
2612 };
2613 //
2614 if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2615 //
2616 glrun->SetEV_FROM(firstentry);
2617 glrun->SetEV_TO(lastentry);
2618 if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
2619 if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
2620 lastentry--;
2621 };
2622 glrun->SetNEVENTS(lastentry-firstentry+1);
2623 //
2624 glrun->Set_GL_RUNH0();
2625 glrun->Set_GL_RUNT0();
2626 //
2627 glrun->SetLAST_TIMESYNC(0);
2628 glrun->SetOBT_TIMESYNC(0);
2629 //
2630 T->GetEntry(firstentry);
2631 ph = eh->GetPscuHeader();
2632 firstObt = ph->GetOrbitalTime();
2633 firstTime = this->GetAbsTime(firstObt);
2634 firstPkt = ph->GetCounter();
2635 //
2636 T->GetEntry(lastentry);
2637 ph = eh->GetPscuHeader();
2638 lastObt = ph->GetOrbitalTime();
2639 lastTime = this->GetAbsTime(lastObt);
2640 lastPkt = ph->GetCounter();
2641 //
2642 glrun->SetRUNHEADER_PKT(firstPkt);
2643 glrun->SetRUNTRAILER_PKT(lastPkt);
2644 //
2645 glrun->SetRUNHEADER_OBT(firstObt);
2646 glrun->SetRUNTRAILER_OBT(lastObt);
2647 //
2648 if ( (firstev == firstentry && !emptyruns) || nrunh == (nrunh1 + 1) ){
2649 rh->GetEntry(nrunh1-1);
2650 phh = ehh->GetPscuHeader();
2651 nrunh1++;
2652 glrun->Set_GL_RUNH(runh,phh);
2653 firstTime = this->GetAbsTime(phh->GetOrbitalTime());
2654 if ( IsDebug() ) printf(" We have the runheader \n");
2655 };
2656 if ( lastev == i && checkfirst == check ){
2657 glrun->Set_GL_RUNT(runt,pht);
2658 lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2659 if ( IsDebug() ) printf(" We have the runtrailer \n");
2660 };
2661 //
2662 if ( lastentry == (firstentry-2) ){ // no events in the run
2663 emptyruns = true;
2664 if ( IsDebug() ) printf(" No events in the run \n");
2665 lastTime = firstTime;
2666 if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
2667 lastObt = glrun->RUNHEADER_OBT;
2668 lastPkt = glrun->RUNHEADER_PKT;
2669 } else {
2670 lastObt = firstObt;
2671 lastPkt = firstPkt;
2672 };
2673 glrun->SetRUNTRAILER_PKT(lastPkt);
2674 glrun->SetRUNTRAILER_OBT(lastObt);
2675 lastentry++;
2676 };
2677 //
2678 this->SetCommonGLRUN(firstTime,lastTime);
2679 //
2680 if ( !IsRunAlreadyInserted() ){
2681 glrun->SetID(this->AssignRunID());
2682 glrun->SetID_RUN_FRAG(0);
2683 glrun->Fill_GL_RUN(conn);
2684 };
2685 //
2686 if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check...
2687 //
2688 firstentry = i;
2689 //
2690 lastentry = i;
2691 //
2692 if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2693 //
2694 glrun->SetEV_FROM(firstentry);
2695 glrun->SetEV_TO(lastentry);
2696 glrun->SetNEVENTS(lastentry-firstentry+1);
2697 //
2698 glrun->Set_GL_RUNH0();
2699 //
2700 glrun->SetLAST_TIMESYNC(0);
2701 glrun->SetOBT_TIMESYNC(0);
2702 //
2703 T->GetEntry(firstentry);
2704 ph = eh->GetPscuHeader();
2705 firstObt = ph->GetOrbitalTime();
2706 firstTime = this->GetAbsTime(firstObt);
2707 firstPkt = ph->GetCounter();
2708 //
2709 glrun->SetRUNHEADER_PKT(firstPkt);
2710 //
2711 glrun->SetRUNHEADER_OBT(firstObt);
2712 //
2713 glrun->Set_GL_RUNT(runt,pht);
2714 lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2715 if ( IsDebug() ) printf(" We have the runtrailer \n");
2716 //
2717 this->SetCommonGLRUN(firstTime,lastTime);
2718 //
2719 if ( !IsRunAlreadyInserted() ){
2720 glrun->SetID(this->AssignRunID());
2721 glrun->SetID_RUN_FRAG(0);
2722 glrun->Fill_GL_RUN(conn);
2723 };
2724 };
2725 //
2726 firstevno = lastentry + 1;
2727 //
2728 checkfirst = check;
2729 //
2730 };
2731 //
2732 if ( check == checklast && i != lastev ) i = lastev - 1; // >>>>>>>>>>>>>>>>>>>>>>
2733 //
2734 };
2735 };
2736 };
2737 //
2738 return;
2739 };
2740
2741
2742 /**
2743 * Scan calorimeter calibrations packets, fill the GL_CALO_CALIB table
2744 */
2745 Int_t PamelaDBOperations::insertCALO_CALIB(){
2746 //
2747 TSQLResult *result = 0;
2748 TSQLRow *row = 0;
2749 //
2750 stringstream oss;
2751 oss.str("");
2752 //
2753 CalibCalPedEvent *calibCalPed = 0;
2754 TTree *tr = 0;
2755 EventHeader *eh = 0;
2756 PscuHeader *ph = 0;
2757 //
2758 UInt_t nevents = 0;
2759 UInt_t fromtime = 0;
2760 UInt_t totime = 0;
2761 UInt_t obt = 0;
2762 UInt_t pkt = 0;
2763 //
2764 tr = (TTree*)file->Get("CalibCalPed");
2765 if ( !tr || tr->IsZombie() ) throw -21;
2766 //
2767 tr->SetBranchAddress("CalibCalPed", &calibCalPed);
2768 tr->SetBranchAddress("Header", &eh);
2769 nevents = tr->GetEntries();
2770 //
2771 if ( !nevents ) return(1);
2772 //
2773 for (UInt_t i=0; i < nevents; i++){
2774 tr->GetEntry(i);
2775 for (UInt_t section = 0; section < 4; section++){
2776 //
2777 if ( calibCalPed->cstwerr[section] ){
2778 valid = 1;
2779 if ( calibCalPed->cperror[section] ) valid = 0;
2780 ph = eh->GetPscuHeader();
2781 obt = ph->GetOrbitalTime();
2782 pkt = ph->GetCounter();
2783 fromtime = this->GetAbsTime(ph->GetOrbitalTime());
2784 if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
2785 //
2786 if ( IsDebug() ) printf(" Calo calibration for section %i at time %i obt %i pkt %i \n",section,fromtime,obt,pkt);
2787 //
2788 // check if the calibration has already been inserted
2789 //
2790 oss.str("");
2791 oss << " SELECT ID FROM GL_CALO_CALIB WHERE "
2792 << " SECTION = "<< section << " AND "
2793 << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
2794 << " OBT = "<< obt << " AND "
2795 << " PKT = "<< pkt << ";";
2796 //
2797 if ( IsDebug() ) printf(" Check if the calo calibration has already been inserted: query is \n %s \n",oss.str().c_str());
2798 result = conn->Query(oss.str().c_str());
2799 //
2800 if ( !result ) throw -4;
2801 //
2802 row = result->Next();
2803 //
2804 if ( row ){
2805 //
2806 if ( IsDebug() ) printf(" Calo calibration already inserted in the DB\n");
2807 //
2808 } else {
2809 //
2810 // we have to insert a new calibration, check where to place it
2811 //
2812 oss.str("");
2813 oss << " SELECT ID,TO_TIME FROM GL_CALO_CALIB WHERE "
2814 << " SECTION = "<< section << " AND "
2815 << " FROM_TIME < "<< fromtime << " AND "
2816 << " TO_TIME > "<< fromtime << ";";
2817 //
2818 if ( IsDebug() ) printf(" Check where to place the calo calibration: query is \n %s \n",oss.str().c_str());
2819 result = conn->Query(oss.str().c_str());
2820 //
2821 if ( !result ) throw -4;
2822 //
2823 row = result->Next();
2824 //
2825 if ( !row ){
2826 //
2827 // no calibrations in the db contain our calibration
2828 //
2829 if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
2830 if ( fromtime < 1150871000 ){ //1150866904
2831 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
2832 fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
2833 };
2834 //
2835 oss.str("");
2836 oss << " SELECT FROM_TIME FROM GL_CALO_CALIB WHERE "
2837 << " SECTION = "<< section << " AND "
2838 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
2839 //
2840 if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
2841 result = conn->Query(oss.str().c_str());
2842 //
2843 if ( !result ) throw -4;
2844 //
2845 row = result->Next();
2846 if ( !row ){
2847 totime = numeric_limits<UInt_t>::max();
2848 } else {
2849 totime = (UInt_t)atoll(row->GetField(0));
2850 };
2851 //
2852 } else {
2853 //
2854 // determine upper and lower limits and make space for the new calibration
2855 //
2856 totime = (UInt_t)atoll(row->GetField(1));
2857 //
2858 oss.str("");
2859 oss << " UPDATE GL_CALO_CALIB SET "
2860 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
2861 << " ID = "<< row->GetField(0) << ";";
2862 //
2863 if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
2864 result = conn->Query(oss.str().c_str());
2865 //
2866 if ( !result ) throw -4;
2867 //
2868 };
2869 //
2870 oss.str("");
2871 oss << " INSERT INTO GL_CALO_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,OBT,PKT,BOOT_NUMBER,VALIDATION) "
2872 << " VALUES (NULL,' "
2873 << idroot << "','"
2874 << i << "','"
2875 << fromtime << "','"
2876 << totime << "','"
2877 << section << "','"
2878 << obt << "','"
2879 << pkt << "','"
2880 << this->GetBOOTnumber() << "','"
2881 << valid << "');";
2882 //
2883 if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
2884 //
2885 result = conn->Query(oss.str().c_str());
2886 //
2887 if ( !result ) throw -4;
2888 //
2889 };
2890 //
2891 } else {
2892 //
2893 if ( IsDebug() ) printf(" Repetead calo calibration for section %i at time %i obt %i pkt %i \n",section,fromtime,obt,pkt);
2894 //
2895 };
2896 //
2897 };
2898 };
2899 };
2900 //
2901 return(0);
2902 };
2903
2904 /**
2905 * Fill the GL_TRK_CALIB table
2906 */
2907 void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){
2908 //
2909 TSQLResult *result = 0;
2910 TSQLRow *row = 0;
2911 //
2912 stringstream oss;
2913 oss.str("");
2914 //
2915 UInt_t totime = 0;
2916 //
2917 if ( !pk1 && !pk2 ){
2918 if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n");
2919 };
2920 //
2921 // check if the calibration has already been inserted
2922 //
2923 oss.str("");
2924 oss << " SELECT ID FROM GL_TRK_CALIB WHERE "
2925 << " BOOT_NUMBER = "<< this->GetBOOTnumber(); //
2926 oss << " AND ( ( ";
2927 if ( pk1 ){
2928 oss << " OBT1 = "<< obt1 << " AND "
2929 << " PKT1 = "<< pkt1
2930 << " ) OR ( ";
2931 } else {
2932 oss << " PKT1 = "<< pkt2-1
2933 << " ) OR ( ";
2934 };
2935 if ( pk2 ){
2936 oss << " OBT2 = "<< obt2 << " AND "
2937 << " PKT2 = "<< pkt2;
2938 } else {
2939 oss << " PKT2 = "<< pkt1+1;
2940 };
2941 oss << " ) );";
2942 //
2943 if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str());
2944 result = conn->Query(oss.str().c_str());
2945 //
2946 if ( !result ) throw -4;
2947 //
2948 row = result->Next();
2949 //
2950 if ( row ){
2951 //
2952 if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");
2953 //
2954 } else {
2955 //
2956 // we have to insert a new calibration, check where to place it
2957 //
2958 oss.str("");
2959 oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "
2960 << " FROM_TIME < "<< fromtime << " AND "
2961 << " TO_TIME > "<< fromtime << ";";
2962 //
2963 if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());
2964 result = conn->Query(oss.str().c_str());
2965 //
2966 if ( !result ) throw -4;
2967 //
2968 row = result->Next();
2969 //
2970 if ( !row ){
2971 //
2972 // no calibrations in the db contain our calibration
2973 //
2974 if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
2975 if ( fromtime < 1150871000 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode
2976 //
2977 oss.str("");
2978 oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
2979 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
2980 //
2981 if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
2982 result = conn->Query(oss.str().c_str());
2983 //
2984 if ( !result ) throw -4;
2985 //
2986 row = result->Next();
2987 if ( !row ){
2988 totime = numeric_limits<UInt_t>::max();
2989 } else {
2990 totime = (UInt_t)atoll(row->GetField(0));
2991 };
2992 //
2993 } else {
2994 //
2995 // determine upper and lower limits and make space for the new calibration
2996 //
2997 totime = (UInt_t)atoll(row->GetField(1));
2998 //
2999 oss.str("");
3000 oss << " UPDATE GL_TRK_CALIB SET "
3001 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
3002 << " ID = "<< row->GetField(0) << ";";
3003 //
3004 if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str());
3005 result = conn->Query(oss.str().c_str());
3006 //
3007 if ( !result ) throw -4;
3008 //
3009 };
3010 //
3011 oss.str("");
3012 oss << " INSERT INTO GL_TRK_CALIB (ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION) "
3013 << " VALUES (NULL,' "
3014 << idroot << "',";
3015 //
3016 if ( !pk1 ){
3017 oss << "NULL,";
3018 } else {
3019 oss << "'"
3020 << t1 << "',";
3021 };
3022 //
3023 if ( !pk2 ){
3024 oss << "NULL,'";
3025 } else {
3026 oss << "'"
3027 << t2 << "','";
3028 };
3029 //
3030 oss << fromtime << "','"
3031 << totime << "','"
3032 << obt1 << "','"
3033 << pkt1 << "','"
3034 << obt2 << "','"
3035 << pkt2 << "','"
3036 << this->GetBOOTnumber() << "','"
3037 << valid << "');";
3038 //
3039 if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());
3040 //
3041 result = conn->Query(oss.str().c_str());
3042 //
3043 if ( !result ) throw -4;
3044 //
3045 };
3046 //
3047 };
3048
3049 /**
3050 * Scan tracker calibrations packets, fill the GL_TRK_CALIB table
3051 */
3052 Int_t PamelaDBOperations::insertTRK_CALIB(){
3053 //
3054 CalibTrk1Event *caltrk1 = 0;
3055 CalibTrk2Event *caltrk2 = 0;
3056 TTree *tr1 = 0;
3057 TTree *tr2 = 0;
3058 EventHeader *eh1 = 0;
3059 PscuHeader *ph1 = 0;
3060 EventHeader *eh2 = 0;
3061 PscuHeader *ph2 = 0;
3062 //
3063 PacketType *pctp=0;
3064 EventCounter *codt2=0;
3065 //
3066 Int_t nevents1 = 0;
3067 Int_t nevents2 = 0;
3068 //
3069 fromtime = 0;
3070 //
3071 obt1 = 0;
3072 pkt1 = 0;
3073 obt2 = 0;
3074 pkt2 = 0;
3075 //
3076 tr1 = (TTree*)file->Get("CalibTrk1");
3077 if ( !tr1 || tr1->IsZombie() ) throw -22;
3078 tr2 = (TTree*)file->Get("CalibTrk2");
3079 if ( !tr2 || tr2->IsZombie() ) throw -23;
3080 //
3081 tr1->SetBranchAddress("CalibTrk1", &caltrk1);
3082 tr1->SetBranchAddress("Header", &eh1);
3083 nevents1 = tr1->GetEntries();
3084 tr2->SetBranchAddress("CalibTrk2", &caltrk2);
3085 tr2->SetBranchAddress("Header", &eh2);
3086 nevents2 = tr2->GetEntries();
3087 //
3088 if ( !nevents1 && !nevents2 ) return(1);
3089 //
3090 t2 = -1;
3091 Int_t pret2 = 0;
3092 Int_t t2t1cal = 0;
3093 //
3094 for (t1=0; t1 < nevents1; t1++){
3095 //
3096 pret2 = t2;
3097 tr1->GetEntry(t1);
3098 //
3099 ph1 = eh1->GetPscuHeader();
3100 obt1 = ph1->GetOrbitalTime();
3101 pkt1 = ph1->GetCounter();
3102 fromtime = this->GetAbsTime(ph1->GetOrbitalTime());
3103 //
3104 // valid = 1;
3105 // //
3106 // if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1
3107 //
3108 //
3109 if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){
3110 //
3111 if ( IsDebug() ) printf(" Trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);
3112 //
3113 valid = ValidateTrkCalib( caltrk1, eh1 );
3114 if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
3115 //
3116 // Do we have the second calibration packet?
3117 //
3118 while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1
3119 //
3120 t2++;
3121 //
3122 if ( t2 < nevents2 ){
3123 tr2->GetEntry(t2);
3124 codt2 = eh2->GetCounter();
3125 t2t1cal = codt2->Get(pctp->CalibTrk1);
3126 //
3127 ph2 = eh2->GetPscuHeader();
3128 obt2 = ph2->GetOrbitalTime();
3129 pkt2 = ph2->GetCounter();
3130 //
3131 // if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2
3132 //
3133 } else {
3134 //
3135 // running out of vector without finding the corresponding calibration, sig
3136 //
3137 pret2 = t2;
3138 obt2 = 0;
3139 pkt2 = pkt1+2;
3140 t2t1cal = t1+1;
3141 };
3142 if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
3143 //
3144 // running out of vector without finding the corresponding calibration, sig
3145 //
3146 pret2 = t2;
3147 obt2 = 0;
3148 pkt2 = pkt1+2;
3149 t2t1cal = t1+1;
3150 };
3151 //
3152 };
3153 //
3154 if ( IsDebug() ) printf(" Found trk calibration2 at obt %i pkt %i t2 is %i \n",obt2,pkt2,t2);
3155 //
3156 // The calibration is good
3157 //
3158 if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){
3159 //
3160 if ( IsDebug() ) printf(" The trk calibration2 at obt %i pkt %i t2 is %i is good \n",obt2,pkt2,t2);
3161 //
3162 UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
3163 if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
3164 valid = valid & valid2;
3165 //
3166 // Handle good calib
3167 //
3168 this->HandleTRK_CALIB(true,true);
3169 //
3170 // Check for missing calibtrk1
3171 //
3172 if ( t2 != pret2+1 ){
3173 //
3174 if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %i pkt %i t2 is %i pret2 is %i \n",obt2,pkt2,t2,pret2);
3175 //
3176 while ( t2 > pret2+1 ){
3177 //
3178 // handle missing calib1
3179 //
3180 pret2++;
3181 //
3182 obt1 = 0;
3183 pkt1 = 0;
3184 //
3185 tr2->GetEntry(pret2);
3186 ph2 = eh2->GetPscuHeader();
3187 obt2 = ph2->GetOrbitalTime();
3188 pkt2 = ph2->GetCounter();
3189 //
3190 fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
3191 //
3192 valid = 0;
3193 this->HandleTRK_CALIB(false,true);
3194 //
3195 };
3196 //
3197 };
3198 //
3199 } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){
3200 //
3201 // Check for missing calibtrk2
3202 //
3203 if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %i pkt %i t2 is %i\n",obt2,pkt2,t2);
3204 t2 = pret2;
3205 //
3206 // handle missing calib2
3207 //
3208 obt2 = 0;
3209 pkt2 = 0;
3210 valid = 0;
3211 this->HandleTRK_CALIB(true,false);
3212 //
3213 };
3214 //
3215 } else {
3216 //
3217 if ( IsDebug() ) printf(" Repetead trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);
3218 //
3219 };
3220 //
3221 };
3222 //
3223 // we have one more calib pkt2 !
3224 //
3225 t2++;
3226 while ( t2 < nevents2 ){
3227 //
3228 // handle missing calib1
3229 //
3230 obt1 = 0;
3231 pkt1 = 0;
3232 //
3233 tr2->GetEntry(t2);
3234 ph2 = eh2->GetPscuHeader();
3235 obt2 = ph2->GetOrbitalTime();
3236 pkt2 = ph2->GetCounter();
3237 //
3238 fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
3239 valid = 0;
3240 if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){
3241 //
3242 if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %i pkt %i t2 is %i\n",obt2,pkt2,t2);
3243 //
3244 this->HandleTRK_CALIB(false,true);
3245 //
3246 };
3247 //
3248 t2++;
3249 //
3250 };
3251 //
3252 return(0);
3253 };
3254
3255
3256 /**
3257 * Scan S4 calibrations packets, fill the GL_S4_CALIB table
3258 */
3259 Int_t PamelaDBOperations::insertS4_CALIB(){
3260 //
3261 TSQLResult *result = 0;
3262 TSQLRow *row = 0;
3263 //
3264 stringstream oss;
3265 oss.str("");
3266 //
3267 TTree *tr = 0;
3268 EventHeader *eh = 0;
3269 PscuHeader *ph = 0;
3270 //
3271 UInt_t nevents = 0;
3272 UInt_t fromtime = 0;
3273 UInt_t totime = 0;
3274 UInt_t obt = 0;
3275 UInt_t pkt = 0;
3276 //
3277 tr = (TTree*)file->Get("CalibS4");
3278 if ( !tr || tr->IsZombie() ) throw -24;
3279 //
3280 tr->SetBranchAddress("Header", &eh);
3281 //
3282 nevents = tr->GetEntries();
3283 //
3284 if ( !nevents ) return(1);
3285 //
3286 for (UInt_t i = 0; i < nevents; i++){
3287 //
3288 tr->GetEntry(i);
3289 //
3290 ph = eh->GetPscuHeader();
3291 obt = ph->GetOrbitalTime();
3292 pkt = ph->GetCounter();
3293 fromtime = this->GetAbsTime(ph->GetOrbitalTime());
3294 if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
3295 //
3296 if ( IsDebug() ) printf(" S4 calibration at time %i obt %i pkt %i \n",fromtime,obt,pkt);
3297 //
3298 // check if the calibration has already been inserted
3299 //
3300 oss.str("");
3301 oss << " SELECT ID FROM GL_S4_CALIB WHERE "
3302 << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
3303 << " OBT = "<< obt << " AND "
3304 << " PKT = "<< pkt << ";";
3305 //
3306 if ( IsDebug() ) printf(" Check if the S4 calibration has already been inserted: query is \n %s \n",oss.str().c_str());
3307 result = conn->Query(oss.str().c_str());
3308 //
3309 if ( !result ) throw -4;
3310 //
3311 row = result->Next();
3312 //
3313 if ( row ){
3314 //
3315 if ( IsDebug() ) printf(" S4 calibration already inserted in the DB\n");
3316 //
3317 } else {
3318 //
3319 // we have to insert a new calibration, check where to place it
3320 //
3321 oss.str("");
3322 oss << " SELECT ID,TO_TIME FROM GL_S4_CALIB WHERE "
3323 << " FROM_TIME < "<< fromtime << " AND "
3324 << " TO_TIME > "<< fromtime << ";";
3325 //
3326 if ( IsDebug() ) printf(" Check where to place the S4 calibration: query is \n %s \n",oss.str().c_str());
3327 result = conn->Query(oss.str().c_str());
3328 //
3329 if ( !result ) throw -4;
3330 //
3331 row = result->Next();
3332 //
3333 if ( !row ){
3334 //
3335 // no calibrations in the db contain our calibration
3336 //
3337 if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");
3338 if ( fromtime < 1150871000 ){
3339 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
3340 fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
3341 };
3342 //
3343 oss.str("");
3344 oss << " SELECT FROM_TIME FROM GL_S4_CALIB WHERE "
3345 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
3346 //
3347 if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
3348 result = conn->Query(oss.str().c_str());
3349 //
3350 if ( !result ) throw -4;
3351 //
3352 row = result->Next();
3353 if ( !row ){
3354 totime = numeric_limits<UInt_t>::max();
3355 } else {
3356 totime = (UInt_t)atoll(row->GetField(0));
3357 };
3358 //
3359 } else {
3360 //
3361 // determine upper and lower limits and make space for the new calibration
3362 //
3363 totime = (UInt_t)atoll(row->GetField(1));
3364 //
3365 oss.str("");
3366 oss << " UPDATE GL_S4_CALIB SET "
3367 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
3368 << " ID = "<< row->GetField(0) << ";";
3369 //
3370 if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
3371 result = conn->Query(oss.str().c_str());
3372 //
3373 if ( !result ) throw -4;
3374 //
3375 };
3376 //
3377 oss.str("");
3378 oss << " INSERT INTO GL_S4_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,OBT,PKT,BOOT_NUMBER) "
3379 << " VALUES (NULL,' "
3380 << idroot << "','"
3381 << i << "','"
3382 << fromtime << "','"
3383 << totime << "','"
3384 << obt << "','"
3385 << pkt << "','"
3386 << this->GetBOOTnumber() << "');";
3387 //
3388 if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
3389 //
3390 result = conn->Query(oss.str().c_str());
3391 //
3392 if ( !result ) throw -4;
3393 //
3394 };
3395 //
3396 } else {
3397 //
3398 if ( IsDebug() ) printf(" Repetead S4 calibration at time %i obt %i pkt %i \n",fromtime,obt,pkt);
3399 //
3400 };
3401 //
3402 };
3403 //
3404 return(0);
3405 };
3406
3407 /**
3408 * Scan the fragment table and move old fragments to the GL_RUN table
3409 */
3410 Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3411 return(this->CleanGL_RUN_FRAGMENTS(""));
3412 };
3413
3414 /**
3415 * Scan the fragment table and move old fragments to the GL_RUN table
3416 */
3417 Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3418 //
3419 TSQLResult *result = 0;
3420 TSQLRow *row = 0;
3421 TSQLResult *result2 = 0;
3422 TSQLRow *row2 = 0;
3423 //
3424 UInt_t moved = 0;
3425 //
3426 stringstream oss;
3427 oss.str("");
3428 //
3429 if ( !strcmp(fcleanfile.Data(),"") ){
3430 //
3431 // check if there are entries older than "olderthan" seconds from now
3432 //
3433 oss.str("");
3434 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3435 << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3436 //
3437 if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
3438 result = conn->Query(oss.str().c_str());
3439 //
3440 } else {
3441 oss.str("");
3442 oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3443 if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3444 result = conn->Query(oss.str().c_str());
3445 //
3446 if ( result ){
3447 //
3448 row = result->Next();
3449 //
3450 if ( row ){
3451 oss.str("");
3452 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3453 << " ID_ROOT_L0=" << row->GetField(0) << ";";
3454 //
3455 if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3456 result = conn->Query(oss.str().c_str());
3457 //
3458 };
3459 } else {
3460 return(2);
3461 };
3462 };
3463 //
3464 if ( result ){
3465 //
3466 row = result->Next();
3467 //
3468 while ( row ){
3469 //
3470 oss.str("");
3471 oss << " ID= "<< row->GetField(0);
3472 //
3473 glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
3474 //
3475 oss.str("");
3476 oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
3477 << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
3478 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3479 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3480 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3481 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3482 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3483 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3484 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3485 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3486 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3487 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3488 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3489 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3490 //
3491 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3492 result2 = conn->Query(oss.str().c_str());
3493 //
3494 if ( !result2 ) throw -4;
3495 //
3496 row2 = result2->Next();
3497 //
3498 if ( !row2 ){
3499 //
3500 if ( IsDebug() ) printf(" The run is new \n");
3501 if ( IsDebug() ) printf(" -> fill the DB \n");
3502 //
3503 // glrun->SetID(this->AssignRunID()); we use the old run number!
3504 glrun->SetID_RUN_FRAG(glrun->GetID());
3505 glrun->Fill_GL_RUN(conn);
3506 //
3507 // oss.str("");
3508 // oss << " SELECT ID FROM GL_RUN WHERE "
3509 // << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3510 // << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3511 // << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3512 // << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3513 // << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3514 // //
3515 // if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
3516 // result2 = conn->Query(oss.str().c_str());
3517 // //
3518 // if ( !result2 ) throw -4;
3519 // //
3520 // row2 = result2->Next();
3521 // //
3522 // if ( !row2 ) throw -25;
3523 // //
3524 // oss.str("");
3525 // oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);
3526 // if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());
3527 // result2 = conn->Query(oss.str().c_str());
3528 // //
3529 // if ( !result2 ) throw -4;
3530 //
3531 moved++;
3532 //
3533 } else {
3534 if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");
3535 };
3536 if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));
3537 //
3538 //
3539 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3540 // oss.str("");
3541 // oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
3542 // if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
3543 // result2 = conn->Query(oss.str().c_str());
3544 // //
3545 // if ( !result2 ) throw -4;
3546 // //
3547 row = result->Next();
3548 };
3549 };
3550 if ( IsDebug() ) printf(" Moved %u runs\n",moved);
3551 return(0);
3552 };
3553
3554 /**
3555 * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3556 */
3557 Int_t PamelaDBOperations::ValidateRuns(){
3558 return(this->ValidateRuns(""));
3559 };
3560
3561 /**
3562 * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3563 */
3564 Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3565 //
3566 TSQLResult *result = 0;
3567 TSQLRow *row = 0;
3568 //
3569 UInt_t calibtime = 50;
3570 //
3571 stringstream oss;
3572 oss.str("");
3573 //
3574 // =======================================================
3575 // validate runs by checking missing calibrations
3576 // =======================================================
3577 UInt_t t_stop = 0;
3578 UInt_t t_start = 0;
3579 if ( !strcmp(valfile.Data(),"") ) {
3580 // --------------------------------------------------------------
3581 // 1) get the OBT of the last run inserted after clean-time limit
3582 // --------------------------------------------------------------
3583 oss.str("");
3584 oss << " SELECT * FROM GL_RUN WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3585 << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3586 if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3587 result = conn->Query(oss.str().c_str());
3588 if ( !result ) throw -4;
3589 if ( !result->GetRowCount() ) {
3590 printf(" No runs to validate \n");
3591 return(1);
3592 }else{
3593 row = result->Next();
3594 t_start = (UInt_t)atoll(row->GetField(4));
3595 };
3596 // --------------------------------------------------------------
3597 // 2) get the OBT of the last validated run
3598 // --------------------------------------------------------------
3599 oss.str("");
3600 oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3601 <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3602 if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3603 result = conn->Query(oss.str().c_str());
3604 if ( !result ) throw -4;
3605 if ( result->GetRowCount() ){
3606 row = result->Next();
3607 t_stop = (UInt_t)atoll(row->GetField(4));
3608 };
3609 if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);
3610 // --------------------------------------------------------------
3611 // now retrieves runs to be validated
3612 // --------------------------------------------------------------
3613 oss.str("");
3614 oss << " SELECT * FROM GL_RUN WHERE RUNHEADER_TIME <=" << t_start;
3615 oss << " AND RUNHEADER_TIME >="<< t_stop;
3616 oss << " ORDER BY RUNHEADER_TIME DESC;";
3617 if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3618 result = conn->Query(oss.str().c_str());
3619 } else {
3620 //
3621 stringstream myquery;
3622 UInt_t myid = 0;
3623 myquery.str("");
3624 myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3625 //
3626 result = conn->Query(myquery.str().c_str());
3627 //
3628 row = result->Next();
3629 if( !row ){
3630 if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3631 if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3632 return(2);
3633 };
3634 if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3635 return(0);
3636 };
3637 myid=(UInt_t)atoll(row->GetField(0));
3638 //
3639 myquery.str("");
3640 myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3641 //
3642 result = conn->Query(myquery.str().c_str());
3643 //
3644 row = result->Next();
3645 if( !row->GetField(0) || !row->GetField(1)){
3646 //
3647 if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3648 //
3649 return(0);
3650 //
3651 } else {
3652 //
3653 UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3654 UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3655 UInt_t caltime = 0;
3656 //
3657 myquery.str("");
3658 myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3659 myquery << " order by FROM_TIME asc limit 1;";
3660 //
3661 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
3662 //
3663 //
3664 result = conn->Query(myquery.str().c_str());
3665 //
3666 row = result->Next();
3667 if( !row ){
3668 caltime = runhtime;
3669 } else {
3670 caltime = (UInt_t)atoll(row->GetField(0));
3671 };
3672 //
3673 myquery.str("");
3674 myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3675 myquery << caltime << " order by RUNHEADER_TIME DESC";
3676 //
3677 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
3678 //
3679 result = conn->Query(myquery.str().c_str());
3680 //
3681 };
3682 };
3683 //
3684 if ( !result ) throw -4;
3685 if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3686 //
3687 Int_t nrow = 0;
3688 GL_RUN* this_run = new GL_RUN();
3689 GL_RUN* next_run = new GL_RUN();
3690 Int_t nseq_max = 1000;
3691 // UInt_t* sequence = new UInt_t[100];
3692 vector<UInt_t> sequence(nseq_max);
3693 Int_t nseq = 0;
3694 Bool_t CHECK = false;
3695 Bool_t this_ONLINE = false;
3696 Bool_t next_ONLINE = false;
3697 UInt_t t1=0,t2=0;
3698 // ---------------------------------------------------------------------------------
3699 // - loop over runs, back in time,
3700 // - select sequences of runs close in time (less than calibtime s apart),
3701 // which could be preceeded by a calibration
3702 // - check if there might be a missing calibration
3703 // ---------------------------------------------------------------------------------
3704 while(1){
3705
3706 row = result->Next();
3707 if( row == NULL ) break;
3708
3709 //------------
3710 //get run info
3711 //------------
3712 this_run->Set_GL_RUN(row);
3713
3714 Bool_t this_BAD = false;
3715 if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;
3716 else if (this_run->GetTRK_CALIB_USED() == 104) this_ONLINE = false;
3717 else{
3718 // printf("Missing or corrupted header!! \n");
3719 this_ONLINE = false;
3720 this_BAD = true;
3721 };
3722
3723 //-----------------------------------
3724 //compare with previous(next in time)
3725 //-----------------------------------
3726 CHECK = false;
3727 UInt_t interval=0;
3728
3729 if( nrow != 0){
3730
3731
3732 t1 = this_run->GetRUNTRAILER_TIME();
3733 t2 = next_run->GetRUNHEADER_TIME();
3734 interval = (t2-t1);
3735
3736 if(this_ONLINE && next_ONLINE){ // this: ON-LINE + next: ON-LINE
3737
3738 if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0; //=> run fragments
3739
3740 if( interval >= calibtime )CHECK = true; //more than calibtime s => there might be a calibration
3741
3742 if( !CHECK && this_run->VALIDATION ){
3743 for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
3744 nseq=0;
3745 }
3746
3747 }else if( !this_ONLINE && next_ONLINE) { // this: DEFAULT + next:ON-LINE
3748
3749 CHECK = true;
3750
3751 }else if( !next_ONLINE ){ // this:ANY + next:DEFAULT
3752
3753 assignVALIDATION(next_run->ID,true);
3754 nseq=0;
3755 }
3756 }
3757
3758 //----------------------------
3759 //check run sequence for calib
3760 //----------------------------
3761 if( CHECK ){
3762 // check if calibration exists
3763 if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
3764 Bool_t MISSING = MissingTRK_CALIB(t1,t2);
3765 for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
3766 nseq=0;
3767 };
3768 //--------------
3769 //store run info
3770 //--------------
3771 *next_run = *this_run;
3772 next_ONLINE = this_ONLINE;
3773 if( !this_BAD ){
3774 if(nseq < nseq_max){
3775 sequence[nseq] = this_run->ID;
3776 nseq++;
3777 }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
3778 };
3779
3780 if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID);
3781 nrow++;
3782
3783 };
3784 delete this_run;
3785 delete next_run;
3786 //
3787 return(0);
3788 };
3789 /**
3790 * Check if there might be a missing tracker calibration in a given time interval
3791 * @param t1 From absolute time
3792 * @param t2 To absolute time
3793 * @return true if there might be a missing calibration
3794 */
3795 Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
3796
3797 GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
3798
3799 // get the closest VALIDATED calibration before the run start (t2)
3800 if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true); //>>> missing
3801
3802 if ( trkcalib->TO_TIME < t2 ) return(true); //>>> missing
3803
3804 //==============================================================
3805 // Check is done first on the basis of time between calibration,
3806 // which should be equal to the time between ascending-nodes.
3807 //==============================================================
3808 if ( t2 - trkcalib->FROM_TIME > 5700) {
3809 if ( IsDebug() )printf("Long time between calib and run start %i :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);
3810 //==============================================================
3811 // there might be a missing calibration, due to:
3812 // - MM full
3813 // - corrupted packets
3814 // - loss of data
3815 // There is an exception in case a download was done during ascending node
3816 //==============================================================
3817 Bool_t DOWNLOAD = false;
3818 // check if the calib was skipped becouse of download .... DA FARE!!
3819 if(DOWNLOAD)return(false);
3820
3821 return(true); //>>> missing
3822
3823 };
3824
3825 //==============================================================
3826 // If the last calibration is close to the run less than this time,
3827 // it is enough to say that there are no missing calibrations
3828 //==============================================================
3829 // the long time interval bewteen runs might be due to download
3830 if ( IsDebug() )printf("Short time between calib and run start %i :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
3831 return(false);
3832
3833 };
3834 /**
3835 * Assign VALIDATION value to a GL_RUN entry
3836 * @param idrun Run ID
3837 * @param validation true/false
3838 */
3839 Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
3840 TSQLResult *result = 0;
3841 stringstream oss;
3842 oss.str("");
3843 oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
3844 //
3845 // if ( IsDebug() )
3846 // printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
3847 if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
3848 result = conn->Query(oss.str().c_str());
3849 if ( !result ) throw -4;
3850 return(0);
3851 }
3852
3853
3854
3855 // Insert TLEs from file tlefilename in the table GL_TLE in the db
3856 // opened by conn, sorting them by date from older to newer, if each
3857 // TLE has not been alread inserted.
3858 Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
3859 {
3860 fstream tlefile(tlefilename, ios::in);
3861
3862 if ( !tlefile ) throw -7;
3863
3864 vector<cTle*> ctles;
3865 vector<cTle*>::iterator iter;
3866 int present = 0;
3867
3868 // Get three lines from tlefile, create a cTle object and put it
3869 // into ctles
3870 while(1) {
3871 cTle *tlef;
3872 string str1, str2, str3;
3873
3874 getline(tlefile, str1);
3875 if(tlefile.eof()) break;
3876
3877 getline(tlefile, str2);
3878 if(tlefile.eof()) break;
3879
3880 getline(tlefile, str3);
3881 if(tlefile.eof()) break;
3882
3883 // We now have three good lines for a cTle.
3884 tlef = new cTle(str1, str2, str3);
3885 ctles.push_back(tlef);
3886 }
3887
3888 tlefile.close();
3889
3890 // Sort by date
3891 sort(ctles.begin(), ctles.end(), compTLE);
3892
3893 // Now we insert each TLE into the db
3894 for(iter = ctles.begin(); iter != ctles.end(); iter++) {
3895 cTle *tle = *iter;
3896
3897 // Do nothing if it's already present in the db. Just increase
3898 // the counter present.
3899 if (! isTlePresent(tle))
3900 {
3901 int status = insertTle(tle);
3902
3903 // Insert query failed. Return 1.
3904 if(status == EXIT_FAILURE) {
3905
3906 if( IsDebug() ) {
3907 cerr << "Error: inserting TLE:" << endl
3908 << tle->getName() << endl
3909 << tle->getLine1() << endl
3910 << tle->getLine2() << endl;
3911 }
3912
3913 throw -4;
3914 return 1;
3915 }
3916
3917 }
3918 else
3919 present++;
3920
3921 }
3922
3923 int inserted = ctles.size() - present; // Number of inserted TLE.
3924 if ( IsDebug() )
3925 cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
3926 << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
3927
3928 ctles.clear();
3929
3930
3931 // Return 2 if no new TLE has been inserted. 0 otherwise.
3932 if(! inserted ) return 2;
3933 return 0;
3934 }
3935
3936
3937 // Insert tle in the table GL_TLE using the connection conn.
3938 Int_t PamelaDBOperations::insertTle(cTle *tle)
3939 {
3940 stringstream oss;
3941 TSQLResult *result = 0;
3942
3943 oss.str("");
3944 oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
3945 << " VALUES ( '"
3946 << tle->getName() << "', '"
3947 << tle->getLine1() << "', '"
3948 << tle->getLine2() << "', '"
3949 << getTleDatetime(tle) << "')";
3950
3951 // cout << oss.str().c_str() << endl;
3952 result = conn->Query(oss.str().c_str());
3953 if (result == NULL)
3954 return EXIT_FAILURE;
3955
3956 return EXIT_SUCCESS;
3957 }
3958
3959
3960 // Return whether tle is already in the db connected by conn.
3961 bool PamelaDBOperations::isTlePresent(cTle *tle)
3962 {
3963 stringstream oss;
3964 TSQLResult *result = 0;
3965
3966 oss.str("");
3967 oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
3968 << getTleDatetime(tle) << "'";
3969
3970 result = conn->Query(oss.str().c_str());
3971 if (result == NULL) throw -4;
3972
3973 if (result->GetRowCount())
3974 return true;
3975 else
3976 return false;
3977 }
3978
3979
3980 // Return whether the first TLE is dated early than the second
3981 bool compTLE (cTle *tle1, cTle *tle2)
3982 {
3983 return getTleJulian(tle1) < getTleJulian(tle2);
3984 }
3985
3986
3987 // Return the date of the tle using the format (year-2000)*1e3 +
3988 // julian day. e.g. 6365 is the 31th Dec 2006.
3989 // It does *not* return a cJulian date.
3990 float getTleJulian(cTle *tle) {
3991 return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
3992 }
3993
3994
3995 // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
3996 // format.
3997 string getTleDatetime(cTle *tle)
3998 {
3999 int year, mon, day, hh, mm, ss;
4000 double dom; // day of month (is double!)
4001 stringstream date; // date in datetime format
4002
4003 // create a cJulian from the date in tle
4004 cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
4005
4006 // get year, month, day of month
4007 jdate.getComponent(&year, &mon, &dom);
4008
4009 // build a datetime YYYY-MM-DD hh:mm:ss
4010 date.str("");
4011 day = (int) floor(dom);
4012 hh = (int) floor( (dom - day) * 24);
4013 mm = (int) floor( ((dom - day) * 24 - hh) * 60);
4014 ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
4015 // ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
4016
4017 date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
4018
4019 return date.str();
4020 }
4021
4022 /**
4023 * Remove a file from the DB, delete on cascade all entries related to that file
4024 * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
4025 * calibration
4026 **/
4027 Int_t PamelaDBOperations::removeFile(TString remfile){
4028 //
4029 // Determine ID_ROOT_L0 and ID_RAW
4030 //
4031 TSQLResult *pResult;
4032 TSQLRow *Row;
4033 stringstream myquery;
4034 //
4035 myquery.str("");
4036 myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
4037 //
4038 pResult = conn->Query(myquery.str().c_str());
4039 //
4040 Row = pResult->Next();
4041 if( !Row ){
4042 if ( strcmp(remfile.Data(),GetRootName().Data()) ){
4043 if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
4044 return(1);
4045 };
4046 if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
4047 return(0);
4048 };
4049 //
4050 this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
4051 this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
4052 //
4053 this->ValidationOFF();
4054 //
4055 this->RemoveCALIBS();
4056 //
4057 this->RemoveRUNS();
4058 //
4059 this->RemoveFILES();
4060 //
4061 this->SetID_ROOT(0);
4062 this->SetID_RAW(0);
4063 //
4064 return(0);
4065 };
4066
4067 /**
4068 *
4069 * Set validation bit to zero for runs following the removing file till
4070 * 1) a run with TRK_CALIB_USED=140
4071 * 2) a run with VALIDATION = 0
4072 * 3) the next calibration
4073 *
4074 **/
4075 void PamelaDBOperations::ValidationOFF(){
4076 TSQLResult *pResult;
4077 TSQLRow *Row;
4078 stringstream myquery;
4079 Int_t unv = 0;
4080 //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
4081 myquery.str("");
4082 myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4083 //
4084 pResult = conn->Query(myquery.str().c_str());
4085 //
4086 Row = pResult->Next();
4087 if( !Row->GetField(0) ){
4088 //
4089 if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
4090 //
4091 } else {
4092 //
4093 UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
4094 UInt_t caltime = 0;
4095 //
4096 myquery.str("");
4097 myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
4098 myquery << " order by FROM_TIME asc limit 1;";
4099 //
4100 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4101 //
4102 //
4103 delete pResult;
4104 pResult = conn->Query(myquery.str().c_str());
4105 //
4106 Row = pResult->Next();
4107 if( !Row ){
4108 caltime = runhtime;
4109 } else {
4110 caltime = (UInt_t)atoll(Row->GetField(0));
4111 };
4112 //
4113 myquery.str("");
4114 myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4115 myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4116 //
4117 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4118 //
4119 pResult = conn->Query(myquery.str().c_str());
4120 //
4121 Row = pResult->Next();
4122 if( !Row ){
4123 //
4124 if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4125 //
4126 } else {
4127 myquery.str("");
4128 myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4129 myquery << " RUNHEADER_TIME>=" <<runhtime;
4130 myquery << " order by RUNHEADER_TIME asc;";
4131 //
4132 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4133 //
4134 pResult = conn->Query(myquery.str().c_str());
4135 //
4136 Row = pResult->Next();
4137 while ( Row ){
4138 //
4139 unv++;
4140 this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4141 Row = pResult->Next();
4142 //
4143 };
4144 };
4145 };
4146 if ( IsDebug() ) printf(" %i runs have been unvalidated \n",unv);
4147 };
4148
4149 /**
4150 *
4151 * Rearrange GL_RUN table and remove runs
4152 *
4153 **/
4154 void PamelaDBOperations::RemoveRUNS(){
4155 TSQLResult *pResult;
4156 TSQLRow *Row;
4157 stringstream myquery;
4158 UInt_t drun = 0;
4159 GL_RUN *delrun = new GL_RUN();
4160 //
4161 myquery.str("");
4162 myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4163 //
4164 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4165 //
4166 pResult = conn->Query(myquery.str().c_str());
4167 //
4168 Row = pResult->Next();
4169 //
4170 //
4171 if ( !Row ){
4172 if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4173 } else {
4174 if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");
4175 while ( Row ){
4176 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4177 if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4178 drun++;
4179 Row = pResult->Next();
4180 };
4181 };
4182 //
4183 //
4184 myquery.str("");
4185 myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4186 //
4187 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4188 //
4189 pResult = conn->Query(myquery.str().c_str());
4190 //
4191 Row = pResult->Next();
4192 //
4193 if ( !Row ){
4194 if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4195 } else {
4196 if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");
4197 while ( Row ){
4198 if ( IsDebug() ) printf(" restore run %i \n",(UInt_t)atoll(Row->GetField(1)));
4199 delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4200 if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(1)));
4201 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4202 if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4203 if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4204 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4205 };
4206 drun++;
4207 Row = pResult->Next();
4208 };
4209 };
4210 //
4211 if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);
4212 //
4213 //
4214 //
4215 drun = 0;
4216 //
4217 myquery.str("");
4218 myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4219 //
4220 pResult = conn->Query(myquery.str().c_str());
4221 //
4222 Row = pResult->Next();
4223 //
4224 if ( !Row ){
4225 if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4226 } else {
4227 if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");
4228 while ( Row ){
4229 if ( IsDebug() ) printf(" del run idtrash %i \n",(UInt_t)atoll(Row->GetField(0)));
4230 myquery.str("");
4231 myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";
4232 conn->Query(myquery.str().c_str());
4233 drun++;
4234 Row = pResult->Next();
4235 };
4236 };
4237 //
4238 if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_TRASH table \n",drun);
4239 //
4240 //
4241 //
4242 drun = 0;
4243 //
4244 myquery.str("");
4245 myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4246 //
4247 pResult = conn->Query(myquery.str().c_str());
4248 //
4249 Row = pResult->Next();
4250 //
4251 if ( !Row ){
4252 if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4253 } else {
4254 if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");
4255 while ( Row ){
4256 if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4257 myquery.str("");
4258 myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4259 conn->Query(myquery.str().c_str());
4260 drun++;
4261 Row = pResult->Next();
4262 };
4263 };
4264 //
4265 if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_FRAGMENTS table \n",drun);
4266 //
4267 //
4268 //
4269 delete delrun;
4270 //
4271 };
4272
4273
4274 /**
4275 *
4276 * Rearrange calibration tables
4277 *
4278 **/
4279 void PamelaDBOperations::RemoveFILES(){
4280 stringstream myquery;
4281 //
4282 myquery.str("");
4283 myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4284 //
4285 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4286 //
4287 conn->Query(myquery.str().c_str());
4288 //
4289 };
4290
4291 /**
4292 *
4293 * Rearrange calibration tables
4294 *
4295 **/
4296 void PamelaDBOperations::RemoveCALIBS(){
4297 TSQLResult *pResult;
4298 TSQLRow *Row;
4299 stringstream myquery;
4300 //
4301 //
4302 // Calorimeter
4303 //
4304 for (Int_t section = 0; section < 4; section++){
4305 myquery.str("");
4306 myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4307 myquery << " SECTION=" << section << ";";
4308 //
4309 pResult = conn->Query(myquery.str().c_str());
4310 //
4311 Row = pResult->Next();
4312 if( !Row->GetField(0) || !Row->GetField(1) ){
4313 //
4314 if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4315 //
4316 } else {
4317 //
4318 myquery.str("");
4319 myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4320 myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4321 myquery << " SECTION=" << section << ";";
4322 //
4323 pResult = conn->Query(myquery.str().c_str());
4324 //
4325 if( !pResult ){
4326 //
4327 if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4328 //
4329 throw -4;
4330 //
4331 };
4332 //
4333 };
4334 };
4335 myquery.str("");
4336 myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4337 //
4338 pResult = conn->Query(myquery.str().c_str());
4339 //
4340 if( !pResult ){
4341 //
4342 if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4343 //
4344 throw -4;
4345 //
4346 };
4347 //
4348 // Tracker
4349 //
4350 myquery.str("");
4351 myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4352 //
4353 pResult = conn->Query(myquery.str().c_str());
4354 //
4355 Row = pResult->Next();
4356 if( !Row->GetField(0) || !Row->GetField(1) ){
4357 //
4358 if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4359 //
4360 } else {
4361 //
4362 myquery.str("");
4363 myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4364 myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4365 //
4366 pResult = conn->Query(myquery.str().c_str());
4367 //
4368 if( !pResult ){
4369 //
4370 if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4371 //
4372 throw -4;
4373 //
4374 };
4375 //
4376 myquery.str("");
4377 myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4378 //
4379 pResult = conn->Query(myquery.str().c_str());
4380 //
4381 if( !pResult ){
4382 //
4383 if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4384 //
4385 throw -4;
4386 //
4387 };
4388 };
4389 //
4390 //
4391 // S4
4392 //
4393 myquery.str("");
4394 myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4395 //
4396 pResult = conn->Query(myquery.str().c_str());
4397 //
4398 Row = pResult->Next();
4399 if( !Row->GetField(0) || !Row->GetField(1) ){
4400 //
4401 if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4402 //
4403 } else {
4404 //
4405 myquery.str("");
4406 myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4407 myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4408 //
4409 pResult = conn->Query(myquery.str().c_str());
4410 //
4411 if( !pResult ){
4412 //
4413 if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4414 //
4415 throw -4;
4416 //
4417 };
4418 //
4419 myquery.str("");
4420 myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4421 //
4422 pResult = conn->Query(myquery.str().c_str());
4423 //
4424 if( !pResult ){
4425 //
4426 if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4427 //
4428 throw -4;
4429 //
4430 };
4431 //
4432 };
4433 };
4434
4435 /**
4436 *
4437 * Rearrange calibration tables
4438 *
4439 **/
4440 UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
4441
4442 Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
4443 UInt_t timeaftercalib=120000; //2000;
4444 // ----------
4445 // Check CRCs
4446 // ----------
4447 for(Int_t ipkt=0; ipkt<6; ipkt++){
4448 if( caltrk->crc_hcal[ipkt] )return 0; // :-(
4449 for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
4450 }
4451 // -----------------------
4452 // Check missing packets:
4453 // -----------------------
4454 // Readout order:
4455 // ------------------
4456 // DSP packet board
4457 // ------------------
4458 // 12 0 1
4459 // 10 1 1
4460 // 8 2 1
4461 // 4 3 1
4462 // 6 4 1
4463 // 2 5 1
4464 // ------------------
4465 // 11 0 2
4466 // 9 1 2
4467 // 7 2 2
4468 // 3 3 2
4469 // 5 4 2
4470 // 1 5 2
4471 // ------------------
4472 // -------------------------------------------------
4473 // Check if it is first or second calibration packet
4474 // -------------------------------------------------
4475 UInt_t build=0;
4476 TString classname = caltrk->GetName();
4477 UInt_t base=0;
4478 UInt_t mask=0;
4479 if(classname.Contains("CalibTrk1Event")){
4480 base=12;
4481 mask=0x03F000;
4482 }
4483 if(classname.Contains("CalibTrk2Event")){
4484 base=18;
4485 mask=0xFC0000;
4486 }
4487 // -------------------------------------------------
4488 // Count number of packets and set build variable
4489 // -------------------------------------------------
4490 Int_t npkts=0;
4491 for(Int_t ipkt=0; ipkt<6; ipkt++){
4492 if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
4493 npkts++;
4494 build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
4495 }
4496 }
4497 // if( npkts==6 )return 1; // :-)
4498
4499 // cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
4500
4501 // -----------------------------------------------
4502 // If missing packets: check the acq configuration
4503 // (some DSPs might be excluded from acquisition)
4504 // -----------------------------------------------
4505
4506 // -----------------------------------------------
4507 // retrieve the first run header after calib
4508 // -----------------------------------------------
4509 PacketType *pctp;
4510 EventCounter *cod;
4511 cod = eh->GetCounter();
4512 Int_t irun = cod->Get(pctp->RunHeader);
4513 TTree *rh=(TTree*)file->Get("RunHeader");
4514 if ( !rh || rh->IsZombie() ) throw -17;
4515 if( rh->GetEntries() == irun ){
4516 if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1) -- cannot validate :-( "<<endl;
4517 return 0; // :-(
4518 }
4519
4520 RunHeaderEvent *run = 0;
4521 EventHeader *hrun = 0;
4522 rh->SetBranchAddress("RunHeader", &run);
4523 rh->SetBranchAddress("Header", &hrun);
4524 rh->GetEntry(irun);
4525 // cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
4526
4527 if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
4528 if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
4529 return 0; // :-(
4530 }
4531
4532 if( !run->RM_ACQ_AFTER_CALIB ){
4533 if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0 -- cannot validate :-( "<<endl;
4534 return 0; // :-(
4535 }
4536
4537 UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
4538 if( dtime > timeaftercalib ){
4539 if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
4540 return 0; // :-(
4541 }
4542
4543
4544
4545 if( (run->ACQ_BUILD_INFO & mask) != build ){
4546 if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;
4547 return 0; // :-(
4548 }
4549 return 1; // :-)
4550
4551 }
4552
4553 /**
4554 *
4555 * Check the DB (only for overlapping runs at the moment)
4556 *
4557 **/
4558 UInt_t PamelaDBOperations::Check(){
4559 //
4560 UInt_t test = 0;
4561 //
4562 UInt_t thisrht = 0;
4563 UInt_t thisrtt = 0;
4564 UInt_t thisid = 0;
4565 UInt_t prevrht = 0;
4566 UInt_t prevrtt = 0;
4567 UInt_t previd = 0;
4568 //
4569 UInt_t prevl0id = 0;
4570 UInt_t thisl0id = 0;
4571 //
4572 stringstream oss;
4573 TSQLResult *result = 0;
4574 TSQLRow *row = 0;
4575 TSQLResult *result2 = 0;
4576 TSQLRow *row2 = 0;
4577 oss.str("");
4578 oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN order by RUNHEADER_TIME asc;";
4579 // oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
4580 result = conn->Query(oss.str().c_str());
4581 //
4582 if ( !result ) throw -4;;
4583 //
4584 row = result->Next();
4585 //
4586 while ( row ){
4587 thisid = (UInt_t)atoll(row->GetField(0));
4588 thisl0id = (UInt_t)atoll(row->GetField(1));
4589 thisrht = (UInt_t)atoll(row->GetField(2));
4590 thisrtt = (UInt_t)atoll(row->GetField(3));
4591 //
4592 // if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
4593 // if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
4594 if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
4595 if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4596 printf(" CHECK n.1 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
4597 TString prevf = "";
4598 TString thisf = "";
4599 oss.str("");
4600 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4601 result2 = conn->Query(oss.str().c_str());
4602 if ( !result2 ) throw -4;;
4603 row2 = result2->Next();
4604 prevf = (TString)row2->GetField(0);
4605 oss.str("");
4606 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4607 result2 = conn->Query(oss.str().c_str());
4608 if ( !result2 ) throw -4;;
4609 row2 = result2->Next();
4610 thisf = (TString)row2->GetField(0);
4611 if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4612 test = 1;
4613 };
4614 //
4615 if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
4616 if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4617 printf(" CHECK n.2 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
4618 TString prevf = "";
4619 TString thisf = "";
4620 oss.str("");
4621 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4622 result2 = conn->Query(oss.str().c_str());
4623 if ( !result2 ) throw -4;;
4624 row2 = result2->Next();
4625 prevf = (TString)row2->GetField(0);
4626 oss.str("");
4627 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4628 result2 = conn->Query(oss.str().c_str());
4629 if ( !result2 ) throw -4;;
4630 row2 = result2->Next();
4631 thisf = (TString)row2->GetField(0);
4632 if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4633 test = 1;
4634 };
4635 //
4636 if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
4637 if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4638 printf(" CHECK n.3 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
4639 TString prevf = "";
4640 TString thisf = "";
4641 oss.str("");
4642 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4643 result2 = conn->Query(oss.str().c_str());
4644 if ( !result2 ) throw -4;;
4645 row2 = result2->Next();
4646 prevf = (TString)row2->GetField(0);
4647 oss.str("");
4648 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4649 result2 = conn->Query(oss.str().c_str());
4650 if ( !result2 ) throw -4;;
4651 row2 = result2->Next();
4652 thisf = (TString)row2->GetField(0);
4653 if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4654 test = 1;
4655 };
4656
4657 //
4658 prevrht = thisrht;
4659 prevrtt = thisrtt;
4660 previd = thisid;
4661 prevl0id = thisl0id;
4662 row = result->Next();
4663 };
4664 //
4665 return(test);
4666 //
4667 };

  ViewVC Help
Powered by ViewVC 1.1.23