/[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.5 by pam-fi, Fri Sep 8 14:42:48 2006 UTC revision 1.19 by mocchiut, Thu Nov 9 16:47:47 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    //    //
   SetRootName(filerootname);  
   SetRawName(filerawname);  
75    //    //
76    this->OpenFile();    INSERT_RAW =!filerawname.IsNull();
77      if(INSERT_RAW)SetRawName(filerawname);
78      //
79      INSERT_ROOT = !filerootname.IsNull();
80      if( INSERT_ROOT ){
81        this->SetRootName(filerootname);
82        this->SetOrbitNo();
83        file = TFile::Open(this->GetRootName().Data());
84      } else {
85        this->SetRootName("");
86      };
87    //    //
88    this->SetID_RAW(0);    this->SetID_RAW(0);
89    this->SetID_ROOT(0);    this->SetID_ROOT(0);
90    
91      VALIDATE = false;
92      
93    //    //
94  };  };
95    
# Line 78  PamelaDBOperations::PamelaDBOperations(T Line 98  PamelaDBOperations::PamelaDBOperations(T
98   */   */
99  void PamelaDBOperations::Close(){  void PamelaDBOperations::Close(){
100    if( conn && conn->IsConnected() ) conn->Close();    if( conn && conn->IsConnected() ) conn->Close();
101      delete clean_time;
102    delete glrun;    delete glrun;
103    delete this;    delete this;
104  };  };
# Line 86  void PamelaDBOperations::Close(){ Line 107  void PamelaDBOperations::Close(){
107  // SETTERS  // SETTERS
108  //  //
109    
110    //
111    // must be out of the constructor in order to FORCE the validation of the latest runs in case you run the validation together with the latest file
112    //
113    void PamelaDBOperations::CheckValidate(Long64_t olderthan){
114      clean_time = new TDatime();
115      //
116      if(olderthan >= 0){
117        VALIDATE = true;
118        UInt_t timelim = 0;
119        timelim =  (UInt_t)clean_time->Convert(true) - olderthan;
120        clean_time->Set(timelim,false);
121      };
122    };
123    
124  /**  /**
125   * Open the DB connection   * Open the DB connection
126   * @param host         hostname for the SQL connection.   * @param host         hostname for the SQL connection.
# Line 93  void PamelaDBOperations::Close(){ Line 128  void PamelaDBOperations::Close(){
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 121  void PamelaDBOperations::SetDebugFlag(Bo Line 157  void PamelaDBOperations::SetDebugFlag(Bo
157  };  };
158    
159  /**  /**
160     * Set the nofrag flag
161     *
162     */
163    void PamelaDBOperations::SetNoFrag(Bool_t nf){
164      NOFRAG = nf;
165    };
166    
167    /**
168   * Store the BOOT number of the RAW file.   * Store the BOOT number of the RAW file.
169   * @param boot    BOOT number of the RAW file   * @param boot    BOOT number of the RAW file
170   */   */
# Line 161  void PamelaDBOperations::SetRootName(TSt Line 205  void PamelaDBOperations::SetRootName(TSt
205  };  };
206    
207  /**  /**
208     * Store the downlink orbit number from filename.
209     */
210    void PamelaDBOperations::SetOrbitNo(){
211      dworbit = 0;
212      TString name = this->GetRootFile();
213      Int_t nlength = name.Length();
214      if ( nlength < 5 ) return;
215      TString dwo = 0;
216      for (Int_t i = 0; i<5; i++){
217        dwo.Append(name[i],1);
218      };
219      if ( dwo.IsDigit() ){
220        dworbit = (UInt_t)dwo.Atoi();
221      } else {
222        dwo="";
223        for (Int_t i = 8; i<13; i++){
224          dwo.Append(name[i],1);
225        };    
226        if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
227      };
228      if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
229      return;
230    };
231    
232    
233    
234    /**
235   * Store the NOBOOT flag.   * Store the NOBOOT flag.
236   * @param noboot    true/false.   * @param noboot    true/false.
237   */   */
# Line 169  void PamelaDBOperations::SetNOBOOT(Bool_ Line 240  void PamelaDBOperations::SetNOBOOT(Bool_
240  };  };
241    
242  /**  /**
243     * Store path to the TLE file.
244     */
245    void PamelaDBOperations::SetTLEPath(TString str){
246      tlefilename = str;
247    };
248    
249    /**
250   * Store the olderthan variable   * Store the olderthan variable
251   * @param olderthan   * @param olderthan
252   */   */
253  void PamelaDBOperations::SetOlderThan(Long64_t oldthan){  // void PamelaDBOperations::SetOlderThan(Long64_t oldthan){
254    olderthan = oldthan;  //   olderthan = oldthan;
255  };  // };
256    
257  /**  /**
258   * Retrieve the ID_RAW, if exists, returns NULL if does not exist.   * Retrieve the ID_RAW, if exists, returns NULL if does not exist.
# Line 225  Int_t PamelaDBOperations::SetUpperLimits Line 303  Int_t PamelaDBOperations::SetUpperLimits
303    UInt_t jump = 50000; // was 5000    UInt_t jump = 50000; // was 5000
304    EventCounter *code=0;    EventCounter *code=0;
305    //    //
306      UInt_t deltapkt = 5000;
307      ULong64_t deltaobt = 50000;
308      //
309    //   pcksList packetsNames;    //   pcksList packetsNames;
310    //   pcksList::iterator Iter;    //   pcksList::iterator Iter;
311    //   getPacketsNames(packetsNames);    //   getPacketsNames(packetsNames);
# Line 271  Int_t PamelaDBOperations::SetUpperLimits Line 352  Int_t PamelaDBOperations::SetUpperLimits
352    //    //
353    if ( nevent < 2 ) return(4);    if ( nevent < 2 ) return(4);
354    //    //
355    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) ){
356        //
357      // go back      // go back
358      zomp = nevent - 2;      zomp = nevent - 2;
359      //      //
# Line 320  Int_t PamelaDBOperations::SetUpperLimits Line 402  Int_t PamelaDBOperations::SetUpperLimits
402    // check if last runtrailer is within limits, if not extend limits (one should check for all packets but we need only runtrailer)    // check if last runtrailer is within limits, if not extend limits (one should check for all packets but we need only runtrailer)
403    //    //
404    PacketType *pctp=0;    PacketType *pctp=0;
   T->GetEntry(upperentry);  
   code = eh->GetCounter();  
   Int_t lasttrail = code->Get(pctp->RunTrailer);  
   Int_t lasthead = code->Get(pctp->RunHeader);  
405    TTree *rh=(TTree*)file->Get("RunHeader");    TTree *rh=(TTree*)file->Get("RunHeader");
406    if ( !rh || rh->IsZombie() ) throw -17;    if ( !rh || rh->IsZombie() ) throw -17;
407    TTree *rt=(TTree*)file->Get("RunTrailer");    TTree *rt=(TTree*)file->Get("RunTrailer");
# Line 346  Int_t PamelaDBOperations::SetUpperLimits Line 424  Int_t PamelaDBOperations::SetUpperLimits
424    UInt_t pkth = 0;    UInt_t pkth = 0;
425    ULong64_t obth = 0;    ULong64_t obth = 0;
426    //    //
427      T->GetEntry(upperentry);
428      code = eh->GetCounter();
429      Int_t lasttrail = code->Get(pctp->RunTrailer);
430      Int_t lasthead = code->Get(pctp->RunHeader);
431    if ( lasttrail < rtev ){    if ( lasttrail < rtev ){
432      rt->GetEntry(lasttrail);      rt->GetEntry(lasttrail);
433      pht = eht->GetPscuHeader();      pht = eht->GetPscuHeader();
# Line 356  Int_t PamelaDBOperations::SetUpperLimits Line 438  Int_t PamelaDBOperations::SetUpperLimits
438    if ( lasthead < rhev ){    if ( lasthead < rhev ){
439      rh->GetEntry(lasthead);      rh->GetEntry(lasthead);
440      phh = ehh->GetPscuHeader();      phh = ehh->GetPscuHeader();
441      pkth = PKT(pht->GetCounter());      pkth = PKT(phh->GetCounter());
442      obth = OBT(pht->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
443    };    };
444    //    //
445    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 %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
# Line 401  Int_t PamelaDBOperations::SetUpperLimits Line 483  Int_t PamelaDBOperations::SetUpperLimits
483      pkth = PKT(phh->GetCounter());      pkth = PKT(phh->GetCounter());
484      obth = OBT(phh->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
485      //      //
486    //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
487        //
488      if ( pkth < spkth && obth < sobth ){      if ( pkth < spkth && obth < sobth ){
       if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);  
489        if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);        if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);
490        //        //
491        rhev--;        rhev = k-1;
492        rh->GetEntry(rhev);        rh->GetEntry(rhev);
493        pkth = spkth;        pkth = spkth;
494        obth = sobth;        obth = sobth;
       if ( IsDebug() ) printf(" lasthead %i pt %i p1 %i ot %u o1 %u \n",rhev,pkth,spkth,obth,sobth);  
495        //        //
496        UInt_t evbefh = 0;        UInt_t evbefh = 0;
497        code = ehh->GetCounter();        code = ehh->GetCounter();
# Line 419  Int_t PamelaDBOperations::SetUpperLimits Line 501  Int_t PamelaDBOperations::SetUpperLimits
501          ph = eh->GetPscuHeader();          ph = eh->GetPscuHeader();
502          t_pktlast = PKT(ph->GetCounter());          t_pktlast = PKT(ph->GetCounter());
503          t_obtlast = OBT(ph->GetOrbitalTime());          t_obtlast = OBT(ph->GetOrbitalTime());
504          if ( t_pktlast < spkth && t_obtlast < sobth ){ // jump          if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
505            upperpkt = pkth;            upperpkt = pkth;
506            upperobt = obth;            upperobt = obth;
507            upperentry = evbefh-1;            upperentry = evbefh-1;
# Line 439  Int_t PamelaDBOperations::SetUpperLimits Line 521  Int_t PamelaDBOperations::SetUpperLimits
521          };              };    
522        };        };
523        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 %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
524          goto kikko0;
525      };      };
526    };    };
527     kikko0:
528    //    //
529    //    //
530    //    //
# Line 458  Int_t PamelaDBOperations::SetUpperLimits Line 542  Int_t PamelaDBOperations::SetUpperLimits
542      pktt = PKT(pht->GetCounter());      pktt = PKT(pht->GetCounter());
543      obtt = OBT(pht->GetOrbitalTime());      obtt = OBT(pht->GetOrbitalTime());
544      //      //
545    //    if ( IsDebug() ) printf(" k %i rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
546        //
547      if ( pktt < spktt && obtt < sobtt ){      if ( pktt < spktt && obtt < sobtt ){
       if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);  
548        if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);        if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
549        //        //
550        rtev--;        rtev = k-1;
551        rt->GetEntry(rtev);        rt->GetEntry(rtev);
552        pktt = spktt;        pktt = spktt;
553        obtt = sobtt;        obtt = sobtt;
# Line 476  Int_t PamelaDBOperations::SetUpperLimits Line 561  Int_t PamelaDBOperations::SetUpperLimits
561          ph = eh->GetPscuHeader();          ph = eh->GetPscuHeader();
562          t_pktlast = PKT(ph->GetCounter());          t_pktlast = PKT(ph->GetCounter());
563          t_obtlast = OBT(ph->GetOrbitalTime());          t_obtlast = OBT(ph->GetOrbitalTime());
564          if ( t_pktlast < spktt && t_obtlast < sobtt ){ // jump          if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
565            upperpkt = pktt;            upperpkt = pktt;
566            upperobt = obtt;            upperobt = obtt;
567            upperentry = evbeft-1;            upperentry = evbeft-1;
# Line 496  Int_t PamelaDBOperations::SetUpperLimits Line 581  Int_t PamelaDBOperations::SetUpperLimits
581          };          };
582        };        };
583        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 %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
584        break;        goto kikko;
585          //      break;
586        //        //
587      };      };
588      //        //  
589    };    };
590    //    //
591    // kikko:   kikko:
592      //
593      T->GetEntry(upperentry);
594      code = eh->GetCounter();
595      lasttrail = code->Get(pctp->RunTrailer);
596      lasthead = code->Get(pctp->RunHeader);
597      if ( lasttrail < rtev ){
598        rt->GetEntry(lasttrail);
599        pht = eht->GetPscuHeader();
600        pktt = PKT(pht->GetCounter());
601        obtt = OBT(pht->GetOrbitalTime());
602      };
603      //
604      if ( lasthead < rhev ){
605        rh->GetEntry(lasthead);
606        phh = ehh->GetPscuHeader();
607        pkth = PKT(phh->GetCounter());
608        obth = OBT(phh->GetOrbitalTime());
609      };
610      //
611      if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
612      if ( pkth > upperpkt && obth > upperobt ){
613        if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);
614        upperpkt = pkth;
615        upperobt = obth;
616        rhev = lasthead+1;
617      } else {
618        rhev = lasthead;
619      };
620      if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
621      //
622      if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
623      if ( pktt > upperpkt && obtt > upperobt ){
624        if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);
625        upperpkt = pktt;
626        upperobt = obtt;
627        rtev = lasttrail+1;
628      } else {
629        rtev = lasttrail;
630      };
631      if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
632    //    //
633    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 %llu pkt_num %i upper entry %i \n",upperobt,upperpkt,upperentry);
634    //    //
635    return(0);    return(0);
636  }  }
637    
638    /**
639     *
640     * Trick to have unique RUN ID numbers even when runs are deleted and mysql deamon restarted.
641     * Entries in the _RUNID_GEN table are never deleted.
642     *
643     **/
644    UInt_t PamelaDBOperations::AssignRunID(){
645      //
646      TSQLResult *result = 0;
647      TSQLRow *row = 0;
648      UInt_t runid = 0;
649      //
650      stringstream   oss;
651      //  
652      oss.str("");
653      oss << "INSERT INTO _RUNID_GEN VALUES (NULL);";
654      result = conn->Query(oss.str().c_str());
655      if ( !result ) throw -10;
656      oss.str("");
657      oss << "SELECT ID FROM _RUNID_GEN ORDER BY ID DESC LIMIT 1;";
658      result = conn->Query(oss.str().c_str());
659      if ( !result ) throw -10;
660      //
661      row = result->Next();
662      //
663      if ( !row ) throw -28;
664      //
665      runid = (UInt_t)atoll(row->GetField(0));
666      //
667      return(runid);
668    };
669    
670  //  //
671  // GETTERS  // GETTERS
672  //  //
# Line 584  const PacketType* PamelaDBOperations::Ge Line 742  const PacketType* PamelaDBOperations::Ge
742  // PRIVATE FUNCTIONS  // PRIVATE FUNCTIONS
743  //  //
744    
745  /**  // /**
746   * Open the ROOT filename for reading  // * Open the ROOT filename for reading
747   */  // */
748  void PamelaDBOperations::OpenFile(){  // void PamelaDBOperations::OpenFile(){
749    file = TFile::Open(this->GetRootName().Data());  //   file = TFile::Open(this->GetRootName().Data());
750    //   //
751    
752    void PamelaDBOperations::CheckFile(){  
753      if ( !file ) throw -12;
754  };  };
755    
756    
757  /**  /**
758   * Check if LEVEL0 file and DB connection have really be opened   * Check if LEVEL0 file and DB connection have really be opened
759   */   */
760  void PamelaDBOperations::CheckFile(){    void PamelaDBOperations::CheckConnection(){  
   //  
   if ( !file ) throw -12;  
761    //    //
762    // check connection    // check connection
763    //    //
764    if( !conn ) throw -1;        if( !conn ) throw -1;
765    bool connect = conn->IsConnected();    bool connect = conn->IsConnected();
766    if( !connect ) throw -1;        if( !connect ) throw -1;
767      if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
768      //
769      // set DB timezone to UTC
770      //
771      stringstream oss;
772      //  
773      oss.str("");
774      oss << "SET time_zone='+0:00';";
775      TSQLResult *result = 0;
776      result = conn->Query(oss.str().c_str());
777      if ( !result ) throw -10;
778      //
779  };  };
780    
781  /**  /**
# Line 783  Int_t PamelaDBOperations::insertPamelaGL Line 955  Int_t PamelaDBOperations::insertPamelaGL
955    if ( this->GetID_RAW() == 0 )  throw -11;    if ( this->GetID_RAW() == 0 )  throw -11;
956    //    //
957    oss.str("");    oss.str("");
958    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='"
959        << id << " AND TO_ID_RAW >= "        << this->GetRawFile().Data() << "';";
       << id << ";";  
960    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
961    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
962    if ( !result ) throw -10;    if ( !result ) throw -10;
963    row = result->Next();    row = result->Next();
   if ( !row ) throw -10;  
964    //    //
965    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();    if ( !row ){
966        oss.str("");
967        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< "
968            << dworbit << " order by FROM_ORBIT desc limit 1;";
969        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
970        result = conn->Query(oss.str().c_str());
971        if ( !result ) throw -10;
972        row = result->Next();
973        if ( !row ) throw -10;
974      };
975      //
976      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);
977      t0 = (UInt_t)tu.GetSec();
978      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));
979      //
980    /*    /*
981     * Verify that the TIMESYNC have been not already processed     * Verify that the TIMESYNC have been not already processed
982     */     */
# Line 807  Int_t PamelaDBOperations::insertPamelaGL Line 991  Int_t PamelaDBOperations::insertPamelaGL
991    if (result == NULL) throw -10;    if (result == NULL) throw -10;
992    row = result->Next();    row = result->Next();
993    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
994        if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
995      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;
996      return(1);      return(1);
997    };    };
# Line 864  Int_t PamelaDBOperations::insertPamelaGL Line 1049  Int_t PamelaDBOperations::insertPamelaGL
1049          //          //
1050          TYPE = 55;//224;          TYPE = 55;//224;
1051          //          //
1052            if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
1053            //
1054          if ( TSYNC && OBT ){          if ( TSYNC && OBT ){
1055            existsts = true;            existsts = true;
1056            goto eout;            goto eout;
# Line 886  Int_t PamelaDBOperations::insertPamelaGL Line 1073  Int_t PamelaDBOperations::insertPamelaGL
1073      //      //
1074      rt->SetBranchAddress("RunTrailer", &runt);      rt->SetBranchAddress("RunTrailer", &runt);
1075      //      //
1076      if ( rhev > 0 ){      Int_t nrhev = rh->GetEntries();
1077        rh->GetEntry(0);      Int_t nrtev = rt->GetEntries();
1078        //      if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev);
1079        TSYNC = runh->LAST_TIME_SYNC_INFO;      //
1080        OBT = runh->OBT_TIME_SYNC * 1000;      if ( nrhev > 0 ){
1081        //        for (Int_t i=0; i<nrhev; i++){
1082        TYPE = 20;          //
1083        //          rh->GetEntry(i);
1084        if ( TSYNC && OBT ){          //
1085          existsts = true;          TSYNC = runh->LAST_TIME_SYNC_INFO;
1086          goto eout;          OBT = runh->OBT_TIME_SYNC * 1000;
1087            //
1088            TYPE = 20;
1089            //
1090            if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
1091            //
1092            if ( TSYNC && OBT ){
1093              existsts = true;
1094              goto eout;
1095            };
1096        };        };
1097        //        //
1098      };      };
1099      if ( rtev > 0 ){      if ( nrtev > 0 ){
1100        //        //
1101        if ( IsDebug() ) printf(" No runheader \n");        if ( IsDebug() ) printf(" No runheader \n");
1102        signal = 6;        signal = 6;
1103        //        //
1104        rt->GetEntry(0);        for (Int_t i=0; i<nrtev; i++){
1105        //          //
1106        TSYNC = runt->LAST_TYME_SYNC_INFO;          rt->GetEntry(i);
1107        OBT = runt->OBT_TYME_SYNC * 1000;          //
1108        //          TSYNC = runt->LAST_TYME_SYNC_INFO;
1109        TYPE = 21;          OBT = runt->OBT_TYME_SYNC * 1000;
1110        //          //
1111        if ( TSYNC && OBT ){          TYPE = 21;
1112          existsts = true;          //
1113          goto eout;          if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
1114            //
1115            if ( TSYNC && OBT ){
1116              existsts = true;
1117              goto eout;
1118            };
1119        };        };
1120        //        //
1121      } else {      } else {
# Line 985  Int_t PamelaDBOperations::insertPamelaGL Line 1186  Int_t PamelaDBOperations::insertPamelaGL
1186    conn->Query(oss.str().c_str());    conn->Query(oss.str().c_str());
1187    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());
1188    //    //
1189      if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1190    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1191    //    //
1192    delete result;    delete result;
# Line 1381  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1583  Bool_t PamelaDBOperations::IsRunAlreadyI
1583      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1584        //        //
1585        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");
1586        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1587        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1588        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) <<";";
1589        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1590    //       conn->Query(oss.str().c_str());
1591        if ( signal ) signal = false;        if ( signal ) signal = false;
1592        goto gonext;              goto gonext;      
1593        //        //
# Line 1398  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1601  Bool_t PamelaDBOperations::IsRunAlreadyI
1601        //        //
1602        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");
1603        //        //
1604        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1605        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1606        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) <<";";
1607        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1608    //       conn->Query(oss.str().c_str());
1609        //        //
1610        if ( signal ) signal = false;        if ( signal ) signal = false;
1611        goto gonext;        goto gonext;
# Line 1415  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1619  Bool_t PamelaDBOperations::IsRunAlreadyI
1619        //        //
1620        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");
1621        //        //
1622        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1623        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1624        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) <<";";
1625        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1626    //       conn->Query(oss.str().c_str());
1627        if ( signal ) signal = false;        if ( signal ) signal = false;
1628        //        //
1629      };      };
# Line 1454  void PamelaDBOperations::HandleRun(){   Line 1659  void PamelaDBOperations::HandleRun(){  
1659      //      //
1660      this->FillClass();      this->FillClass();
1661      //      //
1662      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);      if ( !IsRunAlreadyInserted() ){
1663          glrun->SetID(this->AssignRunID());
1664          glrun->SetID_RUN_FRAG(0);
1665          glrun->Fill_GL_RUN(conn);
1666        };
1667    } else {    } else {
1668      //      //
1669      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 1477  void PamelaDBOperations::HandleRunFragme Line 1686  void PamelaDBOperations::HandleRunFragme
1686    UInt_t rhfirstev = firstev;    UInt_t rhfirstev = firstev;
1687    UInt_t rtlastev = lastev;    UInt_t rtlastev = lastev;
1688    Bool_t found = false;    Bool_t found = false;
1689      Bool_t foundinrun = false;
1690    //    //
1691    TSQLResult *result = 0;    TSQLResult *result = 0;
1692    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 1503  void PamelaDBOperations::HandleRunFragme Line 1713  void PamelaDBOperations::HandleRunFragme
1713    //    //
1714    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");
1715    //    //
1716      // First of all insert the run in the fragment table...
1717      //
1718      oss.str("");
1719      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1720          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1721          << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1722          << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1723          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1724          << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1725          << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1726          << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1727          << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1728          << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1729          << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1730          << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1731          << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1732          << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1733      //
1734      if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1735      result = conn->Query(oss.str().c_str());
1736      //
1737      if ( !result ) throw -4;
1738      //
1739      row = result->Next();
1740      //
1741      if ( !row ){
1742        //
1743        // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
1744        //
1745        if ( IsDebug() ) printf(" The run is new \n");
1746        if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
1747        //
1748        glrun->SetID(this->AssignRunID());
1749        glrun->SetID_RUN_FRAG(0);
1750        glrun->Fill_GL_RUN_FRAGMENTS(conn);
1751        //
1752      } else {
1753        if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
1754      };
1755      //
1756      //
1757    // 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?
1758    //        //    
1759    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 1511  void PamelaDBOperations::HandleRunFragme Line 1762  void PamelaDBOperations::HandleRunFragme
1762      oss.str("");      oss.str("");
1763      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 "
1764          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1765          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME()          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1766            << " ID != " << glrun->ID
1767          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1768      //      //
1769      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 1521  void PamelaDBOperations::HandleRunFragme Line 1773  void PamelaDBOperations::HandleRunFragme
1773      //      //
1774      row = result->Next();      row = result->Next();
1775      //      //
1776        if ( !row && NoFrag() ){
1777          //
1778          oss.str("");
1779          oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
1780              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1781              << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1782              << " ID != " << glrun->ID
1783              << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1784          //
1785          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1786          result = conn->Query(oss.str().c_str());
1787          //
1788          if ( !result ) throw -4;
1789          //
1790          foundinrun = true;
1791          //
1792          row = result->Next();
1793          //
1794        };
1795        //
1796      if ( !row ){      if ( !row ){
1797        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1798        found = false;        found = false;
# Line 1595  void PamelaDBOperations::HandleRunFragme Line 1867  void PamelaDBOperations::HandleRunFragme
1867        //        //
1868        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 ");
1869        //        //
1870          if ( foundinrun ){
1871            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
1872            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1873          };
1874          //
1875        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
1876        //        //
1877        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
1878        //        //
1879        oss.str("");        oss.str("");
1880        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1626  void PamelaDBOperations::HandleRunFragme Line 1903  void PamelaDBOperations::HandleRunFragme
1903        };        };
1904        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);
1905        //        //
       glrun1->SetID(0);  
