/[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.16 by mocchiut, Tue Oct 31 15:36:04 2006 UTC
# 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      if(olderthan >= 0){
116        VALIDATE = true;
117        UInt_t timelim = 0;
118        timelim =  (UInt_t)clean_time->Convert() - olderthan;
119        clean_time->Set(timelim,false);
120      };
121    };
122    
123  /**  /**
124   * Open the DB connection   * Open the DB connection
125   * @param host         hostname for the SQL connection.   * @param host         hostname for the SQL connection.
# Line 93  void PamelaDBOperations::Close(){ Line 127  void PamelaDBOperations::Close(){
127   * @param password     password for the SQL connection.   * @param password     password for the SQL connection.
128   */   */
129  void PamelaDBOperations::SetConnection(TString host, TString user, TString password){  void PamelaDBOperations::SetConnection(TString host, TString user, TString password){
130      if ( IsDebug() ) printf(" Connecting using host = %s user = %s password = %s \n",host.Data(),user.Data(),password.Data());
131    conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());    conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());
132  };  };
133    
# Line 121  void PamelaDBOperations::SetDebugFlag(Bo Line 156  void PamelaDBOperations::SetDebugFlag(Bo
156  };  };
157    
158  /**  /**
159     * Set the nofrag flag
160     *
161     */
162    void PamelaDBOperations::SetNoFrag(Bool_t nf){
163      NOFRAG = nf;
164    };
165    
166    /**
167   * Store the BOOT number of the RAW file.   * Store the BOOT number of the RAW file.
168   * @param boot    BOOT number of the RAW file   * @param boot    BOOT number of the RAW file
169   */   */
# Line 161  void PamelaDBOperations::SetRootName(TSt Line 204  void PamelaDBOperations::SetRootName(TSt
204  };  };
205    
206  /**  /**
207     * Store the downlink orbit number from filename.
208     */
209    void PamelaDBOperations::SetOrbitNo(){
210      dworbit = 0;
211      TString name = this->GetRootFile();
212      Int_t nlength = name.Length();
213      if ( nlength < 5 ) return;
214      TString dwo = 0;
215      for (Int_t i = 0; i<5; i++){
216        dwo.Append(name[i],1);
217      };
218      if ( dwo.IsDigit() ){
219        dworbit = (UInt_t)dwo.Atoi();
220      } else {
221        dwo="";
222        for (Int_t i = 8; i<13; i++){
223          dwo.Append(name[i],1);
224        };    
225        if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
226      };
227      if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
228      return;
229    };
230    
231    
232    
233    /**
234   * Store the NOBOOT flag.   * Store the NOBOOT flag.
235   * @param noboot    true/false.   * @param noboot    true/false.
236   */   */
# Line 169  void PamelaDBOperations::SetNOBOOT(Bool_ Line 239  void PamelaDBOperations::SetNOBOOT(Bool_
239  };  };
240    
241  /**  /**
242     * Store path to the TLE file.
243     */
244    void PamelaDBOperations::SetTLEPath(TString str){
245      tlefilename = str;
246    };
247    
248    /**
249   * Store the olderthan variable   * Store the olderthan variable
250   * @param olderthan   * @param olderthan
251   */   */
252  void PamelaDBOperations::SetOlderThan(Long64_t oldthan){  // void PamelaDBOperations::SetOlderThan(Long64_t oldthan){
253    olderthan = oldthan;  //   olderthan = oldthan;
254  };  // };
255    
256  /**  /**
257   * 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 302  Int_t PamelaDBOperations::SetUpperLimits
302    UInt_t jump = 50000; // was 5000    UInt_t jump = 50000; // was 5000
303    EventCounter *code=0;    EventCounter *code=0;
304    //    //
305      UInt_t deltapkt = 5000;
306      ULong64_t deltaobt = 50000;
307      //
308    //   pcksList packetsNames;    //   pcksList packetsNames;
309    //   pcksList::iterator Iter;    //   pcksList::iterator Iter;
310    //   getPacketsNames(packetsNames);    //   getPacketsNames(packetsNames);
# Line 271  Int_t PamelaDBOperations::SetUpperLimits Line 351  Int_t PamelaDBOperations::SetUpperLimits
351    //    //
352    if ( nevent < 2 ) return(4);    if ( nevent < 2 ) return(4);
353    //    //
354    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) ){
355        //
356      // go back      // go back
357      zomp = nevent - 2;      zomp = nevent - 2;
358      //      //
# Line 320  Int_t PamelaDBOperations::SetUpperLimits Line 401  Int_t PamelaDBOperations::SetUpperLimits
401    // 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)
402    //    //
403    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);  
404    TTree *rh=(TTree*)file->Get("RunHeader");    TTree *rh=(TTree*)file->Get("RunHeader");
405    if ( !rh || rh->IsZombie() ) throw -17;    if ( !rh || rh->IsZombie() ) throw -17;
406    TTree *rt=(TTree*)file->Get("RunTrailer");    TTree *rt=(TTree*)file->Get("RunTrailer");
# Line 346  Int_t PamelaDBOperations::SetUpperLimits Line 423  Int_t PamelaDBOperations::SetUpperLimits
423    UInt_t pkth = 0;    UInt_t pkth = 0;
424    ULong64_t obth = 0;    ULong64_t obth = 0;
425    //    //
426      T->GetEntry(upperentry);
427      code = eh->GetCounter();
428      Int_t lasttrail = code->Get(pctp->RunTrailer);
429      Int_t lasthead = code->Get(pctp->RunHeader);
430    if ( lasttrail < rtev ){    if ( lasttrail < rtev ){
431      rt->GetEntry(lasttrail);      rt->GetEntry(lasttrail);
432      pht = eht->GetPscuHeader();      pht = eht->GetPscuHeader();
# Line 356  Int_t PamelaDBOperations::SetUpperLimits Line 437  Int_t PamelaDBOperations::SetUpperLimits
437    if ( lasthead < rhev ){    if ( lasthead < rhev ){
438      rh->GetEntry(lasthead);      rh->GetEntry(lasthead);
439      phh = ehh->GetPscuHeader();      phh = ehh->GetPscuHeader();
440      pkth = PKT(pht->GetCounter());      pkth = PKT(phh->GetCounter());
441      obth = OBT(pht->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
442    };    };
443    //    //
444    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 482  Int_t PamelaDBOperations::SetUpperLimits
482      pkth = PKT(phh->GetCounter());      pkth = PKT(phh->GetCounter());
483      obth = OBT(phh->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
484      //      //
485    //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
486        //
487      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);  
488        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);
489        //        //
490        rhev--;        rhev = k-1;
491        rh->GetEntry(rhev);        rh->GetEntry(rhev);
492        pkth = spkth;        pkth = spkth;
493        obth = sobth;        obth = sobth;
       if ( IsDebug() ) printf(" lasthead %i pt %i p1 %i ot %u o1 %u \n",rhev,pkth,spkth,obth,sobth);  
494        //        //
495        UInt_t evbefh = 0;        UInt_t evbefh = 0;
496        code = ehh->GetCounter();        code = ehh->GetCounter();
# Line 419  Int_t PamelaDBOperations::SetUpperLimits Line 500  Int_t PamelaDBOperations::SetUpperLimits
500          ph = eh->GetPscuHeader();          ph = eh->GetPscuHeader();
501          t_pktlast = PKT(ph->GetCounter());          t_pktlast = PKT(ph->GetCounter());
502          t_obtlast = OBT(ph->GetOrbitalTime());          t_obtlast = OBT(ph->GetOrbitalTime());
503          if ( t_pktlast < spkth && t_obtlast < sobth ){ // jump          if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
504            upperpkt = pkth;            upperpkt = pkth;
505            upperobt = obth;            upperobt = obth;
506            upperentry = evbefh-1;            upperentry = evbefh-1;
# Line 439  Int_t PamelaDBOperations::SetUpperLimits Line 520  Int_t PamelaDBOperations::SetUpperLimits
520          };              };    
521        };        };
522        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);
523          goto kikko0;
524      };      };
525    };    };
526     kikko0:
527    //    //
528    //    //
529    //    //
# Line 458  Int_t PamelaDBOperations::SetUpperLimits Line 541  Int_t PamelaDBOperations::SetUpperLimits
541      pktt = PKT(pht->GetCounter());      pktt = PKT(pht->GetCounter());
542      obtt = OBT(pht->GetOrbitalTime());      obtt = OBT(pht->GetOrbitalTime());
543      //      //
544    //    if ( IsDebug() ) printf(" k %i rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
545        //
546      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);  
547        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);
548        //        //
549        rtev--;        rtev = k-1;
550        rt->GetEntry(rtev);        rt->GetEntry(rtev);
551        pktt = spktt;        pktt = spktt;
552        obtt = sobtt;        obtt = sobtt;
# Line 476  Int_t PamelaDBOperations::SetUpperLimits Line 560  Int_t PamelaDBOperations::SetUpperLimits
560          ph = eh->GetPscuHeader();          ph = eh->GetPscuHeader();
561          t_pktlast = PKT(ph->GetCounter());          t_pktlast = PKT(ph->GetCounter());
562          t_obtlast = OBT(ph->GetOrbitalTime());          t_obtlast = OBT(ph->GetOrbitalTime());
563          if ( t_pktlast < spktt && t_obtlast < sobtt ){ // jump          if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
564            upperpkt = pktt;            upperpkt = pktt;
565            upperobt = obtt;            upperobt = obtt;
566            upperentry = evbeft-1;            upperentry = evbeft-1;
# Line 496  Int_t PamelaDBOperations::SetUpperLimits Line 580  Int_t PamelaDBOperations::SetUpperLimits
580          };          };
581        };        };
582        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);
583        break;        goto kikko;
584          //      break;
585        //        //
586      };      };
587      //        //  
588    };    };
589    //    //
590    // kikko:   kikko:
591      //
592      T->GetEntry(upperentry);
593      code = eh->GetCounter();
594      lasttrail = code->Get(pctp->RunTrailer);
595      lasthead = code->Get(pctp->RunHeader);
596      if ( lasttrail < rtev ){
597        rt->GetEntry(lasttrail);
598        pht = eht->GetPscuHeader();
599        pktt = PKT(pht->GetCounter());
600        obtt = OBT(pht->GetOrbitalTime());
601      };
602      //
603      if ( lasthead < rhev ){
604        rh->GetEntry(lasthead);
605        phh = ehh->GetPscuHeader();
606        pkth = PKT(phh->GetCounter());
607        obth = OBT(phh->GetOrbitalTime());
608      };
609      //
610      if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
611      if ( pkth > upperpkt && obth > upperobt ){
612        if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);
613        upperpkt = pkth;
614        upperobt = obth;
615        rhev = lasthead+1;
616      } else {
617        rhev = lasthead;
618      };
619      if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
620      //
621      if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
622      if ( pktt > upperpkt && obtt > upperobt ){
623        if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);
624        upperpkt = pktt;
625        upperobt = obtt;
626        rtev = lasttrail+1;
627      } else {
628        rtev = lasttrail;
629      };
630      if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
631    //    //
632    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);
633    //    //
634    return(0);    return(0);
635  }  }
636    
637    /**
638     *
639     * Trick to have unique RUN ID numbers even when runs are deleted and mysql deamon restarted.
640     * Entries in the _RUNID_GEN table are never deleted.
641     *
642     **/
643    UInt_t PamelaDBOperations::AssignRunID(){
644      //
645      TSQLResult *result = 0;
646      TSQLRow *row = 0;
647      UInt_t runid = 0;
648      //
649      stringstream   oss;
650      //  
651      oss.str("");
652      oss << "INSERT INTO _RUNID_GEN VALUES (NULL);";
653      result = conn->Query(oss.str().c_str());
654      if ( !result ) throw -10;
655      oss.str("");
656      oss << "SELECT ID FROM _RUNID_GEN ORDER BY ID DESC LIMIT 1;";
657      result = conn->Query(oss.str().c_str());
658      if ( !result ) throw -10;
659      //
660      row = result->Next();
661      //
662      if ( !row ) throw -28;
663      //
664      runid = (UInt_t)atoll(row->GetField(0));
665      //
666      return(runid);
667    };
668    
669  //  //
670  // GETTERS  // GETTERS
671  //  //
# Line 584  const PacketType* PamelaDBOperations::Ge Line 741  const PacketType* PamelaDBOperations::Ge
741  // PRIVATE FUNCTIONS  // PRIVATE FUNCTIONS
742  //  //
743    
744  /**  // /**
745   * Open the ROOT filename for reading  // * Open the ROOT filename for reading
746   */  // */
747  void PamelaDBOperations::OpenFile(){  // void PamelaDBOperations::OpenFile(){
748    file = TFile::Open(this->GetRootName().Data());  //   file = TFile::Open(this->GetRootName().Data());
749    //   //
750    
751    void PamelaDBOperations::CheckFile(){  
752      if ( !file ) throw -12;
753  };  };
754    
755    
756  /**  /**
757   * Check if LEVEL0 file and DB connection have really be opened   * Check if LEVEL0 file and DB connection have really be opened
758   */   */
759  void PamelaDBOperations::CheckFile(){    void PamelaDBOperations::CheckConnection(){  
   //  
   if ( !file ) throw -12;  
760    //    //
761    // check connection    // check connection
762    //    //
763    if( !conn ) throw -1;        if( !conn ) throw -1;
764    bool connect = conn->IsConnected();    bool connect = conn->IsConnected();
765    if( !connect ) throw -1;        if( !connect ) throw -1;
766      if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
767  };  };
768    
769  /**  /**
# Line 783  Int_t PamelaDBOperations::insertPamelaGL Line 943  Int_t PamelaDBOperations::insertPamelaGL
943    if ( this->GetID_RAW() == 0 )  throw -11;    if ( this->GetID_RAW() == 0 )  throw -11;
944    //    //
945    oss.str("");    oss.str("");
946    oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ID_RAW<= "    oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
947        << id << " AND TO_ID_RAW >= "        << this->GetRawFile().Data() << "';";
       << id << ";";  
948    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
949    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
950    if ( !result ) throw -10;    if ( !result ) throw -10;
951    row = result->Next();    row = result->Next();
952    if ( !row ) throw -10;    //
953      if ( !row ){
954        oss.str("");
955        oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
956            << dworbit << " order by FROM_ORBIT desc limit 1;";
957        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
958        result = conn->Query(oss.str().c_str());
959        if ( !result ) throw -10;
960        row = result->Next();
961        if ( !row ) throw -10;
962      };
963    //    //
964    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();
965    /*    /*
# Line 864  Int_t PamelaDBOperations::insertPamelaGL Line 1033  Int_t PamelaDBOperations::insertPamelaGL
1033          //          //
1034          TYPE = 55;//224;          TYPE = 55;//224;
1035          //          //
1036            if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
1037            //
1038          if ( TSYNC && OBT ){          if ( TSYNC && OBT ){
1039            existsts = true;            existsts = true;
1040            goto eout;            goto eout;
# Line 886  Int_t PamelaDBOperations::insertPamelaGL Line 1057  Int_t PamelaDBOperations::insertPamelaGL
1057      //      //
1058      rt->SetBranchAddress("RunTrailer", &runt);      rt->SetBranchAddress("RunTrailer", &runt);
1059      //      //
1060      if ( rhev > 0 ){      Int_t nrhev = rh->GetEntries();
1061        rh->GetEntry(0);      Int_t nrtev = rt->GetEntries();
1062        //      if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev);
1063        TSYNC = runh->LAST_TIME_SYNC_INFO;      //
1064        OBT = runh->OBT_TIME_SYNC * 1000;      if ( nrhev > 0 ){
1065        //        for (Int_t i=0; i<nrhev; i++){
1066        TYPE = 20;          //
1067        //          rh->GetEntry(i);
1068        if ( TSYNC && OBT ){          //
1069          existsts = true;          TSYNC = runh->LAST_TIME_SYNC_INFO;
1070          goto eout;          OBT = runh->OBT_TIME_SYNC * 1000;
1071            //
1072            TYPE = 20;
1073            //
1074            if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
1075            //
1076            if ( TSYNC && OBT ){
1077              existsts = true;
1078              goto eout;
1079            };
1080        };        };
1081        //        //
1082      };      };
1083      if ( rtev > 0 ){      if ( nrtev > 0 ){
1084        //        //
1085        if ( IsDebug() ) printf(" No runheader \n");        if ( IsDebug() ) printf(" No runheader \n");
1086        signal = 6;        signal = 6;
1087        //        //
1088        rt->GetEntry(0);        for (Int_t i=0; i<nrtev; i++){
1089        //          //
1090        TSYNC = runt->LAST_TYME_SYNC_INFO;          rt->GetEntry(i);
1091        OBT = runt->OBT_TYME_SYNC * 1000;          //
1092        //          TSYNC = runt->LAST_TYME_SYNC_INFO;
1093        TYPE = 21;          OBT = runt->OBT_TYME_SYNC * 1000;
1094        //          //
1095        if ( TSYNC && OBT ){          TYPE = 21;
1096          existsts = true;          //
1097          goto eout;          if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
1098            //
1099            if ( TSYNC && OBT ){
1100              existsts = true;
1101              goto eout;
1102            };
1103        };        };
1104        //        //
1105      } else {      } else {
# Line 1381  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1566  Bool_t PamelaDBOperations::IsRunAlreadyI
1566      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1567        //        //
1568        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");
1569        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1570        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1571        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) <<";";
1572        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1573    //       conn->Query(oss.str().c_str());
1574        if ( signal ) signal = false;        if ( signal ) signal = false;
1575        goto gonext;              goto gonext;      
1576        //        //
# Line 1398  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1584  Bool_t PamelaDBOperations::IsRunAlreadyI
1584        //        //
1585        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");
1586        //        //
1587        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1588        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1589        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) <<";";
1590        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1591    //       conn->Query(oss.str().c_str());
1592        //        //
1593        if ( signal ) signal = false;        if ( signal ) signal = false;
1594        goto gonext;        goto gonext;
# Line 1415  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1602  Bool_t PamelaDBOperations::IsRunAlreadyI
1602        //        //
1603        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");
1604        //        //
1605        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1606        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1607        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) <<";";
1608        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1609    //       conn->Query(oss.str().c_str());
1610        if ( signal ) signal = false;        if ( signal ) signal = false;
1611        //        //
1612      };      };
# Line 1454  void PamelaDBOperations::HandleRun(){   Line 1642  void PamelaDBOperations::HandleRun(){  
1642      //      //
1643      this->FillClass();      this->FillClass();
1644      //      //
1645      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);      if ( !IsRunAlreadyInserted() ){
1646          glrun->SetID(this->AssignRunID());
1647          glrun->SetID_RUN_FRAG(0);
1648          glrun->Fill_GL_RUN(conn);
1649        };
1650    } else {    } else {
1651      //      //
1652      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 1669  void PamelaDBOperations::HandleRunFragme
1669    UInt_t rhfirstev = firstev;    UInt_t rhfirstev = firstev;
1670    UInt_t rtlastev = lastev;    UInt_t rtlastev = lastev;
1671    Bool_t found = false;    Bool_t found = false;
1672      Bool_t foundinrun = false;
1673    //    //
1674    TSQLResult *result = 0;    TSQLResult *result = 0;
1675    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 1503  void PamelaDBOperations::HandleRunFragme Line 1696  void PamelaDBOperations::HandleRunFragme
1696    //    //
1697    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");
1698    //    //
1699      // First of all insert the run in the fragment table...
1700      //
1701      oss.str("");
1702      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1703          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1704          << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1705          << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1706          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1707          << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1708          << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1709          << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1710          << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1711          << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1712          << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1713          << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1714          << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1715          << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1716      //
1717      if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1718      result = conn->Query(oss.str().c_str());
1719      //
1720      if ( !result ) throw -4;
1721      //
1722      row = result->Next();
1723      //
1724      if ( !row ){
1725        //
1726        // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
1727        //
1728        if ( IsDebug() ) printf(" The run is new \n");
1729        if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
1730        //
1731        glrun->SetID(this->AssignRunID());
1732        glrun->SetID_RUN_FRAG(0);
1733        glrun->Fill_GL_RUN_FRAGMENTS(conn);
1734        //
1735      } else {
1736        if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
1737      };
1738      //
1739      //
1740    // 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?
1741    //        //    
1742    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 1745  void PamelaDBOperations::HandleRunFragme
1745      oss.str("");      oss.str("");
1746      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 "
1747          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1748          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME()          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1749            << " ID != " << glrun->ID
1750          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1751      //      //
1752      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 1756  void PamelaDBOperations::HandleRunFragme
1756      //      //
1757      row = result->Next();      row = result->Next();
1758      //      //
1759        if ( !row && NoFrag() ){
1760          //
1761          oss.str("");
1762          oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
1763              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1764              << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1765              << " ID != " << glrun->ID
1766              << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1767          //
1768          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1769          result = conn->Query(oss.str().c_str());
1770          //
1771          if ( !result ) throw -4;
1772          //
1773          foundinrun = true;
1774          //
1775          row = result->Next();
1776          //
1777        };
1778        //
1779      if ( !row ){      if ( !row ){
1780        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1781        found = false;        found = false;
# Line 1595  void PamelaDBOperations::HandleRunFragme Line 1850  void PamelaDBOperations::HandleRunFragme
1850        //        //
1851        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 ");
1852        //        //
1853          if ( foundinrun ){
1854            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
1855            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1856          };
1857          //
1858        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
1859        //        //
1860        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
1861        //        //
1862        oss.str("");        oss.str("");
1863        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1626  void PamelaDBOperations::HandleRunFragme Line 1886  void PamelaDBOperations::HandleRunFragme
1886        };        };
1887        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);
1888        //        //
       glrun1->SetID(0);  
1889        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
1890        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
1891        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
# Line 1635  void PamelaDBOperations::HandleRunFragme Line 1894  void PamelaDBOperations::HandleRunFragme
1894        //        //
1895        glrun->SetEV_FROM(firstev);        glrun->SetEV_FROM(firstev);
1896        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
1897          //
1898        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
1899        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
1900        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
# Line 1654  void PamelaDBOperations::HandleRunFragme Line 1914  void PamelaDBOperations::HandleRunFragme
1914        //        //
1915        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
1916          //          //
1917            //      glrun->SetID(this->AssignRunID());
1918            glrun->SetID_RUN_FRAG(glrun1->GetID());
1919          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
1920          //          //
1921          // 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);  
1922          //          //
1923            glrun1->SetID_RUN_FRAG(glrun->GetID());
1924          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
1925          //          //
         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;  
         //  
1926        };        };
       //  
       delete glrun1;  
       //  
1927        // delete old entry in fragment table        // delete old entry in fragment table
1928        //        //
1929        oss.str("");        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
1930          glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
1931        //        //
1932        oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";        delete glrun1;
1933        //        //
       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;  
1934        //        //
1935        return;        return;
1936        //        //
# Line 1723  void PamelaDBOperations::HandleRunFragme Line 1939  void PamelaDBOperations::HandleRunFragme
1939    };    };
1940    //    //
1941    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
1942                                             // 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
1943      //      //
1944      oss.str("");      oss.str("");
1945      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 "
1946          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1947          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME()          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
1948            << " ID != " << glrun->ID
1949          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
1950      //      //
1951      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 1955  void PamelaDBOperations::HandleRunFragme
1955      //      //
1956      row = result->Next();      row = result->Next();
1957      //      //
1958        if ( !row && NoFrag() ){
1959          //
1960          oss.str("");
1961          oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
1962              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1963              << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
1964              << " ID != " << glrun->ID
1965              << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
1966          //
1967          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1968          result = conn->Query(oss.str().c_str());
1969          //
1970          if ( !result ) throw -4;
1971          //
1972          foundinrun = true;
1973          row = result->Next();
1974          //
1975        };
1976        //
1977      if ( !row ){      if ( !row ){
1978        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1979        found = false;        found = false;
# Line 1811  void PamelaDBOperations::HandleRunFragme Line 2047  void PamelaDBOperations::HandleRunFragme
2047        //        //
2048        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 ");
2049        //        //
2050          if ( foundinrun ){
2051            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2052            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2053          };
2054          //
2055        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
2056        //        //
2057        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2058        //        //
2059        oss.str("");        oss.str("");
2060        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1850  void PamelaDBOperations::HandleRunFragme Line 2091  void PamelaDBOperations::HandleRunFragme
2091        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
2092        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
2093        //        //
       glrun1->SetID(0);  
2094        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
2095        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
2096        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
# Line 1870  void PamelaDBOperations::HandleRunFragme Line 2110  void PamelaDBOperations::HandleRunFragme
2110        //        //
2111        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2112          //          //
2113          glrun->Fill_GL_RUN(conn);          //      glrun->SetID(this->AssignRunID());
2114          //          //
2115          // get id number          glrun->SetID_RUN_FRAG(glrun1->GetID());
2116            glrun->Fill_GL_RUN(conn);
2117          //          //
2118          oss.str("");          // set id number
         oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "  
             << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "  
             << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";  
2119          //          //
2120          if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());          glrun1->SetID_RUN_FRAG(glrun->GetID());
2121          //          glrun1->Fill_GL_RUN(conn);
         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);  
         //  
         glrun1->Fill_GL_RUN(conn);  
         //  
         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;  
2122          //          //
2123        };        };
2124        //        //
2125        delete glrun1;        // delete old entries in fragment table
       //  
       // delete old entry in fragment table  
       //  
       oss.str("");  
       //  
       oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";  
2126        //        //
2127        if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str());        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2128        result =  conn->Query(oss.str().c_str());        glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
       if ( !result ) throw -4;  
2129        //        //
2130          delete glrun1;
2131        //        //
2132        return;        return;
2133        //        //
# Line 1968  void PamelaDBOperations::HandleRunFragme Line 2164  void PamelaDBOperations::HandleRunFragme
2164      //      //
2165      row = result->Next();      row = result->Next();
2166      //      //
2167      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 {  
2168        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");
2169        } else {
2170          if ( NoFrag() ){
2171            glrun->SetID_RUN_FRAG(glrun->GetID());
2172            glrun->Fill_GL_RUN(conn);      
2173            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2174          };    
2175      };      };
2176    };    };
2177    //    //
# Line 2032  void PamelaDBOperations::HandleMissingHo Line 2197  void PamelaDBOperations::HandleMissingHo
2197      //      //
2198      this->FillClass(mishead,mistrail,firstev,lastev);      this->FillClass(mishead,mistrail,firstev,lastev);
2199      //      //
2200      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2201          glrun->SetID(this->AssignRunID());
2202          glrun->SetID_RUN_FRAG(0);
2203          glrun->Fill_GL_RUN(conn);    
2204        };
2205      //      //
2206    };    };
2207    //    //
# Line 2199  Bool_t PamelaDBOperations::IsRunConsiste Line 2368  Bool_t PamelaDBOperations::IsRunConsiste
2368            //                  //      
2369            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2370            //            //
2371            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2372                glrun->SetID(this->AssignRunID());
2373                glrun->SetID_RUN_FRAG(0);
2374                glrun->Fill_GL_RUN(conn);      
2375              };
2376            //            //
2377            firstevno = lastentry + 1;            firstevno = lastentry + 1;
2378            //            //
# Line 2275  void PamelaDBOperations::HandleSuspiciou Line 2448  void PamelaDBOperations::HandleSuspiciou
2448      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2449      //      //
2450      this->FillClass();      this->FillClass();
2451      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2452          glrun->SetID(this->AssignRunID());
2453          glrun->SetID_RUN_FRAG(0);
2454          glrun->Fill_GL_RUN(conn);    
2455        };
2456      //      //
2457    } else {    } else {
2458      //      //
# Line 2303  void PamelaDBOperations::HandleSuspiciou Line 2480  void PamelaDBOperations::HandleSuspiciou
2480        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");
2481        //        //
2482        this->FillClass();        this->FillClass();
2483        if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                if ( !IsRunAlreadyInserted() ){
2484            glrun->SetID(this->AssignRunID());
2485            glrun->SetID_RUN_FRAG(0);
2486            glrun->Fill_GL_RUN(conn);        
2487          };
2488        //        //
2489      } else {      } else {
2490        //        //
# Line 2401  void PamelaDBOperations::HandleSuspiciou Line 2582  void PamelaDBOperations::HandleSuspiciou
2582            //                  //      
2583            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2584            //            //
2585            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2586                glrun->SetID(this->AssignRunID());
2587                glrun->SetID_RUN_FRAG(0);
2588                glrun->Fill_GL_RUN(conn);      
2589              };
2590            //            //
2591            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...
2592              //              //
# Line 2436  void PamelaDBOperations::HandleSuspiciou Line 2621  void PamelaDBOperations::HandleSuspiciou
2621              //                  //    
2622              this->SetCommonGLRUN(firstTime,lastTime);              this->SetCommonGLRUN(firstTime,lastTime);
2623              //              //
2624              if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                    if ( !IsRunAlreadyInserted() ){
2625                  glrun->SetID(this->AssignRunID());
2626                  glrun->SetID_RUN_FRAG(0);
2627                  glrun->Fill_GL_RUN(conn);      
2628                };
2629            };            };
2630            //            //
2631            firstevno = lastentry + 1;            firstevno = lastentry + 1;
# Line 3117  Int_t PamelaDBOperations::insertS4_CALIB Line 3306  Int_t PamelaDBOperations::insertS4_CALIB
3306   * 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
3307   */   */
3308  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3309      return(this->CleanGL_RUN_FRAGMENTS(""));
3310    };
3311    
3312    /**
3313     * Scan the fragment table and move old fragments to the GL_RUN table
3314     */
3315    Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3316    //    //
3317    TSQLResult *result = 0;    TSQLResult *result = 0;
3318    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 3124  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3320  Int_t PamelaDBOperations::CleanGL_RUN_FR
3320    TSQLRow    *row2   = 0;    TSQLRow    *row2   = 0;
3321    //    //
3322    UInt_t moved = 0;    UInt_t moved = 0;
   UInt_t timelim = 0;  
   TDatime *time = new TDatime();  
