--- chewbacca/YodaProfiler/src/GLTables.cpp 2008/12/12 11:07:09 1.6 +++ chewbacca/YodaProfiler/src/GLTables.cpp 2014/10/14 13:19:31 1.19 @@ -7,6 +7,7 @@ // #include #include +#include // #include #include @@ -17,6 +18,7 @@ #include #include // +ClassImp(Q2TH); ClassImp(GL_TABLES); ClassImp(GL_TRK_CALIB); ClassImp(GL_RUN); @@ -30,6 +32,155 @@ // using namespace std; +Q2TH::Q2TH(TString host, TString user, TString psw){ + this->Open(host,user,psw); +}; + +void Q2TH::Open(TString host, TString user, TString psw){ + fh = gSystem->ExpandPathName(host.Data()); + fu = gSystem->ExpandPathName(user.Data()); + fp = gSystem->ExpandPathName(psw.Data()); + printf(" Connecting to DB %s \n",fh.Data()); + dbc = TSQLServer::Connect(fh.Data(),fu.Data(),fp.Data()); + if ( dbc && dbc->IsConnected() ){ + printf(" connected! \n"); + } else { + printf(" ERROR! not connected... :( \n"); + }; +}; + +TObject *Q2TH::Draw(TString query, Bool_t verbose, TString hname){ + // + if ( !strcmp(query.Data(),"help") ){ + printf(" USAGE: \n"); + printf(" 1) start root and create Q2TH object with \n"); + printf(" Q2TH *qt = new Q2TH() \n"); + printf(" or \n"); + printf(" Q2TH *qt = new Q2TH(\"mysql://srvg-g2-01.ts.infn.it/pamelaProcessing9_TS\",\"pamelaprod_ro\",\"mypassword\") \n"); + printf(" 2) query the DB with \n"); + printf(" qt->Draw(\"select REAL_TIME_INIT,BAD_PKT_PERCENTAGE from ROOT_TABLE_MERGING;\"); \n"); + printf(" qt->Draw(\"select REAL_TIME_INIT,BAD_PKT_PERCENTAGE from ROOT_TABLE_MERGING;\",true); this will print numbers on screen \n"); + printf(" qt->Draw(\"select REAL_TIME_INIT from ROOT_TABLE_MERGING;\",true,\"myhisto\"); this will print numbers on screen and create histo \"myhisto\"\n"); + printf(" 3) to use your own THxD create it and then query the DB giving as argument the name of histo: \n"); + printf(" TH2D *myhisto=new TH2D(\"myhisto\",\"myhisto\",5000,1140000000.,1240000000.,10000,0.,1.) \n"); + printf(" qt->Draw(\"select REAL_TIME_INIT,BAD_PKT_PERCENTAGE from ROOT_TABLE_MERGING;\",false,\"myhisto\")\n\n\n"); + + return NULL; + }; + // + if (Row) + delete Row; + pResult = dbc->Query(query.Data()); + // + Row = pResult->Next(); + // + Int_t dim = pResult->GetFieldCount(); + if ( dim < 1 || dim > 2 ){ + printf(" Dim == %i not supported yet \n",dim); + return NULL; + }; + // + TH1D *h1 = NULL; + TH2D *h2 = NULL; + Double_t f1 = 0.; + Double_t minf1 = numeric_limits::max(); + Double_t maxf1 = numeric_limits::min(); + Double_t f2 = 0.; + Double_t minf2 = numeric_limits::max(); + Double_t maxf2 = numeric_limits::min(); + // + while ( Row ){ + f1 = (Double_t)atof(Row->GetField(0)); + if ( f1 > maxf1 ) maxf1 = f1; + if ( f1 < minf1 ) minf1 = f1; + if ( dim == 2 ){ + f2 = (Double_t)atof(Row->GetField(1)); + if ( f2 > maxf2 ) maxf2 = f2; + if ( f2 < minf2 ) minf2 = f2; + + }; + if (Row) + delete Row; + Row = pResult->Next(); + }; + pResult->Delete(); + // + + // + Int_t f1bin = 70; + Int_t f2bin = 70; + if ( dim == 1 ){ + f1bin = int((maxf1-minf1)/1000.); + if ( f1bin < 70 ) f1bin = 70; + if ( f1bin > 1000 ) f1bin = 1000; + if ( !strcmp(hname.Data(),"q2th") ) hname += "1"; + // h1 = dynamic_cast(gDirectory->FindObject(hname.Data())); + h1 = (TH1D*)(gDirectory->FindObject(hname.Data())); + if ( !strcmp(hname.Data(),"q2th1") ){ + if ( h1 ) h1->Delete(); + }; + if ( !h1 ) h1 = new TH1D(hname.Data(),hname.Data(),f1bin,minf1*0.98,maxf1*1.02); + // h1->SetBit(TH1::kCanRebin); + if ( verbose ) printf("\n\n Row %s \n",pResult->GetFieldName(0)); + }; + if ( dim == 2 ){ + f2bin = int((maxf2-minf2)/1000.); + if ( f2bin < 70 ) f2bin = 70; + if ( f2bin > 1000 ) f2bin = 1000; + if ( !strcmp(hname.Data(),"q2th") ) hname += "2"; + // h2 = dynamic_cast(gDirectory->FindObject(hname.Data())); + h2 = (TH2D*)(gDirectory->FindObject(hname.Data())); + if ( !strcmp(hname.Data(),"q2th2") ){ + if ( h2 ) h2->Delete(); + }; + if ( !h2 ) h2 = new TH2D(hname.Data(),hname.Data(),f1bin,minf1*0.98,maxf1*1.02,f2bin,minf2*0.98,maxf2*1.02); + // h2->SetBit(TH2::kCanRebin); + if ( verbose ) printf("\n\n Row %s %s \n",pResult->GetFieldName(0),pResult->GetFieldName(1)); + }; + // + pResult = dbc->Query(query.Data()); + // + if (Row) + delete Row; + Row = pResult->Next(); + // + Int_t r = 0; + // + while ( Row ){ + f1 = (Double_t)atof(Row->GetField(0)); + if ( dim == 1 ){ + if ( verbose ) printf(" %i %f \n",r,f1); + h1->Fill(f1); + } else { + f2 = (Double_t)atof(Row->GetField(1)); + if ( verbose ) printf(" %i %f %f \n",r,f1,f2); + h2->Fill(f1,f2); + }; + r++; + if (Row) + delete Row; + Row = pResult->Next(); + }; + // + TCanvas *c = NULL; + TString cname = Form("%sc",hname.Data()); + // c = dynamic_cast(gDirectory->FindObject(hname.Data())); + c = (TCanvas*)(gDirectory->FindObject(cname.Data())); + if ( !c ) c = new TCanvas(Form("%sc",cname.Data())); + c->Clear(); + c->cd(); + if ( dim == 1 ) h1->Draw(); + if ( dim == 2 ) h2->Draw(); + // + if (Row) + delete Row; + pResult->Delete(); + if ( dim == 1 ) return h1; + if ( dim == 2 ) return h2; + // + return NULL; +}; + GL_TABLES::GL_TABLES(){ }; @@ -56,7 +207,8 @@ mp = psw.Data(); }; -Bool_t GL_TABLES::IsConnected(TSQLServer *&dbc){ +//Bool_t GL_TABLES::IsConnected(TSQLServer *&dbc){ +Bool_t GL_TABLES::IsConnected(TSQLServer *dbc){ // // // @@ -71,7 +223,7 @@ myquery << "show databases;"; if ( dbc ){ if ( dbc->IsConnected() ){ - dbc->Query(myquery.str().c_str()); + delete dbc->Query(myquery.str().c_str()); fNquery++; if ( !(dbc->GetErrorCode()) ){ // printf("ok\n"); @@ -92,13 +244,16 @@ TString host = fHost->Data(); TString user = fUser->Data(); TString psw = fPsw->Data(); - dbc->Close(); - delete dbc; + if ( dbc ){ + dbc->Close(); + delete dbc; + dbc = 0; + }; dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data()); // myquery.str(""); myquery << "show databases;"; - dbc->Query(myquery.str().c_str()); + delete dbc->Query(myquery.str().c_str()); fNquery++; // if ( dbc->GetErrorCode() != 2013 && dbc->GetErrorCode() != 2006 ){ if ( !(dbc->GetErrorCode()) ){ @@ -106,12 +261,16 @@ printf(" ...connection recovered, I can continue! \n"); // myquery.str(""); - myquery << "SET time_zone='+0:00'"; - dbc->Query(myquery.str().c_str()); + myquery << "SET time_zone='+0:00';"; + delete dbc->Query(myquery.str().c_str()); + fNquery++; + myquery.str(""); + myquery << "SET sql_mode = 'NO_UNSIGNED_SUBTRACTION';"; + delete dbc->Query(myquery.str().c_str()); fNquery++; myquery.str(""); myquery << "SET wait_timeout=173000;"; - dbc->Query(myquery.str().c_str()); + delete dbc->Query(myquery.str().c_str()); fNquery++; return true; }; @@ -530,7 +689,7 @@ Int_t GL_RUN::DeleteRun(TSQLServer *dbc,UInt_t IDRUN,TString FromTable){ // MySQL variables TSQLResult *pResult; - TSQLRow *Row; + TSQLRow *Row = NULL; stringstream myquery; // if ( !IDRUN ) IDRUN = ID; @@ -580,7 +739,7 @@ // if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; this->GetGLTABLES()->AddQ(); - dbc->Query(myquery.str().c_str()); + delete dbc->Query(myquery.str().c_str()); // // retrieve this ID_TRASH // @@ -594,6 +753,8 @@ UInt_t idl0 = 0; UInt_t idl2 = 0; // + if (Row) + delete Row; Row = pResult->Next(); if( Row != NULL ){ idtrash = (UInt_t)atoll(Row->GetField(0)); @@ -609,10 +770,18 @@ // // printf("2myquery is %s \n",myquery.str().c_str()); // - if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + if ( !this->GetGLTABLES()->IsConnected(dbc) ){ + if (pResult) + delete pResult; + if (Row) + delete Row; + return -57; + } this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // + if (Row) + delete Row; Row = pResult->Next(); if( Row != NULL ){ fileL0 = (TString)Row->GetField(0); @@ -626,14 +795,28 @@ // // printf("3myquery is %s \n",myquery.str().c_str()); // - if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + if ( !this->GetGLTABLES()->IsConnected(dbc) ){ + if (pResult) + delete pResult; + if (Row) + delete Row; + return -57; + } this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // + if (Row) + delete Row; Row = pResult->Next(); if( Row != NULL ){ fileL2 = (TString)Row->GetField(0); - }; + } + delete pResult; + pResult = NULL; + if (Row){ + delete Row; + Row = NULL; // This variable is not used below + } // // // @@ -646,7 +829,7 @@ // if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; this->GetGLTABLES()->AddQ(); - dbc->Query(myquery.str().c_str()); + delete dbc->Query(myquery.str().c_str()); // myquery.str(""); myquery << " UPDATE GL_RUN_TRASH SET FILENAMEL2='"; @@ -657,7 +840,7 @@ // if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; this->GetGLTABLES()->AddQ(); - dbc->Query(myquery.str().c_str()); + delete dbc->Query(myquery.str().c_str()); // myquery.str(""); myquery << " UPDATE GL_RUN_TRASH SET BELONGED_TO='"; @@ -669,7 +852,7 @@ // if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; this->GetGLTABLES()->AddQ(); - dbc->Query(myquery.str().c_str()); + delete dbc->Query(myquery.str().c_str()); // myquery.str(""); myquery << " DELETE FROM "; @@ -681,7 +864,7 @@ // if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; this->GetGLTABLES()->AddQ(); - dbc->Query(myquery.str().c_str()); + delete dbc->Query(myquery.str().c_str()); // return 0; }; @@ -697,8 +880,8 @@ // insert into GL_RUN_FRAGMENTS select * FROM GL_RUN where ID=11; //insert into GL_RUN_TRASH VALUES (ID , ID_RUN_FRAG , ID_ROOT_L0 , ID_ROOT_L2 , RUNHEADER_TIME , RUNTRAILER_TIME , RUNHEADER_OBT , RUNTRAILER_OBT , RUNHEADER_PKT , RUNTRAILER_PKT , BOOT_NUMBER , EV_FROM , EV_TO , NEVENTS , PKT_COUNTER , PKT_READY_COUNTER , COMPILATIONTIMESTAMP , FAV_WRK_SCHEDULE , EFF_WRK_SCHEDULE , PRH_VAR_TRG_MODE_A , PRH_VAR_TRG_MODE_B , ACQ_BUILD_INFO , ACQ_VAR_INFO , RM_ACQ_AFTER_CALIB , RM_ACQ_SETTING_MODE, TRK_CALIB_USED,CAL_DSP_MASK, LAST_TIMESYNC, OBT_TIMESYNC, VALIDATION, INSERT_TIME) select * FROM GL_RUN where ID=11; // MySQL variables - TSQLResult *pResult; - TSQLRow *Row; + TSQLResult *pResult = NULL; + TSQLRow *Row = NULL; stringstream myquery; // if ( !IDRUN ) IDRUN = ID; @@ -720,9 +903,15 @@ if( Row != NULL ){ ToTable = (TString)Row->GetField(0); } else { + delete pResult; return 1; }; }; + + if (pResult) + delete pResult; + if (Row) + delete Row; // ---------------- myquery.str(""); myquery << " INSERT INTO "; @@ -803,7 +992,7 @@ // if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; this->GetGLTABLES()->AddQ(); - dbc->Query(myquery.str().c_str()); + delete dbc->Query(myquery.str().c_str()); // // myquery.str(""); @@ -812,7 +1001,7 @@ // if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; this->GetGLTABLES()->AddQ(); - dbc->Query(myquery.str().c_str()); + delete dbc->Query(myquery.str().c_str()); // return 0; }; @@ -900,7 +1089,7 @@ // if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; this->GetGLTABLES()->AddQ(); - dbc->Query(myquery.str().c_str()); + delete dbc->Query(myquery.str().c_str()); // return 0; @@ -982,7 +1171,7 @@ // if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; this->GetGLTABLES()->AddQ(); - dbc->Query(myquery.str().c_str()); + delete dbc->Query(myquery.str().c_str()); // return 0; @@ -999,8 +1188,7 @@ Int_t GL_RUN::Query_GL_RUN(UInt_t run, TSQLServer *dbc){ // MySQL variables TSQLResult *pResult; - TSQLRow *Row; - int t; + TSQLRow *Row = NULL; int r; stringstream myquery; // ---------------- @@ -1047,13 +1235,20 @@ // // printf(" getrowcount %i \n",pResult->GetRowCount()); // - if( !pResult->GetRowCount() ) return(-50); + if( !pResult->GetRowCount() ){ + delete pResult; + if (Row) + delete Row; + return(-50); + } // - for( r=0; r < 1000; r++){ + for( r=0; r < 1000; r++){ + if (Row) + delete Row; Row = pResult->Next(); if( Row == NULL ) break; // Set_GL_RUN(Row); - for( t = 0; t < pResult->GetFieldCount(); t++){ + for( int t = 0; t < pResult->GetFieldCount(); t++){ if (t== 0) ID = (UInt_t)atoll(Row->GetField(t)); if (t== 1) ID_RUN_FRAG = (UInt_t)atoll(Row->GetField(t)); if (t== 2) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t)); @@ -1087,8 +1282,11 @@ if (t==30) PHYSENDRUN_MASK_S11CRC = (UInt_t)atoll(Row->GetField(t)); if (t==31) VALIDATION = (UInt_t)atoll(Row->GetField(t)); }; - }; - // delete pResult; + } + + if (Row) + delete Row; + delete pResult; return(0); }; @@ -1102,7 +1300,7 @@ Int_t GL_RUN::Query_GL_RUN_FRAGMENTS(TString where, TSQLServer *dbc){ // MySQL variables TSQLResult *pResult; - TSQLRow *Row; + TSQLRow *Row = NULL; int t; int r; stringstream myquery; @@ -1148,8 +1346,15 @@ if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); - if(!pResult->GetRowCount())return(-50); + if(!pResult->GetRowCount()){ + delete pResult; + if (Row) + delete Row; + return(-50); + } for( r=0; r < 1000; r++){ + if (Row) + delete Row; Row = pResult->Next(); if( Row == NULL ) break; for( t = 0; t < pResult->GetFieldCount(); t++){ @@ -1186,8 +1391,11 @@ if (t==30) PHYSENDRUN_MASK_S11CRC = (UInt_t)atoll(Row->GetField(t)); if (t==31) VALIDATION = (UInt_t)atoll(Row->GetField(t)); }; - }; - // delete pResult; + } + + if (Row) + delete Row; + delete pResult; return(0); };// **************************************************** @@ -1200,7 +1408,7 @@ Int_t GL_ROOT::Query_GL_ROOT(UInt_t id, TSQLServer *dbc){ // MySQL variables TSQLResult *pResult; - TSQLRow *Row; + TSQLRow *Row = NULL; int t; int r; stringstream myquery; @@ -1217,18 +1425,30 @@ if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); - if(!pResult->GetRowCount())return (-51); + if(!pResult->GetRowCount()){ + delete pResult; + if (Row) + delete Row; + return (-51); + } for( r=0; r < 1000; r++){ + if (Row) + delete Row; Row = pResult->Next(); if( Row == NULL ) break; for( t = 0; t < pResult->GetFieldCount(); t++){ if(t==0) ID = (UInt_t)atoll(Row->GetField(t)); if(t==1) ID_RAW = (UInt_t)atoll(Row->GetField(t)); if(t==2) ID_TIMESYNC = (UInt_t)atoll(Row->GetField(t)); - if(t==3) PATH = (TString)gSystem->ExpandPathName(Row->GetField(t))+'/'; + if(t==3){ + PATH = TString(Row->GetField(t)) + '/'; + gSystem->ExpandPathName(PATH); + } if(t==4) NAME = Row->GetField(t); - }; - }; + } + } + if (Row) + delete Row; delete pResult; return 0; }; @@ -1243,7 +1463,7 @@ Int_t GL_TRK_CALIB::Query_GL_TRK_CALIB(UInt_t time, TSQLServer *dbc){ // MySQL variables TSQLResult *pResult; - TSQLRow *Row; + TSQLRow *Row = NULL; int t; int r; stringstream myquery; @@ -1257,8 +1477,13 @@ if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); - if(!pResult->GetRowCount())return (-53); + if(!pResult->GetRowCount()){ + delete pResult; + return (-53); + } for( r=0; r < 1000; r++){ + if (Row) + delete Row; Row = pResult->Next(); if( Row == NULL ) break; for( t = 0; t < pResult->GetFieldCount(); t++){ @@ -1279,7 +1504,9 @@ if (t==10) BOOT_NUMBER = (UInt_t)atoll(Row->GetField(t)); if (t==11) VALIDATION = (UInt_t)atoll(Row->GetField(t)); }; - }; + } + if (Row) + delete Row; delete pResult; // // if ( TO_TIME < time ) return(51); @@ -1299,7 +1526,7 @@ Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB(UInt_t time, UInt_t &uptime, UInt_t section, TSQLServer *dbc){ // MySQL variables TSQLResult *pResult; - TSQLRow *Row; + TSQLRow *Row = NULL; int t; stringstream myquery; uptime = 0; @@ -1318,9 +1545,17 @@ pResult = dbc->Query(myquery.str().c_str()); // printf(" mysquery is %s\n",myquery.str().c_str()); // - if( !pResult->GetRowCount() ) return(-54); + if (Row) + delete Row; + if( !pResult->GetRowCount() ){ + delete pResult; + return(-54); + } Row = pResult->Next(); - if( Row == NULL ) return (-54); + if( Row == NULL ){ + delete pResult; + return (-54); + } // uptime = (UInt_t)atoll(Row->GetField(2)); // @@ -1337,15 +1572,28 @@ myquery << "select ID_ROOT_L0, FROM_TIME, TO_TIME, EV_ROOT,VALIDATION from GL_CALO_CALIB where SECTION=" << section; myquery << " and FROM_TIME <= " << time; myquery << " and VALIDATION=1 ORDER BY FROM_TIME DESC LIMIT 1;"; - if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + if ( !this->GetGLTABLES()->IsConnected(dbc) ){ + if(pResult) + delete pResult; + if(Row) + delete Row; + return -57; + } this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // printf(" mysquery is %s\n",myquery.str().c_str()); // // if no results yet quit with error // - if( !pResult->GetRowCount() ) return (-54); + if( !pResult->GetRowCount() ){ + delete pResult; + if(Row) + delete Row; + return (-54); + } // + if (Row) + delete Row; Row = pResult->Next(); // myfromtime = (UInt_t)atoll(Row->GetField(1)); @@ -1359,29 +1607,47 @@ myquery.str(""); myquery << "select ID_ROOT_L0, FROM_TIME, TO_TIME, EV_ROOT from GL_CALO_CALIB where SECTION=" << section; myquery << " and VALIDATION=1 ORDER BY ABS(" << time << "-FROM_TIME) asc limit 1;"; - if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + if ( !this->GetGLTABLES()->IsConnected(dbc) ){ + if(pResult) + delete pResult; + if(Row) + delete Row; + return -57; + } this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // printf(" mysquery is %s\n",myquery.str().c_str()); // // if no results yet quit with error // - if( !pResult->GetRowCount() ) return (-54); + if( !pResult->GetRowCount() ){ + if (Row) + delete Row; + delete pResult; + return (-54); + } // + if (Row) + delete Row; Row = pResult->Next(); // }; // // store infos and exit // - if( Row == NULL ) return (-54); + if( Row == NULL ){ + delete pResult; + return (-54); + } for( t = 0; t < pResult->GetFieldCount(); t++){ if (t==0) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t)); if (t==1) FROM_TIME = myfromtime; if (t==2) TO_TIME = mytotime; if (t==3) EV_ROOT = (UInt_t)atoll(Row->GetField(t)); - }; - pResult->Delete(); + } + if (Row) + delete Row; + delete pResult; return 0; }; @@ -1396,7 +1662,7 @@ Int_t GL_CALOPULSE_CALIB::Query_GL_CALOPULSE_CALIB(UInt_t time, UInt_t section, UInt_t pampli, TSQLServer *dbc){ // MySQL variables TSQLResult *pResult; - TSQLRow *Row; + TSQLRow *Row = NULL; int t; stringstream myquery; // @@ -1414,9 +1680,14 @@ // if( !pResult ) return(-54); // + if (Row) + delete Row; Row = pResult->Next(); // - if( !Row ) return (-54); + if( !Row ){ + delete pResult; + return (-54); + } // // store infos and exit // @@ -1426,7 +1697,9 @@ if (t==1) FROM_TIME = (UInt_t)atoll(Row->GetField(t)); if (t==2) TO_TIME = (UInt_t)atoll(Row->GetField(t)); if (t==3) EV_ROOT = (UInt_t)atoll(Row->GetField(t)); - }; + } + if (Row) + delete Row; pResult->Delete(); return 0; }; @@ -1442,7 +1715,7 @@ Int_t GL_S4_CALIB::Query_GL_S4_CALIB(UInt_t time, TSQLServer *dbc){ // MySQL variables TSQLResult *pResult; - TSQLRow *Row; + TSQLRow *Row = NULL; int t; int r; stringstream myquery; @@ -1454,8 +1727,13 @@ if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); - if(!pResult->GetRowCount())return (-55);//throw -55; + if(!pResult->GetRowCount()){ + delete pResult; + return (-55);//throw -55; + } for( r=0; r < 1000; r++){ + if (Row) + delete Row; Row = pResult->Next(); if( Row == NULL ) break; for( t = 0; t < pResult->GetFieldCount(); t++){ @@ -1465,7 +1743,9 @@ if (t==3) FROM_TIME = (UInt_t)atoll(Row->GetField(t)); if (t==4) TO_TIME = (UInt_t)atoll(Row->GetField(t)); }; - }; + } + if (Row) + delete Row; delete pResult; // if(TO_TIME < time)return(51); @@ -1484,7 +1764,7 @@ // Bool_t debug = 1; // MySQL variables TSQLResult *pResult; - TSQLRow *Row; + TSQLRow *Row = NULL; int t; int r; stringstream myquery; @@ -1500,20 +1780,30 @@ if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); - if(!pResult->GetRowCount())return (-52); + if(!pResult->GetRowCount()){ + delete pResult; + return (-52); + } for( r=0; r < 1000; r++){ + if (Row) + delete Row; Row = pResult->Next(); if( Row == NULL ) break; for( t = 0; t < pResult->GetFieldCount(); t++){ - if (t==0) ID = (UInt_t)atoll(Row->GetField(t)); - if (t==1) PATH = (TString)gSystem->ExpandPathName(Row->GetField(t))+'/';// put in fpath the path to that file - if (t==2) NAME = Row->GetField(t); - if (t==3) DESCR = Row->GetField(t); - if (t==4) FROM_TIME = (UInt_t)atoll(Row->GetField(t)); - if (t==5) TO_TIME = (UInt_t)atoll(Row->GetField(t)); - if (t==6) TYPE = (UInt_t)atoll(Row->GetField(t)); + if (t==0) ID = (UInt_t)atoll(Row->GetField(t)); + if (t==1) { + PATH = TString(Row->GetField(t)) + "/";// put in fpath the path to that file + gSystem->ExpandPathName(PATH); + } + if (t==2) NAME = Row->GetField(t); + if (t==3) DESCR = Row->GetField(t); + if (t==4) FROM_TIME = (UInt_t)atoll(Row->GetField(t)); + if (t==5) TO_TIME = (UInt_t)atoll(Row->GetField(t)); + if (t==6) TYPE = (UInt_t)atoll(Row->GetField(t)); }; - }; + } + if (Row) + delete Row; delete pResult; // if(TO_TIME==0) TO_TIME = numeric_limits::max(); @@ -1549,7 +1839,7 @@ UInt_t idtsy = 0; // TSQLResult *pResult; - TSQLRow *Row = 0; + TSQLRow *Row = NULL; stringstream myquery; stringstream rname; // pcksList packetsNames; @@ -1569,11 +1859,15 @@ this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if( pResult->GetRowCount() ){ + if (Row) + delete Row; Row = pResult->Next(); if( Row ){ stringstream fname; fname.str(""); - fname << gSystem->ExpandPathName(Row->GetField(0)) << "/" << Row->GetField(1); + TString auxStr(Row->GetField(0)); + gSystem->ExpandPathName(auxStr); + fname << auxStr << "/" << Row->GetField(1); rname << Row->GetField(1); file = new TFile(fname.str().c_str(),"READ"); idtsy = (UInt_t)atoll(Row->GetField(2)); @@ -1600,9 +1894,145 @@ stringstream oss; // TString name=rname.str().c_str(); - UInt_t dworbit = 0; + // UInt_t dworbit = 0; // Int_t nlength = name.Length(); + delete pResult; + if (Row){ + delete Row; + Row = NULL; + } + // + // New code, we have one more column on GL_TIMESYNC so we can trust that one for the Resurs offset + // + oss.str(""); + oss << "SELECT OBT0,TIMESYNC,TYPE,ID_RESURS_OFFSET from GL_TIMESYNC where ID=" << idtsy <<";"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; + this->GetGLTABLES()->AddQ(); + pResult = dbc->Query(oss.str().c_str()); + Bool_t fndit = false; + if ( pResult ){ + if (Row) + delete Row; + Row = pResult->Next(); + if ( Row ){ + // + OBT0 = (UInt_t)atoll(Row->GetField(0)); + obtfirst = OBT0; + TIMESYNC = (UInt_t)atoll(Row->GetField(1)); + TYPE = (UInt_t)atoll(Row->GetField(2)); + // + oss.str(""); + 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 ID=" + << Row->GetField(3) << ";"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ){ + delete pResult; + delete Row; + return; + } + this->GetGLTABLES()->AddQ(); + delete pResult; + pResult = dbc->Query(oss.str().c_str()); + if (pResult){ + if (Row) + delete Row; + Row = pResult->Next(); + if ( Row ){ + // printf(" GREAT! the DB structure is the new one! \n"); + fndit = true; + // dworbit = 1; + }; + }; + }; + }; + if ( !fndit ){ + // + printf(" ERROR OLD DB! \n"); + printf(" ERROR FROM GLTables! cannot determine Resurs offset \n"); + // + }; + // + 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); + T0 = (UInt_t)tu.GetSec(); + // + toffset = (UInt_t)TIMESYNC - (UInt_t)(this->DBobt(OBT0)/1000) + T0; + // + // printf(" T0 %u toffset is %u \n",T0,toffset); + // + if ( file ) file->Close(); + if (Row) + delete Row; delete pResult; +}; + +GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TString type, TSQLServer *dbc, Bool_t usel0file){ + // MySQL variables + TFile *file = 0; + UInt_t idtsy = 0; + // + TSQLResult *pResult; + TSQLRow *Row = NULL; + stringstream myquery; + stringstream rname; + // pcksList packetsNames; + // pcksList::iterator Iter; + // getPacketsNames(packetsNames); + rname.str(""); + // ---------------- + myquery.str(""); + myquery << "select "; + myquery << "PATH"; + myquery << ",NAME,ID_TIMESYNC"; + myquery << " from GL_ROOT where "; + myquery << type.Data(); + myquery << "=" << id << ";"; + // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; + this->GetGLTABLES()->AddQ(); + pResult = dbc->Query(myquery.str().c_str()); + if( pResult->GetRowCount() ){ + if (Row) + delete Row; + Row = pResult->Next(); + if( Row ){ + stringstream fname; + fname.str(""); + TString auxString(Row->GetField(0)); + gSystem->ExpandPathName(auxString); + fname << auxString << "/" << Row->GetField(1); + rname << Row->GetField(1); + if ( usel0file ) file = new TFile(fname.str().c_str(),"READ"); + idtsy = (UInt_t)atoll(Row->GetField(2)); + }; + }; + // + if ( usel0file && file && file->IsOpen() ){ + TTree *T=(TTree*)file->Get("Physics"); + pamela::EventHeader *eh = 0; + pamela::PscuHeader *ph = 0; + T->SetBranchAddress("Header", &eh); + // + T->GetEntry(0); + ph = eh->GetPscuHeader(); + pktfirst = ph->GetCounter(); + // obtfirst = ph->GetOrbitalTime(); + // + }; + if ( !usel0file ) pktfirst = 0; + // + // look for Resurs offset + // + T0 = 0; + // + stringstream oss; + // + TString name=rname.str().c_str(); + // UInt_t dworbit = 0; + // Int_t nlength = name.Length(); + delete pResult; + if (Row){ + delete Row; + Row = NULL; + } // // New code, we have one more column on GL_TIMESYNC so we can trust that one for the Resurs offset // @@ -1613,6 +2043,8 @@ pResult = dbc->Query(oss.str().c_str()); Bool_t fndit = false; if ( pResult ){ + if (Row) + delete Row; Row = pResult->Next(); if ( Row ){ // @@ -1624,17 +2056,23 @@ oss.str(""); 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 ID=" << Row->GetField(3) << ";"; - if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; + if ( !this->GetGLTABLES()->IsConnected(dbc) ){ + delete pResult; + delete Row; + return; + } this->GetGLTABLES()->AddQ(); delete pResult; pResult = dbc->Query(oss.str().c_str()); - if ( pResult ){ - Row = pResult->Next(); - if ( Row ){ - // printf(" GREAT! the DB structure is the new one! \n"); - fndit = true; - dworbit = 1; - }; + if (pResult){ + if (Row) + delete Row; + Row = pResult->Next(); + if (Row){ + // printf(" GREAT! the DB structure is the new one! \n"); + fndit = true; + // dworbit = 1; + }; }; }; }; @@ -1652,7 +2090,9 @@ // // printf(" T0 %u toffset is %u \n",T0,toffset); // - file->Close(); + if ( file ) file->Close(); + if (Row) + delete Row; delete pResult; }; @@ -1923,17 +2363,19 @@ // Int_t GL_TLE::DoQuery(TString myquery, TSQLServer *dbc){ TSQLResult *result; - TSQLRow *row; + TSQLRow *row = NULL; // Set the right time_zone (otherwise horrible things will occur! :) if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; - dbc->Query("SET time_zone = '+0:00'"); + delete dbc->Query("SET time_zone = '+0:00';"); + delete dbc->Query("SET sql_mode = 'NO_UNSIGNED_SUBTRACTION';"); // Do the query this->GetGLTABLES()->AddQ(); result = dbc->Query(myquery.Data()); if(! result->GetRowCount() ) { cerr << "GL_TLE: query failed: " << myquery.Data() << endl; + delete result; return 1; } @@ -1942,7 +2384,8 @@ tle = GiveTle(row); tleFromTime = strtol(row->GetField(4), NULL, 10); - + if (row) + delete row; row = result->Next(); // second tle row if(row) tleToTime = strtol(row->GetField(4), NULL, 10); @@ -1951,7 +2394,8 @@ tleToTime = UINT_MAX; } - delete row; + if (row) + delete row; delete result; return 0;