1906        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
1907        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
1908        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
# Line 1635  void PamelaDBOperations::HandleRunFragme Line 1911  void PamelaDBOperations::HandleRunFragme
1911        //        //
1912        glrun->SetEV_FROM(firstev);        glrun->SetEV_FROM(firstev);
1913        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
1914          //
1915        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
1916        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
1917        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
# Line 1654  void PamelaDBOperations::HandleRunFragme Line 1931  void PamelaDBOperations::HandleRunFragme
1931        //        //
1932        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
1933          //          //
1934            //      glrun->SetID(this->AssignRunID());
1935            glrun->SetID_RUN_FRAG(glrun1->GetID());
1936          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
1937          //          //
1938          // 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);  
1939          //          //
1940            glrun1->SetID_RUN_FRAG(glrun->GetID());
1941          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
1942          //          //
         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;  
         //  
1943        };        };
       //  
       delete glrun1;  
       //  
1944        // delete old entry in fragment table        // delete old entry in fragment table
1945        //        //
1946        oss.str("");        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
1947          glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
1948        //        //
1949        oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";        delete glrun1;
1950        //        //
       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;  
1951        //        //
1952        return;        return;
1953        //        //
# Line 1723  void PamelaDBOperations::HandleRunFragme Line 1956  void PamelaDBOperations::HandleRunFragme
1956    };    };
1957    //    //
1958    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
1959                                             // 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
1960      //      //
1961      oss.str("");      oss.str("");
1962      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 "
1963          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1964          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME()          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
1965            << " ID != " << glrun->ID
1966          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
1967      //      //
1968      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 1738  void PamelaDBOperations::HandleRunFragme Line 1972  void PamelaDBOperations::HandleRunFragme
1972      //      //
1973      row = result->Next();      row = result->Next();
1974      //      //
1975      if ( !row ){      if ( !row && NoFrag() ){
       if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");  
       found = false;  
     } else {  
1976        //        //
1977        found = false; // default value        oss.str("");
1978          oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
1979              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1980              << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
1981              << " ID != " << glrun->ID
1982              << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
1983        //        //
1984        if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");        if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1985          result = conn->Query(oss.str().c_str());
1986          //
1987          if ( !result ) throw -4;
1988          //
1989          foundinrun = true;
1990          row = result->Next();
1991          //
1992        };
1993        //
1994        if ( !row ){
1995          if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1996          found = false;
1997        } else {
1998          //
1999          found = false; // default value
2000          //
2001          if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
2002        //        //
2003        // if we have both runheader and runtrailer we can check with pkt_counter:        // if we have both runheader and runtrailer we can check with pkt_counter:
2004        //        //
# Line 1811  void PamelaDBOperations::HandleRunFragme Line 2064  void PamelaDBOperations::HandleRunFragme
2064        //        //
2065        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 ");
2066        //        //
2067          if ( foundinrun ){
2068            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2069            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2070          };
2071          //
2072        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
2073        //        //
2074        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2075        //        //
2076        oss.str("");        oss.str("");
2077        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1850  void PamelaDBOperations::HandleRunFragme Line 2108  void PamelaDBOperations::HandleRunFragme
2108        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
2109        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
2110        //        //
       glrun1->SetID(0);  
2111        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
2112        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
2113        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
# Line 1870  void PamelaDBOperations::HandleRunFragme Line 2127  void PamelaDBOperations::HandleRunFragme
2127        //        //
2128        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2129          //          //
2130          glrun->Fill_GL_RUN(conn);          //      glrun->SetID(this->AssignRunID());
         //  
         // get id number  
         //  
         oss.str("");  
         oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "  
             << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "  
             << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";  
         //  
         if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());  
         //  
         result =  conn->Query(oss.str().c_str());  
         if ( !result ) throw -4;  
         row = result->Next();  
2131          //          //
2132          UInt_t idrun0 = 0;          glrun->SetID_RUN_FRAG(glrun1->GetID());
2133          if ( !row ){          glrun->Fill_GL_RUN(conn);
           throw -10;  
         } else {  
           idrun0 = (UInt_t)atoll(row->GetField(0));  
         };  
2134          //          //
2135          glrun1->SetID_RUN_FRAG(idrun0);          // set id number
2136          //          //
2137            glrun1->SetID_RUN_FRAG(glrun->GetID());
2138          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2139          //          //
         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;  
         //  
2140        };        };
2141        //        //
2142        delete glrun1;        // delete old entries in fragment table
2143        //        //
2144        // delete old entry in fragment table        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2145        //        glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
       oss.str("");  
       //  
       oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";  
       //  
       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;  
