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

Annotation of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download)
Wed Aug 30 11:18:13 2006 UTC (18 years, 3 months ago) by mocchiut
Branch: MAIN
Branch point for: YodaProfiler
Initial revision

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

  ViewVC Help
Powered by ViewVC 1.1.23