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

Annotation of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations) (download)
Fri Sep 1 12:47:13 2006 UTC (18 years, 3 months ago) by mocchiut
Branch: MAIN
Changes since 1.1: +223 -101 lines
First release of the new profiler

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

  ViewVC Help
Powered by ViewVC 1.1.23