3323    //    //
3324    stringstream oss;    stringstream oss;
3325    oss.str("");    oss.str("");
3326    //    //
3327    //    if ( !strcmp(fcleanfile.Data(),"") ){
3328    //      //
3329    if ( olderthan < 0 ){      // check if there are entries older than "olderthan" seconds from now
3330      if ( IsDebug() ) printf(" Do not clean GL_RUN_FRAGMENTS table \n");      //
3331      return(1);      oss.str("");
3332        oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3333            << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3334        //
3335        if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
3336        result = conn->Query(oss.str().c_str());
3337        //
3338      } else {
3339        oss.str("");
3340        oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3341        if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3342        result = conn->Query(oss.str().c_str());
3343        //    
3344        if ( result ){
3345          //
3346          row = result->Next();
3347          //
3348          if ( row ){
3349            oss.str("");
3350            oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3351                << " ID_ROOT_L0=" << row->GetField(0) << ";";
3352            //
3353            if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3354            result = conn->Query(oss.str().c_str());
3355            //    
3356          };
3357        } else {
3358          return(2);
3359        };
3360    };    };
3361    //    //
   // 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());  
   //  
3362    if ( result ){    if ( result ){
3363      //      //
3364      row = result->Next();      row = result->Next();
3365      //      //
3366      if ( row ){      while ( row ){
3367        //        //
3368        oss.str("");        oss.str("");
3369        oss << " ID= "<< row->GetField(0);        oss << " ID= "<< row->GetField(0);
# Line 3191  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3398  Int_t PamelaDBOperations::CleanGL_RUN_FR
3398          if ( IsDebug() ) printf(" The run is new \n");          if ( IsDebug() ) printf(" The run is new \n");
3399          if ( IsDebug() ) printf(" -> fill the DB \n");                if ( IsDebug() ) printf(" -> fill the DB \n");      
3400          //          //
3401          glrun->SetID(0);          //      glrun->SetID(this->AssignRunID()); we use the old run number!
3402            glrun->SetID_RUN_FRAG(glrun->GetID());
3403          glrun->Fill_GL_RUN(conn);            glrun->Fill_GL_RUN(conn);  
3404          //          //
3405          oss.str("");  //      oss.str("");
3406          oss << " SELECT ID FROM GL_RUN WHERE "  //      oss << " SELECT ID FROM GL_RUN WHERE "
3407              << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "  //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3408              << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "  //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3409              << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "  //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3410              << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "  //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3411              << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";  //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3412          //  //      //
3413          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());
3414          result2 = conn->Query(oss.str().c_str());  //      result2 = conn->Query(oss.str().c_str());
3415          //  //      //
3416          if ( !result2 ) throw -4;  //      if ( !result2 ) throw -4;
3417          //  //      //
3418          row2 = result2->Next();  //      row2 = result2->Next();
3419          //  //      //
3420          if ( !row2 ) throw -25;  //      if ( !row2 ) throw -25;
3421          //  //      //
3422          oss.str("");  //      oss.str("");
3423          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);
3424          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());
3425          result2 = conn->Query(oss.str().c_str());  //      result2 = conn->Query(oss.str().c_str());
3426          //  //      //
3427          if ( !result2 ) throw -4;  //      if ( !result2 ) throw -4;
3428          //          //
3429          moved++;          moved++;
3430          //          //
# Line 3226  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3434  Int_t PamelaDBOperations::CleanGL_RUN_FR
3434        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));      
3435        //        //
3436        //        //
3437        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3438        oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);  //      oss.str("");
3439        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);
3440        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());
3441        //  //       result2 = conn->Query(oss.str().c_str());
3442        if ( !result2 ) throw -4;  //      //
3443        //  //      if ( !result2 ) throw -4;
3444    //      //
3445          row = result->Next();
3446      };      };
3447    };    };
3448    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 3453  Int_t PamelaDBOperations::CleanGL_RUN_FR
3453   * 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..
3454   */   */
3455  Int_t PamelaDBOperations::ValidateRuns(){  Int_t PamelaDBOperations::ValidateRuns(){
3456      return(this->ValidateRuns(""));
3457    };
3458    
3459    /**
3460     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3461     */
3462    Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3463    //    //
3464    TSQLResult *result = 0;    TSQLResult *result = 0;
3465    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
3466    //    //
3467    UInt_t timelim = 0;    UInt_t calibtime = 50;
   TDatime *time = new TDatime();  
3468    //    //
3469    stringstream oss;    stringstream oss;
3470    oss.str("");    oss.str("");
3471    //    //
   //  
   //  
   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());  
   
