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

Annotation of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.56 - (hide annotations) (download)
Mon Mar 10 10:58:01 2008 UTC (17 years ago) by mocchiut
Branch: MAIN
CVS Tags: v5r00, v5r01, HEAD
Changes since 1.55: +21 -1 lines
New checking features

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

  ViewVC Help
Powered by ViewVC 1.1.23