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

Diff of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.9 by mocchiut, Tue Sep 12 07:28:46 2006 UTC revision 1.37 by mocchiut, Mon Apr 30 10:14:42 2007 UTC
# Line 14  Line 14 
14  #include <TSQLRow.h>  #include <TSQLRow.h>
15  #include <TTree.h>  #include <TTree.h>
16  #include <TGraph.h>  #include <TGraph.h>
17  #include <TDatime.h>  #include <TTimeStamp.h>
18  #include <TF1.h>  #include <TF1.h>
19  //  //
20  #include <EventHeader.h>  #include <EventHeader.h>
# Line 31  Line 31 
31  #include <varDump/VarDumpRecord.h>  #include <varDump/VarDumpRecord.h>
32  #include <physics/S4/S4Event.h>  #include <physics/S4/S4Event.h>
33  //  //
34    #include <sgp4.h>
35    
36  #include <PamelaDBOperations.h>  #include <PamelaDBOperations.h>
37  //  //
38  using namespace std;  using namespace std;
39  using namespace pamela;  using namespace pamela;
40    
41    // Some function to work with cTle stuff.
42    bool compTLE(cTle* tle1, cTle *tle2);
43    float getTleJulian(cTle *);
44    string getTleDatetime(cTle*);
45    
46  /**  /**
47   * Constructor.   * Constructor.
48   * @param host         hostname for the SQL connection.   * @param host         hostname for the SQL connection.
# Line 47  using namespace pamela; Line 54  using namespace pamela;
54   * @param obt0         file obt0.   * @param obt0         file obt0.
55   * @param tsync        file timesync.   * @param tsync        file timesync.
56   * @param debug        debug flag.   * @param debug        debug flag.
57     * @param tlefilename  ascii file with TLE 3 line elements.
58   */   */
59  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug){  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename, UInt_t dwinput, Bool_t staticp){
60    //    //
61      STATIC=false;
62      if ( staticp ) STATIC=true;
63    //    //
64    SetConnection(host,user,password);    SetConnection(host,user,password);
65    //    //
# Line 62  PamelaDBOperations::PamelaDBOperations(T Line 72  PamelaDBOperations::PamelaDBOperations(T
72    SetTsync(tsync);    SetTsync(tsync);
73    SetObt0(obt0);    SetObt0(obt0);
74    //    //
75      SetTLEPath(tlefilename);
76      //
77    //    //
78    INSERT_RAW =!filerawname.IsNull();    INSERT_RAW =!filerawname.IsNull();
79    if(INSERT_RAW)SetRawName(filerawname);    if(INSERT_RAW)SetRawName(filerawname);
80    //    //
81    INSERT_ROOT = !filerootname.IsNull();    INSERT_ROOT = !filerootname.IsNull();
82    if( INSERT_ROOT ){    if( INSERT_ROOT ){
83        this->SetRootName(filerootname);
84        this->SetOrbitNo(dwinput);
85      file = TFile::Open(this->GetRootName().Data());      file = TFile::Open(this->GetRootName().Data());
86      //    if ( !file ) INSERT_ROOT = false;    } else {
87        this->SetRootName("");
88    };    };
   //  this->OpenFile();  
89    //    //
90    this->SetID_RAW(0);    this->SetID_RAW(0);
91    this->SetID_ROOT(0);    this->SetID_ROOT(0);
# Line 100  void PamelaDBOperations::Close(){ Line 114  void PamelaDBOperations::Close(){
114  //  //
115  void PamelaDBOperations::CheckValidate(Long64_t olderthan){  void PamelaDBOperations::CheckValidate(Long64_t olderthan){
116    clean_time = new TDatime();    clean_time = new TDatime();
117      //
118    if(olderthan >= 0){    if(olderthan >= 0){
119      VALIDATE = true;      VALIDATE = true;
120      UInt_t timelim = 0;      UInt_t timelim = 0;
121      timelim =  (UInt_t)clean_time->Convert() - olderthan;      timelim =  (UInt_t)clean_time->Convert(true) - olderthan;
122      clean_time->Set(timelim,false);      clean_time->Set(timelim,false);
123    };    };
124  };  };
# Line 115  void PamelaDBOperations::CheckValidate(L Line 130  void PamelaDBOperations::CheckValidate(L
130   * @param password     password for the SQL connection.   * @param password     password for the SQL connection.
131   */   */
132  void PamelaDBOperations::SetConnection(TString host, TString user, TString password){  void PamelaDBOperations::SetConnection(TString host, TString user, TString password){
133      if ( IsDebug() ) printf(" Connecting using host = %s user = %s password = %s \n",host.Data(),user.Data(),password.Data());
134    conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());    conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());
135  };  };
136    
# Line 143  void PamelaDBOperations::SetDebugFlag(Bo Line 159  void PamelaDBOperations::SetDebugFlag(Bo
159  };  };
160    
161  /**  /**
162     * Set the autoboot flag
163     *
164     */
165    void PamelaDBOperations::SetAutoBoot(Bool_t dbg){
166      AUTOBOOT = dbg;
167    };
168    
169    /**
170     * Set the nofrag flag
171     *
172     */
173    void PamelaDBOperations::SetNoFrag(Bool_t nf){
174      NOFRAG = nf;
175    };
176    
177    /**
178   * Store the BOOT number of the RAW file.   * Store the BOOT number of the RAW file.
179   * @param boot    BOOT number of the RAW file   * @param boot    BOOT number of the RAW file
180   */   */
# Line 183  void PamelaDBOperations::SetRootName(TSt Line 215  void PamelaDBOperations::SetRootName(TSt
215  };  };
216    
217  /**  /**
218     * Store the downlink orbit number from filename.
219     */
220    void PamelaDBOperations::SetOrbitNo(UInt_t dwinput){
221      dworbit = 0;
222      //
223      if ( dwinput ){
224        dworbit = dwinput;
225        if ( IsDebug() ) printf(" Downlink orbit given by hand: %i  \n",dworbit);
226        return;
227      };
228      //
229      TString name = this->GetRootFile();
230      Int_t nlength = name.Length();
231      if ( nlength < 5 ) return;
232      TString dwo = 0;
233      for (Int_t i = 0; i<5; i++){
234        dwo.Append(name[i],1);
235      };
236      if ( dwo.IsDigit() ){
237        dworbit = (UInt_t)dwo.Atoi();
238      } else {
239        dwo="";
240        for (Int_t i = 8; i<13; i++){
241          dwo.Append(name[i],1);
242        };    
243        if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
244      };
245      if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
246      return;
247    };
248    
249    
250    
251    /**
252   * Store the NOBOOT flag.   * Store the NOBOOT flag.
253   * @param noboot    true/false.   * @param noboot    true/false.
254   */   */
# Line 191  void PamelaDBOperations::SetNOBOOT(Bool_ Line 257  void PamelaDBOperations::SetNOBOOT(Bool_
257  };  };
258    
259  /**  /**
260     * Store path to the TLE file.
261     */
262    void PamelaDBOperations::SetTLEPath(TString str){
263      tlefilename = str;
264    };
265    
266    TString PamelaDBOperations::GetRawPath(){
267      if ( STATIC ){
268        return((TString)gSystem->DirName(filerawname.Data())+'/');
269      } else {
270        return((TString)gSystem->ExpandPathName("$PAM_RAW")+'/');
271      };
272    };
273    
274    TString PamelaDBOperations::GetRootPath(){
275      if ( STATIC ){
276        return((TString)gSystem->DirName(filerootname.Data())+'/');
277      } else {
278        return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');
279      };
280    };
281    
282    /**
283   * Store the olderthan variable   * Store the olderthan variable
284   * @param olderthan   * @param olderthan
285   */   */
# Line 206  Bool_t PamelaDBOperations::SetID_RAW(){ Line 295  Bool_t PamelaDBOperations::SetID_RAW(){
295    TSQLResult *result = 0;    TSQLResult *result = 0;
296    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
297    oss.str("");    oss.str("");
298    oss << "SELECT ID FROM GL_RAW WHERE "    if ( STATIC ){
299        << " PATH = '" << this->GetRawPath().Data() << "' AND "      oss << "SELECT ID FROM GL_RAW WHERE "
300        << " NAME = '" << this->GetRawFile().Data() << "' ";          << " PATH = '" << this->GetRawPath().Data() << "' AND "
301            << " NAME = '" << this->GetRawFile().Data() << "' ";  
302      } else {
303        oss << "SELECT ID FROM GL_RAW WHERE "
304            << " PATH = '$PAM_RAW' AND "
305            << " NAME = '" << this->GetRawFile().Data() << "' ";  
306      }
307    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
308    if ( result == NULL ) throw -4;    if ( result == NULL ) throw -4;
309    row = result->Next();    row = result->Next();
# Line 239  Int_t PamelaDBOperations::SetUpperLimits Line 334  Int_t PamelaDBOperations::SetUpperLimits
334    UInt_t nevent = 0;    UInt_t nevent = 0;
335    UInt_t pktlast = 0;    UInt_t pktlast = 0;
336    UInt_t obtlast = 0;    UInt_t obtlast = 0;
337    UInt_t t_pktlast = 0;    Long64_t t_pktlast = 0LL;
338    UInt_t t_obtlast = 0;    //  UInt_t t_obtlast = 0;
339    UInt_t upperpkt2 = 0;    Long64_t t_obtlast = 0LL;
340    ULong64_t upperobt2 = 0;    Long64_t upperpkt2 = 0LL;
341      Long64_t upperobt2 = 0LL;
342    UInt_t zomp = 0;    UInt_t zomp = 0;
343    UInt_t jump = 50000; // was 5000    UInt_t jump = 50000; // was 5000
344    EventCounter *code=0;    EventCounter *code=0;
345    //    //
346    //   pcksList packetsNames;    Long64_t deltapkt = 5000LL;
347    //   pcksList::iterator Iter;    Long64_t deltaobt = 50000LL;
348    //   getPacketsNames(packetsNames);    //
349      pcksList packetsNames;
350      pcksList::iterator Iter;
351      getPacketsNames(packetsNames);
352    //    //
353    pktfirst = 0;    pktfirst = 0;
354    obtfirst = 0;    obtfirst = 0;
355      ppktfirst = 0;
356      pobtfirst = 0;
357    //    //
358    TTree *T = 0;    TTree *T = 0;
359    T = (TTree*)file->Get("Physics");    T = (TTree*)file->Get("Physics");
# Line 266  Int_t PamelaDBOperations::SetUpperLimits Line 367  Int_t PamelaDBOperations::SetUpperLimits
367    ph = eh->GetPscuHeader();    ph = eh->GetPscuHeader();
368    pktfirst = ph->GetCounter();    pktfirst = ph->GetCounter();
369    obtfirst = ph->GetOrbitalTime();      obtfirst = ph->GetOrbitalTime();  
370      ppktfirst = pktfirst;
371      pobtfirst = obtfirst;
372    //    //
373    //   code = eh->GetCounter();    code = eh->GetCounter();
374    //   UInt_t en = 0;    UInt_t en = 0;
375    //   for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){    for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
376    //     en = code->Get(GetPacketType(*Iter));      en = code->Get(GetPacketType(*Iter));
377    //     if ( en ) printf(" Packet type is %s, entries: %i \n",*Iter,en);      if ( !strcmp("CalibCalPed",*Iter) || !strcmp("CalibTrk1",*Iter) || !strcmp("CalibTrk2",*Iter) || !strcmp("CalibS4",*Iter) ){
378    //};          if ( IsDebug() ) printf(" We have a calibration before the first physic packet: packet type is %s, entries: %i \n",*Iter,en);
379          //
380          TTree *TC = 0;
381          TC = (TTree*)file->Get("CalibCalPed");
382          if ( !TC || TC->IsZombie() ) throw -16;
383          EventHeader *ehc = 0;
384          PscuHeader *phc = 0;
385          TC->SetBranchAddress("Header", &ehc);
386          TC->GetEntry(0);
387          phc = ehc->GetPscuHeader();
388          pktfirst = phc->GetCounter();
389          obtfirst = phc->GetOrbitalTime();  
390          //
391        };
392      };  
393    //    //
394    T->GetEntry(nevent-1);    T->GetEntry(nevent-1);
395    ph = eh->GetPscuHeader();    ph = eh->GetPscuHeader();
# Line 283  Int_t PamelaDBOperations::SetUpperLimits Line 400  Int_t PamelaDBOperations::SetUpperLimits
400    upperobt = OBT(obtlast);    upperobt = OBT(obtlast);
401    upperentry = nevent-1;    upperentry = nevent-1;
402    //    //
403    if ( IsDebug() ) printf(" First entries are: OBT %llu pkt_num %i \n",obtfirst,pktfirst);    if ( IsDebug() ) printf(" First entries are: OBT %u pkt_num %u \n",obtfirst,pktfirst);
404    //    //
405    if ( IsDebug() ) printf(" Last entries are: OBT %llu pkt_num %i entry %i\n",upperobt,upperpkt,upperentry);    if ( IsDebug() ) printf(" Last entries are: OBT %lld pkt_num %lld entry %i\n",upperobt,upperpkt,upperentry);
406    //    //
407    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ) return(1);    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ) return(32);
408    //    //
409    if ( !nevent ) return(2);    if ( !nevent ) return(64);
410    //    //
411    if ( nevent < 2 ) return(4);    if ( nevent < 2 ) return(128);
412      if ( nevent < jump ) jump = 1;
413      //  if ( nevent < jump ) jump = int(nevent/10);
414      //  if ( !jump ) jump = 1;
415    //    //
416    if ( PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst) ){    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) && nevent > deltapkt ){
417        //
418        if ( IsDebug() ) printf(" starting jump %i \n",jump);
419      // go back      // go back
420      zomp = nevent - 2;      zomp = nevent - 2;
421      //      //
# Line 315  Int_t PamelaDBOperations::SetUpperLimits Line 437  Int_t PamelaDBOperations::SetUpperLimits
437          upperpkt2 = PKT(ph->GetCounter());          upperpkt2 = PKT(ph->GetCounter());
438          upperobt2 = OBT(ph->GetOrbitalTime());          upperobt2 = OBT(ph->GetOrbitalTime());
439          //          //
440          if ( (t_pktlast < upperpkt && t_obtlast > upperobt) || (t_pktlast < upperpkt2 && t_obtlast > upperobt2) ) throw -13;              if ( (t_pktlast < upperpkt && t_obtlast > upperobt) || (t_pktlast < upperpkt2 && t_obtlast > upperobt2) ){
441              if ( IsDebug() ) printf(" .-. upperpkt2 %lld upperobt2 %lld \n",upperpkt2,upperobt2);  
442              if ( IsDebug() ) printf(" .-. upperpkt %lld t_pktlast %lld upperobt %lld t_obtlast %lld \n",upperpkt,t_pktlast,upperobt,t_obtlast);    
443              if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %lld pktlast %u upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);    
444              throw -13;    
445            };
446          //          //
447          if ( t_pktlast < upperpkt && t_obtlast < upperobt && t_pktlast < upperpkt2 && t_obtlast < upperobt2 ){          if ( t_pktlast < upperpkt && t_obtlast < upperobt && t_pktlast < upperpkt2 && t_obtlast < upperobt2 ){
448            zomp = i + jump + 1;            zomp = i + jump + 1;
449            if ( zomp > nevent-2 ) zomp = nevent - 2;            if ( zomp > nevent-2 ) zomp = nevent - 2;
450            if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %i pktlast %i upperobt %llu obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);            if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %lld pktlast %u upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);
451            break;            break;
452          };          };
453          //          //
# Line 355  Int_t PamelaDBOperations::SetUpperLimits Line 482  Int_t PamelaDBOperations::SetUpperLimits
482    //    //
483    rhev = rh->GetEntries();    rhev = rh->GetEntries();
484    rtev = rt->GetEntries();    rtev = rt->GetEntries();
485    UInt_t sobtt = 0;    Long64_t sobtt = 0LL;
486    UInt_t sobth = 0;    Long64_t sobth = 0LL;
487    UInt_t spktt = 0;    Long64_t spktt = 0LL;
488    UInt_t spkth = 0;    Long64_t spkth = 0LL;
489    UInt_t pktt = 0;    Long64_t pktt = 0LL;
490    ULong64_t obtt = 0;    Long64_t obtt = 0LL;
491    UInt_t pkth = 0;    Long64_t pkth = 0LL;
492    ULong64_t obth = 0;    Long64_t obth = 0LL;
   //  
   T->GetEntry(upperentry);  
   code = eh->GetCounter();  
   Int_t lasttrail = code->Get(pctp->RunTrailer);  
   Int_t lasthead = code->Get(pctp->RunHeader);  
   if ( lasttrail < rtev ){  
     rt->GetEntry(lasttrail);  
     pht = eht->GetPscuHeader();  
     pktt = PKT(pht->GetCounter());  
     obtt = OBT(pht->GetOrbitalTime());  
   };  
   //  
   if ( lasthead < rhev ){  
     rh->GetEntry(lasthead);  
     phh = ehh->GetPscuHeader();  
     pkth = PKT(phh->GetCounter());  
     obth = OBT(phh->GetOrbitalTime());  
   };  
   //  
   if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);  
   if ( pkth > upperpkt && obth > upperobt ){  
     if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);  
     upperpkt = pkth;  
     upperobt = obth;  
     rhev = lasthead+1;  
   } else {  
     rhev = lasthead;  
   };  
   if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);  
   //  
   if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);  
   if ( pktt > upperpkt && obtt > upperobt ){  
     if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);  
     upperpkt = pktt;  
     upperobt = obtt;  
     rtev = lasttrail+1;  
   } else {  
     rtev = lasttrail;  
   };  
   if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);  
   //  goto kikko;  
   //  
   //  
   // Check if runtrailer/runheader are within lower limits  
   //  
