/[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.6 by mocchiut, Fri Sep 8 16:06:02 2006 UTC revision 1.14 by mocchiut, Fri Oct 20 11:39:37 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      };
85    //    //
86    this->SetID_RAW(0);    this->SetID_RAW(0);
87    this->SetID_ROOT(0);    this->SetID_ROOT(0);
88    
89      VALIDATE = false;
90      
91    //    //
92  };  };
93    
# Line 78  PamelaDBOperations::PamelaDBOperations(T Line 96  PamelaDBOperations::PamelaDBOperations(T
96   */   */
97  void PamelaDBOperations::Close(){  void PamelaDBOperations::Close(){
98    if( conn && conn->IsConnected() ) conn->Close();    if( conn && conn->IsConnected() ) conn->Close();
99      delete clean_time;
100    delete glrun;    delete glrun;
101    delete this;    delete this;
102  };  };
# Line 86  void PamelaDBOperations::Close(){ Line 105  void PamelaDBOperations::Close(){
105  // SETTERS  // SETTERS
106  //  //
107    
108    //
109    // 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
110    //
111    void PamelaDBOperations::CheckValidate(Long64_t olderthan){
112      clean_time = new TDatime();
113      if(olderthan >= 0){
114        VALIDATE = true;
115        UInt_t timelim = 0;
116        timelim =  (UInt_t)clean_time->Convert() - olderthan;
117        clean_time->Set(timelim,false);
118      };
119    };
120    
121  /**  /**
122   * Open the DB connection   * Open the DB connection
123   * @param host         hostname for the SQL connection.   * @param host         hostname for the SQL connection.
# Line 93  void PamelaDBOperations::Close(){ Line 125  void PamelaDBOperations::Close(){
125   * @param password     password for the SQL connection.   * @param password     password for the SQL connection.
126   */   */
127  void PamelaDBOperations::SetConnection(TString host, TString user, TString password){  void PamelaDBOperations::SetConnection(TString host, TString user, TString password){
128      if ( IsDebug() ) printf(" Connecting using host = %s user = %s password = %s \n",host.Data(),user.Data(),password.Data());
129    conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());    conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());
130  };  };
131    
# Line 161  void PamelaDBOperations::SetRootName(TSt Line 194  void PamelaDBOperations::SetRootName(TSt
194  };  };
195    
196  /**  /**
197     * Store the downlink orbit number from filename.
198     */
199    void PamelaDBOperations::SetOrbitNo(){
200      dworbit = 0;
201      TString name = this->GetRootFile();
202      Int_t nlength = name.Length();
203      if ( nlength < 5 ) return;
204      TString dwo = 0;
205      for (Int_t i = 0; i<5; i++){
206        dwo.Append(name[i],1);
207      };
208      if ( dwo.IsDigit() ){
209        dworbit = (UInt_t)dwo.Atoi();
210      } else {
211        dwo="";
212        for (Int_t i = 8; i<13; i++){
213          dwo.Append(name[i],1);
214        };    
215        if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
216      };
217      if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
218      return;
219    };
220    
221    
222    
223    /**
224   * Store the NOBOOT flag.   * Store the NOBOOT flag.
225   * @param noboot    true/false.   * @param noboot    true/false.
226   */   */
# Line 169  void PamelaDBOperations::SetNOBOOT(Bool_ Line 229  void PamelaDBOperations::SetNOBOOT(Bool_
229  };  };
230    
231  /**  /**
232     * Store path to the TLE file.
233     */
234    void PamelaDBOperations::SetTLEPath(TString str){
235      tlefilename = str;
236    };
237    
238    /**
239   * Store the olderthan variable   * Store the olderthan variable
240   * @param olderthan   * @param olderthan
241   */   */
242  void PamelaDBOperations::SetOlderThan(Long64_t oldthan){  // void PamelaDBOperations::SetOlderThan(Long64_t oldthan){
243    olderthan = oldthan;  //   olderthan = oldthan;
244  };  // };
245    
246  /**  /**
247   * 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 292  Int_t PamelaDBOperations::SetUpperLimits
292    UInt_t jump = 50000; // was 5000    UInt_t jump = 50000; // was 5000
293    EventCounter *code=0;    EventCounter *code=0;
294    //    //
295      UInt_t deltapkt = 5000;
296      ULong64_t deltaobt = 50000;
297      //
298    //   pcksList packetsNames;    //   pcksList packetsNames;
299    //   pcksList::iterator Iter;    //   pcksList::iterator Iter;
300    //   getPacketsNames(packetsNames);    //   getPacketsNames(packetsNames);
# Line 271  Int_t PamelaDBOperations::SetUpperLimits Line 341  Int_t PamelaDBOperations::SetUpperLimits
341    //    //
342    if ( nevent < 2 ) return(4);    if ( nevent < 2 ) return(4);
343    //    //
344    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) ){
345        //
346      // go back      // go back
347      zomp = nevent - 2;      zomp = nevent - 2;
348      //      //
# Line 401  Int_t PamelaDBOperations::SetUpperLimits Line 472  Int_t PamelaDBOperations::SetUpperLimits
472      pkth = PKT(phh->GetCounter());      pkth = PKT(phh->GetCounter());
473      obth = OBT(phh->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
474      //      //
475      if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);  //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
476      //      //
477      if ( pkth < spkth && obth < sobth ){      if ( pkth < spkth && obth < sobth ){
478        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);
# Line 419  Int_t PamelaDBOperations::SetUpperLimits Line 490  Int_t PamelaDBOperations::SetUpperLimits
490          ph = eh->GetPscuHeader();          ph = eh->GetPscuHeader();
491          t_pktlast = PKT(ph->GetCounter());          t_pktlast = PKT(ph->GetCounter());
492          t_obtlast = OBT(ph->GetOrbitalTime());          t_obtlast = OBT(ph->GetOrbitalTime());
493          if ( t_pktlast < spkth && t_obtlast < sobth ){ // jump          if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
494            upperpkt = pkth;            upperpkt = pkth;
495            upperobt = obth;            upperobt = obth;
496            upperentry = evbefh-1;            upperentry = evbefh-1;
# Line 460  Int_t PamelaDBOperations::SetUpperLimits Line 531  Int_t PamelaDBOperations::SetUpperLimits
531      pktt = PKT(pht->GetCounter());      pktt = PKT(pht->GetCounter());
532      obtt = OBT(pht->GetOrbitalTime());      obtt = OBT(pht->GetOrbitalTime());
533      //      //
534      if ( IsDebug() ) printf(" k %i rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);  //    if ( IsDebug() ) printf(" k %i rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
535      //      //
536      if ( pktt < spktt && obtt < sobtt ){      if ( pktt < spktt && obtt < sobtt ){
537        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);
# Line 479  Int_t PamelaDBOperations::SetUpperLimits Line 550  Int_t PamelaDBOperations::SetUpperLimits
550          ph = eh->GetPscuHeader();          ph = eh->GetPscuHeader();
551          t_pktlast = PKT(ph->GetCounter());          t_pktlast = PKT(ph->GetCounter());
552          t_obtlast = OBT(ph->GetOrbitalTime());          t_obtlast = OBT(ph->GetOrbitalTime());
553          if ( t_pktlast < spktt && t_obtlast < sobtt ){ // jump          if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
554            upperpkt = pktt;            upperpkt = pktt;
555            upperobt = obtt;            upperobt = obtt;
556            upperentry = evbeft-1;            upperentry = evbeft-1;
# Line 553  Int_t PamelaDBOperations::SetUpperLimits Line 624  Int_t PamelaDBOperations::SetUpperLimits
624    return(0);    return(0);
625  }  }
626    
627    /**
628     *
629     * Trick to have unique RUN ID numbers even when runs are deleted and mysql deamon restarted.
630     * Entries in the _RUNID_GEN table are never deleted.
631     *
632     **/
633    UInt_t PamelaDBOperations::AssignRunID(){
634      //
635      TSQLResult *result = 0;
636      TSQLRow *row = 0;
637      UInt_t runid = 0;
638      //
639      stringstream   oss;
640      //  
641      oss.str("");
642      oss << "INSERT INTO _RUNID_GEN VALUES (NULL);";
643      result = conn->Query(oss.str().c_str());
644      if ( !result ) throw -10;
645      oss.str("");
646      oss << "SELECT ID FROM _RUNID_GEN ORDER BY ID DESC LIMIT 1;";
647      result = conn->Query(oss.str().c_str());
648      if ( !result ) throw -10;
649      //
650      row = result->Next();
651      //
652      if ( !row ) throw -28;
653      //
654      runid = (UInt_t)atoll(row->GetField(0));
655      //
656      return(runid);
657    };
658    
659  //  //
660  // GETTERS  // GETTERS
661  //  //
# Line 628  const PacketType* PamelaDBOperations::Ge Line 731  const PacketType* PamelaDBOperations::Ge
731  // PRIVATE FUNCTIONS  // PRIVATE FUNCTIONS
732  //  //
733    
734  /**  // /**
735   * Open the ROOT filename for reading  // * Open the ROOT filename for reading
736   */  // */
737  void PamelaDBOperations::OpenFile(){  // void PamelaDBOperations::OpenFile(){
738    file = TFile::Open(this->GetRootName().Data());  //   file = TFile::Open(this->GetRootName().Data());
739    //   //
740    
741    void PamelaDBOperations::CheckFile(){  
742      if ( !file ) throw -12;
743  };  };
744    
745    
746  /**  /**
747   * Check if LEVEL0 file and DB connection have really be opened   * Check if LEVEL0 file and DB connection have really be opened
748   */   */
749  void PamelaDBOperations::CheckFile(){    void PamelaDBOperations::CheckConnection(){  
   //  
   if ( !file ) throw -12;  
750    //    //
751    // check connection    // check connection
752    //    //
753    if( !conn ) throw -1;        if( !conn ) throw -1;
754    bool connect = conn->IsConnected();    bool connect = conn->IsConnected();
755    if( !connect ) throw -1;        if( !connect ) throw -1;
756      if ( !dworbit ) throw -27;
757  };  };
758    
759  /**  /**
# Line 827  Int_t PamelaDBOperations::insertPamelaGL Line 933  Int_t PamelaDBOperations::insertPamelaGL
933    if ( this->GetID_RAW() == 0 )  throw -11;    if ( this->GetID_RAW() == 0 )  throw -11;
934    //    //
935    oss.str("");    oss.str("");
936    oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ID_RAW<= "    oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
937        << id << " AND TO_ID_RAW >= "        << this->GetRawFile().Data() << "';";
       << id << ";";  
938    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
939    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
940    if ( !result ) throw -10;    if ( !result ) throw -10;
941    row = result->Next();    row = result->Next();
942    if ( !row ) throw -10;    //
943      if ( !row ){
944        oss.str("");
945        oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
946            << dworbit << " order by FROM_ORBIT desc limit 1;";
947        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
948        result = conn->Query(oss.str().c_str());
949        if ( !result ) throw -10;
950        row = result->Next();
951        if ( !row ) throw -10;
952      };
953    //    //
954    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();
955    /*    /*
# Line 908  Int_t PamelaDBOperations::insertPamelaGL Line 1023  Int_t PamelaDBOperations::insertPamelaGL
1023          //          //
1024          TYPE = 55;//224;          TYPE = 55;//224;
1025          //          //
1026            if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
1027            //
1028          if ( TSYNC && OBT ){          if ( TSYNC && OBT ){
1029            existsts = true;            existsts = true;
1030            goto eout;            goto eout;
# Line 930  Int_t PamelaDBOperations::insertPamelaGL Line 1047  Int_t PamelaDBOperations::insertPamelaGL
1047      //      //
1048      rt->SetBranchAddress("RunTrailer", &runt);      rt->SetBranchAddress("RunTrailer", &runt);
1049      //      //
1050      if ( rhev > 0 ){      Int_t nrhev = rh->GetEntries();
1051        rh->GetEntry(0);      Int_t nrtev = rt->GetEntries();
1052        //      if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev);
1053        TSYNC = runh->LAST_TIME_SYNC_INFO;      //
1054        OBT = runh->OBT_TIME_SYNC * 1000;      if ( nrhev > 0 ){
1055        //        for (Int_t i=0; i<nrhev; i++){
1056        TYPE = 20;          //
1057        //          rh->GetEntry(i);
1058        if ( TSYNC && OBT ){          //
1059          existsts = true;          TSYNC = runh->LAST_TIME_SYNC_INFO;
1060          goto eout;          OBT = runh->OBT_TIME_SYNC * 1000;
1061            //
1062            TYPE = 20;
1063            //
1064            if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
1065            //
1066            if ( TSYNC && OBT ){
1067              existsts = true;
1068              goto eout;
1069            };
1070        };        };
1071        //        //
1072      };      };
1073      if ( rtev > 0 ){      if ( nrtev > 0 ){
1074        //        //
1075        if ( IsDebug() ) printf(" No runheader \n");        if ( IsDebug() ) printf(" No runheader \n");
1076        signal = 6;        signal = 6;
1077        //        //
1078        rt->GetEntry(0);        for (Int_t i=0; i<nrtev; i++){
1079        //          //
1080        TSYNC = runt->LAST_TYME_SYNC_INFO;          rt->GetEntry(i);
1081        OBT = runt->OBT_TYME_SYNC * 1000;          //
1082        //          TSYNC = runt->LAST_TYME_SYNC_INFO;
1083        TYPE = 21;          OBT = runt->OBT_TYME_SYNC * 1000;
1084        //          //
1085        if ( TSYNC && OBT ){          TYPE = 21;
1086          existsts = true;          //
1087          goto eout;          if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
1088            //
1089            if ( TSYNC && OBT ){
1090              existsts = true;
1091              goto eout;
1092            };
1093        };        };
1094        //        //
1095      } else {      } else {
# Line 1498  void PamelaDBOperations::HandleRun(){   Line 1629  void PamelaDBOperations::HandleRun(){  
1629      //      //
1630      this->FillClass();      this->FillClass();
1631      //      //
1632      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);      if ( !IsRunAlreadyInserted() ){
1633          glrun->SetID(this->AssignRunID());
1634          glrun->SetID_RUN_FRAG(0);
1635          glrun->Fill_GL_RUN(conn);
1636        };
1637    } else {    } else {
1638      //      //
1639      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 1670  void PamelaDBOperations::HandleRunFragme Line 1805  void PamelaDBOperations::HandleRunFragme
1805        };        };
1806        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);
1807        //        //
       glrun1->SetID(0);  
1808        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
1809        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
1810        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
# Line 1698  void PamelaDBOperations::HandleRunFragme Line 1832  void PamelaDBOperations::HandleRunFragme
1832        //        //
1833        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
1834          //          //
1835            glrun->SetID(this->AssignRunID());
1836            glrun->SetID_RUN_FRAG(glrun1->GetID());
1837          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
1838          //          //
1839          // get id number          // get id number
1840          //          //
1841          oss.str("");  //      oss.str("");
1842          oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "  //      oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "
1843              << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "  //          << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "
1844              << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";  //          << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";
1845          //  //      //
1846          if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());  //      if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());
1847          //  //      //
1848          result =  conn->Query(oss.str().c_str());  //      result =  conn->Query(oss.str().c_str());
1849          if ( !result ) throw -4;  //      if ( !result ) throw -4;
1850          row = result->Next();  //      row = result->Next();
1851          //  //      //
1852          UInt_t idrun0 = 0;  //      UInt_t idrun0 = 0;
1853          if ( !row ){  //      if ( !row ){
1854            throw -10;  //        throw -10;
1855          } else {  //      } else {
1856            idrun0 = (UInt_t)atoll(row->GetField(0));  //        idrun0 = (UInt_t)atoll(row->GetField(0));
1857          };  //      };
         //  
         glrun1->SetID_RUN_FRAG(idrun0);  
1858          //          //
1859            //      glrun1->SetID_RUN_FRAG(idrun0);
1860            glrun1->SetID_RUN_FRAG(glrun->GetID());
1861          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
1862          //          //
1863          oss.str("");  //      oss.str("");
1864          oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";  //      oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";
1865          //  //      //
1866          if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());  //      if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());
1867          //  //      //
1868          result =  conn->Query(oss.str().c_str());  //      result =  conn->Query(oss.str().c_str());
1869          if ( !result ) throw -4;  //      if ( !result ) throw -4;
1870          row = result->Next();  //      row = result->Next();
1871          //  //      //
1872          UInt_t idrun1 = 0;  //      UInt_t idrun1 = 0;
1873          if ( !row ){  //      if ( !row ){
1874            throw -10;  //        throw -10;
1875          } else {  //      } else {
1876            idrun1 = (UInt_t)atoll(row->GetField(0));  //        idrun1 = (UInt_t)atoll(row->GetField(0));
1877          };  //      };
1878          //  //      //
1879          oss.str("");  //      oss.str("");
1880          oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";  //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";
1881          //  //      //
1882          result =  conn->Query(oss.str().c_str());  //      result =  conn->Query(oss.str().c_str());
1883          if ( !result ) throw -4;  //      if ( !result ) throw -4;
1884          //          //
1885        };        };
1886        //        //
# Line 1894  void PamelaDBOperations::HandleRunFragme Line 2030  void PamelaDBOperations::HandleRunFragme
2030        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
2031        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
2032        //        //
       glrun1->SetID(0);  
2033        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
2034        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
2035        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
# Line 1914  void PamelaDBOperations::HandleRunFragme Line 2049  void PamelaDBOperations::HandleRunFragme
2049        //        //
2050        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2051          //          //
2052            glrun->SetID(this->AssignRunID());
2053            //
2054            glrun->SetID_RUN_FRAG(glrun1->GetID());
2055          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
2056          //          //
2057          // get id number          // get id number
2058          //          //
2059          oss.str("");  //      oss.str("");
2060          oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "  //      oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "
2061              << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "  //          << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "
2062              << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";  //          << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";
2063          //  //      //
2064          if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());  //      if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());
2065          //  //      //
2066          result =  conn->Query(oss.str().c_str());  //      result =  conn->Query(oss.str().c_str());
2067          if ( !result ) throw -4;  //      if ( !result ) throw -4;
2068          row = result->Next();  //      row = result->Next();
2069          //  //      //
2070          UInt_t idrun0 = 0;  //      UInt_t idrun0 = 0;
2071          if ( !row ){  //      if ( !row ){
2072            throw -10;  //        throw -10;
2073          } else {  //      } else {
2074            idrun0 = (UInt_t)atoll(row->GetField(0));  //        idrun0 = (UInt_t)atoll(row->GetField(0));
2075          };  //      };
2076          //  //      //
2077          glrun1->SetID_RUN_FRAG(idrun0);  //      glrun1->SetID_RUN_FRAG(idrun0);
2078          //  //
2079            glrun1->SetID_RUN_FRAG(glrun->GetID());
2080          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2081          //          //
2082          oss.str("");  //      oss.str("");
2083          oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";  //      oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";
2084          //  //      //
2085          if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());  //      if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());
2086          //  //      //
2087          result =  conn->Query(oss.str().c_str());  //      result =  conn->Query(oss.str().c_str());
2088          if ( !result ) throw -4;  //      if ( !result ) throw -4;
2089          row = result->Next();  //      row = result->Next();
2090          //  //      //
2091          UInt_t idrun1 = 0;  //      UInt_t idrun1 = 0;
2092          if ( !row ){  //      if ( !row ){
2093            throw -10;  //        throw -10;
2094          } else {  //      } else {
2095            idrun1 = (UInt_t)atoll(row->GetField(0));  //        idrun1 = (UInt_t)atoll(row->GetField(0));
2096          };  //      };
2097          //  //      //
2098          oss.str("");  //      oss.str("");
2099          oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";  //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";
2100          //  //      //
2101          result =  conn->Query(oss.str().c_str());  //      result =  conn->Query(oss.str().c_str());
2102          if ( !result ) throw -4;  //      if ( !result ) throw -4;
2103          //          //
2104        };        };
2105        //        //
# Line 2044  void PamelaDBOperations::HandleRunFragme Line 2183  void PamelaDBOperations::HandleRunFragme
2183          if ( IsDebug() ) printf(" The run is new \n");          if ( IsDebug() ) printf(" The run is new \n");
2184          if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");          if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
2185          //          //
2186            glrun->SetID(this->AssignRunID());
2187            glrun->SetID_RUN_FRAG(0);
2188          glrun->Fill_GL_RUN_FRAGMENTS(conn);          glrun->Fill_GL_RUN_FRAGMENTS(conn);
2189          //          //
2190        } else {        } else {
# Line 2076  void PamelaDBOperations::HandleMissingHo Line 2217  void PamelaDBOperations::HandleMissingHo
2217      //      //
2218      this->FillClass(mishead,mistrail,firstev,lastev);      this->FillClass(mishead,mistrail,firstev,lastev);
2219      //      //
2220      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2221          glrun->SetID(this->AssignRunID());
2222          glrun->SetID_RUN_FRAG(0);
2223          glrun->Fill_GL_RUN(conn);    
2224        };
2225      //      //
2226    };    };
2227    //    //
# Line 2243  Bool_t PamelaDBOperations::IsRunConsiste Line 2388  Bool_t PamelaDBOperations::IsRunConsiste
2388            //                  //      
2389            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2390            //            //
2391            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2392                glrun->SetID(this->AssignRunID());
2393                glrun->SetID_RUN_FRAG(0);
2394                glrun->Fill_GL_RUN(conn);      
2395              };
2396            //            //
2397            firstevno = lastentry + 1;            firstevno = lastentry + 1;
2398            //            //
# Line 2319  void PamelaDBOperations::HandleSuspiciou Line 2468  void PamelaDBOperations::HandleSuspiciou
2468      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2469      //      //
2470      this->FillClass();      this->FillClass();
2471      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2472          glrun->SetID(this->AssignRunID());
2473          glrun->SetID_RUN_FRAG(0);
2474          glrun->Fill_GL_RUN(conn);    
2475        };
2476      //      //
2477    } else {    } else {
2478      //      //
# Line 2347  void PamelaDBOperations::HandleSuspiciou Line 2500  void PamelaDBOperations::HandleSuspiciou
2500        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");
2501        //        //
2502        this->FillClass();        this->FillClass();
2503        if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                if ( !IsRunAlreadyInserted() ){
2504            glrun->SetID(this->AssignRunID());
2505            glrun->SetID_RUN_FRAG(0);
2506            glrun->Fill_GL_RUN(conn);        
2507          };
2508        //        //
2509      } else {      } else {
2510        //        //
# Line 2445  void PamelaDBOperations::HandleSuspiciou Line 2602  void PamelaDBOperations::HandleSuspiciou
2602            //                  //      
2603            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2604            //            //
2605            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2606                glrun->SetID(this->AssignRunID());
2607                glrun->SetID_RUN_FRAG(0);
2608                glrun->Fill_GL_RUN(conn);      
2609              };
2610            //            //
2611            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...
2612              //              //
# Line 2480  void PamelaDBOperations::HandleSuspiciou Line 2641  void PamelaDBOperations::HandleSuspiciou
2641              //                  //    
2642              this->SetCommonGLRUN(firstTime,lastTime);              this->SetCommonGLRUN(firstTime,lastTime);
2643              //              //
2644              if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                    if ( !IsRunAlreadyInserted() ){
2645                  glrun->SetID(this->AssignRunID());
2646                  glrun->SetID_RUN_FRAG(0);
2647                  glrun->Fill_GL_RUN(conn);      
2648                };
2649            };            };
2650            //            //
2651            firstevno = lastentry + 1;            firstevno = lastentry + 1;
# Line 3168  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3333  Int_t PamelaDBOperations::CleanGL_RUN_FR
3333    TSQLRow    *row2   = 0;    TSQLRow    *row2   = 0;
3334    //    //
3335    UInt_t moved = 0;    UInt_t moved = 0;
3336    UInt_t timelim = 0;  //  UInt_t timelim = 0;
3337    TDatime *time = new TDatime();  //  TDatime *time = new TDatime();
3338    //    //
3339    stringstream oss;    stringstream oss;
3340    oss.str("");    oss.str("");
3341    //    //
3342    //    //
   //  
   if ( olderthan < 0 ){  
     if ( IsDebug() ) printf(" Do not clean GL_RUN_FRAGMENTS table \n");  
     return(1);  
   };  
   //  
   // timelim = now - olderthan  
   //  
   time->Set();  
   timelim =  (UInt_t)time->Convert() - olderthan;  
   time->Set(timelim,false);  
   //  
3343    // check if there are entries older than "olderthan" seconds from now    // check if there are entries older than "olderthan" seconds from now
3344    //    //
3345    oss.str("");    oss.str("");
3346    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3347        << " INSERT_TIME <= '" << time->AsSQLString() << "';";        << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3348    //    //
3349    if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",time->AsSQLString(),oss.str().c_str());    if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
3350    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
3351    //    //
3352    if ( result ){    if ( result ){
3353      //      //
3354      row = result->Next();      row = result->Next();
3355      //      //
3356      if ( row ){      while ( row ){
3357        //        //
3358        oss.str("");        oss.str("");
3359        oss << " ID= "<< row->GetField(0);        oss << " ID= "<< row->GetField(0);
# Line 3235  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3388  Int_t PamelaDBOperations::CleanGL_RUN_FR
3388          if ( IsDebug() ) printf(" The run is new \n");          if ( IsDebug() ) printf(" The run is new \n");
3389          if ( IsDebug() ) printf(" -> fill the DB \n");                if ( IsDebug() ) printf(" -> fill the DB \n");      
3390          //          //
3391          glrun->SetID(0);          //      glrun->SetID(this->AssignRunID()); we use the old run number!
3392            glrun->SetID_RUN_FRAG(glrun->GetID());
3393          glrun->Fill_GL_RUN(conn);            glrun->Fill_GL_RUN(conn);  
3394          //          //
3395          oss.str("");  //      oss.str("");
3396          oss << " SELECT ID FROM GL_RUN WHERE "  //      oss << " SELECT ID FROM GL_RUN WHERE "
3397              << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "  //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3398              << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "  //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3399              << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "  //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3400              << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "  //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3401              << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";  //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3402          //  //      //
3403          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());
3404          result2 = conn->Query(oss.str().c_str());  //      result2 = conn->Query(oss.str().c_str());
3405          //  //      //
3406          if ( !result2 ) throw -4;  //      if ( !result2 ) throw -4;
3407          //  //      //
3408          row2 = result2->Next();  //      row2 = result2->Next();
3409          //  //      //
3410          if ( !row2 ) throw -25;  //      if ( !row2 ) throw -25;
3411          //  //      //
3412          oss.str("");  //      oss.str("");
3413          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);
3414          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());
3415          result2 = conn->Query(oss.str().c_str());  //      result2 = conn->Query(oss.str().c_str());
3416          //  //      //
3417          if ( !result2 ) throw -4;  //      if ( !result2 ) throw -4;
3418          //          //
3419          moved++;          moved++;
3420          //          //
# Line 3277  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3431  Int_t PamelaDBOperations::CleanGL_RUN_FR
3431        //        //
3432        if ( !result2 ) throw -4;        if ( !result2 ) throw -4;
3433        //        //
3434          row = result->Next();
3435      };      };
3436    };    };
3437    if ( IsDebug() ) printf(" Moved %u runs\n",moved);    if ( IsDebug() ) printf(" Moved %u runs\n",moved);
# Line 3291  Int_t PamelaDBOperations::ValidateRuns() Line 3446  Int_t PamelaDBOperations::ValidateRuns()
3446    TSQLResult *result = 0;    TSQLResult *result = 0;
3447    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
3448    //    //
   UInt_t timelim = 0;  
   TDatime *time = new TDatime();  
   //  
3449    stringstream oss;    stringstream oss;
3450    oss.str("");    oss.str("");
3451    //    //
   //  
   //  
   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());  
   
3452    // =======================================================    // =======================================================
3453    // validate runs by checking missing calibrations    // validate runs by checking missing calibrations
3454    // =======================================================    // =======================================================
# Line 3321  Int_t PamelaDBOperations::ValidateRuns() Line 3458  Int_t PamelaDBOperations::ValidateRuns()
3458    // 1) get the OBT of the last run inserted after clean-time limit    // 1) get the OBT of the last run inserted after clean-time limit
3459    // --------------------------------------------------------------    // --------------------------------------------------------------
3460    oss.str("");    oss.str("");
3461    oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << time->AsSQLString()    oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3462        << "' ORDER BY INSERT_TIME DESC LIMIT 1;";            << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3463    printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3464    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
3465    if ( !result ) throw -4;    if ( !result ) throw -4;
3466    if ( !result->GetRowCount() ) {    if ( !result->GetRowCount() ) {
# Line 3332  Int_t PamelaDBOperations::ValidateRuns() Line 3469  Int_t PamelaDBOperations::ValidateRuns()
3469    }else{    }else{
3470          row = result->Next();          row = result->Next();
3471          t_start = (UInt_t)atoll(row->GetField(4));          t_start = (UInt_t)atoll(row->GetField(4));
         printf("t_start %i\n",t_start);  
3472    };      };  
3473    // --------------------------------------------------------------    // --------------------------------------------------------------
3474    // 2) get the OBT of the last validated run    // 2) get the OBT of the last validated run
# Line 3340  Int_t PamelaDBOperations::ValidateRuns() Line 3476  Int_t PamelaDBOperations::ValidateRuns()
3476    oss.str("");    oss.str("");
3477    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
3478        <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";        <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3479    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());
3480    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
3481    if ( !result ) throw -4;    if ( !result ) throw -4;
3482    if ( result->GetRowCount() ){    if ( result->GetRowCount() ){
3483            row = result->Next();            row = result->Next();
3484            t_stop = (UInt_t)atoll(row->GetField(4));            t_stop = (UInt_t)atoll(row->GetField(4));
3485    };    };
3486    printf("t_stop %i\n",t_stop);    if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);
3487    // --------------------------------------------------------------    // --------------------------------------------------------------
3488    // now retrieves runs to be validated    // now retrieves runs to be validated
3489    // --------------------------------------------------------------    // --------------------------------------------------------------
# Line 3356  Int_t PamelaDBOperations::ValidateRuns() Line 3492  Int_t PamelaDBOperations::ValidateRuns()
3492    oss << " AND RUNHEADER_TIME >="<< t_stop;    oss << " AND RUNHEADER_TIME >="<< t_stop;
3493    oss << " ORDER BY RUNHEADER_TIME DESC;";    oss << " ORDER BY RUNHEADER_TIME DESC;";
3494  //  if ( IsDebug() )  //  if ( IsDebug() )
3495    printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());    if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3496    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
3497    if ( !result ) throw -4;    if ( !result ) throw -4;
3498    if ( !result->GetRowCount() ) printf(" No runs to validate \n");    if ( !result->GetRowCount() ) printf(" No runs to validate \n");
3499    printf("------------------------------------------------------------------------------- \n");  //  printf("------------------------------------------------------------------------------- \n");
3500        
3501    Int_t nrow = 0;    Int_t nrow = 0;
3502    GL_RUN* this_run = new GL_RUN();    GL_RUN* this_run = new GL_RUN();
3503    GL_RUN* next_run = new GL_RUN();    GL_RUN* next_run = new GL_RUN();
3504    Int_t   nseq_max = 100;    Int_t   nseq_max = 1000;
3505  //  UInt_t* sequence = new UInt_t[100];  //  UInt_t* sequence = new UInt_t[100];
3506    vector<UInt_t> sequence(nseq_max);    vector<UInt_t> sequence(nseq_max);
3507    Int_t   nseq = 0;    Int_t   nseq = 0;
# Line 3388  Int_t PamelaDBOperations::ValidateRuns() Line 3524  Int_t PamelaDBOperations::ValidateRuns()
3524            //get run info            //get run info
3525            //------------            //------------
3526            this_run->Set_GL_RUN(row);            this_run->Set_GL_RUN(row);
3527                                
           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);  
             
3528            Bool_t this_BAD = false;            Bool_t this_BAD = false;
3529            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;
3530            else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;            else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;
3531            else{            else{
3532                    printf("Missing or corrupted header!! \n");  //                printf("Missing or corrupted header!! \n");
3533                    this_ONLINE = false;                    this_ONLINE = false;
3534                    this_BAD = true;                    this_BAD = true;
3535            };            };
# Line 3419  Int_t PamelaDBOperations::ValidateRuns() Line 3553  Int_t PamelaDBOperations::ValidateRuns()
3553                                                        
3554                            if( interval >= 60 )CHECK = true;                     //more than 60 s => there might be a calibration                            if( interval >= 60 )CHECK = true;                     //more than 60 s => there might be a calibration
3555                                                        
 /*                        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());*/  
                                                     
                             
