/[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.21 by mocchiut, Mon Nov 27 14:25:35 2006 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){
60    //    //
61    //    //
62    SetConnection(host,user,password);    SetConnection(host,user,password);
# Line 62  PamelaDBOperations::PamelaDBOperations(T Line 70  PamelaDBOperations::PamelaDBOperations(T
70    SetTsync(tsync);    SetTsync(tsync);
71    SetObt0(obt0);    SetObt0(obt0);
72    //    //
73      SetTLEPath(tlefilename);
74      //
75    //    //
76    INSERT_RAW =!filerawname.IsNull();    INSERT_RAW =!filerawname.IsNull();
77    if(INSERT_RAW)SetRawName(filerawname);    if(INSERT_RAW)SetRawName(filerawname);
78    //    //
79    INSERT_ROOT = !filerootname.IsNull();    INSERT_ROOT = !filerootname.IsNull();
80    if( INSERT_ROOT ){    if( INSERT_ROOT ){
81        this->SetRootName(filerootname);
82        this->SetOrbitNo();
83      file = TFile::Open(this->GetRootName().Data());      file = TFile::Open(this->GetRootName().Data());
84      //    if ( !file ) INSERT_ROOT = false;    } else {
85        this->SetRootName("");
86    };    };
   //  this->OpenFile();  
87    //    //
88    this->SetID_RAW(0);    this->SetID_RAW(0);
89    this->SetID_ROOT(0);    this->SetID_ROOT(0);
# Line 100  void PamelaDBOperations::Close(){ Line 112  void PamelaDBOperations::Close(){
112  //  //
113  void PamelaDBOperations::CheckValidate(Long64_t olderthan){  void PamelaDBOperations::CheckValidate(Long64_t olderthan){
114    clean_time = new TDatime();    clean_time = new TDatime();
115      //
116    if(olderthan >= 0){    if(olderthan >= 0){
117      VALIDATE = true;      VALIDATE = true;
118      UInt_t timelim = 0;      UInt_t timelim = 0;
119      timelim =  (UInt_t)clean_time->Convert() - olderthan;      timelim =  (UInt_t)clean_time->Convert(true) - olderthan;
120      clean_time->Set(timelim,false);      clean_time->Set(timelim,false);
121    };    };
122  };  };
# Line 115  void PamelaDBOperations::CheckValidate(L Line 128  void PamelaDBOperations::CheckValidate(L
128   * @param password     password for the SQL connection.   * @param password     password for the SQL connection.
129   */   */
130  void PamelaDBOperations::SetConnection(TString host, TString user, TString password){  void PamelaDBOperations::SetConnection(TString host, TString user, TString password){
131      if ( IsDebug() ) printf(" Connecting using host = %s user = %s password = %s \n",host.Data(),user.Data(),password.Data());
132    conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());    conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());
133  };  };
134    
# Line 143  void PamelaDBOperations::SetDebugFlag(Bo Line 157  void PamelaDBOperations::SetDebugFlag(Bo
157  };  };
158    
159  /**  /**
160     * Set the autoboot flag
161     *
162     */
163    void PamelaDBOperations::SetAutoBoot(Bool_t dbg){
164      AUTOBOOT = dbg;
165    };
166    
167    /**
168     * Set the nofrag flag
169     *
170     */
171    void PamelaDBOperations::SetNoFrag(Bool_t nf){
172      NOFRAG = nf;
173    };
174    
175    /**
176   * Store the BOOT number of the RAW file.   * Store the BOOT number of the RAW file.
177   * @param boot    BOOT number of the RAW file   * @param boot    BOOT number of the RAW file
178   */   */
# Line 183  void PamelaDBOperations::SetRootName(TSt Line 213  void PamelaDBOperations::SetRootName(TSt
213  };  };
214    
215  /**  /**
216     * Store the downlink orbit number from filename.
217     */
218    void PamelaDBOperations::SetOrbitNo(){
219      dworbit = 0;
220      TString name = this->GetRootFile();
221      Int_t nlength = name.Length();
222      if ( nlength < 5 ) return;
223      TString dwo = 0;
224      for (Int_t i = 0; i<5; i++){
225        dwo.Append(name[i],1);
226      };
227      if ( dwo.IsDigit() ){
228        dworbit = (UInt_t)dwo.Atoi();
229      } else {
230        dwo="";
231        for (Int_t i = 8; i<13; i++){
232          dwo.Append(name[i],1);
233        };    
234        if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
235      };
236      if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
237      return;
238    };
239    
240    
241    
242    /**
243   * Store the NOBOOT flag.   * Store the NOBOOT flag.
244   * @param noboot    true/false.   * @param noboot    true/false.
245   */   */
# Line 191  void PamelaDBOperations::SetNOBOOT(Bool_ Line 248  void PamelaDBOperations::SetNOBOOT(Bool_
248  };  };
249    
250  /**  /**
251     * Store path to the TLE file.
252     */
253    void PamelaDBOperations::SetTLEPath(TString str){
254      tlefilename = str;
255    };
256    
257    /**
258   * Store the olderthan variable   * Store the olderthan variable
259   * @param olderthan   * @param olderthan
260   */   */
# Line 239  Int_t PamelaDBOperations::SetUpperLimits Line 303  Int_t PamelaDBOperations::SetUpperLimits
303    UInt_t nevent = 0;    UInt_t nevent = 0;
304    UInt_t pktlast = 0;    UInt_t pktlast = 0;
305    UInt_t obtlast = 0;    UInt_t obtlast = 0;
306    UInt_t t_pktlast = 0;    Long64_t t_pktlast = 0LL;
307    UInt_t t_obtlast = 0;    //  UInt_t t_obtlast = 0;
308    UInt_t upperpkt2 = 0;    Long64_t t_obtlast = 0LL;
309    ULong64_t upperobt2 = 0;    Long64_t upperpkt2 = 0LL;
310      Long64_t upperobt2 = 0LL;
311    UInt_t zomp = 0;    UInt_t zomp = 0;
312    UInt_t jump = 50000; // was 5000    UInt_t jump = 50000; // was 5000
313    EventCounter *code=0;    EventCounter *code=0;
314    //    //
315      Long64_t deltapkt = 5000LL;
316      Long64_t deltaobt = 50000LL;
317      //
318    //   pcksList packetsNames;    //   pcksList packetsNames;
319    //   pcksList::iterator Iter;    //   pcksList::iterator Iter;
320    //   getPacketsNames(packetsNames);    //   getPacketsNames(packetsNames);
# Line 283  Int_t PamelaDBOperations::SetUpperLimits Line 351  Int_t PamelaDBOperations::SetUpperLimits
351    upperobt = OBT(obtlast);    upperobt = OBT(obtlast);
352    upperentry = nevent-1;    upperentry = nevent-1;
353    //    //
354    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);
355    //    //
356    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);
357    //    //
358    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);
359    //    //
# Line 293  Int_t PamelaDBOperations::SetUpperLimits Line 361  Int_t PamelaDBOperations::SetUpperLimits
361    //    //
362    if ( nevent < 2 ) return(4);    if ( nevent < 2 ) return(4);
363    //    //
364    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) ){
365        //
366      // go back      // go back
367      zomp = nevent - 2;      zomp = nevent - 2;
368      //      //
# Line 315  Int_t PamelaDBOperations::SetUpperLimits Line 384  Int_t PamelaDBOperations::SetUpperLimits
384          upperpkt2 = PKT(ph->GetCounter());          upperpkt2 = PKT(ph->GetCounter());
385          upperobt2 = OBT(ph->GetOrbitalTime());          upperobt2 = OBT(ph->GetOrbitalTime());
386          //          //
387          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) ){
388              if ( IsDebug() ) printf(" .-. upperpkt2 %lld upperobt2 %lld \n",upperpkt2,upperobt2);  
389              if ( IsDebug() ) printf(" .-. upperpkt %lld t_pktlast %lld upperobt %lld t_obtlast %lld \n",upperpkt,t_pktlast,upperobt,t_obtlast);    
390              if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %lld pktlast %u upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);    
391              throw -13;    
392            };
393          //          //
394          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 ){
395            zomp = i + jump + 1;            zomp = i + jump + 1;
396            if ( zomp > nevent-2 ) zomp = nevent - 2;            if ( zomp > nevent-2 ) zomp = nevent - 2;
397            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);
398            break;            break;
399          };          };
400          //          //
# Line 355  Int_t PamelaDBOperations::SetUpperLimits Line 429  Int_t PamelaDBOperations::SetUpperLimits
429    //    //
430    rhev = rh->GetEntries();    rhev = rh->GetEntries();
431    rtev = rt->GetEntries();    rtev = rt->GetEntries();
432    UInt_t sobtt = 0;    Long64_t sobtt = 0LL;
433    UInt_t sobth = 0;    Long64_t sobth = 0LL;
434    UInt_t spktt = 0;    Long64_t spktt = 0LL;
435    UInt_t spkth = 0;    Long64_t spkth = 0LL;
436    UInt_t pktt = 0;    Long64_t pktt = 0LL;
437    ULong64_t obtt = 0;    Long64_t obtt = 0LL;
438    UInt_t pkth = 0;    Long64_t pkth = 0LL;
439    ULong64_t obth = 0;    Long64_t obth = 0LL;
440    //    //
441    T->GetEntry(upperentry);    T->GetEntry(upperentry);
442    code = eh->GetCounter();    code = eh->GetCounter();
# Line 382  Int_t PamelaDBOperations::SetUpperLimits Line 456  Int_t PamelaDBOperations::SetUpperLimits
456      obth = OBT(phh->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
457    };    };
458    //    //
459    if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);    if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
460    if ( pkth > upperpkt && obth > upperobt ){    if ( pkth > upperpkt && obth > upperobt ){
461      if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);      if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
462      upperpkt = pkth;      upperpkt = pkth;
463      upperobt = obth;      upperobt = obth;
464      rhev = lasthead+1;      rhev = lasthead+1;
465    } else {    } else {
466      rhev = lasthead;      rhev = lasthead;
467    };    };
468    if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);    if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
469    //    //
470    if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);    if ( IsDebug() ) printf(" rtev beforev %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
471    if ( pktt > upperpkt && obtt > upperobt ){    if ( pktt > upperpkt && obtt > upperobt ){
472      if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);      if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
473      upperpkt = pktt;      upperpkt = pktt;
474      upperobt = obtt;      upperobt = obtt;
475      rtev = lasttrail+1;      rtev = lasttrail+1;
476    } else {    } else {
477      rtev = lasttrail;      rtev = lasttrail;
478    };    };
479    if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);    if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
480    //  goto kikko;    //  goto kikko;
481    //    //
482    //    //
483    // Check if runtrailer/runheader are within lower limits    // Check if runtrailer/runheader are within lower limits
484    //    //
485    //    //
486    pkth = 0;    pkth = 0LL;
487    obth = 0;    obth = 0LL;
488    spkth = 0;    spkth = 0LL;
489    sobth = 0;    sobth = 0LL;
490    for (Int_t k=0; k<rhev; k++){    for (Int_t k=0; k<rhev; k++){
491      if ( k > 0 ){      if ( k > 0 ){
492        spkth = pkth;        spkth = pkth;
# Line 460  Int_t PamelaDBOperations::SetUpperLimits Line 534  Int_t PamelaDBOperations::SetUpperLimits
534            upperentry = evbefh-1;            upperentry = evbefh-1;
535          };              };    
536        };        };
537        if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);        if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
538        goto kikko0;        goto kikko0;
539      };      };
540    };    };
# Line 468  Int_t PamelaDBOperations::SetUpperLimits Line 542  Int_t PamelaDBOperations::SetUpperLimits
542    //    //
543    //    //
544    //    //
545    pktt = 0;    pktt = 0LL;
546    obtt = 0;    obtt = 0LL;
547    spktt = 0;    spktt = 0LL;
548    sobtt = 0;    sobtt = 0LL;
549    for (Int_t k=0; k<rtev; k++){    for (Int_t k=0; k<rtev; k++){
550      if ( k > 0 ){      if ( k > 0 ){
551        spktt = pktt;        spktt = pktt;
# Line 491  Int_t PamelaDBOperations::SetUpperLimits Line 565  Int_t PamelaDBOperations::SetUpperLimits
565        rt->GetEntry(rtev);        rt->GetEntry(rtev);
566        pktt = spktt;        pktt = spktt;
567        obtt = sobtt;        obtt = sobtt;
568        if ( IsDebug() ) printf(" lasttrail %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);        if ( IsDebug() ) printf(" lasttrail %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
569        //        //
570        UInt_t evbeft = 0;        UInt_t evbeft = 0;
571        code = eht->GetCounter();        code = eht->GetCounter();
# Line 520  Int_t PamelaDBOperations::SetUpperLimits Line 594  Int_t PamelaDBOperations::SetUpperLimits
594            upperentry = evbeft-1;            upperentry = evbeft-1;
595          };          };
596        };        };
597        if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);        if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
598        goto kikko;        goto kikko;
599        //      break;        //      break;
600        //        //
# Line 548  Int_t PamelaDBOperations::SetUpperLimits Line 622  Int_t PamelaDBOperations::SetUpperLimits
622      obth = OBT(phh->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
623    };    };
624    //    //
625    if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);    if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
626    if ( pkth > upperpkt && obth > upperobt ){    if ( pkth > upperpkt && obth > upperobt ){
627      if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);      if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
628      upperpkt = pkth;      upperpkt = pkth;
629      upperobt = obth;      upperobt = obth;
630      rhev = lasthead+1;      rhev = lasthead+1;
631    } else {    } else {
632      rhev = lasthead;      rhev = lasthead;
633    };    };
634    if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);    if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
635    //    //
636    if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);    if ( IsDebug() ) printf(" rtev beforev %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
637    if ( pktt > upperpkt && obtt > upperobt ){    if ( pktt > upperpkt && obtt > upperobt ){
638      if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);      if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
639      upperpkt = pktt;      upperpkt = pktt;
640      upperobt = obtt;      upperobt = obtt;
641      rtev = lasttrail+1;      rtev = lasttrail+1;
642    } else {    } else {
643      rtev = lasttrail;      rtev = lasttrail;
644    };    };
645    if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);    if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
646    //    //
647    if ( IsDebug() ) printf(" Upper limits are: OBT %llu pkt_num %i upper entry %i \n",upperobt,upperpkt,upperentry);    if ( IsDebug() ) printf(" Upper limits are: OBT %lld pkt_num %lld upper entry %i \n",upperobt,upperpkt,upperentry);
648    //    //
649    return(0);    return(0);
650  }  }
651    
652    /**
653     *
654     * Trick to have unique RUN ID numbers even when runs are deleted and mysql deamon restarted.
655     * Entries in the _RUNID_GEN table are never deleted.
656     *
657     **/
658    UInt_t PamelaDBOperations::AssignRunID(){
659      //
660      TSQLResult *result = 0;
661      TSQLRow *row = 0;
662      UInt_t runid = 0;
663      //
664      stringstream   oss;
665      //  
666      oss.str("");
667      oss << "INSERT INTO _RUNID_GEN VALUES (NULL);";
668      result = conn->Query(oss.str().c_str());
669      if ( !result ) throw -10;
670      oss.str("");
671      oss << "SELECT ID FROM _RUNID_GEN ORDER BY ID DESC LIMIT 1;";
672      result = conn->Query(oss.str().c_str());
673      if ( !result ) throw -10;
674      //
675      row = result->Next();
676      //
677      if ( !row ) throw -28;
678      //
679      runid = (UInt_t)atoll(row->GetField(0));
680      //
681      return(runid);
682    };
683    
684  //  //
685  // GETTERS  // GETTERS
686  //  //
# Line 672  void PamelaDBOperations::CheckConnection Line 778  void PamelaDBOperations::CheckConnection
778    if( !conn ) throw -1;    if( !conn ) throw -1;
779    bool connect = conn->IsConnected();    bool connect = conn->IsConnected();
780    if( !connect ) throw -1;    if( !connect ) throw -1;
781      if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
782      //
783      // set DB timezone to UTC
784      //
785      stringstream oss;
786      //  
787      oss.str("");
788      oss << "SET time_zone='+0:00';";
789      TSQLResult *result = 0;
790      result = conn->Query(oss.str().c_str());
791      if ( !result ) throw -10;
792      //
793  };  };
794    
795  /**  /**
796   * Return the correct packet number if we went back to zero   * Return the correct packet number if we went back to zero
797   */   */
798  UInt_t PamelaDBOperations::PKT(UInt_t pkt_num){    Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){  
799    //    //
800    //  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,pktfirst,(UInt_t)(16777214/2));
801    //    //
802    if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2)  ) return((pkt_num+16777215));    if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2)  ){
803        if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL);
804        return((Long64_t)pkt_num+16777215LL);
805      };
806    //    //
807    if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){    if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){
808      if ( (pkt_num-16777215) < 0 ){      if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL);
809        return((16777215-pkt_num));      return((Long64_t)pkt_num-16777215LL);
     } else {  
       return((pkt_num-16777215));  
     };  