3472    // =======================================================    // =======================================================
3473    // validate runs by checking missing calibrations    // validate runs by checking missing calibrations
3474    // =======================================================    // =======================================================
3475    UInt_t t_stop  = 0;    UInt_t t_stop  = 0;
3476    UInt_t t_start = 0;    UInt_t t_start = 0;
3477    // --------------------------------------------------------------    if ( !strcmp(valfile.Data(),"") ) {
3478    // 1) get the OBT of the last run inserted after clean-time limit      // --------------------------------------------------------------
3479    // --------------------------------------------------------------      // 1) get the OBT of the last run inserted after clean-time limit
3480    oss.str("");      // --------------------------------------------------------------
3481    oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << time->AsSQLString()      oss.str("");
3482        << "' ORDER BY INSERT_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3483    printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());          << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3484    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3485    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3486    if ( !result->GetRowCount() ) {      if ( !result ) throw -4;
3487            printf(" No runs to validate \n");      if ( !result->GetRowCount() ) {
3488            return(1);        printf(" No runs to validate \n");
3489    }else{        return(1);
3490          row = result->Next();      }else{
3491          t_start = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3492          printf("t_start %i\n",t_start);        t_start = (UInt_t)atoll(row->GetField(4));
3493    };        };  
3494    // --------------------------------------------------------------      // --------------------------------------------------------------
3495    // 2) get the OBT of the last validated run      // 2) get the OBT of the last validated run
3496    // --------------------------------------------------------------      // --------------------------------------------------------------
3497    oss.str("");      oss.str("");
3498    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
3499        <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3500    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());
3501    result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
3502    if ( !result ) throw -4;      if ( !result ) throw -4;
3503    if ( result->GetRowCount() ){      if ( result->GetRowCount() ){
3504            row = result->Next();        row = result->Next();
3505            t_stop = (UInt_t)atoll(row->GetField(4));        t_stop = (UInt_t)atoll(row->GetField(4));
3506        };
3507        if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);
3508        // --------------------------------------------------------------
3509        // now retrieves runs to be validated
3510        // --------------------------------------------------------------
3511        oss.str("");
3512        oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;
3513        oss << " AND RUNHEADER_TIME >="<< t_stop;
3514        oss << " ORDER BY RUNHEADER_TIME DESC;";
3515        if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3516        result = conn->Query(oss.str().c_str());
3517      } else {
3518        //
3519        stringstream myquery;
3520        UInt_t myid = 0;
3521        myquery.str("");
3522        myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3523        //
3524        result = conn->Query(myquery.str().c_str());
3525        //
3526        row = result->Next();      
3527        if( !row ){
3528          if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3529            if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3530            return(2);
3531          };
3532          if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3533          return(0);
3534        };
3535        myid=(UInt_t)atoll(row->GetField(0));
3536        //
3537        myquery.str("");
3538        myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3539        //
3540        result = conn->Query(myquery.str().c_str());
3541        //
3542        row = result->Next();      
3543        if( !row->GetField(0) || !row->GetField(1)){
3544          //
3545          if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3546          //
3547          return(0);
3548          //
3549        } else {  
3550          //
3551          UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3552          UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3553          UInt_t caltime = 0;
3554          //
3555          myquery.str("");
3556          myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3557          myquery << " order by FROM_TIME asc limit 1;";
3558          //
3559          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3560          //
3561          //
3562          result = conn->Query(myquery.str().c_str());
3563          //
3564          row = result->Next();      
3565          if( !row ){
3566            caltime = runhtime;
3567          } else {
3568            caltime = (UInt_t)atoll(row->GetField(0));
3569          };
3570          //
3571          myquery.str("");
3572          myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3573          myquery << caltime << " order by RUNHEADER_TIME DESC";
3574          //
3575          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3576          //
3577          result = conn->Query(myquery.str().c_str());
3578          //
3579        };
3580    };    };
3581    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());  
3582    if ( !result ) throw -4;    if ( !result ) throw -4;
3583    if ( !result->GetRowCount() ) printf(" No runs to validate \n");    if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3584    printf("------------------------------------------------------------------------------- \n");    //
     