3556                            if( !CHECK && this_run->VALIDATION ){                            if( !CHECK && this_run->VALIDATION ){
3557                                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);                                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
3558                                    nseq=0;                                    nseq=0;
# Line 3431  Int_t PamelaDBOperations::ValidateRuns() Line 3561  Int_t PamelaDBOperations::ValidateRuns()
3561                    }else if( !this_ONLINE && next_ONLINE) {              // this: DEFAULT + next:ON-LINE                    }else if( !this_ONLINE && next_ONLINE) {              // this: DEFAULT + next:ON-LINE
3562                                                        
3563                            CHECK = true;                            CHECK = true;
 //                        if( interval < 60 ) printf(" ValidateRuns ***WARNING*** : kkkkkk DT = %i \n",interval);  
3564    
3565                    }else if( !next_ONLINE ){                                             // this:ANY + next:DEFAULT                    }else if( !next_ONLINE ){                                             // this:ANY + next:DEFAULT
3566                                                        
# Line 3445  Int_t PamelaDBOperations::ValidateRuns() Line 3574  Int_t PamelaDBOperations::ValidateRuns()
3574            //----------------------------            //----------------------------
3575            if( CHECK ){            if( CHECK ){
3576                    // check if calibration exists                    // check if calibration exists
3577                    printf("DT %i ===> CHECK Missing calibration\n",interval);                    if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
3578                    Bool_t MISSING = MissingTRK_CALIB(t1,t2);                    Bool_t MISSING = MissingTRK_CALIB(t1,t2);
3579                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
3580                    nseq=0;                    nseq=0;
3581            } else printf("DT %i\n",interval);            };
3582            //--------------            //--------------
3583            //store run info            //store run info
3584            //--------------            //--------------
# Line 3462  Int_t PamelaDBOperations::ValidateRuns() Line 3591  Int_t PamelaDBOperations::ValidateRuns()
3591                    }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);
3592            };            };
3593                        
3594              if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID);
3595            nrow++;            nrow++;
3596                        
3597    };    };
# Line 3490  Bool_t PamelaDBOperations::MissingTRK_CA Line 3620  Bool_t PamelaDBOperations::MissingTRK_CA
3620          // which should be equal to the time between ascending-nodes.          // which should be equal to the time between ascending-nodes.
3621          //==============================================================          //==============================================================
3622          if ( t2 - trkcalib->FROM_TIME > 5700) {          if ( t2 - trkcalib->FROM_TIME > 5700) {
3623                  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);
3624          //==============================================================          //==============================================================
3625          // there might be a missing calibration, due to:          // there might be a missing calibration, due to:
3626          // - MM full          // - MM full
# Line 3511  Bool_t PamelaDBOperations::MissingTRK_CA Line 3641  Bool_t PamelaDBOperations::MissingTRK_CA
3641          // it is enough to say that there are no missing calibrations          // it is enough to say that there are no missing calibrations
3642          //==============================================================          //==============================================================
3643          // the long time interval bewteen runs might be due to download          // the long time interval bewteen runs might be due to download
3644          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);
3645          return(false);          return(false);
3646                    
3647  };  };
# Line 3524  Int_t PamelaDBOperations::assignVALIDATI Line 3654  Int_t PamelaDBOperations::assignVALIDATI
3654          TSQLResult *result = 0;          TSQLResult *result = 0;
3655          stringstream oss;          stringstream oss;
3656          oss.str("");          oss.str("");
3657          oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << "';";          oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
3658          //          //
3659  //      if ( IsDebug() )  //      if ( IsDebug() )
3660          printf(" Set VALIDATION = %i for run %i \n",validation,idrun);  //      printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
3661  //      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());
3662  //      result = conn->Query(oss.str().c_str());          result = conn->Query(oss.str().c_str());
3663  //      if ( !result ) throw -4;          if ( !result ) throw -4;
3664          return(0);          return(0);
3665  }  }
3666    
3667    
3668    
3669    // Insert TLEs from file tlefilename in the table GL_TLE in the db
3670    // opened by conn, sorting them by date from older to newer, if each
3671    // TLE has not been alread inserted.
3672    Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
3673    {
3674      fstream tlefile(tlefilename, ios::in);
3675    
3676      if ( !tlefile ) throw -7;
3677    
3678      vector<cTle*> ctles;
3679      vector<cTle*>::iterator iter;
3680      int present = 0;
3681    
3682      // Get three lines from tlefile, create a cTle object and put it
3683      // into ctles
3684      while(1) {
3685        cTle *tlef;
3686        string str1, str2, str3;
3687    
3688        getline(tlefile, str1);
3689        if(tlefile.eof()) break;
3690    
3691        getline(tlefile, str2);
3692        if(tlefile.eof()) break;
3693    
3694        getline(tlefile, str3);
3695        if(tlefile.eof()) break;
3696    
3697        // We now have three good lines for a cTle.
3698        tlef = new cTle(str1, str2, str3);
3699        ctles.push_back(tlef);
3700      }
3701    
3702      tlefile.close();
3703    
3704      // Sort by date
3705      sort(ctles.begin(), ctles.end(), compTLE);
3706    
3707      // Now we insert each TLE into the db
3708      for(iter = ctles.begin(); iter != ctles.end(); iter++) {
3709        cTle *tle = *iter;
3710    
3711        // Do nothing if it's already present in the db.  Just increase
3712        // the counter present.
3713        if (! isTlePresent(tle))
3714          {
3715            int status = insertTle(tle);
3716    
3717            // Insert query failed.  Return 1.
3718            if(status == EXIT_FAILURE) {
3719              
3720              if( IsDebug() ) {
3721                cerr << "Error: inserting TLE:" << endl
3722                     << tle->getName() << endl
3723                     << tle->getLine1() << endl
3724                     << tle->getLine2() << endl;
3725              }
3726    
3727              throw -4;
3728              return 1;
3729            }
3730    
3731          }
3732        else
3733          present++;
3734    
3735      }
3736    
3737      int inserted = ctles.size() - present;  // Number of inserted TLE.
3738      if ( IsDebug() )
3739        cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
3740             << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
3741    
3742      ctles.clear();
3743    
3744    
3745      // Return 2 if no new TLE has been inserted.  0 otherwise.
3746      if(! inserted ) return 2;
3747      return 0;
3748    }
3749    
3750    
3751    // Insert tle in the table GL_TLE using the connection conn.
3752    int PamelaDBOperations::insertTle(cTle *tle)
3753    {
3754      stringstream oss;
3755      TSQLResult *result = 0;
3756    
3757      oss.str("");
3758      oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
3759          << " VALUES ( '"
3760          << tle->getName() << "', '"
3761          << tle->getLine1() << "', '"
3762          << tle->getLine2() << "', '"
3763          << getTleDatetime(tle) << "')";
3764    
3765      //  cout << oss.str().c_str() << endl;
3766      result = conn->Query(oss.str().c_str());
3767      if (result == NULL)
3768        return EXIT_FAILURE;
3769    
3770      return EXIT_SUCCESS;
3771    }
3772    
3773    
3774    // Return whether tle is already in the db connected by conn.
3775    bool PamelaDBOperations::isTlePresent(cTle *tle)
3776    {
3777      stringstream oss;
3778      TSQLResult *result = 0;
3779    
3780      oss.str("");
3781      oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
3782          << getTleDatetime(tle) << "'";
3783    
3784      result = conn->Query(oss.str().c_str());
3785      if (result == NULL) throw -4;
3786    
3787      if (result->GetRowCount())
3788        return true;
3789      else
3790        return false;
3791    }
3792    
3793    
3794    // Return whether the first TLE is dated early than the second
3795    bool compTLE (cTle *tle1, cTle *tle2)
3796    {
3797      return getTleJulian(tle1) < getTleJulian(tle2);
3798    }
3799    
3800    
3801    // Return the date of the tle using the format (year-2000)*1e3 +
3802    // julian day.  e.g. 6365 is the 31th Dec 2006.
3803    // It does *not* return a cJulian date.
3804    float getTleJulian(cTle *tle) {
3805      return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
3806    }
3807    
3808    
3809    // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
3810    // format.
3811    string getTleDatetime(cTle *tle)
3812    {
3813      int year, mon, day, hh, mm, ss;
3814      double dom; // day of month (is double!)
3815      stringstream date; // date in datetime format
3816    
3817      // create a cJulian from the date in tle
3818      cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
3819    
3820      // get year, month, day of month
3821      jdate.getComponent(&year, &mon, &dom);
3822    
3823      // build a datetime YYYY-MM-DD hh:mm:ss
3824      date.str("");
3825      day = (int) floor(dom);
3826      hh = (int) floor( (dom - day) * 24);
3827      mm = (int) floor( ((dom - day) * 24 - hh) * 60);
3828      ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
3829      //  ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
3830    
3831      date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
3832    
3833      return date.str();
3834    }

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.14

  ViewVC Help
Powered by ViewVC 1.1.23