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

Annotation of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.38 - (hide annotations) (download)
Mon Apr 30 10:22:45 2007 UTC (17 years, 7 months ago) by mocchiut
Branch: MAIN
Changes since 1.37: +3 -2 lines
Convert from ERROR to WARNING 'run with no runh runt nor events'

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

  ViewVC Help
Powered by ViewVC 1.1.23