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

Annotation of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download)
Tue Sep 5 10:48:58 2006 UTC (18 years, 3 months ago) by mocchiut
Branch: MAIN
CVS Tags: v1r02
Changes since 1.2: +2 -1 lines
Fixed compilation bug

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

  ViewVC Help
Powered by ViewVC 1.1.23