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

Annotation of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (hide annotations) (download)
Wed Sep 13 08:34:01 2006 UTC (18 years, 2 months ago) by mocchiut
Branch: MAIN
Changes since 1.10: +38 -22 lines
Small bug in timesync determination 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 mocchiut 1.11 if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
935     //
936 mocchiut 1.1 if ( TSYNC && OBT ){
937     existsts = true;
938 mocchiut 1.2 goto eout;
939 mocchiut 1.1 };
940     //
941     };
942     };
943     };
944     if ( !existsts ) { // try with runheader and runtrailer
945     //
946 mocchiut 1.2 if ( IsDebug() ) printf(" No ts mcmd \n");
947     signal = 2;
948     //
949 mocchiut 1.1 TTree *rh=(TTree*)file->Get("RunHeader");
950     if ( !rh || rh->IsZombie() ) throw -17;
951     TTree *rt=(TTree*)file->Get("RunTrailer");
952     if ( !rt || rt->IsZombie() ) throw -18;
953     //
954     rh->SetBranchAddress("RunHeader", &runh);
955     //
956     rt->SetBranchAddress("RunTrailer", &runt);
957     //
958 mocchiut 1.11 Int_t nrhev = rh->GetEntries();
959     Int_t nrtev = rt->GetEntries();
960     if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev);
961     //
962     if ( nrhev > 0 ){
963     for (Int_t i=0; i<nrhev; i++){
964     //
965     rh->GetEntry(i);
966     //
967     TSYNC = runh->LAST_TIME_SYNC_INFO;
968     OBT = runh->OBT_TIME_SYNC * 1000;
969     //
970     TYPE = 20;
971     //
972     if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
973     //
974     if ( TSYNC && OBT ){
975     existsts = true;
976     goto eout;
977     };
978 mocchiut 1.1 };
979     //
980     };
981 mocchiut 1.11 if ( nrtev > 0 ){
982 mocchiut 1.2 //
983     if ( IsDebug() ) printf(" No runheader \n");
984     signal = 6;
985     //
986 mocchiut 1.11 for (Int_t i=0; i<nrtev; i++){
987     //
988     rt->GetEntry(i);
989     //
990     TSYNC = runt->LAST_TYME_SYNC_INFO;
991     OBT = runt->OBT_TYME_SYNC * 1000;
992     //
993     TYPE = 21;
994     //
995     if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
996     //
997     if ( TSYNC && OBT ){
998     existsts = true;
999     goto eout;
1000     };
1001 mocchiut 1.1 };
1002     //
1003 mocchiut 1.2 } else {
1004     if ( IsDebug() ) printf(" No runheader \n");
1005 mocchiut 1.1 };
1006     };
1007     //
1008     if ( !existsts ){ // try with inclination mcmd
1009 mocchiut 1.2 //
1010     if ( IsDebug() ) printf(" No runtrailer \n");
1011     signal = 14;
1012     //
1013 mocchiut 1.1 Double_t timesync = 0.;
1014     for (UInt_t i=0; i<nevent;i++){
1015     //
1016     T->GetEntry(i);
1017     //
1018     recEntries = mc->Records->GetEntries();
1019     // //
1020     for (UInt_t j = 0; j < recEntries; j++){
1021     mcrc = (pamela::McmdRecord*)mc->Records->At(j);
1022     mcmddata = mcrc->McmdData;
1023     //
1024     if (mcrc->ID1 == 0xE2){ // mcmd inclination
1025     timesync = 0.;
1026     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);
1027     //
1028     if ( timesync > maximum2){
1029     maximum2 = timesync;
1030     OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
1031     };
1032     };
1033     //
1034     };
1035     };
1036     if ( maximum2 > numeric_limits<Double_t>::min() ){
1037     TSYNC = (UInt_t)(maximum2 + 0.5);
1038     TYPE = 666;
1039     if ( TSYNC && OBT ){
1040     existsts = true;
1041 mocchiut 1.2 goto eout;
1042 mocchiut 1.1 };
1043     };
1044     };
1045     //
1046     if ( !existsts && obt0 ){ // insert timesync by hand
1047 mocchiut 1.2 //
1048     if ( IsDebug() ) printf(" No incl mcmd \n");
1049     signal = 30;
1050     //
1051 mocchiut 1.1 OBT = obt0;
1052     TSYNC = tsync;
1053     TYPE = 999;
1054     existsts = true;
1055 mocchiut 1.2 goto eout;
1056 mocchiut 1.1 };
1057     //
1058 mocchiut 1.2 eout:
1059 mocchiut 1.1 //
1060     if ( !existsts ) throw -3;
1061     //
1062     oss.str("");
1063     oss << "INSERT INTO GL_TIMESYNC (ID_RAW,TYPE,OBT0,TIMESYNC) VALUES ('"
1064     << this->GetID_RAW() << "','"//224'"
1065     << dec << (UInt_t)TYPE << "','"
1066     << dec << (UInt_t)OBT << "','"
1067     << dec << (UInt_t)TSYNC << "');";
1068     conn->Query(oss.str().c_str());
1069     if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());
1070     //
1071     toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1072     //
1073     delete result;
1074 mocchiut 1.2 return(signal);
1075 mocchiut 1.1 }
1076    
1077     /**
1078     * Insert all the new rows into GL_ROOT.
1079     * The raw file indicates in the parameters should be already been stored in the database.
1080     */
1081     Int_t PamelaDBOperations::insertPamelaRootFile(){
1082     stringstream oss;
1083     TSQLResult *result = 0;
1084     TSQLRow *row = 0;
1085     UInt_t idtimesync = 0;
1086     //
1087     oss.str("");
1088     oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1089     << " LEFT JOIN GL_ROOT "
1090     << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1091     << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1092     << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1093     result = conn->Query(oss.str().c_str());
1094     //
1095     if ( !result ) throw -12;
1096     //
1097     row = result->Next();
1098     //
1099     if ( !row ) throw -10;
1100     if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){
1101     idroot = (UInt_t)atoll(row->GetField(2));
1102     return(1);
1103     };
1104     //
1105     // determine which timesync has to be used
1106     //
1107     oss.str("");
1108     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;";
1109     result = conn->Query(oss.str().c_str());
1110     //
1111     if ( !result ) throw -3;
1112     //
1113     row = result->Next();
1114     //
1115     if ( !row ) throw -3;
1116     idtimesync = (UInt_t)atoll(row->GetField(0));
1117     //
1118     oss.str("");
1119     oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1120     << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";
1121     //
1122     if (conn->Query(oss.str().c_str()) == 0) throw -4;
1123     //
1124     delete result;
1125     //
1126     oss.str("");
1127     oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";
1128     //
1129     result = conn->Query(oss.str().c_str());
1130     if ( !result ) throw -12;
1131     row = result->Next();
1132     this->SetID_ROOT((UInt_t)atoll(row->GetField(0)));
1133     //
1134     delete result;
1135     //
1136     return(0);
1137     }
1138    
1139     /**
1140     * Assign the BOOT_NUMBER to the raw file.
1141     */
1142     Int_t PamelaDBOperations::assignBOOT_NUMBER(){
1143     stringstream oss;
1144     TSQLResult *result = 0;
1145     TSQLRow *row = 0;
1146     oss.str("");
1147     oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1148     << " PATH = '" << this->GetRawPath().Data() << "' AND "
1149     << " NAME = '" << this->GetRawFile().Data() << "' ";
1150     result = conn->Query(oss.str().c_str());
1151     //
1152 mocchiut 1.2 if ( !result ) throw -4;;
1153 mocchiut 1.1 row = result->Next();
1154     if ( !row ) return(16);
1155     if ( row->GetField(1) ){
1156     this->SetBOOTnumber((UInt_t)atoll(row->GetField(1)));
1157     return(1);
1158     };
1159 mocchiut 1.2 if ( !row->GetField(0) ) throw -26;
1160 mocchiut 1.1 //
1161     UInt_t idRaw = (UInt_t)atoll(row->GetField(0));
1162     //
1163     //
1164     //
1165     TTree *trDumpEv = 0;
1166     trDumpEv = (TTree*)file->Get("VarDump");
1167     if ( !trDumpEv || trDumpEv->IsZombie() ) throw -20;
1168     //
1169     VarDumpEvent *vde = 0;
1170     VarDumpRecord *vdr = 0;
1171     //
1172     trDumpEv->SetBranchAddress("VarDump", &vde);
1173 mocchiut 1.2 if ( trDumpEv->GetEntries() > 0 ){
1174     Bool_t found = false;
1175     for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1176     trDumpEv->GetEntry(i);
1177     vde->Records->GetEntries();
1178     if ( vde->Records->GetEntries()>0 ){
1179     found = true;
1180     goto fill;
1181     };
1182     };
1183     fill:
1184     if ( found ){
1185     //
1186 mocchiut 1.1 vdr = (VarDumpRecord*)vde->Records->At(6);
1187 mocchiut 1.2 //
1188 mocchiut 1.1 this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1189 mocchiut 1.2 //
1190     } else {
1191     if ( !this->GetBOOTnumber() ) return(4);
1192 mocchiut 1.1 };
1193     } else {
1194     if ( !this->GetBOOTnumber() ) return(2);
1195     };
1196     //
1197     oss.str("");
1198     oss << " UPDATE GL_RAW "
1199     << " SET GL_RAW.BOOT_NUMBER = '" << dec << this->GetBOOTnumber() << "'"
1200     << " WHERE GL_RAW.ID = '" << idRaw << "'";
1201     conn->Query(oss.str().c_str());
1202     //
1203     delete result;
1204     return(0);
1205     };
1206    
1207     /**
1208     * Scan runtrailer packet, fill the GL_RUN table and
1209     * check for missing and truncated runs
1210     */
1211     Int_t PamelaDBOperations::insertPamelaRUN(){
1212     Int_t signal = 0;
1213     //
1214     stringstream oss;
1215     oss.str("");
1216     //
1217     signal = this->SetUpperLimits();
1218     //
1219     // loop on runheader and runtrailer events
1220     //
1221     TTree *rh=(TTree*)file->Get("RunHeader");
1222     if ( !rh || rh->IsZombie() ) throw -17;
1223     TTree *rt=(TTree*)file->Get("RunTrailer");
1224     if ( !rt || rt->IsZombie() ) throw -18;
1225     //
1226     PacketType *pctp=0;
1227     EventCounter *cod=0;
1228     //
1229     rh->SetBranchAddress("RunHeader", &runh);
1230     rh->SetBranchAddress("Header", &ehh);
1231     //
1232     rt->SetBranchAddress("RunTrailer", &runt);
1233     rt->SetBranchAddress("Header", &eht);
1234     //
1235     UInt_t obtt = 0;
1236     UInt_t obth = 0;
1237     UInt_t pktt = 0;
1238     UInt_t pkth = 0;
1239     Int_t pth = -1;
1240     Int_t ptht = -1;
1241     Int_t evbeft = 0;
1242     Int_t evbefh = 0;
1243     //
1244     // no runtrailers in the file!
1245     //
1246     if ( !rtev ){
1247     if ( !upperentry ){
1248     if ( IsDebug() ) printf(" No physics events nor runs in the file \n");
1249     throw -8;
1250     } else {
1251     this->HandleRunFragments(true,true,0,upperentry);
1252     };
1253     } else {
1254     //
1255     for (Int_t ptt=0; ptt<rtev; ptt++){
1256     //
1257     rt->GetEntry(ptt);
1258     pht = eht->GetPscuHeader();
1259     pktt = pht->GetCounter();
1260     obtt = pht->GetOrbitalTime();
1261     //
1262     cod = eht->GetCounter();
1263     ptht = cod->Get(pctp->RunHeader) - 1;
1264     evbeft = cod->Get(pctp->Physics);
1265     //
1266     if ( !ptt && !(ptht+1) ){
1267     //
1268     if ( IsDebug() ) printf(" Piece of run at the beginning of the file %i %i %i \n",ptht,pth,ptt);
1269     //
1270     this->HandleRunFragments(true,false,0,(evbeft-1));
1271     //
1272     //
1273     } else if ( pth == ptht ){
1274     //
1275     if ( IsDebug() ) printf(" Missing header %i %i %i\n",ptht,pth,ptt);
1276     //
1277 mocchiut 1.2 if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1278 mocchiut 1.1 rt->GetEntry(ptt-1);
1279     cod = eht->GetCounter();
1280     evbefh = cod->Get(pctp->Physics);
1281     rt->GetEntry(ptt);
1282     pht = eht->GetPscuHeader();
1283     //
1284     if ( IsDebug() ) printf(" Try to find the beginning of a run which has only the runtrailer %i %i %i \n",ptht,pth,ptt);
1285     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);
1286     //
1287     this->HandleMissingHoT(true,false,evbefh,evbeft-1);
1288     //
1289     } else {
1290     //
1291     if ( IsDebug() ) printf(" Could be a good run, we have a runheader followed by a runtrailer %i %i %i\n",ptht,pth,ptt);
1292     //
1293     rh->GetEntry(ptht);
1294     phh = ehh->GetPscuHeader();
1295     pkth = phh->GetCounter();
1296     obth = phh->GetOrbitalTime();
1297     cod = ehh->GetCounter();
1298     evbefh = cod->Get(pctp->Physics);
1299     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);
1300     //
1301     // handle this run
1302     //
1303     this->HandleRun();
1304     //
1305     //
1306     //
1307     if ( PKT(pkth)>PKT(pktfirst) && OBT(obth)>OBT(obtfirst) && !ptt ){
1308     //
1309     if ( IsDebug() ) printf(" Piece of run at the beginning of the file WITH NO RUNTRAILER \n");
1310     //
1311     this->HandleRunFragments(true,true,0,(evbefh-1));
1312     //
1313     };
1314     //
1315     //
1316     if ( (ptht - pth) > 1 ){
1317     //
1318     if ( IsDebug() ) printf(" Missing runtrailers! \n");
1319     if ( IsDebug() ) printf(" Attention there is a jump in the runheader counter %i %i %i \n",ptht,pth,ptt);
1320     // is not the consecutive header
1321     while ( pth != ptht ){
1322     //
1323     // treat the header(s) in the middle and then go to the next header, repeat until you reach the correct header.
1324     //
1325     pth++;
1326     //
1327     rh->GetEntry(pth+1);
1328     phh = ehh->GetPscuHeader();
1329     pktt = phh->GetCounter();
1330     obtt = phh->GetOrbitalTime();
1331     cod = ehh->GetCounter();
1332     evbeft = cod->Get(pctp->Physics);
1333     rh->GetEntry(pth);
1334     phh = ehh->GetPscuHeader();
1335     cod = ehh->GetCounter();
1336     pkth = phh->GetCounter();
1337     obth = phh->GetOrbitalTime();
1338     evbefh = cod->Get(pctp->Physics);
1339     //
1340     if ( IsDebug() ) printf(" Try to find the end of a run which has only the runheader %i %i %i \n",ptht,pth,ptt);
1341     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);
1342     //
1343     this->HandleMissingHoT(false,true,evbefh,evbeft-1);
1344     //
1345     };
1346     //
1347     } else if ( !(ptht - pth) ){
1348     //
1349     if ( IsDebug() ) printf(" Missing runheader! \n");
1350     if ( IsDebug() ) printf(" Attention! the runheader counter did not changed %i %i %i \n",ptht,pth,ptt);
1351     if ( IsDebug() ) printf(" The run should have already been handled by HandleRun() \n");
1352     //
1353     } else {
1354     //
1355     // go on with next header
1356     //
1357     pth = ptht;
1358     };
1359     //
1360     };
1361     //
1362     if ( ptt+1 == rtev){
1363     ptht++;
1364     if ( ptht < rhev ){
1365     rh->GetEntry(ptht);
1366     phh = ehh->GetPscuHeader();
1367     pkth = phh->GetCounter();
1368     obth = phh->GetOrbitalTime();
1369     cod = ehh->GetCounter();
1370     evbefh = cod->Get(pctp->Physics);
1371     if ( IsDebug() ) printf(" Piece of run at the end of file %i %i %i \n",pkth,obth,obtt);
1372     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i %i %i \n",ptht,pth,ptt);
1373     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i \n",rhev);
1374     //
1375     this->HandleRunFragments(false,true,evbefh,upperentry);
1376     } else {
1377     //
1378     // check if we have a fragment with no header
1379     //
1380     if ( (UInt_t)evbeft < upperentry-1 ){
1381     if ( IsDebug() ) printf(" Piece of run at the end of the file with NO RUNHEADER!\n");
1382     //
1383 mocchiut 1.2 if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1384 mocchiut 1.1 rt->GetEntry(ptt-1);
1385     cod = eht->GetCounter();
1386     evbefh = cod->Get(pctp->Physics);
1387     rt->GetEntry(ptt);
1388     pht = eht->GetPscuHeader();
1389     this->HandleRunFragments(true,true,evbefh,upperentry);
1390     };
1391     };
1392     };
1393     //
1394     };
1395     };
1396     //
1397     return(signal);
1398     };
1399    
1400     /**
1401     *
1402     * Check if the run has already been inserted
1403     *
1404     */
1405     Bool_t PamelaDBOperations::IsRunAlreadyInserted(){
1406     //
1407     TSQLResult *result = 0;
1408     TSQLRow *row = 0;
1409     //
1410     stringstream oss;
1411     oss.str("");
1412     //
1413     // the where clause is of the type: boot_number = _our_boot && (
1414     // ( runhead_time >= (_our_runhead_time-10) && runtrail_time <= (_our_runtrail_time+10) &&
1415     // ( runhead_obt >= _our_runheadobt || runhead_pkt >= _our_runheadpkt ) &&
1416     // ( runtrail_obt >= _our_runtrailobt || runtrail_pkt >= _our_runtrailpkt ) )
1417     // ||
1418     // ( runhead_time <= _our_runhead_time && runtrail_time >= _our_runtrail_time) &&
1419     // ( runhead_obt <= _our_runheadobt || runhead_pkt <= _our_runheadpkt ) &&
1420     // ( runtrail_obt <= _our_runtrailobt || runtrail_pkt <= _our_runtrailpkt ) )
1421     // )
1422     //
1423     oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
1424     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1425     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1426     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1427     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1428     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1429     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1430     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1431     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1432     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1433     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1434     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1435     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1436     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1437     //
1438     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1439     result = conn->Query(oss.str().c_str());
1440     //
1441     if ( !result ) throw -4;
1442     //
1443     row = result->Next();
1444     //
1445     if ( !row ){
1446     if ( IsDebug() ) printf(" The run is new \n");
1447     if ( IsDebug() ) printf(" -> fill the DB \n");
1448     return(false); // the file has not been inserted in the DB, go on.
1449     };
1450     //
1451     Bool_t signal = true;
1452     //
1453     while ( row != NULL ){
1454     if ( IsDebug() ) printf(" A run exists with runheader and runtrailer time and packets compatible with this one \n");
1455     //
1456     // the run has already been inserted
1457     //
1458     // return(true); //<<<<<<<<<<<<<<<<<<<<<<<< patch follows, uncomment here
1459     //
1460     // PATCH!
1461     // 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
1462     // while the old run doesn't have it 3) we have more events than the old run
1463     //
1464     if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1465     //
1466     if ( IsDebug() ) printf(" The new run has more events than the old one \n");
1467     oss.str("");
1468     oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1469     if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1470     conn->Query(oss.str().c_str());
1471     if ( signal ) signal = false;
1472     goto gonext;
1473     //
1474     } else if ( glrun->GetNEVENTS() < (UInt_t)atoll(row->GetField(1)) ){
1475     if ( IsDebug() ) printf(" The new run has less events than the old one \n");
1476     if ( IsDebug() ) printf(" The run is already inserted \n");
1477     goto gonext;
1478     };
1479     //
1480     if ( glrun->GetTRK_CALIB() && !(UInt_t)atoll(row->GetField(2)) ){
1481     //
1482     if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");
1483     //
1484     oss.str("");
1485     oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1486     if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1487     conn->Query(oss.str().c_str());
1488     //
1489     if ( signal ) signal = false;
1490     goto gonext;
1491     } else if ( !glrun->GetTRK_CALIB() && (UInt_t)atoll(row->GetField(2)) ){
1492     if ( IsDebug() ) printf(" The new run has the same number of events but miss the runheader the old has the runheader \n");
1493     if ( IsDebug() ) printf(" The run is already inserted \n");
1494     goto gonext;
1495     };
1496     //
1497     if ( glrun->GetPKT_COUNTER() && !(UInt_t)atoll(row->GetField(3)) ){
1498     //
1499     if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");
1500     //
1501     oss.str("");
1502     oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1503     if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1504     conn->Query(oss.str().c_str());
1505     if ( signal ) signal = false;
1506     //
1507     };
1508     //
1509     gonext:
1510     // END PATCH!
1511     //
1512     row = result->Next();
1513     //
1514     };
1515     //
1516     delete result;
1517     //
1518     if ( signal && IsDebug() ) printf(" The run has already been inserted \n");
1519     return(signal);
1520     };
1521    
1522     /**
1523     * Handle runs which seems to be good ones.
1524     **/
1525     void PamelaDBOperations::HandleRun(){
1526     ULong64_t chkpkt = 0;
1527     ULong64_t pktt = (ULong64_t)PKT(pht->GetCounter());
1528     ULong64_t pkth = (ULong64_t)PKT(phh->GetCounter());
1529     //
1530     chkpkt = pkth + (ULong64_t)runt->PKT_COUNTER + 1ULL + 1ULL;
1531     //
1532     if ( labs(chkpkt-pktt)<2 ){
1533     //
1534     if ( IsDebug() ) printf(" check %llu pktt %llu \n",chkpkt,pktt);
1535     //
1536     // it must be a good run, fill the db
1537     //
1538     this->FillClass();
1539     //
1540     if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);
1541     } else {
1542     //
1543     if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt);
1544     if ( IsDebug() ) printf(" try to recover run(s) without runheader and runtrailer between runheader and runtrailer\n");
1545     //
1546     this->HandleSuspiciousRun();
1547     //
1548     };
1549     //
1550     //
1551     return;
1552     };
1553    
1554    
1555     /**
1556     * Handle run fragments at the beginning or at the end of the file
1557     **/
1558     void PamelaDBOperations::HandleRunFragments(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
1559     //
1560     UInt_t rhfirstev = firstev;
1561     UInt_t rtlastev = lastev;
1562     Bool_t found = false;
1563     //
1564     TSQLResult *result = 0;
1565     TSQLRow *row = 0;
1566     //
1567     stringstream oss;
1568     oss.str("");
1569     //
1570     // is the piece of run good (no other packets inside)?
1571     //
1572     if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
1573     //
1574     // if not, handle other pieces and continue with the first one
1575     //
1576     if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
1577     //
1578     };
1579     //
1580     // we have now the good first piece of a run, fill the glrun object
1581     //
1582     if ( rhfirstev != firstev && !mishead ) mishead = true;
1583     if ( rtlastev != lastev && !mistrail ) mistrail = true;
1584     //
1585     this->FillClass(mishead,mistrail,firstev,lastev);
1586     //
1587     if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
1588     //
1589     // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
1590     //
1591     if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
1592     // missing it no way we can found a piece in the frag table
1593     //
1594     oss.str("");
1595     oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1596     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1597     << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME()
1598     << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1599     //
1600     if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
1601     result = conn->Query(oss.str().c_str());
1602     //
1603     if ( !result ) throw -4;
1604     //
1605     row = result->Next();
1606     //
1607     if ( !row ){
1608     if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1609     found = false;
1610     } else {
1611     //
1612     found = false; // default value
1613     //
1614     if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
1615     //
1616     // if we have both runheader and runtrailer we can check with pkt_counter:
1617     //
1618     if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
1619     ULong64_t chkpkt = 0;
1620     ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
1621     ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
1622     //
1623     chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;
1624     //
1625     if ( labs(chkpkt-pktt)<2 ){
1626     //
1627     if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
1628     //
1629     found = true;
1630     //
1631     } else {
1632     //
1633     if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
1634     //
1635     found = false;
1636     //
1637     };
1638     };
1639     if ( !found ){
1640     //
1641     // 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
1642     //
1643     ULong64_t chkpkt1 = 0;
1644     ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
1645     ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
1646     chkpkt1 = labs(orunh1-dbrunt1);
1647     //
1648     ULong64_t chkpkt2 = 0;
1649     ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
1650     ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
1651     chkpkt2 = labs(orunh2-dbrunt2);
1652     //
1653     ULong64_t chkpkt3 = 0;
1654     ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
1655     ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
1656     chkpkt3 = labs(orunh3-dbrunt3);
1657     //
1658     if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
1659     // if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
1660     //
1661     if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
1662     //
1663     found = true;
1664     //
1665     } else {
1666     //
1667     if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
1668     //
1669     found = false;
1670     //
1671     };
1672     };
1673     };
1674     //
1675     if ( found ){
1676     //
1677     // 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
1678     //
1679     if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
1680     //
1681     GL_RUN *glrun1 = new GL_RUN();
1682     //
1683     UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
1684     //
1685     oss.str("");
1686     oss << " ID="<<row->GetField(0)<<";";
1687     //
1688     glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
1689     //
1690     // merge infos
1691     //
1692     UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
1693     ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
1694     UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
1695     ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
1696     if ( IsDebug() ) printf(" Check overlapping events: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
1697     TTree *T= 0;
1698     T = (TTree*)file->Get("Physics");
1699     if ( !T || T->IsZombie() ) throw -16;
1700     EventHeader *eh = 0;
1701     PscuHeader *ph = 0;
1702     T->SetBranchAddress("Header", &eh);
1703     while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
1704     T->GetEntry(firstev);
1705     ph = eh->GetPscuHeader();
1706     bpkt = PKT(ph->GetCounter());
1707     bobt = OBT(ph->GetOrbitalTime());
1708     firstev++;
1709     };
1710     if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
1711     //
1712     glrun1->SetID(0);
1713     glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
1714     glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
1715     glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
1716     glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
1717     glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
1718     //
1719     glrun->SetEV_FROM(firstev);
1720     glrun->SetNEVENTS(lastev-firstev+1);
1721     glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
1722     glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
1723     glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
1724     glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
1725     glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
1726     glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
1727     glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
1728     glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
1729     glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
1730     glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
1731     glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());
1732     glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());
1733     glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());
1734     glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());
1735     glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());
1736     glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());
1737     //
1738     if ( !IsRunAlreadyInserted() ){
1739     //
1740     glrun->Fill_GL_RUN(conn);
1741     //
1742     // get id number
1743     //
1744     oss.str("");
1745     oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "
1746     << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "
1747     << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";
1748     //
1749     if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());
1750     //
1751     result = conn->Query(oss.str().c_str());
1752     if ( !result ) throw -4;
1753     row = result->Next();
1754     //
1755     UInt_t idrun0 = 0;
1756     if ( !row ){
1757     throw -10;
1758     } else {
1759     idrun0 = (UInt_t)atoll(row->GetField(0));
1760     };
1761     //
1762     glrun1->SetID_RUN_FRAG(idrun0);
1763     //
1764     glrun1->Fill_GL_RUN(conn);
1765     //
1766     oss.str("");
1767     oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";
1768     //
1769     if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());
1770     //
1771     result = conn->Query(oss.str().c_str());
1772     if ( !result ) throw -4;
1773     row = result->Next();
1774     //
1775     UInt_t idrun1 = 0;
1776     if ( !row ){
1777     throw -10;
1778     } else {
1779     idrun1 = (UInt_t)atoll(row->GetField(0));
1780     };
1781     //
1782     oss.str("");
1783     oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";
1784     //
1785     result = conn->Query(oss.str().c_str());
1786     if ( !result ) throw -4;
1787     //
1788     };
1789     //
1790     delete glrun1;
1791     //
1792     // delete old entry in fragment table
1793     //
1794     oss.str("");
1795     //
1796     oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";
1797     //
1798     if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str());
1799     result = conn->Query(oss.str().c_str());
1800     if ( !result ) throw -4;
1801     //
1802     return;
1803     //
1804     };
1805     //
1806     };
1807     //
1808     if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
1809     // missing it no way we can found a piece in the frag table
1810     //
1811     oss.str("");
1812     oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1813     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1814     << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME()
1815     << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
1816     //
1817     if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
1818     result = conn->Query(oss.str().c_str());
1819     //
1820     if ( !result ) throw -4;
1821     //
1822     row = result->Next();
1823     //
1824     if ( !row ){
1825     if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1826     found = false;
1827     } else {
1828     //
1829     found = false; // default value
1830     //
1831     if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
1832     //
1833     // if we have both runheader and runtrailer we can check with pkt_counter:
1834     //
1835     if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){
1836     ULong64_t chkpkt = 0;
1837     ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
1838     ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
1839     //
1840     chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;
1841     //
1842     if ( labs(chkpkt-pktt)<2 ){
1843     //
1844     if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
1845     //
1846     found = true;
1847     //
1848     } else {
1849     //
1850     if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
1851     //
1852     found = false;
1853     //
1854     };
1855     };
1856     if ( !found ){
1857     //
1858     // 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
1859     //
1860     ULong64_t chkpkt1 = 0;
1861     ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
1862     ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
1863     chkpkt1 = labs(orunh1-dbrunt1);
1864     //
1865     ULong64_t chkpkt2 = 0;
1866     ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT());
1867     ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
1868     chkpkt2 = labs(orunh2-dbrunt2);
1869     //
1870     ULong64_t chkpkt3 = 0;
1871     ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME());
1872     ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
1873     chkpkt3 = labs(orunh3-dbrunt3);
1874     //
1875     if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
1876     //
1877     if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
1878     //
1879     found = true;
1880     //
1881     } else {
1882     //
1883     if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
1884     //
1885     found = false;
1886     //
1887     };
1888     };
1889     };
1890     //
1891     if ( found ){
1892     //
1893     // 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
1894     //
1895     if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
1896     //
1897     GL_RUN *glrun1 = new GL_RUN();
1898     //
1899     UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
1900     //
1901     oss.str("");
1902     oss << " ID="<<row->GetField(0)<<";";
1903     //
1904     glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runtrailer infos
1905     //
1906     // merge infos
1907     //
1908     UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT());
1909     ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
1910     UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
1911     ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
1912     if ( IsDebug() ) printf(" Check overlapping events: %i %i %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
1913     TTree *T= 0;
1914     T = (TTree*)file->Get("Physics");
1915     if ( !T || T->IsZombie() ) throw -16;
1916     EventHeader *eh = 0;
1917     PscuHeader *ph = 0;
1918     T->SetBranchAddress("Header", &eh);
1919     while ( apkt > bpkt && aobt > bobt && lastev > 0 ){
1920     T->GetEntry(lastev);
1921     ph = eh->GetPscuHeader();
1922     apkt = PKT(ph->GetCounter());
1923     aobt = OBT(ph->GetOrbitalTime());
1924     lastev--;
1925     };
1926     if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
1927     //
1928     glrun->SetEV_TO(lastev);
1929     glrun->SetNEVENTS(lastev-firstev+1);
1930     glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());
1931     glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER());
1932     glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME());
1933     glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
1934     glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
1935     //
1936     glrun1->SetID(0);
1937     glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
1938     glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
1939     glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
1940     glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP());
1941     glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE());
1942     glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE());
1943     glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A());
1944     glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B());
1945     glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO());
1946     glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO());
1947     glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB());
1948     glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE());
1949     glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED());
1950     glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK());
1951     glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC());
1952     glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC());
1953     //
1954     if ( !IsRunAlreadyInserted() ){
1955     //
1956     glrun->Fill_GL_RUN(conn);
1957     //
1958     // get id number
1959     //
1960     oss.str("");
1961     oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "
1962     << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "
1963     << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";
1964     //
1965     if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());
1966     //
1967     result = conn->Query(oss.str().c_str());
1968     if ( !result ) throw -4;
1969     row = result->Next();
1970     //
1971     UInt_t idrun0 = 0;
1972     if ( !row ){
1973     throw -10;
1974     } else {
1975     idrun0 = (UInt_t)atoll(row->GetField(0));
1976     };
1977     //
1978     glrun1->SetID_RUN_FRAG(idrun0);
1979     //
1980     glrun1->Fill_GL_RUN(conn);
1981     //
1982     oss.str("");
1983     oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";
1984     //
1985     if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());
1986     //
1987     result = conn->Query(oss.str().c_str());
1988     if ( !result ) throw -4;
1989     row = result->Next();
1990     //
1991     UInt_t idrun1 = 0;
1992     if ( !row ){
1993     throw -10;
1994     } else {
1995     idrun1 = (UInt_t)atoll(row->GetField(0));
1996     };
1997     //
1998     oss.str("");
1999     oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";
2000     //
2001     result = conn->Query(oss.str().c_str());
2002     if ( !result ) throw -4;
2003     //
2004     };
2005     //
2006     delete glrun1;
2007     //
2008     // delete old entry in fragment table
2009     //
2010     oss.str("");
2011     //
2012     oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";
2013     //
2014     if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str());
2015     result = conn->Query(oss.str().c_str());
2016     if ( !result ) throw -4;
2017     //
2018     //
2019     return;
2020     //
2021     };
2022     //
2023     };
2024     //
2025     if ( !found ){
2026     //
2027     if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
2028     //
2029     // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
2030     //
2031     oss.str("");
2032     oss << " SELECT ID FROM GL_RUN WHERE "
2033     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2034     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2035     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2036     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2037     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2038     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2039     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2040     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2041     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2042     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2043     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2044     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2045     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2046     //
2047     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
2048     result = conn->Query(oss.str().c_str());
2049     //
2050     if ( !result ) throw -4;
2051     //
2052     row = result->Next();
2053     //
2054     if ( !row ){
2055     //
2056     oss.str("");
2057     oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
2058     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2059     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2060     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2061     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2062     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2063     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2064     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2065     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2066     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2067     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2068     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2069     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2070     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2071     //
2072     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
2073     result = conn->Query(oss.str().c_str());
2074     //
2075     if ( !result ) throw -4;
2076     //
2077     row = result->Next();
2078     //
2079     if ( !row ){
2080     //
2081     // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
2082     //
2083     if ( IsDebug() ) printf(" The run is new \n");
2084     if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
2085     //
2086     glrun->Fill_GL_RUN_FRAGMENTS(conn);
2087     //
2088     } else {
2089     if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
2090     };
2091     } else {
2092     if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
2093     };
2094     };
2095     //
2096     return;
2097     };
2098    
2099    
2100     /**
2101     * Handle run without header or trailer
2102     **/
2103     void PamelaDBOperations::HandleMissingHoT(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
2104     //
2105     //
2106     // is the piece of run good (no other packets inside)?
2107     //
2108     if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
2109     //
2110     // if not, handle other pieces and continue with the first one
2111     //
2112     if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
2113     //
2114     } else {
2115     //
2116     this->FillClass(mishead,mistrail,firstev,lastev);
2117     //
2118     if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);
2119     //
2120     };
2121     //
2122     return;
2123     };
2124    
2125     /**
2126     *
2127     * check if we have non-physics packets inside the run
2128     *
2129     */
2130     Bool_t PamelaDBOperations::IsRunConsistent(Bool_t mishead, Bool_t mistrail, UInt_t &firstev, UInt_t &lastev){
2131     //
2132     EventCounter *code=0;
2133     //
2134     UInt_t nevent = 0;
2135     UInt_t checkfirst = 0;
2136     UInt_t checklast = 0;
2137     UInt_t firstentry = 0;
2138     UInt_t lastentry = 0;
2139     UInt_t firstTime = 0;
2140     UInt_t lastTime = 0;
2141     UInt_t firstPkt = 0;
2142     UInt_t lastPkt = 0;
2143     UInt_t firstObt = 0;
2144     UInt_t lastObt = 0;
2145     //
2146     pcksList packetsNames;
2147     pcksList::iterator Iter;
2148     getPacketsNames(packetsNames);
2149     //
2150     TTree *T= 0;
2151     T =(TTree*)file->Get("Physics");
2152     if ( !T || T->IsZombie() ) throw -16;
2153     EventHeader *eh = 0;
2154     PscuHeader *ph = 0;
2155     T->SetBranchAddress("Header", &eh);
2156     nevent = T->GetEntries();
2157     //
2158     //
2159     if ( firstev == lastev+1 ) { // no events inside the run!
2160     if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2161     // return true is correct
2162     return(true);
2163     //
2164     } else {
2165     //
2166     T->GetEntry(firstev);
2167     code = eh->GetCounter();
2168     checkfirst = 0;
2169     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2170     if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
2171     };
2172     if ( IsDebug() ) printf(" Check first is %i firstev is %i\n",checkfirst,firstev);
2173     //
2174     T->GetEntry(lastev);
2175     code = eh->GetCounter();
2176     checklast = 0;
2177     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2178     if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
2179     };
2180     if ( IsDebug() ) printf(" Check last is %i lastev is %i\n",checklast,lastev);
2181     //
2182     if ( checkfirst == checklast ){
2183     //
2184     if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2185     //
2186     return(true);
2187     //
2188     } else {
2189     //
2190     if ( IsDebug() ) printf(" There are no-phyics packets inside the run!\n");
2191     //
2192     // HERE WE MUST HANDLE THAT RUNS AND GO BACK
2193     //
2194     if ( IsDebug() ) printf(" Never seen this case, try to handle it anyway, it was throw -95\n");
2195     //
2196     Bool_t emptyruns = false;
2197     UInt_t check = 0;
2198     UInt_t lastevtemp = lastev;
2199     UInt_t firstevno = firstev;
2200     //
2201     for (UInt_t i=firstev; i<=lastev; i++){
2202     //
2203     T->GetEntry(i);
2204     code = eh->GetCounter();
2205     //
2206     check = 0;
2207     //
2208     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2209     if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
2210     };
2211     //
2212     if ( checkfirst < check || i == lastev ){
2213     //
2214     firstentry = firstevno;
2215     //
2216     if ( checkfirst < check ){
2217     lastentry = i-1;
2218     } else {
2219     lastentry = i;
2220     };
2221     //
2222     if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2223     //
2224     glrun->SetEV_FROM(firstentry);
2225     glrun->SetEV_TO(lastentry);
2226     if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
2227     if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
2228     lastentry--;
2229     };
2230     glrun->SetNEVENTS(lastentry-firstentry+1);
2231     //
2232     glrun->Set_GL_RUNH0();
2233     glrun->Set_GL_RUNT0();
2234     //
2235     glrun->SetLAST_TIMESYNC(0);
2236     glrun->SetOBT_TIMESYNC(0);
2237     //
2238     T->GetEntry(firstentry);
2239     ph = eh->GetPscuHeader();
2240     firstObt = ph->GetOrbitalTime();
2241     firstTime = this->GetAbsTime(firstObt);
2242     firstPkt = ph->GetCounter();
2243     //
2244     T->GetEntry(lastentry);
2245     ph = eh->GetPscuHeader();
2246     lastObt = ph->GetOrbitalTime();
2247     lastTime = this->GetAbsTime(lastObt);
2248     lastPkt = ph->GetCounter();
2249     //
2250     glrun->SetRUNHEADER_PKT(firstPkt);
2251     glrun->SetRUNTRAILER_PKT(lastPkt);
2252     //
2253     glrun->SetRUNHEADER_OBT(firstObt);
2254     glrun->SetRUNTRAILER_OBT(lastObt);
2255     //
2256     if ( firstev == firstentry && !emptyruns && !mishead ){
2257     glrun->Set_GL_RUNH(runh,phh);
2258     firstTime = this->GetAbsTime(phh->GetOrbitalTime());
2259     if ( IsDebug() ) printf(" We have the runheader \n");
2260     };
2261     if ( lastev == i && !mistrail ){
2262     glrun->Set_GL_RUNT(runt,pht);
2263     lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2264     if ( IsDebug() ) printf(" We have the runtrailer \n");
2265     };
2266     //
2267     if ( lastentry == (firstentry-2) ){ // no events in the run
2268     emptyruns = true;
2269     if ( IsDebug() ) printf(" No events in the run \n");
2270     lastTime = firstTime;
2271     if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
2272     lastObt = glrun->RUNHEADER_OBT;
2273     lastPkt = glrun->RUNHEADER_PKT;
2274     } else {
2275     lastObt = firstObt;
2276     lastPkt = firstPkt;
2277     };
2278     glrun->SetRUNTRAILER_PKT(lastPkt);
2279     glrun->SetRUNTRAILER_OBT(lastObt);
2280     lastentry++;
2281     };
2282     //
2283     this->SetCommonGLRUN(firstTime,lastTime);
2284     //
2285     if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);
2286     //
2287     firstevno = lastentry + 1;
2288     //
2289     checkfirst = check;
2290     //
2291     };
2292     //
2293     if ( check == checklast && i != lastev ){
2294     lastevtemp = i - 1;
2295     i = lastev - 1;
2296     };
2297     //
2298     };
2299     //
2300     lastev = lastevtemp;
2301     //
2302     return(false);
2303     //
2304     };
2305     };
2306     //
2307     return(false); // should never arrive here
2308     };
2309    
2310     /**
2311     *
2312     * 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
2313     * 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
2314     * looking for non-physics packets inside.
2315     *
2316     */
2317     void PamelaDBOperations::HandleSuspiciousRun(){
2318     //
2319     PacketType *pctp=0;
2320     EventCounter *codt=0;
2321     EventCounter *codh=0;
2322     EventCounter *code=0;
2323     UInt_t firstev = 0;
2324     UInt_t lastev = 0;
2325     UInt_t nevent = 0;
2326     UInt_t checkfirst = 0;
2327     UInt_t checklast = 0;
2328     UInt_t firstentry = 0;
2329     UInt_t lastentry = 0;
2330     UInt_t firstTime = 0;
2331     UInt_t lastTime = 0;
2332     UInt_t firstPkt = 0;
2333     UInt_t lastPkt = 0;
2334     UInt_t firstObt = 0;
2335     UInt_t lastObt = 0;
2336     //
2337     pcksList packetsNames;
2338     pcksList::iterator Iter;
2339     getPacketsNames(packetsNames);
2340     //
2341     TTree *rh=0;
2342     rh = (TTree*)file->Get("RunHeader");
2343     if ( !rh || rh->IsZombie() ) throw -17;
2344     TTree *T=0;
2345     T =(TTree*)file->Get("Physics");
2346     if ( !T || T->IsZombie() ) throw -16;
2347     EventHeader *eh = 0;
2348     PscuHeader *ph = 0;
2349     T->SetBranchAddress("Header", &eh);
2350     nevent = T->GetEntries();
2351     //
2352     codt = eht->GetCounter();
2353     codh = ehh->GetCounter();
2354     firstev = codh->Get(pctp->Physics);
2355     lastev = codt->Get(pctp->Physics)-1;
2356     //
2357     if ( firstev == lastev+1 ) { // no events inside the run!
2358     if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2359     //
2360     this->FillClass();
2361     if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);
2362     //
2363     } else {
2364     //
2365     UInt_t nrunh = 0;
2366     UInt_t nrunh1 = 0;
2367     T->GetEntry(firstev);
2368     code = eh->GetCounter();
2369     checkfirst = 0;
2370     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2371     if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
2372     if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;
2373     };
2374     if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);
2375     //
2376     T->GetEntry(lastev);
2377     code = eh->GetCounter();
2378     checklast = 0;
2379     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2380     if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
2381     };
2382     if ( IsDebug() ) printf(" Check last is %i \n",checklast);
2383     //
2384     if ( checkfirst == checklast ){
2385     //
2386     if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2387     //
2388     this->FillClass();
2389     if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);
2390     //
2391     } else {
2392     //
2393     if ( IsDebug() ) printf(" There are no-physics packets inside the run, try to separate runs \n");
2394     //
2395     Bool_t emptyruns = false;
2396     UInt_t check = 0;
2397     UInt_t firstevno = firstev;
2398     //
2399     for (UInt_t i=firstev; i<=lastev; i++){
2400     //
2401     T->GetEntry(i);
2402     code = eh->GetCounter();
2403     //
2404     check = 0;
2405     //
2406     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2407     if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
2408     if ( !strcmp(*Iter,"RunHeader") ) nrunh++;
2409     };
2410     //
2411     if ( checkfirst < check || i == lastev ){
2412     //
2413     firstentry = firstevno;
2414     //
2415     if ( checkfirst < check ){
2416     lastentry = i-1;
2417     } else {
2418     lastentry = i;
2419     };
2420     //
2421     if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2422     //
2423     glrun->SetEV_FROM(firstentry);
2424     glrun->SetEV_TO(lastentry);
2425     if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
2426     if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
2427     lastentry--;
2428     };
2429     glrun->SetNEVENTS(lastentry-firstentry+1);
2430     //
2431     glrun->Set_GL_RUNH0();
2432     glrun->Set_GL_RUNT0();
2433     //
2434     glrun->SetLAST_TIMESYNC(0);
2435     glrun->SetOBT_TIMESYNC(0);
2436     //
2437     T->GetEntry(firstentry);
2438     ph = eh->GetPscuHeader();
2439     firstObt = ph->GetOrbitalTime();
2440     firstTime = this->GetAbsTime(firstObt);
2441     firstPkt = ph->GetCounter();
2442     //
2443     T->GetEntry(lastentry);
2444     ph = eh->GetPscuHeader();
2445     lastObt = ph->GetOrbitalTime();
2446     lastTime = this->GetAbsTime(lastObt);
2447     lastPkt = ph->GetCounter();
2448     //
2449     glrun->SetRUNHEADER_PKT(firstPkt);
2450     glrun->SetRUNTRAILER_PKT(lastPkt);
2451     //
2452     glrun->SetRUNHEADER_OBT(firstObt);
2453     glrun->SetRUNTRAILER_OBT(lastObt);
2454     //
2455     if ( (firstev == firstentry && !emptyruns) || nrunh == (nrunh1 + 1) ){
2456     rh->GetEntry(nrunh1-1);
2457     phh = ehh->GetPscuHeader();
2458     nrunh1++;
2459     glrun->Set_GL_RUNH(runh,phh);
2460     firstTime = this->GetAbsTime(phh->GetOrbitalTime());
2461     if ( IsDebug() ) printf(" We have the runheader \n");
2462     };
2463     if ( lastev == i && checkfirst == check ){
2464     glrun->Set_GL_RUNT(runt,pht);
2465     lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2466     if ( IsDebug() ) printf(" We have the runtrailer \n");
2467     };
2468     //
2469     if ( lastentry == (firstentry-2) ){ // no events in the run
2470     emptyruns = true;
2471     if ( IsDebug() ) printf(" No events in the run \n");
2472     lastTime = firstTime;
2473     if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
2474     lastObt = glrun->RUNHEADER_OBT;
2475     lastPkt = glrun->RUNHEADER_PKT;
2476     } else {
2477     lastObt = firstObt;
2478     lastPkt = firstPkt;
2479     };
2480     glrun->SetRUNTRAILER_PKT(lastPkt);
2481     glrun->SetRUNTRAILER_OBT(lastObt);
2482     lastentry++;
2483     };
2484     //
2485     this->SetCommonGLRUN(firstTime,lastTime);
2486     //
2487     if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);
2488     //
2489     if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check...
2490     //
2491     firstentry = i;
2492     //
2493     lastentry = i;
2494     //
2495     if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2496     //
2497     glrun->SetEV_FROM(firstentry);
2498     glrun->SetEV_TO(lastentry);
2499     glrun->SetNEVENTS(lastentry-firstentry+1);
2500     //
2501     glrun->Set_GL_RUNH0();
2502     //
2503     glrun->SetLAST_TIMESYNC(0);
2504     glrun->SetOBT_TIMESYNC(0);
2505     //
2506     T->GetEntry(firstentry);
2507     ph = eh->GetPscuHeader();
2508     firstObt = ph->GetOrbitalTime();
2509     firstTime = this->GetAbsTime(firstObt);
2510     firstPkt = ph->GetCounter();
2511     //
2512     glrun->SetRUNHEADER_PKT(firstPkt);
2513     //
2514     glrun->SetRUNHEADER_OBT(firstObt);
2515     //
2516     glrun->Set_GL_RUNT(runt,pht);
2517     lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2518     if ( IsDebug() ) printf(" We have the runtrailer \n");
2519     //
2520     this->SetCommonGLRUN(firstTime,lastTime);
2521     //
2522     if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);
2523     };
2524     //
2525     firstevno = lastentry + 1;
2526     //
2527     checkfirst = check;
2528     //
2529     };
2530     //
2531     if ( check == checklast && i != lastev ) i = lastev - 1; // >>>>>>>>>>>>>>>>>>>>>>
2532     //
2533     };
2534     };
2535     };
2536     //
2537     return;
2538     };
2539    
2540    
2541     /**
2542     * Scan calorimeter calibrations packets, fill the GL_CALO_CALIB table
2543     */
2544     Int_t PamelaDBOperations::insertCALO_CALIB(){
2545     //
2546     TSQLResult *result = 0;
2547     TSQLRow *row = 0;
2548     //
2549     stringstream oss;
2550     oss.str("");
2551     //
2552     CalibCalPedEvent *calibCalPed = 0;
2553     TTree *tr = 0;
2554     EventHeader *eh = 0;
2555     PscuHeader *ph = 0;
2556     //
2557     UInt_t nevents = 0;
2558     UInt_t fromtime = 0;
2559     UInt_t totime = 0;
2560     UInt_t obt = 0;
2561     UInt_t pkt = 0;
2562     //
2563     tr = (TTree*)file->Get("CalibCalPed");
2564     if ( !tr || tr->IsZombie() ) throw -21;
2565     //
2566     tr->SetBranchAddress("CalibCalPed", &calibCalPed);
2567     tr->SetBranchAddress("Header", &eh);
2568     nevents = tr->GetEntries();
2569     //
2570 mocchiut 1.2 if ( !nevents ) return(1);
2571 mocchiut 1.1 //
2572     for (UInt_t i=0; i < nevents; i++){
2573     tr->GetEntry(i);
2574     for (UInt_t section = 0; section < 4; section++){
2575     //
2576     if ( calibCalPed->cstwerr[section] ){
2577     valid = 1;
2578     if ( calibCalPed->cperror[section] ) valid = 0;
2579     ph = eh->GetPscuHeader();
2580     obt = ph->GetOrbitalTime();
2581     pkt = ph->GetCounter();
2582     fromtime = this->GetAbsTime(ph->GetOrbitalTime());
2583     if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
2584     //
2585     if ( IsDebug() ) printf(" Calo calibration for section %i at time %i obt %i pkt %i \n",section,fromtime,obt,pkt);
2586     //
2587     // check if the calibration has already been inserted
2588     //
2589     oss.str("");
2590     oss << " SELECT ID FROM GL_CALO_CALIB WHERE "
2591     << " SECTION = "<< section << " AND "
2592     << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
2593     << " OBT = "<< obt << " AND "
2594     << " PKT = "<< pkt << ";";
2595     //
2596     if ( IsDebug() ) printf(" Check if the calo calibration has already been inserted: query is \n %s \n",oss.str().c_str());
2597     result = conn->Query(oss.str().c_str());
2598     //
2599     if ( !result ) throw -4;
2600     //
2601     row = result->Next();
2602     //
2603     if ( row ){
2604     //
2605     if ( IsDebug() ) printf(" Calo calibration already inserted in the DB\n");
2606     //
2607     } else {
2608     //
2609     // we have to insert a new calibration, check where to place it
2610     //
2611     oss.str("");
2612     oss << " SELECT ID,TO_TIME FROM GL_CALO_CALIB WHERE "
2613     << " SECTION = "<< section << " AND "
2614     << " FROM_TIME < "<< fromtime << " AND "
2615     << " TO_TIME > "<< fromtime << ";";
2616     //
2617     if ( IsDebug() ) printf(" Check where to place the calo calibration: query is \n %s \n",oss.str().c_str());
2618     result = conn->Query(oss.str().c_str());
2619     //
2620     if ( !result ) throw -4;
2621     //
2622     row = result->Next();
2623     //
2624     if ( !row ){
2625     //
2626     // no calibrations in the db contain our calibration
2627     //
2628     if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
2629     if ( fromtime < 1150863400 ){
2630     if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
2631     fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
2632     };
2633     //
2634     oss.str("");
2635     oss << " SELECT FROM_TIME FROM GL_CALO_CALIB WHERE "
2636     << " SECTION = "<< section << " AND "
2637     << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
2638     //
2639     if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
2640     result = conn->Query(oss.str().c_str());
2641     //
2642     if ( !result ) throw -4;
2643     //
2644     row = result->Next();
2645     if ( !row ){
2646     totime = numeric_limits<UInt_t>::max();
2647     } else {
2648     totime = (UInt_t)atoll(row->GetField(0));
2649     };
2650     //
2651     } else {
2652     //
2653     // determine upper and lower limits and make space for the new calibration
2654     //
2655     totime = (UInt_t)atoll(row->GetField(1));
2656     //
2657     oss.str("");
2658     oss << " UPDATE GL_CALO_CALIB SET "
2659     << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
2660     << " ID = "<< row->GetField(0) << ";";
2661     //
2662     if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
2663     result = conn->Query(oss.str().c_str());
2664     //
2665     if ( !result ) throw -4;
2666     //
2667     };
2668     //
2669     oss.str("");
2670     oss << " INSERT INTO GL_CALO_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,OBT,PKT,BOOT_NUMBER,VALIDATION) "
2671     << " VALUES (NULL,' "
2672     << idroot << "','"
2673     << i << "','"
2674     << fromtime << "','"
2675     << totime << "','"
2676     << section << "','"
2677     << obt << "','"
2678     << pkt << "','"
2679     << this->GetBOOTnumber() << "','"
2680     << valid << "');";
2681     //
2682     if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
2683     //
2684     result = conn->Query(oss.str().c_str());
2685     //
2686     if ( !result ) throw -4;
2687     //
2688     };
2689     //
2690     } else {
2691     //
2692     if ( IsDebug() ) printf(" Repetead calo calibration for section %i at time %i obt %i pkt %i \n",section,fromtime,obt,pkt);
2693     //
2694     };
2695     //
2696     };
2697     };
2698     };
2699     //
2700     return(0);
2701     };
2702    
2703     /**
2704     * Fill the GL_TRK_CALIB table
2705     */
2706     void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){
2707     //
2708     TSQLResult *result = 0;
2709     TSQLRow *row = 0;
2710     //
2711     stringstream oss;
2712     oss.str("");
2713     //
2714     UInt_t totime = 0;
2715     //
2716     if ( !pk1 && !pk2 ){
2717     if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n");
2718     };
2719     //
2720     // check if the calibration has already been inserted
2721     //
2722     oss.str("");
2723     oss << " SELECT ID FROM GL_TRK_CALIB WHERE "
2724     << " BOOT_NUMBER = "<< this->GetBOOTnumber(); //
2725     oss << " AND ( ( ";
2726     if ( pk1 ){
2727     oss << " OBT1 = "<< obt1 << " AND "
2728     << " PKT1 = "<< pkt1
2729     << " ) OR ( ";
2730     } else {
2731     oss << " PKT1 = "<< pkt2-1
2732     << " ) OR ( ";
2733     };
2734     if ( pk2 ){
2735     oss << " OBT2 = "<< obt2 << " AND "
2736     << " PKT2 = "<< pkt2;
2737     } else {
2738     oss << " PKT2 = "<< pkt1+1;
2739     };
2740     oss << " ) );";
2741     //
2742     if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str());
2743     result = conn->Query(oss.str().c_str());
2744     //
2745     if ( !result ) throw -4;
2746     //
2747     row = result->Next();
2748     //
2749     if ( row ){
2750     //
2751     if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");
2752     //
2753     } else {
2754     //
2755     // we have to insert a new calibration, check where to place it
2756     //
2757     oss.str("");
2758     oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "
2759     << " FROM_TIME < "<< fromtime << " AND "
2760     << " TO_TIME > "<< fromtime << ";";
2761     //
2762     if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());
2763     result = conn->Query(oss.str().c_str());
2764     //
2765     if ( !result ) throw -4;
2766     //
2767     row = result->Next();
2768     //
2769     if ( !row ){
2770     //
2771     // no calibrations in the db contain our calibration
2772     //
2773     if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
2774     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
2775     //
2776     oss.str("");
2777     oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
2778     << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
2779     //
2780     if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
2781     result = conn->Query(oss.str().c_str());
2782     //
2783     if ( !result ) throw -4;
2784     //
2785     row = result->Next();
2786     if ( !row ){
2787     totime = numeric_limits<UInt_t>::max();
2788     } else {
2789     totime = (UInt_t)atoll(row->GetField(0));
2790     };
2791     //
2792     } else {
2793     //
2794     // determine upper and lower limits and make space for the new calibration
2795     //
2796     totime = (UInt_t)atoll(row->GetField(1));
2797     //
2798     oss.str("");
2799     oss << " UPDATE GL_TRK_CALIB SET "
2800     << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
2801     << " ID = "<< row->GetField(0) << ";";
2802     //
2803     if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str());
2804     result = conn->Query(oss.str().c_str());
2805     //
2806     if ( !result ) throw -4;
2807     //
2808     };
2809     //
2810     oss.str("");
2811     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) "
2812     << " VALUES (NULL,' "
2813     << idroot << "',";
2814     //
2815     if ( !pk1 ){
2816     oss << "NULL,";
2817     } else {
2818     oss << "'"
2819     << t1 << "',";
2820     };
2821     //
2822     if ( !pk2 ){
2823     oss << "NULL,'";
2824     } else {
2825     oss << "'"
2826     << t2 << "','";
2827     };
2828     //
2829     oss << fromtime << "','"
2830     << totime << "','"
2831     << obt1 << "','"
2832     << pkt1 << "','"
2833     << obt2 << "','"
2834     << pkt2 << "','"
2835     << this->GetBOOTnumber() << "','"
2836     << valid << "');";
2837     //
2838     if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());
2839     //
2840     result = conn->Query(oss.str().c_str());
2841     //
2842     if ( !result ) throw -4;
2843     //
2844     };
2845     //
2846     };
2847    
2848     /**
2849     * Scan tracker calibrations packets, fill the GL_TRK_CALIB table
2850     */
2851     Int_t PamelaDBOperations::insertTRK_CALIB(){
2852     //
2853     CalibTrk1Event *caltrk1 = 0;
2854     CalibTrk2Event *caltrk2 = 0;
2855     TTree *tr1 = 0;
2856     TTree *tr2 = 0;
2857     EventHeader *eh1 = 0;
2858     PscuHeader *ph1 = 0;
2859     EventHeader *eh2 = 0;
2860     PscuHeader *ph2 = 0;
2861     //
2862     PacketType *pctp=0;
2863     EventCounter *codt2=0;
2864     //
2865     Int_t nevents1 = 0;
2866     Int_t nevents2 = 0;
2867     //
2868     fromtime = 0;
2869     //
2870     obt1 = 0;
2871     pkt1 = 0;
2872     obt2 = 0;
2873     pkt2 = 0;
2874     //
2875     tr1 = (TTree*)file->Get("CalibTrk1");
2876     if ( !tr1 || tr1->IsZombie() ) throw -22;
2877     tr2 = (TTree*)file->Get("CalibTrk2");
2878     if ( !tr2 || tr2->IsZombie() ) throw -23;
2879     //
2880     tr1->SetBranchAddress("CalibTrk1", &caltrk1);
2881     tr1->SetBranchAddress("Header", &eh1);
2882     nevents1 = tr1->GetEntries();
2883     tr2->SetBranchAddress("CalibTrk2", &caltrk2);
2884     tr2->SetBranchAddress("Header", &eh2);
2885     nevents2 = tr2->GetEntries();
2886     //
2887 mocchiut 1.2 if ( !nevents1 && !nevents2 ) return(1);
2888 mocchiut 1.1 //
2889     t2 = -1;
2890     Int_t pret2 = 0;
2891     Int_t t2t1cal = 0;
2892     //
2893     for (t1=0; t1 < nevents1; t1++){
2894     //
2895     pret2 = t2;
2896     tr1->GetEntry(t1);
2897     //
2898     ph1 = eh1->GetPscuHeader();
2899     obt1 = ph1->GetOrbitalTime();
2900     pkt1 = ph1->GetCounter();
2901     fromtime = this->GetAbsTime(ph1->GetOrbitalTime());
2902     //
2903     valid = 1;
2904     //
2905     if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1
2906     //
2907     //
2908     if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){
2909     //
2910     if ( IsDebug() ) printf(" Trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);
2911     //
2912     // Do we have the second calibration packet?
2913     //
2914     while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1
2915     //
2916     t2++;
2917     //
2918     if ( t2 < nevents2 ){
2919     tr2->GetEntry(t2);
2920     codt2 = eh2->GetCounter();
2921     t2t1cal = codt2->Get(pctp->CalibTrk1);
2922     //
2923     ph2 = eh2->GetPscuHeader();
2924     obt2 = ph2->GetOrbitalTime();
2925     pkt2 = ph2->GetCounter();
2926     //
2927     if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2
2928     //
2929     } else {
2930     //
2931     // running out of vector without finding the corresponding calibration, sig
2932     //
2933     pret2 = t2;
2934     obt2 = 0;
2935     pkt2 = pkt1+2;
2936     t2t1cal = t1+1;
2937     };
2938     if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
2939     //
2940     // running out of vector without finding the corresponding calibration, sig
2941     //
2942     pret2 = t2;
2943     obt2 = 0;
2944     pkt2 = pkt1+2;
2945     t2t1cal = t1+1;
2946     };
2947     //
2948     };
2949     //
2950     if ( IsDebug() ) printf(" Found trk calibration2 at obt %i pkt %i t2 is %i \n",obt2,pkt2,t2);
2951     //
2952     // The calibration is good
2953     //
2954     if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){
2955     //
2956     if ( IsDebug() ) printf(" The trk calibration2 at obt %i pkt %i t2 is %i is good \n",obt2,pkt2,t2);
2957     //
2958     // Handle good calib
2959     //
2960     this->HandleTRK_CALIB(true,true);
2961     //
2962     // Check for missing calibtrk1
2963     //
2964     if ( t2 != pret2+1 ){
2965     //
2966     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);
2967     //
2968     while ( t2 > pret2+1 ){
2969     //
2970     // handle missing calib1
2971     //
2972     pret2++;
2973     //
2974     obt1 = 0;
2975     pkt1 = 0;
2976     //
2977     tr2->GetEntry(pret2);
2978     ph2 = eh2->GetPscuHeader();
2979     obt2 = ph2->GetOrbitalTime();
2980     pkt2 = ph2->GetCounter();
2981     //
2982     fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
2983     //
2984     valid = 0;
2985     this->HandleTRK_CALIB(false,true);
2986     //
2987     };
2988     //
2989     };
2990     //
2991     } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){
2992     //
2993     // Check for missing calibtrk2
2994     //
2995     if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %i pkt %i t2 is %i\n",obt2,pkt2,t2);
2996     t2 = pret2;
2997     //
2998     // handle missing calib2
2999     //
3000     obt2 = 0;
3001     pkt2 = 0;
3002     valid = 0;
3003     this->HandleTRK_CALIB(true,false);
3004     //
3005     };
3006     //
3007     } else {
3008     //
3009     if ( IsDebug() ) printf(" Repetead trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);
3010     //
3011     };
3012     //
3013     };
3014     //
3015     // we have one more calib pkt2 !
3016     //
3017     t2++;
3018     while ( t2 < nevents2 ){
3019     //
3020     // handle missing calib1
3021     //
3022     obt1 = 0;
3023     pkt1 = 0;
3024     //
3025     tr2->GetEntry(t2);
3026     ph2 = eh2->GetPscuHeader();
3027     obt2 = ph2->GetOrbitalTime();
3028     pkt2 = ph2->GetCounter();
3029     //
3030     fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
3031     valid = 0;
3032     if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){
3033     //
3034     if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %i pkt %i t2 is %i\n",obt2,pkt2,t2);
3035     //
3036     this->HandleTRK_CALIB(false,true);
3037     //
3038     };
3039     //
3040     t2++;
3041     //
3042     };
3043     //
3044     return(0);
3045     };
3046    
3047    
3048     /**
3049     * Scan S4 calibrations packets, fill the GL_S4_CALIB table
3050     */
3051     Int_t PamelaDBOperations::insertS4_CALIB(){
3052     //
3053     TSQLResult *result = 0;
3054     TSQLRow *row = 0;
3055     //
3056     stringstream oss;
3057     oss.str("");
3058     //
3059     TTree *tr = 0;
3060     EventHeader *eh = 0;
3061     PscuHeader *ph = 0;
3062     //
3063     UInt_t nevents = 0;
3064     UInt_t fromtime = 0;
3065     UInt_t totime = 0;
3066     UInt_t obt = 0;
3067     UInt_t pkt = 0;
3068     //
3069     tr = (TTree*)file->Get("CalibS4");
3070     if ( !tr || tr->IsZombie() ) throw -24;
3071     //
3072     tr->SetBranchAddress("Header", &eh);
3073     //
3074     nevents = tr->GetEntries();
3075     //
3076 mocchiut 1.2 if ( !nevents ) return(1);
3077 mocchiut 1.1 //
3078     for (UInt_t i = 0; i < nevents; i++){
3079     //
3080     tr->GetEntry(i);
3081     //
3082     ph = eh->GetPscuHeader();
3083     obt = ph->GetOrbitalTime();
3084     pkt = ph->GetCounter();
3085     fromtime = this->GetAbsTime(ph->GetOrbitalTime());
3086     if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
3087     //
3088     if ( IsDebug() ) printf(" S4 calibration at time %i obt %i pkt %i \n",fromtime,obt,pkt);
3089     //
3090     // check if the calibration has already been inserted
3091     //
3092     oss.str("");
3093     oss << " SELECT ID FROM GL_S4_CALIB WHERE "
3094     << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
3095     << " OBT = "<< obt << " AND "
3096     << " PKT = "<< pkt << ";";
3097     //
3098     if ( IsDebug() ) printf(" Check if the S4 calibration has already been inserted: query is \n %s \n",oss.str().c_str());
3099     result = conn->Query(oss.str().c_str());
3100     //
3101     if ( !result ) throw -4;
3102     //
3103     row = result->Next();
3104     //
3105     if ( row ){
3106     //
3107     if ( IsDebug() ) printf(" S4 calibration already inserted in the DB\n");
3108     //
3109     } else {
3110     //
3111     // we have to insert a new calibration, check where to place it
3112     //
3113     oss.str("");
3114     oss << " SELECT ID,TO_TIME FROM GL_S4_CALIB WHERE "
3115     << " FROM_TIME < "<< fromtime << " AND "
3116     << " TO_TIME > "<< fromtime << ";";
3117     //
3118     if ( IsDebug() ) printf(" Check where to place the S4 calibration: query is \n %s \n",oss.str().c_str());
3119     result = conn->Query(oss.str().c_str());
3120     //
3121     if ( !result ) throw -4;
3122     //
3123     row = result->Next();
3124     //
3125     if ( !row ){
3126     //
3127     // no calibrations in the db contain our calibration
3128     //
3129     if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");
3130     if ( fromtime < 1150863400 ){
3131     if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
3132     fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
3133     };
3134     //
3135     oss.str("");
3136     oss << " SELECT FROM_TIME FROM GL_S4_CALIB WHERE "
3137     << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
3138     //
3139     if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
3140     result = conn->Query(oss.str().c_str());
3141     //
3142     if ( !result ) throw -4;
3143     //
3144     row = result->Next();
3145     if ( !row ){
3146     totime = numeric_limits<UInt_t>::max();
3147     } else {
3148     totime = (UInt_t)atoll(row->GetField(0));
3149     };
3150     //
3151     } else {
3152     //
3153     // determine upper and lower limits and make space for the new calibration
3154     //
3155     totime = (UInt_t)atoll(row->GetField(1));
3156     //
3157     oss.str("");
3158     oss << " UPDATE GL_S4_CALIB SET "
3159     << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
3160     << " ID = "<< row->GetField(0) << ";";
3161     //
3162     if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
3163     result = conn->Query(oss.str().c_str());
3164     //
3165     if ( !result ) throw -4;
3166     //
3167     };
3168     //
3169     oss.str("");
3170 mocchiut 1.2 oss << " INSERT INTO GL_S4_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,OBT,PKT,BOOT_NUMBER) "
3171 mocchiut 1.1 << " VALUES (NULL,' "
3172     << idroot << "','"
3173     << i << "','"
3174     << fromtime << "','"
3175     << totime << "','"
3176     << obt << "','"
3177     << pkt << "','"
3178 mocchiut 1.2 << this->GetBOOTnumber() << "');";
3179 mocchiut 1.1 //
3180     if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
3181     //
3182     result = conn->Query(oss.str().c_str());
3183     //
3184     if ( !result ) throw -4;
3185     //
3186     };
3187     //
3188     } else {
3189     //
3190     if ( IsDebug() ) printf(" Repetead S4 calibration at time %i obt %i pkt %i \n",fromtime,obt,pkt);
3191     //
3192     };
3193     //
3194     };
3195     //
3196     return(0);
3197     };
3198    
3199 mocchiut 1.2 /**
3200     * Scan the fragment table and move old fragments to the GL_RUN table
3201 mocchiut 1.1 */
3202 mocchiut 1.2 Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3203     //
3204     TSQLResult *result = 0;
3205     TSQLRow *row = 0;
3206     TSQLResult *result2 = 0;
3207     TSQLRow *row2 = 0;
3208     //
3209     UInt_t moved = 0;
3210 pam-fi 1.8 // UInt_t timelim = 0;
3211     // TDatime *time = new TDatime();
3212 mocchiut 1.2 //
3213     stringstream oss;
3214     oss.str("");
3215     //
3216     //
3217     // check if there are entries older than "olderthan" seconds from now
3218     //
3219     oss.str("");
3220     oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3221 pam-fi 1.8 << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3222 mocchiut 1.2 //
3223 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());
3224 mocchiut 1.2 result = conn->Query(oss.str().c_str());
3225     //
3226     if ( result ){
3227     //
3228     row = result->Next();
3229     //
3230     if ( row ){
3231     //
3232     oss.str("");
3233     oss << " ID= "<< row->GetField(0);
3234     //
3235     glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
3236     //
3237     oss.str("");
3238     oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
3239     << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
3240     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3241     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3242     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3243     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3244     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3245     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3246     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3247     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3248     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3249     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3250     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3251     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3252     //
3253     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3254     result2 = conn->Query(oss.str().c_str());
3255     //
3256     if ( !result2 ) throw -4;
3257     //
3258     row2 = result2->Next();
3259     //
3260     if ( !row2 ){
3261     //
3262     if ( IsDebug() ) printf(" The run is new \n");
3263     if ( IsDebug() ) printf(" -> fill the DB \n");
3264     //
3265     glrun->SetID(0);
3266     glrun->Fill_GL_RUN(conn);
3267     //
3268     oss.str("");
3269     oss << " SELECT ID FROM GL_RUN WHERE "
3270     << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3271     << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3272     << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3273     << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3274     << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3275     //
3276     if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
3277     result2 = conn->Query(oss.str().c_str());
3278     //
3279     if ( !result2 ) throw -4;
3280     //
3281     row2 = result2->Next();
3282     //
3283     if ( !row2 ) throw -25;
3284     //
3285     oss.str("");
3286     oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);
3287     if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());
3288     result2 = conn->Query(oss.str().c_str());
3289     //
3290     if ( !result2 ) throw -4;
3291     //
3292     moved++;
3293     //
3294     } else {
3295     if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");
3296 mocchiut 1.1 };
3297 mocchiut 1.2 if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));
3298     //
3299     //
3300     oss.str("");
3301     oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
3302     if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
3303     result2 = conn->Query(oss.str().c_str());
3304     //
3305     if ( !result2 ) throw -4;
3306     //
3307 mocchiut 1.1 };
3308     };
3309 mocchiut 1.2 if ( IsDebug() ) printf(" Moved %u runs\n",moved);
3310     return(0);
3311     };
3312 mocchiut 1.1
3313 mocchiut 1.2 /**
3314     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3315     */
3316     Int_t PamelaDBOperations::ValidateRuns(){
3317     //
3318     TSQLResult *result = 0;
3319 pam-fi 1.5 TSQLRow *row = 0;
3320 mocchiut 1.2 //
3321     stringstream oss;
3322     oss.str("");
3323     //
3324 pam-fi 1.5 // =======================================================
3325     // validate runs by checking missing calibrations
3326     // =======================================================
3327     UInt_t t_stop = 0;
3328     UInt_t t_start = 0;
3329     // --------------------------------------------------------------
3330     // 1) get the OBT of the last run inserted after clean-time limit
3331     // --------------------------------------------------------------
3332     oss.str("");
3333 pam-fi 1.8 oss << " SELECT * FROM GL_RUN WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3334     << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3335     if ( IsDebug() ) printf(" Get start 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     printf(" No runs to validate \n");
3340     return(1);
3341     }else{
3342     row = result->Next();
3343     t_start = (UInt_t)atoll(row->GetField(4));
3344     };
3345     // --------------------------------------------------------------
3346     // 2) get the OBT of the last validated run
3347     // --------------------------------------------------------------
3348     oss.str("");
3349     oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3350     <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3351 pam-fi 1.8 if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3352 pam-fi 1.5 result = conn->Query(oss.str().c_str());
3353     if ( !result ) throw -4;
3354     if ( result->GetRowCount() ){
3355     row = result->Next();
3356     t_stop = (UInt_t)atoll(row->GetField(4));
3357     };
3358 pam-fi 1.8 if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);
3359 pam-fi 1.5 // --------------------------------------------------------------
3360     // now retrieves runs to be validated
3361     // --------------------------------------------------------------
3362 mocchiut 1.2 oss.str("");
3363 pam-fi 1.5 oss << " SELECT * FROM GL_RUN WHERE RUNHEADER_TIME <=" << t_start;
3364     oss << " AND RUNHEADER_TIME >="<< t_stop;
3365     oss << " ORDER BY RUNHEADER_TIME DESC;";
3366     // if ( IsDebug() )
3367 pam-fi 1.8 if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3368 mocchiut 1.2 result = conn->Query(oss.str().c_str());
3369     if ( !result ) throw -4;
3370 pam-fi 1.5 if ( !result->GetRowCount() ) printf(" No runs to validate \n");
3371 pam-fi 1.8 // printf("------------------------------------------------------------------------------- \n");
3372 pam-fi 1.5
3373     Int_t nrow = 0;
3374     GL_RUN* this_run = new GL_RUN();
3375     GL_RUN* next_run = new GL_RUN();
3376 pam-fi 1.8 Int_t nseq_max = 1000;
3377 pam-fi 1.5 // UInt_t* sequence = new UInt_t[100];
3378     vector<UInt_t> sequence(nseq_max);
3379     Int_t nseq = 0;
3380     Bool_t CHECK = false;
3381     Bool_t this_ONLINE = false;
3382     Bool_t next_ONLINE = false;
3383     UInt_t t1=0,t2=0;
3384     // ---------------------------------------------------------------------------------
3385     // - loop over runs, back in time,
3386     // - select sequences of runs close in time (less than 60 s apart),
3387     // which could be preceeded by a calibration
3388     // - check if there might be a missing calibration
3389     // ---------------------------------------------------------------------------------
3390     while(1){
3391    
3392     row = result->Next();
3393     if( row == NULL ) break;
3394    
3395     //------------
3396     //get run info
3397     //------------
3398     this_run->Set_GL_RUN(row);
3399 pam-fi 1.8
3400 pam-fi 1.5 Bool_t this_BAD = false;
3401     if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;
3402     else if (this_run->GetTRK_CALIB_USED() == 104) this_ONLINE = false;
3403     else{
3404 pam-fi 1.8 // printf("Missing or corrupted header!! \n");
3405 pam-fi 1.5 this_ONLINE = false;
3406     this_BAD = true;
3407     };
3408    
3409     //-----------------------------------
3410     //compare with previous(next in time)
3411     //-----------------------------------
3412     CHECK = false;
3413     UInt_t interval=0;
3414    
3415     if( nrow != 0){
3416    
3417    
3418     t1 = this_run->GetRUNTRAILER_TIME();
3419     t2 = next_run->GetRUNHEADER_TIME();
3420     interval = (t2-t1);
3421    
3422     if(this_ONLINE && next_ONLINE){ // this: ON-LINE + next: ON-LINE
3423    
3424     if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0; //=> run fragments
3425    
3426     if( interval >= 60 )CHECK = true; //more than 60 s => there might be a calibration
3427    
3428     if( !CHECK && this_run->VALIDATION ){
3429     for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
3430     nseq=0;
3431     }
3432    
3433     }else if( !this_ONLINE && next_ONLINE) { // this: DEFAULT + next:ON-LINE
3434    
3435     CHECK = true;
3436    
3437     }else if( !next_ONLINE ){ // this:ANY + next:DEFAULT
3438    
3439     assignVALIDATION(next_run->ID,true);
3440     nseq=0;
3441     }
3442     }
3443    
3444     //----------------------------
3445     //check run sequence for calib
3446     //----------------------------
3447     if( CHECK ){
3448     // check if calibration exists
3449 pam-fi 1.8 if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
3450 pam-fi 1.5 Bool_t MISSING = MissingTRK_CALIB(t1,t2);
3451     for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
3452     nseq=0;
3453 pam-fi 1.8 };
3454 pam-fi 1.5 //--------------
3455     //store run info
3456     //--------------
3457     *next_run = *this_run;
3458     next_ONLINE = this_ONLINE;
3459     if( !this_BAD ){
3460     if(nseq < nseq_max){
3461     sequence[nseq] = this_run->ID;
3462     nseq++;
3463     }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
3464     };
3465    
3466 pam-fi 1.8 if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID);
3467 pam-fi 1.5 nrow++;
3468    
3469     };
3470     delete this_run;
3471     delete next_run;
3472 mocchiut 1.2 //
3473     return(0);
3474 mocchiut 1.1 };
3475 pam-fi 1.5 /**
3476     * Check if there might be a missing tracker calibration in a given time interval
3477     * @param t1 From absolute time
3478     * @param t2 To absolute time
3479     * @return true if there might be a missing calibration
3480     */
3481     Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
3482    
3483     GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
3484    
3485     // get the closest VALIDATED calibration before the run start (t2)
3486     if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true); //>>> missing
3487    
3488     if ( trkcalib->TO_TIME < t2 ) return(true); //>>> missing
3489    
3490     //==============================================================
3491     // Check is done first on the basis of time between calibration,
3492     // which should be equal to the time between ascending-nodes.
3493     //==============================================================
3494     if ( t2 - trkcalib->FROM_TIME > 5700) {
3495 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);
3496 pam-fi 1.5 //==============================================================
3497     // there might be a missing calibration, due to:
3498     // - MM full
3499     // - corrupted packets
3500     // - loss of data
3501     // There is an exception in case a download was done during ascending node
3502     //==============================================================
3503     Bool_t DOWNLOAD = false;
3504     // check if the calib was skipped becouse of download .... DA FARE!!
3505     if(DOWNLOAD)return(false);
3506    
3507     return(true); //>>> missing
3508    
3509     };
3510    
3511     //==============================================================
3512     // If the last calibration is close to the run less than this time,
3513     // it is enough to say that there are no missing calibrations
3514     //==============================================================
3515     // the long time interval bewteen runs might be due to download
3516 pam-fi 1.8 if ( IsDebug() )printf("Short time between calib and run start %i :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
3517 pam-fi 1.5 return(false);
3518    
3519     };
3520     /**
3521     * Assign VALIDATION value to a GL_RUN entry
3522     * @param idrun Run ID
3523     * @param validation true/false
3524     */
3525     Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
3526     TSQLResult *result = 0;
3527     stringstream oss;
3528     oss.str("");
3529 pam-fi 1.8 oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
3530 pam-fi 1.5 //
3531     // if ( IsDebug() )
3532 pam-fi 1.8 // printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
3533     if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
3534     result = conn->Query(oss.str().c_str());
3535     if ( !result ) throw -4;
3536 pam-fi 1.5 return(0);
3537     }
3538    
3539    
3540    

  ViewVC Help
Powered by ViewVC 1.1.23