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

Annotation of /chewbacca/YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34 - (hide annotations) (download)
Wed Mar 6 10:12:36 2013 UTC (11 years, 9 months ago) by mocchiut
Branch: MAIN
Changes since 1.33: +1 -1 lines
TLE 366.8 day 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     #include <TFile.h>
12     #include <TSystem.h>
13     #include <TSQLResult.h>
14     #include <TSQLRow.h>
15     #include <TTree.h>
16     #include <TGraph.h>
17     #include <TTimeStamp.h>
18     #include <TF1.h>
19     //
20     #include <EventHeader.h>
21     #include <PscuHeader.h>
22     #include <mcmd/McmdEvent.h>
23     #include <mcmd/McmdRecord.h>
24     #include <RunHeaderEvent.h>
25     #include <RunTrailerEvent.h>
26     #include <CalibCalPedEvent.h>
27     #include <CalibCalPulse1Event.h>
28     #include <CalibCalPulse2Event.h>
29     #include <CalibS4Event.h>
30     #include <CalibTrk1Event.h>
31     #include <CalibTrk2Event.h>
32     #include <varDump/VarDumpEvent.h>
33     #include <varDump/VarDumpRecord.h>
34     #include <physics/S4/S4Event.h>
35     //
36     #include <sgp4.h>
37    
38     #include <PamelaDBOperations.h>
39     //
40     using namespace std;
41     using namespace pamela;
42    
43     // Some function to work with cTle stuff.
44     bool compTLE(cTle* tle1, cTle *tle2);
45     float getTleJulian(cTle *);
46     string getTleDatetime(cTle*);
47    
48     /**
49     * Constructor.
50     * @param host hostname for the SQL connection.
51     * @param user username for the SQL connection.
52     * @param password password for the SQL connection.
53     * @param filerawname The path and name to the raw file.
54     * @param filerootname The path and name of the raw file.
55     * @param boot file BOOT number.
56     * @param obt0 file obt0.
57     * @param tsync file timesync.
58     * @param debug debug flag.
59     * @param tlefilename ascii file with TLE 3 line elements.
60     */
61 mocchiut 1.5 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, Bool_t gpamela, Bool_t keepenv){
62 mocchiut 1.1 //
63 mocchiut 1.21 RELAXED = false;
64 mocchiut 1.1 chewbacca = false;
65     chminentry = 0;
66     chID = 0;
67     if ( filerootname.IsDigit() ){
68     if ( debug ) printf(" => using database to process the file \n");
69     chewbacca = true;
70     chID = (UInt_t)filerootname.Atoll();
71     //
72     // are these really necessary?
73     //
74     staticp = false;
75     INSERT_RAW = false;
76     dwinput = 1;
77     //
78     };
79     //
80 mocchiut 1.5 KEEPENV = false;
81     if ( keepenv ) KEEPENV=true;
82 mocchiut 1.1 STATIC=false;
83     if ( staticp ) STATIC=true;
84     //
85     SetConnection(host,user,password);
86     //
87     SetDebugFlag(debug);
88     //
89     glrun = new GL_RUN();
90     //
91 mocchiut 1.30 // if ( !chewbacca ){
92     // if ( !boot ) SetNOBOOT(false);
93     // SetTsync(tsync,gpamela);
94     // SetBOOTnumber(boot,gpamela);
95     // SetObt0(obt0);
96     // };
97 mocchiut 1.1 //
98     SetTLEPath(tlefilename);
99     //
100     //
101     if ( !chewbacca ) INSERT_RAW =!filerawname.IsNull();
102     if (INSERT_RAW) SetRawName(filerawname);
103     //
104     INSERT_ROOT = !filerootname.IsNull();
105 mocchiut 1.13 if ( INSERT_ROOT ) this->SetRootName(filerootname);
106 mocchiut 1.1 this->SetOrbitNo(dwinput);
107     //
108     this->SetID_RAW(0);
109     this->SetID_ROOT(0);
110    
111     VALIDATE = false;
112    
113     //
114     };
115    
116     /**
117     * Destructor
118     */
119     void PamelaDBOperations::Close(){
120     if( conn && conn->IsConnected() ) conn->Close();
121     delete clean_time;
122     delete glrun;
123     delete this;
124     };
125    
126     //
127     // SETTERS
128     //
129 mocchiut 1.30 void PamelaDBOperations::NotChewbacca(UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t gpamela){
130     if ( !boot ) SetNOBOOT(false);
131     SetTsync(tsync,gpamela);
132     SetBOOTnumber(boot,gpamela);
133     SetObt0(obt0);
134     };
135 mocchiut 1.1
136     void PamelaDBOperations::OpenL0File(TString filerootname){
137     if( INSERT_ROOT ){
138     if ( chewbacca ){
139     //
140     // retrieve from the ROOT_TABLE the chewbacca path and filename
141     //
142     stringstream qu;
143     TSQLResult *result = 0;
144     TSQLResult *result2 = 0;
145 mocchiut 1.18 TSQLResult *result3 = 0;
146 mocchiut 1.1 TSQLRow *row = 0;
147     TSQLRow *row2 = 0;
148 mocchiut 1.18 TSQLRow *row3 = 0;
149 mocchiut 1.1 TString chpath;
150     TString chfile;
151     UInt_t ridn = 0;
152     qu.str("");
153 mocchiut 1.21 qu << "SELECT ROOT_ID_N,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,INSERTED_BY,REAL_TIME_INIT from ROOT_TABLE_MERGING where ID_N=" << chID << ";";
154 mocchiut 1.1 if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
155     result = conn->Query(qu.str().c_str());
156     if ( result ){
157     row = result->Next();
158     if ( row ){
159     ridn = (UInt_t)atoll(row->GetField(0));
160     chpktmin = (UInt_t)atoll(row->GetField(1));
161     chpktmax = (UInt_t)atoll(row->GetField(2));
162     chobtmin = (UInt_t)atoll(row->GetField(3));
163     chobtmax = (UInt_t)atoll(row->GetField(4));
164 mocchiut 1.5 chiby = (TString)(row->GetField(5));
165 mocchiut 1.21 chrtinit = (UInt_t)atoll(row->GetField(6));
166 mocchiut 1.1 } else {
167     throw -84;
168     };
169     } else {
170     throw -84;
171     };
172     delete result;
173     qu.str("");
174 mocchiut 1.18 qu << "SELECT FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,TIME_OFFSET,BOOT_NUMBER,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,REAL_TIME_INIT from ROOT_TABLE where ID_N=" << ridn << ";";
175 mocchiut 1.1 if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
176     result2 = conn->Query(qu.str().c_str());
177     if ( result2 ){
178     row2 = result2->Next();
179     if ( row2 ){
180 mocchiut 1.5 if ( KEEPENV ){
181     chpath = (TString)(row2->GetField(0))+'/';
182     } else {
183     chpath = (TString)gSystem->ExpandPathName(row2->GetField(0))+'/';
184     };
185 mocchiut 1.1 chfile = (TString)(row2->GetField(1));
186     chobtts = (UInt_t)atoll(row2->GetField(2));
187     chlastts = (UInt_t)atoll(row2->GetField(3));
188     chresursts = (UInt_t)atoll(row2->GetField(4));
189     chboot = (UInt_t)atoll(row2->GetField(5));
190     //
191 mocchiut 1.18 if ( !chboot ){
192 mocchiut 1.29 if ( debug ) printf(" mmm... no BOOT number determined by chewbacca, try to find it looking at DB \n");
193     //
194     // new feature: check in ROOT_TABLE (timewise) the previous and the next BOOT number != 0 , if it is the same we know this one!
195     // if it is different roll back to the old behaviour
196     //
197 mocchiut 1.18 qu.str("");
198 mocchiut 1.29 qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND REAL_TIME_INIT>"<< row2->GetField(10) <<" order by REAL_TIME_INIT asc limit 1;";
199 mocchiut 1.18 if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
200 mocchiut 1.29 UInt_t chbootA = 0;
201     UInt_t chbootB = 1;
202 mocchiut 1.18 result3 = conn->Query(qu.str().c_str());
203     if ( result3 ){
204     row3 = result3->Next();
205 mocchiut 1.29 if ( row3 ){
206     chbootA = (UInt_t)atoll(row3->GetField(0));
207     if ( debug ) printf(" Found boot_number A = %u \n",chbootA);
208     };
209     };
210     delete result3;
211     qu.str("");
212     qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND REAL_TIME_INIT<"<< row2->GetField(10) <<" order by REAL_TIME_INIT desc limit 1;";
213     if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
214     result3 = conn->Query(qu.str().c_str());
215     if ( result3 ){
216     row3 = result3->Next();
217     if ( row3 ){
218     chbootB = (UInt_t)atoll(row3->GetField(0));
219     if ( debug ) printf(" Found boot_number B = %u \n",chbootB);
220     };
221     };
222     if ( chbootA == chbootB ){
223     chboot = chbootA;
224     if ( debug ) printf(" Found boot_number! it is %u \n",chboot);
225     } else {
226     qu.str("");
227     // qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND ABS(LAST_TIME_SYNC_INFO-"<< chlastts <<")<10000 AND ABS(REAL_TIME_INIT-"<< row2->GetField(10) <<")<7200 group by BOOT_NUMBER order by BOOT_NUMBER asc;";
228     // qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND ABS(LAST_TIME_SYNC_INFO-"<< chlastts <<")<40000 AND ABS(REAL_TIME_INIT-"<< row2->GetField(10) <<")<40000 group by BOOT_NUMBER order by BOOT_NUMBER asc;"; // relax conditions... 090112 [8RED: error -29]
229     qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND ABS(LAST_TIME_SYNC_INFO-"<< chlastts <<")<50000 AND ABS(REAL_TIME_INIT-"<< row2->GetField(10) <<")<50000 group by BOOT_NUMBER order by BOOT_NUMBER asc;"; // even more relaxed conditions... 091214 [9REDtest3: error -29]
230     if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
231     result3 = conn->Query(qu.str().c_str());
232     if ( result3 ){
233     row3 = result3->Next();
234     if ( row3 && result3->GetRowCount() == 1 ){
235     chboot = (UInt_t)atoll(row3->GetField(0));
236     if ( debug ) printf(" Found boot_number = %u \n",chboot);
237     } else {
238     if ( debug ) printf(" AGH CANNOT DETERMINE THE BOOT NUMBER... \n");
239     throw -29;
240     };
241 mocchiut 1.18 };
242     };
243     };
244     //
245 mocchiut 1.1 chpktinit = (UInt_t)atoll(row2->GetField(6));
246     chpktfinal = (UInt_t)atoll(row2->GetField(7));
247     chobtinit = (UInt_t)atoll(row2->GetField(8));
248     chobtfinal = (UInt_t)atoll(row2->GetField(9));
249     //
250     } else {
251     throw -85;
252     };
253     } else {
254     throw -85;
255     };
256     filerootname = chpath + chfile;// + ".root";
257     };
258     this->SetRootName(filerootname);
259     };
260 mocchiut 1.13 if ( debug ) printf(" Filename is %s \n",filerootname.Data());
261     file = TFile::Open(this->GetRootName().Data());
262     // } else {
263     // this->SetRootName("");
264     // this->SetRootName(filerootname);
265     // };
266 mocchiut 1.1 }
267    
268     //
269     // 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
270     //
271     void PamelaDBOperations::CheckValidate(Long64_t olderthan){
272     clean_time = new TDatime();
273     //
274     if(olderthan >= 0){
275     VALIDATE = true;
276     UInt_t timelim = 0;
277     timelim = (UInt_t)clean_time->Convert(true) - olderthan;
278     clean_time->Set(timelim,false);
279     };
280     };
281    
282     /**
283     * Open the DB connection
284     * @param host hostname for the SQL connection.
285     * @param user username for the SQL connection.
286     * @param password password for the SQL connection.
287     */
288     void PamelaDBOperations::SetConnection(TString host, TString user, TString password){
289     if ( IsDebug() ) printf(" Connecting using host = %s user = %s password = %s \n",host.Data(),user.Data(),password.Data());
290     conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());
291     };
292    
293     /**
294     * Store the ID of the ROOT file.
295     * @param idr ID of the ROOT file
296     */
297     void PamelaDBOperations::SetID_ROOT(UInt_t idr){
298     idroot=idr;
299     };
300    
301     /**
302     * Store the ID of the RAW file.
303     * @param idr ID of the RAW file
304     */
305     void PamelaDBOperations::SetID_RAW(UInt_t idr){
306     id=idr;
307     };
308    
309     /**
310     * Set the debug flag
311     *
312     */
313     void PamelaDBOperations::SetDebugFlag(Bool_t dbg){
314     debug = dbg;
315     };
316    
317     /**
318     * Set the autoboot flag
319     *
320     */
321     void PamelaDBOperations::SetAutoBoot(Bool_t dbg){
322     AUTOBOOT = dbg;
323     };
324    
325     /**
326     * Set the pedantic flag
327     *
328     */
329     void PamelaDBOperations::SetPedantic(Bool_t dbg){
330     PEDANTIC = dbg;
331     };
332    
333     /**
334     * Set the nofrag flag
335     *
336     */
337     void PamelaDBOperations::SetNoFrag(Bool_t nf){
338     NOFRAG = nf;
339     };
340    
341     /**
342     * Store the BOOT number of the RAW file.
343     * @param boot BOOT number of the RAW file
344     */
345     void PamelaDBOperations::SetBOOTnumber(UInt_t boot){
346     this->SetBOOTnumber(boot,false);
347     };
348    
349     /**
350     * Store the BOOT number of the RAW file.
351     * @param boot BOOT number of the RAW file
352     */
353     void PamelaDBOperations::SetBOOTnumber(UInt_t boot, Bool_t gpamela){
354     BOOTNO=boot;
355     if ( gpamela ){
356     stringstream oss;
357     TSQLResult *result = 0;
358     TSQLRow *row = 0;
359     if ( !boot ){
360     //
361     BOOTNO = 1;
362     //
363     // look in the DB for the last timesync and the last run
364     //
365     oss.str("");
366     oss << "SELECT BOOT_NUMBER FROM GL_RUN order by RUNHEADER_TIME desc limit 1;";
367     result = conn->Query(oss.str().c_str());
368     if ( result ){
369     row = result->Next();
370     if ( row ){
371     BOOTNO = (UInt_t)atoll(row->GetField(0)) + 1;
372     };
373     };
374     };
375     };
376     };
377    
378     /**
379     * Store the time sync of the RAW file.
380     * @param boot time sync
381     */
382     void PamelaDBOperations::SetTsync(UInt_t ts){
383     this->SetTsync(ts,false);
384     };
385    
386     /**
387     * Store the time sync of the RAW file.
388     * @param boot time sync
389     */
390     void PamelaDBOperations::SetTsync(UInt_t ts, Bool_t gpamela){
391     //
392     // if not gpamela or given tsync file set ts
393     //
394     tsync=ts;
395     if ( gpamela ){
396     stringstream oss;
397     TSQLResult *result = 0;
398     TSQLRow *row = 0;
399     TSQLResult *result2 = 0;
400     TSQLRow *row2 = 0;
401     if ( !ts ){
402     //
403     tsync = 1;
404     //
405     // look in the DB for the last timesync and the last run
406     //
407     oss.str("");
408     oss << "SELECT TIMESYNC FROM GL_TIMESYNC order by TIMESYNC desc limit 1;";
409     result = conn->Query(oss.str().c_str());
410     if ( result ){
411     row = result->Next();
412     if ( row ){
413     tsync = (UInt_t)atoll(row->GetField(0)) + 1;
414     oss.str("");
415     oss << "SELECT (RUNTRAILER_TIME-RUNHEADER_TIME) FROM GL_RUN order by RUNHEADER_TIME desc limit 1;";
416     result2 = conn->Query(oss.str().c_str());
417     if ( result2 ){
418     row2 = result2->Next();
419     if ( row2 ){
420     tsync += (UInt_t)atoll(row2->GetField(0));
421     };
422     }
423     };
424     };
425     };
426     };
427     };
428    
429     /**
430     * Store the time sync of the RAW file.
431     * @param boot time sync
432     */
433     void PamelaDBOperations::SetObt0(UInt_t ts){
434     obt0=ts;
435     };
436    
437     /**
438     * Store the RAW filename.
439     * @param str the RAW filename.
440     */
441     void PamelaDBOperations::SetRawName(TString str){
442     filerawname=str;
443     };
444    
445     /**
446     * Store the ROOT filename.
447     * @param str the ROOT filename.
448     */
449     void PamelaDBOperations::SetRootName(TString str){
450     filerootname=str;
451     };
452    
453     /**
454     * Store the downlink orbit number from filename.
455     */
456     void PamelaDBOperations::SetOrbitNo(UInt_t dwinput){
457     dworbit = 0;
458     //
459     if ( dwinput ){
460     dworbit = dwinput;
461     if ( IsDebug() && !chewbacca ) printf(" Downlink orbit given by hand: %i \n",dworbit);
462     return;
463     };
464     //
465 mocchiut 1.13 if ( !chewbacca ){
466     TString name = this->GetRootFile();
467     Int_t nlength = name.Length();
468     if ( nlength < 5 ){
469     if ( IsDebug() ) printf(" Agh problems determining the orbit number! name = %s \n",name.Data());
470     return;
471     };
472 pizzolot 1.24 TString dwo = "";
473 mocchiut 1.13 for (Int_t i = 0; i<5; i++){
474 mocchiut 1.1 dwo.Append(name[i],1);
475 mocchiut 1.13 };
476     if ( dwo.IsDigit() ){
477     dworbit = (UInt_t)dwo.Atoi();
478     } else {
479     dwo="";
480     for (Int_t i = 8; i<13; i++){
481     dwo.Append(name[i],1);
482     };
483     if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
484     };
485     if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
486 mocchiut 1.1 };
487     return;
488     };
489    
490    
491    
492     /**
493     * Store the NOBOOT flag.
494     * @param noboot true/false.
495     */
496     void PamelaDBOperations::SetNOBOOT(Bool_t noboot){
497     NOBOOT = noboot;
498     };
499    
500     /**
501     * Store path to the TLE file.
502     */
503     void PamelaDBOperations::SetTLEPath(TString str){
504     tlefilename = str;
505     };
506    
507     TString PamelaDBOperations::GetRawPath(){
508     if ( STATIC ){
509     return((TString)gSystem->DirName(filerawname.Data())+'/');
510     } else {
511     return((TString)gSystem->ExpandPathName("$PAM_RAW")+'/');
512     };
513     };
514    
515     TString PamelaDBOperations::GetRootPath(){
516     if ( STATIC ){
517 mocchiut 1.5 return((TString)gSystem->DirName(this->GetRootName().Data())+'/');
518     } else {
519     if ( KEEPENV ){
520     return((TString)gSystem->ExpandPathName(gSystem->DirName(filerootname.Data()))+'/');
521     } else {
522     return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');
523     };
524 mocchiut 1.1 };
525     };
526    
527     /**
528     * Store the olderthan variable
529     * @param olderthan
530     */
531     // void PamelaDBOperations::SetOlderThan(Long64_t oldthan){
532     // olderthan = oldthan;
533     // };
534    
535     /**
536     * Retrieve the ID_RAW, if exists, returns NULL if does not exist.
537     */
538     Bool_t PamelaDBOperations::SetID_RAW(){
539     stringstream oss;
540     TSQLResult *result = 0;
541     TSQLRow *row = 0;
542     oss.str("");
543     if ( STATIC ){
544     oss << "SELECT ID FROM GL_RAW WHERE "
545     << " PATH = '" << this->GetRawPath().Data() << "' AND "
546     << " NAME = '" << this->GetRawFile().Data() << "' ";
547     } else {
548     oss << "SELECT ID FROM GL_RAW WHERE "
549     << " PATH = '$PAM_RAW' AND "
550     << " NAME = '" << this->GetRawFile().Data() << "' ";
551     }
552     result = conn->Query(oss.str().c_str());
553     if ( result == NULL ) throw -4;
554     row = result->Next();
555     if ( !row ) return(false);
556     id = (UInt_t)atoll(row->GetField(0));
557     delete result;
558     return(true);
559     }
560    
561     /**
562     *
563     * Set the variables which have to be stored in the GL_RUN table and that do not depend on the RUN
564     *
565     */
566     void PamelaDBOperations::SetCommonGLRUN(UInt_t absth, UInt_t abstt){
567     glrun->SetBOOTNUMBER(BOOTNO);
568     glrun->SetRUNHEADER_TIME(absth);
569     glrun->SetRUNTRAILER_TIME(abstt);
570     glrun->SetID_ROOT_L2(0);
571     glrun->SetID_ROOT_L0(idroot);
572     glrun->SetVALIDATION(0);
573     };
574    
575     /**
576 mocchiut 1.2 *
577     * Set the variables which belogns to physendrun tree
578     *
579     */
580     void PamelaDBOperations::SetPhysEndRunVariables(){
581     //
582     //
583     //
584     TTree *T = 0;
585     T = (TTree*)file->Get("PhysEndRun");
586     if ( !T || T->IsZombie() ) throw -90;
587     //
588     PhysEndRunEvent *pher= 0;
589     EventHeader *eh = 0;
590     T->SetBranchAddress("PhysEndRun", &pher);
591     T->SetBranchAddress("Header", &eh);
592     //
593     UInt_t phobt = 0;
594     UInt_t phpkt = 0;
595     //
596     glrun->SetPHYSENDRUN_MASK_S3S2S12(0);
597     glrun->SetPHYSENDRUN_MASK_S11CRC(0);
598     //
599     for (Int_t p=0; p<T->GetEntries(); p++){
600     //
601     T->GetEntry(p);
602     //
603     phobt = (UInt_t)eh->GetPscuHeader()->GetOrbitalTime();
604     phpkt = (UInt_t)eh->GetPscuHeader()->GetCounter();
605     //
606     if ( this->PKT(phpkt) >= this->PKT(glrun->GetRUNHEADER_PKT()) && this->PKT(phpkt) <= this->PKT(glrun->GetRUNTRAILER_PKT()) && this->OBT(phobt) >= this->OBT(glrun->GetRUNHEADER_OBT()) && this->OBT(phobt) <= this->OBT(glrun->GetRUNTRAILER_OBT()) ){
607     if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() || glrun->GetPHYSENDRUN_MASK_S11CRC() ){
608     if ( IsDebug() ) printf(" WARNING while looping in physendrun: found two PhysEndRun packet for the same RUN! \n");
609     if ( IsDebug() ) printf(" Actual values: %X %X New values %X %X \n ",glrun->GetPHYSENDRUN_MASK_S3S2S12(),glrun->GetPHYSENDRUN_MASK_S11CRC(),(UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S3S2S12,(UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S11CRC);
610     if ( PEDANTIC && IsDebug() ) printf(" ERROR while looping in physendrun: found two PhysEndRun packet for the same RUN!\n ");
611     if ( PEDANTIC ) throw -91;
612     } else {
613     glrun->SetPHYSENDRUN_MASK_S3S2S12((UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S3S2S12);
614     glrun->SetPHYSENDRUN_MASK_S11CRC((UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S11CRC);
615     };
616     };
617     };
618     //
619     };
620    
621     /**
622 mocchiut 1.1 * Patch, look for upper limits to avoid processing retransmitted data
623     */
624     Int_t PamelaDBOperations::SetUpperLimits(){
625     UInt_t nevent = 0;
626     UInt_t pktlast = 0;
627     UInt_t obtlast = 0;
628     Long64_t t_pktlast = 0LL;
629     // UInt_t t_obtlast = 0;
630     Long64_t t_obtlast = 0LL;
631     Long64_t upperpkt2 = 0LL;
632     Long64_t upperobt2 = 0LL;
633     UInt_t zomp = 0;
634     UInt_t jump = 50000; // was 5000
635     EventCounter *code=0;
636     //
637     Long64_t deltapkt = 5000LL;
638     Long64_t deltaobt = 50000LL;
639     //
640     pcksList packetsNames;
641     pcksList::iterator Iter;
642     getPacketsNames(packetsNames);
643     //
644     pktfirst = 0;
645     obtfirst = 0;
646     ppktfirst = 0;
647     pobtfirst = 0;
648     //
649     //
650     //
651     TTree *T = 0;
652     T = (TTree*)file->Get("Physics");
653     if ( !T || T->IsZombie() ) throw -16;
654     EventHeader *eh = 0;
655     PscuHeader *ph = 0;
656     T->SetBranchAddress("Header", &eh);
657     nevent = T->GetEntries();
658     //
659     T->GetEntry(0);
660     ph = eh->GetPscuHeader();
661    
662     if ( chewbacca ){
663     if ( IsDebug() ) printf(" FROM CHEWBACCA: upperpkt %u upperobt %u lowerpkt %u lowerobt %u \n",chpktmax,chobtmax,chpktmin,chobtmin);
664     ppktfirst = chpktmin;
665     pobtfirst = chobtts*1000;
666     } else {
667     //
668     pktfirst = ph->GetCounter();
669     obtfirst = ph->GetOrbitalTime();
670     ppktfirst = pktfirst;
671     pobtfirst = obtfirst; // to be changed AFTER time sync determination this is a profiler bug!
672     };
673     //
674     code = eh->GetCounter();
675     UInt_t en = 0;
676     // if ( !chewbacca ){
677     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
678     en = code->Get(GetPacketType(*Iter));
679     if ( !strcmp("CalibCalPed",*Iter) || !strcmp("CalibTrk1",*Iter) || !strcmp("CalibTrk2",*Iter) || !strcmp("CalibS4",*Iter) ){
680     if ( IsDebug() ) printf(" We have a calibration before the first physic packet: packet type is %s, entries: %i \n",*Iter,en);
681     //
682     TTree *TC = 0;
683     TC = (TTree*)file->Get("CalibCalPed");
684     if ( !TC || TC->IsZombie() ) throw -16;
685     EventHeader *ehc = 0;
686     PscuHeader *phc = 0;
687     TC->SetBranchAddress("Header", &ehc);
688     TC->GetEntry(0);
689     phc = ehc->GetPscuHeader();
690     pktfirst = phc->GetCounter();
691     obtfirst = phc->GetOrbitalTime();
692     //
693     };
694     };
695     //
696     T->GetEntry(nevent-1);
697     ph = eh->GetPscuHeader();
698     pktlast = ph->GetCounter();
699     obtlast = ph->GetOrbitalTime();
700     //
701     // paranoid check
702     //
703     if ( chewbacca ) {
704     //
705     // query the ROOT_TABLE to find upper and lower limits of the file and compare them to the given interval
706     //
707     if ( IsDebug() ) printf(" Is good if \n");
708     if ( IsDebug() ) printf("PKT(chpktfinal) >= PKT(chpktmax) && PKT(chpktinit) <= PKT(chpktmin) && OBT(chobtfinal) >= OBT(chobtmax) && OBT(chobtinit) <= OBT(chobtmin) \n");
709     if ( IsDebug() ) printf(" %llu >= %llu && %llu <= %llu && %llu >= %llu && %llu <= %llu \n",PKT(chpktfinal),PKT(chpktmax),PKT(chpktinit),PKT(chpktmin),OBT(chobtfinal),OBT(chobtmax),OBT(chobtinit),OBT(chobtmin));
710     if ( PKT(chpktfinal) >= PKT(chpktmax) && PKT(chpktinit) <= PKT(chpktmin) && OBT(chobtfinal) >= OBT(chobtmax) && OBT(chobtinit) <= OBT(chobtmin) ){
711     if ( IsDebug() ) printf(" OK, the file contains the chewbacca interval\n");
712     } else {
713     if ( IsDebug() ) printf(" Bah, the file seems to be the wrong one, the chewbacca interval is not contained in the file... is the DB correctly filled??\n");
714     if ( PEDANTIC ) throw -89;
715     };
716     };
717     //
718     nrtbef = 0;
719     nrtaf = 0;
720     nrtbef = 0;
721     nrtaf = 0;
722     //
723     if ( chewbacca ){
724     pktfirst = chpktmin;
725     obtfirst = chobtmin;
726     pktlast = chpktmax;
727     obtlast = chobtmax;
728     upperpkt = PKT(chpktmax);
729     upperobt = OBT(chobtmax);
730     pktlast = numeric_limits<UInt_t>::max();
731     Int_t it = 0;
732     UInt_t tjump = 50000;
733     //UInt_t tjump = 100;
734     while ( tjump > 0 ){
735 mocchiut 1.9 // pktlast = numeric_limits<UInt_t>::max();
736     pktlast = chpktmax + 1;
737 mocchiut 1.8 while ( PKT(pktlast) > PKT(chpktmax) && (Int_t)(nevent-1-it) >= 0 ){
738 mocchiut 1.1 if ( (Int_t)(nevent-1-it) >= 0 ){
739     T->GetEntry(nevent-1-it);
740     ph = eh->GetPscuHeader();
741     pktlast = ph->GetCounter();
742     } else {
743     pktlast = chpktmax + 1;
744     };
745 mocchiut 1.8 if ( (!(it%1000) || abs((int)(PKT(pktlast) - PKT(chpktmax)))<1000 ) && debug ) printf(" look for up %i %i %i nevent %u (nevent-1-it) %i \n",it,pktlast,chpktmax,nevent,(Int_t)(nevent-1-it));
746 mocchiut 1.1 it += tjump;
747     };
748     if ( tjump > 1 ) it -= 2*tjump;
749     if ( it < 0 ) it = 0;
750     //
751     if ( debug ) printf(" - look for up %i %i %i nevent %u (nevent-1-it) %i \n",it,pktlast,chpktmax,nevent,(Int_t)(nevent-1-it));
752     if ( debug ) printf(" - up , tjump was %u it was %u \n",tjump,it);
753     if ( tjump == 1 ) tjump = 0;
754     if ( tjump == 10 ) tjump = 1;
755     if ( tjump == 100 ) tjump = 10;
756     if ( tjump == 1000 ) tjump = 100;
757     if ( tjump == 5000 ) tjump = 1000;
758     if ( tjump == 50000 ) tjump = 5000;
759     //
760     };
761     Int_t tupperentry = (Int_t)nevent-1-(Int_t)it+1;//+1+1;
762     if ( tupperentry < 0 ) tupperentry = 0;
763     upperentry = tupperentry;
764     it = 0;
765     pktlast = 0;
766     tjump = 50000;
767     //tjump = 100;
768     while ( tjump > 0 ){
769 mocchiut 1.9 // pktlast = 0;
770     pktlast = chpktmin - 1;
771     if ( debug ) printf("LLlook for down %i %i %llu %llu \n",it,pktlast,PKT(pktlast),PKT(chpktmin));
772 mocchiut 1.8 while ( PKT(pktlast) < PKT(chpktmin) && it < (Int_t)nevent ){
773 mocchiut 1.1 if ( it < (Int_t)nevent ){
774     T->GetEntry(it);
775     ph = eh->GetPscuHeader();
776     pktlast = ph->GetCounter();
777     } else {
778     // pktlast = chpktmax - 1;
779     pktlast = chpktmin - 1;
780     };
781     if ( !(it%1000) && debug ) printf("look for down %i %i %i \n",it,pktlast,chpktmin);
782     it += tjump;
783     };
784     if ( tjump > 1 ) it -= 2*tjump;
785     if ( it < 0 ) it = 0;
786     //
787     if ( debug ) printf(" down , tjump was %u it was %u \n",tjump,it);
788     if ( tjump == 1 ) tjump = 0;
789     if ( tjump == 10 ) tjump = 1;
790     if ( tjump == 100 ) tjump = 10;
791     if ( tjump == 1000 ) tjump = 100;
792     if ( tjump == 5000 ) tjump = 1000;
793     if ( tjump == 50000 ) tjump = 5000;
794     //
795     };
796     Int_t tchminentry = (Int_t)it-1;//-1+1;
797     if ( tchminentry < 0 ) tchminentry = 0;
798     chminentry = tchminentry;
799     //
800     if ( debug ) printf(" Chewbacca: chminentry %i chmaxentry %i delta %i nevent %i \n",chminentry,upperentry,upperentry-chminentry,nevent);
801     //
802     //
803     //
804     TTree *rh=(TTree*)file->Get("RunHeader");
805     if ( !rh || rh->IsZombie() ) throw -17;
806     TTree *rt=(TTree*)file->Get("RunTrailer");
807     if ( !rt || rt->IsZombie() ) throw -18;
808     //
809     rh->SetBranchAddress("RunHeader", &runh);
810     rh->SetBranchAddress("Header", &ehh);
811     //
812     rt->SetBranchAddress("RunTrailer", &runt);
813     rt->SetBranchAddress("Header", &eht);
814     //
815     rhev = rh->GetEntries();
816     rtev = rt->GetEntries();
817     //
818     if ( IsDebug() ){
819     for (Int_t rr=0; rr<rh->GetEntries(); rr++){
820     rh->GetEntry(rr);
821     phh = ehh->GetPscuHeader();
822     printf(" RUNHEADER %i OBT %u PKT %u \n",rr,phh->GetOrbitalTime(),phh->GetCounter());
823     };
824     for (Int_t rr=0; rr<rt->GetEntries(); rr++){
825     rt->GetEntry(rr);
826     pht = eht->GetPscuHeader();
827     printf(" RUNTRAILER %i OBT %u PKT %u \n",rr,pht->GetOrbitalTime(),pht->GetCounter());
828     };
829     };
830     //
831     nrhev = 0;
832     nrhbef = 0;
833     nrhaf = 0;
834     for (Int_t rr=0; rr<rh->GetEntries(); rr++){
835     rh->GetEntry(rr);
836     phh = ehh->GetPscuHeader();
837     if ( debug ) printf(" RRRRRRR %llu %llu %llu %llu %llu %llu \n",PKT(phh->GetCounter()),PKT(pktfirst),upperpkt,OBT(obtfirst),OBT(phh->GetOrbitalTime()),upperobt);
838     if ( PKT(phh->GetCounter()) >= PKT(pktfirst) && PKT(phh->GetCounter()) <= upperpkt && OBT(phh->GetOrbitalTime()) >= OBT(obtfirst) && OBT(phh->GetOrbitalTime()) <= upperobt ){
839     nrhev++;
840     if ( debug ) printf(" ++++ RH %i \n",nrhev);
841     };
842     if ( !nrhev ) nrhbef++;
843     if ( (nrhev && (nrhev+1+nrhbef+1) == rr) || nrhaf ) nrhaf++;
844     };
845     nrtev = 0;
846     nrtbef = 0;
847     nrtaf = 0;
848     for (Int_t rr=0; rr<rt->GetEntries(); rr++){
849     rt->GetEntry(rr);
850     pht = eht->GetPscuHeader();
851     if ( debug ) printf(" TTTTTTT %llu %llu %llu %llu %llu %llu \n",PKT(pht->GetCounter()),PKT(pktfirst),upperpkt,OBT(obtfirst),OBT(pht->GetOrbitalTime()),upperobt);
852     if ( PKT(pht->GetCounter()) >= PKT(pktfirst) && PKT(pht->GetCounter()) <= upperpkt && OBT(pht->GetOrbitalTime()) >= OBT(obtfirst) && OBT(pht->GetOrbitalTime()) <= upperobt ){
853     nrtev++;
854     if ( debug ) printf(" ++++ RT %i \n",nrtev);
855     };
856     if ( !nrtev ) nrtbef++;
857     if ( (nrtev && (nrtev+1+nrtbef+1) == rr) || nrtaf ) nrtaf++;
858     };
859     if ( debug ) printf(" NUMBER OF RH %i RT %i IN THE INTERVAL, NUMBER OF TOTAL RH %i RT %i \n",nrhev,nrtev,rhev,rtev);
860     if ( debug ) printf(" RH before %i RH after %i -- RT before %i RT after %i \n",nrhbef,nrhaf,nrtbef,nrtaf);
861     //
862     T->GetEntry(upperentry);
863     ph = eh->GetPscuHeader();
864     pktlast = ph->GetCounter();
865     nevent = upperentry - chminentry;
866 mocchiut 1.21 if ( ((Int_t)upperentry - (Int_t)chminentry) < 0 ) nevent = 0; // nevent is UInt_t! 090112 [8RED: error -88]
867     if ( IsDebug() ) printf(" Setting nevent to %u - upperentry %i chminentry %i \n",nevent,upperentry,chminentry);
868 mocchiut 1.1 //
869     } else {
870     upperpkt = PKT(pktlast);
871     upperobt = OBT(obtlast);
872     upperentry = nevent-1;
873     };
874     //
875     if ( chewbacca && nevent < 1 ) {
876 mocchiut 1.21 if ( IsDebug() ) printf(" chewbacca and no events in the file \n");
877 mocchiut 1.2 pktfirst = chpktmin;
878     upperpkt = PKT(chpktmax);
879     pktlast = chpktmax;
880     obtfirst = chobtmin;
881     obtlast = chobtmax;
882     upperobt = OBT(chobtmax);
883 mocchiut 1.1 };
884     //
885     if ( IsDebug() ) printf(" First entries are: OBT %u pkt_num %u entry %i\n",obtfirst,pktfirst,chminentry);
886     //
887     if ( IsDebug() ) printf(" Last entries are: OBT %lld pkt_num %lld entry %i\n",upperobt,upperpkt,upperentry);
888     //
889     if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ){
890 mocchiut 1.2 if ( IsDebug() ) printf(" Inconsistent PKT/OBT sequence: \n (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) %llu < %llu && %llu > %llu \n OR \n (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) %llu > %llu && %llu < %llu \n",PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst),PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst));
891     if ( PEDANTIC ) throw -88;
892     return(32);
893 mocchiut 1.1 };
894     //
895     if ( !nevent ) return(64);
896     //
897     if ( nevent < 2 ) return(128);
898     if ( nevent < jump ) jump = 1;
899     // if ( nevent < jump ) jump = int(nevent/10);
900     // if ( !jump ) jump = 1;
901     //
902 pam-fi 1.25
903     if ( ((PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt)) && nevent > deltapkt ){
904 mocchiut 1.1 //
905     if ( IsDebug() ) printf(" starting jump %i \n",jump);
906     // if ( IsDebug() ) printf(" (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) %llu < %llu && %llu > %llu \n OR \n (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) %llu > %llu && %llu < %llu \n",PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst),PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst));
907     if ( IsDebug() ) printf(" labs(PKT(pktlast)-PKT(pktfirst) %lu < deltapkt %lld && labs(OBT(obtlast)-OBT(obtfirst)) %lu <deltaobt %lld && nevent %u > deltapkt %lld \n",(labs(PKT(pktlast)-PKT(pktfirst))),deltapkt, labs(OBT(obtlast)-OBT(obtfirst)),deltaobt, nevent, deltapkt);
908     if ( PEDANTIC ) throw -66;
909     // go back
910     zomp = nevent - 2;
911     //
912     while ( jump > 0 ){
913     //
914     t_pktlast = PKT(pktlast);
915     t_obtlast = OBT(obtlast);
916     //
917     for (UInt_t i = zomp; i>1; i-=jump){
918     //
919     if ( i >= 0 ) T->GetEntry(i);
920     ph = eh->GetPscuHeader();
921     upperpkt = PKT(ph->GetCounter());
922     upperobt = OBT(ph->GetOrbitalTime());
923     upperentry = i;
924     //
925     if ( (i-1) >= 0 ) T->GetEntry(i-1);
926     ph = eh->GetPscuHeader();
927     upperpkt2 = PKT(ph->GetCounter());
928     upperobt2 = OBT(ph->GetOrbitalTime());
929     //
930     if ( (t_pktlast < upperpkt && t_obtlast > upperobt) || (t_pktlast < upperpkt2 && t_obtlast > upperobt2) ){
931     if ( IsDebug() ) printf(" .-. upperpkt2 %lld upperobt2 %lld \n",upperpkt2,upperobt2);
932     if ( IsDebug() ) printf(" .-. upperpkt %lld t_pktlast %lld upperobt %lld t_obtlast %lld \n",upperpkt,t_pktlast,upperobt,t_obtlast);
933     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);
934     throw -13;
935     };
936     //
937     if ( t_pktlast < upperpkt && t_obtlast < upperobt && t_pktlast < upperpkt2 && t_obtlast < upperobt2 ){
938     zomp = i + jump + 1;
939     if ( zomp > nevent-2 ) zomp = nevent - 2;
940     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);
941     break;
942     };
943     //
944     t_pktlast = upperpkt;
945     t_obtlast = upperobt;
946     };
947     //
948     if ( jump == 1 ) jump = 0;
949     if ( jump == 10 ) jump = 1;
950     if ( jump == 100 ) jump = 10;
951     if ( jump == 1000 ) jump = 100;
952     if ( jump == 5000 ) jump = 1000;
953     if ( jump == 50000 ) jump = 5000;
954     //
955     };
956     //
957     };
958     //
959     // check if last runtrailer is within limits, if not extend limits (one should check for all packets but we need only runtrailer)
960     //
961     if ( !chewbacca ){
962     PacketType *pctp=0;
963     TTree *rh=(TTree*)file->Get("RunHeader");
964     if ( !rh || rh->IsZombie() ) throw -17;
965     TTree *rt=(TTree*)file->Get("RunTrailer");
966     if ( !rt || rt->IsZombie() ) throw -18;
967     //
968     rh->SetBranchAddress("RunHeader", &runh);
969     rh->SetBranchAddress("Header", &ehh);
970     //
971     rt->SetBranchAddress("RunTrailer", &runt);
972     rt->SetBranchAddress("Header", &eht);
973     //
974     rhev = rh->GetEntries();
975     rtev = rt->GetEntries();
976     Long64_t sobtt = 0LL;
977     Long64_t sobth = 0LL;
978     Long64_t spktt = 0LL;
979     Long64_t spkth = 0LL;
980     Long64_t pktt = 0LL;
981     Long64_t obtt = 0LL;
982     Long64_t pkth = 0LL;
983     Long64_t obth = 0LL;
984     //
985     if ( rhev || rtev ){
986    
987     T->GetEntry(upperentry);
988     code = eh->GetCounter();
989     Int_t lasttrail = code->Get(pctp->RunTrailer);
990     Int_t lasthead = code->Get(pctp->RunHeader);
991     if ( lasttrail < rtev ){
992     rt->GetEntry(lasttrail);
993     pht = eht->GetPscuHeader();
994     pktt = PKT(pht->GetCounter());
995     obtt = OBT(pht->GetOrbitalTime());
996     };
997     //
998     if ( lasthead < rhev ){
999     rh->GetEntry(lasthead);
1000     phh = ehh->GetPscuHeader();
1001     pkth = PKT(phh->GetCounter());
1002     obth = OBT(phh->GetOrbitalTime());
1003     };
1004     //
1005     if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
1006     if ( pkth > upperpkt && obth > upperobt ){
1007     if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
1008     upperpkt = pkth;
1009     upperobt = obth;
1010     rhev = lasthead+1;
1011     } else {
1012     rhev = lasthead;
1013     };
1014     if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
1015     //
1016     if ( IsDebug() ) printf(" rtev beforev %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
1017     if ( pktt > upperpkt && obtt > upperobt ){
1018     if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
1019     upperpkt = pktt;
1020     upperobt = obtt;
1021     rtev = lasttrail+1;
1022     } else {
1023     rtev = lasttrail;
1024     };
1025     if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
1026     // goto kikko;
1027     //
1028     //
1029     // Check if runtrailer/runheader are within lower limits
1030     //
1031     //
1032     pkth = 0LL;
1033     obth = 0LL;
1034     spkth = 0LL;
1035     sobth = 0LL;
1036     for (Int_t k=0; k<rhev; k++){
1037     if ( k > 0 ){
1038     spkth = pkth;
1039     sobth = obth;
1040     };
1041     rh->GetEntry(k);
1042     phh = ehh->GetPscuHeader();
1043     pkth = PKT(phh->GetCounter());
1044     obth = OBT(phh->GetOrbitalTime());
1045     //
1046     // if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
1047     //
1048     if ( pkth < spkth && obth < sobth ){
1049     if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i \n",rhev);
1050     if ( PEDANTIC ) throw -66;
1051     //
1052     rhev = k-1;
1053     rh->GetEntry(rhev);
1054     pkth = spkth;
1055     obth = sobth;
1056     //
1057     UInt_t evbefh = 0;
1058     code = ehh->GetCounter();
1059     evbefh = code->Get(pctp->Physics);
1060     if ( evbefh >= 0 ){
1061     T->GetEntry(evbefh);
1062     ph = eh->GetPscuHeader();
1063     t_pktlast = PKT(ph->GetCounter());
1064     t_obtlast = OBT(ph->GetOrbitalTime());
1065     if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
1066     upperpkt = pkth;
1067     upperobt = obth;
1068     upperentry = evbefh-1;
1069     } else {
1070     while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){
1071     evbefh++;
1072     T->GetEntry(evbefh);
1073     ph = eh->GetPscuHeader();
1074     t_pktlast = PKT(ph->GetCounter());
1075     t_obtlast = OBT(ph->GetOrbitalTime());
1076     };
1077     T->GetEntry(evbefh-1);
1078     ph = eh->GetPscuHeader();
1079     upperpkt = PKT(ph->GetCounter());
1080     upperobt = OBT(ph->GetOrbitalTime());
1081     upperentry = evbefh-1;
1082     };
1083     };
1084     if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
1085     goto kikko0;
1086     };
1087     };
1088     kikko0:
1089     //
1090     //
1091     //
1092     pktt = 0LL;
1093     obtt = 0LL;
1094     spktt = 0LL;
1095     sobtt = 0LL;
1096     for (Int_t k=0; k<rtev; k++){
1097     if ( k > 0 ){
1098     spktt = pktt;
1099     sobtt = obtt;
1100     };
1101     rt->GetEntry(k);
1102     pht = eht->GetPscuHeader();
1103     pktt = PKT(pht->GetCounter());
1104     obtt = OBT(pht->GetOrbitalTime());
1105     //
1106     // if ( IsDebug() ) printf(" k %i rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
1107     //
1108     if ( pktt < spktt && obtt < sobtt ){
1109     if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
1110     if ( PEDANTIC ) throw -66;
1111     //
1112     rtev = k-1;
1113     rt->GetEntry(rtev);
1114     pktt = spktt;
1115     obtt = sobtt;
1116     if ( IsDebug() ) printf(" lasttrail %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
1117     //
1118     UInt_t evbeft = 0;
1119     code = eht->GetCounter();
1120     evbeft = code->Get(pctp->Physics);
1121     if ( evbeft >= 0 ){
1122     T->GetEntry(evbeft);
1123     ph = eh->GetPscuHeader();
1124     t_pktlast = PKT(ph->GetCounter());
1125     t_obtlast = OBT(ph->GetOrbitalTime());
1126     if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
1127     upperpkt = pktt;
1128     upperobt = obtt;
1129     upperentry = evbeft-1;
1130     } else {
1131     while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){
1132     evbeft++;
1133     T->GetEntry(evbeft);
1134     ph = eh->GetPscuHeader();
1135     t_pktlast = PKT(ph->GetCounter());
1136     t_obtlast = OBT(ph->GetOrbitalTime());
1137     };
1138     T->GetEntry(evbeft-1);
1139     ph = eh->GetPscuHeader();
1140     upperpkt = PKT(ph->GetCounter());
1141     upperobt = OBT(ph->GetOrbitalTime());
1142     upperentry = evbeft-1;
1143     };
1144     };
1145     if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
1146     goto kikko;
1147     // break;
1148     //
1149     };
1150     //
1151     };
1152     //
1153     kikko:
1154     //
1155     T->GetEntry(upperentry);
1156     code = eh->GetCounter();
1157     lasttrail = code->Get(pctp->RunTrailer);
1158     lasthead = code->Get(pctp->RunHeader);
1159     if ( lasttrail < rtev ){
1160     rt->GetEntry(lasttrail);
1161     pht = eht->GetPscuHeader();
1162     pktt = PKT(pht->GetCounter());
1163     obtt = OBT(pht->GetOrbitalTime());
1164     };
1165     //
1166     if ( lasthead < rhev ){
1167     rh->GetEntry(lasthead);
1168     phh = ehh->GetPscuHeader();
1169     pkth = PKT(phh->GetCounter());
1170     obth = OBT(phh->GetOrbitalTime());
1171     };
1172     //
1173     if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
1174     if ( pkth > upperpkt && obth > upperobt ){
1175     if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
1176     upperpkt = pkth;
1177     upperobt = obth;
1178     rhev = lasthead+1;
1179 mocchiut 1.2 } else {
1180 mocchiut 1.1 rhev = lasthead;
1181     };
1182     if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
1183     //
1184     if ( IsDebug() ) printf(" rtev beforev %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
1185     if ( pktt > upperpkt && obtt > upperobt ){
1186     if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
1187     upperpkt = pktt;
1188     upperobt = obtt;
1189     rtev = lasttrail+1;
1190     } else {
1191     rtev = lasttrail;
1192     };
1193     if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
1194     //
1195     };
1196     };
1197     //
1198     if ( IsDebug() ) printf(" Upper limits are: OBT %lld pkt_num %lld upper entry %i \n",upperobt,upperpkt,upperentry);
1199     //
1200     return(0);
1201     }
1202    
1203     /**
1204     *
1205     * Trick to have unique RUN ID numbers even when runs are deleted and mysql deamon restarted.
1206     * Entries in the _RUNID_GEN table are never deleted.
1207     *
1208     **/
1209     UInt_t PamelaDBOperations::AssignRunID(){
1210     //
1211     TSQLResult *result = 0;
1212     TSQLRow *row = 0;
1213     UInt_t runid = 0;
1214     //
1215     stringstream oss;
1216     //
1217     oss.str("");
1218 mocchiut 1.5 if ( chewbacca ){// if chewbacca and tag=none then use chewbacca tag (chiby = chewbacca inserted by), if chewbacca and tag!=none then use tag, if not chewbacca use tag.
1219     if ( !strcmp(tag.Data(),"NONE") ){
1220     oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< chiby.Data() <<"');";
1221     } else {
1222     oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');";
1223     };
1224     } else {
1225     oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');";
1226     };
1227 mocchiut 1.1 result = conn->Query(oss.str().c_str());
1228     if ( !result ) throw -10;
1229     oss.str("");
1230     oss << "SELECT ID FROM _RUNID_GEN ORDER BY ID DESC LIMIT 1;";
1231     result = conn->Query(oss.str().c_str());
1232     if ( !result ) throw -10;
1233     //
1234     row = result->Next();
1235     //
1236     if ( !row ) throw -28;
1237     //
1238     runid = (UInt_t)atoll(row->GetField(0));
1239     //
1240     return(runid);
1241     };
1242    
1243     //
1244     // GETTERS
1245     //
1246    
1247     /**
1248     *
1249     * Returns the DB absolute time needed to associate calibrations to data
1250     *
1251     */
1252     UInt_t PamelaDBOperations::GetAbsTime(UInt_t obt){
1253     //
1254     return(((UInt_t)(OBT(obt)/1000)+toffset));
1255     //
1256     };
1257    
1258     /**
1259     *
1260     * List of packet types (just to make easily the loops)
1261     *
1262     */
1263     const PacketType* PamelaDBOperations::GetPacketType(const char* type){
1264     if ( !strcmp(type,"Pscu") ) return(PacketType::Pscu);
1265     if ( !strcmp(type,"PhysEndRun") ) return(PacketType::PhysEndRun);
1266     if ( !strcmp(type,"CalibCalPulse1") ) return(PacketType::CalibCalPulse1);
1267     if ( !strcmp(type,"CalibCalPulse2") ) return(PacketType::CalibCalPulse2);
1268     if ( !strcmp(type,"Physics") ) return(PacketType::Physics);
1269     if ( !strcmp(type,"CalibTrkBoth") ) return(PacketType::CalibTrkBoth);
1270     if ( !strcmp(type,"CalibTrk1") ) return(PacketType::CalibTrk1);
1271     if ( !strcmp(type,"CalibTrk2") ) return(PacketType::CalibTrk2);
1272     if ( !strcmp(type,"CalibTof") ) return(PacketType::CalibTof);
1273     if ( !strcmp(type,"CalibS4") ) return(PacketType::CalibS4);
1274     if ( !strcmp(type,"CalibCalPed") ) return(PacketType::CalibCalPed);
1275     if ( !strcmp(type,"Calib1_Ac1") ) return(PacketType::Calib1_Ac1);
1276     if ( !strcmp(type,"Calib2_Ac1") ) return(PacketType::Calib2_Ac1);
1277     if ( !strcmp(type,"Calib1_Ac2") ) return(PacketType::Calib1_Ac2);
1278     if ( !strcmp(type,"Calib2_Ac2") ) return(PacketType::Calib2_Ac2);
1279     if ( !strcmp(type,"CalibCal") ) return(PacketType::CalibCal);
1280     if ( !strcmp(type,"RunHeader") ) return(PacketType::RunHeader);
1281     if ( !strcmp(type,"RunTrailer") ) return(PacketType::RunTrailer);
1282     if ( !strcmp(type,"CalibHeader") ) return(PacketType::CalibHeader);
1283     if ( !strcmp(type,"CalibTrailer") ) return(PacketType::CalibTrailer);
1284     if ( !strcmp(type,"InitHeader") ) return(PacketType::InitHeader);
1285     if ( !strcmp(type,"InitTrailer") ) return(PacketType::InitTrailer);
1286     if ( !strcmp(type,"EventTrk") ) return(PacketType::EventTrk);
1287     if ( !strcmp(type,"Log") ) return(PacketType::Log);
1288     if ( !strcmp(type,"VarDump") ) return(PacketType::VarDump);
1289     if ( !strcmp(type,"ArrDump") ) return(PacketType::ArrDump);
1290     if ( !strcmp(type,"TabDump") ) return(PacketType::TabDump);
1291     if ( !strcmp(type,"Tmtc") ) return(PacketType::Tmtc);
1292     if ( !strcmp(type,"Mcmd") ) return(PacketType::Mcmd);
1293     if ( !strcmp(type,"ForcedFECmd") ) return(PacketType::ForcedFECmd);
1294     if ( !strcmp(type,"Ac1Init") ) return(PacketType::Ac1Init);
1295     if ( !strcmp(type,"CalInit") ) return(PacketType::CalInit);
1296     if ( !strcmp(type,"TrkInit") ) return(PacketType::TrkInit);
1297     if ( !strcmp(type,"TofInit") ) return(PacketType::TofInit);
1298     if ( !strcmp(type,"TrgInit") ) return(PacketType::TrgInit);
1299     if ( !strcmp(type,"NdInit") ) return(PacketType::NdInit);
1300     if ( !strcmp(type,"S4Init") ) return(PacketType::S4Init);
1301     if ( !strcmp(type,"Ac2Init") ) return(PacketType::Ac2Init);
1302     if ( !strcmp(type,"CalAlarm") ) return(PacketType::CalAlarm);
1303     if ( !strcmp(type,"Ac1Alarm") ) return(PacketType::Ac1Alarm);
1304     if ( !strcmp(type,"TrkAlarm") ) return(PacketType::TrkAlarm);
1305     if ( !strcmp(type,"TrgAlarm") ) return(PacketType::TrgAlarm);
1306     if ( !strcmp(type,"TofAlarm") ) return(PacketType::TofAlarm);
1307     if ( !strcmp(type,"S4Alarm") ) return(PacketType::S4Alarm);
1308     if ( !strcmp(type,"Ac2Alarm") ) return(PacketType::Ac2Alarm);
1309     if ( !strcmp(type,"TsbT") ) return(PacketType::TsbT);
1310     if ( !strcmp(type,"TsbB") ) return(PacketType::TsbB);
1311     return(PacketType::Invalid);
1312     };
1313    
1314     //
1315     // PRIVATE FUNCTIONS
1316     //
1317    
1318     // /**
1319     // * Open the ROOT filename for reading
1320     // */
1321     // void PamelaDBOperations::OpenFile(){
1322     // file = TFile::Open(this->GetRootName().Data());
1323     // //
1324    
1325     void PamelaDBOperations::CheckFile(){
1326     if ( !file ) throw -12;
1327     };
1328    
1329    
1330     /**
1331     * Check if LEVEL0 file and DB connection have really be opened
1332     */
1333     void PamelaDBOperations::CheckConnection(){
1334     //
1335     // check connection
1336     //
1337     if( !conn ) throw -1;
1338     bool connect = conn->IsConnected();
1339     if( !connect ) throw -1;
1340     //
1341     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());
1342     //
1343     if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
1344     //
1345     // set DB timezone to UTC
1346     //
1347     stringstream oss;
1348     //
1349     oss.str("");
1350     oss << "SET time_zone='+0:00';";
1351     TSQLResult *result = 0;
1352     result = conn->Query(oss.str().c_str());
1353     if ( !result ) throw -10;
1354     oss.str("");
1355     oss << "SET wait_timeout=173000;";
1356     conn->Query(oss.str().c_str());
1357     //
1358     }
1359    
1360     /**
1361     * Lock tables
1362     */
1363     void PamelaDBOperations::LockTables(){
1364     //
1365     // check connection
1366     //
1367     if( !conn ) throw -1;
1368     bool connect = conn->IsConnected();
1369     if( !connect ) throw -1;
1370     //
1371     stringstream oss;
1372     //
1373     oss.str("");
1374 pam-fi 1.4 oss << "lock table GL_RUN write, GL_ROOT write, GL_RAW write, GL_TIMESYNC write, GL_RESURS_OFFSET write, GL_PARAM write, GL_TLE write, GL_RUN_FRAGMENTS write, GL_RUN_TRASH write, GL_CALO_CALIB write, GL_CALOPULSE_CALIB write, GL_TRK_CALIB write, GL_S4_CALIB write, ROOT_TABLE_MERGING write, ROOT_TABLE_BAD write, ROOT_TABLE write, _RUNID_GEN write;";
1375 mocchiut 1.1 TSQLResult *result = 0;
1376     result = conn->Query(oss.str().c_str());
1377     if ( !result ) throw -10;
1378     //
1379     }
1380    
1381     /**
1382     * Lock tables
1383     */
1384     void PamelaDBOperations::UnLockTables(){
1385     //
1386     // check connection
1387     //
1388     if( !conn ) throw -1;
1389     bool connect = conn->IsConnected();
1390     if( !connect ) throw -1;
1391     //
1392     stringstream oss;
1393     //
1394     oss.str("");
1395     oss << "unlock tables;";
1396     TSQLResult *result = 0;
1397     result = conn->Query(oss.str().c_str());
1398     if ( !result ) throw -10;
1399     //
1400     }
1401    
1402     /**
1403     * Return the correct packet number if we went back to zero
1404     */
1405     Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){
1406     //
1407 mocchiut 1.2 // 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));
1408 mocchiut 1.1 //
1409     if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2) ){
1410 mocchiut 1.2 // if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL);
1411 mocchiut 1.1 return((Long64_t)pkt_num+16777215LL);
1412     };
1413     //
1414     if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){
1415 mocchiut 1.2 // if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL);
1416 mocchiut 1.1 return((Long64_t)pkt_num-16777215LL);
1417     };
1418     //
1419 mocchiut 1.2 // if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
1420 mocchiut 1.1 return((Long64_t)pkt_num);
1421     //
1422     };
1423    
1424     /**
1425     * Return the correct On Board Time if we went back to zero
1426     */
1427     Long64_t PamelaDBOperations::OBT(UInt_t obt){
1428     //
1429 mocchiut 1.2 // 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));
1430 mocchiut 1.1 //
1431 mocchiut 1.11 if ( obt < ((Long64_t)pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){
1432 mocchiut 1.2 // if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());
1433 mocchiut 1.1 return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());
1434     };
1435     //
1436     if ( obt > ((Long64_t)pobtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
1437 mocchiut 1.2 // if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2));
1438     // if ( IsDebug() ) printf(" rise down pktnum %lld \n", (Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
1439 mocchiut 1.1 return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
1440     };
1441     //
1442 mocchiut 1.2 // if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);
1443 mocchiut 1.1 return((Long64_t)obt);
1444     };
1445    
1446     /**
1447     *
1448     * Fill the glrun class with infos about the run when we have both runtrailer and runheader
1449     *
1450     */
1451     void PamelaDBOperations::FillClass(){
1452     this->FillClass(false,false,-1,-1);
1453     };
1454    
1455     /**
1456     *
1457     * Fill the glrun class with infos about the run when we have both runtrailer and runheader
1458     *
1459     */
1460     void PamelaDBOperations::FillClass(Bool_t mishead, Bool_t mistrail, Int_t firstev, Int_t lastev){
1461     //
1462     TTree *T = 0;
1463     T = (TTree*)file->Get("Physics");
1464     if ( !T || T->IsZombie() ) throw -16;
1465     //
1466     EventHeader *eh = 0;
1467     PscuHeader *ph = 0;
1468     T->SetBranchAddress("Header", &eh);
1469     PacketType *pctp=0;
1470     EventCounter *codt=0;
1471     EventCounter *codh=0;
1472     UInt_t firstObt = 0;
1473     UInt_t lastObt = 0;
1474     UInt_t firstPkt = 0;
1475     UInt_t lastPkt = 0;
1476     UInt_t rhtime = 0;
1477     UInt_t rttime = 0;
1478 mocchiut 1.3 //
1479     if ( IsDebug() ) printf(" A firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1480     //
1481 mocchiut 1.1 if ( !mishead ){
1482     codh = ehh->GetCounter();
1483 mocchiut 1.3 if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){
1484     if ( IsDebug() ) printf(" B firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1485 mocchiut 1.1 firstev = 1;
1486     lastev = 0;
1487     } else {
1488     firstev = codh->Get(pctp->Physics);
1489     };
1490     rhtime = this->GetAbsTime(phh->GetOrbitalTime());
1491     glrun->Set_GL_RUNH(runh,phh);
1492     firstObt = glrun->GetRUNHEADER_OBT();
1493     firstPkt = glrun->GetRUNHEADER_PKT();
1494     };
1495     if ( !mistrail ){
1496     codt = eht->GetCounter();
1497 mocchiut 1.3 if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1)){
1498     if ( IsDebug() ) printf(" C firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1499 mocchiut 1.1 lastev = 0;
1500     firstev = lastev+1;
1501     } else {
1502     lastev = codt->Get(pctp->Physics)-1;
1503     };
1504     rttime = this->GetAbsTime(pht->GetOrbitalTime());
1505     glrun->Set_GL_RUNT(runt,pht);
1506     lastObt = glrun->GetRUNTRAILER_OBT();
1507     lastPkt = glrun->GetRUNTRAILER_PKT();
1508     };
1509     //
1510     if ( mishead && mistrail && lastev+1 == firstev ) throw -14; // run with no events, no runtrailer, no runheader... unsupported should never arrive here
1511     //
1512     if ( mishead ) {
1513     glrun->Set_GL_RUNH0();
1514     //
1515 mocchiut 1.3 if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){
1516 mocchiut 1.1 firstObt = lastObt;
1517     firstPkt = lastPkt;
1518     rhtime = rttime;
1519     } else {
1520     T->GetEntry(firstev);
1521     ph = eh->GetPscuHeader();
1522     firstObt = ph->GetOrbitalTime();
1523     rhtime = this->GetAbsTime(firstObt);
1524     firstPkt = ph->GetCounter();
1525     };
1526     //
1527     glrun->SetRUNHEADER_PKT(firstPkt);
1528     glrun->SetRUNHEADER_OBT(firstObt);
1529     //
1530     };
1531     if ( mistrail ){
1532     glrun->Set_GL_RUNT0();
1533     //
1534 mocchiut 1.3 if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){
1535 mocchiut 1.1 lastObt = firstObt;
1536     lastPkt = firstPkt;
1537     rttime = rhtime;
1538     } else {
1539     T->GetEntry(lastev);
1540     ph = eh->GetPscuHeader();
1541     lastObt = ph->GetOrbitalTime();
1542     rttime = this->GetAbsTime(lastObt);
1543     lastPkt = ph->GetCounter();
1544     };
1545     //
1546     glrun->SetRUNTRAILER_OBT(lastObt);
1547     glrun->SetRUNTRAILER_PKT(lastPkt);
1548     //
1549     };
1550 mocchiut 1.23 //
1551     // ad hoc solution for INF EV_TO [8RED] 090113
1552     //
1553     if ( !firstev && lastev == -1 ){
1554     if ( IsDebug() ) printf(" EE firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1555     firstev = 1;
1556     lastev = 0;
1557     };
1558 mocchiut 1.1 glrun->SetEV_FROM((UInt_t)firstev);
1559     glrun->SetEV_TO((UInt_t)lastev);
1560     glrun->SetNEVENTS((UInt_t)lastev-(UInt_t)firstev+1);
1561     //
1562     if ( IsDebug() ) printf(" firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1563     //
1564     this->SetCommonGLRUN(rhtime,rttime);
1565 mocchiut 1.2 this->SetPhysEndRunVariables();
1566 mocchiut 1.1 //
1567     };
1568    
1569     //
1570     // PUBLIC FUNCTIONS
1571     //
1572    
1573     /**
1574     * Insert a new row into GL_RAW table.
1575     */
1576     Int_t PamelaDBOperations::insertPamelaRawFile(){
1577 pam-fi 1.4
1578     //
1579     Bool_t idr = this->SetID_RAW();
1580     if ( idr ) return(1);
1581    
1582     GL_RAW glraw = GL_RAW();
1583    
1584     glraw.PATH = GetRawPath();
1585     glraw.NAME = GetRawFile();
1586 mocchiut 1.5 // glraw.BOOT_NUMBER = 0;//???
1587     glraw.BOOT_NUMBER = this->GetBOOTnumber();
1588 pam-fi 1.4
1589     if( insertPamelaRawFile(&glraw) )return(1);
1590     //
1591     idr = this->SetID_RAW();
1592     if ( !idr ) throw -11;
1593    
1594     return(0);
1595     }
1596     /**
1597     * Insert a new row into GL_RAW table.
1598     */
1599     Int_t PamelaDBOperations::insertPamelaRawFile(GL_RAW *glraw){
1600 mocchiut 1.1 //
1601 mocchiut 1.5 if(!glraw)return(1);//?? ok I think
1602 pam-fi 1.4 //
1603     stringstream oss;
1604     //
1605     oss.str("");
1606     if ( STATIC ){
1607     oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
1608     << glraw->PATH << "', '" << glraw->NAME << "')";
1609     } else {
1610     oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << glraw->NAME << "')";
1611     };
1612     if ( debug ) cout <<oss.str().c_str() <<endl;
1613     if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1614     //
1615     oss.str("");
1616     oss << "SELECT ID FROM GL_RAW WHERE NAME=\""<<glraw->NAME<<"\";";
1617     if ( debug ) cout << oss.str().c_str()<<endl;
1618     if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1619     //
1620     TSQLResult *result = 0;
1621     TSQLRow *row = 0;
1622     result = conn->Query(oss.str().c_str());
1623     if ( result == NULL ) throw -4;
1624     row = result->Next();
1625     if ( !row ) return(1);
1626     glraw->ID = (UInt_t)atoll(row->GetField(0));
1627     if ( debug ) printf(" The ID of the RAW file is %u \n",glraw->ID);
1628     delete result;
1629     delete row;
1630     //
1631     return(0);
1632 mocchiut 1.1 }
1633    
1634    
1635     /**
1636     * Look for one timesync information in the file and
1637     * 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
1638     */
1639     Int_t PamelaDBOperations::insertPamelaGL_TIMESYNC(){
1640     //
1641     Int_t signal = 0;
1642     UInt_t idresof = 0;
1643     stringstream oss;
1644     TSQLResult *result = 0;
1645     TSQLRow *row = 0;
1646     UInt_t OBT = 0;
1647     UInt_t TYPE = 0;
1648     UInt_t TSYNC = 0;
1649     UInt_t t0 = 0;
1650     Bool_t existsts = false;
1651     //
1652     if ( chewbacca ){
1653     //
1654     OBT = chobtts * 1000;
1655     TSYNC = chlastts;
1656     t0 = chresursts;
1657     TYPE = 777;
1658     oss.str("");
1659     oss << "select * from GL_RESURS_OFFSET where OFFSET_DATE=FROM_UNIXTIME("<< t0 <<") limit 1;";
1660     if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1661     result = conn->Query(oss.str().c_str());
1662     if ( !result ) throw -10;
1663     row = result->Next();
1664 mocchiut 1.14 if ( !row ) throw -92;
1665 mocchiut 1.1 idresof = (UInt_t)atoll(row->GetField(0));
1666     existsts = true;
1667     goto eout;
1668     //
1669     } else {
1670     //
1671     //signal = this->SetUpperLimits();
1672     //
1673     if ( this->GetID_RAW() == 0 ) throw -11;
1674     //
1675     oss.str("");
1676     oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
1677     << this->GetRawFile().Data() << "';";
1678     if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1679     result = conn->Query(oss.str().c_str());
1680     if ( !result ) throw -10;
1681     row = result->Next();
1682     //
1683     if ( !row ){
1684     oss.str("");
1685     oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
1686 mocchiut 1.20 << dworbit << " AND SPECIAL_FILE='' order by FROM_ORBIT desc limit 1;";
1687 mocchiut 1.1 if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1688     result = conn->Query(oss.str().c_str());
1689     if ( !result ) throw -10;
1690     row = result->Next();
1691     if ( !row ) throw -10;
1692     };
1693     //
1694     idresof = (UInt_t)atoll(row->GetField(6));
1695     //
1696     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);
1697     t0 = (UInt_t)tu.GetSec();
1698     if ( IsDebug() ) printf(" t0 is %u ti is %s %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),row->GetField(6));
1699     //
1700     /*
1701     * Verify that the TIMESYNC have been not already processed
1702     */
1703     oss.str("");
1704     oss << " SELECT COUNT(GL_TIMESYNC.ID),GL_TIMESYNC.OBT0,GL_TIMESYNC.TIMESYNC FROM GL_TIMESYNC "
1705     << " LEFT JOIN GL_RAW "
1706     << " ON GL_RAW.ID = GL_TIMESYNC.ID_RAW "
1707     << " WHERE GL_TIMESYNC.ID_RAW = " << this->GetID_RAW()
1708     << " GROUP BY GL_TIMESYNC.OBT0;";
1709     if ( IsDebug() ) printf(" check for old timesync: query is \n %s \n",oss.str().c_str());
1710     result = conn->Query(oss.str().c_str());
1711     if (result == NULL) throw -10;
1712     row = result->Next();
1713     if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
1714     if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1715     toffset = (UInt_t)atoll(row->GetField(2)) - (UInt_t)(this->OBT((UInt_t)atoll(row->GetField(1)))/1000) + t0;
1716     //
1717     tsync = (UInt_t)atoll(row->GetField(2));
1718     obt0 = (UInt_t)atoll(row->GetField(1));
1719     //
1720     if ( PEDANTIC ) throw -67;
1721     return(1);
1722     };
1723     //
1724     TTree *T = 0;
1725     //
1726     UInt_t nevent = 0;
1727     UInt_t recEntries = 0;
1728     //
1729     OBT = 0;
1730     TYPE = 0;
1731     TSYNC = 0;
1732     //
1733 mocchiut 1.33 // Double_t minimum = 0.;
1734     // Double_t maximum = 0.;
1735     //Double_t minimum2 = 0.;
1736 mocchiut 1.1 Double_t maximum2 = 0.;
1737     //
1738     //
1739     pamela::McmdEvent *mc = 0;
1740     pamela::McmdRecord *mcrc = 0;
1741     TArrayC *mcmddata = 0;
1742     //
1743 mocchiut 1.33 // minimum = numeric_limits<Double_t>::max();
1744     // maximum = numeric_limits<Double_t>::min();
1745     // minimum2 = numeric_limits<Double_t>::max();
1746 mocchiut 1.1 maximum2 = numeric_limits<Double_t>::min();
1747     //
1748     T = (TTree*)file->Get("Mcmd");
1749     if ( !T || T->IsZombie() ) throw -19;
1750     T->SetBranchAddress("Mcmd",&mc);
1751     //
1752     nevent = T->GetEntries();
1753     //
1754     // loop over events
1755     //
1756     existsts = false;
1757     //
1758     for (UInt_t i=0; i<nevent;i++){
1759     //
1760     T->GetEntry(i);
1761     //
1762     recEntries = mc->Records->GetEntries();
1763     //
1764     for (UInt_t j = 0; j < recEntries; j++){
1765     mcrc = (pamela::McmdRecord*)mc->Records->At(j);
1766     mcmddata = mcrc->McmdData;
1767     //
1768     if (mcrc->ID1 == 0xE0){ // mcmd timesync
1769     //
1770     OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
1771     //
1772     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);
1773     //
1774     TYPE = 55;//224;
1775     //
1776     if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
1777     //
1778     if ( TSYNC && OBT ){
1779     existsts = true;
1780     goto eout;
1781     };
1782     //
1783     };
1784     };
1785     };
1786     if ( !existsts ) { // try with runheader and runtrailer
1787     //
1788     if ( IsDebug() ) printf(" No ts mcmd \n");
1789     signal = 2;
1790     //
1791     TTree *rh=(TTree*)file->Get("RunHeader");
1792     if ( !rh || rh->IsZombie() ) throw -17;
1793     TTree *rt=(TTree*)file->Get("RunTrailer");
1794     if ( !rt || rt->IsZombie() ) throw -18;
1795     //
1796     rh->SetBranchAddress("RunHeader", &runh);
1797     //
1798     rt->SetBranchAddress("RunTrailer", &runt);
1799     //
1800     Int_t nnrhev = rh->GetEntries();
1801     Int_t nnrtev = rt->GetEntries();
1802     if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nnrhev,nnrtev);
1803     //
1804     if ( nnrhev > 0 ){
1805     for (Int_t i=0; i<nnrhev; i++){
1806     //
1807     rh->GetEntry(i);
1808     //
1809     TSYNC = runh->LAST_TIME_SYNC_INFO;
1810     OBT = runh->OBT_TIME_SYNC * 1000;
1811     //
1812     TYPE = 20;
1813     //
1814     if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
1815     //
1816     if ( TSYNC && OBT ){
1817     existsts = true;
1818     goto eout;
1819     };
1820     };
1821     //
1822     };
1823     if ( nnrtev > 0 ){
1824     //
1825     if ( IsDebug() ) printf(" No runheader \n");
1826     signal = 6;
1827     //
1828     for (Int_t i=0; i<nnrtev; i++){
1829     //
1830     rt->GetEntry(i);
1831     //
1832     TSYNC = runt->LAST_TYME_SYNC_INFO;
1833     OBT = runt->OBT_TYME_SYNC * 1000;
1834     //
1835     TYPE = 21;
1836     //
1837     if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
1838     //
1839     if ( TSYNC && OBT ){
1840     existsts = true;
1841     goto eout;
1842     };
1843     };
1844     //
1845     } else {
1846     if ( IsDebug() ) printf(" No runheader \n");
1847     };
1848     };
1849     //
1850     if ( !existsts ){ // try with inclination mcmd
1851     //
1852     if ( IsDebug() ) printf(" No runtrailer \n");
1853     signal = 14;
1854     //
1855     Double_t timesync = 0.;
1856     for (UInt_t i=0; i<nevent;i++){
1857     //
1858     T->GetEntry(i);
1859     //
1860     recEntries = mc->Records->GetEntries();
1861     // //
1862     for (UInt_t j = 0; j < recEntries; j++){
1863     mcrc = (pamela::McmdRecord*)mc->Records->At(j);
1864     mcmddata = mcrc->McmdData;
1865     //
1866     if (mcrc->ID1 == 0xE2){ // mcmd inclination
1867     timesync = 0.;
1868     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);
1869     //
1870     if ( timesync > maximum2){
1871     maximum2 = timesync;
1872     OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
1873     };
1874     };
1875     //
1876     };
1877     };
1878     if ( maximum2 > numeric_limits<Double_t>::min() ){
1879     TSYNC = (UInt_t)(maximum2 + 0.5);
1880     TYPE = 666;
1881     if ( TSYNC && OBT ){
1882     existsts = true;
1883     goto eout;
1884     };
1885     };
1886     };
1887     //
1888     };
1889     //
1890     if ( !existsts && obt0 ){ // insert timesync by hand
1891     //
1892     if ( PEDANTIC ) throw -68;
1893     if ( IsDebug() ) printf(" No incl mcmd \n");
1894     signal = 30;
1895     //
1896     OBT = obt0;
1897     TSYNC = tsync;
1898     TYPE = 999;
1899     existsts = true;
1900     goto eout;
1901     };
1902     //
1903     eout:
1904     //
1905     if ( !existsts ) throw -3;
1906     //
1907     oss.str("");
1908     oss << "INSERT INTO GL_TIMESYNC (ID_RAW,TYPE,OBT0,TIMESYNC,ID_RESURS_OFFSET) VALUES ('"
1909     << this->GetID_RAW() << "','"//224'"
1910     << dec << (UInt_t)TYPE << "','"
1911     << dec << (UInt_t)OBT << "','"
1912     << dec << (UInt_t)TSYNC << "','"
1913     << dec << (UInt_t)idresof << "');";
1914     conn->Query(oss.str().c_str());
1915     if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());
1916     if ( conn->GetErrorCode() ){
1917     printf(" OK, you got an error because the database structure you are using is not up to date\n Using backward compability code, hence you can continue safetly \n");
1918     oss.str("");
1919     oss << "INSERT INTO GL_TIMESYNC (ID_RAW,TYPE,OBT0,TIMESYNC) VALUES ('"
1920     << this->GetID_RAW() << "','"//224'"
1921     << dec << (UInt_t)TYPE << "','"
1922     << dec << (UInt_t)OBT << "','"
1923     << dec << (UInt_t)TSYNC << "');";
1924     conn->Query(oss.str().c_str());
1925     if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());
1926     };
1927     //
1928     if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1929     //
1930     toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1931     //
1932     tsync = TSYNC;
1933     obt0 = OBT;
1934     //
1935     delete result;
1936     return(signal);
1937     }
1938    
1939     /**
1940     * Insert all the new rows into GL_ROOT.
1941     * The raw file indicates in the parameters should be already been stored in the database.
1942     */
1943     Int_t PamelaDBOperations::insertPamelaRootFile(){
1944 pam-fi 1.4
1945     stringstream oss;
1946     TSQLResult *result = 0;
1947     TSQLRow *row = 0;
1948 mocchiut 1.5 //
1949 pam-fi 1.4 // ----------------------
1950     // determine the timesync
1951     // ----------------------
1952     UInt_t idtimesync = 0;
1953     //
1954     if ( chewbacca ){
1955     oss.str("");
1956     oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";
1957     if ( debug ) printf(" %s \n",oss.str().c_str());
1958     result = conn->Query(oss.str().c_str());
1959     //
1960     if ( !result ) throw -3;
1961     //
1962     row = result->Next();
1963     //
1964     if ( !row ) throw -3;
1965     idtimesync = (UInt_t)atoll(row->GetField(0));
1966     } else {
1967     oss.str("");
1968     if ( STATIC ){
1969     oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1970     << " LEFT JOIN GL_ROOT "
1971     << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1972     << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1973     << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1974     } else {
1975     oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1976     << " LEFT JOIN GL_ROOT "
1977     << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1978     << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "
1979     << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1980     };
1981     result = conn->Query(oss.str().c_str());
1982     //
1983     if ( !result ) throw -12;
1984     //
1985     row = result->Next();
1986     //
1987     if ( !row ) throw -10;
1988     if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){
1989     idroot = (UInt_t)atoll(row->GetField(2));
1990     delete row;
1991     delete result;
1992     return(1);
1993     };
1994     //
1995     // determine which timesync has to be used
1996     //
1997     oss.str("");
1998     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;";
1999     result = conn->Query(oss.str().c_str());
2000     //
2001     if ( !result ) throw -3;
2002     //
2003     row = result->Next();
2004     //
2005     if ( !row ) throw -3;
2006     idtimesync = (UInt_t)atoll(row->GetField(0));
2007     };
2008    
2009     delete row;
2010     delete result;
2011    
2012     // ----------------------
2013     // insert root file
2014     // ----------------------
2015    
2016     GL_ROOT glroot = GL_ROOT();
2017    
2018     glroot.ID_RAW = GetID_RAW();
2019     glroot.ID_TIMESYNC = idtimesync;
2020 mocchiut 1.5 if ( STATIC ){
2021     glroot.PATH = GetRootPath();
2022     } else {
2023     if ( KEEPENV ){
2024     glroot.PATH = gSystem->DirName(filerootname.Data());
2025     } else {
2026     glroot.PATH = "$PAM_L0";
2027     };
2028     };
2029 mocchiut 1.6 glroot.NAME = GetRootFile();
2030 pam-fi 1.4
2031     if ( insertPamelaRootFile(&glroot) )return 1;
2032    
2033     SetID_ROOT(glroot.ID);
2034    
2035    
2036     return (0);
2037     }
2038     /**
2039     * Insert all the new rows into GL_ROOT.
2040     * The raw file indicates in the parameters should be already been stored in the database.
2041     */
2042     Int_t PamelaDBOperations::insertPamelaRootFile(GL_ROOT *glroot){
2043 mocchiut 1.1 stringstream oss;
2044     TSQLResult *result = 0;
2045     TSQLRow *row = 0;
2046     //
2047     //
2048     oss.str("");
2049 mocchiut 1.5 oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
2050     << glroot->ID_RAW << "', '" << glroot->ID_TIMESYNC << "', '" << glroot->PATH << "', '" << glroot->NAME << "')";
2051 mocchiut 1.1 //
2052 pam-fi 1.4 if ( debug ) printf("%s \n",oss.str().c_str());
2053 mocchiut 1.1 if (conn->Query(oss.str().c_str()) == 0) throw -4;
2054     //
2055     delete result;
2056     //
2057     oss.str("");
2058     // oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";
2059 mocchiut 1.5 oss << "SELECT ID FROM GL_ROOT WHERE PATH='" << glroot->PATH << "' and NAME='"<< glroot->NAME <<"';";
2060 mocchiut 1.1 //
2061 pam-fi 1.4 if ( debug ) printf("%s \n",oss.str().c_str());
2062 mocchiut 1.1 result = conn->Query(oss.str().c_str());
2063     if ( !result ) throw -12;
2064     row = result->Next();
2065     if ( !row ) throw -3;
2066 pam-fi 1.4
2067     glroot->ID = (UInt_t)atoll(row->GetField(0));
2068    
2069     if ( debug ) printf(" The ID of the ROOT file is %u \n",glroot->ID);
2070 mocchiut 1.1 //
2071     delete result;
2072     //
2073     return(0);
2074     }
2075    
2076     /**
2077     * Assign the BOOT_NUMBER to the raw file.
2078     */
2079     Int_t PamelaDBOperations::assignBOOT_NUMBER(){
2080     Bool_t found = false;
2081     UInt_t idRaw = 0;
2082     UInt_t bn = 0;
2083     stringstream oss;
2084     TSQLResult *result = 0;
2085     TSQLRow *row = 0;
2086     if ( chewbacca ){
2087     if ( chboot == 1 ){
2088     // not found!
2089     found = false;
2090     } else {
2091     found = true;
2092     this->SetBOOTnumber(chboot);
2093     };
2094     } else {
2095     oss.str("");
2096     if ( STATIC ){
2097     oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
2098     << " PATH = '" << this->GetRawPath().Data() << "' AND "
2099     << " NAME = '" << this->GetRawFile().Data() << "' ";
2100     } else {
2101     oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
2102     << " PATH = '$PAM_RAW' AND "
2103     << " NAME = '" << this->GetRawFile().Data() << "' ";
2104     };
2105     result = conn->Query(oss.str().c_str());
2106     //
2107     if ( !result ) throw -4;;
2108     row = result->Next();
2109     if ( !row ) return(16);
2110     if ( row->GetField(1) ){
2111     this->SetBOOTnumber((UInt_t)atoll(row->GetField(1)));
2112     return(1);
2113     };
2114     if ( !row->GetField(0) ) throw -26;
2115     //
2116     idRaw = (UInt_t)atoll(row->GetField(0));
2117     //
2118     //
2119     //
2120     TTree *trDumpEv = 0;
2121     trDumpEv = (TTree*)file->Get("VarDump");
2122     if ( !trDumpEv || trDumpEv->IsZombie() ) throw -20;
2123     //
2124     VarDumpEvent *vde = 0;
2125     VarDumpRecord *vdr = 0;
2126     //
2127     trDumpEv->SetBranchAddress("VarDump", &vde);
2128     if ( trDumpEv->GetEntries() > 0 ){
2129     found = false;
2130     for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
2131     trDumpEv->GetEntry(i);
2132     // vde->Records->GetEntries();
2133     if ( vde->Records->GetEntries()>5 ){
2134     found = true;
2135     goto fill;
2136     };
2137     };
2138     fill:
2139     if ( found ){
2140     //
2141     vdr = (VarDumpRecord*)vde->Records->At(6);
2142     //
2143     this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
2144     //
2145     } else {
2146     if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4);
2147     };
2148     } else {
2149     if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2);
2150     };
2151     //
2152     };
2153     //
2154     Bool_t afound = false;
2155     if ( !found && this->AutoBoot()){
2156     afound = true;
2157     //
2158     // Search for other files with similar timesync
2159     //
2160     if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0);
2161     UInt_t upperts = tsync-(obt0/1000)+5;
2162     UInt_t lowerts = tsync-(obt0/1000)-5;
2163     if ( chewbacca ){
2164     oss.str("");
2165     oss << "select BOOT_NUMBER from ROOT_TABLE where LAST_TIME_SYNC_INFO-(OBT_TIME_SYNC)<"
2166     << upperts
2167     << " AND LAST_TIME_SYNC_INFO-(OBT_TIME_SYNC)>"
2168     << lowerts
2169     << " AND BOOT_NUMBER>1;";
2170     } else {
2171     oss.str("");
2172     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)<"
2173     << upperts
2174     << " AND TIMESYNC-(OBT0/1000)>"
2175     << lowerts
2176     << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
2177     };
2178     result = conn->Query(oss.str().c_str());
2179     if ( IsDebug() && !chewbacca ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str());
2180     if ( IsDebug() && chewbacca ) printf(" Query the ROOT_TABLE table to find boot number:\n %s \n",oss.str().c_str());
2181     //
2182     if ( !result ) throw -4;;
2183     found = true;
2184     if ( result->GetRowCount()<3 ){
2185     if ( IsDebug() ) printf(" AGH! no results!\n");
2186     found = false;
2187     } else {
2188     row = result->Next();
2189     bn = (UInt_t)atoll(row->GetField(0));
2190     for ( Int_t r=1; r<result->GetRowCount() ;r++){
2191     if ( !row ) throw -4;
2192     if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0));
2193     if ( bn != (UInt_t)atoll(row->GetField(0)) ){
2194     if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0)));
2195     found = false;
2196     };
2197     row = result->Next();
2198     };
2199     };
2200     };
2201     //
2202     Int_t sgn = 0;
2203     //
2204     if ( !found && !BOOTNO ){
2205     throw -29;
2206     } else {
2207     if ( afound ){
2208     this->SetBOOTnumber(bn);
2209     sgn = 8;
2210     };
2211     };
2212     //
2213     if ( !chewbacca ){
2214     oss.str("");
2215     oss << " UPDATE GL_RAW "
2216     << " SET GL_RAW.BOOT_NUMBER = '" << dec << this->GetBOOTnumber() << "'"
2217     << " WHERE GL_RAW.ID = '" << idRaw << "'";
2218     conn->Query(oss.str().c_str());
2219     };
2220     //
2221     delete result;
2222     return(sgn);
2223     };
2224    
2225     /**
2226     * Scan runtrailer packet, fill the GL_RUN table and
2227     * check for missing and truncated runs
2228     */
2229     Int_t PamelaDBOperations::insertPamelaRUN(){
2230     Int_t signal = 0;
2231     //
2232     stringstream oss;
2233     oss.str("");
2234     //
2235     // signal = this->SetUpperLimits();
2236    
2237     //
2238     // loop on runheader and runtrailer events
2239     //
2240     TTree *rh=(TTree*)file->Get("RunHeader");
2241     if ( !rh || rh->IsZombie() ) throw -17;
2242     TTree *rt=(TTree*)file->Get("RunTrailer");
2243     if ( !rt || rt->IsZombie() ) throw -18;
2244     //
2245     PacketType *pctp=0;
2246     EventCounter *cod=0;
2247     //
2248     rh->SetBranchAddress("RunHeader", &runh);
2249     rh->SetBranchAddress("Header", &ehh);
2250     //
2251     rt->SetBranchAddress("RunTrailer", &runt);
2252     rt->SetBranchAddress("Header", &eht);
2253     //
2254     TTree *T = (TTree*)file->Get("Physics");
2255     if ( !T || T->IsZombie() ) throw -16;
2256     EventHeader *eh = 0;
2257     T->SetBranchAddress("Header", &eh);
2258     //
2259     if ( !(rh->GetEntries()) && !(rt->GetEntries()) && !(T->GetEntries()) ) return(16);
2260     //
2261     UInt_t obtt = 0;
2262     UInt_t obth = 0;
2263     UInt_t pktt = 0;
2264     UInt_t pkth = 0;
2265     Int_t pth = -1;
2266     Int_t ptht = -1;
2267     Int_t evbeft = 0;
2268     Int_t evbefh = 0;
2269     UInt_t tcod;
2270     //
2271     // no runtrailers in the file!
2272     //
2273 mocchiut 1.32 if ( IsDebug() ) printf(" Start rtev %i rhev %i nrtev %i nrhev %i \n",rtev,rhev,nrtev,nrhev);
2274     if ( !rtev || (chewbacca && !nrtev) ){
2275     if ( IsDebug() ) printf(" No runtrailers \n");
2276     if ( !rhev || (chewbacca && !nrhev) ){
2277 mocchiut 1.1 if ( IsDebug() ) printf(" No runheaders nor runtrailers!! \n");
2278     if ( !(upperentry-chminentry) ){
2279     if ( IsDebug() ) printf(" No physics events nor runs in the file \n"); // di nuovo potrebbe esserci un runtrailer senza eventi (riempimento MM)
2280     // throw -8;
2281     return 0; // one could check if there is any calibration no need to exit with error
2282     } else {
2283     this->HandleRunFragments(true,true,chminentry,upperentry); // no runtrailers ma potrebbe esserci un runheader ora...
2284     };
2285     } else {
2286     //
2287     // we have runheaders but not runtrailers!
2288     //
2289     if ( debug ) printf(" We have runheaders (%i) but not runtrailers (%i) ! \n",rhev,rtev);
2290     for ( pth=0; pth < rhev; pth++ ){
2291     rh->GetEntry(pth);
2292     phh = ehh->GetPscuHeader();
2293     pkth = phh->GetCounter();
2294     obth = phh->GetOrbitalTime();
2295     if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){
2296     cod = ehh->GetCounter();
2297     tcod = (UInt_t)cod->Get(pctp->Physics);
2298     evbefh = TMath::Max(chminentry,tcod);
2299 mocchiut 1.2 // if ( (UInt_t)evbefh == upperentry ) evbefh = upperentry + 1; // this does not work due to the Counter bug in chewbacca
2300 mocchiut 1.1 if ( (UInt_t)evbefh == upperentry || !upperentry ) evbefh = upperentry + 1;
2301 mocchiut 1.2 //
2302     if ( debug ) printf(" evbefh %i upperentry %u \n",evbefh,upperentry);
2303 mocchiut 1.1 //
2304     this->HandleRunFragments(false,true,evbefh,upperentry);
2305     //
2306     };
2307     };
2308     //
2309     };
2310     //
2311     } else {
2312     //
2313 mocchiut 1.32 if ( IsDebug() ) printf(" We have runtrailers \n");
2314 mocchiut 1.1 Int_t conptt = -1;
2315     for (Int_t ptt=0; ptt<rtev; ptt++){
2316     //
2317     rt->GetEntry(ptt);
2318     pht = eht->GetPscuHeader();
2319     pktt = pht->GetCounter();
2320     obtt = pht->GetOrbitalTime();
2321     //
2322     if ( PKT(pktt) >= PKT(pktfirst) && PKT(pktt) <= upperpkt ){
2323     //
2324     conptt++;
2325     //
2326     cod = eht->GetCounter();
2327     ptht = cod->Get(pctp->RunHeader) - 1;
2328     // evbeft = cod->Get(pctp->Physics);
2329     tcod = (UInt_t)cod->Get(pctp->Physics);
2330 mocchiut 1.17 if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2331 mocchiut 1.1 evbeft = TMath::Min(upperentry,(tcod-1));
2332     if ( debug ) printf(" Loop in runtrailers, evbeft is %u upperentry %u cod->getetc %u \n",evbeft,upperentry,cod->Get(pctp->Physics));
2333     //
2334     // if ( !conptt && !(ptht+1) ){ // here we assume ptht+1 = 0 at the beginning of the interval that could not be true if the interval start from middle file... it must be equal to the number of RH before the interval that is not counted anywhere
2335     if ( !conptt && (ptht+1) == nrhbef ){
2336     //
2337     if ( IsDebug() ) printf(" Piece of run at the beginning of the file %i %i %u \n",ptht,pth,ptt);
2338     //
2339     this->HandleRunFragments(true,false,chminentry,(evbeft));
2340     //
2341     pth = ptht; // ??
2342     //
2343     } else if ( pth == ptht ){
2344     //
2345     if ( IsDebug() ) printf(" Missing header %i %i %u\n",ptht,pth,ptt);
2346     //
2347     if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
2348     rt->GetEntry(ptt-1);
2349     cod = eht->GetCounter();
2350     tcod = (UInt_t)cod->Get(pctp->Physics);
2351     evbefh = TMath::Max(chminentry,tcod);
2352     //evbefh = cod->Get(pctp->Physics);
2353     rt->GetEntry(ptt);
2354     pht = eht->GetPscuHeader();
2355     //
2356     if ( IsDebug() ) printf(" Try to find the beginning of a run which has only the runtrailer %i %i %u \n",ptht,pth,ptt);
2357     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
2358     //
2359     this->HandleMissingHoT(true,false,evbefh,(evbeft));
2360     //
2361     } else {
2362     //
2363     rh->GetEntry(ptht);
2364     phh = ehh->GetPscuHeader();
2365     pkth = phh->GetCounter();
2366     obth = phh->GetOrbitalTime();
2367     cod = ehh->GetCounter();
2368     tcod = (UInt_t)cod->Get(pctp->Physics);
2369 mocchiut 1.17 if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2370 mocchiut 1.1 evbefh = TMath::Max(chminentry,(tcod-1));
2371     //
2372     if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){
2373     if ( IsDebug() ) printf(" Could be a good run, we have a runheader followed by a runtrailer %i %i %u\n",ptht,pth,ptt);
2374     //
2375     // evbefh = cod->Get(pctp->Physics);
2376     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
2377     //
2378     // handle this run
2379     //
2380     this->HandleRun();
2381     //
2382     //
2383     if ( debug ) printf(" Standard case, evbefh is %u chminentry %u cod->getetc %u \n",evbefh,chminentry,cod->Get(pctp->Physics));
2384     //
2385     } else {
2386     // missing header in the considered interval!
2387     if ( debug ) printf(" Missing header in the considered interval \n");
2388     this->HandleRunFragments(true,false,evbefh,evbeft);
2389     };
2390     //
2391     if ( PKT(pkth)>PKT(pktfirst) && OBT(obth)>OBT(obtfirst) && PKT(pkth)<=(upperpkt) && !conptt ){
2392     //
2393     if ( IsDebug() ) printf(" Piece of run at the beginning of the file WITH NO RUNTRAILER evbefh = %u \n",evbefh);
2394     //
2395     if ( evbefh == 0 ) {
2396     //
2397     if ( !chewbacca ){
2398     signal = 8;
2399     if ( IsDebug() ) printf(" Not supported yet: run with no events, no runtrailer, no runheader \n");
2400     } else {
2401     if ( debug ) printf(" The file does not start with a physics packet, no problem continue \n");
2402     };
2403     //
2404     } else {
2405     //
2406     this->HandleRunFragments(true,true,chminentry,(evbefh));
2407     //
2408 mocchiut 1.10 // what about pth=ptht-1
2409 mocchiut 1.1 };
2410     };
2411     //
2412     //
2413     if ( (ptht - pth) > 1 ){
2414     //
2415     if ( IsDebug() ) printf(" Missing runtrailers! \n");
2416     if ( IsDebug() ) printf(" Attention there is a jump in the runheader counter %i %i %u \n",ptht,pth,ptt);
2417     // is not the consecutive header
2418     while ( pth != ptht ){
2419     //
2420     // treat the header(s) in the middle and then go to the next header, repeat until you reach the correct header.
2421     //
2422     pth++;
2423     //
2424     rh->GetEntry(pth+1);
2425     phh = ehh->GetPscuHeader();
2426     pktt = phh->GetCounter();
2427     obtt = phh->GetOrbitalTime();
2428     cod = ehh->GetCounter();
2429     // evbeft = cod->Get(pctp->Physics);
2430     tcod = (UInt_t)cod->Get(pctp->Physics);
2431 mocchiut 1.17 if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2432 mocchiut 1.1 evbeft = TMath::Min(upperentry,(tcod-1));
2433     rh->GetEntry(pth);
2434     phh = ehh->GetPscuHeader();
2435     cod = ehh->GetCounter();
2436     pkth = phh->GetCounter();
2437     obth = phh->GetOrbitalTime();
2438     //evbefh = cod->Get(pctp->Physics);
2439     tcod = (UInt_t)cod->Get(pctp->Physics);
2440     evbefh = TMath::Max(chminentry,tcod);
2441     //
2442     if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt && pth != ptht ){
2443     //
2444     if ( IsDebug() ) printf(" Try to find the end of a run which has only the runheader %i %i %u \n",ptht,pth,ptt);
2445     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
2446     //
2447     this->HandleMissingHoT(false,true,evbefh,evbeft);
2448     //
2449     };
2450     };
2451     //
2452     } else if ( !(ptht - pth) ){
2453     //
2454     if ( IsDebug() ) printf(" Missing runheader! \n");
2455     if ( IsDebug() ) printf(" Attention! the runheader counter did not changed %i %i %u \n",ptht,pth,ptt);
2456     if ( IsDebug() ) printf(" The run should have already been handled by HandleRun() \n");
2457     if ( PEDANTIC ) throw -87;
2458     //
2459     } else {
2460     //
2461     // go on with next header
2462     //
2463     pth = ptht;
2464     };
2465     //
2466     };
2467     //
2468     // if ( ptt+1 == rtev){
2469     if ( conptt+1 == nrtev ){
2470     // if ( conptt+1 == (nrtev+nrtbef )){
2471 mocchiut 1.10 if ( IsDebug() ) printf(" >>>>>>>>>>> %i %u %i %u \n",ptht,rhev,nrtev,conptt);
2472 mocchiut 1.1 ptht++;
2473 mocchiut 1.15 if ( ptht < rhev ){
2474 mocchiut 1.1 rh->GetEntry(ptht);
2475 mocchiut 1.10 //pth++;
2476     //if ( pth < rhev ){
2477     //rh->GetEntry(pth);
2478 mocchiut 1.1 phh = ehh->GetPscuHeader();
2479     pkth = phh->GetCounter();
2480     obth = phh->GetOrbitalTime();
2481     cod = ehh->GetCounter();
2482     tcod = (UInt_t)cod->Get(pctp->Physics);
2483     evbefh = TMath::Max(chminentry,tcod);
2484     if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){
2485     // evbefh = cod->Get(pctp->Physics);
2486     if ( IsDebug() ) printf(" Piece of run at the end of file %u %u %u \n",pkth,obth,obtt);
2487     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i %i %u \n",ptht,pth,ptt);
2488     if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u \n",rhev);
2489     if ( IsDebug() ) printf(" evbefh %u upperentry %u \n",(UInt_t)evbefh,upperentry);
2490     //
2491     this->HandleRunFragments(false,true,evbefh,upperentry);
2492     //
2493     };
2494     } else {
2495     //
2496     // check if we have a fragment with no header
2497     //
2498     if ( (UInt_t)evbeft < upperentry-1 && upperentry>0 ){
2499     if ( IsDebug() ) printf(" Piece of run at the end of the file with NO RUNHEADER! evbeft %u upperentry-1 %u \n",(UInt_t)evbeft,upperentry-1);
2500     //
2501 mocchiut 1.15 // if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
2502     if ( (rtev-1) < 0 || ptt < 0 ) throw -15; // should never arrive here!
2503 mocchiut 1.10 // rt->GetEntry(ptt-1);
2504     rt->GetEntry(rtev-1);
2505 mocchiut 1.1 cod = eht->GetCounter();
2506 mocchiut 1.10 tcod = (UInt_t)cod->Get(pctp->Physics)+1;
2507 mocchiut 1.1 evbefh = TMath::Max(chminentry,tcod);
2508     // evbefh = cod->Get(pctp->Physics);
2509     rt->GetEntry(ptt);
2510     pht = eht->GetPscuHeader();
2511     this->HandleRunFragments(true,true,evbefh,upperentry);
2512     };
2513     };
2514     };
2515     //
2516     };
2517     };
2518     };
2519     //
2520     return(signal);
2521     };
2522    
2523     /**
2524     *
2525     * Check if the run has already been inserted
2526     *
2527     */
2528     Bool_t PamelaDBOperations::IsRunAlreadyInserted(){
2529     //
2530     TSQLResult *result = 0;
2531     TSQLRow *row = 0;
2532     //
2533     stringstream oss;
2534     oss.str("");
2535     //
2536     // the where clause is of the type: boot_number = _our_boot && (
2537     // ( runhead_time >= (_our_runhead_time-10) && runtrail_time <= (_our_runtrail_time+10) &&
2538     // ( runhead_obt >= _our_runheadobt || runhead_pkt >= _our_runheadpkt ) &&
2539     // ( runtrail_obt >= _our_runtrailobt || runtrail_pkt >= _our_runtrailpkt ) )
2540     // ||
2541     // ( runhead_time <= _our_runhead_time && runtrail_time >= _our_runtrail_time &&
2542     // ( runhead_obt <= _our_runheadobt || runhead_pkt <= _our_runheadpkt ) &&
2543     // ( runtrail_obt <= _our_runtrailobt || runtrail_pkt <= _our_runtrailpkt ) )
2544     // ||
2545     // ( runhead_time = _our_runhead_time && runtrail_time = _our_runtrail_time && nevents > 100 )
2546     // )
2547     // ||
2548     // ( runhead_time = _our_runhead_time && runtrail_time > _our_runtrail_time && nevents > 100 )
2549     // )
2550     // ||
2551     // ( runhead_time < _our_runhead_time && runtrail_time = _our_runtrail_time && nevents > 100 )
2552     // )
2553     //
2554     oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
2555     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2556     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2557     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2558     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2559     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2560     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2561     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2562     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2563     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2564     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2565     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2566     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR ";
2567     if ( glrun->GetNEVENTS() < 100 ){
2568     oss<<" RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << "))); ";
2569     } else {
2570     oss << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ")) OR "
2571     << " (RUNHEADER_TIME=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " // these two lines in a certain way disable the patch below...
2572     << " RUNTRAILER_TIME=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100) OR" //
2573     << " (RUNHEADER_TIME=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " //
2574     << " RUNTRAILER_TIME>" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100) OR" //
2575     << " (RUNHEADER_TIME<" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " //
2576     << " RUNTRAILER_TIME=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100)" //
2577     << " );";
2578     };
2579     //
2580     if ( IsDebug() ) printf(" THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u NEVENTS %u\n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT(),glrun->GetNEVENTS());
2581     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
2582     result = conn->Query(oss.str().c_str());
2583     //
2584     if ( !result ) throw -4;
2585     //
2586     row = result->Next();
2587     //
2588     if ( !row ){
2589     if ( IsDebug() ) printf(" The run is new \n");
2590     if ( IsDebug() ) printf(" -> fill the DB \n");
2591     return(false); // the file has not been inserted in the DB, go on.
2592     };
2593     //
2594     Bool_t signal = true;
2595     //
2596     while ( row != NULL ){
2597     if ( IsDebug() ) printf(" A run exists with runheader and runtrailer time and packets compatible with this one \n");
2598     //
2599     // the run has already been inserted
2600     //
2601     if ( signal && IsDebug() ) printf(" The run has already been inserted\n");
2602     if ( PEDANTIC ) throw -86;
2603     return(true); //<<<<<<<<<<<<<<<<<<<<<<<< patch follows, uncomment here
2604     //
2605     // PATCH!
2606     // 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
2607     // while the old run doesn't have it 3) we have more events than the old run
2608     //
2609     if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
2610     //
2611     if ( IsDebug() ) printf(" The new run has more events than the old one \n");
2612     glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2613 mocchiut 1.2 // oss.str("");
2614     // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2615     // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
2616     // conn->Query(oss.str().c_str());
2617 mocchiut 1.1 if ( signal ) signal = false;
2618     goto gonext;
2619     //
2620     } else if ( glrun->GetNEVENTS() < (UInt_t)atoll(row->GetField(1)) ){
2621     if ( IsDebug() ) printf(" The new run has less events than the old one \n");
2622     if ( IsDebug() ) printf(" The run is already inserted \n");
2623     goto gonext;
2624     };
2625     //
2626     if ( glrun->GetTRK_CALIB() && !(UInt_t)atoll(row->GetField(2)) ){
2627     //
2628     if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");
2629     //
2630     glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2631 mocchiut 1.2 // oss.str("");
2632     // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2633     // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
2634     // conn->Query(oss.str().c_str());
2635 mocchiut 1.1 //
2636     if ( signal ) signal = false;
2637     goto gonext;
2638     } else if ( !glrun->GetTRK_CALIB() && (UInt_t)atoll(row->GetField(2)) ){
2639     if ( IsDebug() ) printf(" The new run has the same number of events but miss the runheader the old has the runheader \n");
2640     if ( IsDebug() ) printf(" The run is already inserted \n");
2641     goto gonext;
2642     };
2643     //
2644     if ( glrun->GetPKT_COUNTER() && !(UInt_t)atoll(row->GetField(3)) ){
2645     //
2646     if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");
2647     //
2648     glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2649 mocchiut 1.2 // oss.str("");
2650     // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2651     // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
2652     // conn->Query(oss.str().c_str());
2653 mocchiut 1.1 if ( signal ) signal = false;
2654     //
2655     };
2656     //
2657     gonext:
2658     // END PATCH!
2659     //
2660     row = result->Next();
2661     //
2662     };
2663     //
2664     delete result;
2665     //
2666     if ( signal && IsDebug() ) printf(" The run has already been inserted \n");
2667     if ( !signal && IsDebug() ) printf(" The run existed and has been overridden, fill the DB \n");
2668     if ( PEDANTIC ) throw -86;
2669     return(signal);
2670     };
2671    
2672     /**
2673     * Handle runs which seems to be good ones.
2674     **/
2675     void PamelaDBOperations::HandleRun(){
2676     ULong64_t chkpkt = 0;
2677     ULong64_t pktt = (ULong64_t)PKT(pht->GetCounter());
2678     ULong64_t pkth = (ULong64_t)PKT(phh->GetCounter());
2679     //
2680     chkpkt = pkth + (ULong64_t)runt->PKT_COUNTER + 1ULL + 1ULL;
2681     //
2682     if ( labs(chkpkt-pktt)<2 ){
2683     //
2684     if ( IsDebug() ) printf(" check %llu pktt %llu \n",chkpkt,pktt);
2685     //
2686     // it must be a good run, fill the db
2687     //
2688     this->FillClass();
2689     //
2690     if ( !IsRunAlreadyInserted() ){
2691     glrun->SetID(this->AssignRunID());
2692     glrun->SetID_RUN_FRAG(0);
2693     glrun->Fill_GL_RUN(conn);
2694     };
2695     } else {
2696     //
2697     if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt);
2698     if ( IsDebug() ) printf(" try to recover run(s) without runheader and runtrailer between runheader and runtrailer\n");
2699     //
2700     this->HandleSuspiciousRun();
2701     //
2702     };
2703     //
2704     //
2705     return;
2706     };
2707    
2708    
2709     /**
2710     * Handle run fragments at the beginning or at the end of the file
2711     **/
2712     void PamelaDBOperations::HandleRunFragments(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
2713     //
2714     UInt_t rhfirstev = firstev;
2715     UInt_t rtlastev = lastev;
2716     Bool_t found = false;
2717     Bool_t foundinrun = false;
2718     //
2719     TSQLResult *result = 0;
2720     TSQLRow *row = 0;
2721     //
2722     stringstream oss;
2723     oss.str("");
2724     //
2725     // is the piece of run good (no other packets inside)?
2726     //
2727     if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
2728     //
2729     // if not, handle other pieces and continue with the first one
2730     //
2731     if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
2732     //
2733     } else {
2734     //
2735 mocchiut 1.2 // we have now the good first piece of a run, fill the glrun object
2736     //
2737 mocchiut 1.16 if ( firstev != (lastev+1) ){ // could be a problem sometimes (?)
2738     if ( rhfirstev != firstev && !mishead ) mishead = true;
2739     if ( rtlastev != lastev && !mistrail ) mistrail = true;
2740     };
2741 mocchiut 1.1 //
2742 mocchiut 1.16 if ( IsDebug() ) printf(" bhere firstev is %i lastev is %i \n",firstev,lastev);
2743 mocchiut 1.2 this->FillClass(mishead,mistrail,firstev,lastev);
2744 mocchiut 1.16 if ( IsDebug() ) printf(" chere firstev is %i lastev is %i \n",firstev,lastev);
2745 mocchiut 1.1 //
2746 mocchiut 1.2 if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
2747     if ( IsDebug() ) printf(" C THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());
2748 mocchiut 1.1 //
2749 mocchiut 1.2 // First of all insert the run in the fragment table...
2750 mocchiut 1.1 //
2751     oss.str("");
2752 mocchiut 1.2 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
2753     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2754     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2755     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2756     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2757     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2758     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2759     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2760     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2761     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2762     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2763     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2764     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2765     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2766 mocchiut 1.1 //
2767 mocchiut 1.2 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
2768 mocchiut 1.1 result = conn->Query(oss.str().c_str());
2769     //
2770     if ( !result ) throw -4;
2771     //
2772     row = result->Next();
2773     //
2774 mocchiut 1.2 if ( !row ){
2775     //
2776     // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
2777     //
2778     if ( IsDebug() ) printf(" The run is new \n");
2779     if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
2780     //
2781     glrun->SetID(this->AssignRunID());
2782     glrun->SetID_RUN_FRAG(0);
2783     glrun->Fill_GL_RUN_FRAGMENTS(conn);
2784     //
2785     } else {
2786 mocchiut 1.21 if ( IsDebug() ) printf(" The run is already present in the fragment table, relaxed %u \n",RELAXED);
2787     if ( !RELAXED ){
2788     if ( PEDANTIC ) throw -69;
2789     return;
2790     } else {
2791     glrun->SetID((UInt_t)atoll(row->GetField(0)));
2792     glrun->SetID_RUN_FRAG(0);
2793     };
2794 mocchiut 1.2 };
2795     //
2796     if ( chewbacca && mishead && mistrail ) goto justcheck;
2797     //
2798     // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
2799     //
2800     if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
2801     // missing it no way we can found a piece in the frag table
2802 mocchiut 1.1 //
2803     oss.str("");
2804 mocchiut 1.2 oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
2805 mocchiut 1.1 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2806     << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2807     << " ID != " << glrun->ID
2808 mocchiut 1.2 << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
2809 mocchiut 1.1 //
2810 mocchiut 1.2 if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
2811 mocchiut 1.1 result = conn->Query(oss.str().c_str());
2812     //
2813     if ( !result ) throw -4;
2814     //
2815     row = result->Next();
2816     //
2817 mocchiut 1.2 if ( !row && NoFrag() ){
2818     //
2819     oss.str("");
2820     oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
2821     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2822     << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2823     << " ID != " << glrun->ID
2824     << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
2825     //
2826     if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2827     result = conn->Query(oss.str().c_str());
2828     //
2829     if ( !result ) throw -4;
2830     //
2831     foundinrun = true;
2832     //
2833     row = result->Next();
2834     //
2835     };
2836 mocchiut 1.1 //
2837 mocchiut 1.2 if ( !row ){
2838     if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2839     found = false;
2840     } else {
2841     //
2842     found = false; // default value
2843     //
2844 mocchiut 1.16 if ( IsDebug() ) printf(" A Found a possible candidate, checking if it is the good one... \n");
2845 mocchiut 1.1 //
2846 mocchiut 1.2 // if we have both runheader and runtrailer we can check with pkt_counter:
2847 mocchiut 1.1 //
2848 mocchiut 1.2 if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
2849     ULong64_t chkpkt = 0;
2850     ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
2851     ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
2852     //
2853     chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;
2854 mocchiut 1.1 //
2855 mocchiut 1.2 if ( labs(chkpkt-pktt)<2 ){
2856     //
2857     if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2858     //
2859     found = true;
2860 mocchiut 1.16 if ( IsDebug() ) printf(" where firstev is %i lastev is %i \n",firstev,lastev);
2861 mocchiut 1.2 //
2862     } else {
2863     //
2864     if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
2865     //
2866     found = false;
2867     //
2868     };
2869     };
2870     if ( !found && chewbacca ) goto justcheck;
2871     if ( !found ){
2872 mocchiut 1.1 //
2873 mocchiut 1.2 // 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
2874 mocchiut 1.1 //
2875 mocchiut 1.2 ULong64_t chkpkt1 = 0;
2876     ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2877     ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2878     chkpkt1 = labs(orunh1-dbrunt1);
2879 mocchiut 1.1 //
2880 mocchiut 1.2 ULong64_t chkpkt2 = 0;
2881     ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
2882     ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2883     chkpkt2 = labs(orunh2-dbrunt2);
2884 mocchiut 1.1 //
2885 mocchiut 1.2 ULong64_t chkpkt3 = 0;
2886     ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
2887     ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2888     chkpkt3 = labs(orunh3-dbrunt3);
2889 mocchiut 1.1 //
2890 mocchiut 1.2 if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2891     // if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
2892     //
2893     if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2894     //
2895     found = true;
2896     //
2897     } else {
2898     //
2899     if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2900     //
2901     found = false;
2902     //
2903     };
2904 mocchiut 1.1 };
2905     };
2906 mocchiut 1.2 //
2907     if ( found ){
2908     //
2909     // 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
2910     //
2911     if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2912     //
2913     if ( foundinrun ){
2914     glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2915     glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2916     };
2917 mocchiut 1.1 //
2918 mocchiut 1.2 GL_RUN *glrun1 = new GL_RUN();
2919 mocchiut 1.1 //
2920 mocchiut 1.2 // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2921     //
2922     oss.str("");
2923     oss << " ID="<<row->GetField(0)<<";";
2924     //
2925     glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
2926 mocchiut 1.16 if ( IsDebug() ) printf(" there firstev is %i lastev is %i \n",firstev,lastev);
2927 mocchiut 1.2 //
2928     // merge infos
2929     //
2930     UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
2931     ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
2932     UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
2933     ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
2934     if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2935     TTree *T= 0;
2936     T = (TTree*)file->Get("Physics");
2937     if ( !T || T->IsZombie() ) throw -16;
2938     EventHeader *eh = 0;
2939     PscuHeader *ph = 0;
2940     T->SetBranchAddress("Header", &eh);
2941     while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
2942     T->GetEntry(firstev);
2943     ph = eh->GetPscuHeader();
2944     bpkt = PKT(ph->GetCounter());
2945     bobt = OBT(ph->GetOrbitalTime());
2946     firstev++;
2947     if ( PEDANTIC ) throw -71;
2948     };
2949     if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2950 mocchiut 1.1 //
2951 mocchiut 1.2 glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2952     glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
2953     glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
2954     glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
2955     glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
2956     //
2957 mocchiut 1.16 if ( IsDebug() ) printf(" here firstev is %i lastev is %i \n",firstev,lastev);
2958     //
2959 mocchiut 1.2 glrun->SetEV_FROM(firstev);
2960     glrun->SetNEVENTS(lastev-firstev+1);
2961     //
2962     glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
2963     glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
2964     glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
2965     glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
2966     glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
2967     glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
2968     glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
2969     glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
2970     glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
2971     glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
2972     glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());
2973     glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());
2974     glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());
2975     glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());
2976     glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());
2977     glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());
2978     //
2979     if ( glrun1->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glrun1->GetPHYSENDRUN_MASK_S3S2S12());
2980     if ( glrun1->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glrun1->GetPHYSENDRUN_MASK_S11CRC());
2981     //
2982     if ( !IsRunAlreadyInserted() ){
2983     //
2984     // glrun->SetID(this->AssignRunID());
2985     glrun->SetID_RUN_FRAG(glrun1->GetID());
2986     glrun->Fill_GL_RUN(conn);
2987 mocchiut 1.1 //
2988 mocchiut 1.2 // set id number
2989 mocchiut 1.1 //
2990 mocchiut 1.2 glrun1->SetID_RUN_FRAG(glrun->GetID());
2991     glrun1->Fill_GL_RUN(conn);
2992 mocchiut 1.1 //
2993     };
2994 mocchiut 1.2 // delete old entry in fragment table
2995     //
2996     glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2997     glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2998 mocchiut 1.1 //
2999 mocchiut 1.2 delete glrun1;
3000 mocchiut 1.1 //
3001     //
3002 mocchiut 1.2 return;
3003 mocchiut 1.1 //
3004     };
3005     //
3006     };
3007     //
3008 mocchiut 1.2 if ( mistrail && ( rtlastev == lastev || chewbacca )) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
3009     // missing it no way we can found a piece in the frag table
3010 mocchiut 1.1 //
3011     oss.str("");
3012 mocchiut 1.2 oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "
3013 mocchiut 1.1 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3014     << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
3015     << " ID != " << glrun->ID
3016 mocchiut 1.2 << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
3017 mocchiut 1.1 //
3018 mocchiut 1.2 if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
3019 mocchiut 1.1 result = conn->Query(oss.str().c_str());
3020     //
3021     if ( !result ) throw -4;
3022     //
3023     row = result->Next();
3024     //
3025 mocchiut 1.2 if ( !row && NoFrag() ){
3026     //
3027     oss.str("");
3028     oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
3029     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3030     << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
3031     << " ID != " << glrun->ID
3032     << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
3033     //
3034     if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
3035     result = conn->Query(oss.str().c_str());
3036     //
3037     if ( !result ) throw -4;
3038     //
3039     foundinrun = true;
3040     row = result->Next();
3041     //
3042     };
3043 mocchiut 1.1 //
3044 mocchiut 1.2 if ( !row ){
3045     if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
3046     found = false;
3047     } else {
3048     //
3049     found = false; // default value
3050     //
3051 mocchiut 1.16 if ( IsDebug() ) printf(" B Found a possible candidate, checking if it is the good one... \n");
3052 mocchiut 1.1 //
3053 mocchiut 1.2 // if we have both runheader and runtrailer we can check with pkt_counter:
3054 mocchiut 1.1 //
3055 mocchiut 1.2 if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){
3056     ULong64_t chkpkt = 0;
3057     ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
3058     ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
3059 mocchiut 1.1 //
3060 mocchiut 1.2 chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;
3061 mocchiut 1.1 //
3062 mocchiut 1.2 if ( labs(chkpkt-pktt)<2 ){
3063     //
3064     if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
3065     //
3066     found = true;
3067     //
3068     } else {
3069     //
3070     if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
3071     //
3072     found = false;
3073     //
3074     };
3075     };
3076     if ( !found && chewbacca ) goto justcheck;
3077     if ( !found ){
3078     //
3079     // 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
3080 mocchiut 1.1 //
3081 mocchiut 1.2 ULong64_t chkpkt1 = 0;
3082     ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
3083     ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
3084     chkpkt1 = labs(orunh1-dbrunt1);
3085 mocchiut 1.1 //
3086 mocchiut 1.2 ULong64_t chkpkt2 = 0;
3087     ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT());
3088     ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
3089     chkpkt2 = labs(orunh2-dbrunt2);
3090 mocchiut 1.1 //
3091 mocchiut 1.2 ULong64_t chkpkt3 = 0;
3092     ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME());
3093     ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
3094     chkpkt3 = labs(orunh3-dbrunt3);
3095 mocchiut 1.1 //
3096 mocchiut 1.2 if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
3097     //
3098     if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
3099     //
3100     found = true;
3101     //
3102     } else {
3103     //
3104     if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
3105     //
3106     found = false;
3107     //
3108     };
3109 mocchiut 1.1 };
3110     };
3111 mocchiut 1.2 //
3112     if ( found ){
3113     //
3114     // 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
3115     //
3116     if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
3117     //
3118     if ( foundinrun ){
3119     glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3120     glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
3121     };
3122 mocchiut 1.1 //
3123 mocchiut 1.2 GL_RUN *glrun1 = new GL_RUN();
3124 mocchiut 1.1 //
3125 mocchiut 1.2 // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
3126 mocchiut 1.1 //
3127 mocchiut 1.2 oss.str("");
3128     oss << " ID="<<row->GetField(0)<<";";
3129     //
3130     glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runtrailer infos
3131 mocchiut 1.1 //
3132 mocchiut 1.2 // merge infos
3133 mocchiut 1.1 //
3134 mocchiut 1.2 UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT());
3135     ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
3136     UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
3137     ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
3138     if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
3139     TTree *T= 0;
3140     T = (TTree*)file->Get("Physics");
3141     if ( !T || T->IsZombie() ) throw -16;
3142     EventHeader *eh = 0;
3143     PscuHeader *ph = 0;
3144     T->SetBranchAddress("Header", &eh);
3145     while ( apkt > bpkt && aobt > bobt && lastev > 0 ){
3146     T->GetEntry(lastev);
3147     ph = eh->GetPscuHeader();
3148     apkt = PKT(ph->GetCounter());
3149     aobt = OBT(ph->GetOrbitalTime());
3150     lastev--;
3151 mocchiut 1.21 if ( PEDANTIC && !RELAXED) throw -72;
3152 mocchiut 1.2 };
3153     if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
3154     //
3155     glrun->SetEV_TO(lastev);
3156     glrun->SetNEVENTS(lastev-firstev+1);
3157     glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());
3158     glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER());
3159     glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME());
3160     glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
3161     glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
3162     //
3163     glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
3164     glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
3165     glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
3166     glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP());
3167     glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE());
3168     glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE());
3169     glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A());
3170     glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B());
3171     glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO());
3172     glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO());
3173     glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB());
3174     glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE());
3175     glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED());
3176     glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK());
3177     glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC());
3178     glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC());
3179     //
3180     if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ) glrun1->SetPHYSENDRUN_MASK_S3S2S12(glrun->GetPHYSENDRUN_MASK_S3S2S12());
3181     if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) glrun1->SetPHYSENDRUN_MASK_S11CRC(glrun->GetPHYSENDRUN_MASK_S11CRC());
3182     //
3183     if ( !IsRunAlreadyInserted() ){
3184     //
3185     // glrun->SetID(this->AssignRunID());
3186 mocchiut 1.1 //
3187 mocchiut 1.2 glrun->SetID_RUN_FRAG(glrun1->GetID());
3188     glrun->Fill_GL_RUN(conn);
3189 mocchiut 1.1 //
3190 mocchiut 1.2 // set id number
3191 mocchiut 1.1 //
3192 mocchiut 1.2 glrun1->SetID_RUN_FRAG(glrun->GetID());
3193     glrun1->Fill_GL_RUN(conn);
3194 mocchiut 1.1 //
3195     };
3196 mocchiut 1.2 //
3197     // delete old entries in fragment table
3198     //
3199     glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3200     glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3201     //
3202     delete glrun1;
3203     //
3204     return;
3205     //
3206 mocchiut 1.1 };
3207 mocchiut 1.2 //
3208 mocchiut 1.1 };
3209     //
3210 mocchiut 1.2 justcheck:
3211     //
3212     if ( !found ){
3213 mocchiut 1.1 //
3214 mocchiut 1.2 if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
3215 mocchiut 1.1 //
3216 mocchiut 1.2 // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
3217 mocchiut 1.1 //
3218     oss.str("");
3219 mocchiut 1.21 oss << " SELECT ID,ID_ROOT_L0 FROM GL_RUN WHERE "
3220 mocchiut 1.2 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3221     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3222     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3223     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3224     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3225     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3226     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3227     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3228     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3229     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3230     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3231     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3232     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3233 mocchiut 1.1 //
3234 mocchiut 1.2 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3235     result = conn->Query(oss.str().c_str());
3236 mocchiut 1.1 //
3237 mocchiut 1.2 if ( !result ) throw -4;
3238 mocchiut 1.1 //
3239 mocchiut 1.2 row = result->Next();
3240 mocchiut 1.1 //
3241 mocchiut 1.2 if ( row ){
3242 mocchiut 1.21 //
3243     // we end up here if chewbacca and we have RH---| small gap |---RT, in this case the two pieces are attached and moved to GL_RUN. We are now processing the
3244     // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number.
3245     //
3246     // 090112 [8RED (-70): RUN ALREADY INSERTED]
3247 mocchiut 1.31 // 093012 [8RED (-70): RUN ALREADY INSERTED] try to be more general... the run has not been instered yet if the runheader-trailer PKT interval is not covered by already inserted runs, NOTICE: we must look in the GL_RUN_TRASH table!
3248 mocchiut 1.21 //
3249     Bool_t OK = false;
3250     UInt_t IDRL2A = 0;
3251     UInt_t IDRL2B = 0;
3252     if ( chewbacca ){
3253 mocchiut 1.31 if ( result->GetRowCount() >= 2 ){
3254     //
3255     if ( IsDebug() ) printf(" RH---| gap |---RT case, number of pieces in the GL_RUN table: %i \n",result->GetRowCount());
3256     OK = true;
3257 mocchiut 1.21 IDRL2A = (UInt_t)atoll(row->GetField(0));
3258 mocchiut 1.31 //
3259     while ( row ){
3260     //
3261     TSQLResult *trresult = 0;
3262     TSQLRow *trrow = 0;
3263 mocchiut 1.21 //
3264 mocchiut 1.31 stringstream tross;
3265     tross.str("");
3266 mocchiut 1.21 //
3267 mocchiut 1.31 tross << " SELECT RUNHEADER_PKT, RUNTRAILER_PKT FROM GL_RUN_TRASH where ID=" << row->GetField(0) << ";";
3268     if ( IsDebug() ) printf(" check in the gl_run_trash table for pkt intervals: query is \n %s \n",tross.str().c_str());
3269     trresult = conn->Query(tross.str().c_str());
3270     if ( !trresult ) throw -4;
3271     trrow = trresult->Next();
3272     if ( !trrow || trresult->GetRowCount() != 1 ){
3273     OK = false;
3274     if ( IsDebug() ) printf(" OPS! no such run (or multiple runs!) in GL_RUN_TRASH table, something wrong is going on! \n");
3275     break;
3276     };
3277 mocchiut 1.21 //
3278 mocchiut 1.31 UInt_t pktH = (UInt_t)atoll(row->GetField(0));
3279     UInt_t pktT = (UInt_t)atoll(row->GetField(1));
3280     delete trresult;
3281     if (
3282     (PKT(pktH) >= PKT(glrun->GetRUNHEADER_PKT()) && PKT(pktH) <= PKT(glrun->GetRUNTRAILER_PKT())) ||
3283     (PKT(pktT) >= PKT(glrun->GetRUNHEADER_PKT()) && PKT(pktT) <= PKT(glrun->GetRUNTRAILER_PKT())) ||
3284     (PKT(pktH) <= PKT(glrun->GetRUNTRAILER_PKT()) && PKT(pktT) >= PKT(glrun->GetRUNTRAILER_PKT())) ){
3285     OK = false;
3286     if ( IsDebug() ) printf(" The run is overlapping with already inserted ones!! \n");
3287     break;
3288     };
3289     //PKT(glrun->GetRUNHEADER_PKT()) PKT(glrun->GetRUNTRAILER_PKT())
3290     row = result->Next();
3291 mocchiut 1.21 };
3292 mocchiut 1.31
3293     // if ( result->GetRowCount() == 2 ) {
3294     // IDRL2A = (UInt_t)atoll(row->GetField(0));
3295     // UInt_t IDRL0A = (UInt_t)atoll(row->GetField(1));
3296     // row = result->Next();
3297     // IDRL2B = (UInt_t)atoll(row->GetField(0));
3298     // UInt_t IDRL0B = (UInt_t)atoll(row->GetField(1));
3299     // if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B);
3300     // // if ( IDRL0A == IDRL0B ){ //091230 why this condition???? RH could be in a file and RT in another one...
3301     // if ( true ){
3302     // TSQLResult *result2 = 0;
3303     // TSQLRow *row2 = 0;
3304     // oss.str("");
3305     // oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;";
3306     // if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3307     // result2 = conn->Query(oss.str().c_str());
3308     // //
3309     // if ( !result2 ) throw -4;
3310     // //
3311     // row2 = result2->Next();
3312     // //
3313     // if ( row2 ){
3314     // UInt_t PKA = (UInt_t)atoll(row2->GetField(0));
3315     // delete result2;
3316     // oss.str("");
3317     // oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;";
3318     // if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3319     // result2 = conn->Query(oss.str().c_str());
3320     // //
3321     // if ( !result2 ) throw -4;
3322     // //
3323     // row2 = result2->Next();
3324     // //
3325     // if ( row2 ){
3326     // UInt_t PKB = (UInt_t)atoll(row2->GetField(0));
3327     // //
3328     // if ( IsDebug() ) printf(" PKT(PKA) + 1 %llu == runheaderpkt %llu && PKB = runtrailer %llu + 1 %llu \n",PKT(PKA)+1LL, PKT(glrun->GetRUNHEADER_PKT()),PKT(PKB), PKT(glrun->GetRUNTRAILER_PKT())+1LL);
3329     // if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){
3330     // if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
3331     // OK = true;
3332     // };
3333     // };
3334     // };
3335     // };
3336     // };
3337     };
3338 mocchiut 1.21 };
3339     if ( OK ){
3340     //
3341 mocchiut 1.31 // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table // we arrive here ONLY with internal pieces of runs, never runheader or runtrailer by definition
3342 mocchiut 1.21 //
3343     // we have to update with runheader/trailer infos our run and we have to change the ROOT_ID_FRAG of the header piece ( 1/2 2/1 must become 1/3 3/2 2/1 )
3344     //
3345 mocchiut 1.31 if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
3346     //
3347 mocchiut 1.21 GL_RUN *glA = new GL_RUN();
3348     glA->Query_GL_RUN(IDRL2A,conn);
3349     //
3350 mocchiut 1.31 glrun->SetRUNHEADER_TIME(glA->GetRUNHEADER_TIME());
3351     glrun->SetRUNHEADER_OBT(glA->GetRUNHEADER_OBT());
3352     glrun->SetRUNHEADER_PKT(glA->GetRUNHEADER_PKT());
3353     //
3354     glrun->SetRUNTRAILER_TIME(glA->GetRUNTRAILER_TIME());
3355     glrun->SetRUNTRAILER_OBT(glA->GetRUNTRAILER_OBT());
3356     glrun->SetRUNTRAILER_PKT(glA->GetRUNTRAILER_PKT());
3357     //
3358     if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glA->GetPHYSENDRUN_MASK_S3S2S12());
3359     if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glA->GetPHYSENDRUN_MASK_S11CRC());
3360     //
3361 mocchiut 1.21 if ( glA->GetACQ_BUILD_INFO() != 0 ){
3362     //
3363     // the first piece contains a good runheader we can update all the other runs with correct infos!
3364     //
3365 mocchiut 1.31 glrun->SetCOMPILATIONTIMESTAMP(glA->GetCOMPILATIONTIMESTAMP());
3366     glrun->SetFAV_WRK_SCHEDULE(glA->GetFAV_WRK_SCHEDULE());
3367     glrun->SetEFF_WRK_SCHEDULE(glA->GetEFF_WRK_SCHEDULE());
3368     glrun->SetPRH_VAR_TRG_MODE_A(glA->GetPRH_VAR_TRG_MODE_A());
3369     glrun->SetPRH_VAR_TRG_MODE_B(glA->GetPRH_VAR_TRG_MODE_B());
3370     glrun->SetACQ_BUILD_INFO(glA->GetACQ_BUILD_INFO());
3371     glrun->SetACQ_VAR_INFO(glA->GetACQ_VAR_INFO());
3372     glrun->SetRM_ACQ_AFTER_CALIB(glA->GetRM_ACQ_AFTER_CALIB());
3373     glrun->SetRM_ACQ_SETTING_MODE(glA->GetRM_ACQ_SETTING_MODE());
3374     glrun->SetTRK_CALIB_USED(glA->GetTRK_CALIB_USED());
3375     glrun->SetCAL_DSP_MASK(glA->GetCAL_DSP_MASK());
3376     glrun->SetLAST_TIMESYNC(glA->GetLAST_TIMESYNC());
3377     glrun->SetOBT_TIMESYNC(glA->GetOBT_TIMESYNC());
3378 mocchiut 1.21 //
3379 mocchiut 1.31 };
3380     //
3381     if ( glA->GetPKT_READY_COUNTER() != 0 ){
3382 mocchiut 1.21 //
3383 mocchiut 1.31 // the first piece contains a good runtrailer we can update all the other runs with correct infos!
3384 mocchiut 1.21 //
3385 mocchiut 1.31 glrun->SetPKT_COUNTER(glA->GetPKT_COUNTER());
3386     glrun->SetPKT_READY_COUNTER(glA->GetPKT_READY_COUNTER());
3387 mocchiut 1.21 };
3388     //
3389     // update runheader ROOT_ID_FRAG
3390     //
3391     oss.str("");
3392     oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
3393     if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3394     conn->Query(oss.str().c_str());
3395     //
3396     oss.str("");
3397 mocchiut 1.31 oss << "SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG = " << IDRL2A << ";";
3398     if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3399     TSQLResult *tsresult = 0;
3400     TSQLRow *tsrow = 0;
3401     tsresult=conn->Query(oss.str().c_str());
3402     if ( !tsresult ) throw -4;
3403     tsrow = tsresult->Next();
3404     if ( !tsrow || tsresult->GetRowCount() != 1 ) throw -4;
3405     IDRL2B = (UInt_t)atoll(tsrow->GetField(0));
3406     glrun->SetID_RUN_FRAG(IDRL2B);
3407 mocchiut 1.21 //
3408     // fill the new run in GL_RUN
3409     //
3410     glrun->Fill_GL_RUN(conn);
3411 mocchiut 1.31 //
3412 mocchiut 1.21 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3413 mocchiut 1.23 delete glA;
3414 mocchiut 1.21 //
3415     } else {
3416 mocchiut 1.31 // //
3417     // // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table
3418     // //
3419     // // we have to update with runheader/trailer infos our run and we have to change the ROOT_ID_FRAG of the header piece ( 1/2 2/1 must become 1/3 3/2 2/1 )
3420     // //
3421     // GL_RUN *glA = new GL_RUN();
3422     // glA->Query_GL_RUN(IDRL2A,conn);
3423     // //
3424     // if ( glA->GetACQ_BUILD_INFO() != 0 ){
3425     // //
3426     // // the first piece contains a good runheader we can update all the other runs with correct infos!
3427     // //
3428     // oss.str("");
3429     // oss << "UPDATE GL_RUN_FRAGMENTS SET "
3430     // << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "
3431     // << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3432     // << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , "
3433     // << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , "
3434     // << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , "
3435     // << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , "
3436     // << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , "
3437     // << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , "
3438     // << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , "
3439     // << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , "
3440     // << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , "
3441     // << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , "
3442     // << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , "
3443     // << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , "
3444     // << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , ";
3445     // //
3446     // if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3447     // oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
3448     // if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3449     // oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , ";
3450     // //
3451     // oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC();
3452     // oss << " WHERE ID=" << glrun->GetID() << ";";
3453     // if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str());
3454     // conn->Query(oss.str().c_str());
3455     // //
3456     // } else {
3457     // //
3458     // // sig no runheader, let set anyway what is possible...
3459     // //
3460     // oss.str("");
3461     // oss << "UPDATE GL_RUN_FRAGMENTS SET "
3462     // << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , ";
3463     // //
3464     // if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3465     // oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
3466     // if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3467     // oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
3468     // //
3469     // oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3470     // << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT();
3471     // oss << " WHERE ID=" << glrun->GetID() << ";";
3472     // if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str());
3473     // conn->Query(oss.str().c_str());
3474     // };
3475     // //
3476     // // update runheader ROOT_ID_FRAG
3477     // //
3478     // oss.str("");
3479     // oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
3480     // if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3481     // conn->Query(oss.str().c_str());
3482     // //
3483     // // now let's look for runtrailer if any in the last run
3484     // //
3485     // glA->Query_GL_RUN(IDRL2B,conn);
3486     // //
3487     // if ( glA->GetPKT_READY_COUNTER() != 0 ){
3488     // //
3489     // // the first piece contains a good runtrailer we can update all the other runs with correct infos!
3490     // //
3491     // oss.str("");
3492     // oss << "UPDATE GL_RUN_FRAGMENTS SET "
3493     // << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3494     // << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "
3495     // << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , "
3496     // << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , ";
3497     // //
3498     // if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3499     // oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3500     // if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) {
3501     // oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3502     // //
3503     // oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER()
3504     // << " WHERE ID=" << glrun->GetID() << ";";
3505     // if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str());
3506     // conn->Query(oss.str().c_str());
3507     // //
3508     // } else {
3509     // //
3510     // // sig no runtrailer, let set anyway what is possible...
3511     // //
3512     // oss.str("");
3513     // oss << "UPDATE GL_RUN_FRAGMENTS SET "
3514     // << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3515     // << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , ";
3516     // //
3517     // if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3518     // oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3519     // if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){
3520     // oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3521     // //
3522     // oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()
3523     // << " WHERE ID=" << glrun->GetID() << ";";
3524     // if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str());
3525     // conn->Query(oss.str().c_str());
3526     // };
3527     // //
3528     // UInt_t myi = glrun->GetID();
3529     // oss.str("");
3530     // oss << " ID= "<< myi;
3531     // //
3532     // glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
3533     // //
3534     // // fill the new run in GL_RUN
3535     // //
3536     // glrun->SetID_RUN_FRAG(IDRL2B);
3537     // glrun->Fill_GL_RUN(conn);
3538     // glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3539     // delete glA;
3540     // //
3541     // } else {
3542 mocchiut 1.21 //
3543     // is just a repetition
3544     //
3545     if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
3546     if ( PEDANTIC ) throw -70;
3547     };
3548     //
3549 mocchiut 1.2 } else {
3550     if ( NoFrag() ){
3551     glrun->SetID_RUN_FRAG(glrun->GetID());
3552     glrun->Fill_GL_RUN(conn);
3553     glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3554     };
3555 mocchiut 1.1 };
3556     };
3557     }; // EEE
3558     //
3559     return;
3560     };
3561    
3562    
3563     /**
3564     * Handle run without header or trailer
3565     **/
3566     void PamelaDBOperations::HandleMissingHoT(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
3567     //
3568     //
3569     // is the piece of run good (no other packets inside)?
3570     //
3571     if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
3572     //
3573     // if not, handle other pieces and continue with the first one
3574     //
3575     if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
3576     //
3577     } else {
3578     //
3579     this->FillClass(mishead,mistrail,firstev,lastev);
3580     //
3581     if ( !IsRunAlreadyInserted() ){
3582     glrun->SetID(this->AssignRunID());
3583     glrun->SetID_RUN_FRAG(0);
3584     glrun->Fill_GL_RUN(conn); // it'ok we arrive here only inside a file hence in the middle of the runs...
3585 mocchiut 1.16 };
3586 mocchiut 1.1 //
3587     };
3588     //
3589     return;
3590     };
3591    
3592     /**
3593     *
3594     * check if we have non-physics packets inside the run
3595     *
3596     */
3597     Bool_t PamelaDBOperations::IsRunConsistent(Bool_t mishead, Bool_t mistrail, UInt_t &firstev, UInt_t &lastev){
3598     //
3599     EventCounter *code=0;
3600     //
3601 mocchiut 1.33 // UInt_t nevent = 0;
3602 mocchiut 1.1 UInt_t checkfirst = 0;
3603     UInt_t checklast = 0;
3604     UInt_t firstentry = 0;
3605     UInt_t lastentry = 0;
3606     UInt_t firstTime = 0;
3607     UInt_t lastTime = 0;
3608     UInt_t firstPkt = 0;
3609     UInt_t lastPkt = 0;
3610     UInt_t firstObt = 0;
3611     UInt_t lastObt = 0;
3612     //
3613     pcksList packetsNames;
3614     pcksList::iterator Iter;
3615     getPacketsNames(packetsNames);
3616     //
3617     TTree *T= 0;
3618     T =(TTree*)file->Get("Physics");
3619     if ( !T || T->IsZombie() ) throw -16;
3620     EventHeader *eh = 0;
3621     PscuHeader *ph = 0;
3622     T->SetBranchAddress("Header", &eh);
3623 mocchiut 1.33 // nevent = T->GetEntries();
3624 mocchiut 1.1 //
3625     //
3626     if ( firstev == lastev+1 || lastev == firstev ) { // no events inside the run!
3627     //if ( firstev <= lastev+1 ) { // no events inside the run!
3628     if ( IsDebug() ) printf(" Checking but no events in the run! \n");
3629 mocchiut 1.16 firstev = lastev+1; // this is necessary for files with no Physics entries, should have no influence on other files
3630 mocchiut 1.1 // return true is correct
3631     return(true);
3632     //
3633     } else {
3634     //
3635     T->GetEntry(firstev);
3636     code = eh->GetCounter();
3637     checkfirst = 0;
3638     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3639 mocchiut 1.2 if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
3640     };
3641 mocchiut 1.1 if ( IsDebug() ) printf(" Check first is %i firstev is %i\n",checkfirst,firstev);
3642     //
3643     T->GetEntry(lastev);
3644     code = eh->GetCounter();
3645     checklast = 0;
3646     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3647 mocchiut 1.2 if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
3648     };
3649 mocchiut 1.1 if ( IsDebug() ) printf(" Check last is %i lastev is %i\n",checklast,lastev);
3650     //
3651     if ( checkfirst == checklast ){
3652     //
3653     if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
3654     //
3655     return(true);
3656     //
3657     } else {
3658     //
3659 mocchiut 1.10 if ( IsDebug() ) printf(" There are no-physics packets inside the run!\n");
3660 mocchiut 1.1 //
3661     // HERE WE MUST HANDLE THAT RUNS AND GO BACK
3662     //
3663 mocchiut 1.2 // if ( IsDebug() ) printf(" Never seen this case, try to handle it anyway, it was throw -95\n");
3664 mocchiut 1.1 //
3665     Bool_t emptyruns = false;
3666     UInt_t check = 0;
3667     UInt_t lastevtemp = lastev;
3668     UInt_t firstevno = firstev;
3669 mocchiut 1.10 UInt_t rhchko=0;
3670     UInt_t rhchk=0;
3671 mocchiut 1.1 //
3672     for (UInt_t i=firstev; i<=lastev; i++){
3673     //
3674     T->GetEntry(i);
3675     code = eh->GetCounter();
3676     //
3677     check = 0;
3678     //
3679 mocchiut 1.10
3680     // if we have a runheader set lastev then exit
3681     //
3682 mocchiut 1.1 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3683     if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
3684     };
3685 mocchiut 1.10 // check here if we have a runheader
3686     rhchko = rhchk;
3687     rhchk = code->Get(GetPacketType("RunHeader"));
3688 mocchiut 1.1 //
3689     if ( checkfirst < check || i == lastev ){
3690     //
3691     firstentry = firstevno;
3692     //
3693     if ( checkfirst < check ){
3694     lastentry = i-1;
3695     } else {
3696     lastentry = i;
3697     };
3698     //
3699     if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
3700     //
3701     glrun->SetEV_FROM(firstentry);
3702     glrun->SetEV_TO(lastentry);
3703     if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
3704     if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
3705     lastentry--;
3706     };
3707     glrun->SetNEVENTS(lastentry-firstentry+1);
3708     //
3709     glrun->Set_GL_RUNH0();
3710     glrun->Set_GL_RUNT0();
3711     //
3712     glrun->SetLAST_TIMESYNC(0);
3713     glrun->SetOBT_TIMESYNC(0);
3714     //
3715     T->GetEntry(firstentry);
3716     ph = eh->GetPscuHeader();
3717     firstObt = ph->GetOrbitalTime();
3718     firstTime = this->GetAbsTime(firstObt);
3719     firstPkt = ph->GetCounter();
3720     //
3721     T->GetEntry(lastentry);
3722     ph = eh->GetPscuHeader();
3723     lastObt = ph->GetOrbitalTime();
3724     lastTime = this->GetAbsTime(lastObt);
3725     lastPkt = ph->GetCounter();
3726     //
3727     glrun->SetRUNHEADER_PKT(firstPkt);
3728 mocchiut 1.2 glrun->SetRUNTRAILER_PKT(lastPkt);
3729     //
3730     glrun->SetRUNHEADER_OBT(firstObt);
3731     glrun->SetRUNTRAILER_OBT(lastObt);
3732     //
3733     if ( IsDebug() ) printf(" A THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());
3734     if ( firstev == firstentry && !emptyruns && !mishead ){
3735     glrun->Set_GL_RUNH(runh,phh);
3736     firstTime = this->GetAbsTime(phh->GetOrbitalTime());
3737     if ( IsDebug() ) printf(" We have the runheader \n");
3738     };
3739     if ( lastev == i && !mistrail ){
3740     glrun->Set_GL_RUNT(runt,pht);
3741     lastTime = this->GetAbsTime(pht->GetOrbitalTime());
3742     if ( IsDebug() ) printf(" We have the runtrailer \n");
3743     };
3744     //
3745     if ( IsDebug() ) printf(" B THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());
3746     if ( lastentry == (firstentry-2) ){ // no events in the run
3747     emptyruns = true;
3748     if ( IsDebug() ) printf(" No events in the run \n");
3749     lastTime = firstTime;
3750     if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
3751     lastObt = glrun->RUNHEADER_OBT;
3752     lastPkt = glrun->RUNHEADER_PKT;
3753     } else {
3754     lastObt = firstObt;
3755     lastPkt = firstPkt;
3756     };
3757     glrun->SetRUNTRAILER_PKT(lastPkt);
3758     glrun->SetRUNTRAILER_OBT(lastObt);
3759     lastentry++;
3760     };
3761     //
3762     this->SetCommonGLRUN(firstTime,lastTime);
3763     this->SetPhysEndRunVariables();
3764     //
3765     if ( chminentry == firstentry ){ // EEE
3766     if ( IsDebug() ) printf(" Inside isrunconsistent found a fragment of run at the beginning of the file, put it in the fragment table \n");
3767     //
3768     // this->HandleRunFragments(true,mistrail,firstentry,lastentry); // cannot call it here since it enters a loop which will destroy the already stored variables if we arrive here from HandleRunFragments
3769     //
3770    
3771     mishead = true;
3772    
3773    
3774     UInt_t rhfirstev = firstentry;
3775     // UInt_t rtlastev = lastentry;
3776     Bool_t found = false;
3777     Bool_t foundinrun = false;
3778     //
3779     TSQLResult *result = 0;
3780     TSQLRow *row = 0;
3781     //
3782     stringstream oss;
3783     oss.str("");
3784     //
3785     // we have now the good first piece of a run, fill the glrun object
3786     //
3787     // if ( rhfirstev != firstev && !mishead ) mishead = true;
3788     // if ( rtlastev != lastev && !mistrail ) mistrail = true;
3789     //
3790     // this->FillClass(mishead,mistrail,firstev,lastev);
3791     //
3792     if ( IsDebug() ) printf("zz The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
3793     if ( IsDebug() ) printf("zz C THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());
3794     //
3795     // First of all insert the run in the fragment table...
3796     //
3797     oss.str("");
3798     oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
3799     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3800     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3801     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3802     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3803     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3804     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3805     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3806     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3807     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3808     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3809     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3810     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3811     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3812     //
3813     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3814     result = conn->Query(oss.str().c_str());
3815     //
3816     if ( !result ) throw -4;
3817     //
3818     row = result->Next();
3819     //
3820     if ( !row ){
3821     //
3822     // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
3823     //
3824     if ( IsDebug() ) printf(" The run is new \n");
3825     if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
3826     //
3827     glrun->SetID(this->AssignRunID());
3828     glrun->SetID_RUN_FRAG(0);
3829     glrun->Fill_GL_RUN_FRAGMENTS(conn);
3830     //
3831     } else {
3832     if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
3833     if ( PEDANTIC ) throw -69;
3834     // return;
3835     };
3836     //
3837     if ( chewbacca && mishead && mistrail ) goto zjustcheck;
3838     //
3839     // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
3840     //
3841     if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
3842     // missing it no way we can found a piece in the frag table
3843     //
3844     oss.str("");
3845     oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
3846     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3847     << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3848     << " ID != " << glrun->ID
3849     << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
3850     //
3851     if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
3852     result = conn->Query(oss.str().c_str());
3853     //
3854     if ( !result ) throw -4;
3855     //
3856     row = result->Next();
3857     //
3858     if ( !row && NoFrag() ){
3859     //
3860     oss.str("");
3861     oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
3862     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3863     << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3864     << " ID != " << glrun->ID
3865     << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
3866     //
3867     if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
3868     result = conn->Query(oss.str().c_str());
3869     //
3870     if ( !result ) throw -4;
3871     //
3872     foundinrun = true;
3873     //
3874     row = result->Next();
3875     //
3876     };
3877     //
3878     if ( !row ){
3879     if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
3880     found = false;
3881     } else {
3882     //
3883     found = false; // default value
3884     //
3885 mocchiut 1.16 if ( IsDebug() ) printf(" C Found a possible candidate, checking if it is the good one... \n");
3886 mocchiut 1.2 //
3887     // if we have both runheader and runtrailer we can check with pkt_counter:
3888     //
3889     if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
3890     ULong64_t chkpkt = 0;
3891     ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
3892     ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
3893     //
3894     chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;
3895     //
3896     if ( labs(chkpkt-pktt)<2 ){
3897     //
3898     if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
3899     //
3900     found = true;
3901     //
3902     } else {
3903     //
3904     if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
3905     //
3906     found = false;
3907     //
3908     };
3909     };
3910     if ( !found && chewbacca ) goto zjustcheck;
3911     if ( !found ){
3912     //
3913     // 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
3914     //
3915     ULong64_t chkpkt1 = 0;
3916     ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
3917     ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
3918     chkpkt1 = labs(orunh1-dbrunt1);
3919     //
3920     ULong64_t chkpkt2 = 0;
3921     ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
3922     ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
3923     chkpkt2 = labs(orunh2-dbrunt2);
3924     //
3925     ULong64_t chkpkt3 = 0;
3926     ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
3927     ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
3928     chkpkt3 = labs(orunh3-dbrunt3);
3929     //
3930     if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
3931     // if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
3932     //
3933     if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
3934     //
3935     found = true;
3936     //
3937     } else {
3938     //
3939     if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
3940     //
3941     found = false;
3942     //
3943     };
3944     };
3945     };
3946     //
3947     if ( found ){
3948     //
3949     // 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
3950     //
3951     if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
3952     //
3953     if ( foundinrun ){
3954     glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3955     glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
3956     };
3957     //
3958     GL_RUN *glrun1 = new GL_RUN();
3959     //
3960     // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
3961     //
3962     oss.str("");
3963     oss << " ID="<<row->GetField(0)<<";";
3964     //
3965     glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
3966     //
3967     // merge infos
3968     //
3969     UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
3970     ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
3971     UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
3972     ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
3973     if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
3974     TTree *T= 0;
3975     T = (TTree*)file->Get("Physics");
3976     if ( !T || T->IsZombie() ) throw -16;
3977     EventHeader *eh = 0;
3978     PscuHeader *ph = 0;
3979     T->SetBranchAddress("Header", &eh);
3980     while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
3981     T->GetEntry(firstev);
3982     ph = eh->GetPscuHeader();
3983     bpkt = PKT(ph->GetCounter());
3984     bobt = OBT(ph->GetOrbitalTime());
3985     firstev++;
3986     if ( PEDANTIC ) throw -71;
3987     };
3988     if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
3989     //
3990     glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
3991     glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
3992     glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
3993     glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
3994     glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
3995     //
3996     glrun->SetEV_FROM(firstev);
3997     glrun->SetNEVENTS(lastev-firstev+1);
3998     //
3999     glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
4000     glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
4001     glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
4002     glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
4003     glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
4004     glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
4005     glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
4006     glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
4007     glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
4008     glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
4009     glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());
4010     glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());
4011     glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());
4012     glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());
4013     glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());
4014     glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());
4015     //
4016     if ( glrun1->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glrun1->GetPHYSENDRUN_MASK_S3S2S12());
4017     if ( glrun1->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glrun1->GetPHYSENDRUN_MASK_S11CRC());
4018     //
4019     if ( !IsRunAlreadyInserted() ){
4020     //
4021     // glrun->SetID(this->AssignRunID());
4022     glrun->SetID_RUN_FRAG(glrun1->GetID());
4023     glrun->Fill_GL_RUN(conn);
4024     //
4025     // set id number
4026     //
4027     glrun1->SetID_RUN_FRAG(glrun->GetID());
4028     glrun1->Fill_GL_RUN(conn);
4029     //
4030     };
4031     // delete old entry in fragment table
4032     //
4033     glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
4034     glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
4035     //
4036     delete glrun1;
4037     //
4038     //
4039     // return;
4040     //
4041     };
4042     //
4043 mocchiut 1.1 };
4044 mocchiut 1.2 //
4045     //
4046     zjustcheck:
4047     //
4048     if ( !found ){
4049     //
4050     if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
4051     //
4052     // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
4053     //
4054     oss.str("");
4055     oss << " SELECT ID FROM GL_RUN WHERE "
4056     << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
4057     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
4058     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
4059     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
4060     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
4061     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
4062     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
4063     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
4064     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
4065     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
4066     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
4067     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
4068     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
4069     //
4070     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
4071     result = conn->Query(oss.str().c_str());
4072     //
4073     if ( !result ) throw -4;
4074     //
4075     row = result->Next();
4076     //
4077     if ( row ){
4078     if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
4079     if ( PEDANTIC ) throw -70;
4080     } else {
4081     if ( NoFrag() ){
4082     glrun->SetID_RUN_FRAG(glrun->GetID());
4083     glrun->Fill_GL_RUN(conn);
4084     glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
4085     };
4086     };
4087     }; // EEE
4088 mocchiut 1.1
4089    
4090     } else {
4091 mocchiut 1.2 if ( !IsRunAlreadyInserted() ){
4092     glrun->SetID(this->AssignRunID());
4093     glrun->SetID_RUN_FRAG(0);
4094     glrun->Fill_GL_RUN(conn);
4095     };
4096 mocchiut 1.1 }; // EEE
4097     //
4098     firstevno = lastentry + 1;
4099     //
4100     checkfirst = check;
4101     //
4102     };
4103     //
4104 mocchiut 1.10 if ( i > firstev ){
4105     if ( rhchko != rhchk ){
4106     if ( IsDebug() ) printf("oh oh... we have a runheader! stop here and handle later the remaining piece\n");
4107     lastev = i;
4108     return(false);
4109     };
4110     };
4111     //
4112 mocchiut 1.1 if ( check == checklast && i != lastev ){
4113     lastevtemp = i - 1;
4114     i = lastev - 1;
4115     };
4116     //
4117     };
4118     //
4119     lastev = lastevtemp;
4120     //
4121     return(false);
4122     //
4123     };
4124     };
4125     //
4126     return(false); // should never arrive here
4127     };
4128    
4129     /**
4130     *
4131     * 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
4132     * 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
4133     * looking for non-physics packets inside.
4134     *
4135     */
4136     void PamelaDBOperations::HandleSuspiciousRun(){
4137     //
4138     PacketType *pctp=0;
4139     EventCounter *codt=0;
4140     EventCounter *codh=0;
4141     EventCounter *code=0;
4142     UInt_t firstev = 0;
4143     UInt_t lastev = 0;
4144 mocchiut 1.33 // UInt_t nevent = 0;
4145 mocchiut 1.1 UInt_t checkfirst = 0;
4146     UInt_t checklast = 0;
4147     UInt_t firstentry = 0;
4148     UInt_t lastentry = 0;
4149     UInt_t firstTime = 0;
4150     UInt_t lastTime = 0;
4151     UInt_t firstPkt = 0;
4152     UInt_t lastPkt = 0;
4153     UInt_t firstObt = 0;
4154     UInt_t lastObt = 0;
4155     //
4156     pcksList packetsNames;
4157     pcksList::iterator Iter;
4158     getPacketsNames(packetsNames);
4159     //
4160     TTree *rh=0;
4161     rh = (TTree*)file->Get("RunHeader");
4162     if ( !rh || rh->IsZombie() ) throw -17;
4163     TTree *T=0;
4164     T =(TTree*)file->Get("Physics");
4165     if ( !T || T->IsZombie() ) throw -16;
4166     EventHeader *eh = 0;
4167     PscuHeader *ph = 0;
4168     T->SetBranchAddress("Header", &eh);
4169 mocchiut 1.33 // nevent = T->GetEntries();
4170 mocchiut 1.1 //
4171     codt = eht->GetCounter();
4172     codh = ehh->GetCounter();
4173     firstev = codh->Get(pctp->Physics);
4174     lastev = codt->Get(pctp->Physics)-1;
4175     if ( IsDebug() ) printf(" From the current runheader firstev is %u from the runtrailer lastev is %u \n",firstev,lastev);
4176     //
4177     if ( firstev == lastev+1 ) { // no events inside the run!
4178     if ( IsDebug() ) printf(" Checking but no events in the run! \n");
4179     //
4180     this->FillClass();
4181     if ( !IsRunAlreadyInserted() ){
4182     glrun->SetID(this->AssignRunID());
4183     glrun->SetID_RUN_FRAG(0);
4184     glrun->Fill_GL_RUN(conn);
4185     };
4186     //
4187     } else {
4188     //
4189     UInt_t nrunh = 0 + codh->Get(pctp->RunHeader);
4190     UInt_t nrunh1 = 0 + codh->Get(pctp->RunHeader);
4191     T->GetEntry(firstev);
4192     code = eh->GetCounter();
4193     checkfirst = 0;
4194     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
4195 mocchiut 1.2 if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
4196     if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;
4197     };
4198 mocchiut 1.1 if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);
4199     //
4200     T->GetEntry(lastev);
4201     code = eh->GetCounter();
4202     checklast = 0;
4203     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
4204 mocchiut 1.2 if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
4205     };
4206 mocchiut 1.1 if ( IsDebug() ) printf(" Check last is %i \n",checklast);
4207     //
4208     if ( checkfirst == checklast ){
4209     //
4210     if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
4211     //
4212     this->FillClass();
4213     if ( !IsRunAlreadyInserted() ){
4214     glrun->SetID(this->AssignRunID());
4215     glrun->SetID_RUN_FRAG(0);
4216     glrun->Fill_GL_RUN(conn);
4217     };
4218     //
4219     } else {
4220     //
4221     if ( IsDebug() ) printf(" There are no-physics packets inside the run, try to separate runs \n");
4222     //
4223     Bool_t emptyruns = false;
4224     UInt_t check = 0;
4225     UInt_t firstevno = firstev;
4226     //
4227     for (UInt_t i=firstev; i<=lastev; i++){
4228     //
4229     T->GetEntry(i);
4230     code = eh->GetCounter();
4231     //
4232     check = 0;
4233     //
4234     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
4235     if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
4236     if ( !strcmp(*Iter,"RunHeader") ) nrunh++;
4237     };
4238     //
4239     if ( checkfirst < check || i == lastev ){
4240     //
4241     firstentry = firstevno;
4242     //
4243     if ( checkfirst < check ){
4244     lastentry = i-1;
4245     } else {
4246     lastentry = i;
4247     };
4248     //
4249     if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
4250     //
4251     glrun->SetEV_FROM(firstentry);
4252     glrun->SetEV_TO(lastentry);
4253     if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
4254     if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
4255     lastentry--;
4256     };
4257     glrun->SetNEVENTS(lastentry-firstentry+1);
4258     //
4259     glrun->Set_GL_RUNH0();
4260     glrun->Set_GL_RUNT0();
4261     //
4262     glrun->SetLAST_TIMESYNC(0);
4263     glrun->SetOBT_TIMESYNC(0);
4264     //
4265     T->GetEntry(firstentry);
4266     ph = eh->GetPscuHeader();
4267     firstObt = ph->GetOrbitalTime();
4268     firstTime = this->GetAbsTime(firstObt);
4269     firstPkt = ph->GetCounter();
4270     //
4271     T->GetEntry(lastentry);
4272     ph = eh->GetPscuHeader();
4273     lastObt = ph->GetOrbitalTime();
4274     lastTime = this->GetAbsTime(lastObt);
4275     lastPkt = ph->GetCounter();
4276     //
4277     glrun->SetRUNHEADER_PKT(firstPkt);
4278     glrun->SetRUNTRAILER_PKT(lastPkt);
4279     //
4280     glrun->SetRUNHEADER_OBT(firstObt);
4281     glrun->SetRUNTRAILER_OBT(lastObt);
4282     //
4283     if ( IsDebug() ) printf(" AA THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());
4284     //
4285     if ( (firstev == firstentry && !emptyruns) || nrunh == (nrunh1 + 1) ){
4286     rh->GetEntry(nrunh1-1);
4287     phh = ehh->GetPscuHeader();
4288     nrunh1++;
4289     glrun->Set_GL_RUNH(runh,phh);
4290     firstTime = this->GetAbsTime(phh->GetOrbitalTime());
4291     if ( IsDebug() ) printf(" We have the runheader \n");
4292     };
4293     if ( lastev == i && checkfirst == check ){
4294     glrun->Set_GL_RUNT(runt,pht);
4295     lastTime = this->GetAbsTime(pht->GetOrbitalTime());
4296     if ( IsDebug() ) printf(" We have the runtrailer \n");
4297     };
4298     if ( IsDebug() ) printf(" BB THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());
4299     //
4300     if ( lastentry == (firstentry-2) ){ // no events in the run
4301     emptyruns = true;
4302     if ( IsDebug() ) printf(" No events in the run \n");
4303     lastTime = firstTime;
4304     if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
4305     lastObt = glrun->RUNHEADER_OBT;
4306     lastPkt = glrun->RUNHEADER_PKT;
4307     } else {
4308     lastObt = firstObt;
4309     lastPkt = firstPkt;
4310     };
4311     glrun->SetRUNTRAILER_PKT(lastPkt);
4312     glrun->SetRUNTRAILER_OBT(lastObt);
4313     lastentry++;
4314     };
4315     //
4316     this->SetCommonGLRUN(firstTime,lastTime);
4317 mocchiut 1.2 this->SetPhysEndRunVariables();
4318 mocchiut 1.1 //
4319     if ( !IsRunAlreadyInserted() ){
4320     glrun->SetID(this->AssignRunID());
4321     glrun->SetID_RUN_FRAG(0);
4322     glrun->Fill_GL_RUN(conn);
4323     };
4324     //
4325     if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check...
4326     //
4327     firstentry = i;
4328     //
4329     lastentry = i;
4330     //
4331     if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
4332     //
4333     glrun->SetEV_FROM(firstentry);
4334     glrun->SetEV_TO(lastentry);
4335     glrun->SetNEVENTS(lastentry-firstentry+1);
4336     //
4337     glrun->Set_GL_RUNH0();
4338     //
4339     glrun->SetLAST_TIMESYNC(0);
4340     glrun->SetOBT_TIMESYNC(0);
4341     //
4342     T->GetEntry(firstentry);
4343     ph = eh->GetPscuHeader();
4344     firstObt = ph->GetOrbitalTime();
4345     firstTime = this->GetAbsTime(firstObt);
4346     firstPkt = ph->GetCounter();
4347     //
4348     glrun->SetRUNHEADER_PKT(firstPkt);
4349     //
4350     glrun->SetRUNHEADER_OBT(firstObt);
4351     //
4352     glrun->Set_GL_RUNT(runt,pht);
4353     lastTime = this->GetAbsTime(pht->GetOrbitalTime());
4354     if ( IsDebug() ) printf(" We have the runtrailer \n");
4355     //
4356     this->SetCommonGLRUN(firstTime,lastTime);
4357 mocchiut 1.2 this->SetPhysEndRunVariables();
4358 mocchiut 1.1 //
4359     if ( !IsRunAlreadyInserted() ){
4360     glrun->SetID(this->AssignRunID());
4361     glrun->SetID_RUN_FRAG(0);
4362     glrun->Fill_GL_RUN(conn);
4363     };
4364     };
4365     //
4366     firstevno = lastentry + 1;
4367     //
4368     checkfirst = check;
4369     //
4370     };
4371     //
4372     if ( check == checklast && i != lastev ) i = lastev - 1; // >>>>>>>>>>>>>>>>>>>>>>
4373     //
4374     };
4375     };
4376     };
4377     //
4378     return;
4379     };
4380    
4381    
4382     /**
4383     * Scan calorimeter calibrations packets, fill the GL_CALO_CALIB table
4384     */
4385     Int_t PamelaDBOperations::insertCALO_CALIB(){
4386     //
4387     TSQLResult *result = 0;
4388     TSQLRow *row = 0;
4389     //
4390     stringstream oss;
4391     oss.str("");
4392     //
4393     CalibCalPedEvent *calibCalPed = 0;
4394     TTree *tr = 0;
4395     EventHeader *eh = 0;
4396     PscuHeader *ph = 0;
4397     //
4398     UInt_t nevents = 0;
4399     UInt_t fromtime = 0;
4400     UInt_t totime = 0;
4401     UInt_t obt = 0;
4402     UInt_t pkt = 0;
4403 mocchiut 1.26 Float_t totped = 0.;
4404 mocchiut 1.1 //
4405     tr = (TTree*)file->Get("CalibCalPed");
4406     if ( !tr || tr->IsZombie() ) throw -21;
4407     //
4408     tr->SetBranchAddress("CalibCalPed", &calibCalPed);
4409     tr->SetBranchAddress("Header", &eh);
4410     nevents = tr->GetEntries();
4411     //
4412     if ( !nevents ) return(1);
4413     //
4414     for (UInt_t i=0; i < nevents; i++){
4415     tr->GetEntry(i);
4416     for (UInt_t section = 0; section < 4; section++){
4417     //
4418     if ( calibCalPed->cstwerr[section] ){
4419     valid = 1;
4420     if ( calibCalPed->cperror[section] ) valid = 0;
4421 mocchiut 1.26 //
4422     // check pedestal values for one plane, if all zeros calibration is not valid (calorimeter power problems) [8th data reduction bug, fixed on 25/11/2009 by E.M.]
4423     //
4424     totped = 0.;
4425 mocchiut 1.27 Int_t ns = 0;
4426     if ( section == 2 ) ns = 3;
4427     if ( section == 3 ) ns = 1;
4428     if ( section == 1 ) ns = 2;
4429 mocchiut 1.26 for (UInt_t ss=0; ss<96; ss++){
4430 mocchiut 1.27 totped += fabs(calibCalPed->calped[ns][0][ss]);
4431 mocchiut 1.26 }
4432     if ( totped < 1. ){
4433     if ( IsDebug() ) printf(" Section %i totped %f - No calibration data! Calorimeter power problems? \n",section,totped);
4434     valid = 0;
4435     };
4436     //
4437 mocchiut 1.1 ph = eh->GetPscuHeader();
4438     obt = ph->GetOrbitalTime();
4439     pkt = ph->GetCounter();
4440     fromtime = this->GetAbsTime(ph->GetOrbitalTime());
4441     if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->PKT(pkt) <= upperpkt && this->OBT(obt) >= this->OBT(obtfirst) && this->OBT(obt) <= upperobt ){
4442     //
4443     if ( IsDebug() ) printf(" Calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
4444     //
4445     // check if the calibration has already been inserted
4446     //
4447     oss.str("");
4448     oss << " SELECT ID FROM GL_CALO_CALIB WHERE "
4449     << " SECTION = "<< section << " AND "
4450     << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
4451     << " OBT = "<< obt << " AND "
4452     << " PKT = "<< pkt << ";";
4453     //
4454     if ( IsDebug() ) printf(" Check if the calo calibration has already been inserted: query is \n %s \n",oss.str().c_str());
4455     result = conn->Query(oss.str().c_str());
4456     //
4457     if ( !result ) throw -4;
4458     //
4459     row = result->Next();
4460     //
4461     if ( row ){
4462     //
4463     if ( IsDebug() ) printf(" Calo calibration already inserted in the DB\n");
4464     if ( PEDANTIC ) throw -73;
4465     //
4466     } else {
4467     //
4468     // we have to insert a new calibration, check where to place it
4469     //
4470     oss.str("");
4471     oss << " SELECT ID,TO_TIME FROM GL_CALO_CALIB WHERE "
4472     << " SECTION = "<< section << " AND "
4473     << " FROM_TIME < "<< fromtime << " AND "
4474     << " TO_TIME > "<< fromtime << ";";
4475     //
4476     if ( IsDebug() ) printf(" Check where to place the calo calibration: query is \n %s \n",oss.str().c_str());
4477     result = conn->Query(oss.str().c_str());
4478     //
4479     if ( !result ) throw -4;
4480     //
4481     row = result->Next();
4482     //
4483     if ( !row ){
4484     //
4485     // no calibrations in the db contain our calibration
4486     //
4487     if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
4488     if ( fromtime < 1150871000 ){ //1150866904
4489     if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
4490     fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
4491     };
4492     //
4493     oss.str("");
4494     oss << " SELECT FROM_TIME FROM GL_CALO_CALIB WHERE "
4495     << " SECTION = "<< section << " AND "
4496     << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
4497     //
4498     if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
4499     result = conn->Query(oss.str().c_str());
4500     //
4501     if ( !result ) throw -4;
4502     //
4503     row = result->Next();
4504     if ( !row ){
4505     totime = numeric_limits<UInt_t>::max();
4506     } else {
4507     totime = (UInt_t)atoll(row->GetField(0));
4508     };
4509     //
4510     } else {
4511     //
4512     // determine upper and lower limits and make space for the new calibration
4513     //
4514     totime = (UInt_t)atoll(row->GetField(1));
4515     //
4516     oss.str("");
4517     oss << " UPDATE GL_CALO_CALIB SET "
4518     << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
4519     << " ID = "<< row->GetField(0) << ";";
4520     //
4521     if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
4522     result = conn->Query(oss.str().c_str());
4523     //
4524     if ( !result ) throw -4;
4525     //
4526     };
4527     //
4528     oss.str("");
4529     oss << " INSERT INTO GL_CALO_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,OBT,PKT,BOOT_NUMBER,VALIDATION) "
4530     << " VALUES (NULL,' "
4531     << idroot << "','"
4532     << i << "','"
4533     << fromtime << "','"
4534     << totime << "','"
4535     << section << "','"
4536     << obt << "','"
4537     << pkt << "','"
4538     << this->GetBOOTnumber() << "','"
4539     << valid << "');";
4540     //
4541     if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
4542     //
4543     result = conn->Query(oss.str().c_str());
4544     //
4545     if ( !result ) throw -4;
4546     //
4547     };
4548     //
4549     } else {
4550     //
4551     if ( IsDebug() ) printf(" Calo calibration for section %i at time %u obt %u pkt %u OUTSIDE the considered interval \n",section,fromtime,obt,pkt);
4552     // if ( PEDANTIC ) throw -74;
4553     //
4554     };
4555     //
4556     };
4557     };
4558     };
4559     //
4560     return(0);
4561     };
4562    
4563    
4564     /**
4565     * Scan calorimeter calibrations packets, fill the GL_CALO_CALIB table
4566     */
4567     Int_t PamelaDBOperations::insertCALOPULSE_CALIB(){
4568     //
4569     TSQLResult *result = 0;
4570     TSQLRow *row = 0;
4571     //
4572     stringstream oss;
4573     oss.str("");
4574     //
4575     oss << " DESCRIBE GL_CALOPULSE_CALIB;";
4576     if ( IsDebug() ) printf(" Check if the GL_CALOPULSE_CALIB table exists: query is \n %s \n",oss.str().c_str());
4577     result = conn->Query(oss.str().c_str());
4578     //
4579     if ( conn->GetErrorCode() ){
4580     if ( IsDebug() ) printf(" The GL_CALOPULSE_CALIB table does not exists! \n");
4581     throw -30;
4582     };
4583     //
4584     // CaloPulse1
4585     //
4586     CalibCalPulse1Event *cp1 = 0;
4587     TTree *tr = 0;
4588     EventHeader *eh = 0;
4589     PscuHeader *ph = 0;
4590     //
4591     UInt_t nevents = 0;
4592     UInt_t fromtime = 0;
4593     UInt_t totime = 0;
4594     UInt_t obt = 0;
4595     UInt_t pkt = 0;
4596     //
4597     tr = (TTree*)file->Get("CalibCalPulse1");
4598     if ( !tr || tr->IsZombie() ) throw -31;
4599     //
4600     tr->SetBranchAddress("CalibCalPulse1", &cp1);
4601     tr->SetBranchAddress("Header", &eh);
4602     nevents = tr->GetEntries();
4603     //
4604 mocchiut 1.26 Float_t totpul = 0.;
4605     //
4606 mocchiut 1.1 if ( nevents > 0 ){
4607     //
4608     for (UInt_t i=0; i < nevents; i++){
4609     tr->GetEntry(i);
4610     for (UInt_t section = 0; section < 4; section++){
4611     //
4612     if ( cp1->pstwerr[section] && cp1->unpackError == 0 ){
4613     valid = 1;
4614     if ( cp1->pperror[section] ) valid = 0;
4615 mocchiut 1.26 //
4616     // check pulse values for one plane, if all zeros calibration is not valid (calorimeter power problems) [8th data reduction bug, fixed on 25/11/2009 by E.M.]
4617     //
4618     totpul = 0.;
4619 mocchiut 1.27 Int_t ns = 0;
4620     if ( section == 2 ) ns = 3;
4621     if ( section == 3 ) ns = 1;
4622     if ( section == 1 ) ns = 2;
4623 mocchiut 1.26 for (UInt_t ss=0; ss<96; ss++){
4624 mocchiut 1.27 totpul += cp1->calpuls[ns][0][ss];
4625 mocchiut 1.26 }
4626     if ( totpul >= 3145632. ){
4627     if ( IsDebug() ) printf(" PULSE1 Section %i totpul %f - No calibration data! Calorimeter power problems? \n",section,totpul);
4628     valid = 0;
4629     };
4630     //
4631 mocchiut 1.1 ph = eh->GetPscuHeader();
4632     obt = ph->GetOrbitalTime();
4633     pkt = ph->GetCounter();
4634     fromtime = this->GetAbsTime(ph->GetOrbitalTime());
4635     if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->PKT(pkt) <= upperpkt && this->OBT(obt) >= this->OBT(obtfirst) && this->OBT(obt) <= upperobt ){
4636     // if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
4637     //
4638     if ( IsDebug() ) printf(" Calo pulse1 calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
4639     //
4640     // check if the calibration has already been inserted
4641     //
4642     oss.str("");
4643     oss << " SELECT ID FROM GL_CALOPULSE_CALIB WHERE "
4644     << " SECTION = "<< section << " AND "
4645     << " PULSE_AMPLITUDE = 0 AND "
4646     << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
4647     << " OBT = "<< obt << " AND "
4648     << " PKT = "<< pkt << ";";
4649     //
4650     if ( IsDebug() ) printf(" Check if the calo pulse1 calibration has already been inserted: query is \n %s \n",oss.str().c_str());
4651     result = conn->Query(oss.str().c_str());
4652     //
4653     if ( !result ) throw -4;
4654     //
4655     row = result->Next();
4656     //
4657     if ( row ){
4658     //
4659     if ( IsDebug() ) printf(" Calo pulse1 calibration already inserted in the DB\n");
4660     if ( PEDANTIC ) throw -75;
4661     //
4662     } else {
4663     //
4664     // we have to insert a new calibration, check where to place it
4665     //
4666     oss.str("");
4667     oss << " SELECT ID,TO_TIME FROM GL_CALOPULSE_CALIB WHERE "
4668     << " SECTION = "<< section << " AND "
4669     << " PULSE_AMPLITUDE = 0 AND "
4670     << " SECTION = "<< section << " AND "
4671     << " FROM_TIME < "<< fromtime << " AND "
4672     << " TO_TIME > "<< fromtime << ";";
4673     //
4674     if ( IsDebug() ) printf(" Check where to place the pulse1 calo calibration: query is \n %s \n",oss.str().c_str());
4675     result = conn->Query(oss.str().c_str());
4676     //
4677     if ( !result ) throw -4;
4678     //
4679     row = result->Next();
4680     //
4681     if ( !row ){
4682     //
4683     // no calibrations in the db contain our calibration
4684     //
4685     if ( IsDebug() ) printf(" Pulse1 calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
4686     if ( fromtime < 1150871000 ){ //1150866904
4687     if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
4688     fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
4689     };
4690     //
4691     oss.str("");
4692     oss << " SELECT FROM_TIME FROM GL_CALOPULSE_CALIB WHERE "
4693     << " PULSE_AMPLITUDE = 0 AND "
4694     << " SECTION = "<< section << " AND "
4695     << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
4696     //
4697     if ( IsDebug() ) printf(" Check the upper limit for pulse1 calibration: query is \n %s \n",oss.str().c_str());
4698     result = conn->Query(oss.str().c_str());
4699     //
4700     if ( !result ) throw -4;
4701     //
4702     row = result->Next();
4703     if ( !row ){
4704     totime = numeric_limits<UInt_t>::max();
4705     } else {
4706     totime = (UInt_t)atoll(row->GetField(0));
4707     };
4708     //
4709     } else {
4710     //
4711     // determine upper and lower limits and make space for the new calibration
4712     //
4713     totime = (UInt_t)atoll(row->GetField(1));
4714     //
4715     oss.str("");
4716     oss << " UPDATE GL_CALOPULSE_CALIB SET "
4717     << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
4718     << " ID = "<< row->GetField(0) << ";";
4719     //
4720     if ( IsDebug() ) printf(" Make space for the new pulse1 calibration: query is \n %s \n",oss.str().c_str());
4721     result = conn->Query(oss.str().c_str());
4722     //
4723     if ( !result ) throw -4;
4724     //
4725     };
4726     //
4727     oss.str("");
4728     oss << " INSERT INTO GL_CALOPULSE_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,PULSED_STRIP,PULSE_AMPLITUDE,OBT,PKT,BOOT_NUMBER,VALIDATION) "
4729     << " VALUES (NULL,' "
4730     << idroot << "','"
4731     << i << "','"
4732     << fromtime << "','"
4733     << totime << "','"
4734     << section << "',NULL,'0','"
4735     << obt << "','"
4736     << pkt << "','"
4737     << this->GetBOOTnumber() << "','"
4738     << valid << "');";
4739     //
4740     if ( IsDebug() ) printf(" Insert the new pulse1 calibration: query is \n %s \n",oss.str().c_str());
4741     //
4742     result = conn->Query(oss.str().c_str());
4743     //
4744     if ( !result ) throw -4;
4745     //
4746     };
4747     //
4748     } else {
4749     //
4750     if ( IsDebug() ) printf(" Pulse1 calo calibration for section %i at time %u obt %u pkt %u OUTSIDE the considered time interval \n",section,fromtime,obt,pkt);
4751     // if ( PEDANTIC ) throw -76;
4752     //
4753     };
4754     //
4755     };
4756     };
4757     };
4758     };
4759     //
4760     // CaloPulse2
4761     //
4762     tr->Reset();
4763     CalibCalPulse2Event *cp2 = 0;
4764     tr = 0;
4765     //
4766     nevents = 0;
4767     fromtime = 0;
4768     totime = 0;
4769     obt = 0;
4770     pkt = 0;
4771     //
4772     tr = (TTree*)file->Get("CalibCalPulse2");
4773     if ( !tr || tr->IsZombie() ) throw -32;
4774     //
4775     tr->SetBranchAddress("CalibCalPulse2", &cp2);
4776     tr->SetBranchAddress("Header", &eh);
4777     nevents = tr->GetEntries();
4778     //
4779     if ( nevents > 0 ){
4780     //
4781     for (UInt_t i=0; i < nevents; i++){
4782     tr->GetEntry(i);
4783     for (UInt_t section = 0; section < 4; section++){
4784     //
4785     if ( cp2->pstwerr[section] && cp2->unpackError == 0 ){
4786     valid = 1;
4787     if ( cp2->pperror[section] ) valid = 0;
4788 mocchiut 1.26 //
4789     // check pulse values for one plane, if all zeros calibration is not valid (calorimeter power problems) [8th data reduction bug, fixed on 25/11/2009 by E.M.]
4790     //
4791     totpul = 0.;
4792 mocchiut 1.27 Int_t ns = 0;
4793     if ( section == 2 ) ns = 3;
4794     if ( section == 3 ) ns = 1;
4795     if ( section == 1 ) ns = 2;
4796 mocchiut 1.26 for (UInt_t ss=0; ss<96; ss++){
4797 mocchiut 1.27 totpul += cp2->calpuls[ns][0][ss];
4798 mocchiut 1.26 }
4799     if ( totpul >= 3145632. ){
4800     if ( IsDebug() ) printf(" PULSE2 Section %i totpul %f - No calibration data! Calorimeter power problems? \n",section,totpul);
4801     valid = 0;
4802     };
4803     //
4804 mocchiut 1.1 ph = eh->GetPscuHeader();
4805     obt = ph->GetOrbitalTime();
4806     pkt = ph->GetCounter();
4807     fromtime = this->GetAbsTime(ph->GetOrbitalTime());
4808     if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->PKT(pkt) <= upperpkt && this->OBT(obt) >= this->OBT(obtfirst) && this->OBT(obt) <= upperobt ){
4809     // if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
4810     //
4811     if ( IsDebug() ) printf(" Calo pulse2 calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
4812     //
4813     // check if the calibration has already been inserted
4814     //
4815     oss.str("");
4816     oss << " SELECT ID FROM GL_CALOPULSE_CALIB WHERE "
4817     << " SECTION = "<< section << " AND "
4818     << " PULSE_AMPLITUDE != 0 AND "
4819     << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
4820     << " OBT = "<< obt << " AND "
4821     << " PKT = "<< pkt << ";";
4822     //
4823     if ( IsDebug() ) printf(" Check if the calo pulse2 calibration has already been inserted: query is \n %s \n",oss.str().c_str());
4824     result = conn->Query(oss.str().c_str());
4825     //
4826     if ( !result ) throw -4;
4827     //
4828     row = result->Next();
4829     //
4830     if ( row ){
4831     //
4832     if ( IsDebug() ) printf(" Calo pulse2 calibration already inserted in the DB\n");
4833     if ( PEDANTIC ) throw -77;
4834     //
4835     } else {
4836     //
4837     // we have to insert a new calibration
4838     //
4839     //
4840     // Determine the amplitude of the pulse
4841     //
4842     UInt_t pampli = 1;
4843     UInt_t pstrip = 0;
4844     UInt_t se = 0;
4845     if ( section == 1 ) se = 2;
4846     if ( section == 2 ) se = 3;
4847     if ( section == 3 ) se = 1;
4848     for (Int_t ii=0;ii<16;ii++){
4849     if ( cp2->calpuls[se][0][ii] > 10000. ){
4850     pampli = 2;
4851     pstrip = ii;
4852     };
4853     };
4854     if ( pampli == 1 ){
4855     Bool_t found = false;
4856     Float_t delta=0.;
4857     UInt_t cstr = 0;
4858     while ( !found && cstr < 16 ){
4859     for (Int_t ii=0;ii<16;ii++){
4860     delta = cp2->calpuls[se][0][ii] - cp2->calpuls[se][0][cstr];
4861     if ( IsDebug() ) printf(" cstr is %u ii is %i delta is %f \n",cstr,ii,delta);
4862     if ( delta > 500. ){
4863     pampli = 1;
4864     pstrip = ii;
4865     found = true;
4866     if ( IsDebug() ) printf(" FOUND cstr is %u ii is %i delta is %f \n",cstr,ii,delta);
4867     };
4868     };
4869     cstr++;
4870     };
4871     if ( !found ) pstrip = 100;
4872     };
4873     if ( IsDebug() ) printf(" The amplitude of the pulser is %u (where 1 = low pulse, 2 = high pulse), pulsed strip is %u \n",pampli,pstrip);
4874     //
4875     // we have to insert a new calibration, check where to place it
4876     //
4877     oss.str("");
4878     oss << " SELECT ID,TO_TIME FROM GL_CALOPULSE_CALIB WHERE "
4879     << " SECTION = "<< section << " AND "
4880     << " PULSE_AMPLITUDE = " << pampli << " AND "
4881     << " SECTION = "<< section << " AND "
4882     << " FROM_TIME < "<< fromtime << " AND "
4883     << " TO_TIME > "<< fromtime << ";";
4884     //
4885     if ( IsDebug() ) printf(" Check where to place the pulse2 calo calibration: query is \n %s \n",oss.str().c_str());
4886     result = conn->Query(oss.str().c_str());
4887     //
4888     if ( !result ) throw -4;
4889     //
4890     row = result->Next();
4891     //
4892     if ( !row ){
4893     //
4894     // no calibrations in the db contain our calibration
4895     //
4896     if ( IsDebug() ) printf(" Pulse2 calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
4897     if ( fromtime < 1150871000 ){ //1150866904
4898     if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
4899     fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
4900     };
4901     //
4902     oss.str("");
4903     oss << " SELECT FROM_TIME FROM GL_CALOPULSE_CALIB WHERE "
4904     << " PULSE_AMPLITUDE = " << pampli << " AND "
4905     << " SECTION = "<< section << " AND "
4906     << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
4907     //
4908     if ( IsDebug() ) printf(" Check the upper limit for pulse2 calibration: query is \n %s \n",oss.str().c_str());
4909     result = conn->Query(oss.str().c_str());
4910     //
4911     if ( !result ) throw -4;
4912     //
4913     row = result->Next();
4914     if ( !row ){
4915     totime = numeric_limits<UInt_t>::max();
4916     } else {
4917     totime = (UInt_t)atoll(row->GetField(0));
4918     };
4919     //
4920     } else {
4921     //
4922     // determine upper and lower limits and make space for the new calibration
4923     //
4924     totime = (UInt_t)atoll(row->GetField(1));
4925     //
4926     oss.str("");
4927     oss << " UPDATE GL_CALOPULSE_CALIB SET "
4928     << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
4929     << " ID = "<< row->GetField(0) << ";";
4930     //
4931     if ( IsDebug() ) printf(" Make space for the new pulse2 calibration: query is \n %s \n",oss.str().c_str());
4932     result = conn->Query(oss.str().c_str());
4933     //
4934     if ( !result ) throw -4;
4935     //
4936     };
4937     //
4938     // Fill the DB
4939     //
4940     oss.str("");
4941     // oss << " INSERT INTO GL_CALOPULSE_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,OBT,PKT,BOOT_NUMBER,VALIDATION) "
4942     oss << " INSERT INTO GL_CALOPULSE_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,PULSED_STRIP,PULSE_AMPLITUDE,OBT,PKT,BOOT_NUMBER,VALIDATION) "
4943     << " VALUES (NULL,' "
4944     << idroot << "','"
4945     << i << "','"
4946     << fromtime << "','"
4947     << totime << "','"
4948     << section << "','"
4949     << pstrip << "','"
4950     << pampli << "','"
4951     << obt << "','"
4952     << pkt << "','"
4953     << this->GetBOOTnumber() << "','"
4954     << valid << "');";
4955     //
4956     if ( IsDebug() ) printf(" Insert the new pulse2 calibration: query is \n %s \n",oss.str().c_str());
4957     //
4958     result = conn->Query(oss.str().c_str());
4959     //
4960     if ( !result ) throw -4;
4961     //
4962     };
4963     //
4964     } else {
4965     //
4966     if ( IsDebug() ) printf(" Pulse2 calo calibration for section %i at time %u obt %u pkt %u OUTSIDE the considered time interval \n",section,fromtime,obt,pkt);
4967     // if ( PEDANTIC ) throw -78;
4968     //
4969     };
4970     //
4971     };
4972     };
4973     };
4974     };
4975     //
4976     return(0);
4977     };
4978    
4979     /**
4980     * Fill the GL_TRK_CALIB table
4981     */
4982     void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){
4983 pam-fi 1.4
4984     GL_TRK_CALIB *glcal = new GL_TRK_CALIB();
4985     //
4986     glcal->ID = 0;
4987     glcal->ID_ROOT_L0 = GetID_ROOT();
4988     glcal->EV_ROOT_CALIBTRK1 = t1;
4989     glcal->EV_ROOT_CALIBTRK2 = t2;
4990     glcal->FROM_TIME = fromtime;
4991     glcal->TO_TIME = 0;
4992     glcal->OBT1 = obt1;
4993     glcal->OBT2 = obt2;
4994     glcal->PKT1 = pkt1;
4995     glcal->PKT2 = pkt2;
4996     glcal->BOOT_NUMBER = GetBOOTnumber();
4997     glcal->VALIDATION = valid;
4998     //
4999     HandleTRK_CALIB(glcal);
5000     //
5001     delete glcal;
5002     }
5003     /**
5004     * Fill the GL_TRK_CALIB table
5005     */
5006     void PamelaDBOperations::HandleTRK_CALIB(GL_TRK_CALIB *glcal){
5007    
5008     Bool_t pk1 = (glcal->OBT1>0&&glcal->PKT1>0);
5009     Bool_t pk2 = (glcal->OBT2>0&&glcal->PKT2>0);
5010     UInt_t boot_number = glcal->BOOT_NUMBER;
5011     UInt_t obt1 = glcal->OBT1;
5012     UInt_t obt2 = glcal->OBT2;
5013     UInt_t pkt1 = glcal->PKT1;
5014     UInt_t pkt2 = glcal->PKT2;
5015     UInt_t fromtime = glcal->FROM_TIME;
5016     UInt_t totime = 0;
5017     UInt_t idroot = glcal->ID_ROOT_L0;
5018     UInt_t t1 = glcal->EV_ROOT_CALIBTRK1;
5019     UInt_t t2 = glcal->EV_ROOT_CALIBTRK2;
5020     UInt_t valid = glcal->VALIDATION;
5021     //
5022     TSQLResult *result = 0;
5023     TSQLRow *row = 0;
5024     //
5025     stringstream oss;
5026     oss.str("");
5027 mocchiut 1.1 //
5028     //
5029 pam-fi 1.4 if ( !pk1 && !pk2 ){
5030     if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n");
5031     return;
5032     };
5033 mocchiut 1.1 //
5034 pam-fi 1.4 // check if the calibration has already been inserted
5035 mocchiut 1.1 //
5036     oss.str("");
5037 pam-fi 1.4 oss << " SELECT ID FROM GL_TRK_CALIB WHERE "
5038     << " BOOT_NUMBER = "<< boot_number; //
5039     oss << " AND FROM_TIME="<<fromtime; /// NEWNEWNEW -- VA BENE ?!?!?!?!
5040     oss << " AND ( ( ";
5041     if ( pk1 ){
5042     oss << " OBT1 = "<< obt1 << " AND "
5043     << " PKT1 = "<< pkt1
5044     << " ) OR ( ";
5045     } else {
5046     oss << " PKT1 = "<< pkt2-1
5047     << " ) OR ( ";
5048     };
5049     if ( pk2 ){
5050     oss << " OBT2 = "<< obt2 << " AND "
5051     << " PKT2 = "<< pkt2;
5052     } else {
5053     oss << " PKT2 = "<< pkt1+1;
5054     };
5055     oss << " ) );";
5056 mocchiut 1.1 //
5057 pam-fi 1.4 if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str());
5058 mocchiut 1.1 result = conn->Query(oss.str().c_str());
5059     //
5060     if ( !result ) throw -4;
5061     //
5062     row = result->Next();
5063     //
5064 pam-fi 1.4 if ( row ){
5065     //
5066     if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");
5067     if ( PEDANTIC ) throw -80;
5068     //
5069 mocchiut 1.1 } else {
5070 pam-fi 1.4 //
5071     // we have to insert a new calibration, check where to place it
5072     //
5073     oss.str("");
5074     oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "
5075     << " FROM_TIME < "<< fromtime << " AND "
5076     << " TO_TIME > "<< fromtime << ";";
5077     //
5078     if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());
5079     result = conn->Query(oss.str().c_str());
5080     //
5081     if ( !result ) throw -4;
5082     //
5083     row = result->Next();
5084     //
5085     if ( !row ){
5086     //
5087     // no calibrations in the db contain our calibration
5088     //
5089     if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
5090     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
5091     //
5092     oss.str("");
5093     oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
5094     << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
5095     //
5096     if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
5097     result = conn->Query(oss.str().c_str());
5098     //
5099     if ( !result ) throw -4;
5100     //
5101     row = result->Next();
5102     if ( !row ){
5103     totime = numeric_limits<UInt_t>::max();
5104     } else {
5105     totime = (UInt_t)atoll(row->GetField(0));
5106     };
5107     //
5108     } else {
5109     //
5110     // determine upper and lower limits and make space for the new calibration
5111     //
5112     totime = (UInt_t)atoll(row->GetField(1));
5113     //
5114     oss.str("");
5115     oss << " UPDATE GL_TRK_CALIB SET "
5116     << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
5117     << " ID = "<< row->GetField(0) << ";";
5118     //
5119     if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str());
5120     result = conn->Query(oss.str().c_str());
5121     //
5122     if ( !result ) throw -4;
5123     //
5124     };
5125     //
5126     oss.str("");
5127     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) "
5128     << " VALUES (NULL,' "
5129     << idroot << "',";
5130     //
5131     if ( !pk1 ){
5132     oss << "NULL,";
5133     } else {
5134     oss << "'"
5135     << t1 << "',";
5136     };
5137     //
5138     if ( !pk2 ){
5139     oss << "NULL,'";
5140     } else {
5141     oss << "'"
5142     << t2 << "','";
5143     };
5144     //
5145     oss << fromtime << "','"
5146     << totime << "','"
5147     << obt1 << "','"
5148     << pkt1 << "','"
5149     << obt2 << "','"
5150     << pkt2 << "','"
5151     << boot_number << "','"
5152     << valid << "');";
5153     //
5154     if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());
5155     //
5156     result = conn->Query(oss.str().c_str());
5157     //
5158     if ( !result ) throw -4;
5159     //
5160 mocchiut 1.1 };
5161 pam-fi 1.4
5162 mocchiut 1.1 oss.str("");
5163 pam-fi 1.4 oss << " SELECT ID FROM GL_TRK_CALIB ORDER BY ID DESC LIMIT 1 ;";
5164     if ( IsDebug() ) cout << oss.str().c_str() << endl;
5165 mocchiut 1.1 result = conn->Query(oss.str().c_str());
5166 pam-fi 1.4 if ( !result ) throw -4;;
5167     row = result->Next();
5168     if(row)glcal->ID = (UInt_t)atoll(row->GetField(0));
5169 mocchiut 1.21 //
5170     delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5171 mocchiut 1.1 //
5172     };
5173    
5174     /**
5175     * Scan tracker calibrations packets, fill the GL_TRK_CALIB table
5176     */
5177     Int_t PamelaDBOperations::insertTRK_CALIB(){
5178     //
5179 pam-fi 1.4 CalibTrk1Event *caltrk1 = 0;
5180     CalibTrk2Event *caltrk2 = 0;
5181     TTree *tr1 = 0;
5182     TTree *tr2 = 0;
5183     EventHeader *eh1 = 0;
5184     PscuHeader *ph1 = 0;
5185     EventHeader *eh2 = 0;
5186     PscuHeader *ph2 = 0;
5187     //
5188     PacketType *pctp=0;
5189     EventCounter *codt2=0;
5190     //
5191     Int_t nevents1 = 0;
5192     Int_t nevents2 = 0;
5193 mocchiut 1.1 //
5194 pam-fi 1.4 fromtime = 0;
5195 mocchiut 1.1 //
5196 pam-fi 1.4 obt1 = 0;
5197     pkt1 = 0;
5198     obt2 = 0;
5199     pkt2 = 0;
5200 mocchiut 1.1 //
5201 pam-fi 1.4 tr1 = (TTree*)file->Get("CalibTrk1");
5202     if ( !tr1 || tr1->IsZombie() ) throw -22;
5203     tr2 = (TTree*)file->Get("CalibTrk2");
5204     if ( !tr2 || tr2->IsZombie() ) throw -23;
5205     //
5206     tr1->SetBranchAddress("CalibTrk1", &caltrk1);
5207     tr1->SetBranchAddress("Header", &eh1);
5208     nevents1 = tr1->GetEntries();
5209     tr2->SetBranchAddress("CalibTrk2", &caltrk2);
5210     tr2->SetBranchAddress("Header", &eh2);
5211     nevents2 = tr2->GetEntries();
5212     //
5213     if ( !nevents1 && !nevents2 ) return(1);
5214     //
5215     t2 = -1;
5216     Int_t pret2 = 0;
5217     Int_t t2t1cal = 0;
5218     //
5219     bool MISSING_pkt1 = true;
5220     bool MISSING_pkt2 = true;
5221     int ncalib = 0;
5222     bool try_to_recover = false;
5223 mocchiut 1.1 //
5224 pam-fi 1.4 for (t1=0; t1 < nevents1; t1++){//loop over packet1
5225     //
5226     pret2 = t2;
5227     tr1->GetEntry(t1);
5228 mocchiut 1.1 //
5229 pam-fi 1.4 ph1 = eh1->GetPscuHeader();
5230     obt1 = ph1->GetOrbitalTime();
5231     pkt1 = ph1->GetCounter();
5232     fromtime = GetAbsTime(ph1->GetOrbitalTime());
5233 mocchiut 1.1 //
5234 pam-fi 1.4 // chek if the packet number and obt are consistent with the other packets ???
5235 mocchiut 1.1 //
5236 pam-fi 1.4 if ( PKT(pkt1) >= PKT(pktfirst) && PKT(pkt1) <= upperpkt && OBT(obt1) >= OBT(obtfirst) && OBT(obt1) <= upperobt ){
5237     // if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){
5238     //
5239     if ( IsDebug() ) printf("\n Trk calibration1 %u at time %u obt %u pkt %u \n",t1,fromtime,obt1,pkt1);
5240     //
5241     valid = ValidateTrkCalib( caltrk1, eh1 );
5242     if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
5243     //
5244     // Do we have the second calibration packet?
5245     //
5246     if ( IsDebug() ) cout << " Loop over calibration2 to search associated calibration: "<<endl;
5247     while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1
5248     //
5249     t2++;
5250     //
5251     pret2 = t2 - 1; // EMILIANO
5252     //
5253     if ( t2 < nevents2 ){
5254     tr2->GetEntry(t2);
5255     codt2 = eh2->GetCounter();
5256     t2t1cal = codt2->Get(pctp->CalibTrk1);
5257     //
5258     ph2 = eh2->GetPscuHeader();
5259     obt2 = ph2->GetOrbitalTime();
5260     pkt2 = ph2->GetCounter();
5261     //
5262     if ( IsDebug() ) printf(" >> trk calibration2 at obt %u pkt %u t2 is %u , t2t1cal is %u \n",obt2,pkt2,t2,t2t1cal);
5263     // if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2
5264     //
5265     } else {
5266     //
5267     // running out of vector without finding the corresponding calibration, sig
5268     //
5269     if ( IsDebug() ) printf(" t2 >= nevents2 \n");
5270     pret2 = t2;
5271     obt2 = 0;
5272     // pkt2 = pkt1+2;
5273     pkt2 = 0;
5274     t2t1cal = t1+1;
5275     };
5276     // if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) && (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){
5277    
5278     // EMILIANO
5279     // if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) || (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){
5280     // // if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
5281     // if ( IsDebug() ) printf(" running out of vector without finding the corresponding calibration, sig \n");
5282     // //
5283     // // running out of vector without finding the corresponding calibration, sig
5284     // //
5285     // pret2 = t2;
5286     // obt2 = 0;
5287     // // pkt2 = pkt1+2;
5288     // pkt2 = 0;
5289     // t2t1cal = t1+1;
5290     // };
5291    
5292    
5293     //
5294     };
5295     //
5296     if ( IsDebug() ) printf(" Check if trk calibration2 is the right one \n");
5297     //
5298     // EMILIANO
5299     if ( ( PKT(pkt2) < PKT(pktfirst) || PKT(pkt2) > upperpkt) || (OBT(obt2) < OBT(obtfirst) || OBT(obt2) > upperobt) ){
5300     // if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
5301     if ( IsDebug() ) printf(" *WARNING* The calibration found is outside the interval, sig \n");
5302     //
5303     // running out of vector without finding the corresponding calibration, sig
5304     //
5305     pret2 = t2;
5306     obt2 = 0;
5307     pkt2 = 0;
5308     };
5309     if ( PKT(pkt2) == PKT(pkt1)+1 ){
5310     if ( IsDebug() ) cout << " ...OK"<<endl;
5311     // =======================
5312     // The calibration is good
5313     // =======================
5314     //
5315     // if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2);
5316     // if ( IsDebug() ) printf(" Trk calibration2 at obt %u pkt %u t2 is %u is good \n",obt2,pkt2,t2);
5317     // if ( IsDebug() ) printf("\n Trk calibration2 at time %u obt %u pkt %u \n",fromtime,obt2,pkt2);
5318     if ( IsDebug() ) printf(" Trk calibration2 %u at time %u obt %u pkt %u \n",t2,fromtime,obt2,pkt2);
5319     //
5320     UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
5321     if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
5322     // valid = valid & valid2;
5323     valid = valid & valid2; //QUESTO VA CAMBIATO
5324     //
5325     // Handle good calib
5326     //
5327     MISSING_pkt1 = false;
5328     MISSING_pkt2 = false;
5329     // this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5330     //
5331     // Check for missing calibtrk1
5332     //
5333     if ( t2 != pret2+1 ){
5334     //
5335     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);
5336     //
5337     while ( t2 > pret2+1 ){
5338     //
5339     // handle missing calib1
5340     //
5341     pret2++;
5342     //
5343     obt1 = 0;
5344     pkt1 = 0;
5345     //
5346     tr2->GetEntry(pret2);
5347     ph2 = eh2->GetPscuHeader();
5348     obt2 = ph2->GetOrbitalTime();
5349     pkt2 = ph2->GetCounter();
5350     //
5351     fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
5352     //
5353     valid = 0;
5354     MISSING_pkt1 = true;
5355     MISSING_pkt2 = false;
5356     // this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5357     //
5358     };
5359     //
5360     };
5361     //
5362     } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){
5363     //
5364     // Check for missing calibtrk2
5365     //
5366     if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
5367     t2 = pret2;
5368     //
5369     // handle missing calib2
5370     //
5371     obt2 = 0;
5372     pkt2 = 0;
5373     valid = 0;
5374     MISSING_pkt1 = false;
5375     MISSING_pkt2 = true;
5376     // this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5377     //
5378     };
5379     //
5380 mocchiut 1.7
5381     if( !(MISSING_pkt1&MISSING_pkt2) ){
5382     this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5383     ncalib++;
5384     if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;
5385     }
5386    
5387    
5388 mocchiut 1.1 } else {
5389 mocchiut 1.7 //
5390 pam-fi 1.4 if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u OUTSIDE the considered time interval \n",fromtime,obt1,pkt1);
5391     // if ( PEDANTIC ) throw -79;
5392     //
5393 mocchiut 1.1 };
5394 pam-fi 1.4 //
5395    
5396     }; //end loop on pkt1
5397 mocchiut 1.1
5398    
5399    
5400 pam-fi 1.4 //
5401     // we have one more calib pkt2 !
5402     //
5403     t2++;
5404     while ( t2 < nevents2 ){
5405 mocchiut 1.1 //
5406 pam-fi 1.4 // handle missing calib1
5407 mocchiut 1.1 //
5408 pam-fi 1.4 if ( IsDebug() ) printf(" t2 is %u nevents2 is %u \n",t2,nevents2);
5409     obt1 = 0;
5410     pkt1 = 0;
5411 mocchiut 1.1 //
5412 pam-fi 1.4 tr2->GetEntry(t2);
5413     ph2 = eh2->GetPscuHeader();
5414     obt2 = ph2->GetOrbitalTime();
5415     pkt2 = ph2->GetCounter();
5416 mocchiut 1.1 //
5417 pam-fi 1.4 fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
5418     valid = 0;
5419     // if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){
5420     // EMILIANO
5421     if ( this->PKT(pkt2) >= this->PKT(pktfirst) && this->PKT(pkt2 <= upperpkt) && this->OBT(obt2) >= this->OBT(obtfirst) && this->OBT(obt2) <= upperobt ){
5422     // if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){
5423 mocchiut 1.1 //
5424 pam-fi 1.4 if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
5425 mocchiut 1.1 //
5426 pam-fi 1.4 MISSING_pkt1 = true;
5427     MISSING_pkt2 = false;
5428     this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5429     ncalib++;
5430     if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;
5431 mocchiut 1.1 //
5432     };
5433     //
5434 pam-fi 1.4 t2++;
5435 mocchiut 1.1 //
5436 pam-fi 1.4 };
5437    
5438     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5439     // -----------------------------------------------------------------
5440     // in case of corruption, check if the calibration can be recovered
5441     // from another chewbacca file
5442     // -----------------------------------------------------------------
5443     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5444    
5445     // cout <<" TRY TO RECOVER ?? "<<try_to_recover<<endl;
5446    
5447     if(chewbacca&&try_to_recover){
5448    
5449    
5450     if ( IsDebug() ) cout << endl << ">>>> TRY TO RECOVER TRACKER CALIBRATIONS <<<<"<<endl;
5451    
5452     TSQLResult *result = 0;
5453     TSQLRow *row = 0;
5454 mocchiut 1.1 //
5455 pam-fi 1.4 stringstream oss;
5456     oss.str("");
5457 mocchiut 1.1 //
5458 pam-fi 1.4
5459     ////////////////////////////////////////////////////////////////////////
5460     // retrieve the name of the current file:
5461     ////////////////////////////////////////////////////////////////////////
5462     oss.str("");
5463     oss << "SELECT NAME FROM GL_ROOT where ID=" << GetID_ROOT() <<";";
5464     if ( IsDebug() ) cout << oss.str().c_str() << endl;
5465    
5466     result = conn->Query(oss.str().c_str());
5467     if ( !result ) throw -4;;
5468     row = result->Next();
5469     TString thisfilename = (TString)row->GetField(0);
5470     if ( IsDebug() ) cout << "Current file ==> "<<thisfilename<<endl;
5471    
5472     ////////////////////////////////////////////////////////////////////////
5473     // read all the calibrations inserted
5474     ////////////////////////////////////////////////////////////////////////
5475     oss.str("");
5476     oss << " SELECT ";
5477     oss << " ID,FROM_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,TO_TIME";
5478     oss << " FROM GL_TRK_CALIB ";
5479     oss << " ORDER BY ID DESC LIMIT "<<ncalib<<"; ";
5480     if ( IsDebug() ) cout << oss.str().c_str() << endl;
5481    
5482     result = conn->Query(oss.str().c_str());
5483     if ( !result ) throw -4;;
5484     if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5485    
5486     // -----------------------------------
5487     // loop over calibrations ...
5488     // -----------------------------------
5489     UInt_t nn=0;
5490     do {
5491     row = result->Next();
5492     if(!row)break;
5493    
5494     UInt_t id = (UInt_t)atoll(row->GetField(0));
5495     UInt_t fromtime = (UInt_t)atoll(row->GetField(1));
5496     UInt_t obt1 = (UInt_t)atoll(row->GetField(2));
5497     UInt_t pkt1 = (UInt_t)atoll(row->GetField(3));
5498     UInt_t obt2 = (UInt_t)atoll(row->GetField(4));
5499     UInt_t pkt2 = (UInt_t)atoll(row->GetField(5));
5500     UInt_t boot = (UInt_t)atoll(row->GetField(6));
5501     UInt_t valid = (UInt_t)atoll(row->GetField(7));
5502     bool MISSING_pkt1 = (row->GetFieldLength(8)==0);
5503     bool MISSING_pkt2 = (row->GetFieldLength(9)==0);
5504     UInt_t totime = (UInt_t)atoll(row->GetField(10));
5505    
5506     // -------------------------------------
5507     // ...check if the entry is corrupted...
5508     // -------------------------------------
5509     cout <<"*** "<< MISSING_pkt1 << MISSING_pkt2 << valid <<endl;
5510     bool CORRUPTED = (MISSING_pkt1||MISSING_pkt2||!valid);
5511    
5512     if ( IsDebug() ) cout << "("<<nn<<") ID = "<<id<<" from GL_TRK_CALIB ==> corrupted ? "<<CORRUPTED<<endl;
5513    
5514     // if( !CORRUPTED )continue; // nothing to do
5515    
5516     /////////////////////////////////////////////////////////
5517     // if it is corrupted, ...look for ather chewbacca files
5518     // containing the same calibrations ...
5519     /////////////////////////////////////////////////////////
5520    
5521     bool this_MISSING_pkt1 = false;
5522     bool this_MISSING_pkt2 = false;
5523     int this_t1=0;
5524     int this_t2=0;;
5525     UInt_t this_valid = 0;
5526    
5527     TString path = "";
5528     TString name = "";
5529     TString raw = "";
5530 mocchiut 1.33 // UInt_t obt0 = 0;
5531     // UInt_t timesync = 0;
5532     // UInt_t boot_number = 0;
5533 pam-fi 1.4 bool FOUND = false;
5534    
5535     if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;
5536    
5537     // for(int itable=0; itable<2; itable++){
5538     for(int itable=0; itable<1; itable++){
5539    
5540     // ------------------------------------------------------
5541     // loop over both ROOT_TABLE and ROOT_TABLE_BAD
5542     // ------------------------------------------------------
5543    
5544     TString table = "ROOT_TABLE";
5545     if(itable==1)table = "ROOT_TABLE_BAD";
5546    
5547     oss.str("");
5548     oss << " SELECT ";
5549     oss << " FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,BOOT_NUMBER,INPUT_NAME ";
5550     oss << " FROM "<<table;
5551     oss << " WHERE 1 " << endl;
5552     oss << " AND FILE_NAME != \""<< thisfilename<<"\"";
5553     if( !MISSING_pkt1 ){
5554     oss << " AND ";
5555     oss << " PKT_NUMBER_INIT < "<<pkt1;
5556     oss << " AND ";
5557     oss << " PKT_NUMBER_FINAL > "<<pkt1;
5558     oss << " AND ";
5559     oss << " PKT_OBT_INIT < "<<obt1;
5560     oss << " AND ";
5561     oss << " PKT_OBT_FINAL > "<<obt1;
5562     }else{
5563     if(pkt2>1) pkt1 = pkt2-1;//serve dopo
5564     }
5565     if( !MISSING_pkt2 ){
5566     oss << " AND ";
5567     oss << " PKT_NUMBER_INIT < "<<pkt2;
5568     oss << " AND ";
5569     oss << " PKT_NUMBER_FINAL > "<<pkt2;
5570     oss << " AND ";
5571     oss << " PKT_OBT_INIT < "<<obt2;
5572     oss << " AND ";
5573     oss << " PKT_OBT_FINAL > "<<obt2;
5574     }else{
5575     if(pkt1>0) pkt2 = pkt1+1;//serve dopo
5576     }
5577     if( boot> 0 ){
5578     oss << " AND ";
5579     oss << " BOOT_NUMBER = "<<boot;
5580     }else{
5581     }
5582     oss << " ORDER BY BAD_PKT_CALREAD ASC; ";
5583    
5584     TSQLResult *result2 = 0;
5585     TSQLRow *row2 = 0;
5586    
5587     if ( IsDebug() ) cout << oss.str().c_str() << endl;
5588     result2 = conn->Query(oss.str().c_str());
5589     if ( !result2 ) throw -4;;
5590     if ( IsDebug() ) cout <<"Rows: "<<result2->GetRowCount()<<endl;
5591    
5592     // ------------------------------------------------------
5593     // loop over files containing repetition (if any)
5594     // ------------------------------------------------------
5595     do {
5596     row2 = result2->Next();
5597     if(!row2)break;
5598    
5599     // ------------------------------------------------------
5600     // ... a repetition is found ...
5601     // ------------------------------------------------------
5602     path = (TString)row2->GetField(0);
5603     name = (TString)row2->GetField(1);
5604     raw = (TString)row2->GetField(5);
5605 mocchiut 1.33 // obt0 = (UInt_t)atoll(row2->GetField(2));
5606     // timesync = (UInt_t)atoll(row2->GetField(3));
5607     // boot_number = (UInt_t)atoll(row2->GetField(4));
5608 pam-fi 1.4
5609     if ( IsDebug() ) cout << "- - - - - - - - - - -" <<endl;
5610     // cout << path <<endl;
5611 mocchiut 1.5 // cout << "File : " <<name <<endl;
5612 pam-fi 1.4 // cout << obt0 <<endl;
5613     // cout << timesync <<endl;
5614 mocchiut 1.5 // cout << "boot n. : "<<boot_number <<endl;
5615 pam-fi 1.4 // cout << raw <<endl;
5616    
5617     // ------------------------------------------------------
5618     // ... retrieve the calibration packets.
5619     // ------------------------------------------------------
5620 mocchiut 1.7 if ( IsDebug() ) printf(" file is %s/%s \n",((TString)gSystem->ExpandPathName(path.Data())).Data(),name.Data());
5621     TFile *file = new TFile(((TString)gSystem->ExpandPathName(path.Data()))+"/"+name); // EM, path could be symbolic and we must expand it
5622 pam-fi 1.4 if(!file)throw -100;
5623     if(file->IsZombie())throw -100;
5624     //
5625     tr1 = (TTree*)file->Get("CalibTrk1");
5626     if ( !tr1 || tr1->IsZombie() ) throw -22;
5627     tr2 = (TTree*)file->Get("CalibTrk2");
5628     if ( !tr2 || tr2->IsZombie() ) throw -23;
5629     //
5630     tr1->SetBranchAddress("CalibTrk1", &caltrk1);
5631     tr1->SetBranchAddress("Header", &eh1);
5632     nevents1 = tr1->GetEntries();
5633     tr2->SetBranchAddress("CalibTrk2", &caltrk2);
5634     tr2->SetBranchAddress("Header", &eh2);
5635     nevents2 = tr2->GetEntries();
5636     for(this_t1=0; this_t1<nevents1; this_t1++){
5637     tr1->GetEntry(this_t1);
5638     if(
5639     (UInt_t)eh1->GetPscuHeader()->GetCounter() == pkt1 &&
5640     true) break;
5641     this_MISSING_pkt1 = true;
5642     }
5643     for(this_t2=0; this_t2<nevents2; this_t2++){
5644     tr2->GetEntry(this_t2);
5645     if(
5646     (UInt_t)eh2->GetPscuHeader()->GetCounter() == pkt2 &&
5647     true) break;
5648     this_MISSING_pkt2 = true;
5649     }
5650     this_valid =
5651     ValidateTrkCalib( caltrk1, eh1, file )
5652     *
5653     ValidateTrkCalib( caltrk2, eh2, file );
5654    
5655     // ---------------------------------------------------------------------
5656     // accept the calibration if it is better than the previous:
5657     //
5658     // - if the new calibration is perfect (both valid packets)
5659     // - if the new calibration has both the packets and the previous does not
5660     // ---------------------------------------------------------------------
5661     if(
5662     ( !this_MISSING_pkt1&&!this_MISSING_pkt2&&this_valid )||
5663     ( (MISSING_pkt1||MISSING_pkt2) && (!this_MISSING_pkt1&&!this_MISSING_pkt2) )||
5664     false)FOUND=true;
5665    
5666     if(file)file->Close();
5667    
5668     if(FOUND)break;
5669    
5670     }while(1);//endl loop over root table entries
5671    
5672     if(FOUND)break;
5673    
5674     }//end loop over tables
5675    
5676     if(FOUND){
5677    
5678 mocchiut 1.21 if ( IsDebug() ) cout << " >>> REPETITION FOUND :-) <<<" <<endl;
5679 pam-fi 1.4
5680 mocchiut 1.21 ////////////////////////////////////////////
5681     // insert a new entry in GL_TRK_CALIB and
5682     // modify the time-tag of the previous one
5683     ////////////////////////////////////////////
5684    
5685     // ---------------------------------------------------------------------
5686     // step 1: insert a new raw file in GL_RAW
5687     // ---------------------------------------------------------------------
5688     //
5689     // check if the raw file already exist
5690     //
5691     UInt_t id_raw = 0; // EM GL_RAW is there only for backward compatibility so we do not need to fill it when in "chewbacca" mode
5692     // oss.str("");
5693     // oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";";
5694     // if ( IsDebug() ) cout << oss.str().c_str() << endl;
5695 pam-fi 1.4
5696 mocchiut 1.21 // result = conn->Query(oss.str().c_str());
5697     // if ( !result ) throw -4;;
5698     // if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5699     // if( result->GetRowCount() == 0){
5700     // if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl;
5701     // // - - - - - - - - - - -
5702     // // insert new raw file
5703     // // - - - - - - - - - - -
5704     // GL_RAW glraw = GL_RAW();
5705     // glraw.PATH = gSystem->DirName(raw.Data());
5706     // glraw.NAME = gSystem->BaseName(raw.Data());
5707     // glraw.BOOT_NUMBER = boot_number;
5708     // //
5709     // insertPamelaRawFile( &glraw );
5710     // //
5711     // id_raw = glraw.ID;
5712     // }else{
5713     // row = result->Next();
5714     // id_raw = (UInt_t)atoll(row->GetField(0));
5715     // }
5716     // if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl;
5717    
5718     // ---------------------------------------------------------------------
5719     // step 1(bis): retrieve the timesync id associated to the file
5720     // (NB, uso lo stesso associato al file iniziale)
5721     // ---------------------------------------------------------------------
5722     UInt_t idtimesync = 0;
5723     oss.str("");
5724     oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";
5725     if ( debug ) printf(" %s \n",oss.str().c_str());
5726     result = conn->Query(oss.str().c_str());
5727     if ( !result ) throw -3;
5728 pam-fi 1.4 row = result->Next();
5729     if ( !row ) throw -3;
5730     idtimesync = (UInt_t)atoll(row->GetField(0));
5731     if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl;
5732    
5733 mocchiut 1.21 delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5734 pam-fi 1.4 // ---------------------------------------------------------------------
5735     // step 2: insert a new root file in GL_ROOT
5736     // ---------------------------------------------------------------------
5737     //
5738 mocchiut 1.5 // check if the root file already exist
5739 pam-fi 1.4 //
5740     UInt_t id_root = 0;
5741     oss.str("");
5742     oss << "SELECT ID FROM GL_ROOT where NAME=\"" << gSystem->BaseName(name.Data()) <<"\";";
5743     if ( IsDebug() ) cout << oss.str().c_str() << endl;
5744    
5745     result = conn->Query(oss.str().c_str());
5746     if ( !result ) throw -4;;
5747     if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5748     if( result->GetRowCount() == 0){
5749     if ( IsDebug() ) cout << " << Insert new ROOT file >> "<<endl;
5750     // - - - - - - - - - - -
5751     // insert new root file
5752     // - - - - - - - - - - -
5753     GL_ROOT glroot = GL_ROOT();
5754     glroot.ID_RAW = id_raw;
5755     glroot.ID_TIMESYNC = idtimesync;
5756 mocchiut 1.5 //
5757     // EM STATIC = the full expanded path must be put in the DB, KEEPENV = the path given as input (or found in ROOT_TABLE) must be used,
5758     // NOT STATIC NOT KEEPENV = $PAM_L0 must be used in the DB
5759     //
5760     if ( STATIC ){
5761     glroot.PATH = (TString)gSystem->ExpandPathName(path);
5762     } else {
5763     if ( KEEPENV ){
5764     glroot.PATH = path;
5765     } else {
5766     glroot.PATH = "$PAM_L0";
5767     };
5768     };
5769     // glroot.PATH = path;
5770 pam-fi 1.4 glroot.NAME = name;
5771     //
5772     insertPamelaRootFile( &glroot );
5773     //
5774     id_root = glroot.ID;
5775     }else{
5776     row = result->Next();
5777     if(row)id_root = (UInt_t)atoll(row->GetField(0));
5778     }
5779     if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl;
5780    
5781 mocchiut 1.21 delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5782 pam-fi 1.4 // ---------------------------------------------------------------------
5783     // step 3: modify time-tag of corrupted GL_TRK_CALIB entry
5784     // ---------------------------------------------------------------------
5785     if ( IsDebug() ) cout << " << Modify time-tag of calibration ID="<<id<<" >> "<<endl;
5786     oss.str("");
5787     oss << " UPDATE GL_TRK_CALIB SET "
5788     << " TO_TIME=0 , FROM_TIME=0 WHERE "
5789     << " ID = "<< id << ";";
5790     if ( IsDebug() ) cout << oss.str().c_str() << endl;
5791     result = conn->Query(oss.str().c_str());
5792     if ( !result ) throw -4;;
5793    
5794 mocchiut 1.21 delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5795    
5796 pam-fi 1.4 // ---------------------------------------------------------------------
5797     // step 4: insert the new calibration:
5798     // ---------------------------------------------------------------------
5799     if ( IsDebug() ) cout << " << Insert new TRK calibration >> "<<endl;
5800     //
5801     GL_TRK_CALIB glcal = GL_TRK_CALIB();
5802     //
5803     glcal.ID_ROOT_L0 = id_root;
5804     glcal.EV_ROOT_CALIBTRK1 = this_t1;
5805     glcal.EV_ROOT_CALIBTRK2 = this_t2;
5806     glcal.FROM_TIME = fromtime;
5807     glcal.TO_TIME = totime;
5808     glcal.OBT1 = obt1;
5809     glcal.OBT2 = obt2;
5810     glcal.PKT1 = pkt1;
5811     glcal.PKT2 = pkt1;
5812     glcal.BOOT_NUMBER = GetBOOTnumber();
5813     glcal.VALIDATION = this_valid;
5814     //
5815     HandleTRK_CALIB(&glcal);
5816     if ( IsDebug() ) cout << "ID = "<<glcal.ID<<endl;
5817     //
5818    
5819     }
5820     if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;
5821    
5822     }while(1);//end loop over calibrations
5823    
5824    
5825     if( result )delete result;
5826     if( row )delete row;
5827    
5828    
5829    
5830    
5831    
5832    
5833     }
5834    
5835    
5836     // // ------------------------------
5837     // // try to recover the calibration
5838     // // ------------------------------
5839     // cout << "TRY TO RECOVER TRACKER CALIBRATION"<<endl;
5840     // //
5841     // ULong64_t time = 0; //absolute time
5842     // string path[100]; //mettere un limite massimo
5843     // int nrows = 0;
5844     // UInt_t pkt = 0;
5845     // UInt_t obt = 0;
5846     // char *type = "";
5847     // EventHeader *eh = new EventHeader();
5848     // CalibTrk1Event *c = new CalibTrk1Event();
5849    
5850     // //
5851     // if(which_is_not_valid==1 || which_is_not_valid==3){
5852     // //
5853     // cout << "PKT1 --> missing or corrupted "<<endl;
5854     // type = "CalibTrk1";
5855     // pkt = pkt1;
5856     // obt = obt1;
5857     // time = this->GetAbsTime(obt1);
5858     // if( pkt1 == 0 ){//missing
5859     // time = this->GetAbsTime(obt2);
5860     // pkt = pkt2-1;
5861     // }
5862     // //
5863     // }else if (which_is_not_valid==2 || which_is_not_valid==3){
5864     // //
5865     // cout << "PKT2--> missing or corrupted "<<endl;
5866     // type = "CalibTrk2 ";
5867     // pkt = pkt2;
5868     // obt = obt2;
5869     // time = this->GetAbsTime(obt2);
5870     // if( pkt2 == 0 ){//missing
5871     // time = this->GetAbsTime(obt1);
5872     // pkt = pkt1+1;
5873     // }
5874     // //
5875     // }else{
5876     // cout << "this should not happen!!! "<<endl;
5877     // trow -666;
5878     // }
5879    
5880     // nrows = Query_ROOT_TABLE(time,conn,path);// get the list of file which might contain the packet
5881    
5882    
5883     // for(int r=0; r<nrows; r++){ //loop over rows
5884     // if(path)cout << r << " >>>> "<<(path+r)->c_str() << endl;
5885     // /// verifica che il file non sia quello gia` aperto
5886     // }
5887    
5888     // ////////////////////////////////////////////////////////////////////////
5889    
5890     // TSQLResult *result = 0;
5891     // TSQLRow *row = 0;
5892     // //
5893     // stringstream oss;
5894     // oss.str("");
5895     // // ----------------------------------------
5896     // // read the id of last calibration inserted
5897     // // ----------------------------------------
5898     // oss.str("");
5899     // oss << " SELECT ";
5900     // oss << " (ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION) ";
5901     // oss << " ORDER BY ID DESC LIMIT 1; ";
5902    
5903     // result = conn->Query(oss.str().c_str());
5904     // row = result->Next();
5905     // if( !row )throw -666;
5906    
5907     // if( result )delete result;
5908     // if( row )delete row;
5909    
5910     // UInt_t id = (UInt_t)atoll(row->GetField(0));
5911    
5912     // // -------------------------------------
5913     // // ...and modify it with new parameters
5914     // // -------------------------------------
5915    
5916    
5917     // }
5918 mocchiut 1.1 //
5919     return(0);
5920     };
5921    
5922    
5923     /**
5924     * Scan S4 calibrations packets, fill the GL_S4_CALIB table
5925     */
5926     Int_t PamelaDBOperations::insertS4_CALIB(){
5927     //
5928     TSQLResult *result = 0;
5929     TSQLRow *row = 0;
5930     //
5931     stringstream oss;
5932     oss.str("");
5933     //
5934     TTree *tr = 0;
5935     EventHeader *eh = 0;
5936     PscuHeader *ph = 0;
5937     //
5938     UInt_t nevents = 0;
5939     UInt_t fromtime = 0;
5940     UInt_t totime = 0;
5941     UInt_t obt = 0;
5942     UInt_t pkt = 0;
5943     //
5944     tr = (TTree*)file->Get("CalibS4");
5945     if ( !tr || tr->IsZombie() ) throw -24;
5946     //
5947     tr->SetBranchAddress("Header", &eh);
5948     //
5949     nevents = tr->GetEntries();
5950     //
5951     if ( !nevents ) return(1);
5952     //
5953     for (UInt_t i = 0; i < nevents; i++){
5954     //
5955     tr->GetEntry(i);
5956     //
5957     ph = eh->GetPscuHeader();
5958     obt = ph->GetOrbitalTime();
5959     pkt = ph->GetCounter();
5960     fromtime = this->GetAbsTime(ph->GetOrbitalTime());
5961     if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->PKT(pkt) <= upperpkt && this->OBT(obt) >= this->OBT(obtfirst) && this->OBT(obt) <= upperobt ){
5962     // if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
5963     //
5964     if ( IsDebug() ) printf(" S4 calibration at time %u obt %u pkt %u \n",fromtime,obt,pkt);
5965     //
5966     // check if the calibration has already been inserted
5967     //
5968     oss.str("");
5969     oss << " SELECT ID FROM GL_S4_CALIB WHERE "
5970     << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
5971     << " OBT = "<< obt << " AND "
5972     << " PKT = "<< pkt << ";";
5973     //
5974     if ( IsDebug() ) printf(" Check if the S4 calibration has already been inserted: query is \n %s \n",oss.str().c_str());
5975     result = conn->Query(oss.str().c_str());
5976     //
5977     if ( !result ) throw -4;
5978     //
5979     row = result->Next();
5980     //
5981     if ( row ){
5982     //
5983     if ( IsDebug() ) printf(" S4 calibration already inserted in the DB\n");
5984     if ( PEDANTIC ) throw -81;
5985     //
5986     } else {
5987     //
5988     // we have to insert a new calibration, check where to place it
5989     //
5990     oss.str("");
5991     oss << " SELECT ID,TO_TIME FROM GL_S4_CALIB WHERE "
5992     << " FROM_TIME < "<< fromtime << " AND "
5993     << " TO_TIME > "<< fromtime << ";";
5994     //
5995     if ( IsDebug() ) printf(" Check where to place the S4 calibration: query is \n %s \n",oss.str().c_str());
5996     result = conn->Query(oss.str().c_str());
5997     //
5998     if ( !result ) throw -4;
5999     //
6000     row = result->Next();
6001     //
6002     if ( !row ){
6003     //
6004     // no calibrations in the db contain our calibration
6005     //
6006     if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");
6007     if ( fromtime < 1150871000 ){
6008     if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
6009     fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
6010     };
6011     //
6012     oss.str("");
6013     oss << " SELECT FROM_TIME FROM GL_S4_CALIB WHERE "
6014     << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
6015     //
6016     if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
6017     result = conn->Query(oss.str().c_str());
6018     //
6019     if ( !result ) throw -4;
6020     //
6021     row = result->Next();
6022     if ( !row ){
6023     totime = numeric_limits<UInt_t>::max();
6024     } else {
6025     totime = (UInt_t)atoll(row->GetField(0));
6026     };
6027     //
6028     } else {
6029     //
6030     // determine upper and lower limits and make space for the new calibration
6031     //
6032     totime = (UInt_t)atoll(row->GetField(1));
6033     //
6034     oss.str("");
6035     oss << " UPDATE GL_S4_CALIB SET "
6036     << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
6037     << " ID = "<< row->GetField(0) << ";";
6038     //
6039     if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
6040     result = conn->Query(oss.str().c_str());
6041     //
6042     if ( !result ) throw -4;
6043     //
6044     };
6045     //
6046     oss.str("");
6047     oss << " INSERT INTO GL_S4_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,OBT,PKT,BOOT_NUMBER) "
6048     << " VALUES (NULL,' "
6049     << idroot << "','"
6050     << i << "','"
6051     << fromtime << "','"
6052     << totime << "','"
6053     << obt << "','"
6054     << pkt << "','"
6055     << this->GetBOOTnumber() << "');";
6056     //
6057     if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
6058     //
6059     result = conn->Query(oss.str().c_str());
6060     //
6061     if ( !result ) throw -4;
6062     //
6063     };
6064     //
6065     } else {
6066     //
6067     if ( IsDebug() ) printf(" S4 calibration at time %u obt %u pkt %u OUTSIDE the considered time interval\n",fromtime,obt,pkt);
6068     // if ( PEDANTIC ) throw -82;
6069     //
6070     };
6071     //
6072     };
6073     //
6074     return(0);
6075     };
6076    
6077     /**
6078     * Scan the fragment table and move old fragments to the GL_RUN table
6079     */
6080     Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
6081     return(this->CleanGL_RUN_FRAGMENTS(""));
6082     };
6083    
6084     /**
6085     * Scan the fragment table and move old fragments to the GL_RUN table
6086     */
6087     Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(Bool_t runpieces){
6088     return(this->CleanGL_RUN_FRAGMENTS("",runpieces));
6089     };
6090    
6091     /**
6092     * Scan the fragment table and move old fragments to the GL_RUN table
6093     */
6094     Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
6095     return(this->CleanGL_RUN_FRAGMENTS("",false));
6096     };
6097    
6098     /**
6099     * Scan the fragment table and move old fragments to the GL_RUN table
6100     */
6101     Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile, Bool_t runpieces){
6102     //
6103     TSQLResult *nresult = 0;
6104     TSQLRow *nrow = 0;
6105     TSQLResult *nresult1 = 0;
6106     TSQLRow *nrow1 = 0;
6107     TSQLResult *result = 0;
6108     TSQLRow *row = 0;
6109     TSQLResult *result2 = 0;
6110     TSQLRow *row2 = 0;
6111     //
6112     UInt_t moved = 0;
6113     //
6114     stringstream oss;
6115     oss.str("");
6116     //
6117     // Before moving blindly the runs from GL_RUN_FRAGMENTS to GL_RUN try to find out if we have runs divided in more than two pieces (chewbacca or explicit flag only)
6118     //
6119     if ( runpieces ){
6120     //
6121     UInt_t nid = 0;
6122     UInt_t myid[500];
6123     memset(myid,0,500*sizeof(UInt_t));
6124     //
6125     oss.str("");
6126 mocchiut 1.34 oss << "SELECT ID,RUNTRAILER_TIME,RUNTRAILER_PKT,BOOT_NUMBER FROM GL_RUN_FRAGMENTS WHERE INSERT_TIME <= '" << clean_time->AsSQLString() << "' order BY RUNHEADER_TIME asc;";
6127 mocchiut 1.1 if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
6128     nresult = conn->Query(oss.str().c_str());
6129     //
6130     if ( nresult ){
6131     //
6132     nrow = nresult->Next();
6133     //
6134     while ( nrow ){
6135     //
6136     UInt_t mbo = (UInt_t)atoll(nrow->GetField(3));
6137     UInt_t mrhp = (UInt_t)atoll(nrow->GetField(2));
6138     UInt_t mrht = (UInt_t)atoll(nrow->GetField(1));
6139     Bool_t anr = true;
6140     Bool_t runisthere = true;
6141     //
6142     for (UInt_t u=0; u<=nid; u++){
6143     if ( (UInt_t)atoll(nrow->GetField(0)) == myid[u] && (UInt_t)atoll(nrow->GetField(0)) != 0 ) runisthere = false;
6144     };
6145     //
6146     // check if the run is still in the fragment table or if we have just move it in the gl_run table!
6147     //
6148     if ( runisthere ){
6149     //
6150     memset(myid,0,500*sizeof(UInt_t));
6151     nid = 0;
6152     myid[nid] = (UInt_t)atoll(nrow->GetField(0));
6153     //
6154     while ( anr ){
6155     //
6156     oss.str("");
6157     oss << "SELECT ID,RUNTRAILER_TIME,RUNTRAILER_PKT,BOOT_NUMBER FROM GL_RUN_FRAGMENTS WHERE BOOT_NUMBER=" << mbo << " AND RUNHEADER_PKT=" << mrhp << "+1 AND ABS(RUNHEADER_TIME-"<< mrht <<")<=1 AND INSERT_TIME <= '" << clean_time->AsSQLString() << "' order BY RUNHEADER_TIME asc;";
6158     if ( IsDebug() ) printf(" In the loop searching for fragmented runs : query is \n %s \n",oss.str().c_str());
6159     //
6160     nresult1 = conn->Query(oss.str().c_str());
6161     //
6162     if ( nresult1 ){
6163     //
6164     if ( nresult1->GetRowCount() == 1 ){
6165     //
6166     // one piece is found
6167     //
6168     nrow1 = nresult1->Next();
6169     //
6170     if ( nrow1 ){
6171     //
6172     nid++;
6173     myid[nid] = (UInt_t)atoll(nrow1->GetField(0));
6174     mbo = (UInt_t)atoll(nrow1->GetField(3));
6175     mrhp = (UInt_t)atoll(nrow1->GetField(2));
6176     mrht = (UInt_t)atoll(nrow1->GetField(1));
6177     if ( debug ) printf(" FOUND A PIECE OF RUN! nid %u myid[nid] %u mbo %u mrhp %u mrht %u \n",nid,myid[nid],mbo,mrhp,mrht);
6178     //
6179     nrow1->Close();
6180     } else {
6181     throw -88;
6182     };
6183     } else {
6184     anr = false;
6185     };
6186     nresult1->Close();
6187     } else {
6188     throw -88;
6189     };
6190     };
6191     //
6192     // handle these runs which are ordered and "good". Does the first contain a valid runheader?
6193     //
6194     oss.str("");
6195     oss << " ID= "<< myid[0];
6196     //
6197     glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
6198     //
6199     if ( glrun->GetACQ_BUILD_INFO() != 0 ){
6200     //
6201     // the first piece contains a good runheader we can update all the other runs with correct infos!
6202     //
6203     for (UInt_t u=1; u <= nid ; u++){
6204     oss.str("");
6205     oss << "UPDATE GL_RUN_FRAGMENTS SET "
6206     << " RUNHEADER_TIME=" << glrun->GetRUNHEADER_TIME()<< " , "
6207     << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "
6208     << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< " , "
6209     << " COMPILATIONTIMESTAMP=" << glrun->GetCOMPILATIONTIMESTAMP()<< " , "
6210     << " FAV_WRK_SCHEDULE=" << glrun->GetFAV_WRK_SCHEDULE()<< " , "
6211     << " EFF_WRK_SCHEDULE=" << glrun->GetEFF_WRK_SCHEDULE()<< " , "
6212     << " PRH_VAR_TRG_MODE_A=" << glrun->GetPRH_VAR_TRG_MODE_A()<< " , "
6213     << " PRH_VAR_TRG_MODE_B=" << glrun->GetPRH_VAR_TRG_MODE_B()<< " , "
6214     << " ACQ_BUILD_INFO=" << glrun->GetACQ_BUILD_INFO()<< " , "
6215     << " ACQ_VAR_INFO=" << glrun->GetACQ_VAR_INFO()<< " , "
6216     << " RM_ACQ_AFTER_CALIB=" << glrun->GetRM_ACQ_AFTER_CALIB()<< " , "
6217     << " RM_ACQ_SETTING_MODE=" << glrun->GetRM_ACQ_SETTING_MODE()<< " , "
6218     << " TRK_CALIB_USED=" << glrun->GetTRK_CALIB_USED()<< " , "
6219     << " CAL_DSP_MASK=" << glrun->GetCAL_DSP_MASK()<< " , "
6220 mocchiut 1.2 << " LAST_TIMESYNC=" << glrun->GetLAST_TIMESYNC()<< " , ";
6221     //
6222     if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() )
6223     oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
6224     if ( glrun->GetPHYSENDRUN_MASK_S11CRC() )
6225     oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC() << " , ";
6226     //
6227     oss << " OBT_TIMESYNC=" << glrun->GetOBT_TIMESYNC();
6228     oss << " WHERE ID=" << myid[u] << ";";
6229 mocchiut 1.1 conn->Query(oss.str().c_str());
6230     };
6231     //
6232     } else {
6233     //
6234     // sig no runheader, let set anyway what is possible...
6235     //
6236     for (UInt_t u=1; u <= nid ; u++){
6237     oss.str("");
6238     oss << "UPDATE GL_RUN_FRAGMENTS SET "
6239 mocchiut 1.2 << " RUNHEADER_TIME=" << glrun->GetRUNHEADER_TIME()<< " , ";
6240     //
6241     if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() )
6242     oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
6243     if ( glrun->GetPHYSENDRUN_MASK_S11CRC() )
6244     oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6245     //
6246     oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "
6247 mocchiut 1.21 << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT();
6248     oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6249 mocchiut 1.1 conn->Query(oss.str().c_str());
6250     };
6251     };
6252     //
6253     // now let's look for runtrailer if any in the last run
6254     //
6255     oss.str("");
6256     oss << " ID= "<< myid[nid];
6257     //
6258     glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
6259     //
6260     if ( glrun->GetPKT_READY_COUNTER() != 0 ){
6261     //
6262     // the first piece contains a good runtrailer we can update all the other runs with correct infos!
6263     //
6264     for (UInt_t u=0; u < nid ; u++){
6265     oss.str("");
6266     oss << "UPDATE GL_RUN_FRAGMENTS SET "
6267     << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "
6268     << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , "
6269     << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< " , "
6270 mocchiut 1.2 << " PKT_COUNTER=" << glrun->GetPKT_COUNTER()<< " , ";
6271     //
6272     if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ){
6273     oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6274     if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) {
6275     oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6276     //
6277     oss << " PKT_READY_COUNTER=" << glrun->GetPKT_READY_COUNTER()
6278 mocchiut 1.1 << " WHERE ID=" << myid[u] << ";";
6279     conn->Query(oss.str().c_str());
6280     };
6281     //
6282     } else {
6283     //
6284     // sig no runtrailer, let set anyway what is possible...
6285     //
6286     for (UInt_t u=0; u < nid ; u++){
6287     oss.str("");
6288     oss << "UPDATE GL_RUN_FRAGMENTS SET "
6289     << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "
6290 mocchiut 1.2 << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , ";
6291     //
6292     if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ){
6293     oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6294     if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){
6295     oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6296     //
6297 mocchiut 1.21 oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT();
6298     oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6299 mocchiut 1.1 conn->Query(oss.str().c_str());
6300     };
6301     };
6302     //
6303     // Now we want to cross indexize the runs
6304     //
6305     for (UInt_t u=0; u < nid ; u++){
6306     oss.str("");
6307     oss << "UPDATE GL_RUN_FRAGMENTS SET "
6308     << " ID_RUN_FRAG=" << myid[u+1] << " where ID=" << myid[u] <<";";
6309     conn->Query(oss.str().c_str());
6310     };
6311     oss.str("");
6312     oss << "UPDATE GL_RUN_FRAGMENTS SET "
6313     << " ID_RUN_FRAG=" << myid[0] << " where ID=" << myid[nid] <<";";
6314     conn->Query(oss.str().c_str());
6315     //
6316     // and now we can move the runs in the GL_RUN table
6317     //
6318     for (UInt_t u=0; u <= nid; u++){
6319     oss.str("");
6320     oss << " ID= "<< myid[u];
6321     //
6322     glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
6323     //
6324     if ( u == 0 ){
6325     oss.str("");
6326     oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
6327     << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
6328     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
6329     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
6330     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
6331     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
6332     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
6333     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
6334     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
6335     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
6336     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
6337     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
6338     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
6339     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
6340     //
6341     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
6342     result2 = conn->Query(oss.str().c_str());
6343     //
6344     if ( !result2 ) throw -4;
6345     //
6346     row2 = result2->Next();
6347     //
6348     if ( row2 ){
6349 mocchiut 1.23 if ( IsDebug() ) printf(" The run already exists in the GL_RUN table! \n");
6350 mocchiut 1.1 if ( PEDANTIC ) throw -83;
6351     row2->Close();
6352     };
6353     result2->Close();
6354     };
6355     //
6356     if ( IsDebug() ) printf(" The run is new \n");
6357     if ( IsDebug() ) printf(" -> fill the DB \n");
6358     //
6359     glrun->Fill_GL_RUN(conn);
6360     //
6361     if ( IsDebug() ) printf(" Delete run %u from the GL_RUN_FRAGMENTS table \n",myid[u]);
6362     //
6363     glrun->DeleteRun(conn,myid[u],"GL_RUN_FRAGMENTS");
6364     //
6365     moved++;
6366     //
6367     };
6368     //
6369     };
6370     //
6371     nrow = nresult->Next();
6372     };
6373     };
6374    
6375    
6376    
6377     };
6378     //
6379     if ( !strcmp(fcleanfile.Data(),"") ){
6380     //
6381     // check if there are entries older than "olderthan" seconds from now
6382     //
6383     oss.str("");
6384     oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
6385     << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
6386     //
6387     if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
6388     result = conn->Query(oss.str().c_str());
6389     //
6390     } else {
6391     oss.str("");
6392     oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
6393     if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
6394     result = conn->Query(oss.str().c_str());
6395     //
6396     if ( result ){
6397     //
6398     row = result->Next();
6399     //
6400     if ( row ){
6401     oss.str("");
6402     oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
6403     << " ID_ROOT_L0=" << row->GetField(0) << ";";
6404     //
6405     if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
6406     result = conn->Query(oss.str().c_str());
6407     //
6408     };
6409     } else {
6410     return(2);
6411     };
6412     };
6413     //
6414     if ( result ){
6415     //
6416     row = result->Next();
6417     //
6418     while ( row ){
6419     //
6420     oss.str("");
6421     oss << " ID= "<< row->GetField(0);
6422     //
6423     glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
6424     //
6425     oss.str("");
6426 mocchiut 1.23 oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER,ID_ROOT_L0 FROM GL_RUN WHERE "
6427 mocchiut 1.1 << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
6428     << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
6429     << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
6430     << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
6431     << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
6432     << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
6433     << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
6434     << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
6435     << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
6436     << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
6437     << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
6438     << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
6439     << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
6440     //
6441     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
6442     result2 = conn->Query(oss.str().c_str());
6443     //
6444     if ( !result2 ) throw -4;
6445     //
6446     row2 = result2->Next();
6447     //
6448     if ( !row2 ){
6449     //
6450     if ( IsDebug() ) printf(" The run is new \n");
6451     if ( IsDebug() ) printf(" -> fill the DB \n");
6452     //
6453     // glrun->SetID(this->AssignRunID()); we use the old run number!
6454     glrun->SetID_RUN_FRAG(glrun->GetID());
6455     glrun->Fill_GL_RUN(conn);
6456     //
6457 mocchiut 1.2 // oss.str("");
6458     // oss << " SELECT ID FROM GL_RUN WHERE "
6459     // << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
6460     // << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
6461     // << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
6462     // << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
6463     // << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
6464     // //
6465     // if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
6466     // result2 = conn->Query(oss.str().c_str());
6467     // //
6468     // if ( !result2 ) throw -4;
6469     // //
6470     // row2 = result2->Next();
6471     // //
6472     // if ( !row2 ) throw -25;
6473     // //
6474     // oss.str("");
6475     // oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);
6476     // if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());
6477     // result2 = conn->Query(oss.str().c_str());
6478     // //
6479     // if ( !result2 ) throw -4;
6480 mocchiut 1.1 //
6481     moved++;
6482     //
6483     } else {
6484 mocchiut 1.23
6485     // questa parte mah mah mah... da controllare
6486    
6487     //
6488     // we end up here if chewbacca and we have RH---| small gap |---RT, in this case the two pieces are attached and moved to GL_RUN. We are now processing the
6489     // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number.
6490     //
6491     // 090112 [8RED (-70): RUN ALREADY INSERTED]
6492     //
6493     Bool_t OK = false;
6494     UInt_t IDRL2A = 0;
6495     UInt_t IDRL2B = 0;
6496     if ( result2->GetRowCount() == 2 ){
6497     IDRL2A = (UInt_t)atoll(row2->GetField(0));
6498     UInt_t IDRL0A = (UInt_t)atoll(row2->GetField(4));
6499     row2 = result2->Next();
6500     IDRL2B = (UInt_t)atoll(row2->GetField(0));
6501     UInt_t IDRL0B = (UInt_t)atoll(row2->GetField(4));
6502     if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B);
6503     // if ( IDRL0A == IDRL0B ){
6504     // TSQLResult *result2a = 0;
6505     // TSQLRow *row2a = 0;
6506     // oss.str("");
6507     // oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;";
6508     // if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
6509     // result2a = conn->Query(oss.str().c_str());
6510     // //
6511     // if ( !result2a ) throw -4;
6512     // //
6513     // row2a = result2a->Next();
6514     // //
6515     // if ( row2a ){
6516     // UInt_t PKA = (UInt_t)atoll(row2a->GetField(0));
6517     // delete result2a;
6518     // oss.str("");
6519     // oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;";
6520     // if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
6521     // result2a = conn->Query(oss.str().c_str());
6522     // //
6523     // if ( !result2a ) throw -4;
6524     // //
6525     // row2a = result2a->Next();
6526     // //
6527     // if ( row2a ){
6528     // UInt_t PKB = (UInt_t)atoll(row2a->GetField(0));
6529     // //
6530     // if ( IsDebug() ) printf(" PKT(PKA) + 1 %llu == runheaderpkt %llu && PKB = runtrailer %llu + 1 %llu \n",PKT(PKA)+1LL, PKT(glrun->GetRUNHEADER_PKT()),PKT(PKB), PKT(glrun->GetRUNTRAILER_PKT())+1LL);
6531     // if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){
6532     // if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
6533     OK = true;
6534     // };
6535     // };
6536     // };
6537     // };
6538     };
6539     if ( OK ){
6540     //
6541     // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table
6542     //
6543     // we have to update with runheader/trailer infos our run and we have to change the ROOT_ID_FRAG of the header piece ( 1/2 2/1 must become 1/3 3/2 2/1 )
6544     //
6545     GL_RUN *glA = new GL_RUN();
6546     glA->Query_GL_RUN(IDRL2A,conn);
6547     //
6548     if ( glA->GetACQ_BUILD_INFO() != 0 ){
6549     //
6550     // the first piece contains a good runheader we can update all the other runs with correct infos!
6551     //
6552     oss.str("");
6553     oss << "UPDATE GL_RUN_FRAGMENTS SET "
6554     << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "
6555     << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
6556     << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , "
6557     << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , "
6558     << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , "
6559     << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , "
6560     << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , "
6561     << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , "
6562     << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , "
6563     << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , "
6564     << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , "
6565     << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , "
6566     << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , "
6567     << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , "
6568     << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , ";
6569     //
6570     if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
6571     oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
6572     if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
6573     oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , ";
6574     //
6575     oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC();
6576     oss << " WHERE ID=" << glrun->GetID() << ";";
6577     if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str());
6578     conn->Query(oss.str().c_str());
6579     //
6580     } else {
6581     //
6582     // sig no runheader, let set anyway what is possible...
6583     //
6584     oss.str("");
6585     oss << "UPDATE GL_RUN_FRAGMENTS SET "
6586     << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , ";
6587     //
6588     if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
6589     oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
6590     if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
6591     oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6592     //
6593     oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
6594     << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT();
6595     oss << " WHERE ID=" << glrun->GetID() << ";";
6596     if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str());
6597     conn->Query(oss.str().c_str());
6598     };
6599     //
6600     // update runheader ROOT_ID_FRAG
6601     //
6602     oss.str("");
6603     oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
6604     if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
6605     conn->Query(oss.str().c_str());
6606     //
6607     // now let's look for runtrailer if any in the last run
6608     //
6609     glA->Query_GL_RUN(IDRL2B,conn);
6610     //
6611     if ( glA->GetPKT_READY_COUNTER() != 0 ){
6612     //
6613     // the first piece contains a good runtrailer we can update all the other runs with correct infos!
6614     //
6615     oss.str("");
6616     oss << "UPDATE GL_RUN_FRAGMENTS SET "
6617     << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
6618     << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "
6619     << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , "
6620     << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , ";
6621     //
6622     if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
6623     oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6624     if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) {
6625     oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6626     //
6627     oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER()
6628     << " WHERE ID=" << glrun->GetID() << ";";
6629     if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str());
6630     conn->Query(oss.str().c_str());
6631     //
6632     } else {
6633     //
6634     // sig no runtrailer, let set anyway what is possible...
6635     //
6636     oss.str("");
6637     oss << "UPDATE GL_RUN_FRAGMENTS SET "
6638     << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
6639     << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , ";
6640     //
6641     if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
6642     oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6643     if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){
6644     oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6645     //
6646     oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()
6647     << " WHERE ID=" << glrun->GetID() << ";";
6648     if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str());
6649     conn->Query(oss.str().c_str());
6650     };
6651     //
6652     UInt_t myi = glrun->GetID();
6653     oss.str("");
6654     oss << " ID= "<< myi;
6655     //
6656     glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
6657     //
6658     // fill the new run in GL_RUN
6659     //
6660     glrun->SetID_RUN_FRAG(IDRL2B);
6661     glrun->Fill_GL_RUN(conn);
6662     glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
6663     delete glA;
6664     moved++;
6665     //
6666     } else {
6667     //
6668     // is just a repetition
6669     //
6670     if ( IsDebug() ) printf(" The run %u is already present in the GL_RUN table...\n",glrun->GetID());
6671     // printf(" CCCCCCCCICCCCCCCCCCCCIOOOOOOOOOO si muove Ciccio! %u \n",glrun->GetID());
6672     // if ( PEDANTIC && glrun->GetID() != 61563 ) throw -83;
6673     if ( PEDANTIC ) throw -83;
6674     };
6675     // if ( IsDebug() ) printf(" The run already exists in the GL_RUN table! ...\n");
6676     // if ( PEDANTIC ) throw -83;
6677 mocchiut 1.1 };
6678     if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));
6679     //
6680     //
6681     glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
6682 mocchiut 1.2 // oss.str("");
6683     // oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
6684     // if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
6685     // result2 = conn->Query(oss.str().c_str());
6686     // //
6687     // if ( !result2 ) throw -4;
6688     // //
6689 mocchiut 1.1 row = result->Next();
6690     };
6691     };
6692     if ( IsDebug() ) printf(" Moved %u runs\n",moved);
6693     return(0);
6694     };
6695    
6696     /**
6697     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
6698     */
6699     Int_t PamelaDBOperations::ValidateRuns(){
6700     return(this->ValidateRuns(""));
6701     };
6702    
6703     /**
6704     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
6705     */
6706     Int_t PamelaDBOperations::ValidateRuns(TString valfile){
6707     //
6708     TSQLResult *result = 0;
6709     TSQLRow *row = 0;
6710     //
6711     UInt_t calibtime = 50;
6712     //
6713     stringstream oss;
6714     oss.str("");
6715     //
6716     // =======================================================
6717     // validate runs by checking missing calibrations
6718     // =======================================================
6719     UInt_t t_stop = 0;
6720     UInt_t t_start = 0;
6721     if ( !strcmp(valfile.Data(),"") ) {
6722     // --------------------------------------------------------------
6723     // 1) get the OBT of the last run inserted after clean-time limit
6724     // --------------------------------------------------------------
6725     oss.str("");
6726     oss << " SELECT * FROM GL_RUN WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
6727     << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
6728     if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
6729     result = conn->Query(oss.str().c_str());
6730     if ( !result ) throw -4;
6731     if ( !result->GetRowCount() ) {
6732     printf(" No runs to validate \n");
6733     return(1);
6734     }else{
6735     row = result->Next();
6736     t_start = (UInt_t)atoll(row->GetField(4));
6737     };
6738     // --------------------------------------------------------------
6739     // 2) get the OBT of the last validated run
6740     // --------------------------------------------------------------
6741     oss.str("");
6742 mocchiut 1.12 // oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
6743     oss << " SELECT * FROM GL_RUN WHERE VALIDATION>0 AND RUNHEADER_TIME<="<< t_start
6744 mocchiut 1.1 <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
6745     if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
6746     result = conn->Query(oss.str().c_str());
6747     if ( !result ) throw -4;
6748     if ( result->GetRowCount() ){
6749     row = result->Next();
6750     t_stop = (UInt_t)atoll(row->GetField(4));
6751     };
6752     if ( IsDebug() ) printf("Validation interval: from time %u - to time %u \n\n",t_stop,t_start);
6753     // --------------------------------------------------------------
6754     // now retrieves runs to be validated
6755     // --------------------------------------------------------------
6756     oss.str("");
6757     oss << " SELECT * FROM GL_RUN WHERE RUNHEADER_TIME <=" << t_start;
6758     oss << " AND RUNHEADER_TIME >="<< t_stop;
6759     oss << " ORDER BY RUNHEADER_TIME DESC;";
6760     if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
6761     result = conn->Query(oss.str().c_str());
6762     } else {
6763     //
6764     stringstream myquery;
6765     UInt_t myid = 0;
6766     myquery.str("");
6767     myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
6768     //
6769     result = conn->Query(myquery.str().c_str());
6770     //
6771     row = result->Next();
6772     if( !row ){
6773     if ( strcmp(valfile.Data(),GetRootName().Data()) ){
6774     if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
6775     return(2);
6776     };
6777     if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
6778     return(0);
6779     };
6780     myid=(UInt_t)atoll(row->GetField(0));
6781     //
6782     myquery.str("");
6783     myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
6784     //
6785     result = conn->Query(myquery.str().c_str());
6786     //
6787     row = result->Next();
6788     if( !row->GetField(0) || !row->GetField(1)){
6789     //
6790     if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
6791     //
6792     return(0);
6793     //
6794     } else {
6795     //
6796     UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
6797     UInt_t runttime = (UInt_t)atoll(row->GetField(1));
6798     UInt_t caltime = 0;
6799     //
6800     myquery.str("");
6801     myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
6802     myquery << " order by FROM_TIME asc limit 1;";
6803     //
6804     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
6805     //
6806     //
6807     result = conn->Query(myquery.str().c_str());
6808     //
6809     row = result->Next();
6810     if( !row ){
6811     caltime = runhtime;
6812     } else {
6813     caltime = (UInt_t)atoll(row->GetField(0));
6814     };
6815     //
6816     myquery.str("");
6817     myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
6818     myquery << caltime << " order by RUNHEADER_TIME DESC";
6819     //
6820     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
6821     //
6822     result = conn->Query(myquery.str().c_str());
6823     //
6824     };
6825     };
6826     //
6827     if ( !result ) throw -4;
6828     if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
6829     //
6830     Int_t nrow = 0;
6831     GL_RUN* this_run = new GL_RUN();
6832     GL_RUN* next_run = new GL_RUN();
6833     Int_t nseq_max = 1000;
6834 mocchiut 1.2 // UInt_t* sequence = new UInt_t[100];
6835 mocchiut 1.1 vector<UInt_t> sequence(nseq_max);
6836     Int_t nseq = 0;
6837     Bool_t CHECK = false;
6838     Bool_t this_ONLINE = false;
6839     Bool_t next_ONLINE = false;
6840     UInt_t t1=0,t2=0;
6841     // ---------------------------------------------------------------------------------
6842     // - loop over runs, back in time,
6843     // - select sequences of runs close in time (less than calibtime s apart),
6844     // which could be preceeded by a calibration
6845     // - check if there might be a missing calibration
6846     // ---------------------------------------------------------------------------------
6847     while(1){
6848    
6849 mocchiut 1.2 row = result->Next();
6850     if( row == NULL ) break;
6851 mocchiut 1.1
6852 mocchiut 1.2 //------------
6853     //get run info
6854     //------------
6855     this_run->Set_GL_RUN(row);
6856 mocchiut 1.1
6857 mocchiut 1.2 Bool_t this_BAD = false;
6858     if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;
6859     else if (this_run->GetTRK_CALIB_USED() == 104) this_ONLINE = false;
6860     else{
6861     // printf("Missing or corrupted header!! \n");
6862     this_ONLINE = false;
6863     this_BAD = true;
6864     };
6865 mocchiut 1.1
6866 mocchiut 1.2 //-----------------------------------
6867     //compare with previous(next in time)
6868     //-----------------------------------
6869     CHECK = false;
6870     UInt_t interval=0;
6871 mocchiut 1.1
6872 mocchiut 1.2 if( nrow != 0){
6873 mocchiut 1.1
6874    
6875 mocchiut 1.2 t1 = this_run->GetRUNTRAILER_TIME();
6876     t2 = next_run->GetRUNHEADER_TIME();
6877     interval = (t2-t1);
6878 mocchiut 1.1
6879 mocchiut 1.2 if(this_ONLINE && next_ONLINE){ // this: ON-LINE + next: ON-LINE
6880 mocchiut 1.1
6881 mocchiut 1.2 if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0; //=> run fragments
6882 mocchiut 1.1
6883 mocchiut 1.2 if( interval >= calibtime )CHECK = true; //more than calibtime s => there might be a calibration
6884 mocchiut 1.1
6885 mocchiut 1.2 if( !CHECK && this_run->VALIDATION ){
6886 mocchiut 1.12 // for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
6887     for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],this_run->VALIDATION);
6888 mocchiut 1.2 nseq=0;
6889     }
6890 mocchiut 1.1
6891 mocchiut 1.2 }else if( !this_ONLINE && next_ONLINE) { // this: DEFAULT + next:ON-LINE
6892 mocchiut 1.1
6893 mocchiut 1.2 CHECK = true;
6894 mocchiut 1.1
6895 mocchiut 1.2 }else if( !next_ONLINE ){ // this:ANY + next:DEFAULT
6896 mocchiut 1.1
6897 mocchiut 1.2 assignVALIDATION(next_run->ID,true);
6898     nseq=0;
6899     }
6900     }
6901 mocchiut 1.1
6902 mocchiut 1.2 //----------------------------
6903     //check run sequence for calib
6904     //----------------------------
6905     if( CHECK ){
6906     // check if calibration exists
6907     if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
6908 mocchiut 1.12 // Bool_t MISSING = MissingTRK_CALIB(t1,t2);
6909     UInt_t MISSING = MissingTRK_CALIB(t1,t2);
6910     UInt_t val = 0;
6911     if ( MISSING == 1 ) val = 0;
6912     if ( MISSING == 0 ) val = 1;
6913     if ( MISSING == 2 ) val = 2;
6914     for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],val);
6915 mocchiut 1.2 nseq=0;
6916     };
6917     //--------------
6918     //store run info
6919     //--------------
6920     *next_run = *this_run;
6921     next_ONLINE = this_ONLINE;
6922     if( !this_BAD ){
6923     if(nseq < nseq_max){
6924     sequence[nseq] = this_run->ID;
6925     nseq++;
6926     }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
6927     };
6928 mocchiut 1.1
6929 mocchiut 1.2 if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);
6930     nrow++;
6931 mocchiut 1.1
6932     };
6933     delete this_run;
6934     delete next_run;
6935     //
6936     return(0);
6937     };
6938     /**
6939     * Check if there might be a missing tracker calibration in a given time interval
6940     * @param t1 From absolute time
6941     * @param t2 To absolute time
6942     * @return true if there might be a missing calibration
6943     */
6944 mocchiut 1.12 //Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
6945     UInt_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
6946 mocchiut 1.1
6947 mocchiut 1.2 GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
6948 mocchiut 1.1
6949 pam-fi 1.4 // get the closest calibration before the run start (t2)
6950 mocchiut 1.12 // if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true); //>>> missing
6951     if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(1); //>>> missing
6952 mocchiut 1.1
6953 mocchiut 1.12 // if ( trkcalib->TO_TIME < t2 ) return(true); //>>> missing
6954     if ( trkcalib->TO_TIME < t2 ) return(1); //>>> missing
6955 mocchiut 1.1
6956 mocchiut 1.2 //==============================================================
6957     // Check is done first on the basis of time between calibration,
6958     // which should be equal to the time between ascending-nodes.
6959     //==============================================================
6960     if ( t2 - trkcalib->FROM_TIME > 5700) {
6961     if ( IsDebug() )printf("Long time between calib and run start %u :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);
6962     //==============================================================
6963     // there might be a missing calibration, due to:
6964     // - MM full
6965     // - corrupted packets
6966     // - loss of data
6967     // There is an exception in case a download was done during ascending node
6968     //==============================================================
6969     Bool_t DOWNLOAD = false;
6970     // check if the calib was skipped becouse of download .... DA FARE!!
6971 mocchiut 1.12 // if(DOWNLOAD)return(false);
6972     if(DOWNLOAD)return(0);
6973 mocchiut 1.1
6974 mocchiut 1.12 // return(true); //>>> missing
6975     return(1); //>>> missing
6976 mocchiut 1.1
6977 mocchiut 1.2 };
6978 mocchiut 1.1
6979 mocchiut 1.2 //==============================================================
6980     // If the last calibration is close to the run less than this time,
6981     // it is enough to say that there are no missing calibrations
6982     //==============================================================
6983     // the long time interval bewteen runs might be due to download
6984     if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
6985 mocchiut 1.12 if ( trkcalib->VALIDATION ) return(0);
6986     if ( IsDebug() )printf("Calibration is not validated... :-/ ==> OK but with VALIDATION=2! \n");
6987     return(2);
6988 mocchiut 1.1
6989     };
6990     /**
6991     * Assign VALIDATION value to a GL_RUN entry
6992     * @param idrun Run ID
6993     * @param validation true/false
6994     */
6995     Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
6996 mocchiut 1.12 return(this->assignVALIDATION(idrun,(UInt_t)validation));
6997     }
6998    
6999     Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, UInt_t validation){
7000 mocchiut 1.2 TSQLResult *result = 0;
7001     stringstream oss;
7002     oss.str("");
7003     oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
7004     //
7005     // if ( IsDebug() )
7006     // printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
7007     if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
7008     result = conn->Query(oss.str().c_str());
7009     if ( !result ) throw -4;
7010     return(0);
7011 mocchiut 1.1 }
7012    
7013    
7014    
7015     // Insert TLEs from file tlefilename in the table GL_TLE in the db
7016     // opened by conn, sorting them by date from older to newer, if each
7017     // TLE has not been alread inserted.
7018     Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
7019     {
7020     fstream tlefile(tlefilename, ios::in);
7021    
7022     if ( !tlefile ) throw -7;
7023    
7024     vector<cTle*> ctles;
7025     vector<cTle*>::iterator iter;
7026     int present = 0;
7027    
7028     // Get three lines from tlefile, create a cTle object and put it
7029     // into ctles
7030     while(1) {
7031     cTle *tlef;
7032     string str1, str2, str3;
7033    
7034     getline(tlefile, str1);
7035     if(tlefile.eof()) break;
7036    
7037     getline(tlefile, str2);
7038     if(tlefile.eof()) break;
7039    
7040     getline(tlefile, str3);
7041     if(tlefile.eof()) break;
7042    
7043     // We now have three good lines for a cTle.
7044     tlef = new cTle(str1, str2, str3);
7045     ctles.push_back(tlef);
7046     }
7047    
7048     tlefile.close();
7049    
7050     // Sort by date
7051     sort(ctles.begin(), ctles.end(), compTLE);
7052    
7053     // Now we insert each TLE into the db
7054     for(iter = ctles.begin(); iter != ctles.end(); iter++) {
7055     cTle *tle = *iter;
7056    
7057     // Do nothing if it's already present in the db. Just increase
7058     // the counter present.
7059     if (! isTlePresent(tle))
7060     {
7061     int status = insertTle(tle);
7062    
7063     // Insert query failed. Return 1.
7064     if(status == EXIT_FAILURE) {
7065    
7066     if( IsDebug() ) {
7067     cerr << "Error: inserting TLE:" << endl
7068     << tle->getName() << endl
7069     << tle->getLine1() << endl
7070     << tle->getLine2() << endl;
7071     }
7072    
7073     throw -4;
7074     return 1;
7075     }
7076    
7077     }
7078     else
7079     present++;
7080    
7081     }
7082    
7083     int inserted = ctles.size() - present; // Number of inserted TLE.
7084     if ( IsDebug() )
7085     cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
7086     << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
7087    
7088     ctles.clear();
7089    
7090    
7091     // Return 2 if no new TLE has been inserted. 0 otherwise.
7092     if(! inserted ) return 2;
7093     return 0;
7094     }
7095    
7096    
7097     // Insert tle in the table GL_TLE using the connection conn.
7098     Int_t PamelaDBOperations::insertTle(cTle *tle)
7099     {
7100     stringstream oss;
7101     TSQLResult *result = 0;
7102    
7103     oss.str("");
7104     oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
7105     << " VALUES ( '"
7106     << tle->getName() << "', '"
7107     << tle->getLine1() << "', '"
7108     << tle->getLine2() << "', '"
7109     << getTleDatetime(tle) << "')";
7110    
7111     // cout << oss.str().c_str() << endl;
7112     result = conn->Query(oss.str().c_str());
7113     if (result == NULL)
7114     return EXIT_FAILURE;
7115    
7116     return EXIT_SUCCESS;
7117     }
7118    
7119    
7120     // Return whether tle is already in the db connected by conn.
7121     bool PamelaDBOperations::isTlePresent(cTle *tle)
7122     {
7123     stringstream oss;
7124     TSQLResult *result = 0;
7125    
7126     oss.str("");
7127     oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
7128     << getTleDatetime(tle) << "'";
7129    
7130     result = conn->Query(oss.str().c_str());
7131     if (result == NULL) throw -4;
7132    
7133     if (result->GetRowCount())
7134     return true;
7135     else
7136     return false;
7137     }
7138    
7139    
7140     // Return whether the first TLE is dated early than the second
7141     bool compTLE (cTle *tle1, cTle *tle2)
7142     {
7143     return getTleJulian(tle1) < getTleJulian(tle2);
7144     }
7145    
7146    
7147     // Return the date of the tle using the format (year-2000)*1e3 +
7148     // julian day. e.g. 6365 is the 31th Dec 2006.
7149     // It does *not* return a cJulian date.
7150     float getTleJulian(cTle *tle) {
7151     return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
7152     }
7153    
7154    
7155     // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
7156     // format.
7157     string getTleDatetime(cTle *tle)
7158     {
7159     int year, mon, day, hh, mm, ss;
7160     double dom; // day of month (is double!)
7161     stringstream date; // date in datetime format
7162    
7163     // create a cJulian from the date in tle
7164     cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
7165    
7166     // get year, month, day of month
7167     jdate.getComponent(&year, &mon, &dom);
7168    
7169     // build a datetime YYYY-MM-DD hh:mm:ss
7170     date.str("");
7171     day = (int) floor(dom);
7172     hh = (int) floor( (dom - day) * 24);
7173     mm = (int) floor( ((dom - day) * 24 - hh) * 60);
7174     ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
7175     // ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
7176    
7177     date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
7178    
7179     return date.str();
7180     }
7181    
7182     /**
7183     * Remove a file from the DB, delete on cascade all entries related to that file
7184     * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
7185     * calibration
7186     **/
7187     Int_t PamelaDBOperations::removeFile(TString remfile){
7188     //
7189     // Determine ID_ROOT_L0 and ID_RAW
7190     //
7191     TSQLResult *pResult;
7192     TSQLRow *Row;
7193     stringstream myquery;
7194     //
7195     myquery.str("");
7196 mocchiut 1.10 myquery << " SELECT ID,ID_RAW,ID_TIMESYNC FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
7197 mocchiut 1.1 //
7198     pResult = conn->Query(myquery.str().c_str());
7199     //
7200     Row = pResult->Next();
7201     if( !Row ){
7202     if ( strcmp(remfile.Data(),GetRootName().Data()) ){
7203     if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
7204     return(1);
7205     };
7206     if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
7207     return(0);
7208     };
7209     //
7210     this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
7211     this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
7212 mocchiut 1.10 UInt_t idtsy=(UInt_t)atoll(Row->GetField(2));
7213 mocchiut 1.1 //
7214     this->ValidationOFF();
7215     //
7216     this->RemoveCALIBS();
7217     //
7218     this->RemoveRUNS();
7219     //
7220 mocchiut 1.10 this->RemoveFILES(idtsy);
7221 mocchiut 1.1 //
7222     this->SetID_ROOT(0);
7223     this->SetID_RAW(0);
7224     //
7225     return(0);
7226     };
7227    
7228     /**
7229     *
7230     * Set validation bit to zero for runs following the removing file till
7231     * 1) a run with TRK_CALIB_USED=140
7232     * 2) a run with VALIDATION = 0
7233     * 3) the next calibration
7234     *
7235     **/
7236     void PamelaDBOperations::ValidationOFF(){
7237     TSQLResult *pResult;
7238     TSQLRow *Row;
7239     stringstream myquery;
7240     Int_t unv = 0;
7241     //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
7242     myquery.str("");
7243     myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
7244     //
7245     pResult = conn->Query(myquery.str().c_str());
7246     //
7247     Row = pResult->Next();
7248     if( !Row->GetField(0) ){
7249     //
7250     if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
7251     //
7252     } else {
7253     //
7254     UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
7255     UInt_t caltime = 0;
7256     //
7257     myquery.str("");
7258     myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
7259     myquery << " order by FROM_TIME asc limit 1;";
7260     //
7261     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7262     //
7263     //
7264     delete pResult;
7265     pResult = conn->Query(myquery.str().c_str());
7266     //
7267     Row = pResult->Next();
7268     if( !Row ){
7269     caltime = runhtime;
7270     } else {
7271     caltime = (UInt_t)atoll(Row->GetField(0));
7272     };
7273     //
7274     myquery.str("");
7275     myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
7276     myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
7277     //
7278     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7279     //
7280     pResult = conn->Query(myquery.str().c_str());
7281     //
7282     Row = pResult->Next();
7283     if( !Row ){
7284     //
7285     if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
7286     //
7287     } else {
7288     myquery.str("");
7289     myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
7290     myquery << " RUNHEADER_TIME>=" <<runhtime;
7291     myquery << " order by RUNHEADER_TIME asc;";
7292     //
7293     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7294     //
7295     pResult = conn->Query(myquery.str().c_str());
7296     //
7297     Row = pResult->Next();
7298     while ( Row ){
7299     //
7300     unv++;
7301     this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
7302     Row = pResult->Next();
7303     //
7304     };
7305     };
7306     };
7307     if ( IsDebug() ) printf(" %u runs have been unvalidated \n",unv);
7308     };
7309    
7310     /**
7311     *
7312     * Rearrange GL_RUN table and remove runs
7313     *
7314     **/
7315     void PamelaDBOperations::RemoveRUNS(){
7316     TSQLResult *pResult;
7317     TSQLRow *Row;
7318     stringstream myquery;
7319     UInt_t drun = 0;
7320     GL_RUN *delrun = new GL_RUN();
7321     //
7322     myquery.str("");
7323     myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
7324     //
7325     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7326     //
7327     pResult = conn->Query(myquery.str().c_str());
7328     //
7329     Row = pResult->Next();
7330     //
7331     //
7332     if ( !Row ){
7333     if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
7334     } else {
7335     if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");
7336     while ( Row ){
7337     delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
7338     if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
7339     drun++;
7340     Row = pResult->Next();
7341     };
7342     };
7343     //
7344     //
7345     myquery.str("");
7346     myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
7347     //
7348     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7349     //
7350     pResult = conn->Query(myquery.str().c_str());
7351     //
7352     Row = pResult->Next();
7353     //
7354     if ( !Row ){
7355     if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
7356     } else {
7357     if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");
7358     while ( Row ){
7359     if ( IsDebug() ) printf(" restore run %u \n",(UInt_t)atoll(Row->GetField(1)));
7360     delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
7361     if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(1)));
7362     delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
7363     if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
7364     if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
7365     delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
7366     };
7367     drun++;
7368     Row = pResult->Next();
7369     };
7370     };
7371     //
7372     if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);
7373     //
7374     //
7375     //
7376     drun = 0;
7377     //
7378     myquery.str("");
7379     myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
7380     //
7381     pResult = conn->Query(myquery.str().c_str());
7382     //
7383     Row = pResult->Next();
7384     //
7385     if ( !Row ){
7386     if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
7387     } else {
7388     if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");
7389     while ( Row ){
7390     if ( IsDebug() ) printf(" del run idtrash %u \n",(UInt_t)atoll(Row->GetField(0)));
7391     myquery.str("");
7392     myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";
7393     conn->Query(myquery.str().c_str());
7394     drun++;
7395     Row = pResult->Next();
7396     };
7397     };
7398     //
7399     if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_TRASH table \n",drun);
7400     //
7401     //
7402     //
7403     drun = 0;
7404     //
7405     myquery.str("");
7406     myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
7407     //
7408     pResult = conn->Query(myquery.str().c_str());
7409     //
7410     Row = pResult->Next();
7411     //
7412     if ( !Row ){
7413     if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
7414     } else {
7415     if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");
7416     while ( Row ){
7417     if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
7418     myquery.str("");
7419     myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
7420     conn->Query(myquery.str().c_str());
7421     drun++;
7422     Row = pResult->Next();
7423     };
7424     };
7425     //
7426     if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_FRAGMENTS table \n",drun);
7427     //
7428     //
7429     //
7430     delete delrun;
7431     //
7432     };
7433    
7434    
7435     /**
7436     *
7437     * Rearrange calibration tables
7438     *
7439     **/
7440 mocchiut 1.10 void PamelaDBOperations::RemoveFILES(UInt_t idtsy){
7441 mocchiut 1.1 stringstream myquery;
7442     //
7443     myquery.str("");
7444     myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
7445     //
7446     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7447     //
7448     conn->Query(myquery.str().c_str());
7449     //
7450 mocchiut 1.10 myquery.str("");
7451     myquery << " DELETE FROM GL_ROOT WHERE ID=" <<this->GetID_ROOT() <<";";
7452     //
7453     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7454     //
7455     conn->Query(myquery.str().c_str()); //
7456     //
7457     if ( !chewbacca ){
7458     myquery.str("");
7459     myquery << " DELETE FROM GL_TIMESYNC WHERE ID=" << idtsy <<";";
7460     //
7461     if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7462     //
7463     conn->Query(myquery.str().c_str());
7464     };
7465     //
7466 mocchiut 1.1 };
7467    
7468     /**
7469     *
7470     * Rearrange calibration tables
7471     *
7472     **/
7473     void PamelaDBOperations::RemoveCALIBS(){
7474     TSQLResult *pResult;
7475     TSQLRow *Row;
7476     stringstream myquery;
7477     //
7478     //
7479     // Calorimeter
7480     //
7481     for (Int_t section = 0; section < 4; section++){
7482     myquery.str("");
7483     myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
7484     myquery << " SECTION=" << section << ";";
7485     //
7486     pResult = conn->Query(myquery.str().c_str());
7487     //
7488     Row = pResult->Next();
7489     if( !Row->GetField(0) || !Row->GetField(1) ){
7490     //
7491     if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
7492     //
7493     } else {
7494     //
7495     myquery.str("");
7496     myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
7497     myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
7498     myquery << " SECTION=" << section << ";";
7499     //
7500     pResult = conn->Query(myquery.str().c_str());
7501     //
7502     if( !pResult ){
7503     //
7504     if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
7505     //
7506     throw -4;
7507     //
7508     };
7509     //
7510     };
7511     };
7512     Bool_t OLDDB = false;
7513     for (Int_t section = 0; section < 4; section++){
7514     myquery.str("");
7515     myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALOPULSE_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
7516     myquery << " SECTION=" << section << ";";
7517     //
7518     pResult = conn->Query(myquery.str().c_str());
7519     //
7520     if ( conn->GetErrorCode() ){
7521     printf(" Section %i : warning, old databse structure no GL_CALOPULSE_CALIB table!\n",section);
7522     OLDDB=true;
7523     } else {
7524     Row = pResult->Next();
7525     if( !Row->GetField(0) || !Row->GetField(1) ){
7526     //
7527     if ( IsDebug() ) printf(" NO PULSE CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
7528     //
7529     } else {
7530     //
7531     myquery.str("");
7532     myquery << " UPDATE GL_CALOPULSE_CALIB SET TO_TIME=" << Row->GetField(1);
7533     myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
7534     myquery << " SECTION=" << section << ";";
7535     //
7536     pResult = conn->Query(myquery.str().c_str());
7537     //
7538     if( !pResult ){
7539     //
7540     if ( IsDebug() ) printf(" ERROR DELETING CALO PULSE CALIBRATIONS \n");
7541     //
7542     throw -4;
7543     //
7544     };
7545     //
7546     };
7547     };
7548     };
7549     myquery.str("");
7550     myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT() << ";";
7551     //
7552     pResult = conn->Query(myquery.str().c_str());
7553     //
7554     if( !pResult ){
7555     //
7556     if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
7557     //
7558     throw -4;
7559     //
7560     };
7561     //
7562     myquery.str("");
7563     myquery << " DELETE FROM GL_CALOPULSE_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT() << ";";
7564     //
7565     pResult = conn->Query(myquery.str().c_str());
7566     if ( IsDebug() ) printf(" Delete from GL_CALOPULSE_CALIB query is %s \n",myquery.str().c_str());
7567     if ( !OLDDB ){
7568     //
7569     if( !pResult ){
7570     //
7571     if ( IsDebug() ) printf(" ERROR DELETING PULSE CALO CALIBRATIONS \n");
7572     //
7573     throw -4;
7574     //
7575     };
7576     };
7577     //
7578     // Tracker
7579     //
7580     myquery.str("");
7581     myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
7582     //
7583     pResult = conn->Query(myquery.str().c_str());
7584     //
7585     Row = pResult->Next();
7586     if( !Row->GetField(0) || !Row->GetField(1) ){
7587     //
7588     if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
7589     //
7590     } else {
7591     //
7592     myquery.str("");
7593     myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
7594     myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
7595     //
7596     pResult = conn->Query(myquery.str().c_str());
7597     //
7598     if( !pResult ){
7599     //
7600     if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
7601     //
7602     throw -4;
7603     //
7604     };
7605     //
7606     myquery.str("");
7607     myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT() << ";";
7608     //
7609     pResult = conn->Query(myquery.str().c_str());
7610     //
7611     if( !pResult ){
7612     //
7613     if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
7614     //
7615     throw -4;
7616     //
7617     };
7618     };
7619     //
7620     //
7621     // S4
7622     //
7623     myquery.str("");
7624     myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
7625     //
7626     pResult = conn->Query(myquery.str().c_str());
7627     //
7628     Row = pResult->Next();
7629     if( !Row->GetField(0) || !Row->GetField(1) ){
7630     //
7631     if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
7632     //
7633     } else {
7634     //
7635     myquery.str("");
7636     myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
7637     myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
7638     //
7639     pResult = conn->Query(myquery.str().c_str());
7640     //
7641     if( !pResult ){
7642     //
7643     if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
7644     //
7645     throw -4;
7646     //
7647     };
7648     //
7649     myquery.str("");
7650     myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT() << ";";
7651     //
7652     pResult = conn->Query(myquery.str().c_str());
7653     //
7654     if( !pResult ){
7655     //
7656     if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
7657     //
7658     throw -4;
7659     //
7660     };
7661     //
7662     };
7663     };
7664    
7665     /**
7666     *
7667     * Rearrange calibration tables
7668     *
7669     **/
7670 pam-fi 1.4 UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh , TFile *file){
7671    
7672     if(!caltrk) return 0;
7673    
7674     if ( IsDebug() ) cout << "ValidateTrkCalib:"<<endl;
7675    
7676     UInt_t validate = 1;
7677     Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
7678     UInt_t timeaftercalib=120000; //2000;
7679     TString classname = caltrk->GetName();
7680    
7681     // ----------------------------------
7682     // Check CRCs and failed calibrations
7683     // ----------------------------------
7684     for(Int_t ipkt=0; ipkt<6; ipkt++){
7685     if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
7686     if( caltrk->crc_hcal[ipkt] ){
7687     // if(IsDebug())cout<<"(CRC Header)";
7688     validate = 0;
7689     if(IsDebug())cout <<endl<<" *** CRC *** (header DSPn "<<caltrk->DSPnumber[ipkt]<<")";
7690    
7691     }
7692     for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] ){
7693     // if(IsDebug())cout<<"(CRC Pkt-"<<ilad<<")";
7694     if(IsDebug())cout <<endl<<" *** CRC *** (data DSPn "<<caltrk->DSPnumber[ipkt]<<" ladder "<<ilad<<")";
7695     validate = 0;
7696     }
7697     if( !(caltrk->ncalib_event[ipkt]==0 && caltrk->cal_flag[ipkt]==0) ){
7698     if(IsDebug())cout <<endl<<" *** FAILURE *** (data DSPn "<<caltrk->DSPnumber[ipkt]<<")";
7699     validate = 0;
7700     }
7701     }else{
7702     // validate=0;
7703     if(IsDebug())cout <<endl<<" *** DSPn *** ("<< caltrk->DSPnumber[ipkt] <<" @pkt "<<ipkt<<")";
7704     }
7705     }
7706    
7707     // -----------------------
7708     // Check missing packets:
7709     // -----------------------
7710     // Readout order:
7711     // ------------------
7712     // DSP packet board
7713     // ------------------
7714     // 12 0 1
7715     // 10 1 1
7716     // 8 2 1
7717     // 4 3 1
7718     // 6 4 1
7719     // 2 5 1
7720     // ------------------
7721     // 11 0 2
7722     // 9 1 2
7723     // 7 2 2
7724     // 3 3 2
7725     // 5 4 2
7726     // 1 5 2
7727     // ------------------
7728     // -------------------------------------------------
7729     // Check if it is first or second calibration packet
7730     // -------------------------------------------------
7731     UInt_t build=0;
7732     UInt_t base=0;
7733     UInt_t mask=0;
7734     if(classname.Contains("CalibTrk1Event")){
7735     base=12;
7736     mask=0x03F000;
7737     }
7738     if(classname.Contains("CalibTrk2Event")){
7739     base=18;
7740     mask=0xFC0000;
7741     }
7742     // ----------------------------------------------------
7743     // Count number of valid packets and set build variable
7744     // ----------------------------------------------------
7745     if(IsDebug())cout <<endl<< " DSP: ";
7746     Int_t npkts=0;
7747     for(Int_t ipkt=0; ipkt<6; ipkt++){
7748     if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
7749     if(IsDebug())cout <<" "<<caltrk->DSPnumber[ipkt];
7750     npkts++;
7751     build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
7752     // cout << caltrk->DSPnumber[ipkt]
7753     };
7754     }
7755     if(IsDebug())cout << " ==> "<< hex << build << dec;
7756     // ----------------------------------------------------
7757     // If the number of valid packets is 6, ok exit...
7758     // ----------------------------------------------------
7759     if( npkts==6 ){
7760     return validate; // exit
7761     }
7762     ////////////////////////////////////////////////////////
7763     // ...otherwise there might be some missing packets
7764     //
7765     // In this case check the acq configuration
7766     // (some DSPs might be excluded from acquisition)
7767     ////////////////////////////////////////////////////////
7768    
7769     if(!eh || !file || (file&&file->IsZombie()) ){
7770     if ( IsDebug() )cout << " *** MISSING VIEW *** eh="<<eh<<" file="<<file<<" cannot validate"<<endl;
7771     return (0);
7772     }
7773    
7774     // -----------------------------------------------
7775     // retrieve the first run header after calib
7776     // -----------------------------------------------
7777    
7778     PacketType *pctp;
7779     EventCounter *cod;
7780     cod = eh->GetCounter();
7781     Int_t irun = cod->Get(pctp->RunHeader);
7782     TTree *rh=(TTree*)file->Get("RunHeader");
7783     if ( !rh || rh->IsZombie() ) throw -17;
7784     if( rh->GetEntries() <= irun ){
7785     if ( IsDebug() ) cout << " *** MISSING-PKT *** no runs after calib (1) -- cannot validate :-( "<<endl;
7786     return 0; // :-(
7787     }
7788     RunHeaderEvent *run = 0;
7789     EventHeader *hrun = 0;
7790     rh->SetBranchAddress("RunHeader", &run);
7791     rh->SetBranchAddress("Header", &hrun);
7792     rh->GetEntry(irun);
7793     if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
7794     if ( IsDebug() ) cout << " *** MISSING-PKT *** no runs after calib (2) -- cannot validate :-( "<<endl;
7795     return 0; // :-(
7796     }
7797    
7798     UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
7799     if( dtime > timeaftercalib ){
7800     if ( IsDebug() ) cout << " *** MISSING-PKT *** run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
7801     return 0; // :-(
7802     }
7803    
7804     if ( IsDebug() ) cout <<endl<< " ACQ_BUILD_INFO ==> "<<hex<<(run->ACQ_BUILD_INFO & mask)<<dec;
7805    
7806     if( (run->ACQ_BUILD_INFO & mask) != build ){
7807     validate=0; // :-(
7808     cout <<endl<< " *** MISSING-PKT *** packet mismatch: ACQ_BUILD_INFO="<<hex<<(run->ACQ_BUILD_INFO&mask)<<" != "<<build<<dec;
7809     };
7810    
7811     return validate;
7812 mocchiut 1.1
7813 mocchiut 1.2
7814 pam-fi 1.4 }
7815 mocchiut 1.1
7816     /**
7817     *
7818     * Check the DB (only for overlapping runs at the moment)
7819     *
7820     **/
7821     UInt_t PamelaDBOperations::Check(){
7822 mocchiut 1.2 return(this->Check(0,0));
7823 mocchiut 1.1 }
7824    
7825     UInt_t PamelaDBOperations::Check(UInt_t from, UInt_t to){
7826     //
7827 mocchiut 1.2 if ( IsDebug() ) printf(" from %u to %u \n",from,to);
7828 mocchiut 1.1 //
7829     UInt_t test = 0;
7830     //
7831     UInt_t thisrht = 0;
7832     UInt_t thisrtt = 0;
7833     UInt_t thisid = 0;
7834     UInt_t prevrht = 0;
7835     UInt_t prevrtt = 0;
7836     UInt_t previd = 0;
7837     //
7838     UInt_t prevl0id = 0;
7839     UInt_t thisl0id = 0;
7840     //
7841     stringstream oss;
7842     TSQLResult *result = 0;
7843     TSQLRow *row = 0;
7844     TSQLResult *result2 = 0;
7845     TSQLRow *row2 = 0;
7846     TSQLResult *result3 = 0;
7847     TSQLRow *row3 = 0;
7848     oss.str("");
7849     oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME,NEVENTS FROM GL_RUN order by RUNHEADER_TIME asc;";
7850     // oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
7851     result = conn->Query(oss.str().c_str());
7852     //
7853     if ( !result ) throw -4;;
7854     //
7855     row = result->Next();
7856     UInt_t nid = 0;
7857     //
7858     while ( row ){
7859     nid++;
7860     if ( !(nid%1000) && nid ) printf(" %iK run scanned \n",nid/1000);
7861     thisid = (UInt_t)atoll(row->GetField(0));
7862     thisl0id = (UInt_t)atoll(row->GetField(1));
7863     thisrht = (UInt_t)atoll(row->GetField(2));
7864     thisrtt = (UInt_t)atoll(row->GetField(3));
7865     //
7866     if ( from > 0 && nid <= from ) goto ss;
7867     if ( to > 0 && nid >= to ) goto ss;
7868 mocchiut 1.2 //
7869 mocchiut 1.1 if ( (UInt_t)atoll(row->GetField(4)) > 1 ){
7870     //
7871     //
7872     //
7873     oss.str("");
7874     oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
7875     << thisid << " AND ( RUNHEADER_TIME="
7876     << thisrht << " OR RUNTRAILER_TIME="
7877     << thisrtt << " ) AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
7878     result3 = conn->Query(oss.str().c_str());
7879     if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
7880     if ( result3 ){
7881     //
7882     oss.str("");
7883     oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
7884     << thisid << " AND RUNHEADER_TIME="
7885     << thisrht << " AND RUNTRAILER_TIME!="
7886     << thisrtt << " AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
7887     result3 = conn->Query(oss.str().c_str());
7888     if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
7889     if ( result3 ){
7890     row3 = result3->Next();
7891     //
7892     while ( row3 ){
7893     //
7894     // 2 runs with same runheader
7895     //
7896     printf(" CHECK n.4 RUNs %u and %u HAVE SAME RUNHEADER \n",thisid,(UInt_t)atoll(row3->GetField(0)));
7897     row3 = result3->Next();
7898     };
7899 mocchiut 1.2 // delete result3;
7900 mocchiut 1.1
7901     };
7902     //
7903     oss.str("");
7904     oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
7905     << thisid << " AND RUNHEADER_TIME!="
7906     << thisrht << " AND RUNTRAILER_TIME="
7907     << thisrtt << " AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
7908     result3 = conn->Query(oss.str().c_str());
7909     if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
7910     if ( result3 ){
7911     row3 = result3->Next();
7912     //
7913     while ( row3 ){
7914     //
7915     // 2 runs with same runtrailer
7916     //
7917     printf(" CHECK n.5 RUNs %u and %u HAVE SAME RUNTRAILER \n",thisid,(UInt_t)atoll(row3->GetField(0)));
7918     row3 = result3->Next();
7919     };
7920 mocchiut 1.2 // delete result3;
7921 mocchiut 1.1 };
7922     //
7923     oss.str("");
7924     oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
7925     << thisid << " AND RUNHEADER_TIME="
7926     << thisrht << " AND RUNTRAILER_TIME="
7927     << thisrtt << " AND ID_RUN_FRAG!="
7928     << thisid << " order by RUNHEADER_TIME asc;";
7929     result3 = conn->Query(oss.str().c_str());
7930     if ( result3 ){
7931     row3 = result3->Next();
7932     //
7933     while ( row3 ){
7934     //
7935     // duplicated run
7936     //
7937     printf(" CHECK n.7 RUNs %u and %u HAVE SAME RUNTRAILER AND RUNHEADER (ARE THE SAME?) \n",thisid,(UInt_t)atoll(row3->GetField(0)));
7938     row3 = result3->Next();
7939     };
7940 mocchiut 1.2 // delete result3;
7941 mocchiut 1.1
7942     };
7943     };
7944     //
7945     oss.str("");
7946     oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
7947     << thisid << " AND RUNHEADER_TIME>"
7948     << thisrht << " AND RUNTRAILER_TIME<"
7949     << thisrtt << " order by RUNHEADER_TIME asc;";
7950     result3 = conn->Query(oss.str().c_str());
7951     if ( result3 ){
7952     row3 = result3->Next();
7953     //
7954     while ( row3 ){
7955     //
7956     // run contained in the checked one
7957     //
7958     printf(" CHECK n.6 RUN %u CONTAINS RUN %u \n",thisid,(UInt_t)atoll(row3->GetField(0)));
7959     row3 = result3->Next();
7960     };
7961 mocchiut 1.2 // delete result3;
7962 mocchiut 1.1 };
7963     //
7964     };
7965     //
7966     // if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
7967     // if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
7968     if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
7969     if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
7970     printf(" CHECK n.1 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
7971     TString prevf = "";
7972     TString thisf = "";
7973     oss.str("");
7974     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
7975     result2 = conn->Query(oss.str().c_str());
7976     if ( !result2 ) throw -4;;
7977     row2 = result2->Next();
7978     prevf = (TString)row2->GetField(0);
7979     oss.str("");
7980     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
7981     result2 = conn->Query(oss.str().c_str());
7982     if ( !result2 ) throw -4;;
7983     row2 = result2->Next();
7984     thisf = (TString)row2->GetField(0);
7985     if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
7986     test = 1;
7987 mocchiut 1.2 // delete result2;
7988 mocchiut 1.1 };
7989     //
7990     if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
7991     if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
7992     printf(" CHECK n.2 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
7993     TString prevf = "";
7994     TString thisf = "";
7995     oss.str("");
7996     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
7997     result2 = conn->Query(oss.str().c_str());
7998     if ( !result2 ) throw -4;
7999     row2 = result2->Next();
8000     prevf = (TString)row2->GetField(0);
8001     oss.str("");
8002     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
8003     result2 = conn->Query(oss.str().c_str());
8004     if ( !result2 ) throw -4;;
8005     row2 = result2->Next();
8006     thisf = (TString)row2->GetField(0);
8007     if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
8008     test = 1;
8009 mocchiut 1.2 // delete result2;
8010 mocchiut 1.1 };
8011     //
8012     if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
8013     if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
8014     printf(" CHECK n.3 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
8015     TString prevf = "";
8016     TString thisf = "";
8017     oss.str("");
8018     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
8019     result2 = conn->Query(oss.str().c_str());
8020     if ( !result2 ) throw -4;;
8021     row2 = result2->Next();
8022     prevf = (TString)row2->GetField(0);
8023     oss.str("");
8024     oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
8025     result2 = conn->Query(oss.str().c_str());
8026     if ( !result2 ) throw -4;;
8027     row2 = result2->Next();
8028     thisf = (TString)row2->GetField(0);
8029     if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
8030     test = 1;
8031 mocchiut 1.2 // delete result2;
8032 mocchiut 1.1 };
8033 mocchiut 1.2 ss:
8034 mocchiut 1.1 //
8035     prevrht = thisrht;
8036     prevrtt = thisrtt;
8037     previd = thisid;
8038     prevl0id = thisl0id;
8039     row = result->Next();
8040 mocchiut 1.2 // if ( result2 ) delete result2;
8041     // if ( result3 ) delete result3;
8042 mocchiut 1.1 };
8043     //
8044     return(test);
8045     //
8046     };

  ViewVC Help
Powered by ViewVC 1.1.23