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

Annotation of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (hide annotations) (download)
Tue Sep 12 08:43:50 2006 UTC (18 years, 2 months ago) by mocchiut
Branch: MAIN
Changes since 1.9: +1 -2 lines
small bug fixed

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

  ViewVC Help
Powered by ViewVC 1.1.23