2146        //        //
2147          delete glrun1;
2148        //        //
2149        return;        return;
2150        //        //
# Line 1968  void PamelaDBOperations::HandleRunFragme Line 2181  void PamelaDBOperations::HandleRunFragme
2181      //      //
2182      row = result->Next();      row = result->Next();
2183      //      //
2184      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 {  
2185        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");
2186        } else {
2187          if ( NoFrag() ){
2188            glrun->SetID_RUN_FRAG(glrun->GetID());
2189            glrun->Fill_GL_RUN(conn);      
2190            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2191          };    
2192      };      };
2193    };    };
2194    //    //
# Line 2032  void PamelaDBOperations::HandleMissingHo Line 2214  void PamelaDBOperations::HandleMissingHo
2214      //      //
2215      this->FillClass(mishead,mistrail,firstev,lastev);      this->FillClass(mishead,mistrail,firstev,lastev);
2216      //      //
2217      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2218          glrun->SetID(this->AssignRunID());
2219          glrun->SetID_RUN_FRAG(0);
2220          glrun->Fill_GL_RUN(conn);    
2221        };
2222      //      //
2223    };    };
2224    //    //
# Line 2199  Bool_t PamelaDBOperations::IsRunConsiste Line 2385  Bool_t PamelaDBOperations::IsRunConsiste
2385            //                  //      
2386            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2387            //            //
2388            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2389                glrun->SetID(this->AssignRunID());
2390                glrun->SetID_RUN_FRAG(0);
2391                glrun->Fill_GL_RUN(conn);      
2392              };
2393            //            //
2394            firstevno = lastentry + 1;            firstevno = lastentry + 1;
2395            //            //
# Line 2275  void PamelaDBOperations::HandleSuspiciou Line 2465  void PamelaDBOperations::HandleSuspiciou
2465      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2466      //      //
2467      this->FillClass();      this->FillClass();
2468      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2469          glrun->SetID(this->AssignRunID());
2470          glrun->SetID_RUN_FRAG(0);
2471          glrun->Fill_GL_RUN(conn);    
2472        };
2473      //      //
2474    } else {    } else {
2475      //      //
# Line 2303  void PamelaDBOperations::HandleSuspiciou Line 2497  void PamelaDBOperations::HandleSuspiciou
2497        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");
2498        //        //
2499        this->FillClass();        this->FillClass();
2500        if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                if ( !IsRunAlreadyInserted() ){
2501            glrun->SetID(this->AssignRunID());
2502            glrun->SetID_RUN_FRAG(0);
2503            glrun->Fill_GL_RUN(conn);        
2504          };
2505        //        //
2506      } else {      } else {
2507        //        //
# Line 2401  void PamelaDBOperations::HandleSuspiciou Line 2599  void PamelaDBOperations::HandleSuspiciou
2599            //                  //      
2600            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2601            //            //
2602            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2603                glrun->SetID(this->AssignRunID());
2604                glrun->SetID_RUN_FRAG(0);
2605                glrun->Fill_GL_RUN(conn);      
2606              };
2607            //            //
2608            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...
2609              //              //
# Line 2436  void PamelaDBOperations::HandleSuspiciou Line 2638  void PamelaDBOperations::HandleSuspiciou
2638              //                  //    
2639              this->SetCommonGLRUN(firstTime,lastTime);              this->SetCommonGLRUN(firstTime,lastTime);
2640              //              //
2641              if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                    if ( !IsRunAlreadyInserted() ){
2642                  glrun->SetID(this->AssignRunID());
2643                  glrun->SetID_RUN_FRAG(0);
2644                  glrun->Fill_GL_RUN(conn);      
2645                };
2646            };            };
2647            //            //
2648            firstevno = lastentry + 1;            firstevno = lastentry + 1;
# Line 2543  Int_t PamelaDBOperations::insertCALO_CAL Line 2749  Int_t PamelaDBOperations::insertCALO_CAL
2749                // no calibrations in the db contain our calibration                // no calibrations in the db contain our calibration
2750                //                //
2751                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);
2752                if ( fromtime < 1150863400 ){                if ( fromtime < 1150871000 ){ //1150866904
2753                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
2754                  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
2755                };                };
# Line 2688  void PamelaDBOperations::HandleTRK_CALIB Line 2894  void PamelaDBOperations::HandleTRK_CALIB
2894        // no calibrations in the db contain our calibration        // no calibrations in the db contain our calibration
2895        //        //
2896        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");
2897        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
2898        //        //
2899        oss.str("");        oss.str("");
2900        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
# Line 3044  Int_t PamelaDBOperations::insertS4_CALIB Line 3250  Int_t PamelaDBOperations::insertS4_CALIB
3250            // no calibrations in the db contain our calibration            // no calibrations in the db contain our calibration
3251            //            //
3252            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");
3253            if ( fromtime < 1150863400 ){            if ( fromtime < 1150871000 ){  
3254              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
3255              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
3256            };            };
# Line 3117  Int_t PamelaDBOperations::insertS4_CALIB Line 3323  Int_t PamelaDBOperations::insertS4_CALIB
3323   * 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
3324   */   */
3325  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3326      return(this->CleanGL_RUN_FRAGMENTS(""));
3327    };
3328    
3329    /**
3330     * Scan the fragment table and move old fragments to the GL_RUN table
3331     */
3332    Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3333    //    //
3334    TSQLResult *result = 0;    TSQLResult *result = 0;
3335    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 3124  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3337  Int_t PamelaDBOperations::CleanGL_RUN_FR
3337    TSQLRow    *row2   = 0;    TSQLRow    *row2   = 0;
3338    //    //
3339    UInt_t moved = 0;    UInt_t moved = 0;
   UInt_t timelim = 0;  
   TDatime *time = new TDatime();  
