/[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.15 by mocchiut, Tue Oct 24 14:24:23 2006 UTC revision 1.19 by mocchiut, Thu Nov 9 16:47:47 2006 UTC
# Line 14  Line 14 
14  #include <TSQLRow.h>  #include <TSQLRow.h>
15  #include <TTree.h>  #include <TTree.h>
16  #include <TGraph.h>  #include <TGraph.h>
17  #include <TDatime.h>  #include <TTimeStamp.h>
18  #include <TF1.h>  #include <TF1.h>
19  //  //
20  #include <EventHeader.h>  #include <EventHeader.h>
# Line 81  PamelaDBOperations::PamelaDBOperations(T Line 81  PamelaDBOperations::PamelaDBOperations(T
81      this->SetRootName(filerootname);      this->SetRootName(filerootname);
82      this->SetOrbitNo();      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 110  void PamelaDBOperations::Close(){ Line 112  void PamelaDBOperations::Close(){
112  //  //
113  void PamelaDBOperations::CheckValidate(Long64_t olderthan){  void PamelaDBOperations::CheckValidate(Long64_t olderthan){
114    clean_time = new TDatime();    clean_time = new TDatime();
115      //
116    if(olderthan >= 0){    if(olderthan >= 0){
117      VALIDATE = true;      VALIDATE = true;
118      UInt_t timelim = 0;      UInt_t timelim = 0;
119      timelim =  (UInt_t)clean_time->Convert() - olderthan;      timelim =  (UInt_t)clean_time->Convert(true) - olderthan;
120      clean_time->Set(timelim,false);      clean_time->Set(timelim,false);
121    };    };
122  };  };
# Line 154  void PamelaDBOperations::SetDebugFlag(Bo Line 157  void PamelaDBOperations::SetDebugFlag(Bo
157  };  };
158    
159  /**  /**
160     * Set the nofrag flag
161     *
162     */
163    void PamelaDBOperations::SetNoFrag(Bool_t nf){
164      NOFRAG = nf;
165    };
166    
167    /**
168   * Store the BOOT number of the RAW file.   * Store the BOOT number of the RAW file.
169   * @param boot    BOOT number of the RAW file   * @param boot    BOOT number of the RAW file
170   */   */
# Line 753  void PamelaDBOperations::CheckConnection Line 764  void PamelaDBOperations::CheckConnection
764    if( !conn ) throw -1;    if( !conn ) throw -1;
765    bool connect = conn->IsConnected();    bool connect = conn->IsConnected();
766    if( !connect ) throw -1;    if( !connect ) throw -1;
767    if ( !dworbit ) throw -27;    if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
768      //
769      // set DB timezone to UTC
770      //
771      stringstream oss;
772      //  
773      oss.str("");
774      oss << "SET time_zone='+0:00';";
775      TSQLResult *result = 0;
776      result = conn->Query(oss.str().c_str());
777      if ( !result ) throw -10;
778      //
779  };  };
780    
781  /**  /**
# Line 933  Int_t PamelaDBOperations::insertPamelaGL Line 955  Int_t PamelaDBOperations::insertPamelaGL
955    if ( this->GetID_RAW() == 0 )  throw -11;    if ( this->GetID_RAW() == 0 )  throw -11;
956    //    //
957    oss.str("");    oss.str("");
958    oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"    oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
959        << this->GetRawFile().Data() << "';";        << this->GetRawFile().Data() << "';";
960    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
961    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
# Line 942  Int_t PamelaDBOperations::insertPamelaGL Line 964  Int_t PamelaDBOperations::insertPamelaGL
964    //    //
965    if ( !row ){    if ( !row ){
966      oss.str("");      oss.str("");
967      oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "      oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
968          << dworbit << " order by FROM_ORBIT desc limit 1;";          << dworbit << " order by FROM_ORBIT desc limit 1;";
969      if ( IsDebug() ) printf(" %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
970      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
# Line 951  Int_t PamelaDBOperations::insertPamelaGL Line 973  Int_t PamelaDBOperations::insertPamelaGL
973      if ( !row ) throw -10;      if ( !row ) throw -10;
974    };    };
975    //    //
976    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();    TTimeStamp tu = TTimeStamp((UInt_t)atoi(row->GetField(0)),(UInt_t)atoi(row->GetField(1)),(UInt_t)atoi(row->GetField(2)),(UInt_t)atoi(row->GetField(3)),(UInt_t)atoi(row->GetField(4)),(UInt_t)atoi(row->GetField(5)),0,true,0);
977      t0 = (UInt_t)tu.GetSec();
978      if ( IsDebug() ) printf(" t0 is %u ti is %s %s %s %s %s %s\n",t0,row->GetField(0),row->GetField(1),row->GetField(2),row->GetField(3),row->GetField(4),row->GetField(5));
979      //
980    /*    /*
981     * Verify that the TIMESYNC have been not already processed     * Verify that the TIMESYNC have been not already processed
982     */     */
# Line 966  Int_t PamelaDBOperations::insertPamelaGL Line 991  Int_t PamelaDBOperations::insertPamelaGL
991    if (result == NULL) throw -10;    if (result == NULL) throw -10;
992    row = result->Next();    row = result->Next();
993    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
994        if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
995      toffset = (UInt_t)atoll(row->GetField(2)) - (UInt_t)(this->OBT((UInt_t)atoll(row->GetField(1)))/1000) + t0;      toffset = (UInt_t)atoll(row->GetField(2)) - (UInt_t)(this->OBT((UInt_t)atoll(row->GetField(1)))/1000) + t0;
996      return(1);      return(1);
997    };    };
# Line 1160  Int_t PamelaDBOperations::insertPamelaGL Line 1186  Int_t PamelaDBOperations::insertPamelaGL
1186    conn->Query(oss.str().c_str());    conn->Query(oss.str().c_str());
1187    if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());
1188    //    //
1189      if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1190    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1191    //    //
1192    delete result;    delete result;
# Line 1659  void PamelaDBOperations::HandleRunFragme Line 1686  void PamelaDBOperations::HandleRunFragme
1686    UInt_t rhfirstev = firstev;    UInt_t rhfirstev = firstev;
1687    UInt_t rtlastev = lastev;    UInt_t rtlastev = lastev;
1688    Bool_t found = false;    Bool_t found = false;
1689      Bool_t foundinrun = false;
1690    //    //
1691    TSQLResult *result = 0;    TSQLResult *result = 0;
1692    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 1685  void PamelaDBOperations::HandleRunFragme Line 1713  void PamelaDBOperations::HandleRunFragme
1713    //    //
1714    if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");    if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
1715    //    //
1716      // First of all insert the run in the fragment table...
1717      //
1718      oss.str("");
1719      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1720          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1721          << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1722          << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1723          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1724          << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1725          << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1726          << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1727          << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1728          << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1729          << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1730          << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1731          << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1732          << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1733      //
1734      if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1735      result = conn->Query(oss.str().c_str());
1736      //
1737      if ( !result ) throw -4;
1738      //
1739      row = result->Next();
1740      //
1741      if ( !row ){
1742        //
1743        // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
1744        //
1745        if ( IsDebug() ) printf(" The run is new \n");
1746        if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
1747        //
1748        glrun->SetID(this->AssignRunID());
1749        glrun->SetID_RUN_FRAG(0);
1750        glrun->Fill_GL_RUN_FRAGMENTS(conn);
1751        //
1752      } else {
1753        if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
1754      };
1755      //
1756      //
1757    // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?    // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
1758    //        //    
1759    if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is    if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
# Line 1693  void PamelaDBOperations::HandleRunFragme Line 1762  void PamelaDBOperations::HandleRunFragme
1762      oss.str("");      oss.str("");
1763      oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "      oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1764          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1765          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME()          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1766            << " ID != " << glrun->ID
1767          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1768      //      //
1769      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
# Line 1703  void PamelaDBOperations::HandleRunFragme Line 1773  void PamelaDBOperations::HandleRunFragme
1773      //      //
1774      row = result->Next();      row = result->Next();
1775      //      //
1776        if ( !row && NoFrag() ){
1777          //
1778          oss.str("");
1779          oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
1780              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1781              << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1782              << " ID != " << glrun->ID
1783              << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1784          //
1785          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1786          result = conn->Query(oss.str().c_str());
1787          //
1788          if ( !result ) throw -4;
1789          //
1790          foundinrun = true;
1791          //
1792          row = result->Next();
1793          //
1794        };
1795        //
1796      if ( !row ){      if ( !row ){
1797        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1798        found = false;        found = false;
# Line 1777  void PamelaDBOperations::HandleRunFragme Line 1867  void PamelaDBOperations::HandleRunFragme
1867        //        //
1868        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
1869        //        //
1870          if ( foundinrun ){
1871            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
1872            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1873          };
1874          //
1875        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
1876        //        //
1877        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
1878        //        //
1879        oss.str("");        oss.str("");
1880        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1816  void PamelaDBOperations::HandleRunFragme Line 1911  void PamelaDBOperations::HandleRunFragme
1911        //        //
1912        glrun->SetEV_FROM(firstev);        glrun->SetEV_FROM(firstev);
1913        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
1914          //
1915        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
1916        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
1917        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
# Line 1835  void PamelaDBOperations::HandleRunFragme Line 1931  void PamelaDBOperations::HandleRunFragme
1931        //        //
1932        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
1933          //          //
1934          glrun->SetID(this->AssignRunID());          //      glrun->SetID(this->AssignRunID());
1935          glrun->SetID_RUN_FRAG(glrun1->GetID());          glrun->SetID_RUN_FRAG(glrun1->GetID());
1936          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
1937          //          //
1938          // get id number          // set id number
1939          //          //
 //      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);  
1940          glrun1->SetID_RUN_FRAG(glrun->GetID());          glrun1->SetID_RUN_FRAG(glrun->GetID());
1941          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
1942          //          //
 //      oss.str("");  
 //      oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";  
 //      //  
 //      if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
 //      row = result->Next();  
 //      //  
 //      UInt_t idrun1 = 0;  
 //      if ( !row ){  
 //        throw -10;  
 //      } else {  
 //        idrun1 = (UInt_t)atoll(row->GetField(0));  
 //      };  
 //      //  
 //      oss.str("");  
 //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
         //  
1943        };        };
1944          // delete old entry in fragment table
1945        //        //
1946        delete glrun1;        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
1947          glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
1948        //        //
1949        // delete old entry in fragment table        delete glrun1;
1950        //        //
       glrun->DeleteRun(conn,idfrag,"GL_RUN_FRAGMENTS");  
 //       oss.str("");  
 //       //  
 //       oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";  
 //       //  
 //       if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str());  
 //       result =  conn->Query(oss.str().c_str());  
 //       if ( !result ) throw -4;  
