/[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.10 by mocchiut, Tue Sep 12 08:43:50 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      //
75    //    //
76    INSERT_RAW =!filerawname.IsNull();    INSERT_RAW =!filerawname.IsNull();
77    if(INSERT_RAW)SetRawName(filerawname);    if(INSERT_RAW)SetRawName(filerawname);
# Line 69  PamelaDBOperations::PamelaDBOperations(T Line 79  PamelaDBOperations::PamelaDBOperations(T
79    INSERT_ROOT = !filerootname.IsNull();    INSERT_ROOT = !filerootname.IsNull();
80    if( INSERT_ROOT ){    if( INSERT_ROOT ){
81      this->SetRootName(filerootname);      this->SetRootName(filerootname);
82        this->SetOrbitNo();
83      file = TFile::Open(this->GetRootName().Data());      file = TFile::Open(this->GetRootName().Data());
84      } else {
85        this->SetRootName("");
86    };    };
87    //    //
88    this->SetID_RAW(0);    this->SetID_RAW(0);
# Line 114  void PamelaDBOperations::CheckValidate(L Line 127  void PamelaDBOperations::CheckValidate(L
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 142  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 182  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 190  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   */   */
# Line 246  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 292  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 574  Int_t PamelaDBOperations::SetUpperLimits Line 634  Int_t PamelaDBOperations::SetUpperLimits
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 671  void PamelaDBOperations::CheckConnection Line 763  void PamelaDBOperations::CheckConnection
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 850  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 931  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 953  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 1448  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 1465  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 1482  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 1521  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 1544  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 1570  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 1578  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 1588  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 1662  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 1693  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 1702  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 1721  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 1790  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 1805  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 1878  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 1917  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 1937  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());
         //  
         // get id number  
         //  
         oss.str("");  
         oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "  
             << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "  
             << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";  
         //  
         if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());  
         //  
         result =  conn->Query(oss.str().c_str());  
         if ( !result ) throw -4;  
         row = result->Next();  
2114          //          //
2115          UInt_t idrun0 = 0;          glrun->SetID_RUN_FRAG(glrun1->GetID());
2116          if ( !row ){          glrun->Fill_GL_RUN(conn);
           throw -10;  
         } else {  
           idrun0 = (UInt_t)atoll(row->GetField(0));  
         };  
2117          //          //
2118          glrun1->SetID_RUN_FRAG(idrun0);          // set id number
2119          //          //
2120            glrun1->SetID_RUN_FRAG(glrun->GetID());
2121          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2122          //          //
         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;  
         //  
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 2035  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 2099  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 2266  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 2342  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 2370  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        //        //
2491        if ( IsDebug() ) printf(" There are no-physics packets inside the run, try to separate runs \n");        if ( IsDebug() ) printf(" There are no-physics packets inside the run, try to separate runs \n");
# Line 2468  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 2503  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 3184  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 3191  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    // check if there are entries older than "olderthan" seconds from now      //
3329    //      // check if there are entries older than "olderthan" seconds from now
3330    oss.str("");      //
3331    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"      oss.str("");
3332        << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3333    //          << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3334    if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());      //
3335    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
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    //    //
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 3246  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 3281  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 3298  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 calibtime = 50;
3468      //
3469    stringstream oss;    stringstream oss;
3470    oss.str("");    oss.str("");
3471    //    //
# Line 3310  Int_t PamelaDBOperations::ValidateRuns() Line 3474  Int_t PamelaDBOperations::ValidateRuns()
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 <= '" << clean_time->AsSQLString()      oss.str("");
3482            << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3483    if ( IsDebug() ) 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    };          t_start = (UInt_t)atoll(row->GetField(4));
3493    // --------------------------------------------------------------      };  
3494    // 2) get the OBT of the last validated run      // --------------------------------------------------------------
3495    // --------------------------------------------------------------      // 2) get the OBT of the last validated run
3496    oss.str("");      // --------------------------------------------------------------
3497    oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start      oss.str("");
3498        <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3499    if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3500    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3501    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3502    if ( result->GetRowCount() ){      if ( !result ) throw -4;
3503            row = result->Next();      if ( result->GetRowCount() ){
3504            t_stop = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3505          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    if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);    //
   // --------------------------------------------------------------  
   // now retrieves runs to be validated  
   // --------------------------------------------------------------  
   oss.str("");  
   oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;  
   oss << " AND RUNHEADER_TIME >="<< t_stop;  
   oss << " ORDER BY RUNHEADER_TIME DESC;";  
 //  if ( IsDebug() )  
   if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());  
   result = conn->Query(oss.str().c_str());  
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();
# Line 3367  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 3407  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
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 3522  Int_t PamelaDBOperations::assignVALIDATI Line 3750  Int_t PamelaDBOperations::assignVALIDATI
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.10  
changed lines
  Added in v.1.16

  ViewVC Help
Powered by ViewVC 1.1.23