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

Annotation of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.39 - (hide annotations) (download)
Mon Apr 30 10:23:36 2007 UTC (17 years, 7 months ago) by mocchiut
Branch: MAIN
Changes since 1.38: +2 -3 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.39 if ( (evbefh-1) == 1 ) {
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.39 this->HandleRunFragments(true,true,0,(evbefh-1));
1603 mocchiut 1.36 //
1604     };
1605 mocchiut 1.1 };
1606     //
1607     //
1608     if ( (ptht - pth) > 1 ){
1609     //
1610     if ( IsDebug() ) printf(" Missing runtrailers! \n");
1611 mocchiut 1.35 if ( IsDebug() ) printf(" Attention there is a jump in the runheader counter %u %u %u \n",ptht,pth,ptt);
1612 mocchiut 1.1 // is not the consecutive header
1613     while ( pth != ptht ){
1614     //
1615     // treat the header(s) in the middle and then go to the next header, repeat until you reach the correct header.
1616     //
1617     pth++;
1618     //
1619     rh->GetEntry(pth+1);
1620     phh = ehh->GetPscuHeader();
1621     pktt = phh->GetCounter();
1622     obtt = phh->GetOrbitalTime();
1623     cod = ehh->GetCounter();
1624     evbeft = cod->Get(pctp->Physics);
1625     rh->GetEntry(pth);
1626     phh = ehh->GetPscuHeader();
1627     cod = ehh->GetCounter();
1628     pkth = phh->GetCounter();
1629     obth = phh->GetOrbitalTime();
1630     evbefh = cod->Get(pctp->Physics);
1631     //
1632 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);
1633     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
1634 mocchiut 1.1 //
1635     this->HandleMissingHoT(false,true,evbefh,evbeft-1);
1636     //
1637     };
1638     //
1639     } else if ( !(ptht - pth) ){
1640     //
1641     if ( IsDebug() ) printf(" Missing runheader! \n");
1642 mocchiut 1.35 if ( IsDebug() ) printf(" Attention! the runheader counter did not changed %u %u %u \n",ptht,pth,ptt);
1643 mocchiut 1.1 if ( IsDebug() ) printf(" The run should have already been handled by HandleRun() \n");
1644     //
1645     } else {
1646     //
1647     // go on with next header
1648     //
1649     pth = ptht;
1650     };
1651     //
1652     };
1653     //
1654     if ( ptt+1 == rtev){
1655     ptht++;
1656     if ( ptht < rhev ){
1657     rh->GetEntry(ptht);
1658     phh = ehh->GetPscuHeader();
1659     pkth = phh->GetCounter();
1660     obth = phh->GetOrbitalTime();
1661     cod = ehh->GetCounter();
1662     evbefh = cod->Get(pctp->Physics);
1663 mocchiut 1.35 if ( IsDebug() ) printf(" Piece of run at the end of file %u %u %u \n",pkth,obth,obtt);
1664     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u %u %u \n",ptht,pth,ptt);
1665     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u \n",rhev);
1666 mocchiut 1.1 //
1667     this->HandleRunFragments(false,true,evbefh,upperentry);
1668     } else {
1669     //
1670     // check if we have a fragment with no header
1671     //
1672     if ( (UInt_t)evbeft < upperentry-1 ){
1673     if ( IsDebug() ) printf(" Piece of run at the end of the file with NO RUNHEADER!\n");
1674     //
1675 mocchiut 1.2 if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1676 mocchiut 1.1 rt->GetEntry(ptt-1);
1677     cod = eht->GetCounter();
1678     evbefh = cod->Get(pctp->Physics);
1679     rt->GetEntry(ptt);
1680     pht = eht->GetPscuHeader();
1681     this->HandleRunFragments(true,true,evbefh,upperentry);
1682     };
1683     };
1684     };
1685     //
1686     };
1687     };
1688     //
1689     return(signal);
1690     };
1691    
1692     /**
1693     *
1694     * Check if the run has already been inserted
1695     *
1696     */
1697     Bool_t PamelaDBOperations::IsRunAlreadyInserted(){
1698     //
1699     TSQLResult *result = 0;
1700     TSQLRow *row = 0;
1701     //
1702     stringstream oss;
1703     oss.str("");
1704     //
1705     // the where clause is of the type: boot_number = _our_boot && (
1706     // ( runhead_time >= (_our_runhead_time-10) && runtrail_time <= (_our_runtrail_time+10) &&
1707     // ( runhead_obt >= _our_runheadobt || runhead_pkt >= _our_runheadpkt ) &&
1708     // ( runtrail_obt >= _our_runtrailobt || runtrail_pkt >= _our_runtrailpkt ) )
1709     // ||
1710     // ( runhead_time <= _our_runhead_time && runtrail_time >= _our_runtrail_time) &&
1711     // ( runhead_obt <= _our_runheadobt || runhead_pkt <= _our_runheadpkt ) &&
1712     // ( runtrail_obt <= _our_runtrailobt || runtrail_pkt <= _our_runtrailpkt ) )
1713     // )
1714     //
1715     oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
1716     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1717     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1718     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1719     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1720     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1721     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1722     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1723     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1724     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1725     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1726     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1727     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1728     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1729     //
1730     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1731     result = conn->Query(oss.str().c_str());
1732     //
1733     if ( !result ) throw -4;
1734     //
1735     row = result->Next();
1736     //
1737     if ( !row ){
1738     if ( IsDebug() ) printf(" The run is new \n");
1739     if ( IsDebug() ) printf(" -> fill the DB \n");
1740     return(false); // the file has not been inserted in the DB, go on.
1741     };
1742     //
1743     Bool_t signal = true;
1744     //
1745     while ( row != NULL ){
1746     if ( IsDebug() ) printf(" A run exists with runheader and runtrailer time and packets compatible with this one \n");
1747     //
1748     // the run has already been inserted
1749     //
1750     // return(true); //<<<<<<<<<<<<<<<<<<<<<<<< patch follows, uncomment here
1751     //
1752     // PATCH!
1753     // 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
1754     // while the old run doesn't have it 3) we have more events than the old run
1755     //
1756     if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1757     //
1758     if ( IsDebug() ) printf(" The new run has more events than the old one \n");
1759 mocchiut 1.15 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1760     // oss.str("");
1761     // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1762     // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1763     // conn->Query(oss.str().c_str());
1764 mocchiut 1.1 if ( signal ) signal = false;
1765     goto gonext;
1766     //
1767     } else if ( glrun->GetNEVENTS() < (UInt_t)atoll(row->GetField(1)) ){
1768     if ( IsDebug() ) printf(" The new run has less events than the old one \n");
1769     if ( IsDebug() ) printf(" The run is already inserted \n");
1770     goto gonext;
1771     };
1772     //
1773     if ( glrun->GetTRK_CALIB() && !(UInt_t)atoll(row->GetField(2)) ){
1774     //
1775     if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");
1776     //
1777 mocchiut 1.15 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1778     // oss.str("");
1779     // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1780     // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1781     // conn->Query(oss.str().c_str());
1782 mocchiut 1.1 //
1783     if ( signal ) signal = false;
1784     goto gonext;
1785     } else if ( !glrun->GetTRK_CALIB() && (UInt_t)atoll(row->GetField(2)) ){
1786     if ( IsDebug() ) printf(" The new run has the same number of events but miss the runheader the old has the runheader \n");
1787     if ( IsDebug() ) printf(" The run is already inserted \n");
1788     goto gonext;
1789     };
1790     //
1791     if ( glrun->GetPKT_COUNTER() && !(UInt_t)atoll(row->GetField(3)) ){
1792     //
1793     if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");
1794     //
1795 mocchiut 1.15 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1796     // oss.str("");
1797     // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1798     // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1799     // conn->Query(oss.str().c_str());
1800 mocchiut 1.1 if ( signal ) signal = false;
1801     //
1802     };
1803     //
1804     gonext:
1805     // END PATCH!
1806     //
1807     row = result->Next();
1808     //
1809     };
1810     //
1811     delete result;
1812     //
1813     if ( signal && IsDebug() ) printf(" The run has already been inserted \n");
1814 mocchiut 1.23 if ( !signal && IsDebug() ) printf(" The run existed and was deleted, fill the DB \n");
1815 mocchiut 1.1 return(signal);
1816     };
1817    
1818     /**
1819     * Handle runs which seems to be good ones.
1820     **/
1821     void PamelaDBOperations::HandleRun(){
1822     ULong64_t chkpkt = 0;
1823     ULong64_t pktt = (ULong64_t)PKT(pht->GetCounter());
1824     ULong64_t pkth = (ULong64_t)PKT(phh->GetCounter());
1825     //
1826     chkpkt = pkth + (ULong64_t)runt->PKT_COUNTER + 1ULL + 1ULL;
1827     //
1828     if ( labs(chkpkt-pktt)<2 ){
1829     //
1830     if ( IsDebug() ) printf(" check %llu pktt %llu \n",chkpkt,pktt);
1831     //
1832     // it must be a good run, fill the db
1833     //
1834     this->FillClass();
1835     //
1836 mocchiut 1.13 if ( !IsRunAlreadyInserted() ){
1837     glrun->SetID(this->AssignRunID());
1838     glrun->SetID_RUN_FRAG(0);
1839     glrun->Fill_GL_RUN(conn);
1840     };
1841 mocchiut 1.1 } else {
1842     //
1843     if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt);
1844     if ( IsDebug() ) printf(" try to recover run(s) without runheader and runtrailer between runheader and runtrailer\n");
1845     //
1846     this->HandleSuspiciousRun();
1847     //
1848     };
1849     //
1850     //
1851     return;
1852     };
1853    
1854    
1855     /**
1856     * Handle run fragments at the beginning or at the end of the file
1857     **/
1858     void PamelaDBOperations::HandleRunFragments(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
1859     //
1860     UInt_t rhfirstev = firstev;
1861     UInt_t rtlastev = lastev;
1862     Bool_t found = false;
1863 mocchiut 1.16 Bool_t foundinrun = false;
1864 mocchiut 1.1 //
1865     TSQLResult *result = 0;
1866     TSQLRow *row = 0;
1867     //
1868     stringstream oss;
1869     oss.str("");
1870     //
1871     // is the piece of run good (no other packets inside)?
1872     //
1873     if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
1874     //
1875     // if not, handle other pieces and continue with the first one
1876     //
1877     if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
1878     //
1879     };
1880     //
1881     // we have now the good first piece of a run, fill the glrun object
1882     //
1883     if ( rhfirstev != firstev && !mishead ) mishead = true;
1884     if ( rtlastev != lastev && !mistrail ) mistrail = true;
1885     //
1886     this->FillClass(mishead,mistrail,firstev,lastev);
1887     //
1888     if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
1889     //
1890 mocchiut 1.16 // First of all insert the run in the fragment table...
1891     //
1892     oss.str("");
1893     oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1894     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1895     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1896     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1897     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1898     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1899     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1900     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1901     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1902     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1903     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1904     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1905     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1906     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1907     //
1908     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1909     result = conn->Query(oss.str().c_str());
1910     //
1911     if ( !result ) throw -4;
1912     //
1913     row = result->Next();
1914     //
1915     if ( !row ){
1916     //
1917     // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
1918     //
1919     if ( IsDebug() ) printf(" The run is new \n");
1920     if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
1921     //
1922     glrun->SetID(this->AssignRunID());
1923     glrun->SetID_RUN_FRAG(0);
1924     glrun->Fill_GL_RUN_FRAGMENTS(conn);
1925     //
1926     } else {
1927     if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
1928 mocchiut 1.23 return;
1929 mocchiut 1.16 };
1930     //
1931     //
1932 mocchiut 1.1 // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
1933     //
1934     if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
1935     // missing it no way we can found a piece in the frag table
1936     //
1937     oss.str("");
1938     oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1939     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1940 mocchiut 1.16 << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1941     << " ID != " << glrun->ID
1942 mocchiut 1.1 << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1943     //
1944     if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
1945     result = conn->Query(oss.str().c_str());
1946     //
1947     if ( !result ) throw -4;
1948     //
1949     row = result->Next();
1950     //
1951 mocchiut 1.16 if ( !row && NoFrag() ){
1952     //
1953     oss.str("");
1954     oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
1955     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1956     << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1957     << " ID != " << glrun->ID
1958     << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1959     //
1960     if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1961     result = conn->Query(oss.str().c_str());
1962     //
1963     if ( !result ) throw -4;
1964     //
1965     foundinrun = true;
1966     //
1967     row = result->Next();
1968     //
1969     };
1970     //
1971 mocchiut 1.1 if ( !row ){
1972     if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1973     found = false;
1974     } else {
1975     //
1976     found = false; // default value
1977     //
1978     if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
1979     //
1980     // if we have both runheader and runtrailer we can check with pkt_counter:
1981     //
1982     if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
1983     ULong64_t chkpkt = 0;
1984     ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
1985     ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
1986     //
1987     chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;
1988     //
1989     if ( labs(chkpkt-pktt)<2 ){
1990     //
1991     if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
1992     //
1993     found = true;
1994     //
1995     } else {
1996     //
1997     if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
1998     //
1999     found = false;
2000     //
2001     };
2002     };
2003     if ( !found ){
2004     //
2005     // 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
2006     //
2007     ULong64_t chkpkt1 = 0;
2008     ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2009     ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2010     chkpkt1 = labs(orunh1-dbrunt1);
2011     //
2012     ULong64_t chkpkt2 = 0;
2013     ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
2014     ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2015     chkpkt2 = labs(orunh2-dbrunt2);
2016     //
2017     ULong64_t chkpkt3 = 0;
2018     ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
2019     ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2020     chkpkt3 = labs(orunh3-dbrunt3);
2021     //
2022     if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2023     // if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
2024     //
2025     if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2026     //
2027     found = true;
2028     //
2029     } else {
2030     //
2031     if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2032     //
2033     found = false;
2034     //
2035     };
2036     };
2037     };
2038     //
2039     if ( found ){
2040     //
2041     // 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
2042     //
2043     if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2044     //
2045 mocchiut 1.16 if ( foundinrun ){
2046     glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2047     glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2048     };
2049     //
2050 mocchiut 1.1 GL_RUN *glrun1 = new GL_RUN();
2051     //
2052 mocchiut 1.16 // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2053 mocchiut 1.1 //
2054     oss.str("");
2055     oss << " ID="<<row->GetField(0)<<";";
2056     //
2057     glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
2058     //
2059     // merge infos
2060     //
2061     UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
2062     ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
2063     UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
2064     ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
2065 mocchiut 1.35 if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2066 mocchiut 1.1 TTree *T= 0;
2067     T = (TTree*)file->Get("Physics");
2068     if ( !T || T->IsZombie() ) throw -16;
2069     EventHeader *eh = 0;
2070     PscuHeader *ph = 0;
2071     T->SetBranchAddress("Header", &eh);
2072     while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
2073     T->GetEntry(firstev);
2074     ph = eh->GetPscuHeader();
2075     bpkt = PKT(ph->GetCounter());
2076     bobt = OBT(ph->GetOrbitalTime());
2077     firstev++;
2078     };
2079 mocchiut 1.35 if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2080 mocchiut 1.1 //
2081     glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2082     glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
2083     glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
2084     glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
2085     glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
2086     //
2087     glrun->SetEV_FROM(firstev);
2088     glrun->SetNEVENTS(lastev-firstev+1);
2089 mocchiut 1.16 //
2090 mocchiut 1.1 glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
2091     glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
2092     glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
2093     glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
2094     glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
2095     glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
2096     glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
2097     glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
2098     glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
2099     glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
2100     glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());
2101     glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());
2102     glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());
2103     glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());
2104     glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());
2105     glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());
2106     //
2107     if ( !IsRunAlreadyInserted() ){
2108     //
2109 mocchiut 1.16 // glrun->SetID(this->AssignRunID());
2110 mocchiut 1.13 glrun->SetID_RUN_FRAG(glrun1->GetID());
2111 mocchiut 1.1 glrun->Fill_GL_RUN(conn);
2112     //
2113 mocchiut 1.16 // set id number
2114 mocchiut 1.1 //
2115 mocchiut 1.13 glrun1->SetID_RUN_FRAG(glrun->GetID());
2116 mocchiut 1.1 glrun1->Fill_GL_RUN(conn);
2117     //
2118     };
2119 mocchiut 1.16 // delete old entry in fragment table
2120     //
2121     glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2122     glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2123 mocchiut 1.1 //
2124     delete glrun1;
2125     //
2126     //
2127     return;
2128     //
2129     };
2130     //
2131     };
2132     //
2133     if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
2134 mocchiut 1.16 // missing it no way we can found a piece in the frag table
2135 mocchiut 1.1 //
2136     oss.str("");
2137     oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "
2138     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2139 mocchiut 1.16 << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2140     << " ID != " << glrun->ID
2141 mocchiut 1.1 << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2142     //
2143     if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
2144     result = conn->Query(oss.str().c_str());
2145     //
2146     if ( !result ) throw -4;
2147     //
2148     row = result->Next();
2149     //
2150 mocchiut 1.16 if ( !row && NoFrag() ){
2151     //
2152     oss.str("");
2153     oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
2154     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2155     << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2156     << " ID != " << glrun->ID
2157     << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2158     //
2159     if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2160     result = conn->Query(oss.str().c_str());
2161     //
2162     if ( !result ) throw -4;
2163     //
2164     foundinrun = true;
2165     row = result->Next();
2166     //
2167     };
2168     //
2169 mocchiut 1.1 if ( !row ){
2170     if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2171     found = false;
2172     } else {
2173     //
2174     found = false; // default value
2175     //
2176     if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
2177     //
2178     // if we have both runheader and runtrailer we can check with pkt_counter:
2179     //
2180     if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){
2181     ULong64_t chkpkt = 0;
2182     ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
2183     ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2184     //
2185     chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;
2186     //
2187     if ( labs(chkpkt-pktt)<2 ){
2188     //
2189     if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2190     //
2191     found = true;
2192     //
2193     } else {
2194     //
2195     if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
2196     //
2197     found = false;
2198     //
2199     };
2200     };
2201     if ( !found ){
2202     //
2203     // 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
2204     //
2205     ULong64_t chkpkt1 = 0;
2206     ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
2207     ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2208     chkpkt1 = labs(orunh1-dbrunt1);
2209     //
2210     ULong64_t chkpkt2 = 0;
2211     ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT());
2212     ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2213     chkpkt2 = labs(orunh2-dbrunt2);
2214     //
2215     ULong64_t chkpkt3 = 0;
2216     ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME());
2217     ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2218     chkpkt3 = labs(orunh3-dbrunt3);
2219     //
2220     if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2221     //
2222     if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2223     //
2224     found = true;
2225     //
2226     } else {
2227     //
2228     if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2229     //
2230     found = false;
2231     //
2232     };
2233     };
2234     };
2235     //
2236     if ( found ){
2237     //
2238     // 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
2239     //
2240     if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2241     //
2242 mocchiut 1.16 if ( foundinrun ){
2243     glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2244     glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2245     };
2246     //
2247 mocchiut 1.1 GL_RUN *glrun1 = new GL_RUN();
2248     //
2249 mocchiut 1.16 // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2250 mocchiut 1.1 //
2251     oss.str("");
2252     oss << " ID="<<row->GetField(0)<<";";
2253     //
2254     glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runtrailer infos
2255     //
2256     // merge infos
2257     //
2258     UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT());
2259     ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
2260     UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
2261     ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
2262 mocchiut 1.35 if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2263 mocchiut 1.1 TTree *T= 0;
2264     T = (TTree*)file->Get("Physics");
2265     if ( !T || T->IsZombie() ) throw -16;
2266     EventHeader *eh = 0;
2267     PscuHeader *ph = 0;
2268     T->SetBranchAddress("Header", &eh);
2269     while ( apkt > bpkt && aobt > bobt && lastev > 0 ){
2270     T->GetEntry(lastev);
2271     ph = eh->GetPscuHeader();
2272     apkt = PKT(ph->GetCounter());
2273     aobt = OBT(ph->GetOrbitalTime());
2274     lastev--;
2275     };
2276 mocchiut 1.35 if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2277 mocchiut 1.1 //
2278     glrun->SetEV_TO(lastev);
2279     glrun->SetNEVENTS(lastev-firstev+1);
2280     glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());
2281     glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER());
2282     glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME());
2283     glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
2284     glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
2285     //
2286     glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
2287     glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
2288     glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
2289     glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP());
2290     glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE());
2291     glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE());
2292     glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A());
2293     glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B());
2294     glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO());
2295     glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO());
2296     glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB());
2297     glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE());
2298     glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED());
2299     glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK());
2300     glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC());
2301     glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC());
2302     //
2303     if ( !IsRunAlreadyInserted() ){
2304     //
2305 mocchiut 1.16 // glrun->SetID(this->AssignRunID());
2306 mocchiut 1.13 //
2307     glrun->SetID_RUN_FRAG(glrun1->GetID());
2308 mocchiut 1.1 glrun->Fill_GL_RUN(conn);
2309     //
2310 mocchiut 1.16 // set id number
2311 mocchiut 1.1 //
2312 mocchiut 1.13 glrun1->SetID_RUN_FRAG(glrun->GetID());
2313 mocchiut 1.1 glrun1->Fill_GL_RUN(conn);
2314     //
2315     };
2316     //
2317 mocchiut 1.16 // delete old entries in fragment table
2318 mocchiut 1.1 //
2319 mocchiut 1.16 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2320     glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2321 mocchiut 1.1 //
2322 mocchiut 1.16 delete glrun1;
2323 mocchiut 1.1 //
2324     return;
2325     //
2326     };
2327     //
2328     };
2329     //
2330     if ( !found ){
2331     //
2332     if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
2333     //
2334     // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
2335     //
2336     oss.str("");
2337     oss << " SELECT ID FROM GL_RUN WHERE "
2338     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2339     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2340     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2341     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2342     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2343     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2344     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2345     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2346     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2347     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2348     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2349     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2350     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2351     //
2352     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
2353     result = conn->Query(oss.str().c_str());
2354     //
2355     if ( !result ) throw -4;
2356     //
2357     row = result->Next();
2358     //
2359 mocchiut 1.16 if ( row ){
2360     if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
2361 mocchiut 1.1 } else {
2362 mocchiut 1.16 if ( NoFrag() ){
2363     glrun->SetID_RUN_FRAG(glrun->GetID());
2364     glrun->Fill_GL_RUN(conn);
2365     glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2366     };
2367 mocchiut 1.1 };
2368     };
2369     //
2370     return;
2371     };
2372    
2373    
2374     /**
2375     * Handle run without header or trailer
2376     **/
2377     void PamelaDBOperations::HandleMissingHoT(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
2378     //
2379     //
2380     // is the piece of run good (no other packets inside)?
2381     //
2382     if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
2383     //
2384     // if not, handle other pieces and continue with the first one
2385     //
2386     if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
2387     //
2388     } else {
2389     //
2390     this->FillClass(mishead,mistrail,firstev,lastev);
2391     //
2392 mocchiut 1.13 if ( !IsRunAlreadyInserted() ){
2393     glrun->SetID(this->AssignRunID());
2394     glrun->SetID_RUN_FRAG(0);
2395     glrun->Fill_GL_RUN(conn);
2396     };
2397 mocchiut 1.1 //
2398     };
2399     //
2400     return;
2401     };
2402    
2403     /**
2404     *
2405     * check if we have non-physics packets inside the run
2406     *
2407     */
2408     Bool_t PamelaDBOperations::IsRunConsistent(Bool_t mishead, Bool_t mistrail, UInt_t &firstev, UInt_t &lastev){
2409     //
2410     EventCounter *code=0;
2411     //
2412     UInt_t nevent = 0;
2413     UInt_t checkfirst = 0;
2414     UInt_t checklast = 0;
2415     UInt_t firstentry = 0;
2416     UInt_t lastentry = 0;
2417     UInt_t firstTime = 0;
2418     UInt_t lastTime = 0;
2419     UInt_t firstPkt = 0;
2420     UInt_t lastPkt = 0;
2421     UInt_t firstObt = 0;
2422     UInt_t lastObt = 0;
2423     //
2424     pcksList packetsNames;
2425     pcksList::iterator Iter;
2426     getPacketsNames(packetsNames);
2427     //
2428     TTree *T= 0;
2429     T =(TTree*)file->Get("Physics");
2430     if ( !T || T->IsZombie() ) throw -16;
2431     EventHeader *eh = 0;
2432     PscuHeader *ph = 0;
2433     T->SetBranchAddress("Header", &eh);
2434     nevent = T->GetEntries();
2435     //
2436     //
2437     if ( firstev == lastev+1 ) { // no events inside the run!
2438     if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2439     // return true is correct
2440     return(true);
2441     //
2442     } else {
2443     //
2444     T->GetEntry(firstev);
2445     code = eh->GetCounter();
2446     checkfirst = 0;
2447     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2448     if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
2449     };
2450     if ( IsDebug() ) printf(" Check first is %i firstev is %i\n",checkfirst,firstev);
2451     //
2452     T->GetEntry(lastev);
2453     code = eh->GetCounter();
2454     checklast = 0;
2455     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2456     if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
2457     };
2458     if ( IsDebug() ) printf(" Check last is %i lastev is %i\n",checklast,lastev);
2459     //
2460     if ( checkfirst == checklast ){
2461     //
2462     if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2463     //
2464     return(true);
2465     //
2466     } else {
2467     //
2468     if ( IsDebug() ) printf(" There are no-phyics packets inside the run!\n");
2469     //
2470     // HERE WE MUST HANDLE THAT RUNS AND GO BACK
2471     //
2472     if ( IsDebug() ) printf(" Never seen this case, try to handle it anyway, it was throw -95\n");
2473     //
2474     Bool_t emptyruns = false;
2475     UInt_t check = 0;
2476     UInt_t lastevtemp = lastev;
2477     UInt_t firstevno = firstev;
2478     //
2479     for (UInt_t i=firstev; i<=lastev; i++){
2480     //
2481     T->GetEntry(i);
2482     code = eh->GetCounter();
2483     //
2484     check = 0;
2485     //
2486     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2487     if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
2488     };
2489     //
2490     if ( checkfirst < check || i == lastev ){
2491     //
2492     firstentry = firstevno;
2493     //
2494     if ( checkfirst < check ){
2495     lastentry = i-1;
2496     } else {
2497     lastentry = i;
2498     };
2499     //
2500     if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2501     //
2502     glrun->SetEV_FROM(firstentry);
2503     glrun->SetEV_TO(lastentry);
2504     if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
2505     if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
2506     lastentry--;
2507     };
2508     glrun->SetNEVENTS(lastentry-firstentry+1);
2509     //
2510     glrun->Set_GL_RUNH0();
2511     glrun->Set_GL_RUNT0();
2512     //
2513     glrun->SetLAST_TIMESYNC(0);
2514     glrun->SetOBT_TIMESYNC(0);
2515     //
2516     T->GetEntry(firstentry);
2517     ph = eh->GetPscuHeader();
2518     firstObt = ph->GetOrbitalTime();
2519     firstTime = this->GetAbsTime(firstObt);
2520     firstPkt = ph->GetCounter();
2521     //
2522     T->GetEntry(lastentry);
2523     ph = eh->GetPscuHeader();
2524     lastObt = ph->GetOrbitalTime();
2525     lastTime = this->GetAbsTime(lastObt);
2526     lastPkt = ph->GetCounter();
2527     //
2528     glrun->SetRUNHEADER_PKT(firstPkt);
2529     glrun->SetRUNTRAILER_PKT(lastPkt);
2530     //
2531     glrun->SetRUNHEADER_OBT(firstObt);
2532     glrun->SetRUNTRAILER_OBT(lastObt);
2533     //
2534     if ( firstev == firstentry && !emptyruns && !mishead ){
2535     glrun->Set_GL_RUNH(runh,phh);
2536     firstTime = this->GetAbsTime(phh->GetOrbitalTime());
2537     if ( IsDebug() ) printf(" We have the runheader \n");
2538     };
2539     if ( lastev == i && !mistrail ){
2540     glrun->Set_GL_RUNT(runt,pht);
2541     lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2542     if ( IsDebug() ) printf(" We have the runtrailer \n");
2543     };
2544     //
2545     if ( lastentry == (firstentry-2) ){ // no events in the run
2546     emptyruns = true;
2547     if ( IsDebug() ) printf(" No events in the run \n");
2548     lastTime = firstTime;
2549     if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
2550     lastObt = glrun->RUNHEADER_OBT;
2551     lastPkt = glrun->RUNHEADER_PKT;
2552     } else {
2553     lastObt = firstObt;
2554     lastPkt = firstPkt;
2555     };
2556     glrun->SetRUNTRAILER_PKT(lastPkt);
2557     glrun->SetRUNTRAILER_OBT(lastObt);
2558     lastentry++;
2559     };
2560     //
2561     this->SetCommonGLRUN(firstTime,lastTime);
2562     //
2563 mocchiut 1.13 if ( !IsRunAlreadyInserted() ){
2564     glrun->SetID(this->AssignRunID());
2565     glrun->SetID_RUN_FRAG(0);
2566     glrun->Fill_GL_RUN(conn);
2567     };
2568 mocchiut 1.1 //
2569     firstevno = lastentry + 1;
2570     //
2571     checkfirst = check;
2572     //
2573     };
2574     //
2575     if ( check == checklast && i != lastev ){
2576     lastevtemp = i - 1;
2577     i = lastev - 1;
2578     };
2579     //
2580     };
2581     //
2582     lastev = lastevtemp;
2583     //
2584     return(false);
2585     //
2586     };
2587     };
2588     //
2589     return(false); // should never arrive here
2590     };
2591    
2592     /**
2593     *
2594     * 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
2595     * 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
2596     * looking for non-physics packets inside.
2597     *
2598     */
2599     void PamelaDBOperations::HandleSuspiciousRun(){
2600     //
2601     PacketType *pctp=0;
2602     EventCounter *codt=0;
2603     EventCounter *codh=0;
2604     EventCounter *code=0;
2605     UInt_t firstev = 0;
2606     UInt_t lastev = 0;
2607     UInt_t nevent = 0;
2608     UInt_t checkfirst = 0;
2609     UInt_t checklast = 0;
2610     UInt_t firstentry = 0;
2611     UInt_t lastentry = 0;
2612     UInt_t firstTime = 0;
2613     UInt_t lastTime = 0;
2614     UInt_t firstPkt = 0;
2615     UInt_t lastPkt = 0;
2616     UInt_t firstObt = 0;
2617     UInt_t lastObt = 0;
2618     //
2619     pcksList packetsNames;
2620     pcksList::iterator Iter;
2621     getPacketsNames(packetsNames);
2622     //
2623     TTree *rh=0;
2624     rh = (TTree*)file->Get("RunHeader");
2625     if ( !rh || rh->IsZombie() ) throw -17;
2626     TTree *T=0;
2627     T =(TTree*)file->Get("Physics");
2628     if ( !T || T->IsZombie() ) throw -16;
2629     EventHeader *eh = 0;
2630     PscuHeader *ph = 0;
2631     T->SetBranchAddress("Header", &eh);
2632     nevent = T->GetEntries();
2633     //
2634     codt = eht->GetCounter();
2635     codh = ehh->GetCounter();
2636     firstev = codh->Get(pctp->Physics);
2637     lastev = codt->Get(pctp->Physics)-1;
2638     //
2639     if ( firstev == lastev+1 ) { // no events inside the run!
2640     if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2641     //
2642     this->FillClass();
2643 mocchiut 1.13 if ( !IsRunAlreadyInserted() ){
2644     glrun->SetID(this->AssignRunID());
2645     glrun->SetID_RUN_FRAG(0);
2646     glrun->Fill_GL_RUN(conn);
2647     };
2648 mocchiut 1.1 //
2649     } else {
2650     //
2651     UInt_t nrunh = 0;
2652     UInt_t nrunh1 = 0;
2653     T->GetEntry(firstev);
2654     code = eh->GetCounter();
2655     checkfirst = 0;
2656     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2657     if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
2658     if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;
2659     };
2660     if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);
2661     //
2662     T->GetEntry(lastev);
2663     code = eh->GetCounter();
2664     checklast = 0;
2665     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2666     if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
2667     };
2668     if ( IsDebug() ) printf(" Check last is %i \n",checklast);
2669     //
2670     if ( checkfirst == checklast ){
2671     //
2672     if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2673     //
2674     this->FillClass();
2675 mocchiut 1.13 if ( !IsRunAlreadyInserted() ){
2676     glrun->SetID(this->AssignRunID());
2677     glrun->SetID_RUN_FRAG(0);
2678     glrun->Fill_GL_RUN(conn);
2679     };
2680 mocchiut 1.1 //
2681     } else {
2682     //
2683     if ( IsDebug() ) printf(" There are no-physics packets inside the run, try to separate runs \n");
2684     //
2685     Bool_t emptyruns = false;
2686     UInt_t check = 0;
2687     UInt_t firstevno = firstev;
2688     //
2689     for (UInt_t i=firstev; i<=lastev; i++){
2690     //
2691     T->GetEntry(i);
2692     code = eh->GetCounter();
2693     //
2694     check = 0;
2695     //
2696     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2697     if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
2698     if ( !strcmp(*Iter,"RunHeader") ) nrunh++;
2699     };
2700     //
2701     if ( checkfirst < check || i == lastev ){
2702     //
2703     firstentry = firstevno;
2704     //
2705     if ( checkfirst < check ){
2706     lastentry = i-1;
2707     } else {
2708     lastentry = i;
2709     };
2710     //
2711     if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2712     //
2713     glrun->SetEV_FROM(firstentry);
2714     glrun->SetEV_TO(lastentry);
2715     if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
2716     if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
2717     lastentry--;
2718     };
2719     glrun->SetNEVENTS(lastentry-firstentry+1);
2720     //
2721     glrun->Set_GL_RUNH0();
2722     glrun->Set_GL_RUNT0();
2723     //
2724     glrun->SetLAST_TIMESYNC(0);
2725     glrun->SetOBT_TIMESYNC(0);
2726     //
2727     T->GetEntry(firstentry);
2728     ph = eh->GetPscuHeader();
2729     firstObt = ph->GetOrbitalTime();
2730     firstTime = this->GetAbsTime(firstObt);
2731     firstPkt = ph->GetCounter();
2732     //
2733     T->GetEntry(lastentry);
2734     ph = eh->GetPscuHeader();
2735     lastObt = ph->GetOrbitalTime();
2736     lastTime = this->GetAbsTime(lastObt);
2737     lastPkt = ph->GetCounter();
2738     //
2739     glrun->SetRUNHEADER_PKT(firstPkt);
2740     glrun->SetRUNTRAILER_PKT(lastPkt);
2741     //
2742     glrun->SetRUNHEADER_OBT(firstObt);
2743     glrun->SetRUNTRAILER_OBT(lastObt);
2744     //
2745     if ( (firstev == firstentry && !emptyruns) || nrunh == (nrunh1 + 1) ){
2746     rh->GetEntry(nrunh1-1);
2747     phh = ehh->GetPscuHeader();
2748     nrunh1++;
2749     glrun->Set_GL_RUNH(runh,phh);
2750     firstTime = this->GetAbsTime(phh->GetOrbitalTime());
2751     if ( IsDebug() ) printf(" We have the runheader \n");
2752     };
2753     if ( lastev == i && checkfirst == check ){
2754     glrun->Set_GL_RUNT(runt,pht);
2755     lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2756     if ( IsDebug() ) printf(" We have the runtrailer \n");
2757     };
2758     //
2759     if ( lastentry == (firstentry-2) ){ // no events in the run
2760     emptyruns = true;
2761     if ( IsDebug() ) printf(" No events in the run \n");
2762     lastTime = firstTime;
2763     if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
2764     lastObt = glrun->RUNHEADER_OBT;
2765     lastPkt = glrun->RUNHEADER_PKT;
2766     } else {
2767     lastObt = firstObt;
2768     lastPkt = firstPkt;
2769     };
2770     glrun->SetRUNTRAILER_PKT(lastPkt);
2771     glrun->SetRUNTRAILER_OBT(lastObt);
2772     lastentry++;
2773     };
2774     //
2775     this->SetCommonGLRUN(firstTime,lastTime);
2776     //
2777 mocchiut 1.13 if ( !IsRunAlreadyInserted() ){
2778     glrun->SetID(this->AssignRunID());
2779     glrun->SetID_RUN_FRAG(0);
2780     glrun->Fill_GL_RUN(conn);
2781     };
2782 mocchiut 1.1 //
2783     if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check...
2784     //
2785     firstentry = i;
2786     //
2787     lastentry = i;
2788     //
2789     if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2790     //
2791     glrun->SetEV_FROM(firstentry);
2792     glrun->SetEV_TO(lastentry);
2793     glrun->SetNEVENTS(lastentry-firstentry+1);
2794     //
2795     glrun->Set_GL_RUNH0();
2796     //
2797     glrun->SetLAST_TIMESYNC(0);
2798     glrun->SetOBT_TIMESYNC(0);
2799     //
2800     T->GetEntry(firstentry);
2801     ph = eh->GetPscuHeader();
2802     firstObt = ph->GetOrbitalTime();
2803     firstTime = this->GetAbsTime(firstObt);
2804     firstPkt = ph->GetCounter();
2805     //
2806     glrun->SetRUNHEADER_PKT(firstPkt);
2807     //
2808     glrun->SetRUNHEADER_OBT(firstObt);
2809     //
2810     glrun->Set_GL_RUNT(runt,pht);
2811     lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2812     if ( IsDebug() ) printf(" We have the runtrailer \n");
2813     //
2814     this->SetCommonGLRUN(firstTime,lastTime);
2815     //
2816 mocchiut 1.13 if ( !IsRunAlreadyInserted() ){
2817     glrun->SetID(this->AssignRunID());
2818     glrun->SetID_RUN_FRAG(0);
2819     glrun->Fill_GL_RUN(conn);
2820     };
2821 mocchiut 1.1 };
2822     //
2823     firstevno = lastentry + 1;
2824     //
2825     checkfirst = check;
2826     //
2827     };
2828     //
2829     if ( check == checklast && i != lastev ) i = lastev - 1; // >>>>>>>>>>>>>>>>>>>>>>
2830     //
2831     };
2832     };
2833     };
2834     //
2835     return;
2836     };
2837    
2838    
2839     /**
2840     * Scan calorimeter calibrations packets, fill the GL_CALO_CALIB table
2841     */
2842     Int_t PamelaDBOperations::insertCALO_CALIB(){
2843     //
2844     TSQLResult *result = 0;
2845     TSQLRow *row = 0;
2846     //
2847     stringstream oss;
2848     oss.str("");
2849     //
2850     CalibCalPedEvent *calibCalPed = 0;
2851     TTree *tr = 0;
2852     EventHeader *eh = 0;
2853     PscuHeader *ph = 0;
2854     //
2855     UInt_t nevents = 0;
2856     UInt_t fromtime = 0;
2857     UInt_t totime = 0;
2858     UInt_t obt = 0;
2859     UInt_t pkt = 0;
2860     //
2861     tr = (TTree*)file->Get("CalibCalPed");
2862     if ( !tr || tr->IsZombie() ) throw -21;
2863     //
2864     tr->SetBranchAddress("CalibCalPed", &calibCalPed);
2865     tr->SetBranchAddress("Header", &eh);
2866     nevents = tr->GetEntries();
2867     //
2868 mocchiut 1.2 if ( !nevents ) return(1);
2869 mocchiut 1.1 //
2870     for (UInt_t i=0; i < nevents; i++){
2871     tr->GetEntry(i);
2872     for (UInt_t section = 0; section < 4; section++){
2873     //
2874     if ( calibCalPed->cstwerr[section] ){
2875     valid = 1;
2876     if ( calibCalPed->cperror[section] ) valid = 0;
2877     ph = eh->GetPscuHeader();
2878     obt = ph->GetOrbitalTime();
2879     pkt = ph->GetCounter();
2880     fromtime = this->GetAbsTime(ph->GetOrbitalTime());
2881     if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
2882     //
2883 mocchiut 1.34 if ( IsDebug() ) printf(" Calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
2884 mocchiut 1.1 //
2885     // check if the calibration has already been inserted
2886     //
2887     oss.str("");
2888     oss << " SELECT ID FROM GL_CALO_CALIB WHERE "
2889     << " SECTION = "<< section << " AND "
2890     << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
2891     << " OBT = "<< obt << " AND "
2892     << " PKT = "<< pkt << ";";
2893     //
2894     if ( IsDebug() ) printf(" Check if the calo calibration has already been inserted: query is \n %s \n",oss.str().c_str());
2895     result = conn->Query(oss.str().c_str());
2896     //
2897     if ( !result ) throw -4;
2898     //
2899     row = result->Next();
2900     //
2901     if ( row ){
2902     //
2903     if ( IsDebug() ) printf(" Calo calibration already inserted in the DB\n");
2904     //
2905     } else {
2906     //
2907     // we have to insert a new calibration, check where to place it
2908     //
2909     oss.str("");
2910     oss << " SELECT ID,TO_TIME FROM GL_CALO_CALIB WHERE "
2911     << " SECTION = "<< section << " AND "
2912     << " FROM_TIME < "<< fromtime << " AND "
2913     << " TO_TIME > "<< fromtime << ";";
2914     //
2915     if ( IsDebug() ) printf(" Check where to place the calo calibration: query is \n %s \n",oss.str().c_str());
2916     result = conn->Query(oss.str().c_str());
2917     //
2918     if ( !result ) throw -4;
2919     //
2920     row = result->Next();
2921     //
2922     if ( !row ){
2923     //
2924     // no calibrations in the db contain our calibration
2925     //
2926     if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
2927 mocchiut 1.19 if ( fromtime < 1150871000 ){ //1150866904
2928 mocchiut 1.35 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
2929 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
2930     };
2931     //
2932     oss.str("");
2933     oss << " SELECT FROM_TIME FROM GL_CALO_CALIB WHERE "
2934     << " SECTION = "<< section << " AND "
2935     << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
2936     //
2937     if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
2938     result = conn->Query(oss.str().c_str());
2939     //
2940     if ( !result ) throw -4;
2941     //
2942     row = result->Next();
2943     if ( !row ){
2944     totime = numeric_limits<UInt_t>::max();
2945     } else {
2946     totime = (UInt_t)atoll(row->GetField(0));
2947     };
2948     //
2949     } else {
2950     //
2951     // determine upper and lower limits and make space for the new calibration
2952     //
2953     totime = (UInt_t)atoll(row->GetField(1));
2954     //
2955     oss.str("");
2956     oss << " UPDATE GL_CALO_CALIB SET "
2957     << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
2958     << " ID = "<< row->GetField(0) << ";";
2959     //
2960     if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
2961     result = conn->Query(oss.str().c_str());
2962     //
2963     if ( !result ) throw -4;
2964     //
2965     };
2966     //
2967     oss.str("");
2968     oss << " INSERT INTO GL_CALO_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,OBT,PKT,BOOT_NUMBER,VALIDATION) "
2969     << " VALUES (NULL,' "
2970     << idroot << "','"
2971     << i << "','"
2972     << fromtime << "','"
2973     << totime << "','"
2974     << section << "','"
2975     << obt << "','"
2976     << pkt << "','"
2977     << this->GetBOOTnumber() << "','"
2978     << valid << "');";
2979     //
2980     if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
2981     //
2982     result = conn->Query(oss.str().c_str());
2983     //
2984     if ( !result ) throw -4;
2985     //
2986     };
2987     //
2988     } else {
2989     //
2990 mocchiut 1.34 if ( IsDebug() ) printf(" Repeated calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
2991 mocchiut 1.1 //
2992     };
2993     //
2994     };
2995     };
2996     };
2997     //
2998     return(0);
2999     };
3000    
3001     /**
3002     * Fill the GL_TRK_CALIB table
3003     */
3004     void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){
3005     //
3006     TSQLResult *result = 0;
3007     TSQLRow *row = 0;
3008     //
3009     stringstream oss;
3010     oss.str("");
3011     //
3012     UInt_t totime = 0;
3013     //
3014     if ( !pk1 && !pk2 ){
3015     if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n");
3016     };
3017     //
3018     // check if the calibration has already been inserted
3019     //
3020     oss.str("");
3021     oss << " SELECT ID FROM GL_TRK_CALIB WHERE "
3022     << " BOOT_NUMBER = "<< this->GetBOOTnumber(); //
3023     oss << " AND ( ( ";
3024     if ( pk1 ){
3025     oss << " OBT1 = "<< obt1 << " AND "
3026     << " PKT1 = "<< pkt1
3027     << " ) OR ( ";
3028     } else {
3029     oss << " PKT1 = "<< pkt2-1
3030     << " ) OR ( ";
3031     };
3032     if ( pk2 ){
3033     oss << " OBT2 = "<< obt2 << " AND "
3034     << " PKT2 = "<< pkt2;
3035     } else {
3036     oss << " PKT2 = "<< pkt1+1;
3037     };
3038     oss << " ) );";
3039     //
3040     if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str());
3041     result = conn->Query(oss.str().c_str());
3042     //
3043     if ( !result ) throw -4;
3044     //
3045     row = result->Next();
3046     //
3047     if ( row ){
3048     //
3049     if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");
3050     //
3051     } else {
3052     //
3053     // we have to insert a new calibration, check where to place it
3054     //
3055     oss.str("");
3056     oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "
3057     << " FROM_TIME < "<< fromtime << " AND "
3058     << " TO_TIME > "<< fromtime << ";";
3059     //
3060     if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());
3061     result = conn->Query(oss.str().c_str());
3062     //
3063     if ( !result ) throw -4;
3064     //
3065     row = result->Next();
3066     //
3067     if ( !row ){
3068     //
3069     // no calibrations in the db contain our calibration
3070     //
3071     if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
3072 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
3073 mocchiut 1.1 //
3074     oss.str("");
3075     oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
3076     << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
3077     //
3078     if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
3079     result = conn->Query(oss.str().c_str());
3080     //
3081     if ( !result ) throw -4;
3082     //
3083     row = result->Next();
3084     if ( !row ){
3085     totime = numeric_limits<UInt_t>::max();
3086     } else {
3087     totime = (UInt_t)atoll(row->GetField(0));
3088     };
3089     //
3090     } else {
3091     //
3092     // determine upper and lower limits and make space for the new calibration
3093     //
3094     totime = (UInt_t)atoll(row->GetField(1));
3095     //
3096     oss.str("");
3097     oss << " UPDATE GL_TRK_CALIB SET "
3098     << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
3099     << " ID = "<< row->GetField(0) << ";";
3100     //
3101     if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str());
3102     result = conn->Query(oss.str().c_str());
3103     //
3104     if ( !result ) throw -4;
3105     //
3106     };
3107     //
3108     oss.str("");
3109     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) "
3110     << " VALUES (NULL,' "
3111     << idroot << "',";
3112     //
3113     if ( !pk1 ){
3114     oss << "NULL,";
3115     } else {
3116     oss << "'"
3117     << t1 << "',";
3118     };
3119     //
3120     if ( !pk2 ){
3121     oss << "NULL,'";
3122     } else {
3123     oss << "'"
3124     << t2 << "','";
3125     };
3126     //
3127     oss << fromtime << "','"
3128     << totime << "','"
3129     << obt1 << "','"
3130     << pkt1 << "','"
3131     << obt2 << "','"
3132     << pkt2 << "','"
3133     << this->GetBOOTnumber() << "','"
3134     << valid << "');";
3135     //
3136     if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());
3137     //
3138     result = conn->Query(oss.str().c_str());
3139     //
3140     if ( !result ) throw -4;
3141     //
3142     };
3143     //
3144     };
3145    
3146     /**
3147     * Scan tracker calibrations packets, fill the GL_TRK_CALIB table
3148     */
3149     Int_t PamelaDBOperations::insertTRK_CALIB(){
3150     //
3151     CalibTrk1Event *caltrk1 = 0;
3152     CalibTrk2Event *caltrk2 = 0;
3153     TTree *tr1 = 0;
3154     TTree *tr2 = 0;
3155     EventHeader *eh1 = 0;
3156     PscuHeader *ph1 = 0;
3157     EventHeader *eh2 = 0;
3158     PscuHeader *ph2 = 0;
3159     //
3160     PacketType *pctp=0;
3161     EventCounter *codt2=0;
3162     //
3163     Int_t nevents1 = 0;
3164     Int_t nevents2 = 0;
3165     //
3166     fromtime = 0;
3167     //
3168     obt1 = 0;
3169     pkt1 = 0;
3170     obt2 = 0;
3171     pkt2 = 0;
3172     //
3173     tr1 = (TTree*)file->Get("CalibTrk1");
3174     if ( !tr1 || tr1->IsZombie() ) throw -22;
3175     tr2 = (TTree*)file->Get("CalibTrk2");
3176     if ( !tr2 || tr2->IsZombie() ) throw -23;
3177     //
3178     tr1->SetBranchAddress("CalibTrk1", &caltrk1);
3179     tr1->SetBranchAddress("Header", &eh1);
3180     nevents1 = tr1->GetEntries();
3181     tr2->SetBranchAddress("CalibTrk2", &caltrk2);
3182     tr2->SetBranchAddress("Header", &eh2);
3183     nevents2 = tr2->GetEntries();
3184     //
3185 mocchiut 1.2 if ( !nevents1 && !nevents2 ) return(1);
3186 mocchiut 1.1 //
3187     t2 = -1;
3188     Int_t pret2 = 0;
3189     Int_t t2t1cal = 0;
3190     //
3191     for (t1=0; t1 < nevents1; t1++){
3192     //
3193     pret2 = t2;
3194     tr1->GetEntry(t1);
3195     //
3196     ph1 = eh1->GetPscuHeader();
3197     obt1 = ph1->GetOrbitalTime();
3198     pkt1 = ph1->GetCounter();
3199     fromtime = this->GetAbsTime(ph1->GetOrbitalTime());
3200     //
3201 pam-fi 1.22 // valid = 1;
3202     // //
3203     // if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1
3204 mocchiut 1.1 //
3205     //
3206     if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){
3207     //
3208 mocchiut 1.34 if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u \n",fromtime,obt1,pkt1);
3209 pam-fi 1.22 //
3210     valid = ValidateTrkCalib( caltrk1, eh1 );
3211     if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
3212 mocchiut 1.1 //
3213     // Do we have the second calibration packet?
3214     //
3215     while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1
3216     //
3217     t2++;
3218     //
3219     if ( t2 < nevents2 ){
3220     tr2->GetEntry(t2);
3221     codt2 = eh2->GetCounter();
3222     t2t1cal = codt2->Get(pctp->CalibTrk1);
3223     //
3224     ph2 = eh2->GetPscuHeader();
3225     obt2 = ph2->GetOrbitalTime();
3226     pkt2 = ph2->GetCounter();
3227     //
3228 pam-fi 1.22 // if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2
3229 mocchiut 1.1 //
3230     } else {
3231     //
3232     // running out of vector without finding the corresponding calibration, sig
3233     //
3234     pret2 = t2;
3235     obt2 = 0;
3236     pkt2 = pkt1+2;
3237     t2t1cal = t1+1;
3238     };
3239     if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
3240     //
3241     // running out of vector without finding the corresponding calibration, sig
3242     //
3243     pret2 = t2;
3244     obt2 = 0;
3245     pkt2 = pkt1+2;
3246     t2t1cal = t1+1;
3247     };
3248     //
3249     };
3250     //
3251 mocchiut 1.34 if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2);
3252 mocchiut 1.1 //
3253     // The calibration is good
3254     //
3255     if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){
3256     //
3257 mocchiut 1.34 if ( IsDebug() ) printf(" The trk calibration2 at obt %u pkt %u t2 is %u is good \n",obt2,pkt2,t2);
3258 mocchiut 1.1 //
3259 pam-fi 1.22 UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
3260     if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
3261     valid = valid & valid2;
3262     //
3263 mocchiut 1.1 // Handle good calib
3264     //
3265     this->HandleTRK_CALIB(true,true);
3266     //
3267     // Check for missing calibtrk1
3268     //
3269     if ( t2 != pret2+1 ){
3270     //
3271 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);
3272 mocchiut 1.1 //
3273     while ( t2 > pret2+1 ){
3274     //
3275     // handle missing calib1
3276     //
3277     pret2++;
3278     //
3279     obt1 = 0;
3280     pkt1 = 0;
3281     //
3282     tr2->GetEntry(pret2);
3283     ph2 = eh2->GetPscuHeader();
3284     obt2 = ph2->GetOrbitalTime();
3285     pkt2 = ph2->GetCounter();
3286     //
3287     fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
3288     //
3289     valid = 0;
3290     this->HandleTRK_CALIB(false,true);
3291     //
3292     };
3293     //
3294     };
3295     //
3296     } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){
3297     //
3298     // Check for missing calibtrk2
3299     //
3300 mocchiut 1.34 if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
3301 mocchiut 1.1 t2 = pret2;
3302     //
3303     // handle missing calib2
3304     //
3305     obt2 = 0;
3306     pkt2 = 0;
3307     valid = 0;
3308     this->HandleTRK_CALIB(true,false);
3309     //
3310     };
3311     //
3312     } else {
3313     //
3314 mocchiut 1.34 if ( IsDebug() ) printf(" Repetead trk calibration1 at time %u obt %u pkt %u \n",fromtime,obt1,pkt1);
3315 mocchiut 1.1 //
3316     };
3317     //
3318     };
3319     //
3320     // we have one more calib pkt2 !
3321     //
3322     t2++;
3323     while ( t2 < nevents2 ){
3324     //
3325     // handle missing calib1
3326     //
3327     obt1 = 0;
3328     pkt1 = 0;
3329     //
3330     tr2->GetEntry(t2);
3331     ph2 = eh2->GetPscuHeader();
3332     obt2 = ph2->GetOrbitalTime();
3333     pkt2 = ph2->GetCounter();
3334     //
3335     fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
3336     valid = 0;
3337     if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){
3338     //
3339 mocchiut 1.34 if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
3340 mocchiut 1.1 //
3341     this->HandleTRK_CALIB(false,true);
3342     //
3343     };
3344     //
3345     t2++;
3346     //
3347     };
3348     //
3349     return(0);
3350     };
3351    
3352    
3353     /**
3354     * Scan S4 calibrations packets, fill the GL_S4_CALIB table
3355     */
3356     Int_t PamelaDBOperations::insertS4_CALIB(){
3357     //
3358     TSQLResult *result = 0;
3359     TSQLRow *row = 0;
3360     //
3361     stringstream oss;
3362     oss.str("");
3363     //
3364     TTree *tr = 0;
3365     EventHeader *eh = 0;
3366     PscuHeader *ph = 0;
3367     //
3368     UInt_t nevents = 0;
3369     UInt_t fromtime = 0;
3370     UInt_t totime = 0;
3371     UInt_t obt = 0;
3372     UInt_t pkt = 0;
3373     //
3374     tr = (TTree*)file->Get("CalibS4");
3375     if ( !tr || tr->IsZombie() ) throw -24;
3376     //
3377     tr->SetBranchAddress("Header", &eh);
3378     //
3379     nevents = tr->GetEntries();
3380     //
3381 mocchiut 1.2 if ( !nevents ) return(1);
3382 mocchiut 1.1 //
3383     for (UInt_t i = 0; i < nevents; i++){
3384     //
3385     tr->GetEntry(i);
3386     //
3387     ph = eh->GetPscuHeader();
3388     obt = ph->GetOrbitalTime();
3389     pkt = ph->GetCounter();
3390     fromtime = this->GetAbsTime(ph->GetOrbitalTime());
3391     if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
3392     //
3393 mocchiut 1.34 if ( IsDebug() ) printf(" S4 calibration at time %u obt %u pkt %u \n",fromtime,obt,pkt);
3394 mocchiut 1.1 //
3395     // check if the calibration has already been inserted
3396     //
3397     oss.str("");
3398     oss << " SELECT ID FROM GL_S4_CALIB WHERE "
3399     << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
3400     << " OBT = "<< obt << " AND "
3401     << " PKT = "<< pkt << ";";
3402     //
3403     if ( IsDebug() ) printf(" Check if the S4 calibration has already been inserted: query is \n %s \n",oss.str().c_str());
3404     result = conn->Query(oss.str().c_str());
3405     //
3406     if ( !result ) throw -4;
3407     //
3408     row = result->Next();
3409     //
3410     if ( row ){
3411     //
3412     if ( IsDebug() ) printf(" S4 calibration already inserted in the DB\n");
3413     //
3414     } else {
3415     //
3416     // we have to insert a new calibration, check where to place it
3417     //
3418     oss.str("");
3419     oss << " SELECT ID,TO_TIME FROM GL_S4_CALIB WHERE "
3420     << " FROM_TIME < "<< fromtime << " AND "
3421     << " TO_TIME > "<< fromtime << ";";
3422     //
3423     if ( IsDebug() ) printf(" Check where to place the S4 calibration: query is \n %s \n",oss.str().c_str());
3424     result = conn->Query(oss.str().c_str());
3425     //
3426     if ( !result ) throw -4;
3427     //
3428     row = result->Next();
3429     //
3430     if ( !row ){
3431     //
3432     // no calibrations in the db contain our calibration
3433     //
3434     if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");
3435 mocchiut 1.19 if ( fromtime < 1150871000 ){
3436 mocchiut 1.35 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
3437 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
3438     };
3439     //
3440     oss.str("");
3441     oss << " SELECT FROM_TIME FROM GL_S4_CALIB WHERE "
3442     << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
3443     //
3444     if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
3445     result = conn->Query(oss.str().c_str());
3446     //
3447     if ( !result ) throw -4;
3448     //
3449     row = result->Next();
3450     if ( !row ){
3451     totime = numeric_limits<UInt_t>::max();
3452     } else {
3453     totime = (UInt_t)atoll(row->GetField(0));
3454     };
3455     //
3456     } else {
3457     //
3458     // determine upper and lower limits and make space for the new calibration
3459     //
3460     totime = (UInt_t)atoll(row->GetField(1));
3461     //
3462     oss.str("");
3463     oss << " UPDATE GL_S4_CALIB SET "
3464     << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
3465     << " ID = "<< row->GetField(0) << ";";
3466     //
3467     if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
3468     result = conn->Query(oss.str().c_str());
3469     //
3470     if ( !result ) throw -4;
3471     //
3472     };
3473     //
3474     oss.str("");
3475 mocchiut 1.2 oss << " INSERT INTO GL_S4_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,OBT,PKT,BOOT_NUMBER) "
3476 mocchiut 1.1 << " VALUES (NULL,' "
3477     << idroot << "','"
3478     << i << "','"
3479     << fromtime << "','"
3480     << totime << "','"
3481     << obt << "','"
3482     << pkt << "','"
3483 mocchiut 1.2 << this->GetBOOTnumber() << "');";
3484 mocchiut 1.1 //
3485     if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
3486     //
3487     result = conn->Query(oss.str().c_str());
3488     //
3489     if ( !result ) throw -4;
3490     //
3491     };
3492     //
3493     } else {
3494     //
3495 mocchiut 1.34 if ( IsDebug() ) printf(" Repeated S4 calibration at time %u obt %u pkt %u \n",fromtime,obt,pkt);
3496 mocchiut 1.1 //
3497     };
3498     //
3499     };
3500     //
3501     return(0);
3502     };
3503    
3504 mocchiut 1.2 /**
3505     * Scan the fragment table and move old fragments to the GL_RUN table
3506 mocchiut 1.1 */
3507 mocchiut 1.2 Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3508 mocchiut 1.16 return(this->CleanGL_RUN_FRAGMENTS(""));
3509     };
3510    
3511     /**
3512     * Scan the fragment table and move old fragments to the GL_RUN table
3513     */
3514     Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3515 mocchiut 1.2 //
3516     TSQLResult *result = 0;
3517     TSQLRow *row = 0;
3518     TSQLResult *result2 = 0;
3519     TSQLRow *row2 = 0;
3520     //
3521     UInt_t moved = 0;
3522     //
3523     stringstream oss;
3524     oss.str("");
3525     //
3526 mocchiut 1.16 if ( !strcmp(fcleanfile.Data(),"") ){
3527     //
3528     // check if there are entries older than "olderthan" seconds from now
3529     //
3530     oss.str("");
3531     oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3532     << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3533     //
3534     if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
3535     result = conn->Query(oss.str().c_str());
3536     //
3537     } else {
3538     oss.str("");
3539     oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3540     if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3541     result = conn->Query(oss.str().c_str());
3542     //
3543     if ( result ){
3544     //
3545     row = result->Next();
3546     //
3547     if ( row ){
3548     oss.str("");
3549     oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3550     << " ID_ROOT_L0=" << row->GetField(0) << ";";
3551     //
3552     if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3553     result = conn->Query(oss.str().c_str());
3554     //
3555     };
3556     } else {
3557     return(2);
3558     };
3559     };
3560 mocchiut 1.2 //
3561     if ( result ){
3562     //
3563     row = result->Next();
3564     //
3565 mocchiut 1.12 while ( row ){
3566 mocchiut 1.2 //
3567     oss.str("");
3568     oss << " ID= "<< row->GetField(0);
3569     //
3570     glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
3571     //
3572     oss.str("");
3573     oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
3574     << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
3575     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3576     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3577     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3578     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3579     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3580     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3581     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3582     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3583     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3584     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3585     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3586     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3587     //
3588     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3589     result2 = conn->Query(oss.str().c_str());
3590     //
3591     if ( !result2 ) throw -4;
3592     //
3593     row2 = result2->Next();
3594     //
3595     if ( !row2 ){
3596     //
3597     if ( IsDebug() ) printf(" The run is new \n");
3598     if ( IsDebug() ) printf(" -> fill the DB \n");
3599     //
3600 mocchiut 1.13 // glrun->SetID(this->AssignRunID()); we use the old run number!
3601     glrun->SetID_RUN_FRAG(glrun->GetID());
3602 mocchiut 1.2 glrun->Fill_GL_RUN(conn);
3603     //
3604 mocchiut 1.13 // oss.str("");
3605     // oss << " SELECT ID FROM GL_RUN WHERE "
3606     // << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3607     // << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3608     // << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3609     // << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3610     // << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3611     // //
3612     // if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
3613     // result2 = conn->Query(oss.str().c_str());
3614     // //
3615     // if ( !result2 ) throw -4;
3616     // //
3617     // row2 = result2->Next();
3618     // //
3619     // if ( !row2 ) throw -25;
3620     // //
3621     // oss.str("");
3622     // oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);
3623     // if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());
3624     // result2 = conn->Query(oss.str().c_str());
3625     // //
3626     // if ( !result2 ) throw -4;
3627 mocchiut 1.2 //
3628     moved++;
3629     //
3630     } else {
3631     if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");
3632 mocchiut 1.1 };
3633 mocchiut 1.2 if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));
3634     //
3635     //
3636 mocchiut 1.15 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3637     // oss.str("");
3638     // oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
3639     // if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
3640     // result2 = conn->Query(oss.str().c_str());
3641     // //
3642     // if ( !result2 ) throw -4;
3643     // //
3644 mocchiut 1.12 row = result->Next();
3645 mocchiut 1.1 };
3646     };
3647 mocchiut 1.2 if ( IsDebug() ) printf(" Moved %u runs\n",moved);
3648     return(0);
3649     };
3650 mocchiut 1.1
3651 mocchiut 1.2 /**
3652     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3653     */
3654     Int_t PamelaDBOperations::ValidateRuns(){
3655 mocchiut 1.16 return(this->ValidateRuns(""));
3656     };
3657    
3658     /**
3659     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3660     */
3661     Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3662 mocchiut 1.2 //
3663     TSQLResult *result = 0;
3664 pam-fi 1.5 TSQLRow *row = 0;
3665 mocchiut 1.2 //
3666 mocchiut 1.16 UInt_t calibtime = 50;
3667     //
3668 mocchiut 1.2 stringstream oss;
3669     oss.str("");
3670     //
3671 pam-fi 1.5 // =======================================================
3672     // validate runs by checking missing calibrations
3673     // =======================================================
3674     UInt_t t_stop = 0;
3675     UInt_t t_start = 0;
3676 mocchiut 1.16 if ( !strcmp(valfile.Data(),"") ) {
3677     // --------------------------------------------------------------
3678     // 1) get the OBT of the last run inserted after clean-time limit
3679     // --------------------------------------------------------------
3680     oss.str("");
3681     oss << " SELECT * FROM GL_RUN WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3682     << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3683     if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3684     result = conn->Query(oss.str().c_str());
3685     if ( !result ) throw -4;
3686     if ( !result->GetRowCount() ) {
3687     printf(" No runs to validate \n");
3688     return(1);
3689     }else{
3690     row = result->Next();
3691     t_start = (UInt_t)atoll(row->GetField(4));
3692     };
3693     // --------------------------------------------------------------
3694     // 2) get the OBT of the last validated run
3695     // --------------------------------------------------------------
3696     oss.str("");
3697     oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3698     <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3699     if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3700     result = conn->Query(oss.str().c_str());
3701     if ( !result ) throw -4;
3702     if ( result->GetRowCount() ){
3703     row = result->Next();
3704     t_stop = (UInt_t)atoll(row->GetField(4));
3705     };
3706 mocchiut 1.35 if ( IsDebug() ) printf("Validation interval: from time %u - to time %u \n\n",t_stop,t_start);
3707 mocchiut 1.16 // --------------------------------------------------------------
3708     // now retrieves runs to be validated
3709     // --------------------------------------------------------------
3710     oss.str("");
3711     oss << " SELECT * FROM GL_RUN WHERE RUNHEADER_TIME <=" << t_start;
3712     oss << " AND RUNHEADER_TIME >="<< t_stop;
3713     oss << " ORDER BY RUNHEADER_TIME DESC;";
3714     if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3715     result = conn->Query(oss.str().c_str());
3716     } else {
3717     //
3718     stringstream myquery;
3719     UInt_t myid = 0;
3720     myquery.str("");
3721     myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3722     //
3723     result = conn->Query(myquery.str().c_str());
3724     //
3725     row = result->Next();
3726     if( !row ){
3727     if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3728     if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3729     return(2);
3730     };
3731     if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3732     return(0);
3733     };
3734     myid=(UInt_t)atoll(row->GetField(0));
3735     //
3736     myquery.str("");
3737     myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3738     //
3739     result = conn->Query(myquery.str().c_str());
3740     //
3741     row = result->Next();
3742     if( !row->GetField(0) || !row->GetField(1)){
3743     //
3744     if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3745     //
3746     return(0);
3747     //
3748     } else {
3749     //
3750     UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3751     UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3752     UInt_t caltime = 0;
3753     //
3754     myquery.str("");
3755     myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3756     myquery << " order by FROM_TIME asc limit 1;";
3757     //
3758     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
3759     //
3760     //
3761     result = conn->Query(myquery.str().c_str());
3762     //
3763     row = result->Next();
3764     if( !row ){
3765     caltime = runhtime;
3766     } else {
3767     caltime = (UInt_t)atoll(row->GetField(0));
3768     };
3769     //
3770     myquery.str("");
3771     myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3772     myquery << caltime << " order by RUNHEADER_TIME DESC";
3773     //
3774     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
3775     //
3776     result = conn->Query(myquery.str().c_str());
3777     //
3778     };
3779 pam-fi 1.5 };
3780 mocchiut 1.16 //
3781 mocchiut 1.2 if ( !result ) throw -4;
3782 mocchiut 1.16 if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3783     //
3784 pam-fi 1.5 Int_t nrow = 0;
3785     GL_RUN* this_run = new GL_RUN();
3786     GL_RUN* next_run = new GL_RUN();
3787 pam-fi 1.8 Int_t nseq_max = 1000;
3788 pam-fi 1.5 // UInt_t* sequence = new UInt_t[100];
3789     vector<UInt_t> sequence(nseq_max);
3790     Int_t nseq = 0;
3791     Bool_t CHECK = false;
3792     Bool_t this_ONLINE = false;
3793     Bool_t next_ONLINE = false;
3794     UInt_t t1=0,t2=0;
3795     // ---------------------------------------------------------------------------------
3796     // - loop over runs, back in time,
3797 mocchiut 1.16 // - select sequences of runs close in time (less than calibtime s apart),
3798 pam-fi 1.5 // which could be preceeded by a calibration
3799     // - check if there might be a missing calibration
3800     // ---------------------------------------------------------------------------------
3801     while(1){
3802    
3803     row = result->Next();
3804     if( row == NULL ) break;
3805    
3806     //------------
3807     //get run info
3808     //------------
3809     this_run->Set_GL_RUN(row);
3810 pam-fi 1.8
3811 pam-fi 1.5 Bool_t this_BAD = false;
3812     if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;
3813     else if (this_run->GetTRK_CALIB_USED() == 104) this_ONLINE = false;
3814     else{
3815 pam-fi 1.8 // printf("Missing or corrupted header!! \n");
3816 pam-fi 1.5 this_ONLINE = false;
3817     this_BAD = true;
3818     };
3819    
3820     //-----------------------------------
3821     //compare with previous(next in time)
3822     //-----------------------------------
3823     CHECK = false;
3824     UInt_t interval=0;
3825    
3826     if( nrow != 0){
3827    
3828    
3829     t1 = this_run->GetRUNTRAILER_TIME();
3830     t2 = next_run->GetRUNHEADER_TIME();
3831     interval = (t2-t1);
3832    
3833     if(this_ONLINE && next_ONLINE){ // this: ON-LINE + next: ON-LINE
3834    
3835     if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0; //=> run fragments
3836    
3837 mocchiut 1.16 if( interval >= calibtime )CHECK = true; //more than calibtime s => there might be a calibration
3838 pam-fi 1.5
3839     if( !CHECK && this_run->VALIDATION ){
3840     for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
3841     nseq=0;
3842     }
3843    
3844     }else if( !this_ONLINE && next_ONLINE) { // this: DEFAULT + next:ON-LINE
3845    
3846     CHECK = true;
3847    
3848     }else if( !next_ONLINE ){ // this:ANY + next:DEFAULT
3849    
3850     assignVALIDATION(next_run->ID,true);
3851     nseq=0;
3852     }
3853     }
3854    
3855     //----------------------------
3856     //check run sequence for calib
3857     //----------------------------
3858     if( CHECK ){
3859     // check if calibration exists
3860 pam-fi 1.8 if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
3861 pam-fi 1.5 Bool_t MISSING = MissingTRK_CALIB(t1,t2);
3862     for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
3863     nseq=0;
3864 pam-fi 1.8 };
3865 pam-fi 1.5 //--------------
3866     //store run info
3867     //--------------
3868     *next_run = *this_run;
3869     next_ONLINE = this_ONLINE;
3870     if( !this_BAD ){
3871     if(nseq < nseq_max){
3872     sequence[nseq] = this_run->ID;
3873     nseq++;
3874     }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
3875     };
3876    
3877 mocchiut 1.35 if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);
3878 pam-fi 1.5 nrow++;
3879    
3880     };
3881     delete this_run;
3882     delete next_run;
3883 mocchiut 1.2 //
3884     return(0);
3885 mocchiut 1.1 };
3886 pam-fi 1.5 /**
3887     * Check if there might be a missing tracker calibration in a given time interval
3888     * @param t1 From absolute time
3889     * @param t2 To absolute time
3890     * @return true if there might be a missing calibration
3891     */
3892     Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
3893    
3894     GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
3895    
3896     // get the closest VALIDATED calibration before the run start (t2)
3897     if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true); //>>> missing
3898    
3899     if ( trkcalib->TO_TIME < t2 ) return(true); //>>> missing
3900    
3901     //==============================================================
3902     // Check is done first on the basis of time between calibration,
3903     // which should be equal to the time between ascending-nodes.
3904     //==============================================================
3905     if ( t2 - trkcalib->FROM_TIME > 5700) {
3906 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);
3907 pam-fi 1.5 //==============================================================
3908     // there might be a missing calibration, due to:
3909     // - MM full
3910     // - corrupted packets
3911     // - loss of data
3912     // There is an exception in case a download was done during ascending node
3913     //==============================================================
3914     Bool_t DOWNLOAD = false;
3915     // check if the calib was skipped becouse of download .... DA FARE!!
3916     if(DOWNLOAD)return(false);
3917    
3918     return(true); //>>> missing
3919    
3920     };
3921    
3922     //==============================================================
3923     // If the last calibration is close to the run less than this time,
3924     // it is enough to say that there are no missing calibrations
3925     //==============================================================
3926     // the long time interval bewteen runs might be due to download
3927 mocchiut 1.35 if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
3928 pam-fi 1.5 return(false);
3929    
3930     };
3931     /**
3932     * Assign VALIDATION value to a GL_RUN entry
3933     * @param idrun Run ID
3934     * @param validation true/false
3935     */
3936     Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
3937     TSQLResult *result = 0;
3938     stringstream oss;
3939     oss.str("");
3940 pam-fi 1.8 oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
3941 pam-fi 1.5 //
3942     // if ( IsDebug() )
3943 pam-fi 1.8 // printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
3944     if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
3945     result = conn->Query(oss.str().c_str());
3946     if ( !result ) throw -4;
3947 pam-fi 1.5 return(0);
3948     }
3949    
3950    
3951    
3952 mocchiut 1.12 // Insert TLEs from file tlefilename in the table GL_TLE in the db
3953     // opened by conn, sorting them by date from older to newer, if each
3954     // TLE has not been alread inserted.
3955     Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
3956     {
3957     fstream tlefile(tlefilename, ios::in);
3958    
3959     if ( !tlefile ) throw -7;
3960    
3961     vector<cTle*> ctles;
3962     vector<cTle*>::iterator iter;
3963     int present = 0;
3964    
3965     // Get three lines from tlefile, create a cTle object and put it
3966     // into ctles
3967     while(1) {
3968     cTle *tlef;
3969     string str1, str2, str3;
3970    
3971     getline(tlefile, str1);
3972     if(tlefile.eof()) break;
3973    
3974     getline(tlefile, str2);
3975     if(tlefile.eof()) break;
3976    
3977     getline(tlefile, str3);
3978     if(tlefile.eof()) break;
3979    
3980     // We now have three good lines for a cTle.
3981     tlef = new cTle(str1, str2, str3);
3982     ctles.push_back(tlef);
3983     }
3984    
3985     tlefile.close();
3986    
3987     // Sort by date
3988     sort(ctles.begin(), ctles.end(), compTLE);
3989    
3990     // Now we insert each TLE into the db
3991     for(iter = ctles.begin(); iter != ctles.end(); iter++) {
3992     cTle *tle = *iter;
3993    
3994     // Do nothing if it's already present in the db. Just increase
3995     // the counter present.
3996     if (! isTlePresent(tle))
3997     {
3998     int status = insertTle(tle);
3999    
4000     // Insert query failed. Return 1.
4001     if(status == EXIT_FAILURE) {
4002    
4003     if( IsDebug() ) {
4004     cerr << "Error: inserting TLE:" << endl
4005     << tle->getName() << endl
4006     << tle->getLine1() << endl
4007     << tle->getLine2() << endl;
4008     }
4009    
4010     throw -4;
4011     return 1;
4012     }
4013    
4014     }
4015     else
4016     present++;
4017    
4018     }
4019    
4020     int inserted = ctles.size() - present; // Number of inserted TLE.
4021     if ( IsDebug() )
4022     cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
4023     << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
4024    
4025     ctles.clear();
4026    
4027    
4028     // Return 2 if no new TLE has been inserted. 0 otherwise.
4029     if(! inserted ) return 2;
4030     return 0;
4031     }
4032    
4033    
4034     // Insert tle in the table GL_TLE using the connection conn.
4035 mocchiut 1.16 Int_t PamelaDBOperations::insertTle(cTle *tle)
4036 mocchiut 1.12 {
4037     stringstream oss;
4038     TSQLResult *result = 0;
4039    
4040     oss.str("");
4041     oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
4042     << " VALUES ( '"
4043     << tle->getName() << "', '"
4044     << tle->getLine1() << "', '"
4045     << tle->getLine2() << "', '"
4046     << getTleDatetime(tle) << "')";
4047    
4048     // cout << oss.str().c_str() << endl;
4049     result = conn->Query(oss.str().c_str());
4050     if (result == NULL)
4051     return EXIT_FAILURE;
4052    
4053     return EXIT_SUCCESS;
4054     }
4055    
4056    
4057     // Return whether tle is already in the db connected by conn.
4058     bool PamelaDBOperations::isTlePresent(cTle *tle)
4059     {
4060     stringstream oss;
4061     TSQLResult *result = 0;
4062    
4063     oss.str("");
4064     oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
4065     << getTleDatetime(tle) << "'";
4066    
4067     result = conn->Query(oss.str().c_str());
4068     if (result == NULL) throw -4;
4069    
4070     if (result->GetRowCount())
4071     return true;
4072     else
4073     return false;
4074     }
4075    
4076    
4077     // Return whether the first TLE is dated early than the second
4078     bool compTLE (cTle *tle1, cTle *tle2)
4079     {
4080     return getTleJulian(tle1) < getTleJulian(tle2);
4081     }
4082    
4083    
4084     // Return the date of the tle using the format (year-2000)*1e3 +
4085     // julian day. e.g. 6365 is the 31th Dec 2006.
4086     // It does *not* return a cJulian date.
4087     float getTleJulian(cTle *tle) {
4088     return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
4089     }
4090    
4091    
4092     // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
4093     // format.
4094     string getTleDatetime(cTle *tle)
4095     {
4096     int year, mon, day, hh, mm, ss;
4097     double dom; // day of month (is double!)
4098     stringstream date; // date in datetime format
4099    
4100     // create a cJulian from the date in tle
4101     cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
4102    
4103     // get year, month, day of month
4104     jdate.getComponent(&year, &mon, &dom);
4105    
4106     // build a datetime YYYY-MM-DD hh:mm:ss
4107     date.str("");
4108     day = (int) floor(dom);
4109     hh = (int) floor( (dom - day) * 24);
4110     mm = (int) floor( ((dom - day) * 24 - hh) * 60);
4111     ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
4112     // ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
4113    
4114     date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
4115    
4116     return date.str();
4117     }
4118 mocchiut 1.16
4119     /**
4120     * Remove a file from the DB, delete on cascade all entries related to that file
4121     * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
4122     * calibration
4123     **/
4124     Int_t PamelaDBOperations::removeFile(TString remfile){
4125     //
4126     // Determine ID_ROOT_L0 and ID_RAW
4127     //
4128     TSQLResult *pResult;
4129     TSQLRow *Row;
4130     stringstream myquery;
4131     //
4132     myquery.str("");
4133     myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
4134     //
4135     pResult = conn->Query(myquery.str().c_str());
4136     //
4137     Row = pResult->Next();
4138     if( !Row ){
4139     if ( strcmp(remfile.Data(),GetRootName().Data()) ){
4140     if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
4141     return(1);
4142     };
4143     if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
4144     return(0);
4145     };
4146     //
4147     this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
4148     this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
4149     //
4150     this->ValidationOFF();
4151     //
4152     this->RemoveCALIBS();
4153     //
4154     this->RemoveRUNS();
4155     //
4156     this->RemoveFILES();
4157     //
4158     this->SetID_ROOT(0);
4159     this->SetID_RAW(0);
4160     //
4161     return(0);
4162     };
4163    
4164     /**
4165     *
4166     * Set validation bit to zero for runs following the removing file till
4167     * 1) a run with TRK_CALIB_USED=140
4168     * 2) a run with VALIDATION = 0
4169     * 3) the next calibration
4170     *
4171     **/
4172     void PamelaDBOperations::ValidationOFF(){
4173     TSQLResult *pResult;
4174     TSQLRow *Row;
4175     stringstream myquery;
4176     Int_t unv = 0;
4177     //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
4178     myquery.str("");
4179     myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4180     //
4181     pResult = conn->Query(myquery.str().c_str());
4182     //
4183     Row = pResult->Next();
4184     if( !Row->GetField(0) ){
4185     //
4186     if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
4187     //
4188     } else {
4189     //
4190     UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
4191     UInt_t caltime = 0;
4192     //
4193     myquery.str("");
4194     myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
4195     myquery << " order by FROM_TIME asc limit 1;";
4196     //
4197     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4198     //
4199     //
4200     delete pResult;
4201     pResult = conn->Query(myquery.str().c_str());
4202     //
4203     Row = pResult->Next();
4204     if( !Row ){
4205     caltime = runhtime;
4206     } else {
4207     caltime = (UInt_t)atoll(Row->GetField(0));
4208     };
4209     //
4210     myquery.str("");
4211     myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4212     myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4213     //
4214     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4215     //
4216     pResult = conn->Query(myquery.str().c_str());
4217     //
4218     Row = pResult->Next();
4219     if( !Row ){
4220     //
4221     if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4222     //
4223     } else {
4224     myquery.str("");
4225     myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4226     myquery << " RUNHEADER_TIME>=" <<runhtime;
4227     myquery << " order by RUNHEADER_TIME asc;";
4228     //
4229     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4230     //
4231     pResult = conn->Query(myquery.str().c_str());
4232     //
4233     Row = pResult->Next();
4234     while ( Row ){
4235     //
4236     unv++;
4237     this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4238     Row = pResult->Next();
4239     //
4240     };
4241     };
4242     };
4243 mocchiut 1.35 if ( IsDebug() ) printf(" %u runs have been unvalidated \n",unv);
4244 mocchiut 1.16 };
4245    
4246     /**
4247     *
4248     * Rearrange GL_RUN table and remove runs
4249     *
4250     **/
4251     void PamelaDBOperations::RemoveRUNS(){
4252     TSQLResult *pResult;
4253     TSQLRow *Row;
4254     stringstream myquery;
4255     UInt_t drun = 0;
4256     GL_RUN *delrun = new GL_RUN();
4257     //
4258     myquery.str("");
4259     myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4260     //
4261     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4262     //
4263     pResult = conn->Query(myquery.str().c_str());
4264     //
4265     Row = pResult->Next();
4266     //
4267     //
4268     if ( !Row ){
4269     if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4270     } else {
4271     if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");
4272     while ( Row ){
4273     delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4274 mocchiut 1.35 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4275 mocchiut 1.16 drun++;
4276     Row = pResult->Next();
4277     };
4278     };
4279     //
4280     //
4281     myquery.str("");
4282     myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4283     //
4284     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4285     //
4286     pResult = conn->Query(myquery.str().c_str());
4287     //
4288     Row = pResult->Next();
4289     //
4290     if ( !Row ){
4291     if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4292     } else {
4293     if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");
4294     while ( Row ){
4295 mocchiut 1.35 if ( IsDebug() ) printf(" restore run %u \n",(UInt_t)atoll(Row->GetField(1)));
4296 mocchiut 1.16 delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4297 mocchiut 1.35 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(1)));
4298 mocchiut 1.16 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4299     if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4300 mocchiut 1.35 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4301 mocchiut 1.16 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4302     };
4303     drun++;
4304     Row = pResult->Next();
4305     };
4306     };
4307     //
4308     if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);
4309     //
4310     //
4311     //
4312     drun = 0;
4313     //
4314     myquery.str("");
4315     myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4316     //
4317     pResult = conn->Query(myquery.str().c_str());
4318     //
4319     Row = pResult->Next();
4320     //
4321     if ( !Row ){
4322     if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4323     } else {
4324     if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");
4325     while ( Row ){
4326 mocchiut 1.35 if ( IsDebug() ) printf(" del run idtrash %u \n",(UInt_t)atoll(Row->GetField(0)));
4327 mocchiut 1.16 myquery.str("");
4328     myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";
4329     conn->Query(myquery.str().c_str());
4330     drun++;
4331     Row = pResult->Next();
4332     };
4333     };
4334     //
4335 mocchiut 1.35 if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_TRASH table \n",drun);
4336 mocchiut 1.16 //
4337     //
4338     //
4339     drun = 0;
4340     //
4341     myquery.str("");
4342     myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4343     //
4344     pResult = conn->Query(myquery.str().c_str());
4345     //
4346     Row = pResult->Next();
4347     //
4348     if ( !Row ){
4349     if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4350     } else {
4351     if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");
4352     while ( Row ){
4353 mocchiut 1.35 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4354 mocchiut 1.16 myquery.str("");
4355     myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4356     conn->Query(myquery.str().c_str());
4357     drun++;
4358     Row = pResult->Next();
4359     };
4360     };
4361     //
4362 mocchiut 1.35 if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_FRAGMENTS table \n",drun);
4363 mocchiut 1.16 //
4364     //
4365     //
4366     delete delrun;
4367     //
4368     };
4369    
4370    
4371     /**
4372     *
4373     * Rearrange calibration tables
4374     *
4375     **/
4376     void PamelaDBOperations::RemoveFILES(){
4377     stringstream myquery;
4378     //
4379     myquery.str("");
4380     myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4381     //
4382     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4383     //
4384     conn->Query(myquery.str().c_str());
4385     //
4386     };
4387    
4388     /**
4389     *
4390     * Rearrange calibration tables
4391     *
4392     **/
4393     void PamelaDBOperations::RemoveCALIBS(){
4394     TSQLResult *pResult;
4395     TSQLRow *Row;
4396     stringstream myquery;
4397     //
4398     //
4399     // Calorimeter
4400     //
4401     for (Int_t section = 0; section < 4; section++){
4402     myquery.str("");
4403     myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4404     myquery << " SECTION=" << section << ";";
4405     //
4406     pResult = conn->Query(myquery.str().c_str());
4407     //
4408     Row = pResult->Next();
4409     if( !Row->GetField(0) || !Row->GetField(1) ){
4410     //
4411     if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4412     //
4413     } else {
4414     //
4415     myquery.str("");
4416     myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4417     myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4418     myquery << " SECTION=" << section << ";";
4419     //
4420     pResult = conn->Query(myquery.str().c_str());
4421     //
4422     if( !pResult ){
4423     //
4424     if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4425     //
4426     throw -4;
4427     //
4428     };
4429     //
4430     };
4431     };
4432     myquery.str("");
4433     myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4434     //
4435     pResult = conn->Query(myquery.str().c_str());
4436     //
4437     if( !pResult ){
4438     //
4439     if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4440     //
4441     throw -4;
4442     //
4443     };
4444     //
4445     // Tracker
4446     //
4447     myquery.str("");
4448     myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4449     //
4450     pResult = conn->Query(myquery.str().c_str());
4451     //
4452     Row = pResult->Next();
4453     if( !Row->GetField(0) || !Row->GetField(1) ){
4454     //
4455     if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4456     //
4457     } else {
4458     //
4459     myquery.str("");
4460     myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4461     myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4462     //
4463     pResult = conn->Query(myquery.str().c_str());
4464     //
4465     if( !pResult ){
4466     //
4467     if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4468     //
4469     throw -4;
4470     //
4471     };
4472     //
4473     myquery.str("");
4474     myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4475     //
4476     pResult = conn->Query(myquery.str().c_str());
4477     //
4478     if( !pResult ){
4479     //
4480     if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4481     //
4482     throw -4;
4483     //
4484     };
4485     };
4486     //
4487     //
4488     // S4
4489     //
4490     myquery.str("");
4491     myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4492     //
4493     pResult = conn->Query(myquery.str().c_str());
4494     //
4495     Row = pResult->Next();
4496     if( !Row->GetField(0) || !Row->GetField(1) ){
4497     //
4498     if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4499     //
4500     } else {
4501     //
4502     myquery.str("");
4503     myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4504     myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4505     //
4506     pResult = conn->Query(myquery.str().c_str());
4507     //
4508     if( !pResult ){
4509     //
4510     if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4511     //
4512     throw -4;
4513     //
4514     };
4515     //
4516     myquery.str("");
4517     myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4518     //
4519     pResult = conn->Query(myquery.str().c_str());
4520     //
4521     if( !pResult ){
4522     //
4523     if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4524     //
4525     throw -4;
4526     //
4527     };
4528     //
4529     };
4530     };
4531 mocchiut 1.24
4532 pam-fi 1.22 /**
4533     *
4534     * Rearrange calibration tables
4535     *
4536     **/
4537     UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
4538    
4539     Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
4540     UInt_t timeaftercalib=120000; //2000;
4541     // ----------
4542     // Check CRCs
4543     // ----------
4544     for(Int_t ipkt=0; ipkt<6; ipkt++){
4545     if( caltrk->crc_hcal[ipkt] )return 0; // :-(
4546     for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
4547     }
4548     // -----------------------
4549     // Check missing packets:
4550     // -----------------------
4551     // Readout order:
4552     // ------------------
4553     // DSP packet board
4554     // ------------------
4555     // 12 0 1
4556     // 10 1 1
4557     // 8 2 1
4558     // 4 3 1
4559     // 6 4 1
4560     // 2 5 1
4561     // ------------------
4562     // 11 0 2
4563     // 9 1 2
4564     // 7 2 2
4565     // 3 3 2
4566     // 5 4 2
4567     // 1 5 2
4568     // ------------------
4569     // -------------------------------------------------
4570     // Check if it is first or second calibration packet
4571     // -------------------------------------------------
4572     UInt_t build=0;
4573     TString classname = caltrk->GetName();
4574     UInt_t base=0;
4575     UInt_t mask=0;
4576     if(classname.Contains("CalibTrk1Event")){
4577     base=12;
4578     mask=0x03F000;
4579     }
4580     if(classname.Contains("CalibTrk2Event")){
4581     base=18;
4582     mask=0xFC0000;
4583     }
4584     // -------------------------------------------------
4585     // Count number of packets and set build variable
4586     // -------------------------------------------------
4587     Int_t npkts=0;
4588     for(Int_t ipkt=0; ipkt<6; ipkt++){
4589     if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
4590     npkts++;
4591     build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
4592     }
4593     }
4594     // if( npkts==6 )return 1; // :-)
4595    
4596     // cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
4597    
4598     // -----------------------------------------------
4599     // If missing packets: check the acq configuration
4600     // (some DSPs might be excluded from acquisition)
4601     // -----------------------------------------------
4602    
4603     // -----------------------------------------------
4604     // retrieve the first run header after calib
4605     // -----------------------------------------------
4606     PacketType *pctp;
4607     EventCounter *cod;
4608     cod = eh->GetCounter();
4609     Int_t irun = cod->Get(pctp->RunHeader);
4610     TTree *rh=(TTree*)file->Get("RunHeader");
4611     if ( !rh || rh->IsZombie() ) throw -17;
4612     if( rh->GetEntries() == irun ){
4613     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1) -- cannot validate :-( "<<endl;
4614     return 0; // :-(
4615     }
4616    
4617     RunHeaderEvent *run = 0;
4618     EventHeader *hrun = 0;
4619     rh->SetBranchAddress("RunHeader", &run);
4620     rh->SetBranchAddress("Header", &hrun);
4621     rh->GetEntry(irun);
4622     // cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
4623    
4624     if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
4625     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
4626     return 0; // :-(
4627     }
4628    
4629     if( !run->RM_ACQ_AFTER_CALIB ){
4630     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0 -- cannot validate :-( "<<endl;
4631     return 0; // :-(
4632     }
4633    
4634     UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
4635     if( dtime > timeaftercalib ){
4636     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
4637     return 0; // :-(
4638     }
4639    
4640    
4641    
4642     if( (run->ACQ_BUILD_INFO & mask) != build ){
4643     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;
4644     return 0; // :-(
4645     }
4646     return 1; // :-)
4647    
4648     }
4649 mocchiut 1.24
4650     /**
4651     *
4652     * Check the DB (only for overlapping runs at the moment)
4653     *
4654     **/
4655     UInt_t PamelaDBOperations::Check(){
4656     //
4657     UInt_t test = 0;
4658     //
4659     UInt_t thisrht = 0;
4660     UInt_t thisrtt = 0;
4661     UInt_t thisid = 0;
4662     UInt_t prevrht = 0;
4663     UInt_t prevrtt = 0;
4664     UInt_t previd = 0;
4665     //
4666     UInt_t prevl0id = 0;
4667     UInt_t thisl0id = 0;
4668     //
4669     stringstream oss;
4670     TSQLResult *result = 0;
4671     TSQLRow *row = 0;
4672     TSQLResult *result2 = 0;
4673     TSQLRow *row2 = 0;
4674     oss.str("");
4675     oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN order by RUNHEADER_TIME asc;";
4676     // oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
4677     result = conn->Query(oss.str().c_str());
4678     //
4679     if ( !result ) throw -4;;
4680     //
4681     row = result->Next();
4682     //
4683     while ( row ){
4684     thisid = (UInt_t)atoll(row->GetField(0));
4685     thisl0id = (UInt_t)atoll(row->GetField(1));
4686     thisrht = (UInt_t)atoll(row->GetField(2));
4687     thisrtt = (UInt_t)atoll(row->GetField(3));
4688     //
4689     // if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
4690     // if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
4691     if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
4692     if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4693 mocchiut 1.35 printf(" CHECK n.1 TIME SCREW of %u s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
4694 mocchiut 1.24 TString prevf = "";
4695     TString thisf = "";
4696     oss.str("");
4697     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4698     result2 = conn->Query(oss.str().c_str());
4699     if ( !result2 ) throw -4;;
4700     row2 = result2->Next();
4701     prevf = (TString)row2->GetField(0);
4702     oss.str("");
4703     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4704     result2 = conn->Query(oss.str().c_str());
4705     if ( !result2 ) throw -4;;
4706     row2 = result2->Next();
4707     thisf = (TString)row2->GetField(0);
4708     if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4709     test = 1;
4710     };
4711     //
4712     if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
4713     if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4714 mocchiut 1.35 printf(" CHECK n.2 TIME SCREW of %u s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
4715 mocchiut 1.24 TString prevf = "";
4716     TString thisf = "";
4717     oss.str("");
4718     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4719     result2 = conn->Query(oss.str().c_str());
4720     if ( !result2 ) throw -4;;
4721     row2 = result2->Next();
4722     prevf = (TString)row2->GetField(0);
4723     oss.str("");
4724     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4725     result2 = conn->Query(oss.str().c_str());
4726     if ( !result2 ) throw -4;;
4727     row2 = result2->Next();
4728     thisf = (TString)row2->GetField(0);
4729     if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4730     test = 1;
4731     };
4732     //
4733     if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
4734     if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4735 mocchiut 1.35 printf(" CHECK n.3 TIME SCREW of %u s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
4736 mocchiut 1.24 TString prevf = "";
4737     TString thisf = "";
4738     oss.str("");
4739     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4740     result2 = conn->Query(oss.str().c_str());
4741     if ( !result2 ) throw -4;;
4742     row2 = result2->Next();
4743     prevf = (TString)row2->GetField(0);
4744     oss.str("");
4745     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4746     result2 = conn->Query(oss.str().c_str());
4747     if ( !result2 ) throw -4;;
4748     row2 = result2->Next();
4749     thisf = (TString)row2->GetField(0);
4750     if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4751     test = 1;
4752     };
4753    
4754     //
4755     prevrht = thisrht;
4756     prevrtt = thisrtt;
4757     previd = thisid;
4758     prevl0id = thisl0id;
4759     row = result->Next();
4760     };
4761     //
4762     return(test);
4763     //
4764     };

  ViewVC Help
Powered by ViewVC 1.1.23