1951        //        //
1952        return;        return;
1953        //        //
# Line 1907  void PamelaDBOperations::HandleRunFragme Line 1956  void PamelaDBOperations::HandleRunFragme
1956    };    };
1957    //    //
1958    if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is    if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
1959                                             // missing it no way we can found a piece in the frag table      // missing it no way we can found a piece in the frag table
1960      //      //
1961      oss.str("");      oss.str("");
1962      oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "      oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1963          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1964          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME()          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
1965            << " ID != " << glrun->ID
1966          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
1967      //      //
1968      if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
# Line 1922  void PamelaDBOperations::HandleRunFragme Line 1972  void PamelaDBOperations::HandleRunFragme
1972      //      //
1973      row = result->Next();      row = result->Next();
1974      //      //
1975        if ( !row && NoFrag() ){
1976          //
1977          oss.str("");
1978          oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
1979              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1980              << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
1981              << " ID != " << glrun->ID
1982              << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
1983          //
1984          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1985          result = conn->Query(oss.str().c_str());
1986          //
1987          if ( !result ) throw -4;
1988          //
1989          foundinrun = true;
1990          row = result->Next();
1991          //
1992        };
1993        //
1994      if ( !row ){      if ( !row ){
1995        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1996        found = false;        found = false;
# Line 1995  void PamelaDBOperations::HandleRunFragme Line 2064  void PamelaDBOperations::HandleRunFragme
2064        //        //
2065        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2066        //        //
2067          if ( foundinrun ){
2068            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2069            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2070          };
2071          //
2072        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
2073        //        //
2074        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2075        //        //
2076        oss.str("");        oss.str("");
2077        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 2053  void PamelaDBOperations::HandleRunFragme Line 2127  void PamelaDBOperations::HandleRunFragme
2127        //        //
2128        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2129          //          //
2130          glrun->SetID(this->AssignRunID());          //      glrun->SetID(this->AssignRunID());
2131          //          //
2132          glrun->SetID_RUN_FRAG(glrun1->GetID());          glrun->SetID_RUN_FRAG(glrun1->GetID());
2133          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
2134          //          //
2135          // get id number          // set id number
2136          //          //
 //      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);  
 //  
2137          glrun1->SetID_RUN_FRAG(glrun->GetID());          glrun1->SetID_RUN_FRAG(glrun->GetID());
2138          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2139          //          //
 //      oss.str("");  
 //      oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";  
 //      //  
 //      if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
 //      row = result->Next();  
 //      //  
 //      UInt_t idrun1 = 0;  
 //      if ( !row ){  
 //        throw -10;  
 //      } else {  
 //        idrun1 = (UInt_t)atoll(row->GetField(0));  
 //      };  
 //      //  
 //      oss.str("");  
 //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
         //  
2140        };        };
2141        //        //
2142        delete glrun1;        // delete old entries in fragment table
       //  
       // delete old entry in fragment table  