493    //    //
494    pkth = 0;    if ( rhev || rtev ){
495    obth = 0;  
496    spkth = 0;      T->GetEntry(upperentry);
497    sobth = 0;      code = eh->GetCounter();
498    for (Int_t k=0; k<rhev; k++){      Int_t lasttrail = code->Get(pctp->RunTrailer);
499      if ( k > 0 ){      Int_t lasthead = code->Get(pctp->RunHeader);
500        spkth = pkth;      if ( lasttrail < rtev ){
501        sobth = obth;        rt->GetEntry(lasttrail);
502      };        pht = eht->GetPscuHeader();
503      rh->GetEntry(k);        pktt = PKT(pht->GetCounter());
504      phh = ehh->GetPscuHeader();        obtt = OBT(pht->GetOrbitalTime());
505      pkth = PKT(phh->GetCounter());      };
506      obth = OBT(phh->GetOrbitalTime());      //
507      //      if ( lasthead < rhev ){
508  //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);        rh->GetEntry(lasthead);
509      //        phh = ehh->GetPscuHeader();
510      if ( pkth < spkth && obth < sobth ){        pkth = PKT(phh->GetCounter());
511        if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);        obth = OBT(phh->GetOrbitalTime());
512        //      };
513        rhev = k-1;      //
514        rh->GetEntry(rhev);      if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
515        pkth = spkth;      if ( pkth > upperpkt && obth > upperobt ){
516        obth = sobth;        if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
517        //        upperpkt = pkth;
518        UInt_t evbefh = 0;        upperobt = obth;
519        code = ehh->GetCounter();        rhev = lasthead+1;
520        evbefh = code->Get(pctp->Physics);          } else {
521        if ( evbefh >= 0 ){        rhev = lasthead;
522          T->GetEntry(evbefh);      };
523          ph = eh->GetPscuHeader();      if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
524          t_pktlast = PKT(ph->GetCounter());      //
525          t_obtlast = OBT(ph->GetOrbitalTime());      if ( IsDebug() ) printf(" rtev beforev %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
526          if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump      if ( pktt > upperpkt && obtt > upperobt ){
527            upperpkt = pkth;        if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
528            upperobt = obth;        upperpkt = pktt;
529            upperentry = evbefh-1;        upperobt = obtt;
530          } else {        rtev = lasttrail+1;
531            while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){      } else {
532              evbefh++;          rtev = lasttrail;
533              T->GetEntry(evbefh);      };
534              ph = eh->GetPscuHeader();      if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
535              t_pktlast = PKT(ph->GetCounter());      //  goto kikko;
536              t_obtlast = OBT(ph->GetOrbitalTime());      //
537            };      //
538            T->GetEntry(evbefh-1);      // Check if runtrailer/runheader are within lower limits
539        //
540        //
541        pkth = 0LL;
542        obth = 0LL;
543        spkth = 0LL;
544        sobth = 0LL;
545        for (Int_t k=0; k<rhev; k++){
546          if ( k > 0 ){
547            spkth = pkth;
548            sobth = obth;
549          };
550          rh->GetEntry(k);
551          phh = ehh->GetPscuHeader();
552          pkth = PKT(phh->GetCounter());
553          obth = OBT(phh->GetOrbitalTime());
554          //
555          //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
556          //
557          if ( pkth < spkth && obth < sobth ){
558            if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);
559            //
560            rhev = k-1;
561            rh->GetEntry(rhev);
562            pkth = spkth;
563            obth = sobth;
564            //
565            UInt_t evbefh = 0;
566            code = ehh->GetCounter();
567            evbefh = code->Get(pctp->Physics);    
568            if ( evbefh >= 0 ){
569              T->GetEntry(evbefh);
570            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
571            upperpkt = PKT(ph->GetCounter());            t_pktlast = PKT(ph->GetCounter());
572            upperobt = OBT(ph->GetOrbitalTime());            t_obtlast = OBT(ph->GetOrbitalTime());
573            upperentry = evbefh-1;            if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
574          };                  upperpkt = pkth;
575                upperobt = obth;
576                upperentry = evbefh-1;
577              } else {
578                while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){
579                  evbefh++;
580                  T->GetEntry(evbefh);
581                  ph = eh->GetPscuHeader();
582                  t_pktlast = PKT(ph->GetCounter());
583                  t_obtlast = OBT(ph->GetOrbitalTime());
584                };
585                T->GetEntry(evbefh-1);
586                ph = eh->GetPscuHeader();
587                upperpkt = PKT(ph->GetCounter());
588                upperobt = OBT(ph->GetOrbitalTime());
589                upperentry = evbefh-1;
590              };    
591            };
592            if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
593            goto kikko0;
594        };        };
       if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);  
       goto kikko0;  
     };  
   };  
  kikko0:  
   //  
   //  
   //  
   pktt = 0;  
   obtt = 0;  
   spktt = 0;  
   sobtt = 0;  
   for (Int_t k=0; k<rtev; k++){  
     if ( k > 0 ){  
       spktt = pktt;  
       sobtt = obtt;  
595      };      };
596      rt->GetEntry(k);    kikko0:
     pht = eht->GetPscuHeader();  
     pktt = PKT(pht->GetCounter());  
     obtt = OBT(pht->GetOrbitalTime());  
597      //      //
 //    if ( IsDebug() ) printf(" k %i rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);  
598      //      //
599      if ( pktt < spktt && obtt < sobtt ){      //
600        if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);      pktt = 0LL;
601        obtt = 0LL;
602        spktt = 0LL;
603        sobtt = 0LL;
604        for (Int_t k=0; k<rtev; k++){
605          if ( k > 0 ){
606            spktt = pktt;
607            sobtt = obtt;
608          };
609          rt->GetEntry(k);
610          pht = eht->GetPscuHeader();
611          pktt = PKT(pht->GetCounter());
612          obtt = OBT(pht->GetOrbitalTime());
613        //        //
614        rtev = k-1;        //    if ( IsDebug() ) printf(" k %i rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
       rt->GetEntry(rtev);  
       pktt = spktt;  
       obtt = sobtt;  
       if ( IsDebug() ) printf(" lasttrail %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);  
615        //        //
616        UInt_t evbeft = 0;        if ( pktt < spktt && obtt < sobtt ){
617        code = eht->GetCounter();          if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
618        evbeft = code->Get(pctp->Physics);              //
619        if ( evbeft >= 0 ){          rtev = k-1;
620          T->GetEntry(evbeft);          rt->GetEntry(rtev);
621          ph = eh->GetPscuHeader();          pktt = spktt;
622          t_pktlast = PKT(ph->GetCounter());          obtt = sobtt;
623          t_obtlast = OBT(ph->GetOrbitalTime());          if ( IsDebug() ) printf(" lasttrail %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
624          if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump          //
625            upperpkt = pktt;          UInt_t evbeft = 0;
626            upperobt = obtt;          code = eht->GetCounter();
627            upperentry = evbeft-1;          evbeft = code->Get(pctp->Physics);    
628          } else {          if ( evbeft >= 0 ){
629            while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){            T->GetEntry(evbeft);
630              evbeft++;              ph = eh->GetPscuHeader();
631              T->GetEntry(evbeft);            t_pktlast = PKT(ph->GetCounter());
632              t_obtlast = OBT(ph->GetOrbitalTime());
633              if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
634                upperpkt = pktt;
635                upperobt = obtt;
636                upperentry = evbeft-1;
637              } else {
638                while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){
639                  evbeft++;
640                  T->GetEntry(evbeft);
641                  ph = eh->GetPscuHeader();
642                  t_pktlast = PKT(ph->GetCounter());
643                  t_obtlast = OBT(ph->GetOrbitalTime());
644                };
645                T->GetEntry(evbeft-1);
646              ph = eh->GetPscuHeader();              ph = eh->GetPscuHeader();
647              t_pktlast = PKT(ph->GetCounter());              upperpkt = PKT(ph->GetCounter());
648              t_obtlast = OBT(ph->GetOrbitalTime());              upperobt = OBT(ph->GetOrbitalTime());
649                upperentry = evbeft-1;
650            };            };
           T->GetEntry(evbeft-1);  
           ph = eh->GetPscuHeader();  
           upperpkt = PKT(ph->GetCounter());  
           upperobt = OBT(ph->GetOrbitalTime());  
           upperentry = evbeft-1;  
651          };          };
652            if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
653            goto kikko;
654            //      break;
655            //
656        };        };
657        if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);        //  
658        goto kikko;      };
659        //      break;      //
660        //    kikko:
661        //
662        T->GetEntry(upperentry);
663        code = eh->GetCounter();
664        lasttrail = code->Get(pctp->RunTrailer);
665        lasthead = code->Get(pctp->RunHeader);
666        if ( lasttrail < rtev ){
667          rt->GetEntry(lasttrail);
668          pht = eht->GetPscuHeader();
669          pktt = PKT(pht->GetCounter());
670          obtt = OBT(pht->GetOrbitalTime());
671        };
672        //
673        if ( lasthead < rhev ){
674          rh->GetEntry(lasthead);
675          phh = ehh->GetPscuHeader();
676          pkth = PKT(phh->GetCounter());
677          obth = OBT(phh->GetOrbitalTime());
678        };
679        //
680        if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
681        if ( pkth > upperpkt && obth > upperobt ){
682          if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
683          upperpkt = pkth;
684          upperobt = obth;
685          rhev = lasthead+1;
686        } else {
687          rhev = lasthead;
688        };
689        if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
690        //
691        if ( IsDebug() ) printf(" rtev beforev %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
692        if ( pktt > upperpkt && obtt > upperobt ){
693          if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
694          upperpkt = pktt;
695          upperobt = obtt;
696          rtev = lasttrail+1;
697        } else {
698          rtev = lasttrail;
699      };      };
700      //        if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
701        //
702    };    };
703    //    //
704   kikko:    if ( IsDebug() ) printf(" Upper limits are: OBT %lld pkt_num %lld upper entry %i \n",upperobt,upperpkt,upperentry);
705    //    //
706    T->GetEntry(upperentry);    return(0);
707    code = eh->GetCounter();  }
708    lasttrail = code->Get(pctp->RunTrailer);  
709    lasthead = code->Get(pctp->RunHeader);  /**
710    if ( lasttrail < rtev ){   *
711      rt->GetEntry(lasttrail);   * Trick to have unique RUN ID numbers even when runs are deleted and mysql deamon restarted.
712      pht = eht->GetPscuHeader();   * Entries in the _RUNID_GEN table are never deleted.
713      pktt = PKT(pht->GetCounter());   *
714      obtt = OBT(pht->GetOrbitalTime());   **/
715    };  UInt_t PamelaDBOperations::AssignRunID(){
716    //    //
717    if ( lasthead < rhev ){    TSQLResult *result = 0;
718      rh->GetEntry(lasthead);    TSQLRow *row = 0;
719      phh = ehh->GetPscuHeader();    UInt_t runid = 0;
     pkth = PKT(phh->GetCounter());  
     obth = OBT(phh->GetOrbitalTime());  
   };  
720    //    //
721    if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);    stringstream   oss;
722    if ( pkth > upperpkt && obth > upperobt ){    //  
723      if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);    oss.str("");
724      upperpkt = pkth;    oss << "INSERT INTO _RUNID_GEN VALUES (NULL);";
725      upperobt = obth;    result = conn->Query(oss.str().c_str());
726      rhev = lasthead+1;    if ( !result ) throw -10;
727    } else {    oss.str("");
728      rhev = lasthead;    oss << "SELECT ID FROM _RUNID_GEN ORDER BY ID DESC LIMIT 1;";
729    };    result = conn->Query(oss.str().c_str());
730    if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);    if ( !result ) throw -10;
731    //    //
732    if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);    row = result->Next();
   if ( pktt > upperpkt && obtt > upperobt ){  
     if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);  
     upperpkt = pktt;  
     upperobt = obtt;  
     rtev = lasttrail+1;  
   } else {  
     rtev = lasttrail;  
   };  
   if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);  
