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

Annotation of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.22 - (hide annotations) (download)
Wed Nov 29 18:12:02 2006 UTC (18 years ago) by pam-fi
Branch: MAIN
Changes since 1.21: +128 -4 lines
tracker-calibration validation modiefied

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

  ViewVC Help
Powered by ViewVC 1.1.23