3340    //    //
3341    stringstream oss;    stringstream oss;
3342    oss.str("");    oss.str("");
3343    //    //
3344    //    if ( !strcmp(fcleanfile.Data(),"") ){
3345    //      //
3346    if ( olderthan < 0 ){      // check if there are entries older than "olderthan" seconds from now
3347      if ( IsDebug() ) printf(" Do not clean GL_RUN_FRAGMENTS table \n");      //
3348      return(1);      oss.str("");
3349        oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3350            << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3351        //
3352        if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
3353        result = conn->Query(oss.str().c_str());
3354        //
3355      } else {
3356        oss.str("");
3357        oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3358        if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3359        result = conn->Query(oss.str().c_str());
3360        //    
3361        if ( result ){
3362          //
3363          row = result->Next();
3364          //
3365          if ( row ){
3366            oss.str("");
3367            oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3368                << " ID_ROOT_L0=" << row->GetField(0) << ";";
3369            //
3370            if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3371            result = conn->Query(oss.str().c_str());
3372            //    
3373          };
3374        } else {
3375          return(2);
3376        };
3377    };    };
3378    //    //
   // timelim = now - olderthan  
   //  
   time->Set();  
   timelim =  (UInt_t)time->Convert() - olderthan;  
   time->Set(timelim,false);  
   //  
   // check if there are entries older than "olderthan" seconds from now  
   //  
   oss.str("");  
   oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"  
       << " INSERT_TIME <= '" << time->AsSQLString() << "';";  
   //  
   if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",time->AsSQLString(),oss.str().c_str());  
   result = conn->Query(oss.str().c_str());  
   //  
