/[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.31 by mocchiut, Mon Apr 30 08:16:19 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 %i pkt_num %i \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(1);
408    //    //
409    if ( !nevent ) return(2);    if ( !nevent ) return(2);
410    //    //
411    if ( nevent < 2 ) return(4);    if ( nevent < 2 ) return(4);
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 %i 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 > 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: ob 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",(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 > (pobtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
893        if ( IsDebug() ) printf(" rise down pktnum %lld \n", (Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
894        return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
895    };    };
896    //    //
897    return((ULong64_t)obt);    if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);
898      return((Long64_t)obt);
899  };  };
900    
901  /**  /**
# Line 825  Int_t PamelaDBOperations::insertPamelaRa Line 1010  Int_t PamelaDBOperations::insertPamelaRa
1010    if ( idr ) return(1);    if ( idr ) return(1);
1011    //    //
1012    oss.str("");    oss.str("");
1013    oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"    if ( STATIC ){
1014        << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";      oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
1015            << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";
1016      } else {
1017        oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')";
1018      };
1019    if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;    if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1020    //    //
1021    idr =  this->SetID_RAW();    idr =  this->SetID_RAW();
# Line 851  Int_t PamelaDBOperations::insertPamelaGL Line 1040  Int_t PamelaDBOperations::insertPamelaGL
1040    if ( this->GetID_RAW() == 0 )  throw -11;    if ( this->GetID_RAW() == 0 )  throw -11;
1041    //    //
1042    oss.str("");    oss.str("");
1043    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='"
1044        << id << " AND TO_ID_RAW >= "        << this->GetRawFile().Data() << "';";
       << id << ";";  
1045    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1046    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1047    if ( !result ) throw -10;    if ( !result ) throw -10;
1048    row = result->Next();    row = result->Next();
   if ( !row ) throw -10;  
1049    //    //
1050    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();    if ( !row ){
1051        oss.str("");
1052        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< "
1053            << dworbit << " order by FROM_ORBIT desc limit 1;";
1054        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1055        result = conn->Query(oss.str().c_str());
1056        if ( !result ) throw -10;
1057        row = result->Next();
1058        if ( !row ) throw -10;
1059      };
1060      //
1061      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);
1062      t0 = (UInt_t)tu.GetSec();
1063      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));
1064      //
1065    /*    /*
1066     * Verify that the TIMESYNC have been not already processed     * Verify that the TIMESYNC have been not already processed
1067     */     */
# Line 875  Int_t PamelaDBOperations::insertPamelaGL Line 1076  Int_t PamelaDBOperations::insertPamelaGL
1076    if (result == NULL) throw -10;    if (result == NULL) throw -10;
1077    row = result->Next();    row = result->Next();
1078    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
1079        if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1080      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;
1081        //
1082        tsync = (UInt_t)atoll(row->GetField(2));
1083        obt0 = (UInt_t)atoll(row->GetField(1));
1084        //
1085      return(1);      return(1);
1086    };    };
1087    //    //
# Line 932  Int_t PamelaDBOperations::insertPamelaGL Line 1138  Int_t PamelaDBOperations::insertPamelaGL
1138          //          //
1139          TYPE = 55;//224;          TYPE = 55;//224;
1140          //          //
1141            if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
1142            //
1143          if ( TSYNC && OBT ){          if ( TSYNC && OBT ){
1144            existsts = true;            existsts = true;
1145            goto eout;            goto eout;
# Line 954  Int_t PamelaDBOperations::insertPamelaGL Line 1162  Int_t PamelaDBOperations::insertPamelaGL
1162      //      //
1163      rt->SetBranchAddress("RunTrailer", &runt);      rt->SetBranchAddress("RunTrailer", &runt);
1164      //      //
1165      if ( rhev > 0 ){      Int_t nrhev = rh->GetEntries();
1166        rh->GetEntry(0);      Int_t nrtev = rt->GetEntries();
1167        //      if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev);
1168        TSYNC = runh->LAST_TIME_SYNC_INFO;      //
1169        OBT = runh->OBT_TIME_SYNC * 1000;      if ( nrhev > 0 ){
1170        //        for (Int_t i=0; i<nrhev; i++){
1171        TYPE = 20;          //
1172        //          rh->GetEntry(i);
1173        if ( TSYNC && OBT ){          //
1174          existsts = true;          TSYNC = runh->LAST_TIME_SYNC_INFO;
1175          goto eout;          OBT = runh->OBT_TIME_SYNC * 1000;
1176            //
1177            TYPE = 20;
1178            //
1179            if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
1180            //
1181            if ( TSYNC && OBT ){
1182              existsts = true;
1183              goto eout;
1184            };
1185        };        };
1186        //        //
1187      };      };
1188      if ( rtev > 0 ){      if ( nrtev > 0 ){
1189        //        //
1190        if ( IsDebug() ) printf(" No runheader \n");        if ( IsDebug() ) printf(" No runheader \n");
1191        signal = 6;        signal = 6;
1192        //        //
1193        rt->GetEntry(0);        for (Int_t i=0; i<nrtev; i++){
1194        //          //
1195        TSYNC = runt->LAST_TYME_SYNC_INFO;          rt->GetEntry(i);
1196        OBT = runt->OBT_TYME_SYNC * 1000;          //
1197        //          TSYNC = runt->LAST_TYME_SYNC_INFO;
1198        TYPE = 21;          OBT = runt->OBT_TYME_SYNC * 1000;
1199        //          //
1200        if ( TSYNC && OBT ){          TYPE = 21;
1201          existsts = true;          //
1202          goto eout;          if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
1203            //
1204            if ( TSYNC && OBT ){
1205              existsts = true;
1206              goto eout;
1207            };
1208        };        };
1209        //        //
1210      } else {      } else {
# Line 1053  Int_t PamelaDBOperations::insertPamelaGL Line 1275  Int_t PamelaDBOperations::insertPamelaGL
1275    conn->Query(oss.str().c_str());    conn->Query(oss.str().c_str());
1276    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());
1277    //    //
1278      if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1279    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1280    //    //
1281      tsync = TSYNC;
1282      obt0 = OBT;
1283      //
1284    delete result;    delete result;
1285    return(signal);    return(signal);
1286  }  }
# Line 1070  Int_t PamelaDBOperations::insertPamelaRo Line 1296  Int_t PamelaDBOperations::insertPamelaRo
1296    UInt_t idtimesync = 0;    UInt_t idtimesync = 0;
1297    //    //
1298    oss.str("");    oss.str("");
1299    oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "    if ( STATIC ){
1300        << " LEFT JOIN GL_ROOT "      oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1301        << " ON GL_RAW.ID = GL_ROOT.ID_RAW "          << " LEFT JOIN GL_ROOT "
1302        << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "          << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1303        << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";          << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1304            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1305      } else {
1306        oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1307            << " LEFT JOIN GL_ROOT "
1308            << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1309            << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "
1310            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1311      };
1312    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1313    //    //
1314    if ( !result ) throw -12;    if ( !result ) throw -12;
# Line 1101  Int_t PamelaDBOperations::insertPamelaRo Line 1335  Int_t PamelaDBOperations::insertPamelaRo
1335    idtimesync = (UInt_t)atoll(row->GetField(0));    idtimesync = (UInt_t)atoll(row->GetField(0));
1336    //    //
1337    oss.str("");    oss.str("");
1338    oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"    if ( STATIC ){
1339        << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";      oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1340            << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";
1341      } else {
1342        oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1343            << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')";
1344      };
1345    //    //
1346    if (conn->Query(oss.str().c_str()) == 0) throw -4;    if (conn->Query(oss.str().c_str()) == 0) throw -4;
1347    //    //
# Line 1129  Int_t PamelaDBOperations::assignBOOT_NUM Line 1368  Int_t PamelaDBOperations::assignBOOT_NUM
1368    TSQLResult *result = 0;    TSQLResult *result = 0;
1369    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
1370    oss.str("");    oss.str("");
1371    oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "    if ( STATIC ){
1372        << " PATH = '" << this->GetRawPath().Data() << "' AND "      oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1373        << " NAME = '" << this->GetRawFile().Data() << "' ";          << " PATH = '" << this->GetRawPath().Data() << "' AND "
1374            << " NAME = '" << this->GetRawFile().Data() << "' ";
1375      } else {
1376        oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1377            << " PATH = '$PAM_RAW' AND "
1378            << " NAME = '" << this->GetRawFile().Data() << "' ";
1379      };
1380    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1381    //    //
1382    if ( !result ) throw -4;;    if ( !result ) throw -4;;
# Line 1154  Int_t PamelaDBOperations::assignBOOT_NUM Line 1399  Int_t PamelaDBOperations::assignBOOT_NUM
1399    VarDumpEvent  *vde = 0;    VarDumpEvent  *vde = 0;
1400    VarDumpRecord *vdr = 0;    VarDumpRecord *vdr = 0;
1401    //    //
1402      Bool_t found = false;
1403    trDumpEv->SetBranchAddress("VarDump", &vde);    trDumpEv->SetBranchAddress("VarDump", &vde);
1404    if ( trDumpEv->GetEntries() > 0 ){    if ( trDumpEv->GetEntries() > 0 ){
1405      Bool_t found = false;      found = false;
1406      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1407        trDumpEv->GetEntry(i);        trDumpEv->GetEntry(i);
1408        vde->Records->GetEntries();        //      vde->Records->GetEntries();
1409        if ( vde->Records->GetEntries()>0 ){        if ( vde->Records->GetEntries()>5 ){
1410          found = true;          found = true;
1411          goto fill;          goto fill;
1412        };        };
# Line 1173  Int_t PamelaDBOperations::assignBOOT_NUM Line 1419  Int_t PamelaDBOperations::assignBOOT_NUM
1419        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1420        //        //
1421      } else {      } else {
1422        if ( !this->GetBOOTnumber() ) return(4);        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4);
1423        };
1424      } else {
1425        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2);
1426      };
1427      //
1428      UInt_t bn = 0;
1429      Bool_t afound = false;
1430      if ( !found && this->AutoBoot()){
1431        afound = true;
1432        //
1433        // Search for other files with similar timesync
1434        //
1435        if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0);
1436        UInt_t upperts = tsync-(obt0/1000)+5;
1437        UInt_t lowerts = tsync-(obt0/1000)-5;
1438        oss.str("");
1439        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)<"
1440            << upperts
1441            << " AND TIMESYNC-(OBT0/1000)>"
1442            << lowerts
1443            << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
1444        result = conn->Query(oss.str().c_str());
1445        if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str());
1446        //
1447        if ( !result ) throw -4;;
1448        found = true;
1449        if ( result->GetRowCount()<3 ){
1450          if ( IsDebug() ) printf(" AGH! no results!\n");
1451          found = false;
1452        } else {
1453          row = result->Next();      
1454          bn = (UInt_t)atoll(row->GetField(0));
1455          for ( Int_t r=1; r<result->GetRowCount() ;r++){
1456            if ( !row ) throw -4;
1457            if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0));
1458            if ( bn != (UInt_t)atoll(row->GetField(0)) ){
1459              if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0)));
1460              found = false;
1461            };
1462            row = result->Next();
1463          };
1464      };      };
1465      };
1466      //
1467      Int_t sgn = 0;
1468      //
1469      if ( !found && !BOOTNO ){
1470        throw -29;
1471    } else {    } else {
1472      if ( !this->GetBOOTnumber() ) return(2);      if ( afound ){
1473          this->SetBOOTnumber(bn);
1474          sgn = 8;
1475        };
1476    };    };
1477    //    //
1478    oss.str("");    oss.str("");
# Line 1186  Int_t PamelaDBOperations::assignBOOT_NUM Line 1482  Int_t PamelaDBOperations::assignBOOT_NUM
1482    conn->Query(oss.str().c_str());          conn->Query(oss.str().c_str());      
1483    //    //
1484    delete result;    delete result;
1485    return(0);    return(sgn);
1486  };  };
1487    
1488  /**  /**
# Line 1449  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1745  Bool_t PamelaDBOperations::IsRunAlreadyI
1745      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1746        //        //
1747        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");
1748        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1749        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1750        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) <<";";
1751        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1752    //       conn->Query(oss.str().c_str());
1753        if ( signal ) signal = false;        if ( signal ) signal = false;
1754        goto gonext;              goto gonext;      
1755        //        //
# Line 1466  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1763  Bool_t PamelaDBOperations::IsRunAlreadyI
1763        //        //
1764        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");
1765        //        //
1766        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1767        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1768        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) <<";";
1769        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1770    //       conn->Query(oss.str().c_str());
1771        //        //
1772        if ( signal ) signal = false;        if ( signal ) signal = false;
1773        goto gonext;        goto gonext;
# Line 1483  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1781  Bool_t PamelaDBOperations::IsRunAlreadyI
1781        //        //
1782        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");
1783        //        //
1784        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1785        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1786        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) <<";";
1787        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1788    //       conn->Query(oss.str().c_str());
1789        if ( signal ) signal = false;        if ( signal ) signal = false;
1790        //        //
1791      };      };
# Line 1501  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1800  Bool_t PamelaDBOperations::IsRunAlreadyI
1800    delete result;    delete result;
1801    //    //
1802    if ( signal && IsDebug() ) printf(" The run has already been inserted \n");        if ( signal && IsDebug() ) printf(" The run has already been inserted \n");    
1803      if ( !signal && IsDebug() ) printf(" The run existed and was deleted, fill the DB \n");
1804    return(signal);    return(signal);
1805  };  };
1806    
# Line 1522  void PamelaDBOperations::HandleRun(){   Line 1822  void PamelaDBOperations::HandleRun(){  
1822      //      //
1823      this->FillClass();      this->FillClass();
1824      //      //
1825      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);      if ( !IsRunAlreadyInserted() ){
1826          glrun->SetID(this->AssignRunID());
1827          glrun->SetID_RUN_FRAG(0);
1828          glrun->Fill_GL_RUN(conn);
1829        };
1830    } else {    } else {
1831      //      //
1832      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 1849  void PamelaDBOperations::HandleRunFragme
1849    UInt_t rhfirstev = firstev;    UInt_t rhfirstev = firstev;
1850    UInt_t rtlastev = lastev;    UInt_t rtlastev = lastev;
1851    Bool_t found = false;    Bool_t found = false;
1852      Bool_t foundinrun = false;
1853    //    //
1854    TSQLResult *result = 0;    TSQLResult *result = 0;
1855    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 1571  void PamelaDBOperations::HandleRunFragme Line 1876  void PamelaDBOperations::HandleRunFragme
1876    //    //
1877    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");
1878    //    //
1879    // 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...
1880    //        //
1881    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("");
1882                                             // missing it no way we can found a piece in the frag table    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1883      //        << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1884      oss.str("");        << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1885      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 ("
1886          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "        << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1887          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME()        << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1888          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!        << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1889      //        << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1890      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());        << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1891      result = conn->Query(oss.str().c_str());        << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1892      //        << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1893      if ( !result ) throw -4;        << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1894      //        << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1895      row = result->Next();        << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1896      //    //
1897      if ( !row ){    if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1898        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");    result = conn->Query(oss.str().c_str());
1899      //
1900      if ( !result ) throw -4;
1901      //
1902      row = result->Next();
1903      //
1904      if ( !row ){
1905        //
1906        // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
1907        //
1908        if ( IsDebug() ) printf(" The run is new \n");
1909        if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
1910        //
1911        glrun->SetID(this->AssignRunID());
1912        glrun->SetID_RUN_FRAG(0);
1913        glrun->Fill_GL_RUN_FRAGMENTS(conn);
1914        //
1915      } else {
1916        if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
1917        return;
1918      };
1919      //
1920      //
1921      // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
1922      //    
1923      if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
1924                                               // missing it no way we can found a piece in the frag table
1925        //
1926        oss.str("");
1927        oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1928            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1929            << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1930            << " ID != " << glrun->ID
1931            << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1932        //
1933        if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
1934        result = conn->Query(oss.str().c_str());
1935        //
1936        if ( !result ) throw -4;
1937        //
1938        row = result->Next();
1939        //
1940        if ( !row && NoFrag() ){
1941          //
1942          oss.str("");
1943          oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
1944              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1945              << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1946              << " ID != " << glrun->ID
1947              << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1948          //
1949          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1950          result = conn->Query(oss.str().c_str());
1951          //
1952          if ( !result ) throw -4;
1953          //
1954          foundinrun = true;
1955          //
1956          row = result->Next();
1957          //
1958        };
1959        //
1960        if ( !row ){
1961          if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1962        found = false;        found = false;
1963      } else {      } else {
1964        //        //
# Line 1663  void PamelaDBOperations::HandleRunFragme Line 2031  void PamelaDBOperations::HandleRunFragme
2031        //        //
2032        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 ");
2033        //        //
2034          if ( foundinrun ){
2035            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2036            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2037          };
2038          //
2039        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
2040        //        //
2041        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2042        //        //
2043        oss.str("");        oss.str("");
2044        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1694  void PamelaDBOperations::HandleRunFragme Line 2067  void PamelaDBOperations::HandleRunFragme
2067        };        };
2068        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: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2069        //        //
       glrun1->SetID(0);  
2070        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2071        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
2072        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
# Line 1703  void PamelaDBOperations::HandleRunFragme Line 2075  void PamelaDBOperations::HandleRunFragme
2075        //        //
2076        glrun->SetEV_FROM(firstev);        glrun->SetEV_FROM(firstev);
2077        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
2078          //
2079        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
2080        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
2081        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
# Line 1722  void PamelaDBOperations::HandleRunFragme Line 2095  void PamelaDBOperations::HandleRunFragme
2095        //        //
2096        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2097          //          //
2098            //      glrun->SetID(this->AssignRunID());
2099            glrun->SetID_RUN_FRAG(glrun1->GetID());
2100          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
2101          //          //
2102          // 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);  
2103          //          //
2104            glrun1->SetID_RUN_FRAG(glrun->GetID());
2105          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2106          //          //
         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;  
         //  
2107        };        };
       //  
       delete glrun1;  
       //  
2108        // delete old entry in fragment table        // delete old entry in fragment table
2109        //        //
2110        oss.str("");        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2111          glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2112        //        //
2113        oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";        delete glrun1;
2114        //        //
       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;  
2115        //        //
2116        return;        return;
2117        //        //
# Line 1791  void PamelaDBOperations::HandleRunFragme Line 2120  void PamelaDBOperations::HandleRunFragme
2120    };    };
2121    //    //
2122    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
2123                                             // 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
2124      //      //
2125      oss.str("");      oss.str("");
2126      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 "
2127          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2128          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME()          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2129            << " ID != " << glrun->ID
2130          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2131      //      //
2132      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 2136  void PamelaDBOperations::HandleRunFragme
2136      //      //
2137      row = result->Next();      row = result->Next();
2138      //      //
2139        if ( !row && NoFrag() ){
2140          //
2141          oss.str("");
2142          oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
2143              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2144              << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2145              << " ID != " << glrun->ID
2146              << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2147          //
2148          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2149          result = conn->Query(oss.str().c_str());
2150          //
2151          if ( !result ) throw -4;
2152          //
2153          foundinrun = true;
2154          row = result->Next();
2155          //
2156        };
2157        //
2158      if ( !row ){      if ( !row ){
2159        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2160        found = false;        found = false;
# Line 1879  void PamelaDBOperations::HandleRunFragme Line 2228  void PamelaDBOperations::HandleRunFragme
2228        //        //
2229        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 ");
2230        //        //
2231          if ( foundinrun ){
2232            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2233            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2234          };
2235          //
2236        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
2237        //        //
2238        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2239        //        //
2240        oss.str("");        oss.str("");
2241        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1918  void PamelaDBOperations::HandleRunFragme Line 2272  void PamelaDBOperations::HandleRunFragme
2272        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
2273        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
2274        //        //
       glrun1->SetID(0);  
2275        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
2276        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
2277        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
# Line 1938  void PamelaDBOperations::HandleRunFragme Line 2291  void PamelaDBOperations::HandleRunFragme
2291        //        //
2292        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2293          //          //
2294          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();  
2295          //          //
2296          UInt_t idrun0 = 0;          glrun->SetID_RUN_FRAG(glrun1->GetID());
2297          if ( !row ){          glrun->Fill_GL_RUN(conn);
           throw -10;  
         } else {  
           idrun0 = (UInt_t)atoll(row->GetField(0));  
         };  
2298          //          //
2299          glrun1->SetID_RUN_FRAG(idrun0);          // set id number
2300          //          //
2301            glrun1->SetID_RUN_FRAG(glrun->GetID());
2302          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2303          //          //
         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;  
         //  
2304        };        };
2305        //        //
2306        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 << ";";  
2307        //        //
2308        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");
2309        result =  conn->Query(oss.str().c_str());        glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
       if ( !result ) throw -4;  
2310        //        //
2311          delete glrun1;
2312        //        //
2313        return;        return;
2314        //        //
# Line 2036  void PamelaDBOperations::HandleRunFragme Line 2345  void PamelaDBOperations::HandleRunFragme
2345      //      //
2346      row = result->Next();      row = result->Next();
2347      //      //
2348      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 {  
2349        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");
2350        } else {
2351          if ( NoFrag() ){
2352            glrun->SetID_RUN_FRAG(glrun->GetID());
2353            glrun->Fill_GL_RUN(conn);      
2354            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2355          };    
2356      };      };
2357    };    };
2358    //    //
# Line 2100  void PamelaDBOperations::HandleMissingHo Line 2378  void PamelaDBOperations::HandleMissingHo
2378      //      //
2379      this->FillClass(mishead,mistrail,firstev,lastev);      this->FillClass(mishead,mistrail,firstev,lastev);
2380      //      //
2381      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2382          glrun->SetID(this->AssignRunID());
2383          glrun->SetID_RUN_FRAG(0);
2384          glrun->Fill_GL_RUN(conn);    
2385        };
2386      //      //
2387    };    };
2388    //    //
# Line 2267  Bool_t PamelaDBOperations::IsRunConsiste Line 2549  Bool_t PamelaDBOperations::IsRunConsiste
2549            //                  //      
2550            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2551            //            //
2552            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2553                glrun->SetID(this->AssignRunID());
2554                glrun->SetID_RUN_FRAG(0);
2555                glrun->Fill_GL_RUN(conn);      
2556              };
2557            //            //
2558            firstevno = lastentry + 1;            firstevno = lastentry + 1;
2559            //            //
# Line 2343  void PamelaDBOperations::HandleSuspiciou Line 2629  void PamelaDBOperations::HandleSuspiciou
2629      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2630      //      //
2631      this->FillClass();      this->FillClass();
2632      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2633          glrun->SetID(this->AssignRunID());
2634          glrun->SetID_RUN_FRAG(0);
2635          glrun->Fill_GL_RUN(conn);    
2636        };
2637      //      //
2638    } else {    } else {
2639      //      //
# Line 2371  void PamelaDBOperations::HandleSuspiciou Line 2661  void PamelaDBOperations::HandleSuspiciou
2661        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");
2662        //        //
2663        this->FillClass();        this->FillClass();
2664        if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                if ( !IsRunAlreadyInserted() ){
2665            glrun->SetID(this->AssignRunID());
2666            glrun->SetID_RUN_FRAG(0);
2667            glrun->Fill_GL_RUN(conn);        
2668          };
2669        //        //
2670      } else {      } else {
2671        //        //
# Line 2469  void PamelaDBOperations::HandleSuspiciou Line 2763  void PamelaDBOperations::HandleSuspiciou
2763            //                  //      
2764            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2765            //            //
2766            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2767                glrun->SetID(this->AssignRunID());
2768                glrun->SetID_RUN_FRAG(0);
2769                glrun->Fill_GL_RUN(conn);      
2770              };
2771            //            //
2772            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...
2773              //              //
# Line 2504  void PamelaDBOperations::HandleSuspiciou Line 2802  void PamelaDBOperations::HandleSuspiciou
2802              //                  //    
2803              this->SetCommonGLRUN(firstTime,lastTime);              this->SetCommonGLRUN(firstTime,lastTime);
2804              //              //
2805              if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                    if ( !IsRunAlreadyInserted() ){
2806                  glrun->SetID(this->AssignRunID());
2807                  glrun->SetID_RUN_FRAG(0);
2808                  glrun->Fill_GL_RUN(conn);      
2809                };
2810            };            };
2811            //            //
2812            firstevno = lastentry + 1;            firstevno = lastentry + 1;
# Line 2611  Int_t PamelaDBOperations::insertCALO_CAL Line 2913  Int_t PamelaDBOperations::insertCALO_CAL
2913                // no calibrations in the db contain our calibration                // no calibrations in the db contain our calibration
2914                //                //
2915                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);
2916                if ( fromtime < 1150863400 ){                if ( fromtime < 1150871000 ){ //1150866904
2917                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
2918                  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
2919                };                };
# Line 2674  Int_t PamelaDBOperations::insertCALO_CAL Line 2976  Int_t PamelaDBOperations::insertCALO_CAL
2976            //            //
2977          } else {          } else {
2978            //            //
2979            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 %i obt %i pkt %i \n",section,fromtime,obt,pkt);
2980            //            //
2981          };                };      
2982          //                //      
# Line 2756  void PamelaDBOperations::HandleTRK_CALIB Line 3058  void PamelaDBOperations::HandleTRK_CALIB
3058        // no calibrations in the db contain our calibration        // no calibrations in the db contain our calibration
3059        //        //
3060        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");
3061        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
3062        //        //
3063        oss.str("");        oss.str("");
3064        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 3187  Int_t PamelaDBOperations::insertTRK_CALI
3187      pkt1 = ph1->GetCounter();        pkt1 = ph1->GetCounter();  
3188      fromtime = this->GetAbsTime(ph1->GetOrbitalTime());        fromtime = this->GetAbsTime(ph1->GetOrbitalTime());  
3189      //      //
3190      valid = 1;  //     valid = 1;
3191      //  //     //
3192      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
3193      //      //
3194      //      //
3195      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) ){
3196        //        //
3197        if ( IsDebug() ) printf(" Trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);        if ( IsDebug() ) printf(" Trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);
3198          //      
3199          valid = ValidateTrkCalib( caltrk1, eh1 );
3200          if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
3201        //        //
3202        // Do we have the second calibration packet?        // Do we have the second calibration packet?
3203        //        //
# Line 2909  Int_t PamelaDBOperations::insertTRK_CALI Line 3214  Int_t PamelaDBOperations::insertTRK_CALI
3214            obt2 = ph2->GetOrbitalTime();              obt2 = ph2->GetOrbitalTime();  
3215            pkt2 = ph2->GetCounter();              pkt2 = ph2->GetCounter();  
3216            //            //
3217            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
3218            //                  //      
3219          } else {          } else {
3220            //            //
# Line 2940  Int_t PamelaDBOperations::insertTRK_CALI Line 3245  Int_t PamelaDBOperations::insertTRK_CALI
3245          //          //
3246          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 %i pkt %i t2 is %i is good \n",obt2,pkt2,t2);
3247          //          //
3248            UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
3249            if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
3250            valid = valid & valid2;
3251            //
3252          // Handle good calib          // Handle good calib
3253          //          //
3254          this->HandleTRK_CALIB(true,true);          this->HandleTRK_CALIB(true,true);
# Line 3112  Int_t PamelaDBOperations::insertS4_CALIB Line 3421  Int_t PamelaDBOperations::insertS4_CALIB
3421            // no calibrations in the db contain our calibration            // no calibrations in the db contain our calibration
3422            //            //
3423            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");
3424            if ( fromtime < 1150863400 ){            if ( fromtime < 1150871000 ){  
3425              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
3426              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
3427            };            };
# Line 3185  Int_t PamelaDBOperations::insertS4_CALIB Line 3494  Int_t PamelaDBOperations::insertS4_CALIB
3494   * 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
3495   */   */
3496  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3497      return(this->CleanGL_RUN_FRAGMENTS(""));
3498    };
3499    
3500    /**
3501     * Scan the fragment table and move old fragments to the GL_RUN table
3502     */
3503    Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3504    //    //
3505    TSQLResult *result = 0;    TSQLResult *result = 0;
3506    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 3192  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3508  Int_t PamelaDBOperations::CleanGL_RUN_FR
3508    TSQLRow    *row2   = 0;    TSQLRow    *row2   = 0;
3509    //    //
3510    UInt_t moved = 0;    UInt_t moved = 0;
 //  UInt_t timelim = 0;  
 //  TDatime *time = new TDatime();  
