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

Contents of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.51 - (show annotations) (download)
Fri Oct 19 07:47:52 2007 UTC (17 years, 4 months ago) by mocchiut
Branch: MAIN
Changes since 1.50: +21 -5 lines
GL_TIMESYNC class changed in order to fix asyncronous abs time determination from YP and DV bug

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

  ViewVC Help
Powered by ViewVC 1.1.23