2143        //        //
2144        glrun->DeleteRun(conn,idfrag,"GL_RUN_FRAGMENTS");        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2145  //       oss.str("");        glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
 //       //  
 //       oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";  
 //       //  
 //       if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str());  
 //       result =  conn->Query(oss.str().c_str());  
 //       if ( !result ) throw -4;  
2146        //        //
2147          delete glrun1;
2148        //        //
2149        return;        return;
2150        //        //
# Line 2156  void PamelaDBOperations::HandleRunFragme Line 2181  void PamelaDBOperations::HandleRunFragme
2181      //      //
2182      row = result->Next();      row = result->Next();
2183      //      //
2184      if ( !row ){      if ( row ){
       //  
       oss.str("");  
       oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "  
           << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("  
           << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "  
           << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("  
           << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "  
           << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
           << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "  
           << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "  
           << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "  
           << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("  
           << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "  
           << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
           << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "  
           << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";  
       //  
       if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());  
       result = conn->Query(oss.str().c_str());  
       //  
       if ( !result ) throw -4;  
       //  
       row = result->Next();  
       //  
       if ( !row ){  
         //  
         // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)  
         //  
         if ( IsDebug() ) printf(" The run is new \n");  
         if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");  
         //  
         glrun->SetID(this->AssignRunID());  
         glrun->SetID_RUN_FRAG(0);  
         glrun->Fill_GL_RUN_FRAGMENTS(conn);  
         //  
       } else {  
         if ( IsDebug() ) printf(" The run is already present in the fragment table \n");  
       };  
     } else {  
2185        if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");        if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
2186        } else {
2187          if ( NoFrag() ){
2188            glrun->SetID_RUN_FRAG(glrun->GetID());
2189            glrun->Fill_GL_RUN(conn);      
2190            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2191          };    
2192      };      };
2193    };    };
2194    //    //
# Line 2757  Int_t PamelaDBOperations::insertCALO_CAL Line 2749  Int_t PamelaDBOperations::insertCALO_CAL
2749                // no calibrations in the db contain our calibration                // no calibrations in the db contain our calibration
2750                //                //
2751                if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);                if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
2752                if ( fromtime < 1150863400 ){                if ( fromtime < 1150871000 ){ //1150866904
2753                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
2754                  fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode                  fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode
2755                };                };
# Line 2902  void PamelaDBOperations::HandleTRK_CALIB Line 2894  void PamelaDBOperations::HandleTRK_CALIB
2894        // no calibrations in the db contain our calibration        // no calibrations in the db contain our calibration
2895        //        //
2896        if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");        if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
2897        if ( fromtime < 1150863400 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode        if ( fromtime < 1150871000 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode
2898        //        //
2899        oss.str("");        oss.str("");
2900        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
# Line 3258  Int_t PamelaDBOperations::insertS4_CALIB Line 3250  Int_t PamelaDBOperations::insertS4_CALIB
3250            // no calibrations in the db contain our calibration            // no calibrations in the db contain our calibration
3251            //            //
3252            if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");            if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");
3253            if ( fromtime < 1150863400 ){            if ( fromtime < 1150871000 ){  
3254              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
3255              fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode              fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode
3256            };            };
# Line 3331  Int_t PamelaDBOperations::insertS4_CALIB Line 3323  Int_t PamelaDBOperations::insertS4_CALIB
3323   * Scan the fragment table and move old fragments to the GL_RUN table   * Scan the fragment table and move old fragments to the GL_RUN table
3324   */   */
3325  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3326      return(this->CleanGL_RUN_FRAGMENTS(""));
3327    };
3328    
3329    /**
3330     * Scan the fragment table and move old fragments to the GL_RUN table
3331     */
3332    Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3333    //    //
3334    TSQLResult *result = 0;    TSQLResult *result = 0;
3335    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 3338  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3337  Int_t PamelaDBOperations::CleanGL_RUN_FR
3337    TSQLRow    *row2   = 0;    TSQLRow    *row2   = 0;
3338    //    //
3339    UInt_t moved = 0;    UInt_t moved = 0;
 //  UInt_t timelim = 0;  
 //  TDatime *time = new TDatime();  