3379    if ( result ){    if ( result ){
3380      //      //
3381      row = result->Next();      row = result->Next();
3382      //      //
3383      if ( row ){      while ( row ){
3384        //        //
3385        oss.str("");        oss.str("");
3386        oss << " ID= "<< row->GetField(0);        oss << " ID= "<< row->GetField(0);
# Line 3191  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3415  Int_t PamelaDBOperations::CleanGL_RUN_FR
3415          if ( IsDebug() ) printf(" The run is new \n");          if ( IsDebug() ) printf(" The run is new \n");
3416          if ( IsDebug() ) printf(" -> fill the DB \n");                if ( IsDebug() ) printf(" -> fill the DB \n");      
3417          //          //
3418          glrun->SetID(0);          //      glrun->SetID(this->AssignRunID()); we use the old run number!
3419            glrun->SetID_RUN_FRAG(glrun->GetID());
3420          glrun->Fill_GL_RUN(conn);            glrun->Fill_GL_RUN(conn);  
3421          //          //
3422          oss.str("");  //      oss.str("");
3423          oss << " SELECT ID FROM GL_RUN WHERE "  //      oss << " SELECT ID FROM GL_RUN WHERE "
3424              << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "  //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3425              << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "  //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3426              << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "  //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3427              << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "  //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3428              << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";  //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3429          //  //      //
3430          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());
3431          result2 = conn->Query(oss.str().c_str());  //      result2 = conn->Query(oss.str().c_str());
3432          //  //      //
3433          if ( !result2 ) throw -4;  //      if ( !result2 ) throw -4;
3434          //  //      //
3435          row2 = result2->Next();  //      row2 = result2->Next();
3436          //  //      //
3437          if ( !row2 ) throw -25;  //      if ( !row2 ) throw -25;
3438          //  //      //
3439          oss.str("");  //      oss.str("");
3440          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);
3441          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());
3442          result2 = conn->Query(oss.str().c_str());  //      result2 = conn->Query(oss.str().c_str());
3443          //  //      //
3444          if ( !result2 ) throw -4;  //      if ( !result2 ) throw -4;
3445          //          //
3446          moved++;          moved++;
3447          //          //
# Line 3226  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3451  Int_t PamelaDBOperations::CleanGL_RUN_FR
3451        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));      
3452        //        //
3453        //        //
3454        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3455        oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);  //      oss.str("");
3456        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);
3457        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());
3458        //  //       result2 = conn->Query(oss.str().c_str());
3459        if ( !result2 ) throw -4;  //      //
3460        //  //      if ( !result2 ) throw -4;
3461    //      //
3462          row = result->Next();
3463      };      };
3464    };    };
3465    if ( IsDebug() ) printf(" Moved %u runs\n",moved);    if ( IsDebug() ) printf(" Moved %u runs\n",moved);
# Line 3243  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3470  Int_t PamelaDBOperations::CleanGL_RUN_FR
3470   * 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..
3471   */   */
3472  Int_t PamelaDBOperations::ValidateRuns(){  Int_t PamelaDBOperations::ValidateRuns(){
3473      return(this->ValidateRuns(""));
3474    };
3475    
3476    /**
3477     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3478     */
3479    Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3480    //    //
3481    TSQLResult *result = 0;    TSQLResult *result = 0;
3482    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
3483    //    //
3484    UInt_t timelim = 0;    UInt_t calibtime = 50;
   TDatime *time = new TDatime();  
3485    //    //
3486    stringstream oss;    stringstream oss;
3487    oss.str("");    oss.str("");
3488    //    //
   //  
   //  
   if ( olderthan < 0 ){  
     if ( IsDebug() ) printf(" Do not validate runs \n");  
     return(1);  
   };  
   //  
   // timelim = now - olderthan  
   //  
   time->Set();  
   timelim =  (UInt_t)time->Convert() - olderthan;  
   time->Set(timelim,false);  
   
   printf("Validate runs older than %s \n",time->AsSQLString());  
   
3489    // =======================================================    // =======================================================
3490    // validate runs by checking missing calibrations    // validate runs by checking missing calibrations
3491    // =======================================================    // =======================================================
3492    UInt_t t_stop  = 0;    UInt_t t_stop  = 0;
3493    UInt_t t_start = 0;    UInt_t t_start = 0;
3494    // --------------------------------------------------------------    if ( !strcmp(valfile.Data(),"") ) {
3495    // 1) get the OBT of the last run inserted after clean-time limit      // --------------------------------------------------------------
3496    // --------------------------------------------------------------      // 1) get the OBT of the last run inserted after clean-time limit
3497    oss.str("");      // --------------------------------------------------------------
3498    oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << time->AsSQLString()      oss.str("");
3499        << "' ORDER BY INSERT_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3500    printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());          << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3501    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3502    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3503    if ( !result->GetRowCount() ) {      if ( !result ) throw -4;
3504            printf(" No runs to validate \n");      if ( !result->GetRowCount() ) {
3505            return(1);        printf(" No runs to validate \n");
3506    }else{        return(1);
3507          row = result->Next();      }else{
3508          t_start = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3509          printf("t_start %i\n",t_start);        t_start = (UInt_t)atoll(row->GetField(4));
3510    };        };  
3511    // --------------------------------------------------------------      // --------------------------------------------------------------
3512    // 2) get the OBT of the last validated run      // 2) get the OBT of the last validated run
3513    // --------------------------------------------------------------      // --------------------------------------------------------------
3514    oss.str("");      oss.str("");
3515    oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start      oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3516        <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3517    printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3518    result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
3519    if ( !result ) throw -4;      if ( !result ) throw -4;
3520    if ( result->GetRowCount() ){      if ( result->GetRowCount() ){
3521            row = result->Next();        row = result->Next();
3522            t_stop = (UInt_t)atoll(row->GetField(4));        t_stop = (UInt_t)atoll(row->GetField(4));
3523        };
3524        if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);
3525        // --------------------------------------------------------------
3526        // now retrieves runs to be validated
3527        // --------------------------------------------------------------
3528        oss.str("");
3529        oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;
3530        oss << " AND RUNHEADER_TIME >="<< t_stop;
3531        oss << " ORDER BY RUNHEADER_TIME DESC;";
3532        if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3533        result = conn->Query(oss.str().c_str());
3534      } else {
3535        //
3536        stringstream myquery;
3537        UInt_t myid = 0;
3538        myquery.str("");
3539        myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3540        //
3541        result = conn->Query(myquery.str().c_str());
3542        //
3543        row = result->Next();      
3544        if( !row ){
3545          if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3546            if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3547            return(2);
3548          };
3549          if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3550          return(0);
3551        };
3552        myid=(UInt_t)atoll(row->GetField(0));
3553        //
3554        myquery.str("");
3555        myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3556        //
3557        result = conn->Query(myquery.str().c_str());
3558        //
3559        row = result->Next();      
3560        if( !row->GetField(0) || !row->GetField(1)){
3561          //
3562          if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3563          //
3564          return(0);
3565          //
3566        } else {  
3567          //
3568          UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3569          UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3570          UInt_t caltime = 0;
3571          //
3572          myquery.str("");
3573          myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3574          myquery << " order by FROM_TIME asc limit 1;";
3575          //
3576          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3577          //
3578          //
3579          result = conn->Query(myquery.str().c_str());
3580          //
3581          row = result->Next();      
3582          if( !row ){
3583            caltime = runhtime;
3584          } else {
3585            caltime = (UInt_t)atoll(row->GetField(0));
3586          };
3587          //
3588          myquery.str("");
3589          myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3590          myquery << caltime << " order by RUNHEADER_TIME DESC";
3591          //
3592          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3593          //
3594          result = conn->Query(myquery.str().c_str());
3595          //
3596        };
3597    };    };
3598    printf("t_stop %i\n",t_stop);    //
   // --------------------------------------------------------------  
   // 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() )  
   printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());  
   result = conn->Query(oss.str().c_str());  