3585    Int_t nrow = 0;    Int_t nrow = 0;
3586    GL_RUN* this_run = new GL_RUN();    GL_RUN* this_run = new GL_RUN();
3587    GL_RUN* next_run = new GL_RUN();    GL_RUN* next_run = new GL_RUN();
3588    Int_t   nseq_max = 100;    Int_t   nseq_max = 1000;
3589  //  UInt_t* sequence = new UInt_t[100];  //  UInt_t* sequence = new UInt_t[100];
3590    vector<UInt_t> sequence(nseq_max);    vector<UInt_t> sequence(nseq_max);
3591    Int_t   nseq = 0;    Int_t   nseq = 0;
# Line 3331  Int_t PamelaDBOperations::ValidateRuns() Line 3595  Int_t PamelaDBOperations::ValidateRuns()
3595    UInt_t t1=0,t2=0;    UInt_t t1=0,t2=0;
3596    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
3597    // - loop over runs, back in time,    // - loop over runs, back in time,
3598    // - select sequences of runs close in time (less than 60 s apart),    // - select sequences of runs close in time (less than calibtime s apart),
3599    //   which could be preceeded by a calibration    //   which could be preceeded by a calibration
3600    // - check if there might be a missing calibration    // - check if there might be a missing calibration
3601    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
# Line 3344  Int_t PamelaDBOperations::ValidateRuns() Line 3608  Int_t PamelaDBOperations::ValidateRuns()
3608            //get run info            //get run info
3609            //------------            //------------
3610            this_run->Set_GL_RUN(row);            this_run->Set_GL_RUN(row);
3611                                
           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);  
             
