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

Annotation of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.21 - (hide annotations) (download)
Mon Nov 27 14:25:35 2006 UTC (18 years ago) by mocchiut
Branch: MAIN
Changes since 1.20: +132 -63 lines
Error -13 bug fixed, added autoboot option, changed private PKT and OBT methods

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

  ViewVC Help
Powered by ViewVC 1.1.23