3599    if ( !result ) throw -4;    if ( !result ) throw -4;
3600    if ( !result->GetRowCount() ) printf(" No runs to validate \n");    if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3601    printf("------------------------------------------------------------------------------- \n");    //
     
3602    Int_t nrow = 0;    Int_t nrow = 0;
3603    GL_RUN* this_run = new GL_RUN();    GL_RUN* this_run = new GL_RUN();
3604    GL_RUN* next_run = new GL_RUN();    GL_RUN* next_run = new GL_RUN();
3605    Int_t   nseq_max = 100;    Int_t   nseq_max = 1000;
3606  //  UInt_t* sequence = new UInt_t[100];  //  UInt_t* sequence = new UInt_t[100];
3607    vector<UInt_t> sequence(nseq_max);    vector<UInt_t> sequence(nseq_max);
3608    Int_t   nseq = 0;    Int_t   nseq = 0;
# Line 3331  Int_t PamelaDBOperations::ValidateRuns() Line 3612  Int_t PamelaDBOperations::ValidateRuns()
3612    UInt_t t1=0,t2=0;    UInt_t t1=0,t2=0;
3613    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
3614    // - loop over runs, back in time,    // - loop over runs, back in time,
3615    // - select sequences of runs close in time (less than 60 s apart),    // - select sequences of runs close in time (less than calibtime s apart),
3616    //   which could be preceeded by a calibration    //   which could be preceeded by a calibration
3617    // - check if there might be a missing calibration    // - check if there might be a missing calibration
3618    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
# Line 3344  Int_t PamelaDBOperations::ValidateRuns() Line 3625  Int_t PamelaDBOperations::ValidateRuns()
3625            //get run info            //get run info
3626            //------------            //------------
3627            this_run->Set_GL_RUN(row);            this_run->Set_GL_RUN(row);
3628                                
           printf(" RUN ID %i --- TRK_CALIB_USED %i --- RM_ACQ_AFTER_CALIB %i --- TIME %i %i \n",this_run->ID,this_run->TRK_CALIB_USED,this_run->RM_ACQ_AFTER_CALIB,this_run->RUNHEADER_TIME, this_run->RUNTRAILER_TIME);  
             
3629            Bool_t this_BAD = false;            Bool_t this_BAD = false;
3630            if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;            if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;
3631            else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;            else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;
3632            else{            else{
3633                    printf("Missing or corrupted header!! \n");  //                printf("Missing or corrupted header!! \n");
3634                    this_ONLINE = false;                    this_ONLINE = false;
3635                    this_BAD = true;                    this_BAD = true;
3636            };            };
# Line 3373  Int_t PamelaDBOperations::ValidateRuns() Line 3652  Int_t PamelaDBOperations::ValidateRuns()
3652                                                        
3653                            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
3654                                                        
3655                            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
                             
 /*                        if( CHECK && !next_run->GetRM_ACQ_AFTER_CALIB() )  
                                   printf(" ValidateRuns ***WARNING*** : DT = %i but RM_ACQ_AFTER_CALIB = %i \n",(t2-t1),next_run->GetRM_ACQ_AFTER_CALIB());*/  
                                                     