733    //    //
734    if ( IsDebug() ) printf(" Upper limits are: OBT %llu pkt_num %i upper entry %i \n",upperobt,upperpkt,upperentry);    if ( !row ) throw -28;
735    //    //
736    return(0);    runid = (UInt_t)atoll(row->GetField(0));
737  }    //
738      return(runid);
739    };
740    
741  //  //
742  // GETTERS  // GETTERS
# Line 672  void PamelaDBOperations::CheckConnection Line 835  void PamelaDBOperations::CheckConnection
835    if( !conn ) throw -1;    if( !conn ) throw -1;
836    bool connect = conn->IsConnected();    bool connect = conn->IsConnected();
837    if( !connect ) throw -1;    if( !connect ) throw -1;
838      //
839      if ( IsDebug() ) printf("\n DB INFORMATIONS:\n SQL: %s Version: %s Host %s Port %i \n\n",conn->GetDBMS(),conn->ServerInfo(),conn->GetHost(),conn->GetPort());
840      //
841      if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
842      //
843      // set DB timezone to UTC
844      //
845      stringstream oss;
846      //  
847      oss.str("");
848      oss << "SET time_zone='+0:00';";
849      TSQLResult *result = 0;
850      result = conn->Query(oss.str().c_str());
851      if ( !result ) throw -10;
852      oss.str("");
853      oss << "SET wait_timeout=173000;";
854      conn->Query(oss.str().c_str());
855      //
856  };  };
857    
858  /**  /**
859   * Return the correct packet number if we went back to zero   * Return the correct packet number if we went back to zero
860   */   */
861  UInt_t PamelaDBOperations::PKT(UInt_t pkt_num){    Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){  
862    //    //
863    //  if ( IsDebug() ) printf(" pkt conversion: pkt_num is %u pktfirst is %u (pktfirst - (UInt_t)(16777212/2)) is %u  \n",pkt_num,pktfirst,(pktfirst - (UInt_t)(16777212/2)));    if ( IsDebug() ) printf(" pkt conversion: pkt_num is %u pktfirst is %u  (UInt_t)(16777214/2)) is %u  \n",pkt_num,ppktfirst,(UInt_t)(16777214/2));
864    //    //
865    if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2)  ) return((pkt_num+16777215));    if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2)  ){
866        if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL);
867        return((Long64_t)pkt_num+16777215LL);
868      };
869    //    //
870    if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){    if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){
871      if ( (pkt_num-16777215) < 0 ){      if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL);
872        return((16777215-pkt_num));      return((Long64_t)pkt_num-16777215LL);
     } else {  
       return((pkt_num-16777215));  
     };  
873    };    };
874    //    //
875    return(pkt_num);    if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
876      return((Long64_t)pkt_num);
877    //    //
878  };  };
879    
880  /**  /**
881   * Return the correct On Board Time if we went back to zero   * Return the correct On Board Time if we went back to zero
882   */   */
883  ULong64_t PamelaDBOperations::OBT(UInt_t obt){    Long64_t PamelaDBOperations::OBT(UInt_t obt){  
884    //    //
885    if ( obt < (obtfirst/2) && obtfirst > (numeric_limits<UInt_t>::max()/2) ) return((ULong64_t)(obt+numeric_limits<UInt_t>::max()));    if ( IsDebug() ) printf(" obt conversion: obt is %u obtfirst is %u  (numeric_limits<UInt_t>::max()/2) is %u  \n",obt,pobtfirst,(UInt_t)(numeric_limits<UInt_t>::max()/2));
886    //    //
887    if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){    if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){
888      if ( (obt-numeric_limits<UInt_t>::max()) < 0  ){      if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)(obt+numeric_limits<UInt_t>::max()));
889        return((ULong64_t)(numeric_limits<UInt_t>::max()-obt));      return((Long64_t)(obt+numeric_limits<UInt_t>::max()));
890      } else {    };
891        return((ULong64_t)(obt-numeric_limits<UInt_t>::max()));    //
892      };    if ( obt > ((Long64_t)pobtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
893        if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2));
894        if ( IsDebug() ) printf(" rise down pktnum %lld \n", (Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
895        return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
896    };    };
897    //    //
898    return((ULong64_t)obt);    if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);
899      return((Long64_t)obt);
900  };  };
901    
902  /**  /**
# Line 825  Int_t PamelaDBOperations::insertPamelaRa Line 1011  Int_t PamelaDBOperations::insertPamelaRa
1011    if ( idr ) return(1);    if ( idr ) return(1);
1012    //    //
1013    oss.str("");    oss.str("");
1014    oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"    if ( STATIC ){
1015        << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";      oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
1016            << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";
1017      } else {
1018        oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')";
1019      };
1020    if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;    if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1021    //    //
1022    idr =  this->SetID_RAW();    idr =  this->SetID_RAW();
# Line 845  Int_t PamelaDBOperations::insertPamelaGL Line 1035  Int_t PamelaDBOperations::insertPamelaGL
1035    TSQLResult *result = 0;    TSQLResult *result = 0;
1036    TSQLRow *row = 0;    TSQLRow *row = 0;
1037    UInt_t t0 = 0;    UInt_t t0 = 0;
1038      Int_t signal = 0;
1039      //
1040      signal = this->SetUpperLimits();
1041    //    //
1042    stringstream   oss;    stringstream   oss;
1043    //      //  
1044    if ( this->GetID_RAW() == 0 )  throw -11;    if ( this->GetID_RAW() == 0 )  throw -11;
1045    //    //
1046    oss.str("");    oss.str("");
1047    oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ID_RAW<= "    oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
1048        << id << " AND TO_ID_RAW >= "        << this->GetRawFile().Data() << "';";
       << id << ";";  
1049    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1050    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1051    if ( !result ) throw -10;    if ( !result ) throw -10;
1052    row = result->Next();    row = result->Next();
   if ( !row ) throw -10;  
1053    //    //
1054    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();    if ( !row ){
1055        oss.str("");
1056        oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
1057            << dworbit << " order by FROM_ORBIT desc limit 1;";
1058        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1059        result = conn->Query(oss.str().c_str());
1060        if ( !result ) throw -10;
1061        row = result->Next();
1062        if ( !row ) throw -10;
1063      };
1064      //
1065      TTimeStamp tu = TTimeStamp((UInt_t)atoi(row->GetField(0)),(UInt_t)atoi(row->GetField(1)),(UInt_t)atoi(row->GetField(2)),(UInt_t)atoi(row->GetField(3)),(UInt_t)atoi(row->GetField(4)),(UInt_t)atoi(row->GetField(5)),0,true,0);
1066      t0 = (UInt_t)tu.GetSec();
1067      if ( IsDebug() ) printf(" t0 is %u ti is %s %s %s %s %s %s\n",t0,row->GetField(0),row->GetField(1),row->GetField(2),row->GetField(3),row->GetField(4),row->GetField(5));
1068      //
1069    /*    /*
1070     * Verify that the TIMESYNC have been not already processed     * Verify that the TIMESYNC have been not already processed
1071     */     */
# Line 875  Int_t PamelaDBOperations::insertPamelaGL Line 1080  Int_t PamelaDBOperations::insertPamelaGL
1080    if (result == NULL) throw -10;    if (result == NULL) throw -10;
1081    row = result->Next();    row = result->Next();
1082    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
1083        if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1084      toffset = (UInt_t)atoll(row->GetField(2)) - (UInt_t)(this->OBT((UInt_t)atoll(row->GetField(1)))/1000) + t0;      toffset = (UInt_t)atoll(row->GetField(2)) - (UInt_t)(this->OBT((UInt_t)atoll(row->GetField(1)))/1000) + t0;
1085        //
1086        tsync = (UInt_t)atoll(row->GetField(2));
1087        obt0 = (UInt_t)atoll(row->GetField(1));
1088        //
1089      return(1);      return(1);
1090    };    };
1091    //    //
1092    TTree *T = 0;    TTree *T = 0;
   Int_t signal = 0;  