810    };    };
811    //    //
812    return(pkt_num);    if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
813      return((Long64_t)pkt_num);
814    //    //
815  };  };
816    
817  /**  /**
818   * Return the correct On Board Time if we went back to zero   * Return the correct On Board Time if we went back to zero
819   */   */
820  ULong64_t PamelaDBOperations::OBT(UInt_t obt){    Long64_t PamelaDBOperations::OBT(UInt_t obt){  
821    //    //
822    if ( obt < (obtfirst/2) && obtfirst > (numeric_limits<UInt_t>::max()/2) ) return((ULong64_t)(obt+numeric_limits<UInt_t>::max()));    if ( obt < (obtfirst/2) && obtfirst > (numeric_limits<UInt_t>::max()/2) ) return((Long64_t)(obt+numeric_limits<UInt_t>::max()));
823    //    //
824    if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){    if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
825      if ( (obt-numeric_limits<UInt_t>::max()) < 0  ){      return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
       return((ULong64_t)(numeric_limits<UInt_t>::max()-obt));  
     } else {  
       return((ULong64_t)(obt-numeric_limits<UInt_t>::max()));  
     };  
826    };    };
827    //    //
828    return((ULong64_t)obt);    return((Long64_t)obt);
829  };  };
830    
831  /**  /**
# Line 851  Int_t PamelaDBOperations::insertPamelaGL Line 966  Int_t PamelaDBOperations::insertPamelaGL
966    if ( this->GetID_RAW() == 0 )  throw -11;    if ( this->GetID_RAW() == 0 )  throw -11;
967    //    //
968    oss.str("");    oss.str("");
969    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='"
970        << id << " AND TO_ID_RAW >= "        << this->GetRawFile().Data() << "';";
       << id << ";";  
971    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
972    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
973    if ( !result ) throw -10;    if ( !result ) throw -10;
974    row = result->Next();    row = result->Next();
   if ( !row ) throw -10;  
975    //    //
976    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();    if ( !row ){
977        oss.str("");
978        oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
979            << dworbit << " order by FROM_ORBIT desc limit 1;";
980        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
981        result = conn->Query(oss.str().c_str());
982        if ( !result ) throw -10;
983        row = result->Next();
984        if ( !row ) throw -10;
985      };
986      //
987      TTimeStamp tu = TTimeStamp((UInt_t)atoi(row->GetField(0)),(UInt_t)atoi(row->GetField(1)),(UInt_t)atoi(row->GetField(2)),(UInt_t)atoi(row->GetField(3)),(UInt_t)atoi(row->GetField(4)),(UInt_t)atoi(row->GetField(5)),0,true,0);
988      t0 = (UInt_t)tu.GetSec();
989      if ( IsDebug() ) printf(" t0 is %u ti is %s %s %s %s %s %s\n",t0,row->GetField(0),row->GetField(1),row->GetField(2),row->GetField(3),row->GetField(4),row->GetField(5));
990      //
991    /*    /*
992     * Verify that the TIMESYNC have been not already processed     * Verify that the TIMESYNC have been not already processed
993     */     */
# Line 875  Int_t PamelaDBOperations::insertPamelaGL Line 1002  Int_t PamelaDBOperations::insertPamelaGL
1002    if (result == NULL) throw -10;    if (result == NULL) throw -10;
1003    row = result->Next();    row = result->Next();
1004    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
1005        if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1006      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;
1007        //
1008        tsync = (UInt_t)atoll(row->GetField(2));
1009        obt0 = (UInt_t)atoll(row->GetField(1));
1010        //
1011      return(1);      return(1);
1012    };    };
1013    //    //
# Line 932  Int_t PamelaDBOperations::insertPamelaGL Line 1064  Int_t PamelaDBOperations::insertPamelaGL
1064          //          //
1065          TYPE = 55;//224;          TYPE = 55;//224;
1066          //          //
1067            if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
1068            //
1069          if ( TSYNC && OBT ){          if ( TSYNC && OBT ){
1070            existsts = true;            existsts = true;
1071            goto eout;            goto eout;
# Line 954  Int_t PamelaDBOperations::insertPamelaGL Line 1088  Int_t PamelaDBOperations::insertPamelaGL
1088      //      //
1089      rt->SetBranchAddress("RunTrailer", &runt);      rt->SetBranchAddress("RunTrailer", &runt);
1090      //      //
1091      if ( rhev > 0 ){      Int_t nrhev = rh->GetEntries();
1092        rh->GetEntry(0);      Int_t nrtev = rt->GetEntries();
1093        //      if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev);
1094        TSYNC = runh->LAST_TIME_SYNC_INFO;      //
1095        OBT = runh->OBT_TIME_SYNC * 1000;      if ( nrhev > 0 ){
1096        //        for (Int_t i=0; i<nrhev; i++){
1097        TYPE = 20;          //
1098        //          rh->GetEntry(i);
1099        if ( TSYNC && OBT ){          //
1100          existsts = true;          TSYNC = runh->LAST_TIME_SYNC_INFO;
1101          goto eout;          OBT = runh->OBT_TIME_SYNC * 1000;
1102            //
1103            TYPE = 20;
1104            //
1105            if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
1106            //
1107            if ( TSYNC && OBT ){
1108              existsts = true;
1109              goto eout;
1110            };
1111        };        };
1112        //        //
1113      };      };
1114      if ( rtev > 0 ){      if ( nrtev > 0 ){
1115        //        //
1116        if ( IsDebug() ) printf(" No runheader \n");        if ( IsDebug() ) printf(" No runheader \n");
1117        signal = 6;        signal = 6;
1118        //        //
1119        rt->GetEntry(0);        for (Int_t i=0; i<nrtev; i++){
1120        //          //
1121        TSYNC = runt->LAST_TYME_SYNC_INFO;          rt->GetEntry(i);
1122        OBT = runt->OBT_TYME_SYNC * 1000;          //
1123        //          TSYNC = runt->LAST_TYME_SYNC_INFO;
1124        TYPE = 21;          OBT = runt->OBT_TYME_SYNC * 1000;
1125        //          //
1126        if ( TSYNC && OBT ){          TYPE = 21;
1127          existsts = true;          //
1128          goto eout;          if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
1129            //
1130            if ( TSYNC && OBT ){
1131              existsts = true;
1132              goto eout;
1133            };
1134        };        };
1135        //        //
1136      } else {      } else {
# Line 1053  Int_t PamelaDBOperations::insertPamelaGL Line 1201  Int_t PamelaDBOperations::insertPamelaGL
1201    conn->Query(oss.str().c_str());    conn->Query(oss.str().c_str());
1202    if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());
1203    //    //
1204      if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1205    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1206    //    //
1207      tsync = TSYNC;
1208      obt0 = OBT;
1209      //
1210    delete result;    delete result;
1211    return(signal);    return(signal);
1212  }  }
# Line 1154  Int_t PamelaDBOperations::assignBOOT_NUM Line 1306  Int_t PamelaDBOperations::assignBOOT_NUM
1306    VarDumpEvent  *vde = 0;    VarDumpEvent  *vde = 0;
1307    VarDumpRecord *vdr = 0;    VarDumpRecord *vdr = 0;
1308    //    //
1309      Bool_t found = false;
1310    trDumpEv->SetBranchAddress("VarDump", &vde);    trDumpEv->SetBranchAddress("VarDump", &vde);
1311    if ( trDumpEv->GetEntries() > 0 ){    if ( trDumpEv->GetEntries() > 0 ){
1312      Bool_t found = false;      found = false;
1313      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1314        trDumpEv->GetEntry(i);        trDumpEv->GetEntry(i);
1315        vde->Records->GetEntries();        //      vde->Records->GetEntries();
1316        if ( vde->Records->GetEntries()>0 ){        if ( vde->Records->GetEntries()>5 ){
1317          found = true;          found = true;
1318          goto fill;          goto fill;
1319        };        };
# Line 1173  Int_t PamelaDBOperations::assignBOOT_NUM Line 1326  Int_t PamelaDBOperations::assignBOOT_NUM
1326        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1327        //        //
1328      } else {      } else {
1329        if ( !this->GetBOOTnumber() ) return(4);        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4);
1330        };
1331      } else {
1332        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2);
1333      };
1334      //
1335      UInt_t bn = 0;
1336      Bool_t afound = false;
1337      if ( !found && this->AutoBoot()){
1338        afound = true;
1339        //
1340        // Search for other files with similar timesync
1341        //
1342        if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0);
1343        UInt_t upperts = tsync-(obt0/1000)+5;
1344        UInt_t lowerts = tsync-(obt0/1000)-5;
1345        oss.str("");
1346        oss << "select GL_RAW.BOOT_NUMBER from GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW where TIMESYNC-(OBT0/1000)<"
1347            << upperts
1348            << " AND TIMESYNC-(OBT0/1000)>"
1349            << lowerts
1350            << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
1351        result = conn->Query(oss.str().c_str());
1352        if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str());
1353        //
1354        if ( !result ) throw -4;;
1355        found = true;
1356        if ( result->GetRowCount()<3 ){
1357          if ( IsDebug() ) printf(" AGH! no results!\n");
1358          found = false;
1359        } else {
1360          row = result->Next();      
1361          bn = (UInt_t)atoll(row->GetField(0));
1362          for ( Int_t r=1; r<result->GetRowCount() ;r++){
1363            if ( !row ) throw -4;
1364            if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0));
1365            if ( bn != (UInt_t)atoll(row->GetField(0)) ){
1366              if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0)));
1367              found = false;
1368            };
1369            row = result->Next();
1370          };
1371      };      };
1372      };
1373      //
1374      Int_t sgn = 0;
1375      //
1376      if ( !found ){
1377        throw -29;
1378    } else {    } else {
1379      if ( !this->GetBOOTnumber() ) return(2);      if ( afound ){
1380          this->SetBOOTnumber(bn);
1381          sgn = 8;
1382        };
1383    };    };
1384    //    //
1385    oss.str("");    oss.str("");
# Line 1186  Int_t PamelaDBOperations::assignBOOT_NUM Line 1389  Int_t PamelaDBOperations::assignBOOT_NUM
1389    conn->Query(oss.str().c_str());          conn->Query(oss.str().c_str());      
1390    //    //
1391    delete result;    delete result;
1392    return(0);    return(sgn);
1393  };  };
1394    
1395  /**  /**
# Line 1449  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1652  Bool_t PamelaDBOperations::IsRunAlreadyI
1652      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1653        //        //
1654        if ( IsDebug() ) printf(" The new run has more events than the old one \n");        if ( IsDebug() ) printf(" The new run has more events than the old one \n");
1655        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1656        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1657        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) <<";";
1658        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1659    //       conn->Query(oss.str().c_str());
1660        if ( signal ) signal = false;        if ( signal ) signal = false;
1661        goto gonext;              goto gonext;      
1662        //        //
# Line 1466  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1670  Bool_t PamelaDBOperations::IsRunAlreadyI
1670        //        //
1671        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");
1672        //        //
1673        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1674        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1675        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) <<";";
1676        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1677    //       conn->Query(oss.str().c_str());
1678        //        //
1679        if ( signal ) signal = false;        if ( signal ) signal = false;
1680        goto gonext;        goto gonext;
# Line 1483  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1688  Bool_t PamelaDBOperations::IsRunAlreadyI
1688        //        //
1689        if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");        if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");
1690        //        //
1691        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1692        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1693        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) <<";";
1694        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1695    //       conn->Query(oss.str().c_str());
1696        if ( signal ) signal = false;        if ( signal ) signal = false;
1697        //        //
1698      };      };
# Line 1522  void PamelaDBOperations::HandleRun(){   Line 1728  void PamelaDBOperations::HandleRun(){  
1728      //      //
1729      this->FillClass();      this->FillClass();
1730      //      //
1731      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);      if ( !IsRunAlreadyInserted() ){
1732          glrun->SetID(this->AssignRunID());
1733          glrun->SetID_RUN_FRAG(0);
1734          glrun->Fill_GL_RUN(conn);
1735        };
1736    } else {    } else {
1737      //      //
1738      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 1755  void PamelaDBOperations::HandleRunFragme
1755    UInt_t rhfirstev = firstev;    UInt_t rhfirstev = firstev;
1756    UInt_t rtlastev = lastev;    UInt_t rtlastev = lastev;
1757    Bool_t found = false;    Bool_t found = false;
1758      Bool_t foundinrun = false;
1759    //    //
1760    TSQLResult *result = 0;    TSQLResult *result = 0;
1761    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 1571  void PamelaDBOperations::HandleRunFragme Line 1782  void PamelaDBOperations::HandleRunFragme
1782    //    //
1783    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");
1784    //    //
1785      // First of all insert the run in the fragment table...
1786      //
1787      oss.str("");
1788      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1789          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1790          << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1791          << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1792          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1793          << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1794          << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1795          << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1796          << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1797          << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1798          << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1799          << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1800          << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1801          << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1802      //
1803      if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1804      result = conn->Query(oss.str().c_str());
1805      //
1806      if ( !result ) throw -4;
1807      //
1808      row = result->Next();
1809      //
1810      if ( !row ){
1811        //
1812        // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
1813        //
1814        if ( IsDebug() ) printf(" The run is new \n");
1815        if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
1816        //
1817        glrun->SetID(this->AssignRunID());
1818        glrun->SetID_RUN_FRAG(0);
1819        glrun->Fill_GL_RUN_FRAGMENTS(conn);
1820        //
1821      } else {
1822        if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
1823      };
1824      //
1825      //
1826    // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?    // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
1827    //        //    
1828    if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is    if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
# Line 1579  void PamelaDBOperations::HandleRunFragme Line 1831  void PamelaDBOperations::HandleRunFragme
1831      oss.str("");      oss.str("");
1832      oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "      oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1833          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1834          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME()          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1835            << " ID != " << glrun->ID
1836          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1837      //      //
1838      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
# Line 1589  void PamelaDBOperations::HandleRunFragme Line 1842  void PamelaDBOperations::HandleRunFragme
1842      //      //
1843      row = result->Next();      row = result->Next();
1844      //      //
1845        if ( !row && NoFrag() ){
1846          //
1847          oss.str("");
1848          oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
1849              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1850              << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1851              << " ID != " << glrun->ID
1852              << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1853          //
1854          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1855          result = conn->Query(oss.str().c_str());
1856          //
1857          if ( !result ) throw -4;
1858          //
1859          foundinrun = true;
1860          //
1861          row = result->Next();
1862          //
1863        };
1864        //
1865      if ( !row ){      if ( !row ){
1866        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1867        found = false;        found = false;
# Line 1663  void PamelaDBOperations::HandleRunFragme Line 1936  void PamelaDBOperations::HandleRunFragme
1936        //        //
1937        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 ");
1938        //        //
1939          if ( foundinrun ){
1940            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
1941            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1942          };
1943          //
1944        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
1945        //        //
1946        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
1947        //        //
1948        oss.str("");        oss.str("");
1949        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1694  void PamelaDBOperations::HandleRunFragme Line 1972  void PamelaDBOperations::HandleRunFragme
1972        };        };
1973        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);
1974        //        //
       glrun1->SetID(0);  
1975        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
1976        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
1977        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
# Line 1703  void PamelaDBOperations::HandleRunFragme Line 1980  void PamelaDBOperations::HandleRunFragme
1980        //        //
1981        glrun->SetEV_FROM(firstev);        glrun->SetEV_FROM(firstev);
1982        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
1983          //
1984        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
1985        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
1986        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
# Line 1722  void PamelaDBOperations::HandleRunFragme Line 2000  void PamelaDBOperations::HandleRunFragme
2000        //        //
2001        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2002          //          //
2003            //      glrun->SetID(this->AssignRunID());
2004            glrun->SetID_RUN_FRAG(glrun1->GetID());
2005          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
2006          //          //
2007          // 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);  
2008          //          //
2009            glrun1->SetID_RUN_FRAG(glrun->GetID());
2010          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2011          //          //
         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;  
         //  
2012        };        };
       //  
       delete glrun1;  
       //  
2013        // delete old entry in fragment table        // delete old entry in fragment table
2014        //        //
2015        oss.str("");        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2016          glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2017        //        //
2018        oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";        delete glrun1;
2019        //        //
       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;  
2020        //        //
2021        return;        return;
2022        //        //
# Line 1791  void PamelaDBOperations::HandleRunFragme Line 2025  void PamelaDBOperations::HandleRunFragme
2025    };    };
2026    //    //
2027    if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is    if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
2028                                             // 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
2029      //      //
2030      oss.str("");      oss.str("");
2031      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 "
2032          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2033          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME()          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2034            << " ID != " << glrun->ID
2035          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2036      //      //
2037      if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());      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 2041  void PamelaDBOperations::HandleRunFragme
2041      //      //
2042      row = result->Next();      row = result->Next();
2043      //      //
2044        if ( !row && NoFrag() ){
2045          //
2046          oss.str("");
2047          oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
2048              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2049              << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2050              << " ID != " << glrun->ID
2051              << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2052          //
2053          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2054          result = conn->Query(oss.str().c_str());
2055          //
2056          if ( !result ) throw -4;
2057          //
2058          foundinrun = true;
2059          row = result->Next();
2060          //
2061        };
2062        //
2063      if ( !row ){      if ( !row ){
2064        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2065        found = false;        found = false;
# Line 1879  void PamelaDBOperations::HandleRunFragme Line 2133  void PamelaDBOperations::HandleRunFragme
2133        //        //
2134        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 ");
2135        //        //
2136          if ( foundinrun ){
2137            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2138            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2139          };
2140          //
2141        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
2142        //        //
2143        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2144        //        //
2145        oss.str("");        oss.str("");
2146        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1918  void PamelaDBOperations::HandleRunFragme Line 2177  void PamelaDBOperations::HandleRunFragme
2177        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
2178        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
2179        //        //
       glrun1->SetID(0);  
2180        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
2181        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
2182        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
# Line 1938  void PamelaDBOperations::HandleRunFragme Line 2196  void PamelaDBOperations::HandleRunFragme
2196        //        //
2197        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2198          //          //
2199          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());  
2200          //          //
2201          result =  conn->Query(oss.str().c_str());          glrun->SetID_RUN_FRAG(glrun1->GetID());
2202          if ( !result ) throw -4;          glrun->Fill_GL_RUN(conn);
         row = result->Next();  
         //  
         UInt_t idrun0 = 0;  
         if ( !row ){  
           throw -10;  
         } else {  
           idrun0 = (UInt_t)atoll(row->GetField(0));  
         };  
2203          //          //
2204          glrun1->SetID_RUN_FRAG(idrun0);          // set id number
2205          //          //
2206            glrun1->SetID_RUN_FRAG(glrun->GetID());
2207          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2208          //          //
         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;  
         //  
2209        };        };
2210        //        //
2211        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 << ";";  
2212        //        //
2213        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");
2214        result =  conn->Query(oss.str().c_str());        glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
       if ( !result ) throw -4;  
2215        //        //
2216          delete glrun1;
2217        //        //
2218        return;        return;
2219        //        //
# Line 2036  void PamelaDBOperations::HandleRunFragme Line 2250  void PamelaDBOperations::HandleRunFragme
2250      //      //
2251      row = result->Next();      row = result->Next();
2252      //      //
2253      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 {  
2254        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");
2255        } else {
2256          if ( NoFrag() ){
2257            glrun->SetID_RUN_FRAG(glrun->GetID());
2258            glrun->Fill_GL_RUN(conn);      
2259            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2260          };    
2261      };      };
2262    };    };
2263    //    //
# Line 2100  void PamelaDBOperations::HandleMissingHo Line 2283  void PamelaDBOperations::HandleMissingHo
2283      //      //
2284      this->FillClass(mishead,mistrail,firstev,lastev);      this->FillClass(mishead,mistrail,firstev,lastev);
2285      //      //
2286      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2287          glrun->SetID(this->AssignRunID());
2288          glrun->SetID_RUN_FRAG(0);
2289          glrun->Fill_GL_RUN(conn);    
2290        };
2291      //      //
2292    };    };
2293    //    //
# Line 2267  Bool_t PamelaDBOperations::IsRunConsiste Line 2454  Bool_t PamelaDBOperations::IsRunConsiste
2454            //                  //      
2455            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2456            //            //
2457            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2458                glrun->SetID(this->AssignRunID());
2459                glrun->SetID_RUN_FRAG(0);
2460                glrun->Fill_GL_RUN(conn);      
2461              };
2462            //            //
2463            firstevno = lastentry + 1;            firstevno = lastentry + 1;
2464            //            //
# Line 2343  void PamelaDBOperations::HandleSuspiciou Line 2534  void PamelaDBOperations::HandleSuspiciou
2534      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2535      //      //
2536      this->FillClass();      this->FillClass();
2537      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2538          glrun->SetID(this->AssignRunID());
2539          glrun->SetID_RUN_FRAG(0);
2540          glrun->Fill_GL_RUN(conn);    
2541        };
2542      //      //
2543    } else {    } else {
2544      //      //
# Line 2371  void PamelaDBOperations::HandleSuspiciou Line 2566  void PamelaDBOperations::HandleSuspiciou
2566        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");
2567        //        //
2568        this->FillClass();        this->FillClass();
2569        if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                if ( !IsRunAlreadyInserted() ){
2570            glrun->SetID(this->AssignRunID());
2571            glrun->SetID_RUN_FRAG(0);
2572            glrun->Fill_GL_RUN(conn);        
2573          };
2574        //        //
2575      } else {      } else {
2576        //        //
# Line 2469  void PamelaDBOperations::HandleSuspiciou Line 2668  void PamelaDBOperations::HandleSuspiciou
2668            //                  //      
2669            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2670            //            //
2671            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2672                glrun->SetID(this->AssignRunID());
2673                glrun->SetID_RUN_FRAG(0);
2674                glrun->Fill_GL_RUN(conn);      
2675              };
2676            //            //
2677            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...
2678              //              //
# Line 2504  void PamelaDBOperations::HandleSuspiciou Line 2707  void PamelaDBOperations::HandleSuspiciou
2707              //                  //    
2708              this->SetCommonGLRUN(firstTime,lastTime);              this->SetCommonGLRUN(firstTime,lastTime);
2709              //              //
2710              if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                    if ( !IsRunAlreadyInserted() ){
2711                  glrun->SetID(this->AssignRunID());
2712                  glrun->SetID_RUN_FRAG(0);
2713                  glrun->Fill_GL_RUN(conn);      
2714                };
2715            };            };
2716            //            //
2717            firstevno = lastentry + 1;            firstevno = lastentry + 1;
# Line 2611  Int_t PamelaDBOperations::insertCALO_CAL Line 2818  Int_t PamelaDBOperations::insertCALO_CAL
2818                // no calibrations in the db contain our calibration                // no calibrations in the db contain our calibration
2819                //                //
2820                if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);                if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
2821                if ( fromtime < 1150863400 ){                if ( fromtime < 1150871000 ){ //1150866904
2822                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
2823                  fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode                  fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode
2824                };                };
# Line 2756  void PamelaDBOperations::HandleTRK_CALIB Line 2963  void PamelaDBOperations::HandleTRK_CALIB
2963        // no calibrations in the db contain our calibration        // no calibrations in the db contain our calibration
2964        //        //
2965        if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");        if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
2966        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
2967        //        //
2968        oss.str("");        oss.str("");
2969        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
# Line 3112  Int_t PamelaDBOperations::insertS4_CALIB Line 3319  Int_t PamelaDBOperations::insertS4_CALIB
3319            // no calibrations in the db contain our calibration            // no calibrations in the db contain our calibration
3320            //            //
3321            if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");            if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");
3322            if ( fromtime < 1150863400 ){            if ( fromtime < 1150871000 ){  
3323              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
3324              fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode              fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode
3325            };            };
# Line 3185  Int_t PamelaDBOperations::insertS4_CALIB Line 3392  Int_t PamelaDBOperations::insertS4_CALIB
3392   * 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
3393   */   */
3394  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3395      return(this->CleanGL_RUN_FRAGMENTS(""));
3396    };
3397    
3398    /**
3399     * Scan the fragment table and move old fragments to the GL_RUN table
3400     */
3401    Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3402    //    //
3403    TSQLResult *result = 0;    TSQLResult *result = 0;
3404    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 3192  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3406  Int_t PamelaDBOperations::CleanGL_RUN_FR
3406    TSQLRow    *row2   = 0;    TSQLRow    *row2   = 0;
3407    //    //
3408    UInt_t moved = 0;    UInt_t moved = 0;
 //  UInt_t timelim = 0;  
 //  TDatime *time = new TDatime();  
