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

Annotation of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34 - (hide annotations) (download)
Mon Apr 30 08:52:19 2007 UTC (17 years, 7 months ago) by mocchiut
Branch: MAIN
Changes since 1.33: +19 -17 lines
Obt bug fixed, timesync bug fixed

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

  ViewVC Help
Powered by ViewVC 1.1.23