3612            Bool_t this_BAD = false;            Bool_t this_BAD = false;
3613            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;
3614            else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;            else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;
3615            else{            else{
3616                    printf("Missing or corrupted header!! \n");  //                printf("Missing or corrupted header!! \n");
3617                    this_ONLINE = false;                    this_ONLINE = false;
3618                    this_BAD = true;                    this_BAD = true;
3619            };            };
# Line 3373  Int_t PamelaDBOperations::ValidateRuns() Line 3635  Int_t PamelaDBOperations::ValidateRuns()
3635                                                        
3636                            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
3637                                                        
3638                            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());*/  
                                                     
3639                                                        
3640                            if( !CHECK && this_run->VALIDATION ){                            if( !CHECK && this_run->VALIDATION ){
3641                                    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 3645  Int_t PamelaDBOperations::ValidateRuns()
3645                    }else if( !this_ONLINE && next_ONLINE) {              // this: DEFAULT + next:ON-LINE                    }else if( !this_ONLINE && next_ONLINE) {              // this: DEFAULT + next:ON-LINE
3646                                                        
3647                            CHECK = true;                            CHECK = true;
 //                        if( interval < 60 ) printf(" ValidateRuns ***WARNING*** : kkkkkk DT = %i \n",interval);  
3648    
3649                    }else if( !next_ONLINE ){                                             // this:ANY + next:DEFAULT                    }else if( !next_ONLINE ){                                             // this:ANY + next:DEFAULT
3650                                                        
# Line 3401  Int_t PamelaDBOperations::ValidateRuns() Line 3658  Int_t PamelaDBOperations::ValidateRuns()
3658            //----------------------------            //----------------------------
3659            if( CHECK ){            if( CHECK ){
3660                    // check if calibration exists                    // check if calibration exists
3661                    printf("DT %i ===> CHECK Missing calibration\n",interval);                    if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
3662                    Bool_t MISSING = MissingTRK_CALIB(t1,t2);                    Bool_t MISSING = MissingTRK_CALIB(t1,t2);
3663                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
3664                    nseq=0;                    nseq=0;
3665            } else printf("DT %i\n",interval);            };
3666            //--------------            //--------------
3667            //store run info            //store run info
3668            //--------------            //--------------
# Line 3418  Int_t PamelaDBOperations::ValidateRuns() Line 3675  Int_t PamelaDBOperations::ValidateRuns()
3675                    }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);
3676            };            };
3677                        
3678              if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID);
3679            nrow++;            nrow++;
3680                        
3681    };    };
# Line 3446  Bool_t PamelaDBOperations::MissingTRK_CA Line 3704  Bool_t PamelaDBOperations::MissingTRK_CA
3704          // which should be equal to the time between ascending-nodes.          // which should be equal to the time between ascending-nodes.
3705          //==============================================================          //==============================================================
3706          if ( t2 - trkcalib->FROM_TIME > 5700) {          if ( t2 - trkcalib->FROM_TIME > 5700) {
3707                  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);
3708          //==============================================================          //==============================================================
3709          // there might be a missing calibration, due to:          // there might be a missing calibration, due to:
3710          // - MM full          // - MM full
# Line 3467  Bool_t PamelaDBOperations::MissingTRK_CA Line 3725  Bool_t PamelaDBOperations::MissingTRK_CA
3725          // it is enough to say that there are no missing calibrations          // it is enough to say that there are no missing calibrations
3726          //==============================================================          //==============================================================
3727          // the long time interval bewteen runs might be due to download          // the long time interval bewteen runs might be due to download
3728          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);
3729          return(false);          return(false);
3730                    
3731  };  };
# Line 3480  Int_t PamelaDBOperations::assignVALIDATI Line 3738  Int_t PamelaDBOperations::assignVALIDATI
3738          TSQLResult *result = 0;          TSQLResult *result = 0;
3739          stringstream oss;          stringstream oss;
3740          oss.str("");          oss.str("");
3741          oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << "';";          oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
3742          //          //
3743  //      if ( IsDebug() )  //      if ( IsDebug() )
3744          printf(" Set VALIDATION = %i for run %i \n",validation,idrun);  //      printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
3745  //      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());
3746  //      result = conn->Query(oss.str().c_str());          result = conn->Query(oss.str().c_str());
3747  //      if ( !result ) throw -4;          if ( !result ) throw -4;
3748          return(0);          return(0);
3749  }  }
3750    
3751    
3752    
3753    // Insert TLEs from file tlefilename in the table GL_TLE in the db
3754    // opened by conn, sorting them by date from older to newer, if each
3755    // TLE has not been alread inserted.
3756    Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
3757    {
3758      fstream tlefile(tlefilename, ios::in);
3759    
3760      if ( !tlefile ) throw -7;
3761    
3762      vector<cTle*> ctles;
3763      vector<cTle*>::iterator iter;
3764      int present = 0;
3765    
3766      // Get three lines from tlefile, create a cTle object and put it
3767      // into ctles
3768      while(1) {
3769        cTle *tlef;
3770        string str1, str2, str3;
3771    
3772        getline(tlefile, str1);
3773        if(tlefile.eof()) break;
3774    
3775        getline(tlefile, str2);
3776        if(tlefile.eof()) break;
3777    
3778        getline(tlefile, str3);
3779        if(tlefile.eof()) break;
3780    
3781        // We now have three good lines for a cTle.
3782        tlef = new cTle(str1, str2, str3);
3783        ctles.push_back(tlef);
3784      }
3785    
3786      tlefile.close();
3787    
3788      // Sort by date
3789      sort(ctles.begin(), ctles.end(), compTLE);
3790    
3791      // Now we insert each TLE into the db
3792      for(iter = ctles.begin(); iter != ctles.end(); iter++) {
3793        cTle *tle = *iter;
3794    
3795        // Do nothing if it's already present in the db.  Just increase
3796        // the counter present.
3797        if (! isTlePresent(tle))
3798          {
3799            int status = insertTle(tle);
3800    
3801            // Insert query failed.  Return 1.
3802            if(status == EXIT_FAILURE) {
3803              
3804              if( IsDebug() ) {
3805                cerr << "Error: inserting TLE:" << endl
3806                     << tle->getName() << endl
3807                     << tle->getLine1() << endl
3808                     << tle->getLine2() << endl;
3809              }
3810    
3811              throw -4;
3812              return 1;
3813            }
3814    
3815          }
3816        else
3817          present++;
3818    
3819      }
3820    
3821      int inserted = ctles.size() - present;  // Number of inserted TLE.
3822      if ( IsDebug() )
3823        cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
3824             << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
3825    
3826      ctles.clear();
3827    
3828    
3829      // Return 2 if no new TLE has been inserted.  0 otherwise.
3830      if(! inserted ) return 2;
3831      return 0;
3832    }
3833    
3834    
3835    // Insert tle in the table GL_TLE using the connection conn.
3836    Int_t PamelaDBOperations::insertTle(cTle *tle)
3837    {
3838      stringstream oss;
3839      TSQLResult *result = 0;
3840    
3841      oss.str("");
3842      oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
3843          << " VALUES ( '"
3844          << tle->getName() << "', '"
3845          << tle->getLine1() << "', '"
3846          << tle->getLine2() << "', '"
3847          << getTleDatetime(tle) << "')";
3848    
3849      //  cout << oss.str().c_str() << endl;
3850      result = conn->Query(oss.str().c_str());
3851      if (result == NULL)
3852        return EXIT_FAILURE;
3853    
3854      return EXIT_SUCCESS;
3855    }
3856    
3857    
3858    // Return whether tle is already in the db connected by conn.
3859    bool PamelaDBOperations::isTlePresent(cTle *tle)
3860    {
3861      stringstream oss;
3862      TSQLResult *result = 0;
3863    
3864      oss.str("");
3865      oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
3866          << getTleDatetime(tle) << "'";
3867    
3868      result = conn->Query(oss.str().c_str());
3869      if (result == NULL) throw -4;
3870    
3871      if (result->GetRowCount())
3872        return true;
3873      else
3874        return false;
3875    }
3876    
3877    
3878    // Return whether the first TLE is dated early than the second
3879    bool compTLE (cTle *tle1, cTle *tle2)
3880    {
3881      return getTleJulian(tle1) < getTleJulian(tle2);
3882    }
3883    
3884    
3885    // Return the date of the tle using the format (year-2000)*1e3 +
3886    // julian day.  e.g. 6365 is the 31th Dec 2006.
3887    // It does *not* return a cJulian date.
3888    float getTleJulian(cTle *tle) {
3889      return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
3890    }
3891    
3892    
3893    // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
3894    // format.
3895    string getTleDatetime(cTle *tle)
3896    {
3897      int year, mon, day, hh, mm, ss;
3898      double dom; // day of month (is double!)
3899      stringstream date; // date in datetime format
3900    
3901      // create a cJulian from the date in tle
3902      cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
3903    
3904      // get year, month, day of month
3905      jdate.getComponent(&year, &mon, &dom);
3906    
3907      // build a datetime YYYY-MM-DD hh:mm:ss
3908      date.str("");
3909      day = (int) floor(dom);
3910      hh = (int) floor( (dom - day) * 24);
3911      mm = (int) floor( ((dom - day) * 24 - hh) * 60);
3912      ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
3913      //  ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
3914    
3915      date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
3916    
3917      return date.str();
3918    }
3919    
3920    /**
3921     * Remove a file from the DB, delete on cascade all entries related to that file
3922     * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
3923     * calibration
3924     **/
3925    Int_t PamelaDBOperations::removeFile(TString remfile){
3926      //
3927      // Determine ID_ROOT_L0 and ID_RAW
3928      //
3929      TSQLResult *pResult;
3930      TSQLRow *Row;
3931      stringstream myquery;
3932      //  
3933      myquery.str("");
3934      myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
3935      //
3936      pResult = conn->Query(myquery.str().c_str());
3937      //
3938      Row = pResult->Next();      
3939      if( !Row ){
3940        if ( strcmp(remfile.Data(),GetRootName().Data()) ){
3941          if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
3942          return(1);
3943        };
3944        if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
3945        return(0);
3946      };
3947      //
3948      this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
3949      this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
3950      //
3951      this->ValidationOFF();
3952      //
3953      this->RemoveCALIBS();
3954      //
3955      this->RemoveRUNS();
3956      //
3957      this->RemoveFILES();
3958      //
3959      this->SetID_ROOT(0);
3960      this->SetID_RAW(0);
3961      //
3962      return(0);
3963    };
3964    
3965    /**
3966     *
3967     * Set validation bit to zero for runs following the removing file till
3968     * 1) a run with TRK_CALIB_USED=140
3969     * 2) a run with VALIDATION = 0
3970     * 3) the next calibration
3971     *
3972     **/
3973    void PamelaDBOperations::ValidationOFF(){
3974      TSQLResult *pResult;
3975      TSQLRow *Row;
3976      stringstream myquery;
3977      Int_t unv = 0;
3978      //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
3979      myquery.str("");
3980      myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
3981      //
3982      pResult = conn->Query(myquery.str().c_str());
3983      //
3984      Row = pResult->Next();      
3985      if( !Row->GetField(0) ){
3986        //
3987        if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3988        //
3989      } else {  
3990        //
3991        UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
3992        UInt_t caltime = 0;
3993        //
3994        myquery.str("");
3995        myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3996        myquery << " order by FROM_TIME asc limit 1;";
3997        //
3998        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3999        //
4000        //
4001        delete pResult;
4002        pResult = conn->Query(myquery.str().c_str());
4003        //
4004        Row = pResult->Next();      
4005        if( !Row ){
4006          caltime = runhtime;
4007        } else {
4008          caltime = (UInt_t)atoll(Row->GetField(0));
4009        };
4010        //
4011        myquery.str("");
4012        myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4013        myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4014        //
4015        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4016        //
4017        pResult = conn->Query(myquery.str().c_str());
4018        //
4019        Row = pResult->Next();      
4020        if( !Row ){
4021          //
4022          if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4023          //
4024        } else {
4025          myquery.str("");
4026          myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4027          myquery << " RUNHEADER_TIME>=" <<runhtime;
4028          myquery << " order by RUNHEADER_TIME asc;";
4029          //
4030          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4031          //
4032          pResult = conn->Query(myquery.str().c_str());
4033          //
4034          Row = pResult->Next();  
4035          while ( Row ){
4036            //      
4037            unv++;
4038            this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4039            Row = pResult->Next();  
4040            //
4041          };
4042        };
4043      };
4044      if ( IsDebug() ) printf(" %i runs have been unvalidated \n",unv);
4045    };
4046    
4047    /**
4048     *
4049     * Rearrange GL_RUN table and remove runs
4050     *
4051     **/
4052    void PamelaDBOperations::RemoveRUNS(){
4053      TSQLResult *pResult;
4054      TSQLRow *Row;
4055      stringstream myquery;
4056      UInt_t drun = 0;
4057      GL_RUN *delrun = new GL_RUN();
4058      //
4059      myquery.str("");
4060      myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4061      //
4062      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4063      //
4064      pResult = conn->Query(myquery.str().c_str());
4065      //
4066      Row = pResult->Next();    
4067      //
4068      //
4069      if ( !Row ){
4070        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4071      } else {
4072        if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");    
4073        while ( Row ){
4074          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4075          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4076          drun++;
4077          Row = pResult->Next();    
4078        };
4079      };
4080      //
4081      //
4082      myquery.str("");
4083      myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4084      //
4085      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4086      //
4087      pResult = conn->Query(myquery.str().c_str());
4088      //
4089      Row = pResult->Next();    
4090      //
4091      if ( !Row ){
4092        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4093      } else {
4094        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");    
4095        while ( Row ){
4096          if ( IsDebug() ) printf(" restore run %i \n",(UInt_t)atoll(Row->GetField(1)));
4097          delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4098          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(1)));
4099          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4100          if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4101            if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4102            delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4103          };
4104          drun++;
4105          Row = pResult->Next();    
4106        };
4107      };
4108      //
4109      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);    
4110      //
4111      //
4112      //
4113      drun = 0;
4114      //
4115      myquery.str("");
4116      myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4117      //
4118      pResult = conn->Query(myquery.str().c_str());
4119      //
4120      Row = pResult->Next();    
4121      //
4122      if ( !Row ){
4123        if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4124      } else {
4125        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");    
4126        while ( Row ){
4127          if ( IsDebug() ) printf(" del run idtrash %i \n",(UInt_t)atoll(Row->GetField(0)));
4128          myquery.str("");
4129          myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";      
4130          conn->Query(myquery.str().c_str());
4131          drun++;
4132          Row = pResult->Next();    
4133        };
4134      };
4135      //  
4136      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_TRASH table \n",drun);    
4137      //
4138      //
4139      //
4140      drun = 0;
4141      //
4142      myquery.str("");
4143      myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4144      //
4145      pResult = conn->Query(myquery.str().c_str());
4146      //
4147      Row = pResult->Next();    
4148      //
4149      if ( !Row ){
4150        if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4151      } else {
4152        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");    
4153        while ( Row ){
4154          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4155          myquery.str("");
4156          myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4157          conn->Query(myquery.str().c_str());
4158          drun++;
4159          Row = pResult->Next();    
4160        };
4161      };
4162      //  
4163      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_FRAGMENTS table \n",drun);    
4164      //
4165      //
4166      //
4167      delete delrun;
4168      //
4169    };
4170    
4171    
4172    /**
4173     *
4174     * Rearrange calibration tables
4175     *
4176     **/
4177    void PamelaDBOperations::RemoveFILES(){
4178      stringstream myquery;
4179      //
4180      myquery.str("");
4181      myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4182      //
4183      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4184      //
4185      conn->Query(myquery.str().c_str());
4186      //
4187    };
4188    
4189    /**
4190     *
4191     * Rearrange calibration tables
4192     *
4193     **/
4194    void PamelaDBOperations::RemoveCALIBS(){
4195      TSQLResult *pResult;
4196      TSQLRow *Row;
4197      stringstream myquery;
4198      //  
4199      //
4200      // Calorimeter
4201      //
4202      for (Int_t section = 0; section < 4; section++){
4203        myquery.str("");
4204        myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4205        myquery << " SECTION=" << section << ";";
4206        //
4207        pResult = conn->Query(myquery.str().c_str());
4208        //
4209        Row = pResult->Next();      
4210        if( !Row->GetField(0) || !Row->GetField(1) ){
4211          //
4212          if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4213          //
4214        } else {
4215          //
4216          myquery.str("");
4217          myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4218          myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4219          myquery << " SECTION=" << section << ";";
4220          //
4221          pResult = conn->Query(myquery.str().c_str());
4222          //
4223          if( !pResult ){
4224            //
4225            if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4226            //
4227            throw -4;
4228            //
4229          };
4230          //
4231        };
4232      };
4233      myquery.str("");
4234      myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4235      //
4236      pResult = conn->Query(myquery.str().c_str());
4237      //
4238      if( !pResult ){
4239        //
4240        if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4241        //
4242        throw -4;
4243        //
4244      };
4245      //
4246      // Tracker
4247      //
4248      myquery.str("");
4249      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4250      //
4251      pResult = conn->Query(myquery.str().c_str());
4252      //
4253      Row = pResult->Next();      
4254      if( !Row->GetField(0) || !Row->GetField(1) ){
4255        //
4256        if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4257        //
4258      } else {
4259        //
4260        myquery.str("");
4261        myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4262        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4263        //
4264        pResult = conn->Query(myquery.str().c_str());
4265        //
4266        if( !pResult ){
4267          //
4268          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4269          //
4270          throw -4;
4271          //
4272        };
4273        //
4274        myquery.str("");
4275        myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4276        //
4277        pResult = conn->Query(myquery.str().c_str());
4278        //
4279        if( !pResult ){
4280          //
4281          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4282          //
4283          throw -4;
4284          //
4285        };
4286      };
4287      //
4288      //
4289      // S4
4290      //
4291      myquery.str("");
4292      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4293      //
4294      pResult = conn->Query(myquery.str().c_str());
4295      //
4296      Row = pResult->Next();      
4297      if( !Row->GetField(0) || !Row->GetField(1) ){
4298        //
4299        if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4300        //
4301      } else {
4302        //
4303        myquery.str("");
4304        myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4305        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4306        //
4307        pResult = conn->Query(myquery.str().c_str());
4308        //
4309        if( !pResult ){
4310          //
4311          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4312          //
4313          throw -4;
4314          //
4315        };
4316        //
4317        myquery.str("");
4318        myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4319        //
4320        pResult = conn->Query(myquery.str().c_str());
4321        //
4322        if( !pResult ){
4323          //
4324          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4325          //
4326          throw -4;
4327          //
4328        };
4329        //
4330      };
4331    };

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

  ViewVC Help
Powered by ViewVC 1.1.23