1093    //    //
1094    UInt_t nevent = 0;    UInt_t nevent = 0;
1095    UInt_t recEntries = 0;    UInt_t recEntries = 0;
# Line 932  Int_t PamelaDBOperations::insertPamelaGL Line 1141  Int_t PamelaDBOperations::insertPamelaGL
1141          //          //
1142          TYPE = 55;//224;          TYPE = 55;//224;
1143          //          //
1144            if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
1145            //
1146          if ( TSYNC && OBT ){          if ( TSYNC && OBT ){
1147            existsts = true;            existsts = true;
1148            goto eout;            goto eout;
# Line 954  Int_t PamelaDBOperations::insertPamelaGL Line 1165  Int_t PamelaDBOperations::insertPamelaGL
1165      //      //
1166      rt->SetBranchAddress("RunTrailer", &runt);      rt->SetBranchAddress("RunTrailer", &runt);
1167      //      //
1168      if ( rhev > 0 ){      Int_t nrhev = rh->GetEntries();
1169        rh->GetEntry(0);      Int_t nrtev = rt->GetEntries();
1170        //      if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev);
1171        TSYNC = runh->LAST_TIME_SYNC_INFO;      //
1172        OBT = runh->OBT_TIME_SYNC * 1000;      if ( nrhev > 0 ){
1173        //        for (Int_t i=0; i<nrhev; i++){
1174        TYPE = 20;          //
1175        //          rh->GetEntry(i);
1176        if ( TSYNC && OBT ){          //
1177          existsts = true;          TSYNC = runh->LAST_TIME_SYNC_INFO;
1178          goto eout;          OBT = runh->OBT_TIME_SYNC * 1000;
1179            //
1180            TYPE = 20;
1181            //
1182            if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
1183            //
1184            if ( TSYNC && OBT ){
1185              existsts = true;
1186              goto eout;
1187            };
1188        };        };
1189        //        //
1190      };      };
1191      if ( rtev > 0 ){      if ( nrtev > 0 ){
1192        //        //
1193        if ( IsDebug() ) printf(" No runheader \n");        if ( IsDebug() ) printf(" No runheader \n");
1194        signal = 6;        signal = 6;
1195        //        //
1196        rt->GetEntry(0);        for (Int_t i=0; i<nrtev; i++){
1197        //          //
1198        TSYNC = runt->LAST_TYME_SYNC_INFO;          rt->GetEntry(i);
1199        OBT = runt->OBT_TYME_SYNC * 1000;          //
1200        //          TSYNC = runt->LAST_TYME_SYNC_INFO;
1201        TYPE = 21;          OBT = runt->OBT_TYME_SYNC * 1000;
1202        //          //
1203        if ( TSYNC && OBT ){          TYPE = 21;
1204          existsts = true;          //
1205          goto eout;          if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
1206            //
1207            if ( TSYNC && OBT ){
1208              existsts = true;
1209              goto eout;
1210            };
1211        };        };
1212        //        //
1213      } else {      } else {
# Line 1053  Int_t PamelaDBOperations::insertPamelaGL Line 1278  Int_t PamelaDBOperations::insertPamelaGL
1278    conn->Query(oss.str().c_str());    conn->Query(oss.str().c_str());
1279    if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());
1280    //    //
1281      if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1282    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1283    //    //
1284      tsync = TSYNC;
1285      obt0 = OBT;
1286      //
1287    delete result;    delete result;
1288    return(signal);    return(signal);
1289  }  }
# Line 1070  Int_t PamelaDBOperations::insertPamelaRo Line 1299  Int_t PamelaDBOperations::insertPamelaRo
1299    UInt_t idtimesync = 0;    UInt_t idtimesync = 0;
1300    //    //
1301    oss.str("");    oss.str("");
1302    oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "    if ( STATIC ){
1303        << " LEFT JOIN GL_ROOT "      oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1304        << " ON GL_RAW.ID = GL_ROOT.ID_RAW "          << " LEFT JOIN GL_ROOT "
1305        << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "          << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1306        << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";          << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1307            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1308      } else {
1309        oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1310            << " LEFT JOIN GL_ROOT "
1311            << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1312            << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "
1313            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1314      };
1315    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1316    //    //
1317    if ( !result ) throw -12;    if ( !result ) throw -12;
# Line 1101  Int_t PamelaDBOperations::insertPamelaRo Line 1338  Int_t PamelaDBOperations::insertPamelaRo
1338    idtimesync = (UInt_t)atoll(row->GetField(0));    idtimesync = (UInt_t)atoll(row->GetField(0));
1339    //    //
1340    oss.str("");    oss.str("");
1341    oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"    if ( STATIC ){
1342        << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";      oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1343            << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";
1344      } else {
1345        oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1346            << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')";
1347      };
1348    //    //
1349    if (conn->Query(oss.str().c_str()) == 0) throw -4;    if (conn->Query(oss.str().c_str()) == 0) throw -4;
1350    //    //
# Line 1129  Int_t PamelaDBOperations::assignBOOT_NUM Line 1371  Int_t PamelaDBOperations::assignBOOT_NUM
1371    TSQLResult *result = 0;    TSQLResult *result = 0;
1372    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
1373    oss.str("");    oss.str("");
1374    oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "    if ( STATIC ){
1375        << " PATH = '" << this->GetRawPath().Data() << "' AND "      oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1376        << " NAME = '" << this->GetRawFile().Data() << "' ";          << " PATH = '" << this->GetRawPath().Data() << "' AND "
1377            << " NAME = '" << this->GetRawFile().Data() << "' ";
1378      } else {
1379        oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1380            << " PATH = '$PAM_RAW' AND "
1381            << " NAME = '" << this->GetRawFile().Data() << "' ";
1382      };
1383    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1384    //    //
1385    if ( !result ) throw -4;;    if ( !result ) throw -4;;
# Line 1154  Int_t PamelaDBOperations::assignBOOT_NUM Line 1402  Int_t PamelaDBOperations::assignBOOT_NUM
1402    VarDumpEvent  *vde = 0;    VarDumpEvent  *vde = 0;
1403    VarDumpRecord *vdr = 0;    VarDumpRecord *vdr = 0;
1404    //    //
1405      Bool_t found = false;
1406    trDumpEv->SetBranchAddress("VarDump", &vde);    trDumpEv->SetBranchAddress("VarDump", &vde);
1407    if ( trDumpEv->GetEntries() > 0 ){    if ( trDumpEv->GetEntries() > 0 ){
1408      Bool_t found = false;      found = false;
1409      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1410        trDumpEv->GetEntry(i);        trDumpEv->GetEntry(i);
1411        vde->Records->GetEntries();        //      vde->Records->GetEntries();
1412        if ( vde->Records->GetEntries()>0 ){        if ( vde->Records->GetEntries()>5 ){
1413          found = true;          found = true;
1414          goto fill;          goto fill;
1415        };        };
# Line 1173  Int_t PamelaDBOperations::assignBOOT_NUM Line 1422  Int_t PamelaDBOperations::assignBOOT_NUM
1422        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1423        //        //
1424      } else {      } else {
1425        if ( !this->GetBOOTnumber() ) return(4);        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4);
1426        };
1427      } else {
1428        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2);
1429      };
1430      //
1431      UInt_t bn = 0;
1432      Bool_t afound = false;
1433      if ( !found && this->AutoBoot()){
1434        afound = true;
1435        //
1436        // Search for other files with similar timesync
1437        //
1438        if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0);
1439        UInt_t upperts = tsync-(obt0/1000)+5;
1440        UInt_t lowerts = tsync-(obt0/1000)-5;
1441        oss.str("");
1442        oss << "select GL_RAW.BOOT_NUMBER from GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW where TIMESYNC-(OBT0/1000)<"
1443            << upperts
1444            << " AND TIMESYNC-(OBT0/1000)>"
1445            << lowerts
1446            << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
1447        result = conn->Query(oss.str().c_str());
1448        if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str());
1449        //
1450        if ( !result ) throw -4;;
1451        found = true;
1452        if ( result->GetRowCount()<3 ){
1453          if ( IsDebug() ) printf(" AGH! no results!\n");
1454          found = false;
1455        } else {
1456          row = result->Next();      
1457          bn = (UInt_t)atoll(row->GetField(0));
1458          for ( Int_t r=1; r<result->GetRowCount() ;r++){
1459            if ( !row ) throw -4;
1460            if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0));
1461            if ( bn != (UInt_t)atoll(row->GetField(0)) ){
1462              if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0)));
1463              found = false;
1464            };
1465            row = result->Next();
1466          };
1467      };      };
1468      };
1469      //
1470      Int_t sgn = 0;
1471      //
1472      if ( !found && !BOOTNO ){
1473        throw -29;
1474    } else {    } else {
1475      if ( !this->GetBOOTnumber() ) return(2);      if ( afound ){
1476          this->SetBOOTnumber(bn);
1477          sgn = 8;
1478        };
1479    };    };
1480    //    //
1481    oss.str("");    oss.str("");
# Line 1186  Int_t PamelaDBOperations::assignBOOT_NUM Line 1485  Int_t PamelaDBOperations::assignBOOT_NUM
1485    conn->Query(oss.str().c_str());          conn->Query(oss.str().c_str());      
1486    //    //
1487    delete result;    delete result;
1488    return(0);    return(sgn);
1489  };  };
1490    
1491  /**  /**
# Line 1199  Int_t PamelaDBOperations::insertPamelaRU Line 1498  Int_t PamelaDBOperations::insertPamelaRU
1498    stringstream oss;    stringstream oss;
1499    oss.str("");    oss.str("");
1500    //    //
1501    signal = this->SetUpperLimits();    //  signal = this->SetUpperLimits();
1502    //    //
1503    // loop on runheader and runtrailer events    // loop on runheader and runtrailer events
1504    //    //
# Line 1250  Int_t PamelaDBOperations::insertPamelaRU Line 1549  Int_t PamelaDBOperations::insertPamelaRU
1549        //        //
1550        if ( !ptt &&  !(ptht+1) ){        if ( !ptt &&  !(ptht+1) ){
1551          //          //
1552          if ( IsDebug() ) printf(" Piece of run at the beginning of the file %i %i %i \n",ptht,pth,ptt);          if ( IsDebug() ) printf(" Piece of run at the beginning of the file %u %u %u \n",ptht,pth,ptt);
1553          //          //
1554          this->HandleRunFragments(true,false,0,(evbeft-1));          this->HandleRunFragments(true,false,0,(evbeft-1));
1555          //          //
1556          //          //
1557        } else if ( pth == ptht ){        } else if ( pth == ptht ){
1558          //          //
1559          if ( IsDebug() ) printf(" Missing header %i %i %i\n",ptht,pth,ptt);          if ( IsDebug() ) printf(" Missing header %u %u %u\n",ptht,pth,ptt);
1560          //          //
1561          if ( (ptt-1) < 0 ) throw -15; // should never arrive here!          if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1562          rt->GetEntry(ptt-1);          rt->GetEntry(ptt-1);
# Line 1266  Int_t PamelaDBOperations::insertPamelaRU Line 1565  Int_t PamelaDBOperations::insertPamelaRU
1565          rt->GetEntry(ptt);          rt->GetEntry(ptt);
1566          pht = eht->GetPscuHeader();          pht = eht->GetPscuHeader();
1567          //          //
1568          if ( IsDebug() ) printf(" Try to find the beginning of a run which has only the runtrailer   %i %i %i \n",ptht,pth,ptt);          if ( IsDebug() ) printf(" Try to find the beginning of a run which has only the runtrailer   %u %u %u \n",ptht,pth,ptt);
1569          if ( IsDebug() ) printf(" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  %i %i %i \n",pkth,obth,obtt);          if ( IsDebug() ) printf(" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  %u %u %u \n",pkth,obth,obtt);
1570          //          //
1571          this->HandleMissingHoT(true,false,evbefh,evbeft-1);          this->HandleMissingHoT(true,false,evbefh,evbeft-1);
1572          //          //
1573        } else {        } else {
1574          //          //
1575          if ( IsDebug() ) printf(" Could be a good run, we have a runheader followed by a runtrailer %i %i %i\n",ptht,pth,ptt);          if ( IsDebug() ) printf(" Could be a good run, we have a runheader followed by a runtrailer %u %u %u\n",ptht,pth,ptt);
1576          //          //
1577          rh->GetEntry(ptht);          rh->GetEntry(ptht);
1578          phh = ehh->GetPscuHeader();          phh = ehh->GetPscuHeader();
# Line 1281  Int_t PamelaDBOperations::insertPamelaRU Line 1580  Int_t PamelaDBOperations::insertPamelaRU
1580          obth = phh->GetOrbitalTime();          obth = phh->GetOrbitalTime();
1581          cod = ehh->GetCounter();          cod = ehh->GetCounter();
1582          evbefh = cod->Get(pctp->Physics);          evbefh = cod->Get(pctp->Physics);
1583          if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);          if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
1584          //          //
1585          // handle this run          // handle this run
1586          //          //
# Line 1293  Int_t PamelaDBOperations::insertPamelaRU Line 1592  Int_t PamelaDBOperations::insertPamelaRU
1592            //            //
1593            if ( IsDebug() ) printf(" Piece of run at the beginning of the file WITH NO RUNTRAILER \n");            if ( IsDebug() ) printf(" Piece of run at the beginning of the file WITH NO RUNTRAILER \n");
1594            //            //
1595            this->HandleRunFragments(true,true,0,(evbefh-1));            if ( evbefh == 1 ){
1596            //              //
1597                signal = 8;
1598                if ( IsDebug() ) printf(" Not supported yet: run with no events, no runtrailer, no runheader \n");
1599                //
1600              } else {
1601                //
1602                this->HandleRunFragments(true,true,0,(evbefh-1));
1603                //
1604              };
1605          };          };
1606          //          //
1607          //          //
1608          if ( (ptht - pth) > 1 ){          if ( (ptht - pth) > 1 ){
1609            //            //
1610            if ( IsDebug() ) printf(" Missing runtrailers! \n");            if ( IsDebug() ) printf(" Missing runtrailers! \n");
1611            if ( IsDebug() ) printf(" Attention there is a jump in the runheader counter %i %i %i \n",ptht,pth,ptt);            if ( IsDebug() ) printf(" Attention there is a jump in the runheader counter %u %u %u \n",ptht,pth,ptt);
1612            // is not the consecutive header            // is not the consecutive header
1613            while ( pth != ptht ){                      while ( pth != ptht ){          
1614              //              //
# Line 1322  Int_t PamelaDBOperations::insertPamelaRU Line 1629  Int_t PamelaDBOperations::insertPamelaRU
1629              obth = phh->GetOrbitalTime();              obth = phh->GetOrbitalTime();
1630              evbefh = cod->Get(pctp->Physics);              evbefh = cod->Get(pctp->Physics);
1631              //              //
1632              if ( IsDebug() ) printf(" Try to find the end of a run which has only the runheader   %i %i %i \n",ptht,pth,ptt);              if ( IsDebug() ) printf(" Try to find the end of a run which has only the runheader   %u %u %u \n",ptht,pth,ptt);
1633              if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);              if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
1634              //              //
1635              this->HandleMissingHoT(false,true,evbefh,evbeft-1);              this->HandleMissingHoT(false,true,evbefh,evbeft-1);
1636              //              //
# Line 1332  Int_t PamelaDBOperations::insertPamelaRU Line 1639  Int_t PamelaDBOperations::insertPamelaRU
1639          } else if ( !(ptht - pth) ){          } else if ( !(ptht - pth) ){
1640            //            //
1641            if ( IsDebug() ) printf(" Missing runheader! \n");            if ( IsDebug() ) printf(" Missing runheader! \n");
1642            if ( IsDebug() ) printf(" Attention! the runheader counter did not changed %i %i %i \n",ptht,pth,ptt);            if ( IsDebug() ) printf(" Attention! the runheader counter did not changed %u %u %u \n",ptht,pth,ptt);
1643            if ( IsDebug() ) printf(" The run should have already been handled by HandleRun() \n");            if ( IsDebug() ) printf(" The run should have already been handled by HandleRun() \n");
1644            //            //
1645          } else {          } else {
# Line 1353  Int_t PamelaDBOperations::insertPamelaRU Line 1660  Int_t PamelaDBOperations::insertPamelaRU
1660            obth = phh->GetOrbitalTime();            obth = phh->GetOrbitalTime();
1661            cod = ehh->GetCounter();            cod = ehh->GetCounter();
1662            evbefh = cod->Get(pctp->Physics);            evbefh = cod->Get(pctp->Physics);
1663            if ( IsDebug() ) printf(" Piece of run at the end of file %i %i %i \n",pkth,obth,obtt);            if ( IsDebug() ) printf(" Piece of run at the end of file %u %u %u \n",pkth,obth,obtt);
1664            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i %i %i \n",ptht,pth,ptt);            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u %u %u \n",ptht,pth,ptt);
1665            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i \n",rhev);            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u \n",rhev);
1666            //            //
1667            this->HandleRunFragments(false,true,evbefh,upperentry);            this->HandleRunFragments(false,true,evbefh,upperentry);
1668          } else {          } else {
# Line 1449  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1756  Bool_t PamelaDBOperations::IsRunAlreadyI
1756      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1757        //        //
1758        if ( IsDebug() ) printf(" The new run has more events than the old one \n");        if ( IsDebug() ) printf(" The new run has more events than the old one \n");
1759        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1760        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1761        if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1762        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1763    //       conn->Query(oss.str().c_str());
1764        if ( signal ) signal = false;        if ( signal ) signal = false;
1765        goto gonext;              goto gonext;      
1766        //        //
# Line 1466  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1774  Bool_t PamelaDBOperations::IsRunAlreadyI
1774        //        //
1775        if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");        if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");
1776        //        //
1777        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1778        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1779        if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1780        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1781    //       conn->Query(oss.str().c_str());
1782        //        //
1783        if ( signal ) signal = false;        if ( signal ) signal = false;
1784        goto gonext;        goto gonext;
# Line 1483  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1792  Bool_t PamelaDBOperations::IsRunAlreadyI
1792        //        //
1793        if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");        if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");
1794        //        //
1795        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1796        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1797        if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1798        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1799    //       conn->Query(oss.str().c_str());
1800        if ( signal ) signal = false;        if ( signal ) signal = false;
1801        //        //
1802      };      };
# Line 1501  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1811  Bool_t PamelaDBOperations::IsRunAlreadyI
1811    delete result;    delete result;
1812    //    //
1813    if ( signal && IsDebug() ) printf(" The run has already been inserted \n");        if ( signal && IsDebug() ) printf(" The run has already been inserted \n");    
1814      if ( !signal && IsDebug() ) printf(" The run existed and was deleted, fill the DB \n");
1815    return(signal);    return(signal);
1816  };  };
1817    
# Line 1522  void PamelaDBOperations::HandleRun(){   Line 1833  void PamelaDBOperations::HandleRun(){  
1833      //      //
1834      this->FillClass();      this->FillClass();
1835      //      //
1836      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);      if ( !IsRunAlreadyInserted() ){
1837          glrun->SetID(this->AssignRunID());
1838          glrun->SetID_RUN_FRAG(0);
1839          glrun->Fill_GL_RUN(conn);
1840        };
1841    } else {    } else {
1842      //      //
1843      if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt);      if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt);
# Line 1545  void PamelaDBOperations::HandleRunFragme Line 1860  void PamelaDBOperations::HandleRunFragme
1860    UInt_t rhfirstev = firstev;    UInt_t rhfirstev = firstev;
1861    UInt_t rtlastev = lastev;    UInt_t rtlastev = lastev;
1862    Bool_t found = false;    Bool_t found = false;
1863      Bool_t foundinrun = false;
1864    //    //
1865    TSQLResult *result = 0;    TSQLResult *result = 0;
1866    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 1571  void PamelaDBOperations::HandleRunFragme Line 1887  void PamelaDBOperations::HandleRunFragme
1887    //    //
1888    if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");    if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
1889    //    //
1890    // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?    // First of all insert the run in the fragment table...
1891    //        //
1892    if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is    oss.str("");
1893                                             // missing it no way we can found a piece in the frag table    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1894      //        << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1895      oss.str("");        << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1896      oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "        << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1897          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1898          << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1899          << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1900          << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1901          << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1902          << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1903          << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1904          << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1905          << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1906          << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1907      //
1908      if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1909      result = conn->Query(oss.str().c_str());
1910      //
1911      if ( !result ) throw -4;
1912      //
1913      row = result->Next();
1914      //
1915      if ( !row ){
1916        //
1917        // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
1918        //
1919        if ( IsDebug() ) printf(" The run is new \n");
1920        if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
1921        //
1922        glrun->SetID(this->AssignRunID());
1923        glrun->SetID_RUN_FRAG(0);
1924        glrun->Fill_GL_RUN_FRAGMENTS(conn);
1925        //
1926      } else {
1927        if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
1928        return;
1929      };
1930      //
1931      //
1932      // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
1933      //    
1934      if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
1935                                               // missing it no way we can found a piece in the frag table
1936        //
1937        oss.str("");
1938        oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1939          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1940          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME()          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1941            << " ID != " << glrun->ID
1942          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1943      //      //
1944      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
# Line 1589  void PamelaDBOperations::HandleRunFragme Line 1948  void PamelaDBOperations::HandleRunFragme
1948      //      //
1949      row = result->Next();      row = result->Next();
1950      //      //
1951        if ( !row && NoFrag() ){
1952          //
1953          oss.str("");
1954          oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
1955              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1956              << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1957              << " ID != " << glrun->ID
1958              << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1959          //
1960          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1961          result = conn->Query(oss.str().c_str());
1962          //
1963          if ( !result ) throw -4;
1964          //
1965          foundinrun = true;
1966          //
1967          row = result->Next();
1968          //
1969        };
1970        //
1971      if ( !row ){      if ( !row ){
1972        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1973        found = false;        found = false;
# Line 1663  void PamelaDBOperations::HandleRunFragme Line 2042  void PamelaDBOperations::HandleRunFragme
2042        //        //
2043        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2044        //        //
2045          if ( foundinrun ){
2046            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2047            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2048          };
2049          //
2050        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
2051        //        //
2052        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2053        //        //
2054        oss.str("");        oss.str("");
2055        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1678  void PamelaDBOperations::HandleRunFragme Line 2062  void PamelaDBOperations::HandleRunFragme
2062        ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());        ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
2063        UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());        UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
2064        ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());        ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
2065        if ( IsDebug() ) printf(" Check overlapping events: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);        if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2066        TTree *T= 0;        TTree *T= 0;
2067        T = (TTree*)file->Get("Physics");        T = (TTree*)file->Get("Physics");
2068        if ( !T || T->IsZombie() ) throw -16;        if ( !T || T->IsZombie() ) throw -16;
# Line 1692  void PamelaDBOperations::HandleRunFragme Line 2076  void PamelaDBOperations::HandleRunFragme
2076          bobt = OBT(ph->GetOrbitalTime());                bobt = OBT(ph->GetOrbitalTime());      
2077          firstev++;          firstev++;
2078        };        };
2079        if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);        if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2080        //        //
       glrun1->SetID(0);  
2081        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2082        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
2083        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
# Line 1703  void PamelaDBOperations::HandleRunFragme Line 2086  void PamelaDBOperations::HandleRunFragme
2086        //        //
2087        glrun->SetEV_FROM(firstev);        glrun->SetEV_FROM(firstev);
2088        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
2089          //
2090        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
2091        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
2092        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
# Line 1722  void PamelaDBOperations::HandleRunFragme Line 2106  void PamelaDBOperations::HandleRunFragme
2106        //        //
2107        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2108          //          //
2109            //      glrun->SetID(this->AssignRunID());
2110            glrun->SetID_RUN_FRAG(glrun1->GetID());
2111          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
2112          //          //
2113          // get id number          // set id number
         //  
         oss.str("");  
         oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "  
             << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "  
             << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";  
         //  
         if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());  
         //  
         result =  conn->Query(oss.str().c_str());  
         if ( !result ) throw -4;  
         row = result->Next();  
         //  
         UInt_t idrun0 = 0;  
         if ( !row ){  
           throw -10;  
         } else {  
           idrun0 = (UInt_t)atoll(row->GetField(0));  
         };  
         //  
         glrun1->SetID_RUN_FRAG(idrun0);  
2114          //          //
2115            glrun1->SetID_RUN_FRAG(glrun->GetID());
2116          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2117          //          //
         oss.str("");  
         oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";  
         //  
         if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());  
         //  
         result =  conn->Query(oss.str().c_str());  
         if ( !result ) throw -4;  
         row = result->Next();  
         //  
         UInt_t idrun1 = 0;  
         if ( !row ){  
           throw -10;  
         } else {  
           idrun1 = (UInt_t)atoll(row->GetField(0));  
         };  
         //  
         oss.str("");  
         oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";  
         //  
         result =  conn->Query(oss.str().c_str());  
         if ( !result ) throw -4;  
         //  
2118        };        };
       //  
       delete glrun1;  
       //  
2119        // delete old entry in fragment table        // delete old entry in fragment table
2120        //        //
2121        oss.str("");        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2122          glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2123        //        //
2124        oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";        delete glrun1;
2125        //        //
       if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str());  
       result =  conn->Query(oss.str().c_str());  
       if ( !result ) throw -4;  
2126        //        //
2127        return;        return;
2128        //        //
# Line 1791  void PamelaDBOperations::HandleRunFragme Line 2131  void PamelaDBOperations::HandleRunFragme
2131    };    };
2132    //    //
2133    if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is    if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
2134                                             // missing it no way we can found a piece in the frag table      // missing it no way we can found a piece in the frag table
2135      //      //
2136      oss.str("");      oss.str("");
2137      oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "      oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "
2138          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2139          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME()          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2140            << " ID != " << glrun->ID
2141          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2142      //      //
2143      if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
# Line 1806  void PamelaDBOperations::HandleRunFragme Line 2147  void PamelaDBOperations::HandleRunFragme
2147      //      //
2148      row = result->Next();      row = result->Next();
2149      //      //
2150        if ( !row && NoFrag() ){
2151          //
2152          oss.str("");
2153          oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
2154              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2155              << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2156              << " ID != " << glrun->ID
2157              << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2158          //
2159          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2160          result = conn->Query(oss.str().c_str());
2161          //
2162          if ( !result ) throw -4;
2163          //
2164          foundinrun = true;
2165          row = result->Next();
2166          //
2167        };
2168        //
2169      if ( !row ){      if ( !row ){
2170        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2171        found = false;        found = false;
# Line 1879  void PamelaDBOperations::HandleRunFragme Line 2239  void PamelaDBOperations::HandleRunFragme
2239        //        //
2240        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2241        //        //
2242          if ( foundinrun ){
2243            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2244            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2245          };
2246          //
2247        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
2248        //        //
2249        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2250        //        //
2251        oss.str("");        oss.str("");
2252        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1894  void PamelaDBOperations::HandleRunFragme Line 2259  void PamelaDBOperations::HandleRunFragme
2259        ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());        ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
2260        UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());        UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
2261        ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());        ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
2262        if ( IsDebug() ) printf(" Check overlapping events: %i %i %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);        if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2263        TTree *T= 0;        TTree *T= 0;
2264        T = (TTree*)file->Get("Physics");        T = (TTree*)file->Get("Physics");
2265        if ( !T || T->IsZombie() ) throw -16;        if ( !T || T->IsZombie() ) throw -16;
# Line 1908  void PamelaDBOperations::HandleRunFragme Line 2273  void PamelaDBOperations::HandleRunFragme
2273          aobt = OBT(ph->GetOrbitalTime());                aobt = OBT(ph->GetOrbitalTime());      
2274          lastev--;          lastev--;
2275        };        };
2276        if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);        if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2277        //        //
2278        glrun->SetEV_TO(lastev);        glrun->SetEV_TO(lastev);
2279        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
# Line 1918  void PamelaDBOperations::HandleRunFragme Line 2283  void PamelaDBOperations::HandleRunFragme
2283        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
2284        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
2285        //        //
       glrun1->SetID(0);  
2286        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
2287        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
2288        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
# Line 1938  void PamelaDBOperations::HandleRunFragme Line 2302  void PamelaDBOperations::HandleRunFragme
2302        //        //
2303        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2304          //          //
2305          glrun->Fill_GL_RUN(conn);          //      glrun->SetID(this->AssignRunID());
         //  
         // get id number  
         //  
         oss.str("");  
         oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "  
             << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "  
             << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";  
         //  
         if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());  
         //  
         result =  conn->Query(oss.str().c_str());  
         if ( !result ) throw -4;  
         row = result->Next();  
2306          //          //
2307          UInt_t idrun0 = 0;          glrun->SetID_RUN_FRAG(glrun1->GetID());
2308          if ( !row ){          glrun->Fill_GL_RUN(conn);
           throw -10;  
         } else {  
           idrun0 = (UInt_t)atoll(row->GetField(0));  
         };  
2309          //          //
2310          glrun1->SetID_RUN_FRAG(idrun0);          // set id number
2311          //          //
2312            glrun1->SetID_RUN_FRAG(glrun->GetID());
2313          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2314          //          //
         oss.str("");  
         oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";  
         //  
         if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());  
         //  
         result =  conn->Query(oss.str().c_str());  
         if ( !result ) throw -4;  
         row = result->Next();  
         //  
         UInt_t idrun1 = 0;  
         if ( !row ){  
           throw -10;  
         } else {  
           idrun1 = (UInt_t)atoll(row->GetField(0));  
         };  
         //  
         oss.str("");  
         oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";  
         //  
         result =  conn->Query(oss.str().c_str());  
         if ( !result ) throw -4;  
         //  
2315        };        };
2316        //        //
2317        delete glrun1;        // delete old entries in fragment table
       //  
       // delete old entry in fragment table  
       //  
       oss.str("");  
       //  
       oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";  
2318        //        //
2319        if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str());        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2320        result =  conn->Query(oss.str().c_str());        glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
       if ( !result ) throw -4;  
2321        //        //
2322          delete glrun1;
2323        //        //
2324        return;        return;
2325        //        //
# Line 2036  void PamelaDBOperations::HandleRunFragme Line 2356  void PamelaDBOperations::HandleRunFragme
2356      //      //
2357      row = result->Next();      row = result->Next();
2358      //      //
2359      if ( !row ){      if ( row ){
       //  
       oss.str("");  
       oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "  
           << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("  
           << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "  
           << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("  
           << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "  
           << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
           << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "  
           << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "  
           << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "  
           << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("  
           << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "  
           << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
           << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "  
           << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";  
       //  
       if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());  
       result = conn->Query(oss.str().c_str());  
       //  
       if ( !result ) throw -4;  
       //  
       row = result->Next();  
       //  
       if ( !row ){  
         //  
         // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)  
         //  
         if ( IsDebug() ) printf(" The run is new \n");  
         if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");  
         //  
         glrun->Fill_GL_RUN_FRAGMENTS(conn);  
         //  
       } else {  
         if ( IsDebug() ) printf(" The run is already present in the fragment table \n");  
       };  
     } else {  
2360        if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");        if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
2361        } else {
2362          if ( NoFrag() ){
2363            glrun->SetID_RUN_FRAG(glrun->GetID());
2364            glrun->Fill_GL_RUN(conn);      
2365            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2366          };    
2367      };      };
2368    };    };
2369    //    //
# Line 2100  void PamelaDBOperations::HandleMissingHo Line 2389  void PamelaDBOperations::HandleMissingHo
2389      //      //
2390      this->FillClass(mishead,mistrail,firstev,lastev);      this->FillClass(mishead,mistrail,firstev,lastev);
2391      //      //
2392      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2393          glrun->SetID(this->AssignRunID());
2394          glrun->SetID_RUN_FRAG(0);
2395          glrun->Fill_GL_RUN(conn);    
2396        };
2397      //      //
2398    };    };
2399    //    //
# Line 2267  Bool_t PamelaDBOperations::IsRunConsiste Line 2560  Bool_t PamelaDBOperations::IsRunConsiste
2560            //                  //      
2561            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2562            //            //
2563            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2564                glrun->SetID(this->AssignRunID());
2565                glrun->SetID_RUN_FRAG(0);
2566                glrun->Fill_GL_RUN(conn);      
2567              };
2568            //            //
2569            firstevno = lastentry + 1;            firstevno = lastentry + 1;
2570            //            //
# Line 2343  void PamelaDBOperations::HandleSuspiciou Line 2640  void PamelaDBOperations::HandleSuspiciou
2640      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2641      //      //
2642      this->FillClass();      this->FillClass();
2643      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2644          glrun->SetID(this->AssignRunID());
2645          glrun->SetID_RUN_FRAG(0);
2646          glrun->Fill_GL_RUN(conn);    
2647        };
2648      //      //
2649    } else {    } else {
2650      //      //
# Line 2371  void PamelaDBOperations::HandleSuspiciou Line 2672  void PamelaDBOperations::HandleSuspiciou
2672        if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");        if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2673        //        //
2674        this->FillClass();        this->FillClass();
2675        if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                if ( !IsRunAlreadyInserted() ){
2676            glrun->SetID(this->AssignRunID());
2677            glrun->SetID_RUN_FRAG(0);
2678            glrun->Fill_GL_RUN(conn);        
2679          };
2680        //        //
2681      } else {      } else {
2682        //        //
# Line 2469  void PamelaDBOperations::HandleSuspiciou Line 2774  void PamelaDBOperations::HandleSuspiciou
2774            //                  //      
2775            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2776            //            //
2777            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2778                glrun->SetID(this->AssignRunID());
2779                glrun->SetID_RUN_FRAG(0);
2780                glrun->Fill_GL_RUN(conn);      
2781              };
2782            //            //
2783            if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check...            if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check...
2784              //              //
# Line 2504  void PamelaDBOperations::HandleSuspiciou Line 2813  void PamelaDBOperations::HandleSuspiciou
2813              //                  //    
2814              this->SetCommonGLRUN(firstTime,lastTime);              this->SetCommonGLRUN(firstTime,lastTime);
2815              //              //
2816              if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                    if ( !IsRunAlreadyInserted() ){
2817                  glrun->SetID(this->AssignRunID());
2818                  glrun->SetID_RUN_FRAG(0);
2819                  glrun->Fill_GL_RUN(conn);      
2820                };
2821            };            };
2822            //            //
2823            firstevno = lastentry + 1;            firstevno = lastentry + 1;
# Line 2567  Int_t PamelaDBOperations::insertCALO_CAL Line 2880  Int_t PamelaDBOperations::insertCALO_CAL
2880          fromtime = this->GetAbsTime(ph->GetOrbitalTime());            fromtime = this->GetAbsTime(ph->GetOrbitalTime());  
2881          if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){          if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
2882            //            //
2883            if ( IsDebug() ) printf(" Calo calibration for section %i at time %i obt %i pkt %i \n",section,fromtime,obt,pkt);            if ( IsDebug() ) printf(" Calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
2884            //            //
2885            // check if the calibration has already been inserted            // check if the calibration has already been inserted
2886            //            //
# Line 2611  Int_t PamelaDBOperations::insertCALO_CAL Line 2924  Int_t PamelaDBOperations::insertCALO_CAL
2924                // no calibrations in the db contain our calibration                // no calibrations in the db contain our calibration
2925                //                //
2926                if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);                if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
2927                if ( fromtime < 1150863400 ){                if ( fromtime < 1150871000 ){ //1150866904
2928                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
2929                  fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode                  fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode
2930                };                };
2931                //                //
# Line 2674  Int_t PamelaDBOperations::insertCALO_CAL Line 2987  Int_t PamelaDBOperations::insertCALO_CAL
2987            //            //
2988          } else {          } else {
2989            //            //
2990            if ( IsDebug() ) printf(" Repetead calo calibration for section %i at time %i obt %i pkt %i \n",section,fromtime,obt,pkt);            if ( IsDebug() ) printf(" Repeated calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
2991            //            //
2992          };                };      
2993          //                //      
# Line 2756  void PamelaDBOperations::HandleTRK_CALIB Line 3069  void PamelaDBOperations::HandleTRK_CALIB
3069        // no calibrations in the db contain our calibration        // no calibrations in the db contain our calibration
3070        //        //
3071        if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");        if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
3072        if ( fromtime < 1150863400 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode        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
3073        //        //
3074        oss.str("");        oss.str("");
3075        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
# Line 2885  Int_t PamelaDBOperations::insertTRK_CALI Line 3198  Int_t PamelaDBOperations::insertTRK_CALI
3198      pkt1 = ph1->GetCounter();        pkt1 = ph1->GetCounter();  
3199      fromtime = this->GetAbsTime(ph1->GetOrbitalTime());        fromtime = this->GetAbsTime(ph1->GetOrbitalTime());  
3200      //      //
3201      valid = 1;  //     valid = 1;
3202      //  //     //
3203      if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1  //     if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1
3204      //      //
3205      //      //
3206      if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){      if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){
3207        //        //
3208        if ( IsDebug() ) printf(" Trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);        if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u \n",fromtime,obt1,pkt1);
3209          //      
3210          valid = ValidateTrkCalib( caltrk1, eh1 );
3211          if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
3212        //        //
3213        // Do we have the second calibration packet?        // Do we have the second calibration packet?
3214        //        //
# Line 2909  Int_t PamelaDBOperations::insertTRK_CALI Line 3225  Int_t PamelaDBOperations::insertTRK_CALI
3225            obt2 = ph2->GetOrbitalTime();              obt2 = ph2->GetOrbitalTime();  
3226            pkt2 = ph2->GetCounter();              pkt2 = ph2->GetCounter();  
3227            //            //
3228            if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2  //        if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2
3229            //                  //      
3230          } else {          } else {
3231            //            //
# Line 2932  Int_t PamelaDBOperations::insertTRK_CALI Line 3248  Int_t PamelaDBOperations::insertTRK_CALI
3248          //          //
3249        };        };
3250        //        //
3251        if ( IsDebug() ) printf(" Found trk calibration2 at obt %i pkt %i t2 is %i \n",obt2,pkt2,t2);        if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2);
3252        //        //
3253        // The calibration is good        // The calibration is good
3254        //        //
3255        if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){        if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){
3256          //          //
3257          if ( IsDebug() ) printf(" The trk calibration2 at obt %i pkt %i t2 is %i is good \n",obt2,pkt2,t2);          if ( IsDebug() ) printf(" The trk calibration2 at obt %u pkt %u t2 is %u is good \n",obt2,pkt2,t2);
3258            //
3259            UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
3260            if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
3261            valid = valid & valid2;
3262          //          //
3263          // Handle good calib          // Handle good calib
3264          //          //
# Line 2948  Int_t PamelaDBOperations::insertTRK_CALI Line 3268  Int_t PamelaDBOperations::insertTRK_CALI
3268          //          //
3269          if ( t2 != pret2+1 ){          if ( t2 != pret2+1 ){
3270            //            //
3271            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);            if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u pret2 is %u \n",obt2,pkt2,t2,pret2);
3272            //            //
3273            while ( t2 > pret2+1 ){            while ( t2 > pret2+1 ){
3274              //              //
# Line 2977  Int_t PamelaDBOperations::insertTRK_CALI Line 3297  Int_t PamelaDBOperations::insertTRK_CALI
3297          //          //
3298          // Check for missing calibtrk2          // Check for missing calibtrk2
3299          //          //
3300          if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %i pkt %i t2 is %i\n",obt2,pkt2,t2);          if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
3301          t2 = pret2;          t2 = pret2;
3302          //          //
3303          // handle missing calib2          // handle missing calib2
# Line 2991  Int_t PamelaDBOperations::insertTRK_CALI Line 3311  Int_t PamelaDBOperations::insertTRK_CALI
3311        //        //
3312      } else {      } else {
3313        //        //
3314        if ( IsDebug() ) printf(" Repetead trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);        if ( IsDebug() ) printf(" Repetead trk calibration1 at time %u obt %u pkt %u \n",fromtime,obt1,pkt1);
3315        //        //
3316      };      };
3317      //          //    
# Line 3016  Int_t PamelaDBOperations::insertTRK_CALI Line 3336  Int_t PamelaDBOperations::insertTRK_CALI
3336      valid = 0;      valid = 0;
3337      if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){      if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){
3338        //        //
3339        if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %i pkt %i t2 is %i\n",obt2,pkt2,t2);        if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
3340        //        //
3341        this->HandleTRK_CALIB(false,true);        this->HandleTRK_CALIB(false,true);
3342        //        //
# Line 3070  Int_t PamelaDBOperations::insertS4_CALIB Line 3390  Int_t PamelaDBOperations::insertS4_CALIB
3390      fromtime = this->GetAbsTime(ph->GetOrbitalTime());        fromtime = this->GetAbsTime(ph->GetOrbitalTime());  
3391      if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){      if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
3392        //        //
3393        if ( IsDebug() ) printf(" S4 calibration at time %i obt %i pkt %i \n",fromtime,obt,pkt);        if ( IsDebug() ) printf(" S4 calibration at time %u obt %u pkt %u \n",fromtime,obt,pkt);
3394        //        //
3395        // check if the calibration has already been inserted        // check if the calibration has already been inserted
3396        //        //
# Line 3112  Int_t PamelaDBOperations::insertS4_CALIB Line 3432  Int_t PamelaDBOperations::insertS4_CALIB
3432            // no calibrations in the db contain our calibration            // no calibrations in the db contain our calibration
3433            //            //
3434            if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");            if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");
3435            if ( fromtime < 1150863400 ){            if ( fromtime < 1150871000 ){  
3436              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
3437              fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode              fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode
3438            };            };
3439            //            //
# Line 3172  Int_t PamelaDBOperations::insertS4_CALIB Line 3492  Int_t PamelaDBOperations::insertS4_CALIB
3492        //        //
3493      } else {      } else {
3494        //        //
3495        if ( IsDebug() ) printf(" Repetead S4 calibration at time %i obt %i pkt %i \n",fromtime,obt,pkt);        if ( IsDebug() ) printf(" Repeated S4 calibration at time %u obt %u pkt %u \n",fromtime,obt,pkt);
3496        //        //
3497      };            };      
3498      //            //      
# Line 3185  Int_t PamelaDBOperations::insertS4_CALIB Line 3505  Int_t PamelaDBOperations::insertS4_CALIB
3505   * Scan the fragment table and move old fragments to the GL_RUN table   * Scan the fragment table and move old fragments to the GL_RUN table
3506   */   */
3507  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3508      return(this->CleanGL_RUN_FRAGMENTS(""));
3509    };
3510    
3511    /**
3512     * Scan the fragment table and move old fragments to the GL_RUN table
3513     */
3514    Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3515    //    //
3516    TSQLResult *result = 0;    TSQLResult *result = 0;
3517    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 3192  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3519  Int_t PamelaDBOperations::CleanGL_RUN_FR
3519    TSQLRow    *row2   = 0;    TSQLRow    *row2   = 0;
3520    //    //
3521    UInt_t moved = 0;    UInt_t moved = 0;
 //  UInt_t timelim = 0;  
 //  TDatime *time = new TDatime();  
