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

Annotation of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (hide annotations) (download)
Fri Oct 20 11:11:12 2006 UTC (18 years, 1 month ago) by mocchiut
Branch: MAIN
Changes since 1.12: +229 -119 lines
GL_RESURS_OFFSET changed, added _RUNID_GEN table, unique run ID number implemented

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

  ViewVC Help
Powered by ViewVC 1.1.23