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

Annotation of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.47 - (hide annotations) (download)
Fri Aug 31 08:51:21 2007 UTC (17 years, 3 months ago) by mocchiut
Branch: MAIN
Changes since 1.46: +10 -0 lines
Repeated fragments bug fixed

1 mocchiut 1.1 //
2     #include <iomanip>
3     #include <sstream>
4     //
5     #include <iostream>
6     #include <string>
7     #include <fstream>
8     #include <list>
9     #include <errno.h>
10     //
11 mocchiut 1.3 #include <TFile.h>
12     #include <TSystem.h>
13 mocchiut 1.1 #include <TSQLResult.h>
14     #include <TSQLRow.h>
15     #include <TTree.h>
16     #include <TGraph.h>
17 mocchiut 1.17 #include <TTimeStamp.h>
18 mocchiut 1.1 #include <TF1.h>
19     //
20     #include <EventHeader.h>
21     #include <PscuHeader.h>
22     #include <mcmd/McmdEvent.h>
23     #include <mcmd/McmdRecord.h>
24     #include <RunHeaderEvent.h>
25     #include <RunTrailerEvent.h>
26     #include <CalibCalPedEvent.h>
27     #include <CalibS4Event.h>
28     #include <CalibTrk1Event.h>
29     #include <CalibTrk2Event.h>
30     #include <varDump/VarDumpEvent.h>
31     #include <varDump/VarDumpRecord.h>
32     #include <physics/S4/S4Event.h>
33     //
34 mocchiut 1.14 #include <sgp4.h>
35 mocchiut 1.12
36 mocchiut 1.1 #include <PamelaDBOperations.h>
37     //
38     using namespace std;
39     using namespace pamela;
40    
41 mocchiut 1.12 // Some function to work with cTle stuff.
42     bool compTLE(cTle* tle1, cTle *tle2);
43     float getTleJulian(cTle *);
44     string getTleDatetime(cTle*);
45    
46 mocchiut 1.1 /**
47     * Constructor.
48     * @param host hostname for the SQL connection.
49     * @param user username for the SQL connection.
50     * @param password password for the SQL connection.
51     * @param filerawname The path and name to the raw file.
52     * @param filerootname The path and name of the raw file.
53     * @param boot file BOOT number.
54     * @param obt0 file obt0.
55     * @param tsync file timesync.
56     * @param debug debug flag.
57 mocchiut 1.12 * @param tlefilename ascii file with TLE 3 line elements.
58 mocchiut 1.1 */
59 mocchiut 1.29 PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename, UInt_t dwinput, Bool_t staticp){
60 mocchiut 1.1 //
61 mocchiut 1.29 STATIC=false;
62     if ( staticp ) STATIC=true;
63 mocchiut 1.1 //
64     SetConnection(host,user,password);
65     //
66     SetDebugFlag(debug);
67     //
68     glrun = new GL_RUN();
69     //
70     if ( !boot ) SetNOBOOT(false);
71     SetBOOTnumber(boot);
72     SetTsync(tsync);
73     SetObt0(obt0);
74     //
75 mocchiut 1.12 SetTLEPath(tlefilename);
76     //
77 mocchiut 1.1 //
78 pam-fi 1.8 INSERT_RAW =!filerawname.IsNull();
79     if(INSERT_RAW)SetRawName(filerawname);
80 mocchiut 1.1 //
81 pam-fi 1.8 INSERT_ROOT = !filerootname.IsNull();
82 mocchiut 1.9 if( INSERT_ROOT ){
83 mocchiut 1.10 this->SetRootName(filerootname);
84 mocchiut 1.27 this->SetOrbitNo(dwinput);
85 mocchiut 1.9 file = TFile::Open(this->GetRootName().Data());
86 mocchiut 1.16 } else {
87     this->SetRootName("");
88 pam-fi 1.8 };
89 mocchiut 1.1 //
90     this->SetID_RAW(0);
91     this->SetID_ROOT(0);
92 pam-fi 1.8
93     VALIDATE = false;
94    
95 mocchiut 1.9 //
96 mocchiut 1.1 };
97    
98     /**
99     * Destructor
100     */
101     void PamelaDBOperations::Close(){
102     if( conn && conn->IsConnected() ) conn->Close();
103 pam-fi 1.8 delete clean_time;
104 mocchiut 1.1 delete glrun;
105     delete this;
106     };
107    
108     //
109     // SETTERS
110     //
111    
112 mocchiut 1.9 //
113     // must be out of the constructor in order to FORCE the validation of the latest runs in case you run the validation together with the latest file
114     //
115     void PamelaDBOperations::CheckValidate(Long64_t olderthan){
116     clean_time = new TDatime();
117 mocchiut 1.17 //
118 mocchiut 1.9 if(olderthan >= 0){
119     VALIDATE = true;
120     UInt_t timelim = 0;
121 mocchiut 1.17 timelim = (UInt_t)clean_time->Convert(true) - olderthan;
122 mocchiut 1.9 clean_time->Set(timelim,false);
123     };
124     };
125    
126 mocchiut 1.1 /**
127     * Open the DB connection
128     * @param host hostname for the SQL connection.
129     * @param user username for the SQL connection.
130     * @param password password for the SQL connection.
131     */
132     void PamelaDBOperations::SetConnection(TString host, TString user, TString password){
133 mocchiut 1.13 if ( IsDebug() ) printf(" Connecting using host = %s user = %s password = %s \n",host.Data(),user.Data(),password.Data());
134 mocchiut 1.1 conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());
135     };
136    
137     /**
138     * Store the ID of the ROOT file.
139     * @param idr ID of the ROOT file
140     */
141     void PamelaDBOperations::SetID_ROOT(UInt_t idr){
142     idroot=idr;
143     };
144    
145     /**
146     * Store the ID of the RAW file.
147     * @param idr ID of the RAW file
148     */
149     void PamelaDBOperations::SetID_RAW(UInt_t idr){
150     id=idr;
151     };
152    
153     /**
154     * Set the debug flag
155     *
156     */
157     void PamelaDBOperations::SetDebugFlag(Bool_t dbg){
158     debug = dbg;
159     };
160    
161     /**
162 mocchiut 1.21 * Set the autoboot flag
163     *
164     */
165     void PamelaDBOperations::SetAutoBoot(Bool_t dbg){
166     AUTOBOOT = dbg;
167     };
168    
169     /**
170 mocchiut 1.16 * Set the nofrag flag
171     *
172     */
173     void PamelaDBOperations::SetNoFrag(Bool_t nf){
174     NOFRAG = nf;
175     };
176    
177     /**
178 mocchiut 1.1 * Store the BOOT number of the RAW file.
179     * @param boot BOOT number of the RAW file
180     */
181     void PamelaDBOperations::SetBOOTnumber(UInt_t boot){
182     BOOTNO=boot;
183     };
184    
185     /**
186     * Store the time sync of the RAW file.
187     * @param boot time sync
188     */
189     void PamelaDBOperations::SetTsync(UInt_t ts){
190     tsync=ts;
191     };
192    
193     /**
194     * Store the time sync of the RAW file.
195     * @param boot time sync
196     */
197     void PamelaDBOperations::SetObt0(UInt_t ts){
198     obt0=ts;
199     };
200    
201     /**
202     * Store the RAW filename.
203     * @param str the RAW filename.
204     */
205     void PamelaDBOperations::SetRawName(TString str){
206     filerawname=str;
207     };
208    
209     /**
210     * Store the ROOT filename.
211     * @param str the ROOT filename.
212     */
213     void PamelaDBOperations::SetRootName(TString str){
214     filerootname=str;
215     };
216    
217     /**
218 mocchiut 1.13 * Store the downlink orbit number from filename.
219     */
220 mocchiut 1.27 void PamelaDBOperations::SetOrbitNo(UInt_t dwinput){
221 mocchiut 1.13 dworbit = 0;
222 mocchiut 1.27 //
223     if ( dwinput ){
224     dworbit = dwinput;
225     if ( IsDebug() ) printf(" Downlink orbit given by hand: %i \n",dworbit);
226     return;
227     };
228     //
229 mocchiut 1.13 TString name = this->GetRootFile();
230     Int_t nlength = name.Length();
231     if ( nlength < 5 ) return;
232     TString dwo = 0;
233     for (Int_t i = 0; i<5; i++){
234     dwo.Append(name[i],1);
235     };
236     if ( dwo.IsDigit() ){
237     dworbit = (UInt_t)dwo.Atoi();
238     } else {
239     dwo="";
240     for (Int_t i = 8; i<13; i++){
241     dwo.Append(name[i],1);
242     };
243     if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
244     };
245     if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
246     return;
247     };
248    
249    
250    
251     /**
252 mocchiut 1.1 * Store the NOBOOT flag.
253     * @param noboot true/false.
254     */
255     void PamelaDBOperations::SetNOBOOT(Bool_t noboot){
256     NOBOOT = noboot;
257     };
258    
259     /**
260 mocchiut 1.12 * Store path to the TLE file.
261     */
262     void PamelaDBOperations::SetTLEPath(TString str){
263     tlefilename = str;
264     };
265    
266 mocchiut 1.29 TString PamelaDBOperations::GetRawPath(){
267     if ( STATIC ){
268     return((TString)gSystem->DirName(filerawname.Data())+'/');
269     } else {
270     return((TString)gSystem->ExpandPathName("$PAM_RAW")+'/');
271     };
272     };
273    
274     TString PamelaDBOperations::GetRootPath(){
275     if ( STATIC ){
276     return((TString)gSystem->DirName(filerootname.Data())+'/');
277     } else {
278     return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');
279     };
280     };
281    
282 mocchiut 1.12 /**
283 mocchiut 1.2 * Store the olderthan variable
284     * @param olderthan
285     */
286 pam-fi 1.8 // void PamelaDBOperations::SetOlderThan(Long64_t oldthan){
287     // olderthan = oldthan;
288     // };
289 mocchiut 1.2
290     /**
291 mocchiut 1.1 * Retrieve the ID_RAW, if exists, returns NULL if does not exist.
292     */
293     Bool_t PamelaDBOperations::SetID_RAW(){
294     stringstream oss;
295     TSQLResult *result = 0;
296     TSQLRow *row = 0;
297     oss.str("");
298 mocchiut 1.29 if ( STATIC ){
299     oss << "SELECT ID FROM GL_RAW WHERE "
300     << " PATH = '" << this->GetRawPath().Data() << "' AND "
301     << " NAME = '" << this->GetRawFile().Data() << "' ";
302     } else {
303     oss << "SELECT ID FROM GL_RAW WHERE "
304     << " PATH = '$PAM_RAW' AND "
305     << " NAME = '" << this->GetRawFile().Data() << "' ";
306     }
307 mocchiut 1.1 result = conn->Query(oss.str().c_str());
308 mocchiut 1.2 if ( result == NULL ) throw -4;
309 mocchiut 1.1 row = result->Next();
310 mocchiut 1.2 if ( !row ) return(false);
311 mocchiut 1.1 delete result;
312     id = (UInt_t)atoll(row->GetField(0));
313     return(true);
314     }
315    
316     /**
317     *
318     * Set the variables which have to be stored in the GL_RUN table and that do not depend on the RUN
319     *
320     */
321     void PamelaDBOperations::SetCommonGLRUN(UInt_t absth, UInt_t abstt){
322     glrun->SetBOOTNUMBER(BOOTNO);
323     glrun->SetRUNHEADER_TIME(absth);
324     glrun->SetRUNTRAILER_TIME(abstt);
325     glrun->SetID_ROOT_L2(0);
326     glrun->SetID_ROOT_L0(idroot);
327     glrun->SetVALIDATION(0);
328     };
329    
330     /**
331     * Patch, look for upper limits to avoid processing retransmitted data
332     */
333     Int_t PamelaDBOperations::SetUpperLimits(){
334     UInt_t nevent = 0;
335     UInt_t pktlast = 0;
336     UInt_t obtlast = 0;
337 mocchiut 1.21 Long64_t t_pktlast = 0LL;
338     // UInt_t t_obtlast = 0;
339     Long64_t t_obtlast = 0LL;
340     Long64_t upperpkt2 = 0LL;
341     Long64_t upperobt2 = 0LL;
342 mocchiut 1.1 UInt_t zomp = 0;
343     UInt_t jump = 50000; // was 5000
344 mocchiut 1.4 EventCounter *code=0;
345     //
346 mocchiut 1.21 Long64_t deltapkt = 5000LL;
347     Long64_t deltaobt = 50000LL;
348 mocchiut 1.13 //
349 mocchiut 1.28 pcksList packetsNames;
350     pcksList::iterator Iter;
351     getPacketsNames(packetsNames);
352 mocchiut 1.1 //
353     pktfirst = 0;
354     obtfirst = 0;
355 mocchiut 1.28 ppktfirst = 0;
356     pobtfirst = 0;
357 mocchiut 1.1 //
358     TTree *T = 0;
359     T = (TTree*)file->Get("Physics");
360     if ( !T || T->IsZombie() ) throw -16;
361     EventHeader *eh = 0;
362     PscuHeader *ph = 0;
363     T->SetBranchAddress("Header", &eh);
364     nevent = T->GetEntries();
365     //
366     T->GetEntry(0);
367     ph = eh->GetPscuHeader();
368     pktfirst = ph->GetCounter();
369     obtfirst = ph->GetOrbitalTime();
370 mocchiut 1.28 ppktfirst = pktfirst;
371     pobtfirst = obtfirst;
372 mocchiut 1.1 //
373 mocchiut 1.28 code = eh->GetCounter();
374     UInt_t en = 0;
375     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
376     en = code->Get(GetPacketType(*Iter));
377     if ( !strcmp("CalibCalPed",*Iter) || !strcmp("CalibTrk1",*Iter) || !strcmp("CalibTrk2",*Iter) || !strcmp("CalibS4",*Iter) ){
378     if ( IsDebug() ) printf(" We have a calibration before the first physic packet: packet type is %s, entries: %i \n",*Iter,en);
379     //
380     TTree *TC = 0;
381     TC = (TTree*)file->Get("CalibCalPed");
382     if ( !TC || TC->IsZombie() ) throw -16;
383     EventHeader *ehc = 0;
384     PscuHeader *phc = 0;
385     TC->SetBranchAddress("Header", &ehc);
386     TC->GetEntry(0);
387     phc = ehc->GetPscuHeader();
388     pktfirst = phc->GetCounter();
389     obtfirst = phc->GetOrbitalTime();
390     //
391     };
392     };
393 mocchiut 1.4 //
394 mocchiut 1.1 T->GetEntry(nevent-1);
395     ph = eh->GetPscuHeader();
396     pktlast = ph->GetCounter();
397     obtlast = ph->GetOrbitalTime();
398     //
399     upperpkt = PKT(pktlast);
400     upperobt = OBT(obtlast);
401     upperentry = nevent-1;
402     //
403 mocchiut 1.34 if ( IsDebug() ) printf(" First entries are: OBT %u pkt_num %u \n",obtfirst,pktfirst);
404 mocchiut 1.1 //
405 mocchiut 1.21 if ( IsDebug() ) printf(" Last entries are: OBT %lld pkt_num %lld entry %i\n",upperobt,upperpkt,upperentry);
406 mocchiut 1.1 //
407 mocchiut 1.34 if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ) return(32);
408 mocchiut 1.1 //
409 mocchiut 1.34 if ( !nevent ) return(64);
410 mocchiut 1.1 //
411 mocchiut 1.34 if ( nevent < 2 ) return(128);
412 mocchiut 1.27 if ( nevent < jump ) jump = 1;
413     // if ( nevent < jump ) jump = int(nevent/10);
414     // if ( !jump ) jump = 1;
415 mocchiut 1.1 //
416 mocchiut 1.27 if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) && nevent > deltapkt ){
417 mocchiut 1.13 //
418 mocchiut 1.27 if ( IsDebug() ) printf(" starting jump %i \n",jump);
419 mocchiut 1.1 // go back
420     zomp = nevent - 2;
421     //
422     while ( jump > 0 ){
423     //
424     t_pktlast = PKT(pktlast);
425     t_obtlast = OBT(obtlast);
426     //
427     for (UInt_t i = zomp; i>1; i-=jump){
428     //
429     if ( i >= 0 ) T->GetEntry(i);
430     ph = eh->GetPscuHeader();
431     upperpkt = PKT(ph->GetCounter());
432     upperobt = OBT(ph->GetOrbitalTime());
433     upperentry = i;
434     //
435     if ( (i-1) >= 0 ) T->GetEntry(i-1);
436     ph = eh->GetPscuHeader();
437     upperpkt2 = PKT(ph->GetCounter());
438     upperobt2 = OBT(ph->GetOrbitalTime());
439     //
440 mocchiut 1.21 if ( (t_pktlast < upperpkt && t_obtlast > upperobt) || (t_pktlast < upperpkt2 && t_obtlast > upperobt2) ){
441     if ( IsDebug() ) printf(" .-. upperpkt2 %lld upperobt2 %lld \n",upperpkt2,upperobt2);
442     if ( IsDebug() ) printf(" .-. upperpkt %lld t_pktlast %lld upperobt %lld t_obtlast %lld \n",upperpkt,t_pktlast,upperobt,t_obtlast);
443     if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %lld pktlast %u upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);
444     throw -13;
445     };
446 mocchiut 1.1 //
447     if ( t_pktlast < upperpkt && t_obtlast < upperobt && t_pktlast < upperpkt2 && t_obtlast < upperobt2 ){
448     zomp = i + jump + 1;
449     if ( zomp > nevent-2 ) zomp = nevent - 2;
450 mocchiut 1.35 if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %lld pktlast %u upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);
451 mocchiut 1.1 break;
452     };
453     //
454     t_pktlast = upperpkt;
455     t_obtlast = upperobt;
456     };
457     //
458     if ( jump == 1 ) jump = 0;
459     if ( jump == 10 ) jump = 1;
460     if ( jump == 100 ) jump = 10;
461     if ( jump == 1000 ) jump = 100;
462     if ( jump == 5000 ) jump = 1000;
463     if ( jump == 50000 ) jump = 5000;
464     //
465     };
466     //
467     };
468     //
469     // check if last runtrailer is within limits, if not extend limits (one should check for all packets but we need only runtrailer)
470     //
471     PacketType *pctp=0;
472     TTree *rh=(TTree*)file->Get("RunHeader");
473     if ( !rh || rh->IsZombie() ) throw -17;
474     TTree *rt=(TTree*)file->Get("RunTrailer");
475     if ( !rt || rt->IsZombie() ) throw -18;
476     //
477     rh->SetBranchAddress("RunHeader", &runh);
478     rh->SetBranchAddress("Header", &ehh);
479     //
480     rt->SetBranchAddress("RunTrailer", &runt);
481     rt->SetBranchAddress("Header", &eht);
482     //
483     rhev = rh->GetEntries();
484     rtev = rt->GetEntries();
485 mocchiut 1.21 Long64_t sobtt = 0LL;
486     Long64_t sobth = 0LL;
487     Long64_t spktt = 0LL;
488     Long64_t spkth = 0LL;
489     Long64_t pktt = 0LL;
490     Long64_t obtt = 0LL;
491     Long64_t pkth = 0LL;
492     Long64_t obth = 0LL;
493 mocchiut 1.1 //
494 mocchiut 1.27 if ( rhev || rtev ){
495    
496 mocchiut 1.28 T->GetEntry(upperentry);
497     code = eh->GetCounter();
498     Int_t lasttrail = code->Get(pctp->RunTrailer);
499     Int_t lasthead = code->Get(pctp->RunHeader);
500     if ( lasttrail < rtev ){
501     rt->GetEntry(lasttrail);
502     pht = eht->GetPscuHeader();
503     pktt = PKT(pht->GetCounter());
504     obtt = OBT(pht->GetOrbitalTime());
505     };
506     //
507     if ( lasthead < rhev ){
508     rh->GetEntry(lasthead);
509     phh = ehh->GetPscuHeader();
510     pkth = PKT(phh->GetCounter());
511     obth = OBT(phh->GetOrbitalTime());
512     };
513     //
514     if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
515     if ( pkth > upperpkt && obth > upperobt ){
516     if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
517     upperpkt = pkth;
518     upperobt = obth;
519     rhev = lasthead+1;
520     } else {
521     rhev = lasthead;
522     };
523     if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
524     //
525     if ( IsDebug() ) printf(" rtev beforev %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
526     if ( pktt > upperpkt && obtt > upperobt ){
527     if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
528     upperpkt = pktt;
529     upperobt = obtt;
530     rtev = lasttrail+1;
531     } else {
532     rtev = lasttrail;
533     };
534     if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
535     // goto kikko;
536     //
537     //
538     // Check if runtrailer/runheader are within lower limits
539     //
540     //
541     pkth = 0LL;
542     obth = 0LL;
543     spkth = 0LL;
544     sobth = 0LL;
545     for (Int_t k=0; k<rhev; k++){
546     if ( k > 0 ){
547     spkth = pkth;
548     sobth = obth;
549     };
550     rh->GetEntry(k);
551     phh = ehh->GetPscuHeader();
552     pkth = PKT(phh->GetCounter());
553     obth = OBT(phh->GetOrbitalTime());
554     //
555     // if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
556     //
557     if ( pkth < spkth && obth < sobth ){
558     if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i \n",rhev);
559     //
560     rhev = k-1;
561     rh->GetEntry(rhev);
562     pkth = spkth;
563     obth = sobth;
564     //
565     UInt_t evbefh = 0;
566     code = ehh->GetCounter();
567     evbefh = code->Get(pctp->Physics);
568     if ( evbefh >= 0 ){
569     T->GetEntry(evbefh);
570     ph = eh->GetPscuHeader();
571     t_pktlast = PKT(ph->GetCounter());
572     t_obtlast = OBT(ph->GetOrbitalTime());
573     if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
574     upperpkt = pkth;
575     upperobt = obth;
576     upperentry = evbefh-1;
577     } else {
578     while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){
579     evbefh++;
580     T->GetEntry(evbefh);
581     ph = eh->GetPscuHeader();
582     t_pktlast = PKT(ph->GetCounter());
583     t_obtlast = OBT(ph->GetOrbitalTime());
584     };
585     T->GetEntry(evbefh-1);
586 mocchiut 1.4 ph = eh->GetPscuHeader();
587 mocchiut 1.28 upperpkt = PKT(ph->GetCounter());
588     upperobt = OBT(ph->GetOrbitalTime());
589     upperentry = evbefh-1;
590     };
591     };
592     if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
593     goto kikko0;
594 mocchiut 1.4 };
595     };
596 mocchiut 1.28 kikko0:
597     //
598 mocchiut 1.4 //
599 mocchiut 1.6 //
600 mocchiut 1.28 pktt = 0LL;
601     obtt = 0LL;
602     spktt = 0LL;
603     sobtt = 0LL;
604     for (Int_t k=0; k<rtev; k++){
605     if ( k > 0 ){
606     spktt = pktt;
607     sobtt = obtt;
608     };
609     rt->GetEntry(k);
610     pht = eht->GetPscuHeader();
611     pktt = PKT(pht->GetCounter());
612     obtt = OBT(pht->GetOrbitalTime());
613 mocchiut 1.4 //
614 mocchiut 1.28 // if ( IsDebug() ) printf(" k %i rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
615 mocchiut 1.4 //
616 mocchiut 1.28 if ( pktt < spktt && obtt < sobtt ){
617     if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
618     //
619     rtev = k-1;
620     rt->GetEntry(rtev);
621     pktt = spktt;
622     obtt = sobtt;
623     if ( IsDebug() ) printf(" lasttrail %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
624     //
625     UInt_t evbeft = 0;
626     code = eht->GetCounter();
627     evbeft = code->Get(pctp->Physics);
628     if ( evbeft >= 0 ){
629     T->GetEntry(evbeft);
630     ph = eh->GetPscuHeader();
631     t_pktlast = PKT(ph->GetCounter());
632     t_obtlast = OBT(ph->GetOrbitalTime());
633     if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
634     upperpkt = pktt;
635     upperobt = obtt;
636     upperentry = evbeft-1;
637     } else {
638     while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){
639     evbeft++;
640     T->GetEntry(evbeft);
641     ph = eh->GetPscuHeader();
642     t_pktlast = PKT(ph->GetCounter());
643     t_obtlast = OBT(ph->GetOrbitalTime());
644     };
645     T->GetEntry(evbeft-1);
646 mocchiut 1.4 ph = eh->GetPscuHeader();
647 mocchiut 1.28 upperpkt = PKT(ph->GetCounter());
648     upperobt = OBT(ph->GetOrbitalTime());
649     upperentry = evbeft-1;
650 mocchiut 1.4 };
651     };
652 mocchiut 1.28 if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
653     goto kikko;
654     // break;
655     //
656 mocchiut 1.4 };
657 mocchiut 1.28 //
658     };
659     //
660     kikko:
661     //
662     T->GetEntry(upperentry);
663     code = eh->GetCounter();
664     lasttrail = code->Get(pctp->RunTrailer);
665     lasthead = code->Get(pctp->RunHeader);
666     if ( lasttrail < rtev ){
667     rt->GetEntry(lasttrail);
668     pht = eht->GetPscuHeader();
669     pktt = PKT(pht->GetCounter());
670     obtt = OBT(pht->GetOrbitalTime());
671     };
672     //
673     if ( lasthead < rhev ){
674     rh->GetEntry(lasthead);
675     phh = ehh->GetPscuHeader();
676     pkth = PKT(phh->GetCounter());
677     obth = OBT(phh->GetOrbitalTime());
678     };
679     //
680     if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
681     if ( pkth > upperpkt && obth > upperobt ){
682     if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
683     upperpkt = pkth;
684     upperobt = obth;
685     rhev = lasthead+1;
686     } else {
687     rhev = lasthead;
688     };
689     if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
690     //
691     if ( IsDebug() ) printf(" rtev beforev %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
692     if ( pktt > upperpkt && obtt > upperobt ){
693     if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
694     upperpkt = pktt;
695     upperobt = obtt;
696     rtev = lasttrail+1;
697     } else {
698     rtev = lasttrail;
699 mocchiut 1.4 };
700 mocchiut 1.28 if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
701     //
702 mocchiut 1.27 };
703     //
704 mocchiut 1.21 if ( IsDebug() ) printf(" Upper limits are: OBT %lld pkt_num %lld upper entry %i \n",upperobt,upperpkt,upperentry);
705 mocchiut 1.1 //
706     return(0);
707     }
708    
709 mocchiut 1.13 /**
710     *
711     * Trick to have unique RUN ID numbers even when runs are deleted and mysql deamon restarted.
712     * Entries in the _RUNID_GEN table are never deleted.
713     *
714     **/
715     UInt_t PamelaDBOperations::AssignRunID(){
716     //
717     TSQLResult *result = 0;
718     TSQLRow *row = 0;
719     UInt_t runid = 0;
720     //
721     stringstream oss;
722     //
723     oss.str("");
724     oss << "INSERT INTO _RUNID_GEN VALUES (NULL);";
725     result = conn->Query(oss.str().c_str());
726     if ( !result ) throw -10;
727     oss.str("");
728     oss << "SELECT ID FROM _RUNID_GEN ORDER BY ID DESC LIMIT 1;";
729     result = conn->Query(oss.str().c_str());
730     if ( !result ) throw -10;
731     //
732     row = result->Next();
733     //
734     if ( !row ) throw -28;
735     //
736     runid = (UInt_t)atoll(row->GetField(0));
737     //
738     return(runid);
739     };
740    
741 mocchiut 1.1 //
742     // GETTERS
743     //
744    
745     /**
746     *
747     * Returns the DB absolute time needed to associate calibrations to data
748     *
749     */
750     UInt_t PamelaDBOperations::GetAbsTime(UInt_t obt){
751     //
752     return(((UInt_t)(OBT(obt)/1000)+toffset));
753     //
754     };
755    
756     /**
757     *
758     * List of packet types (just to make easily the loops)
759     *
760     */
761     const PacketType* PamelaDBOperations::GetPacketType(const char* type){
762     if ( !strcmp(type,"Pscu") ) return(PacketType::Pscu);
763     if ( !strcmp(type,"PhysEndRun") ) return(PacketType::PhysEndRun);
764     if ( !strcmp(type,"CalibCalPulse1") ) return(PacketType::CalibCalPulse1);
765     if ( !strcmp(type,"CalibCalPulse2") ) return(PacketType::CalibCalPulse2);
766     if ( !strcmp(type,"Physics") ) return(PacketType::Physics);
767     if ( !strcmp(type,"CalibTrkBoth") ) return(PacketType::CalibTrkBoth);
768     if ( !strcmp(type,"CalibTrk1") ) return(PacketType::CalibTrk1);
769     if ( !strcmp(type,"CalibTrk2") ) return(PacketType::CalibTrk2);
770     if ( !strcmp(type,"CalibTof") ) return(PacketType::CalibTof);
771     if ( !strcmp(type,"CalibS4") ) return(PacketType::CalibS4);
772     if ( !strcmp(type,"CalibCalPed") ) return(PacketType::CalibCalPed);
773     if ( !strcmp(type,"Calib1_Ac1") ) return(PacketType::Calib1_Ac1);
774     if ( !strcmp(type,"Calib2_Ac1") ) return(PacketType::Calib2_Ac1);
775     if ( !strcmp(type,"Calib1_Ac2") ) return(PacketType::Calib1_Ac2);
776     if ( !strcmp(type,"Calib2_Ac2") ) return(PacketType::Calib2_Ac2);
777     if ( !strcmp(type,"CalibCal") ) return(PacketType::CalibCal);
778     if ( !strcmp(type,"RunHeader") ) return(PacketType::RunHeader);
779     if ( !strcmp(type,"RunTrailer") ) return(PacketType::RunTrailer);
780     if ( !strcmp(type,"CalibHeader") ) return(PacketType::CalibHeader);
781     if ( !strcmp(type,"CalibTrailer") ) return(PacketType::CalibTrailer);
782     if ( !strcmp(type,"InitHeader") ) return(PacketType::InitHeader);
783     if ( !strcmp(type,"InitTrailer") ) return(PacketType::InitTrailer);
784     if ( !strcmp(type,"EventTrk") ) return(PacketType::EventTrk);
785     if ( !strcmp(type,"Log") ) return(PacketType::Log);
786     if ( !strcmp(type,"VarDump") ) return(PacketType::VarDump);
787     if ( !strcmp(type,"ArrDump") ) return(PacketType::ArrDump);
788     if ( !strcmp(type,"TabDump") ) return(PacketType::TabDump);
789     if ( !strcmp(type,"Tmtc") ) return(PacketType::Tmtc);
790     if ( !strcmp(type,"Mcmd") ) return(PacketType::Mcmd);
791     if ( !strcmp(type,"ForcedFECmd") ) return(PacketType::ForcedFECmd);
792     if ( !strcmp(type,"Ac1Init") ) return(PacketType::Ac1Init);
793     if ( !strcmp(type,"CalInit") ) return(PacketType::CalInit);
794     if ( !strcmp(type,"TrkInit") ) return(PacketType::TrkInit);
795     if ( !strcmp(type,"TofInit") ) return(PacketType::TofInit);
796     if ( !strcmp(type,"TrgInit") ) return(PacketType::TrgInit);
797     if ( !strcmp(type,"NdInit") ) return(PacketType::NdInit);
798     if ( !strcmp(type,"S4Init") ) return(PacketType::S4Init);
799     if ( !strcmp(type,"Ac2Init") ) return(PacketType::Ac2Init);
800     if ( !strcmp(type,"CalAlarm") ) return(PacketType::CalAlarm);
801     if ( !strcmp(type,"Ac1Alarm") ) return(PacketType::Ac1Alarm);
802     if ( !strcmp(type,"TrkAlarm") ) return(PacketType::TrkAlarm);
803     if ( !strcmp(type,"TrgAlarm") ) return(PacketType::TrgAlarm);
804     if ( !strcmp(type,"TofAlarm") ) return(PacketType::TofAlarm);
805     if ( !strcmp(type,"S4Alarm") ) return(PacketType::S4Alarm);
806     if ( !strcmp(type,"Ac2Alarm") ) return(PacketType::Ac2Alarm);
807     if ( !strcmp(type,"TsbT") ) return(PacketType::TsbT);
808     if ( !strcmp(type,"TsbB") ) return(PacketType::TsbB);
809     return(PacketType::Invalid);
810     };
811    
812     //
813     // PRIVATE FUNCTIONS
814     //
815    
816 pam-fi 1.8 // /**
817     // * Open the ROOT filename for reading
818     // */
819     // void PamelaDBOperations::OpenFile(){
820     // file = TFile::Open(this->GetRootName().Data());
821     // //
822 mocchiut 1.9
823     void PamelaDBOperations::CheckFile(){
824     if ( !file ) throw -12;
825     };
826 mocchiut 1.1
827    
828     /**
829     * Check if LEVEL0 file and DB connection have really be opened
830     */
831 pam-fi 1.8 void PamelaDBOperations::CheckConnection(){
832 mocchiut 1.1 //
833     // check connection
834     //
835 pam-fi 1.8 if( !conn ) throw -1;
836 mocchiut 1.1 bool connect = conn->IsConnected();
837 pam-fi 1.8 if( !connect ) throw -1;
838 mocchiut 1.26 //
839     if ( IsDebug() ) printf("\n DB INFORMATIONS:\n SQL: %s Version: %s Host %s Port %i \n\n",conn->GetDBMS(),conn->ServerInfo(),conn->GetHost(),conn->GetPort());
840     //
841 mocchiut 1.16 if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
842 mocchiut 1.17 //
843     // set DB timezone to UTC
844     //
845 mocchiut 1.19 stringstream oss;
846 mocchiut 1.17 //
847     oss.str("");
848     oss << "SET time_zone='+0:00';";
849 mocchiut 1.19 TSQLResult *result = 0;
850     result = conn->Query(oss.str().c_str());
851     if ( !result ) throw -10;
852 mocchiut 1.26 oss.str("");
853     oss << "SET wait_timeout=173000;";
854     conn->Query(oss.str().c_str());
855 mocchiut 1.17 //
856 mocchiut 1.1 };
857    
858     /**
859     * Return the correct packet number if we went back to zero
860     */
861 mocchiut 1.21 Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){
862 mocchiut 1.1 //
863 mocchiut 1.28 if ( IsDebug() ) printf(" pkt conversion: pkt_num is %u pktfirst is %u (UInt_t)(16777214/2)) is %u \n",pkt_num,ppktfirst,(UInt_t)(16777214/2));
864 mocchiut 1.1 //
865 mocchiut 1.28 if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2) ){
866 mocchiut 1.21 if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL);
867     return((Long64_t)pkt_num+16777215LL);
868     };
869 mocchiut 1.1 //
870 mocchiut 1.33 if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){
871 mocchiut 1.21 if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL);
872     return((Long64_t)pkt_num-16777215LL);
873 mocchiut 1.1 };
874     //
875 mocchiut 1.21 if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
876     return((Long64_t)pkt_num);
877 mocchiut 1.1 //
878     };
879    
880     /**
881     * Return the correct On Board Time if we went back to zero
882     */
883 mocchiut 1.21 Long64_t PamelaDBOperations::OBT(UInt_t obt){
884 mocchiut 1.1 //
885 mocchiut 1.33 if ( IsDebug() ) printf(" obt conversion: obt is %u obtfirst is %u (numeric_limits<UInt_t>::max()/2) is %u \n",obt,pobtfirst,(UInt_t)(numeric_limits<UInt_t>::max()/2));
886 mocchiut 1.30 //
887 mocchiut 1.31 if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){
888 mocchiut 1.32 if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)(obt+numeric_limits<UInt_t>::max()));
889 mocchiut 1.31 return((Long64_t)(obt+numeric_limits<UInt_t>::max()));
890     };
891 mocchiut 1.1 //
892 mocchiut 1.33 if ( obt > ((Long64_t)pobtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
893     if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2));
894 mocchiut 1.31 if ( IsDebug() ) printf(" rise down pktnum %lld \n", (Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
895 mocchiut 1.21 return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
896 mocchiut 1.1 };
897     //
898 mocchiut 1.31 if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);
899 mocchiut 1.21 return((Long64_t)obt);
900 mocchiut 1.1 };
901    
902     /**
903     *
904     * Fill the glrun class with infos about the run when we have both runtrailer and runheader
905     *
906     */
907     void PamelaDBOperations::FillClass(){
908     this->FillClass(false,false,0,0);
909     };
910    
911     /**
912     *
913     * Fill the glrun class with infos about the run when we have both runtrailer and runheader
914     *
915     */
916     void PamelaDBOperations::FillClass(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
917     //
918     TTree *T = 0;
919     T = (TTree*)file->Get("Physics");
920     if ( !T || T->IsZombie() ) throw -16;
921     //
922     EventHeader *eh = 0;
923     PscuHeader *ph = 0;
924     T->SetBranchAddress("Header", &eh);
925     PacketType *pctp=0;
926     EventCounter *codt=0;
927     EventCounter *codh=0;
928     UInt_t firstObt = 0;
929     UInt_t lastObt = 0;
930     UInt_t firstPkt = 0;
931     UInt_t lastPkt = 0;
932     UInt_t rhtime = 0;
933     UInt_t rttime = 0;
934     if ( !mishead ){
935     codh = ehh->GetCounter();
936     firstev = codh->Get(pctp->Physics);
937     rhtime = this->GetAbsTime(phh->GetOrbitalTime());
938     glrun->Set_GL_RUNH(runh,phh);
939     firstObt = glrun->GetRUNHEADER_OBT();
940     firstPkt = glrun->GetRUNHEADER_PKT();
941     };
942     if ( !mistrail ){
943     codt = eht->GetCounter();
944     lastev = codt->Get(pctp->Physics)-1;
945     rttime = this->GetAbsTime(pht->GetOrbitalTime());
946     glrun->Set_GL_RUNT(runt,pht);
947     lastObt = glrun->GetRUNTRAILER_OBT();
948     lastPkt = glrun->GetRUNTRAILER_PKT();
949     };
950     //
951 mocchiut 1.2 if ( mishead && mistrail && lastev+1 == firstev ) throw -14; // run with no events, no runtrailer, no runheader... unsupported should never arrive here
952 mocchiut 1.1 //
953     if ( mishead ) {
954     glrun->Set_GL_RUNH0();
955     //
956     if ( lastev+1 == firstev ){
957     firstObt = lastObt;
958     firstPkt = lastPkt;
959     rhtime = rttime;
960     } else {
961     T->GetEntry(firstev);
962     ph = eh->GetPscuHeader();
963     firstObt = ph->GetOrbitalTime();
964     rhtime = this->GetAbsTime(firstObt);
965     firstPkt = ph->GetCounter();
966     };
967     //
968     glrun->SetRUNHEADER_PKT(firstPkt);
969     glrun->SetRUNHEADER_OBT(firstObt);
970     //
971     };
972     if ( mistrail ){
973     glrun->Set_GL_RUNT0();
974     //
975     if ( lastev+1 == firstev ){
976     lastObt = firstObt;
977     lastPkt = firstPkt;
978     rttime = rhtime;
979     } else {
980     T->GetEntry(lastev);
981     ph = eh->GetPscuHeader();
982     lastObt = ph->GetOrbitalTime();
983     rttime = this->GetAbsTime(lastObt);
984     lastPkt = ph->GetCounter();
985     };
986     //
987     glrun->SetRUNTRAILER_OBT(lastObt);
988     glrun->SetRUNTRAILER_PKT(lastPkt);
989     //
990     };
991     glrun->SetEV_FROM(firstev);
992     glrun->SetEV_TO(lastev);
993     glrun->SetNEVENTS(lastev-firstev+1);
994     //
995     this->SetCommonGLRUN(rhtime,rttime);
996     //
997     };
998    
999     //
1000     // PUBLIC FUNCTIONS
1001     //
1002    
1003     /**
1004     * Insert a new row into GL_RAW table.
1005     */
1006     Int_t PamelaDBOperations::insertPamelaRawFile(){
1007     //
1008     stringstream oss;
1009     //
1010     Bool_t idr = this->SetID_RAW();
1011     if ( idr ) return(1);
1012     //
1013     oss.str("");
1014 mocchiut 1.29 if ( STATIC ){
1015     oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
1016     << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";
1017     } else {
1018     oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')";
1019     };
1020 mocchiut 1.1 if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1021     //
1022     idr = this->SetID_RAW();
1023     if ( !idr ) throw -11;
1024     //
1025     return(0);
1026     }
1027    
1028    
1029     /**
1030     * Look for one timesync information in the file and
1031     * 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
1032     */
1033     Int_t PamelaDBOperations::insertPamelaGL_TIMESYNC(){
1034     //
1035     TSQLResult *result = 0;
1036     TSQLRow *row = 0;
1037     UInt_t t0 = 0;
1038 mocchiut 1.34 Int_t signal = 0;
1039     //
1040     signal = this->SetUpperLimits();
1041 mocchiut 1.1 //
1042     stringstream oss;
1043     //
1044     if ( this->GetID_RAW() == 0 ) throw -11;
1045     //
1046     oss.str("");
1047 mocchiut 1.19 oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
1048 mocchiut 1.13 << this->GetRawFile().Data() << "';";
1049 mocchiut 1.1 if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1050     result = conn->Query(oss.str().c_str());
1051     if ( !result ) throw -10;
1052     row = result->Next();
1053 mocchiut 1.13 //
1054     if ( !row ){
1055     oss.str("");
1056 mocchiut 1.19 oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
1057 mocchiut 1.13 << dworbit << " order by FROM_ORBIT desc limit 1;";
1058     if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1059     result = conn->Query(oss.str().c_str());
1060     if ( !result ) throw -10;
1061     row = result->Next();
1062     if ( !row ) throw -10;
1063     };
1064 mocchiut 1.1 //
1065 mocchiut 1.19 TTimeStamp tu = TTimeStamp((UInt_t)atoi(row->GetField(0)),(UInt_t)atoi(row->GetField(1)),(UInt_t)atoi(row->GetField(2)),(UInt_t)atoi(row->GetField(3)),(UInt_t)atoi(row->GetField(4)),(UInt_t)atoi(row->GetField(5)),0,true,0);
1066 mocchiut 1.17 t0 = (UInt_t)tu.GetSec();
1067 mocchiut 1.19 if ( IsDebug() ) printf(" t0 is %u ti is %s %s %s %s %s %s\n",t0,row->GetField(0),row->GetField(1),row->GetField(2),row->GetField(3),row->GetField(4),row->GetField(5));
1068 mocchiut 1.17 //
1069 mocchiut 1.1 /*
1070     * Verify that the TIMESYNC have been not already processed
1071     */
1072     oss.str("");
1073     oss << " SELECT COUNT(GL_TIMESYNC.ID),GL_TIMESYNC.OBT0,GL_TIMESYNC.TIMESYNC FROM GL_TIMESYNC "
1074     << " LEFT JOIN GL_RAW "
1075     << " ON GL_RAW.ID = GL_TIMESYNC.ID_RAW "
1076     << " WHERE GL_TIMESYNC.ID_RAW = " << this->GetID_RAW()
1077     << " GROUP BY GL_TIMESYNC.OBT0;";
1078     if ( IsDebug() ) printf(" check for old timesync: query is \n %s \n",oss.str().c_str());
1079     result = conn->Query(oss.str().c_str());
1080     if (result == NULL) throw -10;
1081     row = result->Next();
1082     if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
1083 mocchiut 1.19 if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1084 mocchiut 1.1 toffset = (UInt_t)atoll(row->GetField(2)) - (UInt_t)(this->OBT((UInt_t)atoll(row->GetField(1)))/1000) + t0;
1085 mocchiut 1.21 //
1086     tsync = (UInt_t)atoll(row->GetField(2));
1087     obt0 = (UInt_t)atoll(row->GetField(1));
1088     //
1089 mocchiut 1.1 return(1);
1090     };
1091     //
1092     TTree *T = 0;
1093     //
1094     UInt_t nevent = 0;
1095     UInt_t recEntries = 0;
1096     //
1097     UInt_t OBT = 0;
1098     UInt_t TYPE = 0;
1099     //
1100     Double_t minimum = 0.;
1101     Double_t maximum = 0.;
1102     Double_t minimum2 = 0.;
1103     Double_t maximum2 = 0.;
1104     //
1105     UInt_t TSYNC = 0;
1106     //
1107     pamela::McmdEvent *mc = 0;
1108     pamela::McmdRecord *mcrc = 0;
1109     TArrayC *mcmddata = 0;
1110     //
1111     minimum = numeric_limits<Double_t>::max();
1112     maximum = numeric_limits<Double_t>::min();
1113     minimum2 = numeric_limits<Double_t>::max();
1114     maximum2 = numeric_limits<Double_t>::min();
1115     //
1116     T = (TTree*)file->Get("Mcmd");
1117     if ( !T || T->IsZombie() ) throw -19;
1118     T->SetBranchAddress("Mcmd",&mc);
1119     //
1120     nevent = T->GetEntries();
1121     //
1122     // loop over events
1123     //
1124     Bool_t existsts = false;
1125     //
1126     for (UInt_t i=0; i<nevent;i++){
1127     //
1128     T->GetEntry(i);
1129     //
1130     recEntries = mc->Records->GetEntries();
1131     //
1132     for (UInt_t j = 0; j < recEntries; j++){
1133     mcrc = (pamela::McmdRecord*)mc->Records->At(j);
1134     mcmddata = mcrc->McmdData;
1135     //
1136     if (mcrc->ID1 == 0xE0){ // mcmd timesync
1137     //
1138     OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
1139     //
1140     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);
1141     //
1142     TYPE = 55;//224;
1143     //
1144 mocchiut 1.11 if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
1145     //
1146 mocchiut 1.1 if ( TSYNC && OBT ){
1147     existsts = true;
1148 mocchiut 1.2 goto eout;
1149 mocchiut 1.1 };
1150     //
1151     };
1152     };
1153     };
1154     if ( !existsts ) { // try with runheader and runtrailer
1155     //
1156 mocchiut 1.2 if ( IsDebug() ) printf(" No ts mcmd \n");
1157     signal = 2;
1158     //
1159 mocchiut 1.1 TTree *rh=(TTree*)file->Get("RunHeader");
1160     if ( !rh || rh->IsZombie() ) throw -17;
1161     TTree *rt=(TTree*)file->Get("RunTrailer");
1162     if ( !rt || rt->IsZombie() ) throw -18;
1163     //
1164     rh->SetBranchAddress("RunHeader", &runh);
1165     //
1166     rt->SetBranchAddress("RunTrailer", &runt);
1167     //
1168 mocchiut 1.11 Int_t nrhev = rh->GetEntries();
1169     Int_t nrtev = rt->GetEntries();
1170     if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev);
1171     //
1172     if ( nrhev > 0 ){
1173     for (Int_t i=0; i<nrhev; i++){
1174     //
1175     rh->GetEntry(i);
1176     //
1177     TSYNC = runh->LAST_TIME_SYNC_INFO;
1178 mocchiut 1.45 OBT = runh->OBT_TIME_SYNC * 1000;
1179 mocchiut 1.11 //
1180     TYPE = 20;
1181     //
1182     if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
1183     //
1184     if ( TSYNC && OBT ){
1185     existsts = true;
1186     goto eout;
1187     };
1188 mocchiut 1.1 };
1189     //
1190     };
1191 mocchiut 1.11 if ( nrtev > 0 ){
1192 mocchiut 1.2 //
1193     if ( IsDebug() ) printf(" No runheader \n");
1194     signal = 6;
1195     //
1196 mocchiut 1.11 for (Int_t i=0; i<nrtev; i++){
1197     //
1198     rt->GetEntry(i);
1199     //
1200     TSYNC = runt->LAST_TYME_SYNC_INFO;
1201 mocchiut 1.45 OBT = runt->OBT_TYME_SYNC * 1000;
1202 mocchiut 1.11 //
1203     TYPE = 21;
1204     //
1205     if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
1206     //
1207     if ( TSYNC && OBT ){
1208     existsts = true;
1209     goto eout;
1210     };
1211 mocchiut 1.1 };
1212     //
1213 mocchiut 1.2 } else {
1214     if ( IsDebug() ) printf(" No runheader \n");
1215 mocchiut 1.1 };
1216     };
1217     //
1218     if ( !existsts ){ // try with inclination mcmd
1219 mocchiut 1.2 //
1220     if ( IsDebug() ) printf(" No runtrailer \n");
1221     signal = 14;
1222     //
1223 mocchiut 1.1 Double_t timesync = 0.;
1224     for (UInt_t i=0; i<nevent;i++){
1225     //
1226     T->GetEntry(i);
1227     //
1228     recEntries = mc->Records->GetEntries();
1229     // //
1230     for (UInt_t j = 0; j < recEntries; j++){
1231     mcrc = (pamela::McmdRecord*)mc->Records->At(j);
1232     mcmddata = mcrc->McmdData;
1233     //
1234     if (mcrc->ID1 == 0xE2){ // mcmd inclination
1235     timesync = 0.;
1236     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);
1237     //
1238     if ( timesync > maximum2){
1239     maximum2 = timesync;
1240     OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
1241     };
1242     };
1243     //
1244     };
1245     };
1246     if ( maximum2 > numeric_limits<Double_t>::min() ){
1247     TSYNC = (UInt_t)(maximum2 + 0.5);
1248     TYPE = 666;
1249     if ( TSYNC && OBT ){
1250     existsts = true;
1251 mocchiut 1.2 goto eout;
1252 mocchiut 1.1 };
1253     };
1254     };
1255     //
1256     if ( !existsts && obt0 ){ // insert timesync by hand
1257 mocchiut 1.2 //
1258     if ( IsDebug() ) printf(" No incl mcmd \n");
1259     signal = 30;
1260     //
1261 mocchiut 1.1 OBT = obt0;
1262     TSYNC = tsync;
1263     TYPE = 999;
1264     existsts = true;
1265 mocchiut 1.2 goto eout;
1266 mocchiut 1.1 };
1267     //
1268 mocchiut 1.2 eout:
1269 mocchiut 1.1 //
1270     if ( !existsts ) throw -3;
1271     //
1272     oss.str("");
1273     oss << "INSERT INTO GL_TIMESYNC (ID_RAW,TYPE,OBT0,TIMESYNC) VALUES ('"
1274     << this->GetID_RAW() << "','"//224'"
1275     << dec << (UInt_t)TYPE << "','"
1276     << dec << (UInt_t)OBT << "','"
1277     << dec << (UInt_t)TSYNC << "');";
1278     conn->Query(oss.str().c_str());
1279     if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());
1280     //
1281 mocchiut 1.19 if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1282 mocchiut 1.1 toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1283     //
1284 mocchiut 1.21 tsync = TSYNC;
1285     obt0 = OBT;
1286     //
1287 mocchiut 1.1 delete result;
1288 mocchiut 1.2 return(signal);
1289 mocchiut 1.1 }
1290    
1291     /**
1292     * Insert all the new rows into GL_ROOT.
1293     * The raw file indicates in the parameters should be already been stored in the database.
1294     */
1295     Int_t PamelaDBOperations::insertPamelaRootFile(){
1296     stringstream oss;
1297     TSQLResult *result = 0;
1298     TSQLRow *row = 0;
1299     UInt_t idtimesync = 0;
1300     //
1301     oss.str("");
1302 mocchiut 1.29 if ( STATIC ){
1303     oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1304     << " LEFT JOIN GL_ROOT "
1305     << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1306     << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1307     << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1308     } else {
1309     oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1310     << " LEFT JOIN GL_ROOT "
1311     << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1312     << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "
1313     << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1314     };
1315 mocchiut 1.1 result = conn->Query(oss.str().c_str());
1316     //
1317     if ( !result ) throw -12;
1318     //
1319     row = result->Next();
1320     //
1321     if ( !row ) throw -10;
1322     if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){
1323     idroot = (UInt_t)atoll(row->GetField(2));
1324     return(1);
1325     };
1326     //
1327     // determine which timesync has to be used
1328     //
1329     oss.str("");
1330     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;";
1331     result = conn->Query(oss.str().c_str());
1332     //
1333     if ( !result ) throw -3;
1334     //
1335     row = result->Next();
1336     //
1337     if ( !row ) throw -3;
1338     idtimesync = (UInt_t)atoll(row->GetField(0));
1339     //
1340     oss.str("");
1341 mocchiut 1.29 if ( STATIC ){
1342     oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1343     << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";
1344     } else {
1345     oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1346     << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')";
1347     };
1348 mocchiut 1.1 //
1349     if (conn->Query(oss.str().c_str()) == 0) throw -4;
1350     //
1351     delete result;
1352     //
1353     oss.str("");
1354     oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";
1355     //
1356     result = conn->Query(oss.str().c_str());
1357     if ( !result ) throw -12;
1358     row = result->Next();
1359     this->SetID_ROOT((UInt_t)atoll(row->GetField(0)));
1360     //
1361     delete result;
1362     //
1363     return(0);
1364     }
1365    
1366     /**
1367     * Assign the BOOT_NUMBER to the raw file.
1368     */
1369     Int_t PamelaDBOperations::assignBOOT_NUMBER(){
1370     stringstream oss;
1371     TSQLResult *result = 0;
1372     TSQLRow *row = 0;
1373     oss.str("");
1374 mocchiut 1.29 if ( STATIC ){
1375     oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1376     << " PATH = '" << this->GetRawPath().Data() << "' AND "
1377     << " NAME = '" << this->GetRawFile().Data() << "' ";
1378     } else {
1379     oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1380     << " PATH = '$PAM_RAW' AND "
1381     << " NAME = '" << this->GetRawFile().Data() << "' ";
1382     };
1383 mocchiut 1.1 result = conn->Query(oss.str().c_str());
1384     //
1385 mocchiut 1.2 if ( !result ) throw -4;;
1386 mocchiut 1.1 row = result->Next();
1387     if ( !row ) return(16);
1388     if ( row->GetField(1) ){
1389     this->SetBOOTnumber((UInt_t)atoll(row->GetField(1)));
1390     return(1);
1391     };
1392 mocchiut 1.2 if ( !row->GetField(0) ) throw -26;
1393 mocchiut 1.1 //
1394     UInt_t idRaw = (UInt_t)atoll(row->GetField(0));
1395     //
1396     //
1397     //
1398     TTree *trDumpEv = 0;
1399     trDumpEv = (TTree*)file->Get("VarDump");
1400     if ( !trDumpEv || trDumpEv->IsZombie() ) throw -20;
1401     //
1402     VarDumpEvent *vde = 0;
1403     VarDumpRecord *vdr = 0;
1404     //
1405 mocchiut 1.21 Bool_t found = false;
1406 mocchiut 1.1 trDumpEv->SetBranchAddress("VarDump", &vde);
1407 mocchiut 1.2 if ( trDumpEv->GetEntries() > 0 ){
1408 mocchiut 1.21 found = false;
1409 mocchiut 1.2 for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1410     trDumpEv->GetEntry(i);
1411 mocchiut 1.20 // vde->Records->GetEntries();
1412     if ( vde->Records->GetEntries()>5 ){
1413 mocchiut 1.2 found = true;
1414     goto fill;
1415     };
1416     };
1417     fill:
1418     if ( found ){
1419     //
1420 mocchiut 1.1 vdr = (VarDumpRecord*)vde->Records->At(6);
1421 mocchiut 1.2 //
1422 mocchiut 1.1 this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1423 mocchiut 1.2 //
1424     } else {
1425 mocchiut 1.21 if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4);
1426     };
1427     } else {
1428     if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2);
1429     };
1430     //
1431     UInt_t bn = 0;
1432     Bool_t afound = false;
1433     if ( !found && this->AutoBoot()){
1434     afound = true;
1435     //
1436     // Search for other files with similar timesync
1437     //
1438     if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0);
1439     UInt_t upperts = tsync-(obt0/1000)+5;
1440     UInt_t lowerts = tsync-(obt0/1000)-5;
1441     oss.str("");
1442     oss << "select GL_RAW.BOOT_NUMBER from GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW where TIMESYNC-(OBT0/1000)<"
1443     << upperts
1444     << " AND TIMESYNC-(OBT0/1000)>"
1445     << lowerts
1446     << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
1447     result = conn->Query(oss.str().c_str());
1448     if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str());
1449     //
1450     if ( !result ) throw -4;;
1451     found = true;
1452     if ( result->GetRowCount()<3 ){
1453     if ( IsDebug() ) printf(" AGH! no results!\n");
1454     found = false;
1455     } else {
1456     row = result->Next();
1457     bn = (UInt_t)atoll(row->GetField(0));
1458     for ( Int_t r=1; r<result->GetRowCount() ;r++){
1459     if ( !row ) throw -4;
1460     if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0));
1461     if ( bn != (UInt_t)atoll(row->GetField(0)) ){
1462     if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0)));
1463     found = false;
1464     };
1465     row = result->Next();
1466     };
1467 mocchiut 1.1 };
1468 mocchiut 1.21 };
1469     //
1470     Int_t sgn = 0;
1471     //
1472 mocchiut 1.27 if ( !found && !BOOTNO ){
1473 mocchiut 1.21 throw -29;
1474 mocchiut 1.1 } else {
1475 mocchiut 1.21 if ( afound ){
1476     this->SetBOOTnumber(bn);
1477     sgn = 8;
1478     };
1479 mocchiut 1.1 };
1480     //
1481     oss.str("");
1482     oss << " UPDATE GL_RAW "
1483     << " SET GL_RAW.BOOT_NUMBER = '" << dec << this->GetBOOTnumber() << "'"
1484     << " WHERE GL_RAW.ID = '" << idRaw << "'";
1485     conn->Query(oss.str().c_str());
1486     //
1487     delete result;
1488 mocchiut 1.21 return(sgn);
1489 mocchiut 1.1 };
1490    
1491     /**
1492     * Scan runtrailer packet, fill the GL_RUN table and
1493     * check for missing and truncated runs
1494     */
1495     Int_t PamelaDBOperations::insertPamelaRUN(){
1496     Int_t signal = 0;
1497     //
1498     stringstream oss;
1499     oss.str("");
1500     //
1501 mocchiut 1.34 // signal = this->SetUpperLimits();
1502 mocchiut 1.1 //
1503     // loop on runheader and runtrailer events
1504     //
1505     TTree *rh=(TTree*)file->Get("RunHeader");
1506     if ( !rh || rh->IsZombie() ) throw -17;
1507     TTree *rt=(TTree*)file->Get("RunTrailer");
1508     if ( !rt || rt->IsZombie() ) throw -18;
1509     //
1510     PacketType *pctp=0;
1511     EventCounter *cod=0;
1512     //
1513     rh->SetBranchAddress("RunHeader", &runh);
1514     rh->SetBranchAddress("Header", &ehh);
1515     //
1516     rt->SetBranchAddress("RunTrailer", &runt);
1517     rt->SetBranchAddress("Header", &eht);
1518     //
1519 mocchiut 1.42 TTree *T = (TTree*)file->Get("Physics");
1520     if ( !T || T->IsZombie() ) throw -16;
1521     EventHeader *eh = 0;
1522     T->SetBranchAddress("Header", &eh);
1523     //
1524     if ( !(rh->GetEntries()) && !(rt->GetEntries()) && !(T->GetEntries()) ) return(16);
1525     //
1526 mocchiut 1.1 UInt_t obtt = 0;
1527     UInt_t obth = 0;
1528     UInt_t pktt = 0;
1529     UInt_t pkth = 0;
1530     Int_t pth = -1;
1531     Int_t ptht = -1;
1532     Int_t evbeft = 0;
1533     Int_t evbefh = 0;
1534     //
1535     // no runtrailers in the file!
1536     //
1537     if ( !rtev ){
1538     if ( !upperentry ){
1539     if ( IsDebug() ) printf(" No physics events nor runs in the file \n");
1540     throw -8;
1541     } else {
1542     this->HandleRunFragments(true,true,0,upperentry);
1543     };
1544     } else {
1545     //
1546     for (Int_t ptt=0; ptt<rtev; ptt++){
1547     //
1548     rt->GetEntry(ptt);
1549     pht = eht->GetPscuHeader();
1550     pktt = pht->GetCounter();
1551     obtt = pht->GetOrbitalTime();
1552     //
1553     cod = eht->GetCounter();
1554     ptht = cod->Get(pctp->RunHeader) - 1;
1555     evbeft = cod->Get(pctp->Physics);
1556     //
1557     if ( !ptt && !(ptht+1) ){
1558     //
1559 mocchiut 1.35 if ( IsDebug() ) printf(" Piece of run at the beginning of the file %u %u %u \n",ptht,pth,ptt);
1560 mocchiut 1.1 //
1561     this->HandleRunFragments(true,false,0,(evbeft-1));
1562     //
1563     //
1564     } else if ( pth == ptht ){
1565     //
1566 mocchiut 1.35 if ( IsDebug() ) printf(" Missing header %u %u %u\n",ptht,pth,ptt);
1567 mocchiut 1.1 //
1568 mocchiut 1.2 if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1569 mocchiut 1.1 rt->GetEntry(ptt-1);
1570     cod = eht->GetCounter();
1571     evbefh = cod->Get(pctp->Physics);
1572     rt->GetEntry(ptt);
1573     pht = eht->GetPscuHeader();
1574     //
1575 mocchiut 1.35 if ( IsDebug() ) printf(" Try to find the beginning of a run which has only the runtrailer %u %u %u \n",ptht,pth,ptt);
1576     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
1577 mocchiut 1.1 //
1578     this->HandleMissingHoT(true,false,evbefh,evbeft-1);
1579     //
1580     } else {
1581     //
1582 mocchiut 1.35 if ( IsDebug() ) printf(" Could be a good run, we have a runheader followed by a runtrailer %u %u %u\n",ptht,pth,ptt);
1583 mocchiut 1.1 //
1584     rh->GetEntry(ptht);
1585     phh = ehh->GetPscuHeader();
1586     pkth = phh->GetCounter();
1587     obth = phh->GetOrbitalTime();
1588     cod = ehh->GetCounter();
1589     evbefh = cod->Get(pctp->Physics);
1590 mocchiut 1.35 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
1591 mocchiut 1.1 //
1592     // handle this run
1593     //
1594     this->HandleRun();
1595     //
1596     //
1597     //
1598     if ( PKT(pkth)>PKT(pktfirst) && OBT(obth)>OBT(obtfirst) && !ptt ){
1599     //
1600 mocchiut 1.40 if ( IsDebug() ) printf(" Piece of run at the beginning of the file WITH NO RUNTRAILER evbefh = %u \n",evbefh);
1601 mocchiut 1.1 //
1602 mocchiut 1.41 if ( evbefh == 0 ) {
1603 mocchiut 1.36 //
1604     signal = 8;
1605     if ( IsDebug() ) printf(" Not supported yet: run with no events, no runtrailer, no runheader \n");
1606     //
1607     } else {
1608     //
1609 mocchiut 1.39 this->HandleRunFragments(true,true,0,(evbefh-1));
1610 mocchiut 1.36 //
1611     };
1612 mocchiut 1.1 };
1613     //
1614     //
1615     if ( (ptht - pth) > 1 ){
1616     //
1617     if ( IsDebug() ) printf(" Missing runtrailers! \n");
1618 mocchiut 1.35 if ( IsDebug() ) printf(" Attention there is a jump in the runheader counter %u %u %u \n",ptht,pth,ptt);
1619 mocchiut 1.1 // is not the consecutive header
1620     while ( pth != ptht ){
1621     //
1622     // treat the header(s) in the middle and then go to the next header, repeat until you reach the correct header.
1623     //
1624     pth++;
1625     //
1626     rh->GetEntry(pth+1);
1627     phh = ehh->GetPscuHeader();
1628     pktt = phh->GetCounter();
1629     obtt = phh->GetOrbitalTime();
1630     cod = ehh->GetCounter();
1631     evbeft = cod->Get(pctp->Physics);
1632     rh->GetEntry(pth);
1633     phh = ehh->GetPscuHeader();
1634     cod = ehh->GetCounter();
1635     pkth = phh->GetCounter();
1636     obth = phh->GetOrbitalTime();
1637     evbefh = cod->Get(pctp->Physics);
1638     //
1639 mocchiut 1.35 if ( IsDebug() ) printf(" Try to find the end of a run which has only the runheader %u %u %u \n",ptht,pth,ptt);
1640     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
1641 mocchiut 1.1 //
1642     this->HandleMissingHoT(false,true,evbefh,evbeft-1);
1643     //
1644     };
1645     //
1646     } else if ( !(ptht - pth) ){
1647     //
1648     if ( IsDebug() ) printf(" Missing runheader! \n");
1649 mocchiut 1.35 if ( IsDebug() ) printf(" Attention! the runheader counter did not changed %u %u %u \n",ptht,pth,ptt);
1650 mocchiut 1.1 if ( IsDebug() ) printf(" The run should have already been handled by HandleRun() \n");
1651     //
1652     } else {
1653     //
1654     // go on with next header
1655     //
1656     pth = ptht;
1657     };
1658     //
1659     };
1660     //
1661     if ( ptt+1 == rtev){
1662     ptht++;
1663     if ( ptht < rhev ){
1664     rh->GetEntry(ptht);
1665     phh = ehh->GetPscuHeader();
1666     pkth = phh->GetCounter();
1667     obth = phh->GetOrbitalTime();
1668     cod = ehh->GetCounter();
1669     evbefh = cod->Get(pctp->Physics);
1670 mocchiut 1.35 if ( IsDebug() ) printf(" Piece of run at the end of file %u %u %u \n",pkth,obth,obtt);
1671     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u %u %u \n",ptht,pth,ptt);
1672     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u \n",rhev);
1673 mocchiut 1.1 //
1674     this->HandleRunFragments(false,true,evbefh,upperentry);
1675     } else {
1676     //
1677     // check if we have a fragment with no header
1678     //
1679     if ( (UInt_t)evbeft < upperentry-1 ){
1680     if ( IsDebug() ) printf(" Piece of run at the end of the file with NO RUNHEADER!\n");
1681     //
1682 mocchiut 1.2 if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1683 mocchiut 1.1 rt->GetEntry(ptt-1);
1684     cod = eht->GetCounter();
1685     evbefh = cod->Get(pctp->Physics);
1686     rt->GetEntry(ptt);
1687     pht = eht->GetPscuHeader();
1688     this->HandleRunFragments(true,true,evbefh,upperentry);
1689     };
1690     };
1691     };
1692     //
1693     };
1694     };
1695     //
1696     return(signal);
1697     };
1698    
1699     /**
1700     *
1701     * Check if the run has already been inserted
1702     *
1703     */
1704     Bool_t PamelaDBOperations::IsRunAlreadyInserted(){
1705     //
1706     TSQLResult *result = 0;
1707     TSQLRow *row = 0;
1708     //
1709     stringstream oss;
1710     oss.str("");
1711     //
1712     // the where clause is of the type: boot_number = _our_boot && (
1713     // ( runhead_time >= (_our_runhead_time-10) && runtrail_time <= (_our_runtrail_time+10) &&
1714     // ( runhead_obt >= _our_runheadobt || runhead_pkt >= _our_runheadpkt ) &&
1715     // ( runtrail_obt >= _our_runtrailobt || runtrail_pkt >= _our_runtrailpkt ) )
1716     // ||
1717     // ( runhead_time <= _our_runhead_time && runtrail_time >= _our_runtrail_time) &&
1718     // ( runhead_obt <= _our_runheadobt || runhead_pkt <= _our_runheadpkt ) &&
1719     // ( runtrail_obt <= _our_runtrailobt || runtrail_pkt <= _our_runtrailpkt ) )
1720 mocchiut 1.46 // ||
1721     // ( runhead_time = _our_runhead_time && runtrail_time = _our_runtrail_time && nevents > 100 )
1722 mocchiut 1.1 // )
1723 mocchiut 1.47 // ||
1724     // ( runhead_time = _our_runhead_time && runtrail_time > _our_runtrail_time && nevents > 100 )
1725     // )
1726     // ||
1727     // ( runhead_time < _our_runhead_time && runtrail_time = _our_runtrail_time && nevents > 100 )
1728     // )
1729 mocchiut 1.1 //
1730     oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
1731     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1732     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1733     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1734     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1735     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1736     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1737     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1738     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1739     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1740     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1741     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1742     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1743 mocchiut 1.46 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") OR "
1744     << " (RUNHEADER_TIME=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " // these two lines in a certain way disable the patch below...
1745     << " RUNTRAILER_TIME=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100)" //
1746 mocchiut 1.47 << " (RUNHEADER_TIME=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " //
1747     << " RUNTRAILER_TIME>" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100)" //
1748     << " (RUNHEADER_TIME<" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " //
1749     << " RUNTRAILER_TIME=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100)" //
1750 mocchiut 1.46 << " ));";
1751 mocchiut 1.1 //
1752     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1753     result = conn->Query(oss.str().c_str());
1754     //
1755     if ( !result ) throw -4;
1756     //
1757     row = result->Next();
1758     //
1759     if ( !row ){
1760     if ( IsDebug() ) printf(" The run is new \n");
1761     if ( IsDebug() ) printf(" -> fill the DB \n");
1762     return(false); // the file has not been inserted in the DB, go on.
1763     };
1764     //
1765     Bool_t signal = true;
1766     //
1767     while ( row != NULL ){
1768     if ( IsDebug() ) printf(" A run exists with runheader and runtrailer time and packets compatible with this one \n");
1769     //
1770     // the run has already been inserted
1771     //
1772 mocchiut 1.46 if ( signal && IsDebug() ) printf(" The run has already been inserted \n");
1773     return(true); //<<<<<<<<<<<<<<<<<<<<<<<< patch follows, uncomment here
1774 mocchiut 1.1 //
1775     // PATCH!
1776     // 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
1777     // while the old run doesn't have it 3) we have more events than the old run
1778     //
1779     if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1780     //
1781     if ( IsDebug() ) printf(" The new run has more events than the old one \n");
1782 mocchiut 1.15 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1783     // oss.str("");
1784     // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1785     // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1786     // conn->Query(oss.str().c_str());
1787 mocchiut 1.1 if ( signal ) signal = false;
1788     goto gonext;
1789     //
1790     } else if ( glrun->GetNEVENTS() < (UInt_t)atoll(row->GetField(1)) ){
1791     if ( IsDebug() ) printf(" The new run has less events than the old one \n");
1792     if ( IsDebug() ) printf(" The run is already inserted \n");
1793     goto gonext;
1794     };
1795     //
1796     if ( glrun->GetTRK_CALIB() && !(UInt_t)atoll(row->GetField(2)) ){
1797     //
1798     if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");
1799     //
1800 mocchiut 1.15 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1801     // oss.str("");
1802     // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1803     // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1804     // conn->Query(oss.str().c_str());
1805 mocchiut 1.1 //
1806     if ( signal ) signal = false;
1807     goto gonext;
1808     } else if ( !glrun->GetTRK_CALIB() && (UInt_t)atoll(row->GetField(2)) ){
1809     if ( IsDebug() ) printf(" The new run has the same number of events but miss the runheader the old has the runheader \n");
1810     if ( IsDebug() ) printf(" The run is already inserted \n");
1811     goto gonext;
1812     };
1813     //
1814     if ( glrun->GetPKT_COUNTER() && !(UInt_t)atoll(row->GetField(3)) ){
1815     //
1816     if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");
1817     //
1818 mocchiut 1.15 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1819     // oss.str("");
1820     // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1821     // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1822     // conn->Query(oss.str().c_str());
1823 mocchiut 1.1 if ( signal ) signal = false;
1824     //
1825     };
1826     //
1827     gonext:
1828     // END PATCH!
1829     //
1830     row = result->Next();
1831     //
1832     };
1833     //
1834     delete result;
1835     //
1836     if ( signal && IsDebug() ) printf(" The run has already been inserted \n");
1837 mocchiut 1.23 if ( !signal && IsDebug() ) printf(" The run existed and was deleted, fill the DB \n");
1838 mocchiut 1.1 return(signal);
1839     };
1840    
1841     /**
1842     * Handle runs which seems to be good ones.
1843     **/
1844     void PamelaDBOperations::HandleRun(){
1845     ULong64_t chkpkt = 0;
1846     ULong64_t pktt = (ULong64_t)PKT(pht->GetCounter());
1847     ULong64_t pkth = (ULong64_t)PKT(phh->GetCounter());
1848     //
1849     chkpkt = pkth + (ULong64_t)runt->PKT_COUNTER + 1ULL + 1ULL;
1850     //
1851     if ( labs(chkpkt-pktt)<2 ){
1852     //
1853     if ( IsDebug() ) printf(" check %llu pktt %llu \n",chkpkt,pktt);
1854     //
1855     // it must be a good run, fill the db
1856     //
1857     this->FillClass();
1858     //
1859 mocchiut 1.13 if ( !IsRunAlreadyInserted() ){
1860     glrun->SetID(this->AssignRunID());
1861     glrun->SetID_RUN_FRAG(0);
1862     glrun->Fill_GL_RUN(conn);
1863     };
1864 mocchiut 1.1 } else {
1865     //
1866     if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt);
1867     if ( IsDebug() ) printf(" try to recover run(s) without runheader and runtrailer between runheader and runtrailer\n");
1868     //
1869     this->HandleSuspiciousRun();
1870     //
1871     };
1872     //
1873     //
1874     return;
1875     };
1876    
1877    
1878     /**
1879     * Handle run fragments at the beginning or at the end of the file
1880     **/
1881     void PamelaDBOperations::HandleRunFragments(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
1882     //
1883     UInt_t rhfirstev = firstev;
1884     UInt_t rtlastev = lastev;
1885     Bool_t found = false;
1886 mocchiut 1.16 Bool_t foundinrun = false;
1887 mocchiut 1.1 //
1888     TSQLResult *result = 0;
1889     TSQLRow *row = 0;
1890     //
1891     stringstream oss;
1892     oss.str("");
1893     //
1894     // is the piece of run good (no other packets inside)?
1895     //
1896     if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
1897     //
1898     // if not, handle other pieces and continue with the first one
1899     //
1900     if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
1901     //
1902     };
1903     //
1904     // we have now the good first piece of a run, fill the glrun object
1905     //
1906     if ( rhfirstev != firstev && !mishead ) mishead = true;
1907     if ( rtlastev != lastev && !mistrail ) mistrail = true;
1908     //
1909     this->FillClass(mishead,mistrail,firstev,lastev);
1910     //
1911     if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
1912     //
1913 mocchiut 1.16 // First of all insert the run in the fragment table...
1914     //
1915     oss.str("");
1916     oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1917     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1918     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1919     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1920     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1921     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1922     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1923     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1924     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1925     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1926     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1927     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1928     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1929     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1930     //
1931     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1932     result = conn->Query(oss.str().c_str());
1933     //
1934     if ( !result ) throw -4;
1935     //
1936     row = result->Next();
1937     //
1938     if ( !row ){
1939     //
1940     // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
1941     //
1942     if ( IsDebug() ) printf(" The run is new \n");
1943     if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
1944     //
1945     glrun->SetID(this->AssignRunID());
1946     glrun->SetID_RUN_FRAG(0);
1947     glrun->Fill_GL_RUN_FRAGMENTS(conn);
1948     //
1949     } else {
1950     if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
1951 mocchiut 1.23 return;
1952 mocchiut 1.16 };
1953     //
1954     //
1955 mocchiut 1.1 // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
1956     //
1957     if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
1958     // missing it no way we can found a piece in the frag table
1959     //
1960     oss.str("");
1961     oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1962     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1963 mocchiut 1.16 << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1964     << " ID != " << glrun->ID
1965 mocchiut 1.1 << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1966     //
1967     if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
1968     result = conn->Query(oss.str().c_str());
1969     //
1970     if ( !result ) throw -4;
1971     //
1972     row = result->Next();
1973     //
1974 mocchiut 1.16 if ( !row && NoFrag() ){
1975     //
1976     oss.str("");
1977     oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
1978     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1979     << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1980     << " ID != " << glrun->ID
1981     << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1982     //
1983     if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1984     result = conn->Query(oss.str().c_str());
1985     //
1986     if ( !result ) throw -4;
1987     //
1988     foundinrun = true;
1989     //
1990     row = result->Next();
1991     //
1992     };
1993     //
1994 mocchiut 1.1 if ( !row ){
1995     if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1996     found = false;
1997     } else {
1998     //
1999     found = false; // default value
2000     //
2001     if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
2002     //
2003     // if we have both runheader and runtrailer we can check with pkt_counter:
2004     //
2005     if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
2006     ULong64_t chkpkt = 0;
2007     ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
2008     ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
2009     //
2010     chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;
2011     //
2012     if ( labs(chkpkt-pktt)<2 ){
2013     //
2014     if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2015     //
2016     found = true;
2017     //
2018     } else {
2019     //
2020     if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
2021     //
2022     found = false;
2023     //
2024     };
2025     };
2026     if ( !found ){
2027     //
2028     // 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
2029     //
2030     ULong64_t chkpkt1 = 0;
2031     ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2032     ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2033     chkpkt1 = labs(orunh1-dbrunt1);
2034     //
2035     ULong64_t chkpkt2 = 0;
2036     ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
2037     ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2038     chkpkt2 = labs(orunh2-dbrunt2);
2039     //
2040     ULong64_t chkpkt3 = 0;
2041     ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
2042     ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2043     chkpkt3 = labs(orunh3-dbrunt3);
2044     //
2045     if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2046     // if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
2047     //
2048     if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2049     //
2050     found = true;
2051     //
2052     } else {
2053     //
2054     if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2055     //
2056     found = false;
2057     //
2058     };
2059     };
2060     };
2061     //
2062     if ( found ){
2063     //
2064     // 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
2065     //
2066     if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2067     //
2068 mocchiut 1.16 if ( foundinrun ){
2069     glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2070     glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2071     };
2072     //
2073 mocchiut 1.1 GL_RUN *glrun1 = new GL_RUN();
2074     //
2075 mocchiut 1.16 // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2076 mocchiut 1.1 //
2077     oss.str("");
2078     oss << " ID="<<row->GetField(0)<<";";
2079     //
2080     glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
2081     //
2082     // merge infos
2083     //
2084     UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
2085     ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
2086     UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
2087     ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
2088 mocchiut 1.35 if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2089 mocchiut 1.1 TTree *T= 0;
2090     T = (TTree*)file->Get("Physics");
2091     if ( !T || T->IsZombie() ) throw -16;
2092     EventHeader *eh = 0;
2093     PscuHeader *ph = 0;
2094     T->SetBranchAddress("Header", &eh);
2095     while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
2096     T->GetEntry(firstev);
2097     ph = eh->GetPscuHeader();
2098     bpkt = PKT(ph->GetCounter());
2099     bobt = OBT(ph->GetOrbitalTime());
2100     firstev++;
2101     };
2102 mocchiut 1.35 if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2103 mocchiut 1.1 //
2104     glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2105     glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
2106     glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
2107     glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
2108     glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
2109     //
2110     glrun->SetEV_FROM(firstev);
2111     glrun->SetNEVENTS(lastev-firstev+1);
2112 mocchiut 1.16 //
2113 mocchiut 1.1 glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
2114     glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
2115     glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
2116     glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
2117     glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
2118     glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
2119     glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
2120     glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
2121     glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
2122     glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
2123     glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());
2124     glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());
2125     glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());
2126     glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());
2127     glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());
2128     glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());
2129     //
2130     if ( !IsRunAlreadyInserted() ){
2131     //
2132 mocchiut 1.16 // glrun->SetID(this->AssignRunID());
2133 mocchiut 1.13 glrun->SetID_RUN_FRAG(glrun1->GetID());
2134 mocchiut 1.1 glrun->Fill_GL_RUN(conn);
2135     //
2136 mocchiut 1.16 // set id number
2137 mocchiut 1.1 //
2138 mocchiut 1.13 glrun1->SetID_RUN_FRAG(glrun->GetID());
2139 mocchiut 1.1 glrun1->Fill_GL_RUN(conn);
2140     //
2141     };
2142 mocchiut 1.16 // delete old entry in fragment table
2143     //
2144     glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2145     glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2146 mocchiut 1.1 //
2147     delete glrun1;
2148     //
2149     //
2150     return;
2151     //
2152     };
2153     //
2154     };
2155     //
2156     if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
2157 mocchiut 1.16 // missing it no way we can found a piece in the frag table
2158 mocchiut 1.1 //
2159     oss.str("");
2160     oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "
2161     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2162 mocchiut 1.16 << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2163     << " ID != " << glrun->ID
2164 mocchiut 1.1 << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2165     //
2166     if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
2167     result = conn->Query(oss.str().c_str());
2168     //
2169     if ( !result ) throw -4;
2170     //
2171     row = result->Next();
2172     //
2173 mocchiut 1.16 if ( !row && NoFrag() ){
2174     //
2175     oss.str("");
2176     oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
2177     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2178     << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2179     << " ID != " << glrun->ID
2180     << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2181     //
2182     if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2183     result = conn->Query(oss.str().c_str());
2184     //
2185     if ( !result ) throw -4;
2186     //
2187     foundinrun = true;
2188     row = result->Next();
2189     //
2190     };
2191     //
2192 mocchiut 1.1 if ( !row ){
2193     if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2194     found = false;
2195     } else {
2196     //
2197     found = false; // default value
2198     //
2199     if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
2200     //
2201     // if we have both runheader and runtrailer we can check with pkt_counter:
2202     //
2203     if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){
2204     ULong64_t chkpkt = 0;
2205     ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
2206     ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2207     //
2208     chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;
2209     //
2210     if ( labs(chkpkt-pktt)<2 ){
2211     //
2212     if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2213     //
2214     found = true;
2215     //
2216     } else {
2217     //
2218     if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
2219     //
2220     found = false;
2221     //
2222     };
2223     };
2224     if ( !found ){
2225     //
2226     // 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
2227     //
2228     ULong64_t chkpkt1 = 0;
2229     ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
2230     ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2231     chkpkt1 = labs(orunh1-dbrunt1);
2232     //
2233     ULong64_t chkpkt2 = 0;
2234     ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT());
2235     ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2236     chkpkt2 = labs(orunh2-dbrunt2);
2237     //
2238     ULong64_t chkpkt3 = 0;
2239     ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME());
2240     ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2241     chkpkt3 = labs(orunh3-dbrunt3);
2242     //
2243     if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2244     //
2245     if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2246     //
2247     found = true;
2248     //
2249     } else {
2250     //
2251     if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2252     //
2253     found = false;
2254     //
2255     };
2256     };
2257     };
2258     //
2259     if ( found ){
2260     //
2261     // 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
2262     //
2263     if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2264     //
2265 mocchiut 1.16 if ( foundinrun ){
2266     glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2267     glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2268     };
2269     //
2270 mocchiut 1.1 GL_RUN *glrun1 = new GL_RUN();
2271     //
2272 mocchiut 1.16 // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2273 mocchiut 1.1 //
2274     oss.str("");
2275     oss << " ID="<<row->GetField(0)<<";";
2276     //
2277     glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runtrailer infos
2278     //
2279     // merge infos
2280     //
2281     UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT());
2282     ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
2283     UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
2284     ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
2285 mocchiut 1.35 if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2286 mocchiut 1.1 TTree *T= 0;
2287     T = (TTree*)file->Get("Physics");
2288     if ( !T || T->IsZombie() ) throw -16;
2289     EventHeader *eh = 0;
2290     PscuHeader *ph = 0;
2291     T->SetBranchAddress("Header", &eh);
2292     while ( apkt > bpkt && aobt > bobt && lastev > 0 ){
2293     T->GetEntry(lastev);
2294     ph = eh->GetPscuHeader();
2295     apkt = PKT(ph->GetCounter());
2296     aobt = OBT(ph->GetOrbitalTime());
2297     lastev--;
2298     };
2299 mocchiut 1.35 if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2300 mocchiut 1.1 //
2301     glrun->SetEV_TO(lastev);
2302     glrun->SetNEVENTS(lastev-firstev+1);
2303     glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());
2304     glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER());
2305     glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME());
2306     glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
2307     glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
2308     //
2309     glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
2310     glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
2311     glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
2312     glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP());
2313     glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE());
2314     glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE());
2315     glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A());
2316     glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B());
2317     glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO());
2318     glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO());
2319     glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB());
2320     glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE());
2321     glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED());
2322     glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK());
2323     glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC());
2324     glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC());
2325     //
2326     if ( !IsRunAlreadyInserted() ){
2327     //
2328 mocchiut 1.16 // glrun->SetID(this->AssignRunID());
2329 mocchiut 1.13 //
2330     glrun->SetID_RUN_FRAG(glrun1->GetID());
2331 mocchiut 1.1 glrun->Fill_GL_RUN(conn);
2332     //
2333 mocchiut 1.16 // set id number
2334 mocchiut 1.1 //
2335 mocchiut 1.13 glrun1->SetID_RUN_FRAG(glrun->GetID());
2336 mocchiut 1.1 glrun1->Fill_GL_RUN(conn);
2337     //
2338     };
2339     //
2340 mocchiut 1.16 // delete old entries in fragment table
2341 mocchiut 1.1 //
2342 mocchiut 1.16 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2343     glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2344 mocchiut 1.1 //
2345 mocchiut 1.16 delete glrun1;
2346 mocchiut 1.1 //
2347     return;
2348     //
2349     };
2350     //
2351     };
2352     //
2353     if ( !found ){
2354     //
2355     if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
2356     //
2357     // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
2358     //
2359     oss.str("");
2360     oss << " SELECT ID FROM GL_RUN WHERE "
2361     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2362     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2363     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2364     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2365     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2366     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2367     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2368     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2369     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2370     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2371     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2372     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2373     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2374     //
2375     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
2376     result = conn->Query(oss.str().c_str());
2377     //
2378     if ( !result ) throw -4;
2379     //
2380     row = result->Next();
2381     //
2382 mocchiut 1.16 if ( row ){
2383     if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
2384 mocchiut 1.1 } else {
2385 mocchiut 1.16 if ( NoFrag() ){
2386     glrun->SetID_RUN_FRAG(glrun->GetID());
2387     glrun->Fill_GL_RUN(conn);
2388     glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2389     };
2390 mocchiut 1.1 };
2391     };
2392     //
2393     return;
2394     };
2395    
2396    
2397     /**
2398     * Handle run without header or trailer
2399     **/
2400     void PamelaDBOperations::HandleMissingHoT(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
2401     //
2402     //
2403     // is the piece of run good (no other packets inside)?
2404     //
2405     if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
2406     //
2407     // if not, handle other pieces and continue with the first one
2408     //
2409     if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
2410     //
2411     } else {
2412     //
2413     this->FillClass(mishead,mistrail,firstev,lastev);
2414     //
2415 mocchiut 1.13 if ( !IsRunAlreadyInserted() ){
2416     glrun->SetID(this->AssignRunID());
2417     glrun->SetID_RUN_FRAG(0);
2418     glrun->Fill_GL_RUN(conn);
2419     };
2420 mocchiut 1.1 //
2421     };
2422     //
2423     return;
2424     };
2425    
2426     /**
2427     *
2428     * check if we have non-physics packets inside the run
2429     *
2430     */
2431     Bool_t PamelaDBOperations::IsRunConsistent(Bool_t mishead, Bool_t mistrail, UInt_t &firstev, UInt_t &lastev){
2432     //
2433     EventCounter *code=0;
2434     //
2435     UInt_t nevent = 0;
2436     UInt_t checkfirst = 0;
2437     UInt_t checklast = 0;
2438     UInt_t firstentry = 0;
2439     UInt_t lastentry = 0;
2440     UInt_t firstTime = 0;
2441     UInt_t lastTime = 0;
2442     UInt_t firstPkt = 0;
2443     UInt_t lastPkt = 0;
2444     UInt_t firstObt = 0;
2445     UInt_t lastObt = 0;
2446     //
2447     pcksList packetsNames;
2448     pcksList::iterator Iter;
2449     getPacketsNames(packetsNames);
2450     //
2451     TTree *T= 0;
2452     T =(TTree*)file->Get("Physics");
2453     if ( !T || T->IsZombie() ) throw -16;
2454     EventHeader *eh = 0;
2455     PscuHeader *ph = 0;
2456     T->SetBranchAddress("Header", &eh);
2457     nevent = T->GetEntries();
2458     //
2459     //
2460     if ( firstev == lastev+1 ) { // no events inside the run!
2461     if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2462     // return true is correct
2463     return(true);
2464     //
2465     } else {
2466     //
2467     T->GetEntry(firstev);
2468     code = eh->GetCounter();
2469     checkfirst = 0;
2470     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2471     if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
2472     };
2473     if ( IsDebug() ) printf(" Check first is %i firstev is %i\n",checkfirst,firstev);
2474     //
2475     T->GetEntry(lastev);
2476     code = eh->GetCounter();
2477     checklast = 0;
2478     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2479     if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
2480     };
2481     if ( IsDebug() ) printf(" Check last is %i lastev is %i\n",checklast,lastev);
2482     //
2483     if ( checkfirst == checklast ){
2484     //
2485     if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2486     //
2487     return(true);
2488     //
2489     } else {
2490     //
2491     if ( IsDebug() ) printf(" There are no-phyics packets inside the run!\n");
2492     //
2493     // HERE WE MUST HANDLE THAT RUNS AND GO BACK
2494     //
2495     if ( IsDebug() ) printf(" Never seen this case, try to handle it anyway, it was throw -95\n");
2496     //
2497     Bool_t emptyruns = false;
2498     UInt_t check = 0;
2499     UInt_t lastevtemp = lastev;
2500     UInt_t firstevno = firstev;
2501     //
2502     for (UInt_t i=firstev; i<=lastev; i++){
2503     //
2504     T->GetEntry(i);
2505     code = eh->GetCounter();
2506     //
2507     check = 0;
2508     //
2509     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2510     if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
2511     };
2512     //
2513     if ( checkfirst < check || i == lastev ){
2514     //
2515     firstentry = firstevno;
2516     //
2517     if ( checkfirst < check ){
2518     lastentry = i-1;
2519     } else {
2520     lastentry = i;
2521     };
2522     //
2523     if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2524     //
2525     glrun->SetEV_FROM(firstentry);
2526     glrun->SetEV_TO(lastentry);
2527     if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
2528     if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
2529     lastentry--;
2530     };
2531     glrun->SetNEVENTS(lastentry-firstentry+1);
2532     //
2533     glrun->Set_GL_RUNH0();
2534     glrun->Set_GL_RUNT0();
2535     //
2536     glrun->SetLAST_TIMESYNC(0);
2537     glrun->SetOBT_TIMESYNC(0);
2538     //
2539     T->GetEntry(firstentry);
2540     ph = eh->GetPscuHeader();
2541     firstObt = ph->GetOrbitalTime();
2542     firstTime = this->GetAbsTime(firstObt);
2543     firstPkt = ph->GetCounter();
2544     //
2545     T->GetEntry(lastentry);
2546     ph = eh->GetPscuHeader();
2547     lastObt = ph->GetOrbitalTime();
2548     lastTime = this->GetAbsTime(lastObt);
2549     lastPkt = ph->GetCounter();
2550     //
2551     glrun->SetRUNHEADER_PKT(firstPkt);
2552     glrun->SetRUNTRAILER_PKT(lastPkt);
2553     //
2554     glrun->SetRUNHEADER_OBT(firstObt);
2555     glrun->SetRUNTRAILER_OBT(lastObt);
2556     //
2557     if ( firstev == firstentry && !emptyruns && !mishead ){
2558     glrun->Set_GL_RUNH(runh,phh);
2559     firstTime = this->GetAbsTime(phh->GetOrbitalTime());
2560     if ( IsDebug() ) printf(" We have the runheader \n");
2561     };
2562     if ( lastev == i && !mistrail ){
2563     glrun->Set_GL_RUNT(runt,pht);
2564     lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2565     if ( IsDebug() ) printf(" We have the runtrailer \n");
2566     };
2567     //
2568     if ( lastentry == (firstentry-2) ){ // no events in the run
2569     emptyruns = true;
2570     if ( IsDebug() ) printf(" No events in the run \n");
2571     lastTime = firstTime;
2572     if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
2573     lastObt = glrun->RUNHEADER_OBT;
2574     lastPkt = glrun->RUNHEADER_PKT;
2575     } else {
2576     lastObt = firstObt;
2577     lastPkt = firstPkt;
2578     };
2579     glrun->SetRUNTRAILER_PKT(lastPkt);
2580     glrun->SetRUNTRAILER_OBT(lastObt);
2581     lastentry++;
2582     };
2583     //
2584     this->SetCommonGLRUN(firstTime,lastTime);
2585     //
2586 mocchiut 1.13 if ( !IsRunAlreadyInserted() ){
2587     glrun->SetID(this->AssignRunID());
2588     glrun->SetID_RUN_FRAG(0);
2589     glrun->Fill_GL_RUN(conn);
2590     };
2591 mocchiut 1.1 //
2592     firstevno = lastentry + 1;
2593     //
2594     checkfirst = check;
2595     //
2596     };
2597     //
2598     if ( check == checklast && i != lastev ){
2599     lastevtemp = i - 1;
2600     i = lastev - 1;
2601     };
2602     //
2603     };
2604     //
2605     lastev = lastevtemp;
2606     //
2607     return(false);
2608     //
2609     };
2610     };
2611     //
2612     return(false); // should never arrive here
2613     };
2614    
2615     /**
2616     *
2617     * 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
2618     * 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
2619     * looking for non-physics packets inside.
2620     *
2621     */
2622     void PamelaDBOperations::HandleSuspiciousRun(){
2623     //
2624     PacketType *pctp=0;
2625     EventCounter *codt=0;
2626     EventCounter *codh=0;
2627     EventCounter *code=0;
2628     UInt_t firstev = 0;
2629     UInt_t lastev = 0;
2630     UInt_t nevent = 0;
2631     UInt_t checkfirst = 0;
2632     UInt_t checklast = 0;
2633     UInt_t firstentry = 0;
2634     UInt_t lastentry = 0;
2635     UInt_t firstTime = 0;
2636     UInt_t lastTime = 0;
2637     UInt_t firstPkt = 0;
2638     UInt_t lastPkt = 0;
2639     UInt_t firstObt = 0;
2640     UInt_t lastObt = 0;
2641     //
2642     pcksList packetsNames;
2643     pcksList::iterator Iter;
2644     getPacketsNames(packetsNames);
2645     //
2646     TTree *rh=0;
2647     rh = (TTree*)file->Get("RunHeader");
2648     if ( !rh || rh->IsZombie() ) throw -17;
2649     TTree *T=0;
2650     T =(TTree*)file->Get("Physics");
2651     if ( !T || T->IsZombie() ) throw -16;
2652     EventHeader *eh = 0;
2653     PscuHeader *ph = 0;
2654     T->SetBranchAddress("Header", &eh);
2655     nevent = T->GetEntries();
2656     //
2657     codt = eht->GetCounter();
2658     codh = ehh->GetCounter();
2659     firstev = codh->Get(pctp->Physics);
2660     lastev = codt->Get(pctp->Physics)-1;
2661     //
2662     if ( firstev == lastev+1 ) { // no events inside the run!
2663     if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2664     //
2665     this->FillClass();
2666 mocchiut 1.13 if ( !IsRunAlreadyInserted() ){
2667     glrun->SetID(this->AssignRunID());
2668     glrun->SetID_RUN_FRAG(0);
2669     glrun->Fill_GL_RUN(conn);
2670     };
2671 mocchiut 1.1 //
2672     } else {
2673     //
2674     UInt_t nrunh = 0;
2675     UInt_t nrunh1 = 0;
2676     T->GetEntry(firstev);
2677     code = eh->GetCounter();
2678     checkfirst = 0;
2679     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2680     if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
2681     if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;
2682     };
2683     if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);
2684     //
2685     T->GetEntry(lastev);
2686     code = eh->GetCounter();
2687     checklast = 0;
2688     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2689     if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
2690     };
2691     if ( IsDebug() ) printf(" Check last is %i \n",checklast);
2692     //
2693     if ( checkfirst == checklast ){
2694     //
2695     if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2696     //
2697     this->FillClass();
2698 mocchiut 1.13 if ( !IsRunAlreadyInserted() ){
2699     glrun->SetID(this->AssignRunID());
2700     glrun->SetID_RUN_FRAG(0);
2701     glrun->Fill_GL_RUN(conn);
2702     };
2703 mocchiut 1.1 //
2704     } else {
2705     //
2706     if ( IsDebug() ) printf(" There are no-physics packets inside the run, try to separate runs \n");
2707     //
2708     Bool_t emptyruns = false;
2709     UInt_t check = 0;
2710     UInt_t firstevno = firstev;
2711     //
2712     for (UInt_t i=firstev; i<=lastev; i++){
2713     //
2714     T->GetEntry(i);
2715     code = eh->GetCounter();
2716     //
2717     check = 0;
2718     //
2719     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2720     if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
2721     if ( !strcmp(*Iter,"RunHeader") ) nrunh++;
2722     };
2723     //
2724     if ( checkfirst < check || i == lastev ){
2725     //
2726     firstentry = firstevno;
2727     //
2728     if ( checkfirst < check ){
2729     lastentry = i-1;
2730     } else {
2731     lastentry = i;
2732     };
2733     //
2734     if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2735     //
2736     glrun->SetEV_FROM(firstentry);
2737     glrun->SetEV_TO(lastentry);
2738     if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
2739     if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
2740     lastentry--;
2741     };
2742     glrun->SetNEVENTS(lastentry-firstentry+1);
2743     //
2744     glrun->Set_GL_RUNH0();
2745     glrun->Set_GL_RUNT0();
2746     //
2747     glrun->SetLAST_TIMESYNC(0);
2748     glrun->SetOBT_TIMESYNC(0);
2749     //
2750     T->GetEntry(firstentry);
2751     ph = eh->GetPscuHeader();
2752     firstObt = ph->GetOrbitalTime();
2753     firstTime = this->GetAbsTime(firstObt);
2754     firstPkt = ph->GetCounter();
2755     //
2756     T->GetEntry(lastentry);
2757     ph = eh->GetPscuHeader();
2758     lastObt = ph->GetOrbitalTime();
2759     lastTime = this->GetAbsTime(lastObt);
2760     lastPkt = ph->GetCounter();
2761     //
2762     glrun->SetRUNHEADER_PKT(firstPkt);
2763     glrun->SetRUNTRAILER_PKT(lastPkt);
2764     //
2765     glrun->SetRUNHEADER_OBT(firstObt);
2766     glrun->SetRUNTRAILER_OBT(lastObt);
2767     //
2768     if ( (firstev == firstentry && !emptyruns) || nrunh == (nrunh1 + 1) ){
2769     rh->GetEntry(nrunh1-1);
2770     phh = ehh->GetPscuHeader();
2771     nrunh1++;
2772     glrun->Set_GL_RUNH(runh,phh);
2773     firstTime = this->GetAbsTime(phh->GetOrbitalTime());
2774     if ( IsDebug() ) printf(" We have the runheader \n");
2775     };
2776     if ( lastev == i && checkfirst == check ){
2777     glrun->Set_GL_RUNT(runt,pht);
2778     lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2779     if ( IsDebug() ) printf(" We have the runtrailer \n");
2780     };
2781     //
2782     if ( lastentry == (firstentry-2) ){ // no events in the run
2783     emptyruns = true;
2784     if ( IsDebug() ) printf(" No events in the run \n");
2785     lastTime = firstTime;
2786     if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
2787     lastObt = glrun->RUNHEADER_OBT;
2788     lastPkt = glrun->RUNHEADER_PKT;
2789     } else {
2790     lastObt = firstObt;
2791     lastPkt = firstPkt;
2792     };
2793     glrun->SetRUNTRAILER_PKT(lastPkt);
2794     glrun->SetRUNTRAILER_OBT(lastObt);
2795     lastentry++;
2796     };
2797     //
2798     this->SetCommonGLRUN(firstTime,lastTime);
2799     //
2800 mocchiut 1.13 if ( !IsRunAlreadyInserted() ){
2801     glrun->SetID(this->AssignRunID());
2802     glrun->SetID_RUN_FRAG(0);
2803     glrun->Fill_GL_RUN(conn);
2804     };
2805 mocchiut 1.1 //
2806     if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check...
2807     //
2808     firstentry = i;
2809     //
2810     lastentry = i;
2811     //
2812     if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2813     //
2814     glrun->SetEV_FROM(firstentry);
2815     glrun->SetEV_TO(lastentry);
2816     glrun->SetNEVENTS(lastentry-firstentry+1);
2817     //
2818     glrun->Set_GL_RUNH0();
2819     //
2820     glrun->SetLAST_TIMESYNC(0);
2821     glrun->SetOBT_TIMESYNC(0);
2822     //
2823     T->GetEntry(firstentry);
2824     ph = eh->GetPscuHeader();
2825     firstObt = ph->GetOrbitalTime();
2826     firstTime = this->GetAbsTime(firstObt);
2827     firstPkt = ph->GetCounter();
2828     //
2829     glrun->SetRUNHEADER_PKT(firstPkt);
2830     //
2831     glrun->SetRUNHEADER_OBT(firstObt);
2832     //
2833     glrun->Set_GL_RUNT(runt,pht);
2834     lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2835     if ( IsDebug() ) printf(" We have the runtrailer \n");
2836     //
2837     this->SetCommonGLRUN(firstTime,lastTime);
2838     //
2839 mocchiut 1.13 if ( !IsRunAlreadyInserted() ){
2840     glrun->SetID(this->AssignRunID());
2841     glrun->SetID_RUN_FRAG(0);
2842     glrun->Fill_GL_RUN(conn);
2843     };
2844 mocchiut 1.1 };
2845     //
2846     firstevno = lastentry + 1;
2847     //
2848     checkfirst = check;
2849     //
2850     };
2851     //
2852     if ( check == checklast && i != lastev ) i = lastev - 1; // >>>>>>>>>>>>>>>>>>>>>>
2853     //
2854     };
2855     };
2856     };
2857     //
2858     return;
2859     };
2860    
2861    
2862     /**
2863     * Scan calorimeter calibrations packets, fill the GL_CALO_CALIB table
2864     */
2865     Int_t PamelaDBOperations::insertCALO_CALIB(){
2866     //
2867     TSQLResult *result = 0;
2868     TSQLRow *row = 0;
2869     //
2870     stringstream oss;
2871     oss.str("");
2872     //
2873     CalibCalPedEvent *calibCalPed = 0;
2874     TTree *tr = 0;
2875     EventHeader *eh = 0;
2876     PscuHeader *ph = 0;
2877     //
2878     UInt_t nevents = 0;
2879     UInt_t fromtime = 0;
2880     UInt_t totime = 0;
2881     UInt_t obt = 0;
2882     UInt_t pkt = 0;
2883     //
2884     tr = (TTree*)file->Get("CalibCalPed");
2885     if ( !tr || tr->IsZombie() ) throw -21;
2886     //
2887     tr->SetBranchAddress("CalibCalPed", &calibCalPed);
2888     tr->SetBranchAddress("Header", &eh);
2889     nevents = tr->GetEntries();
2890     //
2891 mocchiut 1.2 if ( !nevents ) return(1);
2892 mocchiut 1.1 //
2893     for (UInt_t i=0; i < nevents; i++){
2894     tr->GetEntry(i);
2895     for (UInt_t section = 0; section < 4; section++){
2896     //
2897     if ( calibCalPed->cstwerr[section] ){
2898     valid = 1;
2899     if ( calibCalPed->cperror[section] ) valid = 0;
2900     ph = eh->GetPscuHeader();
2901     obt = ph->GetOrbitalTime();
2902     pkt = ph->GetCounter();
2903     fromtime = this->GetAbsTime(ph->GetOrbitalTime());
2904     if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
2905     //
2906 mocchiut 1.34 if ( IsDebug() ) printf(" Calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
2907 mocchiut 1.1 //
2908     // check if the calibration has already been inserted
2909     //
2910     oss.str("");
2911     oss << " SELECT ID FROM GL_CALO_CALIB WHERE "
2912     << " SECTION = "<< section << " AND "
2913     << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
2914     << " OBT = "<< obt << " AND "
2915     << " PKT = "<< pkt << ";";
2916     //
2917     if ( IsDebug() ) printf(" Check if the calo calibration has already been inserted: query is \n %s \n",oss.str().c_str());
2918     result = conn->Query(oss.str().c_str());
2919     //
2920     if ( !result ) throw -4;
2921     //
2922     row = result->Next();
2923     //
2924     if ( row ){
2925     //
2926     if ( IsDebug() ) printf(" Calo calibration already inserted in the DB\n");
2927     //
2928     } else {
2929     //
2930     // we have to insert a new calibration, check where to place it
2931     //
2932     oss.str("");
2933     oss << " SELECT ID,TO_TIME FROM GL_CALO_CALIB WHERE "
2934     << " SECTION = "<< section << " AND "
2935     << " FROM_TIME < "<< fromtime << " AND "
2936     << " TO_TIME > "<< fromtime << ";";
2937     //
2938     if ( IsDebug() ) printf(" Check where to place the calo calibration: query is \n %s \n",oss.str().c_str());
2939     result = conn->Query(oss.str().c_str());
2940     //
2941     if ( !result ) throw -4;
2942     //
2943     row = result->Next();
2944     //
2945     if ( !row ){
2946     //
2947     // no calibrations in the db contain our calibration
2948     //
2949     if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
2950 mocchiut 1.19 if ( fromtime < 1150871000 ){ //1150866904
2951 mocchiut 1.35 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
2952 mocchiut 1.1 fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
2953     };
2954     //
2955     oss.str("");
2956     oss << " SELECT FROM_TIME FROM GL_CALO_CALIB WHERE "
2957     << " SECTION = "<< section << " AND "
2958     << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
2959     //
2960     if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
2961     result = conn->Query(oss.str().c_str());
2962     //
2963     if ( !result ) throw -4;
2964     //
2965     row = result->Next();
2966     if ( !row ){
2967     totime = numeric_limits<UInt_t>::max();
2968     } else {
2969     totime = (UInt_t)atoll(row->GetField(0));
2970     };
2971     //
2972     } else {
2973     //
2974     // determine upper and lower limits and make space for the new calibration
2975     //
2976     totime = (UInt_t)atoll(row->GetField(1));
2977     //
2978     oss.str("");
2979     oss << " UPDATE GL_CALO_CALIB SET "
2980     << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
2981     << " ID = "<< row->GetField(0) << ";";
2982     //
2983     if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
2984     result = conn->Query(oss.str().c_str());
2985     //
2986     if ( !result ) throw -4;
2987     //
2988     };
2989     //
2990     oss.str("");
2991     oss << " INSERT INTO GL_CALO_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,OBT,PKT,BOOT_NUMBER,VALIDATION) "
2992     << " VALUES (NULL,' "
2993     << idroot << "','"
2994     << i << "','"
2995     << fromtime << "','"
2996     << totime << "','"
2997     << section << "','"
2998     << obt << "','"
2999     << pkt << "','"
3000     << this->GetBOOTnumber() << "','"
3001     << valid << "');";
3002     //
3003     if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
3004     //
3005     result = conn->Query(oss.str().c_str());
3006     //
3007     if ( !result ) throw -4;
3008     //
3009     };
3010     //
3011     } else {
3012     //
3013 mocchiut 1.34 if ( IsDebug() ) printf(" Repeated calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
3014 mocchiut 1.1 //
3015     };
3016     //
3017     };
3018     };
3019     };
3020     //
3021     return(0);
3022     };
3023    
3024     /**
3025     * Fill the GL_TRK_CALIB table
3026     */
3027     void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){
3028     //
3029     TSQLResult *result = 0;
3030     TSQLRow *row = 0;
3031     //
3032     stringstream oss;
3033     oss.str("");
3034     //
3035     UInt_t totime = 0;
3036     //
3037     if ( !pk1 && !pk2 ){
3038     if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n");
3039     };
3040     //
3041     // check if the calibration has already been inserted
3042     //
3043     oss.str("");
3044     oss << " SELECT ID FROM GL_TRK_CALIB WHERE "
3045     << " BOOT_NUMBER = "<< this->GetBOOTnumber(); //
3046     oss << " AND ( ( ";
3047     if ( pk1 ){
3048     oss << " OBT1 = "<< obt1 << " AND "
3049     << " PKT1 = "<< pkt1
3050     << " ) OR ( ";
3051     } else {
3052     oss << " PKT1 = "<< pkt2-1
3053     << " ) OR ( ";
3054     };
3055     if ( pk2 ){
3056     oss << " OBT2 = "<< obt2 << " AND "
3057     << " PKT2 = "<< pkt2;
3058     } else {
3059     oss << " PKT2 = "<< pkt1+1;
3060     };
3061     oss << " ) );";
3062     //
3063     if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str());
3064     result = conn->Query(oss.str().c_str());
3065     //
3066     if ( !result ) throw -4;
3067     //
3068     row = result->Next();
3069     //
3070     if ( row ){
3071     //
3072     if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");
3073     //
3074     } else {
3075     //
3076     // we have to insert a new calibration, check where to place it
3077     //
3078     oss.str("");
3079     oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "
3080     << " FROM_TIME < "<< fromtime << " AND "
3081     << " TO_TIME > "<< fromtime << ";";
3082     //
3083     if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());
3084     result = conn->Query(oss.str().c_str());
3085     //
3086     if ( !result ) throw -4;
3087     //
3088     row = result->Next();
3089     //
3090     if ( !row ){
3091     //
3092     // no calibrations in the db contain our calibration
3093     //
3094     if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
3095 mocchiut 1.19 if ( fromtime < 1150871000 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode
3096 mocchiut 1.1 //
3097     oss.str("");
3098     oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
3099     << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
3100     //
3101     if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
3102     result = conn->Query(oss.str().c_str());
3103     //
3104     if ( !result ) throw -4;
3105     //
3106     row = result->Next();
3107     if ( !row ){
3108     totime = numeric_limits<UInt_t>::max();
3109     } else {
3110     totime = (UInt_t)atoll(row->GetField(0));
3111     };
3112     //
3113     } else {
3114     //
3115     // determine upper and lower limits and make space for the new calibration
3116     //
3117     totime = (UInt_t)atoll(row->GetField(1));
3118     //
3119     oss.str("");
3120     oss << " UPDATE GL_TRK_CALIB SET "
3121     << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
3122     << " ID = "<< row->GetField(0) << ";";
3123     //
3124     if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str());
3125     result = conn->Query(oss.str().c_str());
3126     //
3127     if ( !result ) throw -4;
3128     //
3129     };
3130     //
3131     oss.str("");
3132     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) "
3133     << " VALUES (NULL,' "
3134     << idroot << "',";
3135     //
3136     if ( !pk1 ){
3137     oss << "NULL,";
3138     } else {
3139     oss << "'"
3140     << t1 << "',";
3141     };
3142     //
3143     if ( !pk2 ){
3144     oss << "NULL,'";
3145     } else {
3146     oss << "'"
3147     << t2 << "','";
3148     };
3149     //
3150     oss << fromtime << "','"
3151     << totime << "','"
3152     << obt1 << "','"
3153     << pkt1 << "','"
3154     << obt2 << "','"
3155     << pkt2 << "','"
3156     << this->GetBOOTnumber() << "','"
3157     << valid << "');";
3158     //
3159     if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());
3160     //
3161     result = conn->Query(oss.str().c_str());
3162     //
3163     if ( !result ) throw -4;
3164     //
3165     };
3166     //
3167     };
3168    
3169     /**
3170     * Scan tracker calibrations packets, fill the GL_TRK_CALIB table
3171     */
3172     Int_t PamelaDBOperations::insertTRK_CALIB(){
3173     //
3174     CalibTrk1Event *caltrk1 = 0;
3175     CalibTrk2Event *caltrk2 = 0;
3176     TTree *tr1 = 0;
3177     TTree *tr2 = 0;
3178     EventHeader *eh1 = 0;
3179     PscuHeader *ph1 = 0;
3180     EventHeader *eh2 = 0;
3181     PscuHeader *ph2 = 0;
3182     //
3183     PacketType *pctp=0;
3184     EventCounter *codt2=0;
3185     //
3186     Int_t nevents1 = 0;
3187     Int_t nevents2 = 0;
3188     //
3189     fromtime = 0;
3190     //
3191     obt1 = 0;
3192     pkt1 = 0;
3193     obt2 = 0;
3194     pkt2 = 0;
3195     //
3196     tr1 = (TTree*)file->Get("CalibTrk1");
3197     if ( !tr1 || tr1->IsZombie() ) throw -22;
3198     tr2 = (TTree*)file->Get("CalibTrk2");
3199     if ( !tr2 || tr2->IsZombie() ) throw -23;
3200     //
3201     tr1->SetBranchAddress("CalibTrk1", &caltrk1);
3202     tr1->SetBranchAddress("Header", &eh1);
3203     nevents1 = tr1->GetEntries();
3204     tr2->SetBranchAddress("CalibTrk2", &caltrk2);
3205     tr2->SetBranchAddress("Header", &eh2);
3206     nevents2 = tr2->GetEntries();
3207     //
3208 mocchiut 1.2 if ( !nevents1 && !nevents2 ) return(1);
3209 mocchiut 1.1 //
3210     t2 = -1;
3211     Int_t pret2 = 0;
3212     Int_t t2t1cal = 0;
3213     //
3214     for (t1=0; t1 < nevents1; t1++){
3215     //
3216     pret2 = t2;
3217     tr1->GetEntry(t1);
3218     //
3219     ph1 = eh1->GetPscuHeader();
3220     obt1 = ph1->GetOrbitalTime();
3221     pkt1 = ph1->GetCounter();
3222     fromtime = this->GetAbsTime(ph1->GetOrbitalTime());
3223     //
3224 pam-fi 1.22 // valid = 1;
3225     // //
3226     // if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1
3227 mocchiut 1.1 //
3228     //
3229     if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){
3230     //
3231 mocchiut 1.34 if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u \n",fromtime,obt1,pkt1);
3232 pam-fi 1.22 //
3233     valid = ValidateTrkCalib( caltrk1, eh1 );
3234     if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
3235 mocchiut 1.1 //
3236     // Do we have the second calibration packet?
3237     //
3238     while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1
3239     //
3240     t2++;
3241     //
3242     if ( t2 < nevents2 ){
3243     tr2->GetEntry(t2);
3244     codt2 = eh2->GetCounter();
3245     t2t1cal = codt2->Get(pctp->CalibTrk1);
3246     //
3247     ph2 = eh2->GetPscuHeader();
3248     obt2 = ph2->GetOrbitalTime();
3249     pkt2 = ph2->GetCounter();
3250     //
3251 pam-fi 1.22 // if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2
3252 mocchiut 1.1 //
3253     } else {
3254     //
3255     // running out of vector without finding the corresponding calibration, sig
3256     //
3257     pret2 = t2;
3258     obt2 = 0;
3259     pkt2 = pkt1+2;
3260     t2t1cal = t1+1;
3261     };
3262     if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
3263     //
3264     // running out of vector without finding the corresponding calibration, sig
3265     //
3266     pret2 = t2;
3267     obt2 = 0;
3268     pkt2 = pkt1+2;
3269     t2t1cal = t1+1;
3270     };
3271     //
3272     };
3273     //
3274 mocchiut 1.34 if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2);
3275 mocchiut 1.1 //
3276     // The calibration is good
3277     //
3278     if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){
3279     //
3280 mocchiut 1.34 if ( IsDebug() ) printf(" The trk calibration2 at obt %u pkt %u t2 is %u is good \n",obt2,pkt2,t2);
3281 mocchiut 1.1 //
3282 pam-fi 1.22 UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
3283     if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
3284     valid = valid & valid2;
3285     //
3286 mocchiut 1.1 // Handle good calib
3287     //
3288     this->HandleTRK_CALIB(true,true);
3289     //
3290     // Check for missing calibtrk1
3291     //
3292     if ( t2 != pret2+1 ){
3293     //
3294 mocchiut 1.34 if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u pret2 is %u \n",obt2,pkt2,t2,pret2);
3295 mocchiut 1.1 //
3296     while ( t2 > pret2+1 ){
3297     //
3298     // handle missing calib1
3299     //
3300     pret2++;
3301     //
3302     obt1 = 0;
3303     pkt1 = 0;
3304     //
3305     tr2->GetEntry(pret2);
3306     ph2 = eh2->GetPscuHeader();
3307     obt2 = ph2->GetOrbitalTime();
3308     pkt2 = ph2->GetCounter();
3309     //
3310     fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
3311     //
3312     valid = 0;
3313     this->HandleTRK_CALIB(false,true);
3314     //
3315     };
3316     //
3317     };
3318     //
3319     } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){
3320     //
3321     // Check for missing calibtrk2
3322     //
3323 mocchiut 1.34 if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
3324 mocchiut 1.1 t2 = pret2;
3325     //
3326     // handle missing calib2
3327     //
3328     obt2 = 0;
3329     pkt2 = 0;
3330     valid = 0;
3331     this->HandleTRK_CALIB(true,false);
3332     //
3333     };
3334     //
3335     } else {
3336     //
3337 mocchiut 1.34 if ( IsDebug() ) printf(" Repetead trk calibration1 at time %u obt %u pkt %u \n",fromtime,obt1,pkt1);
3338 mocchiut 1.1 //
3339     };
3340     //
3341     };
3342     //
3343     // we have one more calib pkt2 !
3344     //
3345     t2++;
3346     while ( t2 < nevents2 ){
3347     //
3348     // handle missing calib1
3349     //
3350     obt1 = 0;
3351     pkt1 = 0;
3352     //
3353     tr2->GetEntry(t2);
3354     ph2 = eh2->GetPscuHeader();
3355     obt2 = ph2->GetOrbitalTime();
3356     pkt2 = ph2->GetCounter();
3357     //
3358     fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
3359     valid = 0;
3360     if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){
3361     //
3362 mocchiut 1.34 if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
3363 mocchiut 1.1 //
3364     this->HandleTRK_CALIB(false,true);
3365     //
3366     };
3367     //
3368     t2++;
3369     //
3370     };
3371     //
3372     return(0);
3373     };
3374    
3375    
3376     /**
3377     * Scan S4 calibrations packets, fill the GL_S4_CALIB table
3378     */
3379     Int_t PamelaDBOperations::insertS4_CALIB(){
3380     //
3381     TSQLResult *result = 0;
3382     TSQLRow *row = 0;
3383     //
3384     stringstream oss;
3385     oss.str("");
3386     //
3387     TTree *tr = 0;
3388     EventHeader *eh = 0;
3389     PscuHeader *ph = 0;
3390     //
3391     UInt_t nevents = 0;
3392     UInt_t fromtime = 0;
3393     UInt_t totime = 0;
3394     UInt_t obt = 0;
3395     UInt_t pkt = 0;
3396     //
3397     tr = (TTree*)file->Get("CalibS4");
3398     if ( !tr || tr->IsZombie() ) throw -24;
3399     //
3400     tr->SetBranchAddress("Header", &eh);
3401     //
3402     nevents = tr->GetEntries();
3403     //
3404 mocchiut 1.2 if ( !nevents ) return(1);
3405 mocchiut 1.1 //
3406     for (UInt_t i = 0; i < nevents; i++){
3407     //
3408     tr->GetEntry(i);
3409     //
3410     ph = eh->GetPscuHeader();
3411     obt = ph->GetOrbitalTime();
3412     pkt = ph->GetCounter();
3413     fromtime = this->GetAbsTime(ph->GetOrbitalTime());
3414     if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
3415     //
3416 mocchiut 1.34 if ( IsDebug() ) printf(" S4 calibration at time %u obt %u pkt %u \n",fromtime,obt,pkt);
3417 mocchiut 1.1 //
3418     // check if the calibration has already been inserted
3419     //
3420     oss.str("");
3421     oss << " SELECT ID FROM GL_S4_CALIB WHERE "
3422     << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
3423     << " OBT = "<< obt << " AND "
3424     << " PKT = "<< pkt << ";";
3425     //
3426     if ( IsDebug() ) printf(" Check if the S4 calibration has already been inserted: query is \n %s \n",oss.str().c_str());
3427     result = conn->Query(oss.str().c_str());
3428     //
3429     if ( !result ) throw -4;
3430     //
3431     row = result->Next();
3432     //
3433     if ( row ){
3434     //
3435     if ( IsDebug() ) printf(" S4 calibration already inserted in the DB\n");
3436     //
3437     } else {
3438     //
3439     // we have to insert a new calibration, check where to place it
3440     //
3441     oss.str("");
3442     oss << " SELECT ID,TO_TIME FROM GL_S4_CALIB WHERE "
3443     << " FROM_TIME < "<< fromtime << " AND "
3444     << " TO_TIME > "<< fromtime << ";";
3445     //
3446     if ( IsDebug() ) printf(" Check where to place the S4 calibration: query is \n %s \n",oss.str().c_str());
3447     result = conn->Query(oss.str().c_str());
3448     //
3449     if ( !result ) throw -4;
3450     //
3451     row = result->Next();
3452     //
3453     if ( !row ){
3454     //
3455     // no calibrations in the db contain our calibration
3456     //
3457     if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");
3458 mocchiut 1.19 if ( fromtime < 1150871000 ){
3459 mocchiut 1.35 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
3460 mocchiut 1.1 fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
3461     };
3462     //
3463     oss.str("");
3464     oss << " SELECT FROM_TIME FROM GL_S4_CALIB WHERE "
3465     << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
3466     //
3467     if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
3468     result = conn->Query(oss.str().c_str());
3469     //
3470     if ( !result ) throw -4;
3471     //
3472     row = result->Next();
3473     if ( !row ){
3474     totime = numeric_limits<UInt_t>::max();
3475     } else {
3476     totime = (UInt_t)atoll(row->GetField(0));
3477     };
3478     //
3479     } else {
3480     //
3481     // determine upper and lower limits and make space for the new calibration
3482     //
3483     totime = (UInt_t)atoll(row->GetField(1));
3484     //
3485     oss.str("");
3486     oss << " UPDATE GL_S4_CALIB SET "
3487     << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
3488     << " ID = "<< row->GetField(0) << ";";
3489     //
3490     if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
3491     result = conn->Query(oss.str().c_str());
3492     //
3493     if ( !result ) throw -4;
3494     //
3495     };
3496     //
3497     oss.str("");
3498 mocchiut 1.2 oss << " INSERT INTO GL_S4_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,OBT,PKT,BOOT_NUMBER) "
3499 mocchiut 1.1 << " VALUES (NULL,' "
3500     << idroot << "','"
3501     << i << "','"
3502     << fromtime << "','"
3503     << totime << "','"
3504     << obt << "','"
3505     << pkt << "','"
3506 mocchiut 1.2 << this->GetBOOTnumber() << "');";
3507 mocchiut 1.1 //
3508     if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
3509     //
3510     result = conn->Query(oss.str().c_str());
3511     //
3512     if ( !result ) throw -4;
3513     //
3514     };
3515     //
3516     } else {
3517     //
3518 mocchiut 1.34 if ( IsDebug() ) printf(" Repeated S4 calibration at time %u obt %u pkt %u \n",fromtime,obt,pkt);
3519 mocchiut 1.1 //
3520     };
3521     //
3522     };
3523     //
3524     return(0);
3525     };
3526    
3527 mocchiut 1.2 /**
3528     * Scan the fragment table and move old fragments to the GL_RUN table
3529 mocchiut 1.1 */
3530 mocchiut 1.2 Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3531 mocchiut 1.16 return(this->CleanGL_RUN_FRAGMENTS(""));
3532     };
3533    
3534     /**
3535     * Scan the fragment table and move old fragments to the GL_RUN table
3536     */
3537     Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3538 mocchiut 1.2 //
3539     TSQLResult *result = 0;
3540     TSQLRow *row = 0;
3541     TSQLResult *result2 = 0;
3542     TSQLRow *row2 = 0;
3543     //
3544     UInt_t moved = 0;
3545     //
3546     stringstream oss;
3547     oss.str("");
3548     //
3549 mocchiut 1.16 if ( !strcmp(fcleanfile.Data(),"") ){
3550     //
3551     // check if there are entries older than "olderthan" seconds from now
3552     //
3553     oss.str("");
3554     oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3555     << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3556     //
3557     if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
3558     result = conn->Query(oss.str().c_str());
3559     //
3560     } else {
3561     oss.str("");
3562     oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3563     if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3564     result = conn->Query(oss.str().c_str());
3565     //
3566     if ( result ){
3567     //
3568     row = result->Next();
3569     //
3570     if ( row ){
3571     oss.str("");
3572     oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3573     << " ID_ROOT_L0=" << row->GetField(0) << ";";
3574     //
3575     if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3576     result = conn->Query(oss.str().c_str());
3577     //
3578     };
3579     } else {
3580     return(2);
3581     };
3582     };
3583 mocchiut 1.2 //
3584     if ( result ){
3585     //
3586     row = result->Next();
3587     //
3588 mocchiut 1.12 while ( row ){
3589 mocchiut 1.2 //
3590     oss.str("");
3591     oss << " ID= "<< row->GetField(0);
3592     //
3593     glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
3594     //
3595     oss.str("");
3596     oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
3597     << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
3598     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3599     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3600     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3601     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3602     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3603     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3604     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3605     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3606     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3607     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3608     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3609     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3610     //
3611     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3612     result2 = conn->Query(oss.str().c_str());
3613     //
3614     if ( !result2 ) throw -4;
3615     //
3616     row2 = result2->Next();
3617     //
3618     if ( !row2 ){
3619     //
3620     if ( IsDebug() ) printf(" The run is new \n");
3621     if ( IsDebug() ) printf(" -> fill the DB \n");
3622     //
3623 mocchiut 1.13 // glrun->SetID(this->AssignRunID()); we use the old run number!
3624     glrun->SetID_RUN_FRAG(glrun->GetID());
3625 mocchiut 1.2 glrun->Fill_GL_RUN(conn);
3626     //
3627 mocchiut 1.13 // oss.str("");
3628     // oss << " SELECT ID FROM GL_RUN WHERE "
3629     // << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3630     // << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3631     // << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3632     // << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3633     // << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3634     // //
3635     // if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
3636     // result2 = conn->Query(oss.str().c_str());
3637     // //
3638     // if ( !result2 ) throw -4;
3639     // //
3640     // row2 = result2->Next();
3641     // //
3642     // if ( !row2 ) throw -25;
3643     // //
3644     // oss.str("");
3645     // oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);
3646     // if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());
3647     // result2 = conn->Query(oss.str().c_str());
3648     // //
3649     // if ( !result2 ) throw -4;
3650 mocchiut 1.2 //
3651     moved++;
3652     //
3653     } else {
3654     if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");
3655 mocchiut 1.1 };
3656 mocchiut 1.2 if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));
3657     //
3658     //
3659 mocchiut 1.15 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3660     // oss.str("");
3661     // oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
3662     // if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
3663     // result2 = conn->Query(oss.str().c_str());
3664     // //
3665     // if ( !result2 ) throw -4;
3666     // //
3667 mocchiut 1.12 row = result->Next();
3668 mocchiut 1.1 };
3669     };
3670 mocchiut 1.2 if ( IsDebug() ) printf(" Moved %u runs\n",moved);
3671     return(0);
3672     };
3673 mocchiut 1.1
3674 mocchiut 1.2 /**
3675     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3676     */
3677     Int_t PamelaDBOperations::ValidateRuns(){
3678 mocchiut 1.16 return(this->ValidateRuns(""));
3679     };
3680    
3681     /**
3682     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3683     */
3684     Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3685 mocchiut 1.2 //
3686     TSQLResult *result = 0;
3687 pam-fi 1.5 TSQLRow *row = 0;
3688 mocchiut 1.2 //
3689 mocchiut 1.16 UInt_t calibtime = 50;
3690     //
3691 mocchiut 1.2 stringstream oss;
3692     oss.str("");
3693     //
3694 pam-fi 1.5 // =======================================================
3695     // validate runs by checking missing calibrations
3696     // =======================================================
3697     UInt_t t_stop = 0;
3698     UInt_t t_start = 0;
3699 mocchiut 1.16 if ( !strcmp(valfile.Data(),"") ) {
3700     // --------------------------------------------------------------
3701     // 1) get the OBT of the last run inserted after clean-time limit
3702     // --------------------------------------------------------------
3703     oss.str("");
3704     oss << " SELECT * FROM GL_RUN WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3705     << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3706     if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3707     result = conn->Query(oss.str().c_str());
3708     if ( !result ) throw -4;
3709     if ( !result->GetRowCount() ) {
3710     printf(" No runs to validate \n");
3711     return(1);
3712     }else{
3713     row = result->Next();
3714     t_start = (UInt_t)atoll(row->GetField(4));
3715     };
3716     // --------------------------------------------------------------
3717     // 2) get the OBT of the last validated run
3718     // --------------------------------------------------------------
3719     oss.str("");
3720     oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3721     <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3722     if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3723     result = conn->Query(oss.str().c_str());
3724     if ( !result ) throw -4;
3725     if ( result->GetRowCount() ){
3726     row = result->Next();
3727     t_stop = (UInt_t)atoll(row->GetField(4));
3728     };
3729 mocchiut 1.35 if ( IsDebug() ) printf("Validation interval: from time %u - to time %u \n\n",t_stop,t_start);
3730 mocchiut 1.16 // --------------------------------------------------------------
3731     // now retrieves runs to be validated
3732     // --------------------------------------------------------------
3733     oss.str("");
3734     oss << " SELECT * FROM GL_RUN WHERE RUNHEADER_TIME <=" << t_start;
3735     oss << " AND RUNHEADER_TIME >="<< t_stop;
3736     oss << " ORDER BY RUNHEADER_TIME DESC;";
3737     if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3738     result = conn->Query(oss.str().c_str());
3739     } else {
3740     //
3741     stringstream myquery;
3742     UInt_t myid = 0;
3743     myquery.str("");
3744     myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3745     //
3746     result = conn->Query(myquery.str().c_str());
3747     //
3748     row = result->Next();
3749     if( !row ){
3750     if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3751     if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3752     return(2);
3753     };
3754     if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3755     return(0);
3756     };
3757     myid=(UInt_t)atoll(row->GetField(0));
3758     //
3759     myquery.str("");
3760     myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3761     //
3762     result = conn->Query(myquery.str().c_str());
3763     //
3764     row = result->Next();
3765     if( !row->GetField(0) || !row->GetField(1)){
3766     //
3767     if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3768     //
3769     return(0);
3770     //
3771     } else {
3772     //
3773     UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3774     UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3775     UInt_t caltime = 0;
3776     //
3777     myquery.str("");
3778     myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3779     myquery << " order by FROM_TIME asc limit 1;";
3780     //
3781     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
3782     //
3783     //
3784     result = conn->Query(myquery.str().c_str());
3785     //
3786     row = result->Next();
3787     if( !row ){
3788     caltime = runhtime;
3789     } else {
3790     caltime = (UInt_t)atoll(row->GetField(0));
3791     };
3792     //
3793     myquery.str("");
3794     myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3795     myquery << caltime << " order by RUNHEADER_TIME DESC";
3796     //
3797     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
3798     //
3799     result = conn->Query(myquery.str().c_str());
3800     //
3801     };
3802 pam-fi 1.5 };
3803 mocchiut 1.16 //
3804 mocchiut 1.2 if ( !result ) throw -4;
3805 mocchiut 1.16 if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3806     //
3807 pam-fi 1.5 Int_t nrow = 0;
3808     GL_RUN* this_run = new GL_RUN();
3809     GL_RUN* next_run = new GL_RUN();
3810 pam-fi 1.8 Int_t nseq_max = 1000;
3811 pam-fi 1.5 // UInt_t* sequence = new UInt_t[100];
3812     vector<UInt_t> sequence(nseq_max);
3813     Int_t nseq = 0;
3814     Bool_t CHECK = false;
3815     Bool_t this_ONLINE = false;
3816     Bool_t next_ONLINE = false;
3817     UInt_t t1=0,t2=0;
3818     // ---------------------------------------------------------------------------------
3819     // - loop over runs, back in time,
3820 mocchiut 1.16 // - select sequences of runs close in time (less than calibtime s apart),
3821 pam-fi 1.5 // which could be preceeded by a calibration
3822     // - check if there might be a missing calibration
3823     // ---------------------------------------------------------------------------------
3824     while(1){
3825    
3826     row = result->Next();
3827     if( row == NULL ) break;
3828    
3829     //------------
3830     //get run info
3831     //------------
3832     this_run->Set_GL_RUN(row);
3833 pam-fi 1.8
3834 pam-fi 1.5 Bool_t this_BAD = false;
3835     if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;
3836     else if (this_run->GetTRK_CALIB_USED() == 104) this_ONLINE = false;
3837     else{
3838 pam-fi 1.8 // printf("Missing or corrupted header!! \n");
3839 pam-fi 1.5 this_ONLINE = false;
3840     this_BAD = true;
3841     };
3842    
3843     //-----------------------------------
3844     //compare with previous(next in time)
3845     //-----------------------------------
3846     CHECK = false;
3847     UInt_t interval=0;
3848    
3849     if( nrow != 0){
3850    
3851    
3852     t1 = this_run->GetRUNTRAILER_TIME();
3853     t2 = next_run->GetRUNHEADER_TIME();
3854     interval = (t2-t1);
3855    
3856     if(this_ONLINE && next_ONLINE){ // this: ON-LINE + next: ON-LINE
3857    
3858     if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0; //=> run fragments
3859    
3860 mocchiut 1.16 if( interval >= calibtime )CHECK = true; //more than calibtime s => there might be a calibration
3861 pam-fi 1.5
3862     if( !CHECK && this_run->VALIDATION ){
3863     for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
3864     nseq=0;
3865     }
3866    
3867     }else if( !this_ONLINE && next_ONLINE) { // this: DEFAULT + next:ON-LINE
3868    
3869     CHECK = true;
3870    
3871     }else if( !next_ONLINE ){ // this:ANY + next:DEFAULT
3872    
3873     assignVALIDATION(next_run->ID,true);
3874     nseq=0;
3875     }
3876     }
3877    
3878     //----------------------------
3879     //check run sequence for calib
3880     //----------------------------
3881     if( CHECK ){
3882     // check if calibration exists
3883 pam-fi 1.8 if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
3884 pam-fi 1.5 Bool_t MISSING = MissingTRK_CALIB(t1,t2);
3885     for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
3886     nseq=0;
3887 pam-fi 1.8 };
3888 pam-fi 1.5 //--------------
3889     //store run info
3890     //--------------
3891     *next_run = *this_run;
3892     next_ONLINE = this_ONLINE;
3893     if( !this_BAD ){
3894     if(nseq < nseq_max){
3895     sequence[nseq] = this_run->ID;
3896     nseq++;
3897     }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
3898     };
3899    
3900 mocchiut 1.35 if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);
3901 pam-fi 1.5 nrow++;
3902    
3903     };
3904     delete this_run;
3905     delete next_run;
3906 mocchiut 1.2 //
3907     return(0);
3908 mocchiut 1.1 };
3909 pam-fi 1.5 /**
3910     * Check if there might be a missing tracker calibration in a given time interval
3911     * @param t1 From absolute time
3912     * @param t2 To absolute time
3913     * @return true if there might be a missing calibration
3914     */
3915     Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
3916    
3917     GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
3918    
3919     // get the closest VALIDATED calibration before the run start (t2)
3920     if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true); //>>> missing
3921    
3922     if ( trkcalib->TO_TIME < t2 ) return(true); //>>> missing
3923    
3924     //==============================================================
3925     // Check is done first on the basis of time between calibration,
3926     // which should be equal to the time between ascending-nodes.
3927     //==============================================================
3928     if ( t2 - trkcalib->FROM_TIME > 5700) {
3929 mocchiut 1.35 if ( IsDebug() )printf("Long time between calib and run start %u :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);
3930 pam-fi 1.5 //==============================================================
3931     // there might be a missing calibration, due to:
3932     // - MM full
3933     // - corrupted packets
3934     // - loss of data
3935     // There is an exception in case a download was done during ascending node
3936     //==============================================================
3937     Bool_t DOWNLOAD = false;
3938     // check if the calib was skipped becouse of download .... DA FARE!!
3939     if(DOWNLOAD)return(false);
3940    
3941     return(true); //>>> missing
3942    
3943     };
3944    
3945     //==============================================================
3946     // If the last calibration is close to the run less than this time,
3947     // it is enough to say that there are no missing calibrations
3948     //==============================================================
3949     // the long time interval bewteen runs might be due to download
3950 mocchiut 1.35 if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
3951 pam-fi 1.5 return(false);
3952    
3953     };
3954     /**
3955     * Assign VALIDATION value to a GL_RUN entry
3956     * @param idrun Run ID
3957     * @param validation true/false
3958     */
3959     Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
3960     TSQLResult *result = 0;
3961     stringstream oss;
3962     oss.str("");
3963 pam-fi 1.8 oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
3964 pam-fi 1.5 //
3965     // if ( IsDebug() )
3966 pam-fi 1.8 // printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
3967     if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
3968     result = conn->Query(oss.str().c_str());
3969     if ( !result ) throw -4;
3970 pam-fi 1.5 return(0);
3971     }
3972    
3973    
3974    
3975 mocchiut 1.12 // Insert TLEs from file tlefilename in the table GL_TLE in the db
3976     // opened by conn, sorting them by date from older to newer, if each
3977     // TLE has not been alread inserted.
3978     Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
3979     {
3980     fstream tlefile(tlefilename, ios::in);
3981    
3982     if ( !tlefile ) throw -7;
3983    
3984     vector<cTle*> ctles;
3985     vector<cTle*>::iterator iter;
3986     int present = 0;
3987    
3988     // Get three lines from tlefile, create a cTle object and put it
3989     // into ctles
3990     while(1) {
3991     cTle *tlef;
3992     string str1, str2, str3;
3993    
3994     getline(tlefile, str1);
3995     if(tlefile.eof()) break;
3996    
3997     getline(tlefile, str2);
3998     if(tlefile.eof()) break;
3999    
4000     getline(tlefile, str3);
4001     if(tlefile.eof()) break;
4002    
4003     // We now have three good lines for a cTle.
4004     tlef = new cTle(str1, str2, str3);
4005     ctles.push_back(tlef);
4006     }
4007    
4008     tlefile.close();
4009    
4010     // Sort by date
4011     sort(ctles.begin(), ctles.end(), compTLE);
4012    
4013     // Now we insert each TLE into the db
4014     for(iter = ctles.begin(); iter != ctles.end(); iter++) {
4015     cTle *tle = *iter;
4016    
4017     // Do nothing if it's already present in the db. Just increase
4018     // the counter present.
4019     if (! isTlePresent(tle))
4020     {
4021     int status = insertTle(tle);
4022    
4023     // Insert query failed. Return 1.
4024     if(status == EXIT_FAILURE) {
4025    
4026     if( IsDebug() ) {
4027     cerr << "Error: inserting TLE:" << endl
4028     << tle->getName() << endl
4029     << tle->getLine1() << endl
4030     << tle->getLine2() << endl;
4031     }
4032    
4033     throw -4;
4034     return 1;
4035     }
4036    
4037     }
4038     else
4039     present++;
4040    
4041     }
4042    
4043     int inserted = ctles.size() - present; // Number of inserted TLE.
4044     if ( IsDebug() )
4045     cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
4046     << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
4047    
4048     ctles.clear();
4049    
4050    
4051     // Return 2 if no new TLE has been inserted. 0 otherwise.
4052     if(! inserted ) return 2;
4053     return 0;
4054     }
4055    
4056    
4057     // Insert tle in the table GL_TLE using the connection conn.
4058 mocchiut 1.16 Int_t PamelaDBOperations::insertTle(cTle *tle)
4059 mocchiut 1.12 {
4060     stringstream oss;
4061     TSQLResult *result = 0;
4062    
4063     oss.str("");
4064     oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
4065     << " VALUES ( '"
4066     << tle->getName() << "', '"
4067     << tle->getLine1() << "', '"
4068     << tle->getLine2() << "', '"
4069     << getTleDatetime(tle) << "')";
4070    
4071     // cout << oss.str().c_str() << endl;
4072     result = conn->Query(oss.str().c_str());
4073     if (result == NULL)
4074     return EXIT_FAILURE;
4075    
4076     return EXIT_SUCCESS;
4077     }
4078    
4079    
4080     // Return whether tle is already in the db connected by conn.
4081     bool PamelaDBOperations::isTlePresent(cTle *tle)
4082     {
4083     stringstream oss;
4084     TSQLResult *result = 0;
4085    
4086     oss.str("");
4087     oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
4088     << getTleDatetime(tle) << "'";
4089    
4090     result = conn->Query(oss.str().c_str());
4091     if (result == NULL) throw -4;
4092    
4093     if (result->GetRowCount())
4094     return true;
4095     else
4096     return false;
4097     }
4098    
4099    
4100     // Return whether the first TLE is dated early than the second
4101     bool compTLE (cTle *tle1, cTle *tle2)
4102     {
4103     return getTleJulian(tle1) < getTleJulian(tle2);
4104     }
4105    
4106    
4107     // Return the date of the tle using the format (year-2000)*1e3 +
4108     // julian day. e.g. 6365 is the 31th Dec 2006.
4109     // It does *not* return a cJulian date.
4110     float getTleJulian(cTle *tle) {
4111     return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
4112     }
4113    
4114    
4115     // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
4116     // format.
4117     string getTleDatetime(cTle *tle)
4118     {
4119     int year, mon, day, hh, mm, ss;
4120     double dom; // day of month (is double!)
4121     stringstream date; // date in datetime format
4122    
4123     // create a cJulian from the date in tle
4124     cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
4125    
4126     // get year, month, day of month
4127     jdate.getComponent(&year, &mon, &dom);
4128    
4129     // build a datetime YYYY-MM-DD hh:mm:ss
4130     date.str("");
4131     day = (int) floor(dom);
4132     hh = (int) floor( (dom - day) * 24);
4133     mm = (int) floor( ((dom - day) * 24 - hh) * 60);
4134     ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
4135     // ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
4136    
4137     date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
4138    
4139     return date.str();
4140     }
4141 mocchiut 1.16
4142     /**
4143     * Remove a file from the DB, delete on cascade all entries related to that file
4144     * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
4145     * calibration
4146     **/
4147     Int_t PamelaDBOperations::removeFile(TString remfile){
4148     //
4149     // Determine ID_ROOT_L0 and ID_RAW
4150     //
4151     TSQLResult *pResult;
4152     TSQLRow *Row;
4153     stringstream myquery;
4154     //
4155     myquery.str("");
4156     myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
4157     //
4158     pResult = conn->Query(myquery.str().c_str());
4159     //
4160     Row = pResult->Next();
4161     if( !Row ){
4162     if ( strcmp(remfile.Data(),GetRootName().Data()) ){
4163     if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
4164     return(1);
4165     };
4166     if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
4167     return(0);
4168     };
4169     //
4170     this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
4171     this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
4172     //
4173     this->ValidationOFF();
4174     //
4175     this->RemoveCALIBS();
4176     //
4177     this->RemoveRUNS();
4178     //
4179     this->RemoveFILES();
4180     //
4181     this->SetID_ROOT(0);
4182     this->SetID_RAW(0);
4183     //
4184     return(0);
4185     };
4186    
4187     /**
4188     *
4189     * Set validation bit to zero for runs following the removing file till
4190     * 1) a run with TRK_CALIB_USED=140
4191     * 2) a run with VALIDATION = 0
4192     * 3) the next calibration
4193     *
4194     **/
4195     void PamelaDBOperations::ValidationOFF(){
4196     TSQLResult *pResult;
4197     TSQLRow *Row;
4198     stringstream myquery;
4199     Int_t unv = 0;
4200     //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
4201     myquery.str("");
4202     myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4203     //
4204     pResult = conn->Query(myquery.str().c_str());
4205     //
4206     Row = pResult->Next();
4207     if( !Row->GetField(0) ){
4208     //
4209     if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
4210     //
4211     } else {
4212     //
4213     UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
4214     UInt_t caltime = 0;
4215     //
4216     myquery.str("");
4217     myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
4218     myquery << " order by FROM_TIME asc limit 1;";
4219     //
4220     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4221     //
4222     //
4223     delete pResult;
4224     pResult = conn->Query(myquery.str().c_str());
4225     //
4226     Row = pResult->Next();
4227     if( !Row ){
4228     caltime = runhtime;
4229     } else {
4230     caltime = (UInt_t)atoll(Row->GetField(0));
4231     };
4232     //
4233     myquery.str("");
4234     myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4235     myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4236     //
4237     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4238     //
4239     pResult = conn->Query(myquery.str().c_str());
4240     //
4241     Row = pResult->Next();
4242     if( !Row ){
4243     //
4244     if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4245     //
4246     } else {
4247     myquery.str("");
4248     myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4249     myquery << " RUNHEADER_TIME>=" <<runhtime;
4250     myquery << " order by RUNHEADER_TIME asc;";
4251     //
4252     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4253     //
4254     pResult = conn->Query(myquery.str().c_str());
4255     //
4256     Row = pResult->Next();
4257     while ( Row ){
4258     //
4259     unv++;
4260     this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4261     Row = pResult->Next();
4262     //
4263     };
4264     };
4265     };
4266 mocchiut 1.35 if ( IsDebug() ) printf(" %u runs have been unvalidated \n",unv);
4267 mocchiut 1.16 };
4268    
4269     /**
4270     *
4271     * Rearrange GL_RUN table and remove runs
4272     *
4273     **/
4274     void PamelaDBOperations::RemoveRUNS(){
4275     TSQLResult *pResult;
4276     TSQLRow *Row;
4277     stringstream myquery;
4278     UInt_t drun = 0;
4279     GL_RUN *delrun = new GL_RUN();
4280     //
4281     myquery.str("");
4282     myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4283     //
4284     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4285     //
4286     pResult = conn->Query(myquery.str().c_str());
4287     //
4288     Row = pResult->Next();
4289     //
4290     //
4291     if ( !Row ){
4292     if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4293     } else {
4294     if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");
4295     while ( Row ){
4296     delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4297 mocchiut 1.35 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4298 mocchiut 1.16 drun++;
4299     Row = pResult->Next();
4300     };
4301     };
4302     //
4303     //
4304     myquery.str("");
4305     myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4306     //
4307     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4308     //
4309     pResult = conn->Query(myquery.str().c_str());
4310     //
4311     Row = pResult->Next();
4312     //
4313     if ( !Row ){
4314     if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4315     } else {
4316     if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");
4317     while ( Row ){
4318 mocchiut 1.35 if ( IsDebug() ) printf(" restore run %u \n",(UInt_t)atoll(Row->GetField(1)));
4319 mocchiut 1.16 delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4320 mocchiut 1.35 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(1)));
4321 mocchiut 1.16 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4322     if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4323 mocchiut 1.35 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4324 mocchiut 1.16 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4325     };
4326     drun++;
4327     Row = pResult->Next();
4328     };
4329     };
4330     //
4331     if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);
4332     //
4333     //
4334     //
4335     drun = 0;
4336     //
4337     myquery.str("");
4338     myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4339     //
4340     pResult = conn->Query(myquery.str().c_str());
4341     //
4342     Row = pResult->Next();
4343     //
4344     if ( !Row ){
4345     if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4346     } else {
4347     if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");
4348     while ( Row ){
4349 mocchiut 1.35 if ( IsDebug() ) printf(" del run idtrash %u \n",(UInt_t)atoll(Row->GetField(0)));
4350 mocchiut 1.16 myquery.str("");
4351     myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";
4352     conn->Query(myquery.str().c_str());
4353     drun++;
4354     Row = pResult->Next();
4355     };
4356     };
4357     //
4358 mocchiut 1.35 if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_TRASH table \n",drun);
4359 mocchiut 1.16 //
4360     //
4361     //
4362     drun = 0;
4363     //
4364     myquery.str("");
4365     myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4366     //
4367     pResult = conn->Query(myquery.str().c_str());
4368     //
4369     Row = pResult->Next();
4370     //
4371     if ( !Row ){
4372     if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4373     } else {
4374     if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");
4375     while ( Row ){
4376 mocchiut 1.35 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4377 mocchiut 1.16 myquery.str("");
4378     myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4379     conn->Query(myquery.str().c_str());
4380     drun++;
4381     Row = pResult->Next();
4382     };
4383     };
4384     //
4385 mocchiut 1.35 if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_FRAGMENTS table \n",drun);
4386 mocchiut 1.16 //
4387     //
4388     //
4389     delete delrun;
4390     //
4391     };
4392    
4393    
4394     /**
4395     *
4396     * Rearrange calibration tables
4397     *
4398     **/
4399     void PamelaDBOperations::RemoveFILES(){
4400     stringstream myquery;
4401     //
4402     myquery.str("");
4403     myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4404     //
4405     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4406     //
4407     conn->Query(myquery.str().c_str());
4408     //
4409     };
4410    
4411     /**
4412     *
4413     * Rearrange calibration tables
4414     *
4415     **/
4416     void PamelaDBOperations::RemoveCALIBS(){
4417     TSQLResult *pResult;
4418     TSQLRow *Row;
4419     stringstream myquery;
4420     //
4421     //
4422     // Calorimeter
4423     //
4424     for (Int_t section = 0; section < 4; section++){
4425     myquery.str("");
4426     myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4427     myquery << " SECTION=" << section << ";";
4428     //
4429     pResult = conn->Query(myquery.str().c_str());
4430     //
4431     Row = pResult->Next();
4432     if( !Row->GetField(0) || !Row->GetField(1) ){
4433     //
4434     if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4435     //
4436     } else {
4437     //
4438     myquery.str("");
4439     myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4440     myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4441     myquery << " SECTION=" << section << ";";
4442     //
4443     pResult = conn->Query(myquery.str().c_str());
4444     //
4445     if( !pResult ){
4446     //
4447     if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4448     //
4449     throw -4;
4450     //
4451     };
4452     //
4453     };
4454     };
4455     myquery.str("");
4456     myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4457     //
4458     pResult = conn->Query(myquery.str().c_str());
4459     //
4460     if( !pResult ){
4461     //
4462     if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4463     //
4464     throw -4;
4465     //
4466     };
4467     //
4468     // Tracker
4469     //
4470     myquery.str("");
4471     myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4472     //
4473     pResult = conn->Query(myquery.str().c_str());
4474     //
4475     Row = pResult->Next();
4476     if( !Row->GetField(0) || !Row->GetField(1) ){
4477     //
4478     if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4479     //
4480     } else {
4481     //
4482     myquery.str("");
4483     myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4484     myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4485     //
4486     pResult = conn->Query(myquery.str().c_str());
4487     //
4488     if( !pResult ){
4489     //
4490     if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4491     //
4492     throw -4;
4493     //
4494     };
4495     //
4496     myquery.str("");
4497     myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4498     //
4499     pResult = conn->Query(myquery.str().c_str());
4500     //
4501     if( !pResult ){
4502     //
4503     if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4504     //
4505     throw -4;
4506     //
4507     };
4508     };
4509     //
4510     //
4511     // S4
4512     //
4513     myquery.str("");
4514     myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4515     //
4516     pResult = conn->Query(myquery.str().c_str());
4517     //
4518     Row = pResult->Next();
4519     if( !Row->GetField(0) || !Row->GetField(1) ){
4520     //
4521     if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4522     //
4523     } else {
4524     //
4525     myquery.str("");
4526     myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4527     myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4528     //
4529     pResult = conn->Query(myquery.str().c_str());
4530     //
4531     if( !pResult ){
4532     //
4533     if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4534     //
4535     throw -4;
4536     //
4537     };
4538     //
4539     myquery.str("");
4540     myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4541     //
4542     pResult = conn->Query(myquery.str().c_str());
4543     //
4544     if( !pResult ){
4545     //
4546     if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4547     //
4548     throw -4;
4549     //
4550     };
4551     //
4552     };
4553     };
4554 mocchiut 1.24
4555 pam-fi 1.22 /**
4556     *
4557     * Rearrange calibration tables
4558     *
4559     **/
4560     UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
4561    
4562     Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
4563     UInt_t timeaftercalib=120000; //2000;
4564     // ----------
4565     // Check CRCs
4566     // ----------
4567     for(Int_t ipkt=0; ipkt<6; ipkt++){
4568     if( caltrk->crc_hcal[ipkt] )return 0; // :-(
4569     for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
4570     }
4571     // -----------------------
4572     // Check missing packets:
4573     // -----------------------
4574     // Readout order:
4575     // ------------------
4576     // DSP packet board
4577     // ------------------
4578     // 12 0 1
4579     // 10 1 1
4580     // 8 2 1
4581     // 4 3 1
4582     // 6 4 1
4583     // 2 5 1
4584     // ------------------
4585     // 11 0 2
4586     // 9 1 2
4587     // 7 2 2
4588     // 3 3 2
4589     // 5 4 2
4590     // 1 5 2
4591     // ------------------
4592     // -------------------------------------------------
4593     // Check if it is first or second calibration packet
4594     // -------------------------------------------------
4595     UInt_t build=0;
4596     TString classname = caltrk->GetName();
4597     UInt_t base=0;
4598     UInt_t mask=0;
4599     if(classname.Contains("CalibTrk1Event")){
4600     base=12;
4601     mask=0x03F000;
4602     }
4603     if(classname.Contains("CalibTrk2Event")){
4604     base=18;
4605     mask=0xFC0000;
4606     }
4607     // -------------------------------------------------
4608     // Count number of packets and set build variable
4609     // -------------------------------------------------
4610     Int_t npkts=0;
4611     for(Int_t ipkt=0; ipkt<6; ipkt++){
4612     if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
4613     npkts++;
4614     build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
4615     }
4616     }
4617     // if( npkts==6 )return 1; // :-)
4618    
4619     // cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
4620    
4621     // -----------------------------------------------
4622     // If missing packets: check the acq configuration
4623     // (some DSPs might be excluded from acquisition)
4624     // -----------------------------------------------
4625    
4626     // -----------------------------------------------
4627     // retrieve the first run header after calib
4628     // -----------------------------------------------
4629     PacketType *pctp;
4630     EventCounter *cod;
4631     cod = eh->GetCounter();
4632     Int_t irun = cod->Get(pctp->RunHeader);
4633     TTree *rh=(TTree*)file->Get("RunHeader");
4634     if ( !rh || rh->IsZombie() ) throw -17;
4635     if( rh->GetEntries() == irun ){
4636     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1) -- cannot validate :-( "<<endl;
4637     return 0; // :-(
4638     }
4639    
4640     RunHeaderEvent *run = 0;
4641     EventHeader *hrun = 0;
4642     rh->SetBranchAddress("RunHeader", &run);
4643     rh->SetBranchAddress("Header", &hrun);
4644     rh->GetEntry(irun);
4645     // cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
4646    
4647     if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
4648     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
4649     return 0; // :-(
4650     }
4651    
4652     if( !run->RM_ACQ_AFTER_CALIB ){
4653     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0 -- cannot validate :-( "<<endl;
4654     return 0; // :-(
4655     }
4656    
4657     UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
4658     if( dtime > timeaftercalib ){
4659     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
4660     return 0; // :-(
4661     }
4662    
4663    
4664    
4665     if( (run->ACQ_BUILD_INFO & mask) != build ){
4666     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;
4667     return 0; // :-(
4668     }
4669     return 1; // :-)
4670    
4671     }
4672 mocchiut 1.24
4673     /**
4674     *
4675     * Check the DB (only for overlapping runs at the moment)
4676     *
4677     **/
4678     UInt_t PamelaDBOperations::Check(){
4679     //
4680     UInt_t test = 0;
4681     //
4682     UInt_t thisrht = 0;
4683     UInt_t thisrtt = 0;
4684     UInt_t thisid = 0;
4685     UInt_t prevrht = 0;
4686     UInt_t prevrtt = 0;
4687     UInt_t previd = 0;
4688     //
4689     UInt_t prevl0id = 0;
4690     UInt_t thisl0id = 0;
4691     //
4692     stringstream oss;
4693     TSQLResult *result = 0;
4694     TSQLRow *row = 0;
4695     TSQLResult *result2 = 0;
4696     TSQLRow *row2 = 0;
4697 mocchiut 1.46 TSQLResult *result3 = 0;
4698     TSQLRow *row3 = 0;
4699 mocchiut 1.24 oss.str("");
4700 mocchiut 1.46 oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME,NEVENTS FROM GL_RUN order by RUNHEADER_TIME asc;";
4701 mocchiut 1.24 // oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
4702     result = conn->Query(oss.str().c_str());
4703     //
4704     if ( !result ) throw -4;;
4705     //
4706     row = result->Next();
4707     //
4708     while ( row ){
4709     thisid = (UInt_t)atoll(row->GetField(0));
4710     thisl0id = (UInt_t)atoll(row->GetField(1));
4711     thisrht = (UInt_t)atoll(row->GetField(2));
4712     thisrtt = (UInt_t)atoll(row->GetField(3));
4713     //
4714 mocchiut 1.46 if ( (UInt_t)atoll(row->GetField(4)) > 1 ){
4715     //
4716     //
4717     //
4718     oss.str("");
4719     oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4720     << thisid << " AND RUNHEADER_TIME="
4721     << thisrht << " AND RUNTRAILER_TIME!="
4722     << thisrtt << " AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
4723     result3 = conn->Query(oss.str().c_str());
4724     if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
4725     if ( result3 ){
4726     row3 = result3->Next();
4727     //
4728     while ( row3 ){
4729     //
4730     // 2 runs with same runheader
4731     //
4732     printf(" CHECK n.4 RUNs %u and %u HAVE SAME RUNHEADER \n",thisid,(UInt_t)atoll(row3->GetField(0)));
4733     row3 = result3->Next();
4734     };
4735     };
4736     //
4737     oss.str("");
4738     oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4739     << thisid << " AND RUNHEADER_TIME!="
4740     << thisrht << " AND RUNTRAILER_TIME="
4741     << thisrtt << " AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
4742     result3 = conn->Query(oss.str().c_str());
4743     if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
4744     if ( result3 ){
4745     row3 = result3->Next();
4746     //
4747     while ( row3 ){
4748     //
4749     // 2 runs with same runtrailer
4750     //
4751     printf(" CHECK n.5 RUNs %u and %u HAVE SAME RUNTRAILER \n",thisid,(UInt_t)atoll(row3->GetField(0)));
4752     row3 = result3->Next();
4753     };
4754     };
4755     //
4756     oss.str("");
4757     oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4758     << thisid << " AND RUNHEADER_TIME>"
4759     << thisrht << " AND RUNTRAILER_TIME<"
4760     << thisrtt << " order by RUNHEADER_TIME asc;";
4761     result3 = conn->Query(oss.str().c_str());
4762     if ( result3 ){
4763     row3 = result3->Next();
4764     //
4765     while ( row3 ){
4766     //
4767     // run contained in the checked one
4768     //
4769     printf(" CHECK n.6 RUN %u CONTAINS RUN %u \n",thisid,(UInt_t)atoll(row3->GetField(0)));
4770     row3 = result3->Next();
4771     };
4772     };
4773     //
4774     oss.str("");
4775     oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4776     << thisid << " AND RUNHEADER_TIME="
4777     << thisrht << " AND RUNTRAILER_TIME="
4778     << thisrtt << " AND ID_RUN_FRAG!="
4779     << thisid << " order by RUNHEADER_TIME asc;";
4780     result3 = conn->Query(oss.str().c_str());
4781     if ( result3 ){
4782     row3 = result3->Next();
4783     //
4784     while ( row3 ){
4785     //
4786     // duplicated run
4787     //
4788     printf(" CHECK n.7 RUNs %u and %u HAVE SAME RUNTRAILER AND RUNHEADER (ARE THE SAME?) \n",thisid,(UInt_t)atoll(row3->GetField(0)));
4789     row3 = result3->Next();
4790     };
4791     };
4792     };
4793     //
4794 mocchiut 1.24 // if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
4795     // if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
4796     if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
4797     if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4798 mocchiut 1.43 printf(" CHECK n.1 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
4799 mocchiut 1.24 TString prevf = "";
4800     TString thisf = "";
4801     oss.str("");
4802     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4803     result2 = conn->Query(oss.str().c_str());
4804     if ( !result2 ) throw -4;;
4805     row2 = result2->Next();
4806     prevf = (TString)row2->GetField(0);
4807     oss.str("");
4808     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4809     result2 = conn->Query(oss.str().c_str());
4810     if ( !result2 ) throw -4;;
4811     row2 = result2->Next();
4812     thisf = (TString)row2->GetField(0);
4813     if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4814     test = 1;
4815     };
4816     //
4817     if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
4818     if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4819 mocchiut 1.43 printf(" CHECK n.2 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
4820 mocchiut 1.24 TString prevf = "";
4821     TString thisf = "";
4822     oss.str("");
4823     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4824     result2 = conn->Query(oss.str().c_str());
4825 mocchiut 1.46 if ( !result2 ) throw -4;
4826 mocchiut 1.24 row2 = result2->Next();
4827     prevf = (TString)row2->GetField(0);
4828     oss.str("");
4829     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4830     result2 = conn->Query(oss.str().c_str());
4831     if ( !result2 ) throw -4;;
4832     row2 = result2->Next();
4833     thisf = (TString)row2->GetField(0);
4834     if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4835     test = 1;
4836     };
4837     //
4838     if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
4839     if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4840 mocchiut 1.43 printf(" CHECK n.3 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
4841 mocchiut 1.24 TString prevf = "";
4842     TString thisf = "";
4843     oss.str("");
4844     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4845     result2 = conn->Query(oss.str().c_str());
4846     if ( !result2 ) throw -4;;
4847     row2 = result2->Next();
4848     prevf = (TString)row2->GetField(0);
4849     oss.str("");
4850     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4851     result2 = conn->Query(oss.str().c_str());
4852     if ( !result2 ) throw -4;;
4853     row2 = result2->Next();
4854     thisf = (TString)row2->GetField(0);
4855     if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4856     test = 1;
4857     };
4858    
4859     //
4860     prevrht = thisrht;
4861     prevrtt = thisrtt;
4862     previd = thisid;
4863     prevl0id = thisl0id;
4864     row = result->Next();
4865     };
4866     //
4867     return(test);
4868     //
4869     };

  ViewVC Help
Powered by ViewVC 1.1.23