3522    //    //
3523    stringstream oss;    stringstream oss;
3524    oss.str("");    oss.str("");
3525    //    //
3526    //    if ( !strcmp(fcleanfile.Data(),"") ){
3527    // check if there are entries older than "olderthan" seconds from now      //
3528    //      // check if there are entries older than "olderthan" seconds from now
3529    oss.str("");      //
3530    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"      oss.str("");
3531        << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3532    //          << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3533    if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());      //
3534    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
3535        result = conn->Query(oss.str().c_str());
3536        //
3537      } else {
3538        oss.str("");
3539        oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3540        if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3541        result = conn->Query(oss.str().c_str());
3542        //    
3543        if ( result ){
3544          //
3545          row = result->Next();
3546          //
3547          if ( row ){
3548            oss.str("");
3549            oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3550                << " ID_ROOT_L0=" << row->GetField(0) << ";";
3551            //
3552            if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3553            result = conn->Query(oss.str().c_str());
3554            //    
3555          };
3556        } else {
3557          return(2);
3558        };
3559      };
3560    //    //
3561    if ( result ){    if ( result ){
3562      //      //
3563      row = result->Next();      row = result->Next();
3564      //      //
3565      if ( row ){      while ( row ){
3566        //        //
3567        oss.str("");        oss.str("");
3568        oss << " ID= "<< row->GetField(0);        oss << " ID= "<< row->GetField(0);
# Line 3247  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3597  Int_t PamelaDBOperations::CleanGL_RUN_FR
3597          if ( IsDebug() ) printf(" The run is new \n");          if ( IsDebug() ) printf(" The run is new \n");
3598          if ( IsDebug() ) printf(" -> fill the DB \n");                if ( IsDebug() ) printf(" -> fill the DB \n");      
3599          //          //
3600          glrun->SetID(0);          //      glrun->SetID(this->AssignRunID()); we use the old run number!
3601            glrun->SetID_RUN_FRAG(glrun->GetID());
3602          glrun->Fill_GL_RUN(conn);            glrun->Fill_GL_RUN(conn);  
3603          //          //
3604          oss.str("");  //      oss.str("");
3605          oss << " SELECT ID FROM GL_RUN WHERE "  //      oss << " SELECT ID FROM GL_RUN WHERE "
3606              << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "  //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3607              << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "  //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3608              << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "  //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3609              << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "  //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3610              << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";  //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3611          //  //      //
3612          if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());  //      if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
3613          result2 = conn->Query(oss.str().c_str());  //      result2 = conn->Query(oss.str().c_str());
3614          //  //      //
3615          if ( !result2 ) throw -4;  //      if ( !result2 ) throw -4;
3616          //  //      //
3617          row2 = result2->Next();  //      row2 = result2->Next();
3618          //  //      //
3619          if ( !row2 ) throw -25;  //      if ( !row2 ) throw -25;
3620          //  //      //
3621          oss.str("");  //      oss.str("");
3622          oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);  //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);
3623          if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());  //      if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());
3624          result2 = conn->Query(oss.str().c_str());  //      result2 = conn->Query(oss.str().c_str());
3625          //  //      //
3626          if ( !result2 ) throw -4;  //      if ( !result2 ) throw -4;
3627          //          //
3628          moved++;          moved++;
3629          //          //
# Line 3282  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3633  Int_t PamelaDBOperations::CleanGL_RUN_FR
3633        if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));              if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));      
3634        //        //
3635        //        //
3636        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3637        oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);  //      oss.str("");
3638        if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());  //       oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
3639        result2 = conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
3640        //  //       result2 = conn->Query(oss.str().c_str());
3641        if ( !result2 ) throw -4;  //      //
3642        //  //      if ( !result2 ) throw -4;
3643    //      //
3644          row = result->Next();
3645      };      };
3646    };    };
3647    if ( IsDebug() ) printf(" Moved %u runs\n",moved);    if ( IsDebug() ) printf(" Moved %u runs\n",moved);
# Line 3299  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3652  Int_t PamelaDBOperations::CleanGL_RUN_FR
3652   * Check if runs are good, i.e. if the tracker calibration is correctly associated..   * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3653   */   */
3654  Int_t PamelaDBOperations::ValidateRuns(){  Int_t PamelaDBOperations::ValidateRuns(){
3655      return(this->ValidateRuns(""));
3656    };
3657    
3658    /**
3659     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3660     */
3661    Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3662    //    //
3663    TSQLResult *result = 0;    TSQLResult *result = 0;
3664    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
3665    //    //
3666      UInt_t calibtime = 50;
3667      //
3668    stringstream oss;    stringstream oss;
3669    oss.str("");    oss.str("");
3670    //    //
# Line 3311  Int_t PamelaDBOperations::ValidateRuns() Line 3673  Int_t PamelaDBOperations::ValidateRuns()
3673    // =======================================================    // =======================================================
3674    UInt_t t_stop  = 0;    UInt_t t_stop  = 0;
3675    UInt_t t_start = 0;    UInt_t t_start = 0;
3676    // --------------------------------------------------------------    if ( !strcmp(valfile.Data(),"") ) {
3677    // 1) get the OBT of the last run inserted after clean-time limit      // --------------------------------------------------------------
3678    // --------------------------------------------------------------      // 1) get the OBT of the last run inserted after clean-time limit
3679    oss.str("");      // --------------------------------------------------------------
3680    oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()      oss.str("");
3681            << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3682    if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());          << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3683    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3684    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3685    if ( !result->GetRowCount() ) {      if ( !result ) throw -4;
3686            printf(" No runs to validate \n");      if ( !result->GetRowCount() ) {
3687            return(1);        printf(" No runs to validate \n");
3688    }else{        return(1);
3689          row = result->Next();      }else{
3690          t_start = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3691    };          t_start = (UInt_t)atoll(row->GetField(4));
3692    // --------------------------------------------------------------      };  
3693    // 2) get the OBT of the last validated run      // --------------------------------------------------------------
3694    // --------------------------------------------------------------      // 2) get the OBT of the last validated run
3695    oss.str("");      // --------------------------------------------------------------
3696    oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start      oss.str("");
3697        <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3698    if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3699    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3700    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3701    if ( result->GetRowCount() ){      if ( !result ) throw -4;
3702            row = result->Next();      if ( result->GetRowCount() ){
3703            t_stop = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3704    };        t_stop = (UInt_t)atoll(row->GetField(4));
3705    if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);      };
3706    // --------------------------------------------------------------      if ( IsDebug() ) printf("Validation interval: from time %u - to time %u \n\n",t_stop,t_start);
3707    // now retrieves runs to be validated      // --------------------------------------------------------------
3708    // --------------------------------------------------------------      // now retrieves runs to be validated
3709    oss.str("");      // --------------------------------------------------------------
3710    oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;      oss.str("");
3711    oss << " AND RUNHEADER_TIME >="<< t_stop;      oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;
3712    oss << " ORDER BY RUNHEADER_TIME DESC;";      oss << " AND RUNHEADER_TIME >="<< t_stop;
3713  //  if ( IsDebug() )      oss << " ORDER BY RUNHEADER_TIME DESC;";
3714    if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());      if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3715    result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
3716    if ( !result ) throw -4;    } else {
3717    if ( !result->GetRowCount() ) printf(" No runs to validate \n");      //
3718  //  printf("------------------------------------------------------------------------------- \n");      stringstream myquery;
3719          UInt_t myid = 0;
3720    Int_t nrow = 0;      myquery.str("");
3721    GL_RUN* this_run = new GL_RUN();      myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3722    GL_RUN* next_run = new GL_RUN();      //
3723    Int_t   nseq_max = 1000;      result = conn->Query(myquery.str().c_str());
3724  //  UInt_t* sequence = new UInt_t[100];      //
3725    vector<UInt_t> sequence(nseq_max);      row = result->Next();      
3726    Int_t   nseq = 0;      if( !row ){
3727    Bool_t CHECK = false;        if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3728    Bool_t this_ONLINE = false;          if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3729    Bool_t next_ONLINE = false;          return(2);
3730    UInt_t t1=0,t2=0;        };
3731    // ---------------------------------------------------------------------------------        if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3732    // - loop over runs, back in time,        return(0);
3733    // - select sequences of runs close in time (less than 60 s apart),      };
3734    //   which could be preceeded by a calibration      myid=(UInt_t)atoll(row->GetField(0));
3735    // - check if there might be a missing calibration      //
3736    // ---------------------------------------------------------------------------------      myquery.str("");
3737        myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3738        //
3739        result = conn->Query(myquery.str().c_str());
3740        //
3741        row = result->Next();      
3742        if( !row->GetField(0) || !row->GetField(1)){
3743          //
3744          if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3745          //
3746          return(0);
3747          //
3748        } else {  
3749          //
3750          UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3751          UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3752          UInt_t caltime = 0;
3753          //
3754          myquery.str("");
3755          myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3756          myquery << " order by FROM_TIME asc limit 1;";
3757          //
3758          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3759          //
3760          //
3761          result = conn->Query(myquery.str().c_str());
3762          //
3763          row = result->Next();      
3764          if( !row ){
3765            caltime = runhtime;
3766          } else {
3767            caltime = (UInt_t)atoll(row->GetField(0));
3768          };
3769          //
3770          myquery.str("");
3771          myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3772          myquery << caltime << " order by RUNHEADER_TIME DESC";
3773          //
3774          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3775          //
3776          result = conn->Query(myquery.str().c_str());
3777          //
3778        };
3779      };
3780      //
3781      if ( !result ) throw -4;
3782      if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3783      //
3784      Int_t nrow = 0;
3785      GL_RUN* this_run = new GL_RUN();
3786      GL_RUN* next_run = new GL_RUN();
3787      Int_t   nseq_max = 1000;
3788    //  UInt_t* sequence = new UInt_t[100];
3789      vector<UInt_t> sequence(nseq_max);
3790      Int_t   nseq = 0;
3791      Bool_t CHECK = false;
3792      Bool_t this_ONLINE = false;
3793      Bool_t next_ONLINE = false;
3794      UInt_t t1=0,t2=0;
3795      // ---------------------------------------------------------------------------------
3796      // - loop over runs, back in time,
3797      // - select sequences of runs close in time (less than calibtime s apart),
3798      //   which could be preceeded by a calibration
3799      // - check if there might be a missing calibration
3800      // ---------------------------------------------------------------------------------
3801    while(1){    while(1){
3802                        
3803            row = result->Next();            row = result->Next();
# Line 3408  Int_t PamelaDBOperations::ValidateRuns() Line 3834  Int_t PamelaDBOperations::ValidateRuns()
3834                                                        
3835                            if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0;     //=> run fragments                            if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0;     //=> run fragments
3836                                                        
3837                            if( interval >= 60 )CHECK = true;                     //more than 60 s => there might be a calibration                            if( interval >= calibtime )CHECK = true;                      //more than calibtime s => there might be a calibration
3838                                                        
3839                            if( !CHECK && this_run->VALIDATION ){                            if( !CHECK && this_run->VALIDATION ){
3840                                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);                                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
# Line 3448  Int_t PamelaDBOperations::ValidateRuns() Line 3874  Int_t PamelaDBOperations::ValidateRuns()
3874                    }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);                    }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
3875            };            };
3876                        
3877            if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID);            if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);
3878            nrow++;            nrow++;
3879                        
3880    };    };
# Line 3477  Bool_t PamelaDBOperations::MissingTRK_CA Line 3903  Bool_t PamelaDBOperations::MissingTRK_CA
3903          // which should be equal to the time between ascending-nodes.          // which should be equal to the time between ascending-nodes.
3904          //==============================================================          //==============================================================
3905          if ( t2 - trkcalib->FROM_TIME > 5700) {          if ( t2 - trkcalib->FROM_TIME > 5700) {
3906                  if ( IsDebug() )printf("Long time between calib and run start %i :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);                  if ( IsDebug() )printf("Long time between calib and run start %u :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);
3907          //==============================================================          //==============================================================
3908          // there might be a missing calibration, due to:          // there might be a missing calibration, due to:
3909          // - MM full          // - MM full
# Line 3498  Bool_t PamelaDBOperations::MissingTRK_CA Line 3924  Bool_t PamelaDBOperations::MissingTRK_CA
3924          // it is enough to say that there are no missing calibrations          // it is enough to say that there are no missing calibrations
3925          //==============================================================          //==============================================================
3926          // the long time interval bewteen runs might be due to download          // the long time interval bewteen runs might be due to download
3927          if ( IsDebug() )printf("Short time between calib and run start %i :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);          if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
3928          return(false);          return(false);
3929                    
3930  };  };
# Line 3523  Int_t PamelaDBOperations::assignVALIDATI Line 3949  Int_t PamelaDBOperations::assignVALIDATI
3949    
3950    
3951    
3952    // Insert TLEs from file tlefilename in the table GL_TLE in the db
3953    // opened by conn, sorting them by date from older to newer, if each
3954    // TLE has not been alread inserted.
3955    Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
3956    {
3957      fstream tlefile(tlefilename, ios::in);
3958    
3959      if ( !tlefile ) throw -7;
3960    
3961      vector<cTle*> ctles;
3962      vector<cTle*>::iterator iter;
3963      int present = 0;
3964    
3965      // Get three lines from tlefile, create a cTle object and put it
3966      // into ctles
3967      while(1) {
3968        cTle *tlef;
3969        string str1, str2, str3;
3970    
3971        getline(tlefile, str1);
3972        if(tlefile.eof()) break;
3973    
3974        getline(tlefile, str2);
3975        if(tlefile.eof()) break;
3976    
3977        getline(tlefile, str3);
3978        if(tlefile.eof()) break;
3979    
3980        // We now have three good lines for a cTle.
3981        tlef = new cTle(str1, str2, str3);
3982        ctles.push_back(tlef);
3983      }
3984    
3985      tlefile.close();
3986    
3987      // Sort by date
3988      sort(ctles.begin(), ctles.end(), compTLE);
3989    
3990      // Now we insert each TLE into the db
3991      for(iter = ctles.begin(); iter != ctles.end(); iter++) {
3992        cTle *tle = *iter;
3993    
3994        // Do nothing if it's already present in the db.  Just increase
3995        // the counter present.
3996        if (! isTlePresent(tle))
3997          {
3998            int status = insertTle(tle);
3999    
4000            // Insert query failed.  Return 1.
4001            if(status == EXIT_FAILURE) {
4002              
4003              if( IsDebug() ) {
4004                cerr << "Error: inserting TLE:" << endl
4005                     << tle->getName() << endl
4006                     << tle->getLine1() << endl
4007                     << tle->getLine2() << endl;
4008              }
4009    
4010              throw -4;
4011              return 1;
4012            }
4013    
4014          }
4015        else
4016          present++;
4017    
4018      }
4019    
4020      int inserted = ctles.size() - present;  // Number of inserted TLE.
4021      if ( IsDebug() )
4022        cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
4023             << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
4024    
4025      ctles.clear();
4026    
4027    
4028      // Return 2 if no new TLE has been inserted.  0 otherwise.
4029      if(! inserted ) return 2;
4030      return 0;
4031    }
4032    
4033    
4034    // Insert tle in the table GL_TLE using the connection conn.
4035    Int_t PamelaDBOperations::insertTle(cTle *tle)
4036    {
4037      stringstream oss;
4038      TSQLResult *result = 0;
4039    
4040      oss.str("");
4041      oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
4042          << " VALUES ( '"
4043          << tle->getName() << "', '"
4044          << tle->getLine1() << "', '"
4045          << tle->getLine2() << "', '"
4046          << getTleDatetime(tle) << "')";
4047    
4048      //  cout << oss.str().c_str() << endl;
4049      result = conn->Query(oss.str().c_str());
4050      if (result == NULL)
4051        return EXIT_FAILURE;
4052    
4053      return EXIT_SUCCESS;
4054    }
4055    
4056    
4057    // Return whether tle is already in the db connected by conn.
4058    bool PamelaDBOperations::isTlePresent(cTle *tle)
4059    {
4060      stringstream oss;
4061      TSQLResult *result = 0;
4062    
4063      oss.str("");
4064      oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
4065          << getTleDatetime(tle) << "'";
4066    
4067      result = conn->Query(oss.str().c_str());
4068      if (result == NULL) throw -4;
4069    
4070      if (result->GetRowCount())
4071        return true;
4072      else
4073        return false;
4074    }
4075    
4076    
4077    // Return whether the first TLE is dated early than the second
4078    bool compTLE (cTle *tle1, cTle *tle2)
4079    {
4080      return getTleJulian(tle1) < getTleJulian(tle2);
4081    }
4082    
4083    
4084    // Return the date of the tle using the format (year-2000)*1e3 +
4085    // julian day.  e.g. 6365 is the 31th Dec 2006.
4086    // It does *not* return a cJulian date.
4087    float getTleJulian(cTle *tle) {
4088      return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
4089    }
4090    
4091    
4092    // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
4093    // format.
4094    string getTleDatetime(cTle *tle)
4095    {
4096      int year, mon, day, hh, mm, ss;
4097      double dom; // day of month (is double!)
4098      stringstream date; // date in datetime format
4099    
4100      // create a cJulian from the date in tle
4101      cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
4102    
4103      // get year, month, day of month
4104      jdate.getComponent(&year, &mon, &dom);
4105    
4106      // build a datetime YYYY-MM-DD hh:mm:ss
4107      date.str("");
4108      day = (int) floor(dom);
4109      hh = (int) floor( (dom - day) * 24);
4110      mm = (int) floor( ((dom - day) * 24 - hh) * 60);
4111      ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
4112      //  ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
4113    
4114      date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
4115    
4116      return date.str();
4117    }
4118    
4119    /**
4120     * Remove a file from the DB, delete on cascade all entries related to that file
4121     * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
4122     * calibration
4123     **/
4124    Int_t PamelaDBOperations::removeFile(TString remfile){
4125      //
4126      // Determine ID_ROOT_L0 and ID_RAW
4127      //
4128      TSQLResult *pResult;
4129      TSQLRow *Row;
4130      stringstream myquery;
4131      //  
4132      myquery.str("");
4133      myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
4134      //
4135      pResult = conn->Query(myquery.str().c_str());
4136      //
4137      Row = pResult->Next();      
4138      if( !Row ){
4139        if ( strcmp(remfile.Data(),GetRootName().Data()) ){
4140          if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
4141          return(1);
4142        };
4143        if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
4144        return(0);
4145      };
4146      //
4147      this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
4148      this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
4149      //
4150      this->ValidationOFF();
4151      //
4152      this->RemoveCALIBS();
4153      //
4154      this->RemoveRUNS();
4155      //
4156      this->RemoveFILES();
4157      //
4158      this->SetID_ROOT(0);
4159      this->SetID_RAW(0);
4160      //
4161      return(0);
4162    };
4163    
4164    /**
4165     *
4166     * Set validation bit to zero for runs following the removing file till
4167     * 1) a run with TRK_CALIB_USED=140
4168     * 2) a run with VALIDATION = 0
4169     * 3) the next calibration
4170     *
4171     **/
4172    void PamelaDBOperations::ValidationOFF(){
4173      TSQLResult *pResult;
4174      TSQLRow *Row;
4175      stringstream myquery;
4176      Int_t unv = 0;
4177      //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
4178      myquery.str("");
4179      myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4180      //
4181      pResult = conn->Query(myquery.str().c_str());
4182      //
4183      Row = pResult->Next();      
4184      if( !Row->GetField(0) ){
4185        //
4186        if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
4187        //
4188      } else {  
4189        //
4190        UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
4191        UInt_t caltime = 0;
4192        //
4193        myquery.str("");
4194        myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
4195        myquery << " order by FROM_TIME asc limit 1;";
4196        //
4197        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4198        //
4199        //
4200        delete pResult;
4201        pResult = conn->Query(myquery.str().c_str());
4202        //
4203        Row = pResult->Next();      
4204        if( !Row ){
4205          caltime = runhtime;
4206        } else {
4207          caltime = (UInt_t)atoll(Row->GetField(0));
4208        };
4209        //
4210        myquery.str("");
4211        myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4212        myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4213        //
4214        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4215        //
4216        pResult = conn->Query(myquery.str().c_str());
4217        //
4218        Row = pResult->Next();      
4219        if( !Row ){
4220          //
4221          if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4222          //
4223        } else {
4224          myquery.str("");
4225          myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4226          myquery << " RUNHEADER_TIME>=" <<runhtime;
4227          myquery << " order by RUNHEADER_TIME asc;";
4228          //
4229          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4230          //
4231          pResult = conn->Query(myquery.str().c_str());
4232          //
4233          Row = pResult->Next();  
4234          while ( Row ){
4235            //      
4236            unv++;
4237            this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4238            Row = pResult->Next();  
4239            //
4240          };
4241        };
4242      };
4243      if ( IsDebug() ) printf(" %u runs have been unvalidated \n",unv);
4244    };
4245    
4246    /**
4247     *
4248     * Rearrange GL_RUN table and remove runs
4249     *
4250     **/
4251    void PamelaDBOperations::RemoveRUNS(){
4252      TSQLResult *pResult;
4253      TSQLRow *Row;
4254      stringstream myquery;
4255      UInt_t drun = 0;
4256      GL_RUN *delrun = new GL_RUN();
4257      //
4258      myquery.str("");
4259      myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4260      //
4261      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4262      //
4263      pResult = conn->Query(myquery.str().c_str());
4264      //
4265      Row = pResult->Next();    
4266      //
4267      //
4268      if ( !Row ){
4269        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4270      } else {
4271        if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");    
4272        while ( Row ){
4273          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4274          if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4275          drun++;
4276          Row = pResult->Next();    
4277        };
4278      };
4279      //
4280      //
4281      myquery.str("");
4282      myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4283      //
4284      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4285      //
4286      pResult = conn->Query(myquery.str().c_str());
4287      //
4288      Row = pResult->Next();    
4289      //
4290      if ( !Row ){
4291        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4292      } else {
4293        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");    
4294        while ( Row ){
4295          if ( IsDebug() ) printf(" restore run %u \n",(UInt_t)atoll(Row->GetField(1)));
4296          delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4297          if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(1)));
4298          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4299          if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4300            if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4301            delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4302          };
4303          drun++;
4304          Row = pResult->Next();    
4305        };
4306      };
4307      //
4308      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);    
4309      //
4310      //
4311      //
4312      drun = 0;
4313      //
4314      myquery.str("");
4315      myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4316      //
4317      pResult = conn->Query(myquery.str().c_str());
4318      //
4319      Row = pResult->Next();    
4320      //
4321      if ( !Row ){
4322        if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4323      } else {
4324        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");    
4325        while ( Row ){
4326          if ( IsDebug() ) printf(" del run idtrash %u \n",(UInt_t)atoll(Row->GetField(0)));
4327          myquery.str("");
4328          myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";      
4329          conn->Query(myquery.str().c_str());
4330          drun++;
4331          Row = pResult->Next();    
4332        };
4333      };
4334      //  
4335      if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_TRASH table \n",drun);    
4336      //
4337      //
4338      //
4339      drun = 0;
4340      //
4341      myquery.str("");
4342      myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4343      //
4344      pResult = conn->Query(myquery.str().c_str());
4345      //
4346      Row = pResult->Next();    
4347      //
4348      if ( !Row ){
4349        if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4350      } else {
4351        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");    
4352        while ( Row ){
4353          if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4354          myquery.str("");
4355          myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4356          conn->Query(myquery.str().c_str());
4357          drun++;
4358          Row = pResult->Next();    
4359        };
4360      };
4361      //  
4362      if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_FRAGMENTS table \n",drun);    
4363      //
4364      //
4365      //
4366      delete delrun;
4367      //
4368    };
4369    
4370    
4371    /**
4372     *
4373     * Rearrange calibration tables
4374     *
4375     **/
4376    void PamelaDBOperations::RemoveFILES(){
4377      stringstream myquery;
4378      //
4379      myquery.str("");
4380      myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4381      //
4382      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4383      //
4384      conn->Query(myquery.str().c_str());
4385      //
4386    };
4387    
4388    /**
4389     *
4390     * Rearrange calibration tables
4391     *
4392     **/
4393    void PamelaDBOperations::RemoveCALIBS(){
4394      TSQLResult *pResult;
4395      TSQLRow *Row;
4396      stringstream myquery;
4397      //  
4398      //
4399      // Calorimeter
4400      //
4401      for (Int_t section = 0; section < 4; section++){
4402        myquery.str("");
4403        myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4404        myquery << " SECTION=" << section << ";";
4405        //
4406        pResult = conn->Query(myquery.str().c_str());
4407        //
4408        Row = pResult->Next();      
4409        if( !Row->GetField(0) || !Row->GetField(1) ){
4410          //
4411          if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4412          //
4413        } else {
4414          //
4415          myquery.str("");
4416          myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4417          myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4418          myquery << " SECTION=" << section << ";";
4419          //
4420          pResult = conn->Query(myquery.str().c_str());
4421          //
4422          if( !pResult ){
4423            //
4424            if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4425            //
4426            throw -4;
4427            //
4428          };
4429          //
4430        };
4431      };
4432      myquery.str("");
4433      myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4434      //
4435      pResult = conn->Query(myquery.str().c_str());
4436      //
4437      if( !pResult ){
4438        //
4439        if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4440        //
4441        throw -4;
4442        //
4443      };
4444      //
4445      // Tracker
4446      //
4447      myquery.str("");
4448      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4449      //
4450      pResult = conn->Query(myquery.str().c_str());
4451      //
4452      Row = pResult->Next();      
4453      if( !Row->GetField(0) || !Row->GetField(1) ){
4454        //
4455        if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4456        //
4457      } else {
4458        //
4459        myquery.str("");
4460        myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4461        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4462        //
4463        pResult = conn->Query(myquery.str().c_str());
4464        //
4465        if( !pResult ){
4466          //
4467          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4468          //
4469          throw -4;
4470          //
4471        };
4472        //
4473        myquery.str("");
4474        myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4475        //
4476        pResult = conn->Query(myquery.str().c_str());
4477        //
4478        if( !pResult ){
4479          //
4480          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4481          //
4482          throw -4;
4483          //
4484        };
4485      };
4486      //
4487      //
4488      // S4
4489      //
4490      myquery.str("");
4491      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4492      //
4493      pResult = conn->Query(myquery.str().c_str());
4494      //
4495      Row = pResult->Next();      
4496      if( !Row->GetField(0) || !Row->GetField(1) ){
4497        //
4498        if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4499        //
4500      } else {
4501        //
4502        myquery.str("");
4503        myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4504        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4505        //
4506        pResult = conn->Query(myquery.str().c_str());
4507        //
4508        if( !pResult ){
4509          //
4510          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4511          //
4512          throw -4;
4513          //
4514        };
4515        //
4516        myquery.str("");
4517        myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4518        //
4519        pResult = conn->Query(myquery.str().c_str());
4520        //
4521        if( !pResult ){
4522          //
4523          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4524          //
4525          throw -4;
4526          //
4527        };
4528        //
4529      };
4530    };
4531    
4532    /**
4533     *
4534     * Rearrange calibration tables
4535     *
4536     **/
4537    UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
4538    
4539        Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
4540        UInt_t timeaftercalib=120000; //2000;
4541    //  ----------
4542    //  Check CRCs
4543    //  ----------
4544        for(Int_t ipkt=0; ipkt<6; ipkt++){
4545            if( caltrk->crc_hcal[ipkt] )return 0; // :-(
4546            for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
4547        }
4548    //  -----------------------
4549    //  Check missing packets:
4550    //  -----------------------
4551    //    Readout order:
4552    //    ------------------
4553    //    DSP   packet board
4554    //    ------------------
4555    //    12    0      1
4556    //    10    1      1
4557    //     8    2      1
4558    //     4    3      1
4559    //     6    4      1
4560    //     2    5      1
4561    //    ------------------
4562    //    11    0      2
4563    //     9    1      2
4564    //     7    2      2
4565    //     3    3      2
4566    //     5    4      2
4567    //     1    5      2
4568    //    ------------------
4569    //  -------------------------------------------------
4570    //  Check if it is first or second calibration packet
4571    //  -------------------------------------------------
4572        UInt_t build=0;
4573        TString classname = caltrk->GetName();
4574        UInt_t base=0;
4575        UInt_t mask=0;
4576        if(classname.Contains("CalibTrk1Event")){
4577            base=12;
4578            mask=0x03F000;
4579        }
4580        if(classname.Contains("CalibTrk2Event")){
4581            base=18;
4582            mask=0xFC0000;
4583        }
4584    //  -------------------------------------------------
4585    //  Count number of packets and set build variable
4586    //  -------------------------------------------------
4587        Int_t  npkts=0;
4588        for(Int_t ipkt=0; ipkt<6; ipkt++){
4589            if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
4590                npkts++;
4591                build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
4592            }
4593        }
4594    //    if( npkts==6 )return 1; // :-)
4595    
4596    //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
4597    
4598    //  -----------------------------------------------
4599    //  If missing packets: check the acq configuration
4600    //  (some DSPs might be excluded from acquisition)
4601    //  -----------------------------------------------
4602    
4603    //  -----------------------------------------------
4604    //  retrieve the first run header after calib
4605    //  -----------------------------------------------
4606         PacketType *pctp;
4607         EventCounter *cod;
4608         cod = eh->GetCounter();
4609         Int_t irun = cod->Get(pctp->RunHeader);
4610         TTree *rh=(TTree*)file->Get("RunHeader");
4611         if ( !rh || rh->IsZombie() ) throw -17;
4612         if( rh->GetEntries() == irun ){
4613              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;
4614             return 0; // :-(
4615         }
4616    
4617         RunHeaderEvent *run  = 0;
4618         EventHeader    *hrun = 0;
4619         rh->SetBranchAddress("RunHeader", &run);
4620         rh->SetBranchAddress("Header", &hrun);
4621         rh->GetEntry(irun);
4622    //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
4623    
4624         if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
4625              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
4626             return 0; // :-(
4627         }
4628        
4629         if( !run->RM_ACQ_AFTER_CALIB ){
4630              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0    -- cannot validate :-( "<<endl;
4631             return 0; // :-(
4632         }
4633    
4634         UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
4635         if( dtime > timeaftercalib ){
4636             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
4637             return 0; // :-(
4638         }
4639        
4640    
4641    
4642         if( (run->ACQ_BUILD_INFO & mask) != build ){
4643             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;
4644             return 0; // :-(
4645         }
4646         return 1; // :-)
4647    
4648    }
4649    
4650    /**
4651     *
4652     * Check the DB (only for overlapping runs at the moment)
4653     *
4654     **/
4655    UInt_t PamelaDBOperations::Check(){
4656      //
4657      UInt_t test = 0;
4658      //
4659      UInt_t thisrht = 0;
4660      UInt_t thisrtt = 0;
4661      UInt_t thisid = 0;
4662      UInt_t prevrht = 0;
4663      UInt_t prevrtt = 0;
4664      UInt_t previd = 0;
4665      //
4666      UInt_t prevl0id = 0;
4667      UInt_t thisl0id = 0;
4668      //
4669      stringstream oss;
4670      TSQLResult *result = 0;
4671      TSQLRow    *row    = 0;
4672      TSQLResult *result2 = 0;
4673      TSQLRow    *row2    = 0;
4674      oss.str("");
4675      oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN order by RUNHEADER_TIME asc;";
4676      //  oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
4677      result = conn->Query(oss.str().c_str());
4678      //
4679      if ( !result ) throw -4;;
4680      //
4681      row = result->Next();
4682      //
4683      while ( row ){
4684        thisid = (UInt_t)atoll(row->GetField(0));
4685        thisl0id = (UInt_t)atoll(row->GetField(1));
4686        thisrht = (UInt_t)atoll(row->GetField(2));
4687        thisrtt = (UInt_t)atoll(row->GetField(3));
4688        //
4689        //    if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
4690        //    if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
4691        if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
4692          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4693          printf(" CHECK n.1 TIME SCREW of %u s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
4694          TString prevf = "";
4695          TString thisf = "";
4696          oss.str("");
4697          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4698          result2 = conn->Query(oss.str().c_str());
4699          if ( !result2 ) throw -4;;
4700          row2 = result2->Next();
4701          prevf = (TString)row2->GetField(0);
4702          oss.str("");
4703          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4704          result2 = conn->Query(oss.str().c_str());
4705          if ( !result2 ) throw -4;;
4706          row2 = result2->Next();
4707          thisf = (TString)row2->GetField(0);
4708          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4709          test = 1;
4710        };
4711        //
4712        if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
4713          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4714          printf(" CHECK n.2 TIME SCREW of %u s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
4715          TString prevf = "";
4716          TString thisf = "";
4717          oss.str("");
4718          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4719          result2 = conn->Query(oss.str().c_str());
4720          if ( !result2 ) throw -4;;
4721          row2 = result2->Next();
4722          prevf = (TString)row2->GetField(0);
4723          oss.str("");
4724          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4725          result2 = conn->Query(oss.str().c_str());
4726          if ( !result2 ) throw -4;;
4727          row2 = result2->Next();
4728          thisf = (TString)row2->GetField(0);
4729          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4730          test = 1;
4731        };
4732        //
4733        if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
4734          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4735          printf(" CHECK n.3 TIME SCREW of %u s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
4736          TString prevf = "";
4737          TString thisf = "";
4738          oss.str("");
4739          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4740          result2 = conn->Query(oss.str().c_str());
4741          if ( !result2 ) throw -4;;
4742          row2 = result2->Next();
4743          prevf = (TString)row2->GetField(0);
4744          oss.str("");
4745          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4746          result2 = conn->Query(oss.str().c_str());
4747          if ( !result2 ) throw -4;;
4748          row2 = result2->Next();
4749          thisf = (TString)row2->GetField(0);
4750          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4751          test = 1;
4752        };
4753    
4754        //
4755        prevrht = thisrht;
4756        prevrtt = thisrtt;
4757        previd = thisid;
4758        prevl0id = thisl0id;
4759        row = result->Next();
4760      };
4761      //
4762      return(test);
4763      //
4764    };

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.37

  ViewVC Help
Powered by ViewVC 1.1.23