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

Annotation of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.53 - (hide annotations) (download)
Fri Nov 30 13:17:34 2007 UTC (17 years, 3 months ago) by mocchiut
Branch: MAIN
Changes since 1.52: +1 -1 lines
Bug in PamelaDBOperations::SetID_RAW() fixed

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

  ViewVC Help
Powered by ViewVC 1.1.23