3340    //    //
3341    stringstream oss;    stringstream oss;
3342    oss.str("");    oss.str("");
3343    //    //
3344    //    if ( !strcmp(fcleanfile.Data(),"") ){
3345    // check if there are entries older than "olderthan" seconds from now      //
3346    //      // check if there are entries older than "olderthan" seconds from now
3347    oss.str("");      //
3348    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"      oss.str("");
3349        << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3350    //          << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3351    if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());      //
3352    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());
3353        result = conn->Query(oss.str().c_str());
3354        //
3355      } else {
3356        oss.str("");
3357        oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3358        if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3359        result = conn->Query(oss.str().c_str());
3360        //    
3361        if ( result ){
3362          //
3363          row = result->Next();
3364          //
3365          if ( row ){
3366            oss.str("");
3367            oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3368                << " ID_ROOT_L0=" << row->GetField(0) << ";";
3369            //
3370            if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3371            result = conn->Query(oss.str().c_str());
3372            //    
3373          };
3374        } else {
3375          return(2);
3376        };
3377      };
3378    //    //
3379    if ( result ){    if ( result ){
3380      //      //
# Line 3448  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3470  Int_t PamelaDBOperations::CleanGL_RUN_FR
3470   * Check if runs are good, i.e. if the tracker calibration is correctly associated..   * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3471   */   */
3472  Int_t PamelaDBOperations::ValidateRuns(){  Int_t PamelaDBOperations::ValidateRuns(){
3473      return(this->ValidateRuns(""));
3474    };
3475    
3476    /**
3477     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3478     */
3479    Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3480    //    //
3481    TSQLResult *result = 0;    TSQLResult *result = 0;
3482    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
3483    //    //
3484      UInt_t calibtime = 50;
3485      //
3486    stringstream oss;    stringstream oss;
3487    oss.str("");    oss.str("");
3488    //    //
# Line 3460  Int_t PamelaDBOperations::ValidateRuns() Line 3491  Int_t PamelaDBOperations::ValidateRuns()
3491    // =======================================================    // =======================================================
3492    UInt_t t_stop  = 0;    UInt_t t_stop  = 0;
3493    UInt_t t_start = 0;    UInt_t t_start = 0;
3494    // --------------------------------------------------------------    if ( !strcmp(valfile.Data(),"") ) {
3495    // 1) get the OBT of the last run inserted after clean-time limit      // --------------------------------------------------------------
3496    // --------------------------------------------------------------      // 1) get the OBT of the last run inserted after clean-time limit
3497    oss.str("");      // --------------------------------------------------------------
3498    oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()      oss.str("");
3499            << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3500    if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());          << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3501    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3502    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3503    if ( !result->GetRowCount() ) {      if ( !result ) throw -4;
3504            printf(" No runs to validate \n");      if ( !result->GetRowCount() ) {
3505            return(1);        printf(" No runs to validate \n");
3506    }else{        return(1);
3507          row = result->Next();      }else{
3508          t_start = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3509    };          t_start = (UInt_t)atoll(row->GetField(4));
3510    // --------------------------------------------------------------      };  
3511    // 2) get the OBT of the last validated run      // --------------------------------------------------------------
3512    // --------------------------------------------------------------      // 2) get the OBT of the last validated run
3513    oss.str("");      // --------------------------------------------------------------
3514    oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start      oss.str("");
3515        <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3516    if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3517    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3518    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3519    if ( result->GetRowCount() ){      if ( !result ) throw -4;
3520            row = result->Next();      if ( result->GetRowCount() ){
3521            t_stop = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3522          t_stop = (UInt_t)atoll(row->GetField(4));
3523        };
3524        if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);
3525        // --------------------------------------------------------------
3526        // now retrieves runs to be validated
3527        // --------------------------------------------------------------
3528        oss.str("");
3529        oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;
3530        oss << " AND RUNHEADER_TIME >="<< t_stop;
3531        oss << " ORDER BY RUNHEADER_TIME DESC;";
3532        if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3533        result = conn->Query(oss.str().c_str());
3534      } else {
3535        //
3536        stringstream myquery;
3537        UInt_t myid = 0;
3538        myquery.str("");
3539        myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3540        //
3541        result = conn->Query(myquery.str().c_str());
3542        //
3543        row = result->Next();      
3544        if( !row ){
3545          if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3546            if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3547            return(2);
3548          };
3549          if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3550          return(0);
3551        };
3552        myid=(UInt_t)atoll(row->GetField(0));
3553        //
3554        myquery.str("");
3555        myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3556        //
3557        result = conn->Query(myquery.str().c_str());
3558        //
3559        row = result->Next();      
3560        if( !row->GetField(0) || !row->GetField(1)){
3561          //
3562          if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3563          //
3564          return(0);
3565          //
3566        } else {  
3567          //
3568          UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3569          UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3570          UInt_t caltime = 0;
3571          //
3572          myquery.str("");
3573          myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3574          myquery << " order by FROM_TIME asc limit 1;";
3575          //
3576          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3577          //
3578          //
3579          result = conn->Query(myquery.str().c_str());
3580          //
3581          row = result->Next();      
3582          if( !row ){
3583            caltime = runhtime;
3584          } else {
3585            caltime = (UInt_t)atoll(row->GetField(0));
3586          };
3587          //
3588          myquery.str("");
3589          myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3590          myquery << caltime << " order by RUNHEADER_TIME DESC";
3591          //
3592          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3593          //
3594          result = conn->Query(myquery.str().c_str());
3595          //
3596        };
3597    };    };
3598    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());  
3599    if ( !result ) throw -4;    if ( !result ) throw -4;
3600    if ( !result->GetRowCount() ) printf(" No runs to validate \n");    if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3601  //  printf("------------------------------------------------------------------------------- \n");    //
     