3409    //    //
3410    stringstream oss;    stringstream oss;
3411    oss.str("");    oss.str("");
3412    //    //
3413    //    if ( !strcmp(fcleanfile.Data(),"") ){
3414    // check if there are entries older than "olderthan" seconds from now      //
3415    //      // check if there are entries older than "olderthan" seconds from now
3416    oss.str("");      //
3417    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"      oss.str("");
3418        << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3419    //          << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3420    if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());      //
3421    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());
3422        result = conn->Query(oss.str().c_str());
3423        //
3424      } else {
3425        oss.str("");
3426        oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3427        if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3428        result = conn->Query(oss.str().c_str());
3429        //    
3430        if ( result ){
3431          //
3432          row = result->Next();
3433          //
3434          if ( row ){
3435            oss.str("");
3436            oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3437                << " ID_ROOT_L0=" << row->GetField(0) << ";";
3438            //
3439            if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3440            result = conn->Query(oss.str().c_str());
3441            //    
3442          };
3443        } else {
3444          return(2);
3445        };
3446      };
3447    //    //
3448    if ( result ){    if ( result ){
3449      //      //
3450      row = result->Next();      row = result->Next();
3451      //      //
3452      if ( row ){      while ( row ){
3453        //        //
3454        oss.str("");        oss.str("");
3455        oss << " ID= "<< row->GetField(0);        oss << " ID= "<< row->GetField(0);
# Line 3247  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3484  Int_t PamelaDBOperations::CleanGL_RUN_FR
3484          if ( IsDebug() ) printf(" The run is new \n");          if ( IsDebug() ) printf(" The run is new \n");
3485          if ( IsDebug() ) printf(" -> fill the DB \n");                if ( IsDebug() ) printf(" -> fill the DB \n");      
3486          //          //
3487          glrun->SetID(0);          //      glrun->SetID(this->AssignRunID()); we use the old run number!
3488            glrun->SetID_RUN_FRAG(glrun->GetID());
3489          glrun->Fill_GL_RUN(conn);            glrun->Fill_GL_RUN(conn);  
3490          //          //
3491          oss.str("");  //      oss.str("");
3492          oss << " SELECT ID FROM GL_RUN WHERE "  //      oss << " SELECT ID FROM GL_RUN WHERE "
3493              << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "  //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3494              << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "  //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3495              << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "  //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3496              << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "  //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3497              << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";  //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3498          //  //      //
3499          if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());  //      if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
3500          result2 = conn->Query(oss.str().c_str());  //      result2 = conn->Query(oss.str().c_str());
3501          //  //      //
3502          if ( !result2 ) throw -4;  //      if ( !result2 ) throw -4;
3503          //  //      //
3504          row2 = result2->Next();  //      row2 = result2->Next();
3505          //  //      //
3506          if ( !row2 ) throw -25;  //      if ( !row2 ) throw -25;
3507          //  //      //
3508          oss.str("");  //      oss.str("");
3509          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);
3510          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());
3511          result2 = conn->Query(oss.str().c_str());  //      result2 = conn->Query(oss.str().c_str());
3512          //  //      //
3513          if ( !result2 ) throw -4;  //      if ( !result2 ) throw -4;
3514          //          //
3515          moved++;          moved++;
3516          //          //
# Line 3282  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3520  Int_t PamelaDBOperations::CleanGL_RUN_FR
3520        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));      
3521        //        //
3522        //        //
3523        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3524        oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);  //      oss.str("");
3525        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);
3526        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());
3527        //  //       result2 = conn->Query(oss.str().c_str());
3528        if ( !result2 ) throw -4;  //      //
3529        //  //      if ( !result2 ) throw -4;
3530    //      //
3531          row = result->Next();
3532      };      };
3533    };    };
3534    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 3539  Int_t PamelaDBOperations::CleanGL_RUN_FR
3539   * 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..
3540   */   */
3541  Int_t PamelaDBOperations::ValidateRuns(){  Int_t PamelaDBOperations::ValidateRuns(){
3542      return(this->ValidateRuns(""));
3543    };
3544    
3545    /**
3546     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3547     */
3548    Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3549    //    //
3550    TSQLResult *result = 0;    TSQLResult *result = 0;
3551    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
3552    //    //
3553      UInt_t calibtime = 50;
3554      //
3555    stringstream oss;    stringstream oss;
3556    oss.str("");    oss.str("");
3557    //    //
# Line 3311  Int_t PamelaDBOperations::ValidateRuns() Line 3560  Int_t PamelaDBOperations::ValidateRuns()
3560    // =======================================================    // =======================================================
3561    UInt_t t_stop  = 0;    UInt_t t_stop  = 0;
3562    UInt_t t_start = 0;    UInt_t t_start = 0;
3563    // --------------------------------------------------------------    if ( !strcmp(valfile.Data(),"") ) {
3564    // 1) get the OBT of the last run inserted after clean-time limit      // --------------------------------------------------------------
3565    // --------------------------------------------------------------      // 1) get the OBT of the last run inserted after clean-time limit
3566    oss.str("");      // --------------------------------------------------------------
3567    oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()      oss.str("");
3568            << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3569    if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());          << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3570    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3571    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3572    if ( !result->GetRowCount() ) {      if ( !result ) throw -4;
3573            printf(" No runs to validate \n");      if ( !result->GetRowCount() ) {
3574            return(1);        printf(" No runs to validate \n");
3575    }else{        return(1);
3576          row = result->Next();      }else{
3577          t_start = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3578    };          t_start = (UInt_t)atoll(row->GetField(4));
3579    // --------------------------------------------------------------      };  
3580    // 2) get the OBT of the last validated run      // --------------------------------------------------------------
3581    // --------------------------------------------------------------      // 2) get the OBT of the last validated run
3582    oss.str("");      // --------------------------------------------------------------
3583    oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start      oss.str("");
3584        <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3585    if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3586    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3587    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3588    if ( result->GetRowCount() ){      if ( !result ) throw -4;
3589            row = result->Next();      if ( result->GetRowCount() ){
3590            t_stop = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3591          t_stop = (UInt_t)atoll(row->GetField(4));
3592        };
3593        if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);
3594        // --------------------------------------------------------------
3595        // now retrieves runs to be validated
3596        // --------------------------------------------------------------
3597        oss.str("");
3598        oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;
3599        oss << " AND RUNHEADER_TIME >="<< t_stop;
3600        oss << " ORDER BY RUNHEADER_TIME DESC;";
3601        if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3602        result = conn->Query(oss.str().c_str());
3603      } else {
3604        //
3605        stringstream myquery;
3606        UInt_t myid = 0;
3607        myquery.str("");
3608        myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3609        //
3610        result = conn->Query(myquery.str().c_str());
3611        //
3612        row = result->Next();      
3613        if( !row ){
3614          if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3615            if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3616            return(2);
3617          };
3618          if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3619          return(0);
3620        };
3621        myid=(UInt_t)atoll(row->GetField(0));
3622        //
3623        myquery.str("");
3624        myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3625        //
3626        result = conn->Query(myquery.str().c_str());
3627        //
3628        row = result->Next();      
3629        if( !row->GetField(0) || !row->GetField(1)){
3630          //
3631          if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3632          //
3633          return(0);
3634          //
3635        } else {  
3636          //
3637          UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3638          UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3639          UInt_t caltime = 0;
3640          //
3641          myquery.str("");
3642          myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3643          myquery << " order by FROM_TIME asc limit 1;";
3644          //
3645          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3646          //
3647          //
3648          result = conn->Query(myquery.str().c_str());
3649          //
3650          row = result->Next();      
3651          if( !row ){
3652            caltime = runhtime;
3653          } else {
3654            caltime = (UInt_t)atoll(row->GetField(0));
3655          };
3656          //
3657          myquery.str("");
3658          myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3659          myquery << caltime << " order by RUNHEADER_TIME DESC";
3660          //
3661          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3662          //
3663          result = conn->Query(myquery.str().c_str());
3664          //
3665        };
3666    };    };
3667    if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);    //
   // --------------------------------------------------------------  
   // now retrieves runs to be validated  
   // --------------------------------------------------------------  
   oss.str("");  
   oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;  
   oss << " AND RUNHEADER_TIME >="<< t_stop;  
   oss << " ORDER BY RUNHEADER_TIME DESC;";  
 //  if ( IsDebug() )  
   if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());  
   result = conn->Query(oss.str().c_str());  