3656                                                        
3657                            if( !CHECK && this_run->VALIDATION ){                            if( !CHECK && this_run->VALIDATION ){
3658                                    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 3387  Int_t PamelaDBOperations::ValidateRuns() Line 3662  Int_t PamelaDBOperations::ValidateRuns()
3662                    }else if( !this_ONLINE && next_ONLINE) {              // this: DEFAULT + next:ON-LINE                    }else if( !this_ONLINE && next_ONLINE) {              // this: DEFAULT + next:ON-LINE
3663                                                        
3664                            CHECK = true;                            CHECK = true;
 //                        if( interval < 60 ) printf(" ValidateRuns ***WARNING*** : kkkkkk DT = %i \n",interval);  
3665    
3666                    }else if( !next_ONLINE ){                                             // this:ANY + next:DEFAULT                    }else if( !next_ONLINE ){                                             // this:ANY + next:DEFAULT
3667                                                        
# Line 3401  Int_t PamelaDBOperations::ValidateRuns() Line 3675  Int_t PamelaDBOperations::ValidateRuns()
3675            //----------------------------            //----------------------------
3676            if( CHECK ){            if( CHECK ){
3677                    // check if calibration exists                    // check if calibration exists
3678                    printf("DT %i ===> CHECK Missing calibration\n",interval);                    if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
3679                    Bool_t MISSING = MissingTRK_CALIB(t1,t2);                    Bool_t MISSING = MissingTRK_CALIB(t1,t2);
3680                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
3681                    nseq=0;                    nseq=0;
3682            } else printf("DT %i\n",interval);            };
3683            //--------------            //--------------
3684            //store run info            //store run info
3685            //--------------            //--------------
# Line 3418  Int_t PamelaDBOperations::ValidateRuns() Line 3692  Int_t PamelaDBOperations::ValidateRuns()
3692                    }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);                    }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
3693            };            };
3694                        
3695              if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID);
3696            nrow++;            nrow++;
3697                        
3698    };    };
# Line 3446  Bool_t PamelaDBOperations::MissingTRK_CA Line 3721  Bool_t PamelaDBOperations::MissingTRK_CA
3721          // which should be equal to the time between ascending-nodes.          // which should be equal to the time between ascending-nodes.
3722          //==============================================================          //==============================================================
3723          if ( t2 - trkcalib->FROM_TIME > 5700) {          if ( t2 - trkcalib->FROM_TIME > 5700) {
3724                  printf("Long time to previous calib %i :-( \n",t2 - trkcalib->FROM_TIME);                  if ( IsDebug() )printf("Long time between calib and run start %i :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);
3725          //==============================================================          //==============================================================
3726          // there might be a missing calibration, due to:          // there might be a missing calibration, due to:
3727          // - MM full          // - MM full
# Line 3467  Bool_t PamelaDBOperations::MissingTRK_CA Line 3742  Bool_t PamelaDBOperations::MissingTRK_CA
3742          // it is enough to say that there are no missing calibrations          // it is enough to say that there are no missing calibrations
3743          //==============================================================          //==============================================================
3744          // the long time interval bewteen runs might be due to download          // the long time interval bewteen runs might be due to download
3745          printf("Short time to previous calib %i :-) \n",t2 - trkcalib->FROM_TIME);          if ( IsDebug() )printf("Short time between calib and run start %i :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
3746          return(false);          return(false);
3747                    
3748  };  };
# Line 3480  Int_t PamelaDBOperations::assignVALIDATI Line 3755  Int_t PamelaDBOperations::assignVALIDATI
3755          TSQLResult *result = 0;          TSQLResult *result = 0;
3756          stringstream oss;          stringstream oss;
3757          oss.str("");          oss.str("");
3758          oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << "';";          oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
3759          //          //
3760  //      if ( IsDebug() )  //      if ( IsDebug() )
3761          printf(" Set VALIDATION = %i for run %i \n",validation,idrun);  //      printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
3762  //      printf(" Set VALIDATION = %i for run %i : query is \n %s \n",validation,idrun,oss.str().c_str());          if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
3763  //      result = conn->Query(oss.str().c_str());          result = conn->Query(oss.str().c_str());
3764  //      if ( !result ) throw -4;          if ( !result ) throw -4;
3765          return(0);          return(0);
3766  }  }
3767    
3768    
3769    
3770    // Insert TLEs from file tlefilename in the table GL_TLE in the db
3771    // opened by conn, sorting them by date from older to newer, if each
3772    // TLE has not been alread inserted.
3773    Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
3774    {
3775      fstream tlefile(tlefilename, ios::in);
3776    
3777      if ( !tlefile ) throw -7;
3778    
3779      vector<cTle*> ctles;
3780      vector<cTle*>::iterator iter;
3781      int present = 0;
3782    
3783      // Get three lines from tlefile, create a cTle object and put it
3784      // into ctles
3785      while(1) {
3786        cTle *tlef;
3787        string str1, str2, str3;
3788    
3789        getline(tlefile, str1);
3790        if(tlefile.eof()) break;
3791    
3792        getline(tlefile, str2);
3793        if(tlefile.eof()) break;
3794    
3795        getline(tlefile, str3);
3796        if(tlefile.eof()) break;
3797    
3798        // We now have three good lines for a cTle.
3799        tlef = new cTle(str1, str2, str3);
3800        ctles.push_back(tlef);
3801      }
3802    
3803      tlefile.close();
3804    
3805      // Sort by date
3806      sort(ctles.begin(), ctles.end(), compTLE);
3807    
3808      // Now we insert each TLE into the db
3809      for(iter = ctles.begin(); iter != ctles.end(); iter++) {
3810        cTle *tle = *iter;
3811    
3812        // Do nothing if it's already present in the db.  Just increase
3813        // the counter present.
3814        if (! isTlePresent(tle))
3815          {
3816            int status = insertTle(tle);
3817    
3818            // Insert query failed.  Return 1.
3819            if(status == EXIT_FAILURE) {
3820              
3821              if( IsDebug() ) {
3822                cerr << "Error: inserting TLE:" << endl
3823                     << tle->getName() << endl
3824                     << tle->getLine1() << endl
3825                     << tle->getLine2() << endl;
3826              }
3827    
3828              throw -4;
3829              return 1;
3830            }
3831    
3832          }
3833        else
3834          present++;
3835    
3836      }
3837    
3838      int inserted = ctles.size() - present;  // Number of inserted TLE.
3839      if ( IsDebug() )
3840        cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
3841             << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
3842    
3843      ctles.clear();
3844    
3845    
3846      // Return 2 if no new TLE has been inserted.  0 otherwise.
3847      if(! inserted ) return 2;
3848      return 0;
3849    }
3850    
3851    
3852    // Insert tle in the table GL_TLE using the connection conn.
3853    Int_t PamelaDBOperations::insertTle(cTle *tle)
3854    {
3855      stringstream oss;
3856      TSQLResult *result = 0;
3857    
3858      oss.str("");
3859      oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
3860          << " VALUES ( '"
3861          << tle->getName() << "', '"
3862          << tle->getLine1() << "', '"
3863          << tle->getLine2() << "', '"
3864          << getTleDatetime(tle) << "')";
3865    
3866      //  cout << oss.str().c_str() << endl;
3867      result = conn->Query(oss.str().c_str());
3868      if (result == NULL)
3869        return EXIT_FAILURE;
3870    
3871      return EXIT_SUCCESS;
3872    }
3873    
3874    
3875    // Return whether tle is already in the db connected by conn.
3876    bool PamelaDBOperations::isTlePresent(cTle *tle)
3877    {
3878      stringstream oss;
3879      TSQLResult *result = 0;
3880    
3881      oss.str("");
3882      oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
3883          << getTleDatetime(tle) << "'";
3884    
3885      result = conn->Query(oss.str().c_str());
3886      if (result == NULL) throw -4;
3887    
3888      if (result->GetRowCount())
3889        return true;
3890      else
3891        return false;
3892    }
3893    
3894    
3895    // Return whether the first TLE is dated early than the second
3896    bool compTLE (cTle *tle1, cTle *tle2)
3897    {
3898      return getTleJulian(tle1) < getTleJulian(tle2);
3899    }
3900    
3901    
3902    // Return the date of the tle using the format (year-2000)*1e3 +
3903    // julian day.  e.g. 6365 is the 31th Dec 2006.
3904    // It does *not* return a cJulian date.
3905    float getTleJulian(cTle *tle) {
3906      return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
3907    }
3908    
3909    
3910    // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
3911    // format.
3912    string getTleDatetime(cTle *tle)
3913    {
3914      int year, mon, day, hh, mm, ss;
3915      double dom; // day of month (is double!)
3916      stringstream date; // date in datetime format
3917    
3918      // create a cJulian from the date in tle
3919      cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
3920    
3921      // get year, month, day of month
3922      jdate.getComponent(&year, &mon, &dom);
3923    
3924      // build a datetime YYYY-MM-DD hh:mm:ss
3925      date.str("");
3926      day = (int) floor(dom);
3927      hh = (int) floor( (dom - day) * 24);
3928      mm = (int) floor( ((dom - day) * 24 - hh) * 60);
3929      ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
3930      //  ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
3931    
3932      date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
3933    
3934      return date.str();
3935    }
3936    
3937    /**
3938     * Remove a file from the DB, delete on cascade all entries related to that file
3939     * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
3940     * calibration
3941     **/
3942    Int_t PamelaDBOperations::removeFile(TString remfile){
3943      //
3944      // Determine ID_ROOT_L0 and ID_RAW
3945      //
3946      TSQLResult *pResult;
3947      TSQLRow *Row;
3948      stringstream myquery;
3949      //  
3950      myquery.str("");
3951      myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
3952      //
3953      pResult = conn->Query(myquery.str().c_str());
3954      //
3955      Row = pResult->Next();      
3956      if( !Row ){
3957        if ( strcmp(remfile.Data(),GetRootName().Data()) ){
3958          if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
3959          return(1);
3960        };
3961        if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
3962        return(0);
3963      };
3964      //
3965      this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
3966      this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
3967      //
3968      this->ValidationOFF();
3969      //
3970      this->RemoveCALIBS();
3971      //
3972      this->RemoveRUNS();
3973      //
3974      this->RemoveFILES();
3975      //
3976      this->SetID_ROOT(0);
3977      this->SetID_RAW(0);
3978      //
3979      return(0);
3980    };
3981    
3982    /**
3983     *
3984     * Set validation bit to zero for runs following the removing file till
3985     * 1) a run with TRK_CALIB_USED=140
3986     * 2) a run with VALIDATION = 0
3987     * 3) the next calibration
3988     *
3989     **/
3990    void PamelaDBOperations::ValidationOFF(){
3991      TSQLResult *pResult;
3992      TSQLRow *Row;
3993      stringstream myquery;
3994      Int_t unv = 0;
3995      //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
3996      myquery.str("");
3997      myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
3998      //
3999      pResult = conn->Query(myquery.str().c_str());
4000      //
4001      Row = pResult->Next();      
4002      if( !Row->GetField(0) ){
4003        //
4004        if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
4005        //
4006      } else {  
4007        //
4008        UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
4009        UInt_t caltime = 0;
4010        //
4011        myquery.str("");
4012        myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
4013        myquery << " order by FROM_TIME asc limit 1;";
4014        //
4015        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4016        //
4017        //
4018        delete pResult;
4019        pResult = conn->Query(myquery.str().c_str());
4020        //
4021        Row = pResult->Next();      
4022        if( !Row ){
4023          caltime = runhtime;
4024        } else {
4025          caltime = (UInt_t)atoll(Row->GetField(0));
4026        };
4027        //
4028        myquery.str("");
4029        myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4030        myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4031        //
4032        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4033        //
4034        pResult = conn->Query(myquery.str().c_str());
4035        //
4036        Row = pResult->Next();      
4037        if( !Row ){
4038          //
4039          if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4040          //
4041        } else {
4042          myquery.str("");
4043          myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4044          myquery << " RUNHEADER_TIME>=" <<runhtime;
4045          myquery << " order by RUNHEADER_TIME asc;";
4046          //
4047          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4048          //
4049          pResult = conn->Query(myquery.str().c_str());
4050          //
4051          Row = pResult->Next();  
4052          while ( Row ){
4053            //      
4054            unv++;
4055            this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4056            Row = pResult->Next();  
4057            //
4058          };
4059        };
4060      };
4061      if ( IsDebug() ) printf(" %i runs have been unvalidated \n",unv);
4062    };
4063    
4064    /**
4065     *
4066     * Rearrange GL_RUN table and remove runs
4067     *
4068     **/
4069    void PamelaDBOperations::RemoveRUNS(){
4070      TSQLResult *pResult;
4071      TSQLRow *Row;
4072      stringstream myquery;
4073      UInt_t drun = 0;
4074      GL_RUN *delrun = new GL_RUN();
4075      //
4076      myquery.str("");
4077      myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4078      //
4079      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4080      //
4081      pResult = conn->Query(myquery.str().c_str());
4082      //
4083      Row = pResult->Next();    
4084      //
4085      //
4086      if ( !Row ){
4087        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4088      } else {
4089        if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");    
4090        while ( Row ){
4091          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4092          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4093          drun++;
4094          Row = pResult->Next();    
4095        };
4096      };
4097      //
4098      //
4099      myquery.str("");
4100      myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4101      //
4102      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4103      //
4104      pResult = conn->Query(myquery.str().c_str());
4105      //
4106      Row = pResult->Next();    
4107      //
4108      if ( !Row ){
4109        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4110      } else {
4111        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");    
4112        while ( Row ){
4113          if ( IsDebug() ) printf(" restore run %i \n",(UInt_t)atoll(Row->GetField(1)));
4114          delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4115          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(1)));
4116          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4117          if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4118            if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4119            delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4120          };
4121          drun++;
4122          Row = pResult->Next();    
4123        };
4124      };
4125      //
4126      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);    
4127      //
4128      //
4129      //
4130      drun = 0;
4131      //
4132      myquery.str("");
4133      myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4134      //
4135      pResult = conn->Query(myquery.str().c_str());
4136      //
4137      Row = pResult->Next();    
4138      //
4139      if ( !Row ){
4140        if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4141      } else {
4142        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");    
4143        while ( Row ){
4144          if ( IsDebug() ) printf(" del run idtrash %i \n",(UInt_t)atoll(Row->GetField(0)));
4145          myquery.str("");
4146          myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";      
4147          conn->Query(myquery.str().c_str());
4148          drun++;
4149          Row = pResult->Next();    
4150        };
4151      };
4152      //  
4153      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_TRASH table \n",drun);    
4154      //
4155      //
4156      //
4157      drun = 0;
4158      //
4159      myquery.str("");
4160      myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4161      //
4162      pResult = conn->Query(myquery.str().c_str());
4163      //
4164      Row = pResult->Next();    
4165      //
4166      if ( !Row ){
4167        if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4168      } else {
4169        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");    
4170        while ( Row ){
4171          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4172          myquery.str("");
4173          myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4174          conn->Query(myquery.str().c_str());
4175          drun++;
4176          Row = pResult->Next();    
4177        };
4178      };
4179      //  
4180      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_FRAGMENTS table \n",drun);    
4181      //
4182      //
4183      //
4184      delete delrun;
4185      //
4186    };
4187    
4188    
4189    /**
4190     *
4191     * Rearrange calibration tables
4192     *
4193     **/
4194    void PamelaDBOperations::RemoveFILES(){
4195      stringstream myquery;
4196      //
4197      myquery.str("");
4198      myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4199      //
4200      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4201      //
4202      conn->Query(myquery.str().c_str());
4203      //
4204    };
4205    
4206    /**
4207     *
4208     * Rearrange calibration tables
4209     *
4210     **/
4211    void PamelaDBOperations::RemoveCALIBS(){
4212      TSQLResult *pResult;
4213      TSQLRow *Row;
4214      stringstream myquery;
4215      //  
4216      //
4217      // Calorimeter
4218      //
4219      for (Int_t section = 0; section < 4; section++){
4220        myquery.str("");
4221        myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4222        myquery << " SECTION=" << section << ";";
4223        //
4224        pResult = conn->Query(myquery.str().c_str());
4225        //
4226        Row = pResult->Next();      
4227        if( !Row->GetField(0) || !Row->GetField(1) ){
4228          //
4229          if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4230          //
4231        } else {
4232          //
4233          myquery.str("");
4234          myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4235          myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4236          myquery << " SECTION=" << section << ";";
4237          //
4238          pResult = conn->Query(myquery.str().c_str());
4239          //
4240          if( !pResult ){
4241            //
4242            if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4243            //
4244            throw -4;
4245            //
4246          };
4247          //
4248        };
4249      };
4250      myquery.str("");
4251      myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4252      //
4253      pResult = conn->Query(myquery.str().c_str());
4254      //
4255      if( !pResult ){
4256        //
4257        if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4258        //
4259        throw -4;
4260        //
4261      };
4262      //
4263      // Tracker
4264      //
4265      myquery.str("");
4266      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4267      //
4268      pResult = conn->Query(myquery.str().c_str());
4269      //
4270      Row = pResult->Next();      
4271      if( !Row->GetField(0) || !Row->GetField(1) ){
4272        //
4273        if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4274        //
4275      } else {
4276        //
4277        myquery.str("");
4278        myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4279        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4280        //
4281        pResult = conn->Query(myquery.str().c_str());
4282        //
4283        if( !pResult ){
4284          //
4285          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4286          //
4287          throw -4;
4288          //
4289        };
4290        //
4291        myquery.str("");
4292        myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4293        //
4294        pResult = conn->Query(myquery.str().c_str());
4295        //
4296        if( !pResult ){
4297          //
4298          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4299          //
4300          throw -4;
4301          //
4302        };
4303      };
4304      //
4305      //
4306      // S4
4307      //
4308      myquery.str("");
4309      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4310      //
4311      pResult = conn->Query(myquery.str().c_str());
4312      //
4313      Row = pResult->Next();      
4314      if( !Row->GetField(0) || !Row->GetField(1) ){
4315        //
4316        if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4317        //
4318      } else {
4319        //
4320        myquery.str("");
4321        myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4322        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4323        //
4324        pResult = conn->Query(myquery.str().c_str());
4325        //
4326        if( !pResult ){
4327          //
4328          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4329          //
4330          throw -4;
4331          //
4332        };
4333        //
4334        myquery.str("");
4335        myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4336        //
4337        pResult = conn->Query(myquery.str().c_str());
4338        //
4339        if( !pResult ){
4340          //
4341          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4342          //
4343          throw -4;
4344          //
4345        };
4346        //
4347      };
4348    };

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.19

  ViewVC Help
Powered by ViewVC 1.1.23