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

Annotation of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.50 - (hide annotations) (download)
Wed Sep 5 15:34:49 2007 UTC (17 years, 3 months ago) by mocchiut
Branch: MAIN
CVS Tags: v4r01, v4r00
Changes since 1.49: +75 -3 lines
New feature: now the -gpamela option automagically create the best tsync and boot number according to DB present status

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

  ViewVC Help
Powered by ViewVC 1.1.23