3511    //    //
3512    stringstream oss;    stringstream oss;
3513    oss.str("");    oss.str("");
3514    //    //
3515    //    if ( !strcmp(fcleanfile.Data(),"") ){
3516    // check if there are entries older than "olderthan" seconds from now      //
3517    //      // check if there are entries older than "olderthan" seconds from now
3518    oss.str("");      //
3519    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"      oss.str("");
3520        << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3521    //          << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3522    if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());      //
3523    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());
3524        result = conn->Query(oss.str().c_str());
3525        //
3526      } else {
3527        oss.str("");
3528        oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3529        if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3530        result = conn->Query(oss.str().c_str());
3531        //    
3532        if ( result ){
3533          //
3534          row = result->Next();
3535          //
3536          if ( row ){
3537            oss.str("");
3538            oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3539                << " ID_ROOT_L0=" << row->GetField(0) << ";";
3540            //
3541            if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3542            result = conn->Query(oss.str().c_str());
3543            //    
3544          };
3545        } else {
3546          return(2);
3547        };
3548      };
3549    //    //
3550    if ( result ){    if ( result ){
3551      //      //
3552      row = result->Next();      row = result->Next();
3553      //      //
3554      if ( row ){      while ( row ){
3555        //        //
3556        oss.str("");        oss.str("");
3557        oss << " ID= "<< row->GetField(0);        oss << " ID= "<< row->GetField(0);
# Line 3247  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3586  Int_t PamelaDBOperations::CleanGL_RUN_FR
3586          if ( IsDebug() ) printf(" The run is new \n");          if ( IsDebug() ) printf(" The run is new \n");
3587          if ( IsDebug() ) printf(" -> fill the DB \n");                if ( IsDebug() ) printf(" -> fill the DB \n");      
3588          //          //
3589          glrun->SetID(0);          //      glrun->SetID(this->AssignRunID()); we use the old run number!
3590            glrun->SetID_RUN_FRAG(glrun->GetID());
3591          glrun->Fill_GL_RUN(conn);            glrun->Fill_GL_RUN(conn);  
3592          //          //
3593          oss.str("");  //      oss.str("");
3594          oss << " SELECT ID FROM GL_RUN WHERE "  //      oss << " SELECT ID FROM GL_RUN WHERE "
3595              << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "  //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3596              << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "  //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3597              << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "  //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3598              << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "  //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3599              << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";  //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3600          //  //      //
3601          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());
3602          result2 = conn->Query(oss.str().c_str());  //      result2 = conn->Query(oss.str().c_str());
3603          //  //      //
3604          if ( !result2 ) throw -4;  //      if ( !result2 ) throw -4;
3605          //  //      //
3606          row2 = result2->Next();  //      row2 = result2->Next();
3607          //  //      //
3608          if ( !row2 ) throw -25;  //      if ( !row2 ) throw -25;
3609          //  //      //
3610          oss.str("");  //      oss.str("");
3611          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);
3612          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());
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          moved++;          moved++;
3618          //          //
# Line 3282  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3622  Int_t PamelaDBOperations::CleanGL_RUN_FR
3622        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));      
3623        //        //
3624        //        //
3625        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3626        oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);  //      oss.str("");
3627        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);
3628        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());
3629        //  //       result2 = conn->Query(oss.str().c_str());
3630        if ( !result2 ) throw -4;  //      //
3631        //  //      if ( !result2 ) throw -4;
3632    //      //
3633          row = result->Next();
3634      };      };
3635    };    };
3636    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 3641  Int_t PamelaDBOperations::CleanGL_RUN_FR
3641   * 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..
3642   */   */
3643  Int_t PamelaDBOperations::ValidateRuns(){  Int_t PamelaDBOperations::ValidateRuns(){
3644      return(this->ValidateRuns(""));
3645    };
3646    
3647    /**
3648     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3649     */
3650    Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3651    //    //
3652    TSQLResult *result = 0;    TSQLResult *result = 0;
3653    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
3654    //    //
3655      UInt_t calibtime = 50;
3656      //
3657    stringstream oss;    stringstream oss;
3658    oss.str("");    oss.str("");
3659    //    //
# Line 3311  Int_t PamelaDBOperations::ValidateRuns() Line 3662  Int_t PamelaDBOperations::ValidateRuns()
3662    // =======================================================    // =======================================================
3663    UInt_t t_stop  = 0;    UInt_t t_stop  = 0;
3664    UInt_t t_start = 0;    UInt_t t_start = 0;
3665    // --------------------------------------------------------------    if ( !strcmp(valfile.Data(),"") ) {
3666    // 1) get the OBT of the last run inserted after clean-time limit      // --------------------------------------------------------------
3667    // --------------------------------------------------------------      // 1) get the OBT of the last run inserted after clean-time limit
3668    oss.str("");      // --------------------------------------------------------------
3669    oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()      oss.str("");
3670            << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3671    if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());          << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3672    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3673    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3674    if ( !result->GetRowCount() ) {      if ( !result ) throw -4;
3675            printf(" No runs to validate \n");      if ( !result->GetRowCount() ) {
3676            return(1);        printf(" No runs to validate \n");
3677    }else{        return(1);
3678          row = result->Next();      }else{
3679          t_start = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3680    };          t_start = (UInt_t)atoll(row->GetField(4));
3681    // --------------------------------------------------------------      };  
3682    // 2) get the OBT of the last validated run      // --------------------------------------------------------------
3683    // --------------------------------------------------------------      // 2) get the OBT of the last validated run
3684    oss.str("");      // --------------------------------------------------------------
3685    oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start      oss.str("");
3686        <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3687    if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3688    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3689    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3690    if ( result->GetRowCount() ){      if ( !result ) throw -4;
3691            row = result->Next();      if ( result->GetRowCount() ){
3692            t_stop = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3693    };        t_stop = (UInt_t)atoll(row->GetField(4));
3694    if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);      };
3695    // --------------------------------------------------------------      if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);
3696    // now retrieves runs to be validated      // --------------------------------------------------------------
3697    // --------------------------------------------------------------      // now retrieves runs to be validated
3698    oss.str("");      // --------------------------------------------------------------
3699    oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;      oss.str("");
3700    oss << " AND RUNHEADER_TIME >="<< t_stop;      oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;
3701    oss << " ORDER BY RUNHEADER_TIME DESC;";      oss << " AND RUNHEADER_TIME >="<< t_stop;
3702  //  if ( IsDebug() )      oss << " ORDER BY RUNHEADER_TIME DESC;";
3703    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());
3704    result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
3705    if ( !result ) throw -4;    } else {
3706    if ( !result->GetRowCount() ) printf(" No runs to validate \n");      //
3707  //  printf("------------------------------------------------------------------------------- \n");      stringstream myquery;
3708          UInt_t myid = 0;
3709    Int_t nrow = 0;      myquery.str("");
3710    GL_RUN* this_run = new GL_RUN();      myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3711    GL_RUN* next_run = new GL_RUN();      //
3712    Int_t   nseq_max = 1000;      result = conn->Query(myquery.str().c_str());
3713        //
3714        row = result->Next();      
3715        if( !row ){
3716          if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3717            if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3718            return(2);
3719          };
3720          if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3721          return(0);
3722        };
3723        myid=(UInt_t)atoll(row->GetField(0));
3724        //
3725        myquery.str("");
3726        myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3727        //
3728        result = conn->Query(myquery.str().c_str());
3729        //
3730        row = result->Next();      
3731        if( !row->GetField(0) || !row->GetField(1)){
3732          //
3733          if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3734          //
3735          return(0);
3736          //
3737        } else {  
3738          //
3739          UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3740          UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3741          UInt_t caltime = 0;
3742          //
3743          myquery.str("");
3744          myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3745          myquery << " order by FROM_TIME asc limit 1;";
3746          //
3747          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3748          //
3749          //
3750          result = conn->Query(myquery.str().c_str());
3751          //
3752          row = result->Next();      
3753          if( !row ){
3754            caltime = runhtime;
3755          } else {
3756            caltime = (UInt_t)atoll(row->GetField(0));
3757          };
3758          //
3759          myquery.str("");
3760          myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3761          myquery << caltime << " order by RUNHEADER_TIME DESC";
3762          //
3763          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3764          //
3765          result = conn->Query(myquery.str().c_str());
3766          //
3767        };
3768      };
3769      //
3770      if ( !result ) throw -4;
3771      if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3772      //
3773      Int_t nrow = 0;
3774      GL_RUN* this_run = new GL_RUN();
3775      GL_RUN* next_run = new GL_RUN();
3776      Int_t   nseq_max = 1000;
3777  //  UInt_t* sequence = new UInt_t[100];  //  UInt_t* sequence = new UInt_t[100];
3778    vector<UInt_t> sequence(nseq_max);    vector<UInt_t> sequence(nseq_max);
3779    Int_t   nseq = 0;    Int_t   nseq = 0;
# Line 3368  Int_t PamelaDBOperations::ValidateRuns() Line 3783  Int_t PamelaDBOperations::ValidateRuns()
3783    UInt_t t1=0,t2=0;    UInt_t t1=0,t2=0;
3784    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
3785    // - loop over runs, back in time,    // - loop over runs, back in time,
3786    // - select sequences of runs close in time (less than 60 s apart),    // - select sequences of runs close in time (less than calibtime s apart),
3787    //   which could be preceeded by a calibration    //   which could be preceeded by a calibration
3788    // - check if there might be a missing calibration    // - check if there might be a missing calibration
3789    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
# Line 3408  Int_t PamelaDBOperations::ValidateRuns() Line 3823  Int_t PamelaDBOperations::ValidateRuns()
3823                                                        
3824                            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
3825                                                        
3826                            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
3827                                                        
3828                            if( !CHECK && this_run->VALIDATION ){                            if( !CHECK && this_run->VALIDATION ){
3829                                    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 3523  Int_t PamelaDBOperations::assignVALIDATI Line 3938  Int_t PamelaDBOperations::assignVALIDATI
3938    
3939    
3940    
3941    // Insert TLEs from file tlefilename in the table GL_TLE in the db
3942    // opened by conn, sorting them by date from older to newer, if each
3943    // TLE has not been alread inserted.
3944    Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
3945    {
3946      fstream tlefile(tlefilename, ios::in);
3947    
3948      if ( !tlefile ) throw -7;
3949    
3950      vector<cTle*> ctles;
3951      vector<cTle*>::iterator iter;
3952      int present = 0;
3953    
3954      // Get three lines from tlefile, create a cTle object and put it
3955      // into ctles
3956      while(1) {
3957        cTle *tlef;
3958        string str1, str2, str3;
3959    
3960        getline(tlefile, str1);
3961        if(tlefile.eof()) break;
3962    
3963        getline(tlefile, str2);
3964        if(tlefile.eof()) break;
3965    
3966        getline(tlefile, str3);
3967        if(tlefile.eof()) break;
3968    
3969        // We now have three good lines for a cTle.
3970        tlef = new cTle(str1, str2, str3);
3971        ctles.push_back(tlef);
3972      }
3973    
3974      tlefile.close();
3975    
3976      // Sort by date
3977      sort(ctles.begin(), ctles.end(), compTLE);
3978    
3979      // Now we insert each TLE into the db
3980      for(iter = ctles.begin(); iter != ctles.end(); iter++) {
3981        cTle *tle = *iter;
3982    
3983        // Do nothing if it's already present in the db.  Just increase
3984        // the counter present.
3985        if (! isTlePresent(tle))
3986          {
3987            int status = insertTle(tle);
3988    
3989            // Insert query failed.  Return 1.
3990            if(status == EXIT_FAILURE) {
3991              
3992              if( IsDebug() ) {
3993                cerr << "Error: inserting TLE:" << endl
3994                     << tle->getName() << endl
3995                     << tle->getLine1() << endl
3996                     << tle->getLine2() << endl;
3997              }
3998    
3999              throw -4;
4000              return 1;
4001            }
4002    
4003          }
4004        else
4005          present++;
4006    
4007      }
4008    
4009      int inserted = ctles.size() - present;  // Number of inserted TLE.
4010      if ( IsDebug() )
4011        cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
4012             << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
4013    
4014      ctles.clear();
4015    
4016    
4017      // Return 2 if no new TLE has been inserted.  0 otherwise.
4018      if(! inserted ) return 2;
4019      return 0;
4020    }
4021    
4022    
4023    // Insert tle in the table GL_TLE using the connection conn.
4024    Int_t PamelaDBOperations::insertTle(cTle *tle)
4025    {
4026      stringstream oss;
4027      TSQLResult *result = 0;
4028    
4029      oss.str("");
4030      oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
4031          << " VALUES ( '"
4032          << tle->getName() << "', '"
4033          << tle->getLine1() << "', '"
4034          << tle->getLine2() << "', '"
4035          << getTleDatetime(tle) << "')";
4036    
4037      //  cout << oss.str().c_str() << endl;
4038      result = conn->Query(oss.str().c_str());
4039      if (result == NULL)
4040        return EXIT_FAILURE;
4041    
4042      return EXIT_SUCCESS;
4043    }
4044    
4045    
4046    // Return whether tle is already in the db connected by conn.
4047    bool PamelaDBOperations::isTlePresent(cTle *tle)
4048    {
4049      stringstream oss;
4050      TSQLResult *result = 0;
4051    
4052      oss.str("");
4053      oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
4054          << getTleDatetime(tle) << "'";
4055    
4056      result = conn->Query(oss.str().c_str());
4057      if (result == NULL) throw -4;
4058    
4059      if (result->GetRowCount())
4060        return true;
4061      else
4062        return false;
4063    }
4064    
4065    
4066    // Return whether the first TLE is dated early than the second
4067    bool compTLE (cTle *tle1, cTle *tle2)
4068    {
4069      return getTleJulian(tle1) < getTleJulian(tle2);
4070    }
4071    
4072    
4073    // Return the date of the tle using the format (year-2000)*1e3 +
4074    // julian day.  e.g. 6365 is the 31th Dec 2006.
4075    // It does *not* return a cJulian date.
4076    float getTleJulian(cTle *tle) {
4077      return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
4078    }
4079    
4080    
4081    // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
4082    // format.
4083    string getTleDatetime(cTle *tle)
4084    {
4085      int year, mon, day, hh, mm, ss;
4086      double dom; // day of month (is double!)
4087      stringstream date; // date in datetime format
4088    
4089      // create a cJulian from the date in tle
4090      cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
4091    
4092      // get year, month, day of month
4093      jdate.getComponent(&year, &mon, &dom);
4094    
4095      // build a datetime YYYY-MM-DD hh:mm:ss
4096      date.str("");
4097      day = (int) floor(dom);
4098      hh = (int) floor( (dom - day) * 24);
4099      mm = (int) floor( ((dom - day) * 24 - hh) * 60);
4100      ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
4101      //  ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
4102    
4103      date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
4104    
4105      return date.str();
4106    }
4107    
4108    /**
4109     * Remove a file from the DB, delete on cascade all entries related to that file
4110     * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
4111     * calibration
4112     **/
4113    Int_t PamelaDBOperations::removeFile(TString remfile){
4114      //
4115      // Determine ID_ROOT_L0 and ID_RAW
4116      //
4117      TSQLResult *pResult;
4118      TSQLRow *Row;
4119      stringstream myquery;
4120      //  
4121      myquery.str("");
4122      myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
4123      //
4124      pResult = conn->Query(myquery.str().c_str());
4125      //
4126      Row = pResult->Next();      
4127      if( !Row ){
4128        if ( strcmp(remfile.Data(),GetRootName().Data()) ){
4129          if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
4130          return(1);
4131        };
4132        if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
4133        return(0);
4134      };
4135      //
4136      this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
4137      this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
4138      //
4139      this->ValidationOFF();
4140      //
4141      this->RemoveCALIBS();
4142      //
4143      this->RemoveRUNS();
4144      //
4145      this->RemoveFILES();
4146      //
4147      this->SetID_ROOT(0);
4148      this->SetID_RAW(0);
4149      //
4150      return(0);
4151    };
4152    
4153    /**
4154     *
4155     * Set validation bit to zero for runs following the removing file till
4156     * 1) a run with TRK_CALIB_USED=140
4157     * 2) a run with VALIDATION = 0
4158     * 3) the next calibration
4159     *
4160     **/
4161    void PamelaDBOperations::ValidationOFF(){
4162      TSQLResult *pResult;
4163      TSQLRow *Row;
4164      stringstream myquery;
4165      Int_t unv = 0;
4166      //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
4167      myquery.str("");
4168      myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4169      //
4170      pResult = conn->Query(myquery.str().c_str());
4171      //
4172      Row = pResult->Next();      
4173      if( !Row->GetField(0) ){
4174        //
4175        if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
4176        //
4177      } else {  
4178        //
4179        UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
4180        UInt_t caltime = 0;
4181        //
4182        myquery.str("");
4183        myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
4184        myquery << " order by FROM_TIME asc limit 1;";
4185        //
4186        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4187        //
4188        //
4189        delete pResult;
4190        pResult = conn->Query(myquery.str().c_str());
4191        //
4192        Row = pResult->Next();      
4193        if( !Row ){
4194          caltime = runhtime;
4195        } else {
4196          caltime = (UInt_t)atoll(Row->GetField(0));
4197        };
4198        //
4199        myquery.str("");
4200        myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4201        myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4202        //
4203        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4204        //
4205        pResult = conn->Query(myquery.str().c_str());
4206        //
4207        Row = pResult->Next();      
4208        if( !Row ){
4209          //
4210          if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4211          //
4212        } else {
4213          myquery.str("");
4214          myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4215          myquery << " RUNHEADER_TIME>=" <<runhtime;
4216          myquery << " order by RUNHEADER_TIME asc;";
4217          //
4218          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4219          //
4220          pResult = conn->Query(myquery.str().c_str());
4221          //
4222          Row = pResult->Next();  
4223          while ( Row ){
4224            //      
4225            unv++;
4226            this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4227            Row = pResult->Next();  
4228            //
4229          };
4230        };
4231      };
4232      if ( IsDebug() ) printf(" %i runs have been unvalidated \n",unv);
4233    };
4234    
4235    /**
4236     *
4237     * Rearrange GL_RUN table and remove runs
4238     *
4239     **/
4240    void PamelaDBOperations::RemoveRUNS(){
4241      TSQLResult *pResult;
4242      TSQLRow *Row;
4243      stringstream myquery;
4244      UInt_t drun = 0;
4245      GL_RUN *delrun = new GL_RUN();
4246      //
4247      myquery.str("");
4248      myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4249      //
4250      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4251      //
4252      pResult = conn->Query(myquery.str().c_str());
4253      //
4254      Row = pResult->Next();    
4255      //
4256      //
4257      if ( !Row ){
4258        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4259      } else {
4260        if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");    
4261        while ( Row ){
4262          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4263          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4264          drun++;
4265          Row = pResult->Next();    
4266        };
4267      };
4268      //
4269      //
4270      myquery.str("");
4271      myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4272      //
4273      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4274      //
4275      pResult = conn->Query(myquery.str().c_str());
4276      //
4277      Row = pResult->Next();    
4278      //
4279      if ( !Row ){
4280        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4281      } else {
4282        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");    
4283        while ( Row ){
4284          if ( IsDebug() ) printf(" restore run %i \n",(UInt_t)atoll(Row->GetField(1)));
4285          delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4286          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(1)));
4287          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4288          if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4289            if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4290            delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4291          };
4292          drun++;
4293          Row = pResult->Next();    
4294        };
4295      };
4296      //
4297      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);    
4298      //
4299      //
4300      //
4301      drun = 0;
4302      //
4303      myquery.str("");
4304      myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4305      //
4306      pResult = conn->Query(myquery.str().c_str());
4307      //
4308      Row = pResult->Next();    
4309      //
4310      if ( !Row ){
4311        if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4312      } else {
4313        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");    
4314        while ( Row ){
4315          if ( IsDebug() ) printf(" del run idtrash %i \n",(UInt_t)atoll(Row->GetField(0)));
4316          myquery.str("");
4317          myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";      
4318          conn->Query(myquery.str().c_str());
4319          drun++;
4320          Row = pResult->Next();    
4321        };
4322      };
4323      //  
4324      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_TRASH table \n",drun);    
4325      //
4326      //
4327      //
4328      drun = 0;
4329      //
4330      myquery.str("");
4331      myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4332      //
4333      pResult = conn->Query(myquery.str().c_str());
4334      //
4335      Row = pResult->Next();    
4336      //
4337      if ( !Row ){
4338        if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4339      } else {
4340        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");    
4341        while ( Row ){
4342          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4343          myquery.str("");
4344          myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4345          conn->Query(myquery.str().c_str());
4346          drun++;
4347          Row = pResult->Next();    
4348        };
4349      };
4350      //  
4351      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_FRAGMENTS table \n",drun);    
4352      //
4353      //
4354      //
4355      delete delrun;
4356      //
4357    };
4358    
4359    
4360    /**
4361     *
4362     * Rearrange calibration tables
4363     *
4364     **/
4365    void PamelaDBOperations::RemoveFILES(){
4366      stringstream myquery;
4367      //
4368      myquery.str("");
4369      myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4370      //
4371      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4372      //
4373      conn->Query(myquery.str().c_str());
4374      //
4375    };
4376    
4377    /**
4378     *
4379     * Rearrange calibration tables
4380     *
4381     **/
4382    void PamelaDBOperations::RemoveCALIBS(){
4383      TSQLResult *pResult;
4384      TSQLRow *Row;
4385      stringstream myquery;
4386      //  
4387      //
4388      // Calorimeter
4389      //
4390      for (Int_t section = 0; section < 4; section++){
4391        myquery.str("");
4392        myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4393        myquery << " SECTION=" << section << ";";
4394        //
4395        pResult = conn->Query(myquery.str().c_str());
4396        //
4397        Row = pResult->Next();      
4398        if( !Row->GetField(0) || !Row->GetField(1) ){
4399          //
4400          if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4401          //
4402        } else {
4403          //
4404          myquery.str("");
4405          myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4406          myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4407          myquery << " SECTION=" << section << ";";
4408          //
4409          pResult = conn->Query(myquery.str().c_str());
4410          //
4411          if( !pResult ){
4412            //
4413            if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4414            //
4415            throw -4;
4416            //
4417          };
4418          //
4419        };
4420      };
4421      myquery.str("");
4422      myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4423      //
4424      pResult = conn->Query(myquery.str().c_str());
4425      //
4426      if( !pResult ){
4427        //
4428        if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4429        //
4430        throw -4;
4431        //
4432      };
4433      //
4434      // Tracker
4435      //
4436      myquery.str("");
4437      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4438      //
4439      pResult = conn->Query(myquery.str().c_str());
4440      //
4441      Row = pResult->Next();      
4442      if( !Row->GetField(0) || !Row->GetField(1) ){
4443        //
4444        if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4445        //
4446      } else {
4447        //
4448        myquery.str("");
4449        myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4450        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4451        //
4452        pResult = conn->Query(myquery.str().c_str());
4453        //
4454        if( !pResult ){
4455          //
4456          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4457          //
4458          throw -4;
4459          //
4460        };
4461        //
4462        myquery.str("");
4463        myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4464        //
4465        pResult = conn->Query(myquery.str().c_str());
4466        //
4467        if( !pResult ){
4468          //
4469          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4470          //
4471          throw -4;
4472          //
4473        };
4474      };
4475      //
4476      //
4477      // S4
4478      //
4479      myquery.str("");
4480      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4481      //
4482      pResult = conn->Query(myquery.str().c_str());
4483      //
4484      Row = pResult->Next();      
4485      if( !Row->GetField(0) || !Row->GetField(1) ){
4486        //
4487        if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4488        //
4489      } else {
4490        //
4491        myquery.str("");
4492        myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4493        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4494        //
4495        pResult = conn->Query(myquery.str().c_str());
4496        //
4497        if( !pResult ){
4498          //
4499          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4500          //
4501          throw -4;
4502          //
4503        };
4504        //
4505        myquery.str("");
4506        myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4507        //
4508        pResult = conn->Query(myquery.str().c_str());
4509        //
4510        if( !pResult ){
4511          //
4512          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4513          //
4514          throw -4;
4515          //
4516        };
4517        //
4518      };
4519    };
4520    
4521    /**
4522     *
4523     * Rearrange calibration tables
4524     *
4525     **/
4526    UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
4527    
4528        Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
4529        UInt_t timeaftercalib=120000; //2000;
4530    //  ----------
4531    //  Check CRCs
4532    //  ----------
4533        for(Int_t ipkt=0; ipkt<6; ipkt++){
4534            if( caltrk->crc_hcal[ipkt] )return 0; // :-(
4535            for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
4536        }
4537    //  -----------------------
4538    //  Check missing packets:
4539    //  -----------------------
4540    //    Readout order:
4541    //    ------------------
4542    //    DSP   packet board
4543    //    ------------------
4544    //    12    0      1
4545    //    10    1      1
4546    //     8    2      1
4547    //     4    3      1
4548    //     6    4      1
4549    //     2    5      1
4550    //    ------------------
4551    //    11    0      2
4552    //     9    1      2
4553    //     7    2      2
4554    //     3    3      2
4555    //     5    4      2
4556    //     1    5      2
4557    //    ------------------
4558    //  -------------------------------------------------
4559    //  Check if it is first or second calibration packet
4560    //  -------------------------------------------------
4561        UInt_t build=0;
4562        TString classname = caltrk->GetName();
4563        UInt_t base=0;
4564        UInt_t mask=0;
4565        if(classname.Contains("CalibTrk1Event")){
4566            base=12;
4567            mask=0x03F000;
4568        }
4569        if(classname.Contains("CalibTrk2Event")){
4570            base=18;
4571            mask=0xFC0000;
4572        }
4573    //  -------------------------------------------------
4574    //  Count number of packets and set build variable
4575    //  -------------------------------------------------
4576        Int_t  npkts=0;
4577        for(Int_t ipkt=0; ipkt<6; ipkt++){
4578            if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
4579                npkts++;
4580                build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
4581            }
4582        }
4583    //    if( npkts==6 )return 1; // :-)
4584    
4585    //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
4586    
4587    //  -----------------------------------------------
4588    //  If missing packets: check the acq configuration
4589    //  (some DSPs might be excluded from acquisition)
4590    //  -----------------------------------------------
4591    
4592    //  -----------------------------------------------
4593    //  retrieve the first run header after calib
4594    //  -----------------------------------------------
4595         PacketType *pctp;
4596         EventCounter *cod;
4597         cod = eh->GetCounter();
4598         Int_t irun = cod->Get(pctp->RunHeader);
4599         TTree *rh=(TTree*)file->Get("RunHeader");
4600         if ( !rh || rh->IsZombie() ) throw -17;
4601         if( rh->GetEntries() == irun ){
4602              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;
4603             return 0; // :-(
4604         }
4605    
4606         RunHeaderEvent *run  = 0;
4607         EventHeader    *hrun = 0;
4608         rh->SetBranchAddress("RunHeader", &run);
4609         rh->SetBranchAddress("Header", &hrun);
4610         rh->GetEntry(irun);
4611    //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
4612    
4613         if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
4614              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
4615             return 0; // :-(
4616         }
4617        
4618         if( !run->RM_ACQ_AFTER_CALIB ){
4619              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0    -- cannot validate :-( "<<endl;
4620             return 0; // :-(
4621         }
4622    
4623         UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
4624         if( dtime > timeaftercalib ){
4625             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
4626             return 0; // :-(
4627         }
4628        
4629    
4630    
4631         if( (run->ACQ_BUILD_INFO & mask) != build ){
4632             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;
4633             return 0; // :-(
4634         }
4635         return 1; // :-)
4636    
4637    }
4638    
4639    /**
4640     *
4641     * Check the DB (only for overlapping runs at the moment)
4642     *
4643     **/
4644    UInt_t PamelaDBOperations::Check(){
4645      //
4646      UInt_t test = 0;
4647      //
4648      UInt_t thisrht = 0;
4649      UInt_t thisrtt = 0;
4650      UInt_t thisid = 0;
4651      UInt_t prevrht = 0;
4652      UInt_t prevrtt = 0;
4653      UInt_t previd = 0;
4654      //
4655      UInt_t prevl0id = 0;
4656      UInt_t thisl0id = 0;
4657      //
4658      stringstream oss;
4659      TSQLResult *result = 0;
4660      TSQLRow    *row    = 0;
4661      TSQLResult *result2 = 0;
4662      TSQLRow    *row2    = 0;
4663      oss.str("");
4664      oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN order by RUNHEADER_TIME asc;";
4665      //  oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
4666      result = conn->Query(oss.str().c_str());
4667      //
4668      if ( !result ) throw -4;;
4669      //
4670      row = result->Next();
4671      //
4672      while ( row ){
4673        thisid = (UInt_t)atoll(row->GetField(0));
4674        thisl0id = (UInt_t)atoll(row->GetField(1));
4675        thisrht = (UInt_t)atoll(row->GetField(2));
4676        thisrtt = (UInt_t)atoll(row->GetField(3));
4677        //
4678        //    if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
4679        //    if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
4680        if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
4681          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4682          printf(" CHECK n.1 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
4683          TString prevf = "";
4684          TString thisf = "";
4685          oss.str("");
4686          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4687          result2 = conn->Query(oss.str().c_str());
4688          if ( !result2 ) throw -4;;
4689          row2 = result2->Next();
4690          prevf = (TString)row2->GetField(0);
4691          oss.str("");
4692          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4693          result2 = conn->Query(oss.str().c_str());
4694          if ( !result2 ) throw -4;;
4695          row2 = result2->Next();
4696          thisf = (TString)row2->GetField(0);
4697          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4698          test = 1;
4699        };
4700        //
4701        if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
4702          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4703          printf(" CHECK n.2 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
4704          TString prevf = "";
4705          TString thisf = "";
4706          oss.str("");
4707          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4708          result2 = conn->Query(oss.str().c_str());
4709          if ( !result2 ) throw -4;;
4710          row2 = result2->Next();
4711          prevf = (TString)row2->GetField(0);
4712          oss.str("");
4713          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4714          result2 = conn->Query(oss.str().c_str());
4715          if ( !result2 ) throw -4;;
4716          row2 = result2->Next();
4717          thisf = (TString)row2->GetField(0);
4718          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4719          test = 1;
4720        };
4721        //
4722        if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
4723          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4724          printf(" CHECK n.3 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
4725          TString prevf = "";
4726          TString thisf = "";
4727          oss.str("");
4728          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4729          result2 = conn->Query(oss.str().c_str());
4730          if ( !result2 ) throw -4;;
4731          row2 = result2->Next();
4732          prevf = (TString)row2->GetField(0);
4733          oss.str("");
4734          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4735          result2 = conn->Query(oss.str().c_str());
4736          if ( !result2 ) throw -4;;
4737          row2 = result2->Next();
4738          thisf = (TString)row2->GetField(0);
4739          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4740          test = 1;
4741        };
4742    
4743        //
4744        prevrht = thisrht;
4745        prevrtt = thisrtt;
4746        previd = thisid;
4747        prevl0id = thisl0id;
4748        row = result->Next();
4749      };
4750      //
4751      return(test);
4752      //
4753    };

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

  ViewVC Help
Powered by ViewVC 1.1.23