3668    if ( !result ) throw -4;    if ( !result ) throw -4;
3669    if ( !result->GetRowCount() ) printf(" No runs to validate \n");    if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3670  //  printf("------------------------------------------------------------------------------- \n");    //
     
3671    Int_t nrow = 0;    Int_t nrow = 0;
3672    GL_RUN* this_run = new GL_RUN();    GL_RUN* this_run = new GL_RUN();
3673    GL_RUN* next_run = new GL_RUN();    GL_RUN* next_run = new GL_RUN();
# Line 3368  Int_t PamelaDBOperations::ValidateRuns() Line 3681  Int_t PamelaDBOperations::ValidateRuns()
3681    UInt_t t1=0,t2=0;    UInt_t t1=0,t2=0;
3682    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
3683    // - loop over runs, back in time,    // - loop over runs, back in time,
3684    // - select sequences of runs close in time (less than 60 s apart),    // - select sequences of runs close in time (less than calibtime s apart),
3685    //   which could be preceeded by a calibration    //   which could be preceeded by a calibration
3686    // - check if there might be a missing calibration    // - check if there might be a missing calibration
3687    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
# Line 3408  Int_t PamelaDBOperations::ValidateRuns() Line 3721  Int_t PamelaDBOperations::ValidateRuns()
3721                                                        
3722                            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
3723                                                        
3724                            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
3725                                                        
3726                            if( !CHECK && this_run->VALIDATION ){                            if( !CHECK && this_run->VALIDATION ){
3727                                    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 3836  Int_t PamelaDBOperations::assignVALIDATI
3836    
3837    
3838    
3839    // Insert TLEs from file tlefilename in the table GL_TLE in the db
3840    // opened by conn, sorting them by date from older to newer, if each
3841    // TLE has not been alread inserted.
3842    Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
3843    {
3844      fstream tlefile(tlefilename, ios::in);
3845    
3846      if ( !tlefile ) throw -7;
3847    
3848      vector<cTle*> ctles;
3849      vector<cTle*>::iterator iter;
3850      int present = 0;
3851    
3852      // Get three lines from tlefile, create a cTle object and put it
3853      // into ctles
3854      while(1) {
3855        cTle *tlef;
3856        string str1, str2, str3;
3857    
3858        getline(tlefile, str1);
3859        if(tlefile.eof()) break;
3860    
3861        getline(tlefile, str2);
3862        if(tlefile.eof()) break;
3863    
3864        getline(tlefile, str3);
3865        if(tlefile.eof()) break;
3866    
3867        // We now have three good lines for a cTle.
3868        tlef = new cTle(str1, str2, str3);
3869        ctles.push_back(tlef);
3870      }
3871    
3872      tlefile.close();
3873    
3874      // Sort by date
3875      sort(ctles.begin(), ctles.end(), compTLE);
3876    
3877      // Now we insert each TLE into the db
3878      for(iter = ctles.begin(); iter != ctles.end(); iter++) {
3879        cTle *tle = *iter;
3880    
3881        // Do nothing if it's already present in the db.  Just increase
3882        // the counter present.
3883        if (! isTlePresent(tle))
3884          {
3885            int status = insertTle(tle);
3886    
3887            // Insert query failed.  Return 1.
3888            if(status == EXIT_FAILURE) {
3889              
3890              if( IsDebug() ) {
3891                cerr << "Error: inserting TLE:" << endl
3892                     << tle->getName() << endl
3893                     << tle->getLine1() << endl
3894                     << tle->getLine2() << endl;
3895              }
3896    
3897              throw -4;
3898              return 1;
3899            }
3900    
3901          }
3902        else
3903          present++;
3904    
3905      }
3906    
3907      int inserted = ctles.size() - present;  // Number of inserted TLE.
3908      if ( IsDebug() )
3909        cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
3910             << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
3911    
3912      ctles.clear();
3913    
3914    
3915      // Return 2 if no new TLE has been inserted.  0 otherwise.
3916      if(! inserted ) return 2;
3917      return 0;
3918    }
3919    
3920    
3921    // Insert tle in the table GL_TLE using the connection conn.
3922    Int_t PamelaDBOperations::insertTle(cTle *tle)
3923    {
3924      stringstream oss;
3925      TSQLResult *result = 0;
3926    
3927      oss.str("");
3928      oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
3929          << " VALUES ( '"
3930          << tle->getName() << "', '"
3931          << tle->getLine1() << "', '"
3932          << tle->getLine2() << "', '"
3933          << getTleDatetime(tle) << "')";
3934    
3935      //  cout << oss.str().c_str() << endl;
3936      result = conn->Query(oss.str().c_str());
3937      if (result == NULL)
3938        return EXIT_FAILURE;
3939    
3940      return EXIT_SUCCESS;
3941    }
3942    
3943    
3944    // Return whether tle is already in the db connected by conn.
3945    bool PamelaDBOperations::isTlePresent(cTle *tle)
3946    {
3947      stringstream oss;
3948      TSQLResult *result = 0;
3949    
3950      oss.str("");
3951      oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
3952          << getTleDatetime(tle) << "'";
3953    
3954      result = conn->Query(oss.str().c_str());
3955      if (result == NULL) throw -4;
3956    
3957      if (result->GetRowCount())
3958        return true;
3959      else
3960        return false;
3961    }
3962    
3963    
3964    // Return whether the first TLE is dated early than the second
3965    bool compTLE (cTle *tle1, cTle *tle2)
3966    {
3967      return getTleJulian(tle1) < getTleJulian(tle2);
3968    }
3969    
3970    
3971    // Return the date of the tle using the format (year-2000)*1e3 +
3972    // julian day.  e.g. 6365 is the 31th Dec 2006.
3973    // It does *not* return a cJulian date.
3974    float getTleJulian(cTle *tle) {
3975      return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
3976    }
3977    
3978    
3979    // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
3980    // format.
3981    string getTleDatetime(cTle *tle)
3982    {
3983      int year, mon, day, hh, mm, ss;
3984      double dom; // day of month (is double!)
3985      stringstream date; // date in datetime format
3986    
3987      // create a cJulian from the date in tle
3988      cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
3989    
3990      // get year, month, day of month
3991      jdate.getComponent(&year, &mon, &dom);
3992    
3993      // build a datetime YYYY-MM-DD hh:mm:ss
3994      date.str("");
3995      day = (int) floor(dom);
3996      hh = (int) floor( (dom - day) * 24);
3997      mm = (int) floor( ((dom - day) * 24 - hh) * 60);
3998      ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
3999      //  ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
4000    
4001      date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
4002    
4003      return date.str();
4004    }
4005    
4006    /**
4007     * Remove a file from the DB, delete on cascade all entries related to that file
4008     * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
4009     * calibration
4010     **/
4011    Int_t PamelaDBOperations::removeFile(TString remfile){
4012      //
4013      // Determine ID_ROOT_L0 and ID_RAW
4014      //
4015      TSQLResult *pResult;
4016      TSQLRow *Row;
4017      stringstream myquery;
4018      //  
4019      myquery.str("");
4020      myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
4021      //
4022      pResult = conn->Query(myquery.str().c_str());
4023      //
4024      Row = pResult->Next();      
4025      if( !Row ){
4026        if ( strcmp(remfile.Data(),GetRootName().Data()) ){
4027          if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
4028          return(1);
4029        };
4030        if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
4031        return(0);
4032      };
4033      //
4034      this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
4035      this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
4036      //
4037      this->ValidationOFF();
4038      //
4039      this->RemoveCALIBS();
4040      //
4041      this->RemoveRUNS();
4042      //
4043      this->RemoveFILES();
4044      //
4045      this->SetID_ROOT(0);
4046      this->SetID_RAW(0);
4047      //
4048      return(0);
4049    };
4050    
4051    /**
4052     *
4053     * Set validation bit to zero for runs following the removing file till
4054     * 1) a run with TRK_CALIB_USED=140
4055     * 2) a run with VALIDATION = 0
4056     * 3) the next calibration
4057     *
4058     **/
4059    void PamelaDBOperations::ValidationOFF(){
4060      TSQLResult *pResult;
4061      TSQLRow *Row;
4062      stringstream myquery;
4063      Int_t unv = 0;
4064      //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
4065      myquery.str("");
4066      myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4067      //
4068      pResult = conn->Query(myquery.str().c_str());
4069      //
4070      Row = pResult->Next();      
4071      if( !Row->GetField(0) ){
4072        //
4073        if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
4074        //
4075      } else {  
4076        //
4077        UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
4078        UInt_t caltime = 0;
4079        //
4080        myquery.str("");
4081        myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
4082        myquery << " order by FROM_TIME asc limit 1;";
4083        //
4084        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4085        //
4086        //
4087        delete pResult;
4088        pResult = conn->Query(myquery.str().c_str());
4089        //
4090        Row = pResult->Next();      
4091        if( !Row ){
4092          caltime = runhtime;
4093        } else {
4094          caltime = (UInt_t)atoll(Row->GetField(0));
4095        };
4096        //
4097        myquery.str("");
4098        myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4099        myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4100        //
4101        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4102        //
4103        pResult = conn->Query(myquery.str().c_str());
4104        //
4105        Row = pResult->Next();      
4106        if( !Row ){
4107          //
4108          if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4109          //
4110        } else {
4111          myquery.str("");
4112          myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4113          myquery << " RUNHEADER_TIME>=" <<runhtime;
4114          myquery << " order by RUNHEADER_TIME asc;";
4115          //
4116          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4117          //
4118          pResult = conn->Query(myquery.str().c_str());
4119          //
4120          Row = pResult->Next();  
4121          while ( Row ){
4122            //      
4123            unv++;
4124            this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4125            Row = pResult->Next();  
4126            //
4127          };
4128        };
4129      };
4130      if ( IsDebug() ) printf(" %i runs have been unvalidated \n",unv);
4131    };
4132    
4133    /**
4134     *
4135     * Rearrange GL_RUN table and remove runs
4136     *
4137     **/
4138    void PamelaDBOperations::RemoveRUNS(){
4139      TSQLResult *pResult;
4140      TSQLRow *Row;
4141      stringstream myquery;
4142      UInt_t drun = 0;
4143      GL_RUN *delrun = new GL_RUN();
4144      //
4145      myquery.str("");
4146      myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4147      //
4148      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4149      //
4150      pResult = conn->Query(myquery.str().c_str());
4151      //
4152      Row = pResult->Next();    
4153      //
4154      //
4155      if ( !Row ){
4156        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4157      } else {
4158        if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");    
4159        while ( Row ){
4160          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4161          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4162          drun++;
4163          Row = pResult->Next();    
4164        };
4165      };
4166      //
4167      //
4168      myquery.str("");
4169      myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4170      //
4171      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4172      //
4173      pResult = conn->Query(myquery.str().c_str());
4174      //
4175      Row = pResult->Next();    
4176      //
4177      if ( !Row ){
4178        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4179      } else {
4180        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");    
4181        while ( Row ){
4182          if ( IsDebug() ) printf(" restore run %i \n",(UInt_t)atoll(Row->GetField(1)));
4183          delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4184          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(1)));
4185          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4186          if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4187            if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4188            delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4189          };
4190          drun++;
4191          Row = pResult->Next();    
4192        };
4193      };
4194      //
4195      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);    
4196      //
4197      //
4198      //
4199      drun = 0;
4200      //
4201      myquery.str("");
4202      myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4203      //
4204      pResult = conn->Query(myquery.str().c_str());
4205      //
4206      Row = pResult->Next();    
4207      //
4208      if ( !Row ){
4209        if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4210      } else {
4211        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");    
4212        while ( Row ){
4213          if ( IsDebug() ) printf(" del run idtrash %i \n",(UInt_t)atoll(Row->GetField(0)));
4214          myquery.str("");
4215          myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";      
4216          conn->Query(myquery.str().c_str());
4217          drun++;
4218          Row = pResult->Next();    
4219        };
4220      };
4221      //  
4222      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_TRASH table \n",drun);    
4223      //
4224      //
4225      //
4226      drun = 0;
4227      //
4228      myquery.str("");
4229      myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4230      //
4231      pResult = conn->Query(myquery.str().c_str());
4232      //
4233      Row = pResult->Next();    
4234      //
4235      if ( !Row ){
4236        if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4237      } else {
4238        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");    
4239        while ( Row ){
4240          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4241          myquery.str("");
4242          myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4243          conn->Query(myquery.str().c_str());
4244          drun++;
4245          Row = pResult->Next();    
4246        };
4247      };
4248      //  
4249      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_FRAGMENTS table \n",drun);    
4250      //
4251      //
4252      //
4253      delete delrun;
4254      //
4255    };
4256    
4257    
4258    /**
4259     *
4260     * Rearrange calibration tables
4261     *
4262     **/
4263    void PamelaDBOperations::RemoveFILES(){
4264      stringstream myquery;
4265      //
4266      myquery.str("");
4267      myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4268      //
4269      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4270      //
4271      conn->Query(myquery.str().c_str());
4272      //
4273    };
4274    
4275    /**
4276     *
4277     * Rearrange calibration tables
4278     *
4279     **/
4280    void PamelaDBOperations::RemoveCALIBS(){
4281      TSQLResult *pResult;
4282      TSQLRow *Row;
4283      stringstream myquery;
4284      //  
4285      //
4286      // Calorimeter
4287      //
4288      for (Int_t section = 0; section < 4; section++){
4289        myquery.str("");
4290        myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4291        myquery << " SECTION=" << section << ";";
4292        //
4293        pResult = conn->Query(myquery.str().c_str());
4294        //
4295        Row = pResult->Next();      
4296        if( !Row->GetField(0) || !Row->GetField(1) ){
4297          //
4298          if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4299          //
4300        } else {
4301          //
4302          myquery.str("");
4303          myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4304          myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4305          myquery << " SECTION=" << section << ";";
4306          //
4307          pResult = conn->Query(myquery.str().c_str());
4308          //
4309          if( !pResult ){
4310            //
4311            if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4312            //
4313            throw -4;
4314            //
4315          };
4316          //
4317        };
4318      };
4319      myquery.str("");
4320      myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4321      //
4322      pResult = conn->Query(myquery.str().c_str());
4323      //
4324      if( !pResult ){
4325        //
4326        if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4327        //
4328        throw -4;
4329        //
4330      };
4331      //
4332      // Tracker
4333      //
4334      myquery.str("");
4335      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4336      //
4337      pResult = conn->Query(myquery.str().c_str());
4338      //
4339      Row = pResult->Next();      
4340      if( !Row->GetField(0) || !Row->GetField(1) ){
4341        //
4342        if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4343        //
4344      } else {
4345        //
4346        myquery.str("");
4347        myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4348        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4349        //
4350        pResult = conn->Query(myquery.str().c_str());
4351        //
4352        if( !pResult ){
4353          //
4354          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4355          //
4356          throw -4;
4357          //
4358        };
4359        //
4360        myquery.str("");
4361        myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4362        //
4363        pResult = conn->Query(myquery.str().c_str());
4364        //
4365        if( !pResult ){
4366          //
4367          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4368          //
4369          throw -4;
4370          //
4371        };
4372      };
4373      //
4374      //
4375      // S4
4376      //
4377      myquery.str("");
4378      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4379      //
4380      pResult = conn->Query(myquery.str().c_str());
4381      //
4382      Row = pResult->Next();      
4383      if( !Row->GetField(0) || !Row->GetField(1) ){
4384        //
4385        if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4386        //
4387      } else {
4388        //
4389        myquery.str("");
4390        myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4391        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4392        //
4393        pResult = conn->Query(myquery.str().c_str());
4394        //
4395        if( !pResult ){
4396          //
4397          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4398          //
4399          throw -4;
4400          //
4401        };
4402        //
4403        myquery.str("");
4404        myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4405        //
4406        pResult = conn->Query(myquery.str().c_str());
4407        //
4408        if( !pResult ){
4409          //
4410          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4411          //
4412          throw -4;
4413          //
4414        };
4415        //
4416      };
4417    };

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

  ViewVC Help
Powered by ViewVC 1.1.23