3602    Int_t nrow = 0;    Int_t nrow = 0;
3603    GL_RUN* this_run = new GL_RUN();    GL_RUN* this_run = new GL_RUN();
3604    GL_RUN* next_run = new GL_RUN();    GL_RUN* next_run = new GL_RUN();
# Line 3517  Int_t PamelaDBOperations::ValidateRuns() Line 3612  Int_t PamelaDBOperations::ValidateRuns()
3612    UInt_t t1=0,t2=0;    UInt_t t1=0,t2=0;
3613    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
3614    // - loop over runs, back in time,    // - loop over runs, back in time,
3615    // - select sequences of runs close in time (less than 60 s apart),    // - select sequences of runs close in time (less than calibtime s apart),
3616    //   which could be preceeded by a calibration    //   which could be preceeded by a calibration
3617    // - check if there might be a missing calibration    // - check if there might be a missing calibration
3618    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
# Line 3557  Int_t PamelaDBOperations::ValidateRuns() Line 3652  Int_t PamelaDBOperations::ValidateRuns()
3652                                                        
3653                            if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0;     //=> run fragments                            if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0;     //=> run fragments
3654                                                        
3655                            if( interval >= 60 )CHECK = true;                     //more than 60 s => there might be a calibration                            if( interval >= calibtime )CHECK = true;                      //more than calibtime s => there might be a calibration
3656                                                        
3657                            if( !CHECK && this_run->VALIDATION ){                            if( !CHECK && this_run->VALIDATION ){
3658                                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);                                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
# Line 3755  Int_t PamelaDBOperations::populateTLE()/ Line 3850  Int_t PamelaDBOperations::populateTLE()/
3850    
3851    
3852  // Insert tle in the table GL_TLE using the connection conn.  // Insert tle in the table GL_TLE using the connection conn.
3853  int PamelaDBOperations::insertTle(cTle *tle)  Int_t PamelaDBOperations::insertTle(cTle *tle)
3854  {  {
3855    stringstream oss;    stringstream oss;
3856    TSQLResult *result = 0;    TSQLResult *result = 0;
# Line 3838  string getTleDatetime(cTle *tle) Line 3933  string getTleDatetime(cTle *tle)
3933    
3934    return date.str();    return date.str();
3935  }  }
3936    
3937    /**
3938     * Remove a file from the DB, delete on cascade all entries related to that file
3939     * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
3940     * calibration
3941     **/
3942    Int_t PamelaDBOperations::removeFile(TString remfile){
3943      //
3944      // Determine ID_ROOT_L0 and ID_RAW
3945      //
3946      TSQLResult *pResult;
3947      TSQLRow *Row;
3948      stringstream myquery;
3949      //  
3950      myquery.str("");
3951      myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
3952      //
3953      pResult = conn->Query(myquery.str().c_str());
3954      //
3955      Row = pResult->Next();      
3956      if( !Row ){
3957        if ( strcmp(remfile.Data(),GetRootName().Data()) ){
3958          if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
3959          return(1);
3960        };
3961        if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
3962        return(0);
3963      };
3964      //
3965      this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
3966      this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
3967      //
3968      this->ValidationOFF();
3969      //
3970      this->RemoveCALIBS();
3971      //
3972      this->RemoveRUNS();
3973      //
3974      this->RemoveFILES();
3975      //
3976      this->SetID_ROOT(0);
3977      this->SetID_RAW(0);
3978      //
3979      return(0);
3980    };
3981    
3982    /**
3983     *
3984     * Set validation bit to zero for runs following the removing file till
3985     * 1) a run with TRK_CALIB_USED=140
3986     * 2) a run with VALIDATION = 0
3987     * 3) the next calibration
3988     *
3989     **/
3990    void PamelaDBOperations::ValidationOFF(){
3991      TSQLResult *pResult;
3992      TSQLRow *Row;
3993      stringstream myquery;
3994      Int_t unv = 0;
3995      //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
3996      myquery.str("");
3997      myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
3998      //
3999      pResult = conn->Query(myquery.str().c_str());
4000      //
4001      Row = pResult->Next();      
4002      if( !Row->GetField(0) ){
4003        //
4004        if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
4005        //
4006      } else {  
4007        //
4008        UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
4009        UInt_t caltime = 0;
4010        //
4011        myquery.str("");
4012        myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
4013        myquery << " order by FROM_TIME asc limit 1;";
4014        //
4015        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4016        //
4017        //
4018        delete pResult;
4019        pResult = conn->Query(myquery.str().c_str());
4020        //
4021        Row = pResult->Next();      
4022        if( !Row ){
4023          caltime = runhtime;
4024        } else {
4025          caltime = (UInt_t)atoll(Row->GetField(0));
4026        };
4027        //
4028        myquery.str("");
4029        myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4030        myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4031        //
4032        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4033        //
4034        pResult = conn->Query(myquery.str().c_str());
4035        //
4036        Row = pResult->Next();      
4037        if( !Row ){
4038          //
4039          if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4040          //
4041        } else {
4042          myquery.str("");
4043          myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4044          myquery << " RUNHEADER_TIME>=" <<runhtime;
4045          myquery << " order by RUNHEADER_TIME asc;";
4046          //
4047          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4048          //
4049          pResult = conn->Query(myquery.str().c_str());
4050          //
4051          Row = pResult->Next();  
4052          while ( Row ){
4053            //      
4054            unv++;
4055            this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4056            Row = pResult->Next();  
4057            //
4058          };
4059        };
4060      };
4061      if ( IsDebug() ) printf(" %i runs have been unvalidated \n",unv);
4062    };
4063    
4064    /**
4065     *
4066     * Rearrange GL_RUN table and remove runs
4067     *
4068     **/
4069    void PamelaDBOperations::RemoveRUNS(){
4070      TSQLResult *pResult;
4071      TSQLRow *Row;
4072      stringstream myquery;
4073      UInt_t drun = 0;
4074      GL_RUN *delrun = new GL_RUN();
4075      //
4076      myquery.str("");
4077      myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4078      //
4079      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4080      //
4081      pResult = conn->Query(myquery.str().c_str());
4082      //
4083      Row = pResult->Next();    
4084      //
4085      //
4086      if ( !Row ){
4087        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4088      } else {
4089        if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");    
4090        while ( Row ){
4091          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4092          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4093          drun++;
4094          Row = pResult->Next();    
4095        };
4096      };
4097      //
4098      //
4099      myquery.str("");
4100      myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4101      //
4102      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4103      //
4104      pResult = conn->Query(myquery.str().c_str());
4105      //
4106      Row = pResult->Next();    
4107      //
4108      if ( !Row ){
4109        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4110      } else {
4111        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");    
4112        while ( Row ){
4113          if ( IsDebug() ) printf(" restore run %i \n",(UInt_t)atoll(Row->GetField(1)));
4114          delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4115          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(1)));
4116          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4117          if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4118            if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4119            delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4120          };
4121          drun++;
4122          Row = pResult->Next();    
4123        };
4124      };
4125      //
4126      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);    
4127      //
4128      //
4129      //
4130      drun = 0;
4131      //
4132      myquery.str("");
4133      myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4134      //
4135      pResult = conn->Query(myquery.str().c_str());
4136      //
4137      Row = pResult->Next();    
4138      //
4139      if ( !Row ){
4140        if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4141      } else {
4142        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");    
4143        while ( Row ){
4144          if ( IsDebug() ) printf(" del run idtrash %i \n",(UInt_t)atoll(Row->GetField(0)));
4145          myquery.str("");
4146          myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";      
4147          conn->Query(myquery.str().c_str());
4148          drun++;
4149          Row = pResult->Next();    
4150        };
4151      };
4152      //  
4153      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_TRASH table \n",drun);    
4154      //
4155      //
4156      //
4157      drun = 0;
4158      //
4159      myquery.str("");
4160      myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4161      //
4162      pResult = conn->Query(myquery.str().c_str());
4163      //
4164      Row = pResult->Next();    
4165      //
4166      if ( !Row ){
4167        if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4168      } else {
4169        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");    
4170        while ( Row ){
4171          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4172          myquery.str("");
4173          myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4174          conn->Query(myquery.str().c_str());
4175          drun++;
4176          Row = pResult->Next();    
4177        };
4178      };
4179      //  
4180      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_FRAGMENTS table \n",drun);    
4181      //
4182      //
4183      //
4184      delete delrun;
4185      //
4186    };
4187    
4188    
4189    /**
4190     *
4191     * Rearrange calibration tables
4192     *
4193     **/
4194    void PamelaDBOperations::RemoveFILES(){
4195      stringstream myquery;
4196      //
4197      myquery.str("");
4198      myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4199      //
4200      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4201      //
4202      conn->Query(myquery.str().c_str());
4203      //
4204    };
4205    
4206    /**
4207     *
4208     * Rearrange calibration tables
4209     *
4210     **/
4211    void PamelaDBOperations::RemoveCALIBS(){
4212      TSQLResult *pResult;
4213      TSQLRow *Row;
4214      stringstream myquery;
4215      //  
4216      //
4217      // Calorimeter
4218      //
4219      for (Int_t section = 0; section < 4; section++){
4220        myquery.str("");
4221        myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4222        myquery << " SECTION=" << section << ";";
4223        //
4224        pResult = conn->Query(myquery.str().c_str());
4225        //
4226        Row = pResult->Next();      
4227        if( !Row->GetField(0) || !Row->GetField(1) ){
4228          //
4229          if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4230          //
4231        } else {
4232          //
4233          myquery.str("");
4234          myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4235          myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4236          myquery << " SECTION=" << section << ";";
4237          //
4238          pResult = conn->Query(myquery.str().c_str());
4239          //
4240          if( !pResult ){
4241            //
4242            if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4243            //
4244            throw -4;
4245            //
4246          };
4247          //
4248        };
4249      };
4250      myquery.str("");
4251      myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4252      //
4253      pResult = conn->Query(myquery.str().c_str());
4254      //
4255      if( !pResult ){
4256        //
4257        if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4258        //
4259        throw -4;
4260        //
4261      };
4262      //
4263      // Tracker
4264      //
4265      myquery.str("");
4266      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4267      //
4268      pResult = conn->Query(myquery.str().c_str());
4269      //
4270      Row = pResult->Next();      
4271      if( !Row->GetField(0) || !Row->GetField(1) ){
4272        //
4273        if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4274        //
4275      } else {
4276        //
4277        myquery.str("");
4278        myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4279        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4280        //
4281        pResult = conn->Query(myquery.str().c_str());
4282        //
4283        if( !pResult ){
4284          //
4285          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4286          //
4287          throw -4;
4288          //
4289        };
4290        //
4291        myquery.str("");
4292        myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4293        //
4294        pResult = conn->Query(myquery.str().c_str());
4295        //
4296        if( !pResult ){
4297          //
4298          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4299          //
4300          throw -4;
4301          //
4302        };
4303      };
4304      //
4305      //
4306      // S4
4307      //
4308      myquery.str("");
4309      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4310      //
4311      pResult = conn->Query(myquery.str().c_str());
4312      //
4313      Row = pResult->Next();      
4314      if( !Row->GetField(0) || !Row->GetField(1) ){
4315        //
4316        if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4317        //
4318      } else {
4319        //
4320        myquery.str("");
4321        myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4322        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4323        //
4324        pResult = conn->Query(myquery.str().c_str());
4325        //
4326        if( !pResult ){
4327          //
4328          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4329          //
4330          throw -4;
4331          //
4332        };
4333        //
4334        myquery.str("");
4335        myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4336        //
4337        pResult = conn->Query(myquery.str().c_str());
4338        //
4339        if( !pResult ){
4340          //
4341          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4342          //
4343          throw -4;
4344          //
4345        };
4346        //
4347      };
4348    };

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

  ViewVC Help
Powered by ViewVC 1.1.23