--- chewbacca/YodaProfiler/src/GLTables.cpp 2008/10/31 11:21:43 1.4 +++ chewbacca/YodaProfiler/src/GLTables.cpp 2011/11/29 13:18:37 1.14 @@ -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,145 @@ // 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; + }; + // + 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; + + }; + 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()); + // + 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++; + 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(); + // + pResult->Delete(); + if ( dim == 1 ) return h1; + if ( dim == 2 ) return h2; + // + return NULL; +}; + GL_TABLES::GL_TABLES(){ }; @@ -56,7 +197,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){ // // // @@ -92,8 +234,10 @@ TString host = fHost->Data(); TString user = fUser->Data(); TString psw = fPsw->Data(); - dbc->Close(); - delete dbc; + if ( dbc ){ + dbc->Close(); + delete dbc; + }; dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data()); // myquery.str(""); @@ -530,7 +674,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; @@ -594,6 +738,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)); @@ -613,6 +759,8 @@ 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); @@ -630,10 +778,16 @@ 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); - }; + } + if (Row){ + delete Row; + Row = NULL; // This variable is not used below + } // // // @@ -698,7 +852,7 @@ //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; + TSQLRow *Row = NULL; stringstream myquery; // if ( !IDRUN ) IDRUN = ID; @@ -999,8 +1153,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; // ---------------- @@ -1049,11 +1202,13 @@ // if( !pResult->GetRowCount() ) 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 +1242,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 +1260,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; @@ -1150,6 +1308,8 @@ pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())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 +1346,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 +1363,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; @@ -1219,6 +1382,8 @@ pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())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++){ @@ -1227,8 +1392,10 @@ if(t==2) ID_TIMESYNC = (UInt_t)atoll(Row->GetField(t)); if(t==3) PATH = (TString)gSystem->ExpandPathName(Row->GetField(t))+'/'; if(t==4) NAME = Row->GetField(t); - }; - }; + } + } + if (Row) + delete Row; delete pResult; return 0; }; @@ -1243,7 +1410,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; @@ -1259,6 +1426,8 @@ pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())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 +1448,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 +1470,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; @@ -1316,9 +1487,11 @@ if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); - // printf(" mysquery is %s\n",myquery.str().c_str()); + // printf(" mysquery is %s\n",myquery.str().c_str()); // if( !pResult->GetRowCount() ) return(-54); + if (Row) + delete Row; Row = pResult->Next(); if( Row == NULL ) return (-54); // @@ -1346,6 +1519,8 @@ // if( !pResult->GetRowCount() ) return (-54); // + if (Row) + delete Row; Row = pResult->Next(); // myfromtime = (UInt_t)atoll(Row->GetField(1)); @@ -1380,7 +1555,9 @@ if (t==1) FROM_TIME = myfromtime; if (t==2) TO_TIME = mytotime; if (t==3) EV_ROOT = (UInt_t)atoll(Row->GetField(t)); - }; + } + if (Row) + delete Row; pResult->Delete(); return 0; }; @@ -1396,7 +1573,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,6 +1591,8 @@ // if( !pResult ) return(-54); // + if (Row) + delete Row; Row = pResult->Next(); // if( !Row ) return (-54); @@ -1426,7 +1605,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 +1623,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; @@ -1456,6 +1637,8 @@ pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())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 +1648,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 +1669,7 @@ // Bool_t debug = 1; // MySQL variables TSQLResult *pResult; - TSQLRow *Row; + TSQLRow *Row = NULL; int t; int r; stringstream myquery; @@ -1502,6 +1687,8 @@ pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())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++){ @@ -1513,7 +1700,9 @@ 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(); @@ -1546,10 +1735,10 @@ GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TString type, TSQLServer *dbc){ // MySQL variables TFile *file = 0; - UInt_t idraw = 0; + UInt_t idtsy = 0; // TSQLResult *pResult; - TSQLRow *Row; + TSQLRow *Row = NULL; stringstream myquery; stringstream rname; // pcksList packetsNames; @@ -1560,7 +1749,7 @@ myquery.str(""); myquery << "select "; myquery << "PATH"; - myquery << ",NAME,ID_RAW"; + myquery << ",NAME,ID_TIMESYNC"; myquery << " from GL_ROOT where "; myquery << type.Data(); myquery << "=" << id << ";"; @@ -1569,6 +1758,8 @@ this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if( pResult->GetRowCount() ){ + if (Row) + delete Row; Row = pResult->Next(); if( Row ){ stringstream fname; @@ -1576,7 +1767,7 @@ fname << gSystem->ExpandPathName(Row->GetField(0)) << "/" << Row->GetField(1); rname << Row->GetField(1); file = new TFile(fname.str().c_str(),"READ"); - idraw = (UInt_t)atoll(Row->GetField(2)); + idtsy = (UInt_t)atoll(Row->GetField(2)); }; }; // @@ -1589,27 +1780,7 @@ T->GetEntry(0); ph = eh->GetPscuHeader(); pktfirst = ph->GetCounter(); - obtfirst = ph->GetOrbitalTime(); - // -// code = eh->GetCounter(); -// UInt_t en = 0; -// for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){ -// en = code->Get(GetPacketType(*Iter)); -// if ( !strcmp("CalibCalPed",*Iter) || !strcmp("CalibTrk1",*Iter) || !strcmp("CalibTrk2",*Iter) || !strcmp("CalibS4",*Iter) ){ -// // -// TTree *TC = 0; -// TC = (TTree*)file->Get("CalibCalPed"); -// if ( !TC || TC->IsZombie() ) return; -// EventHeader *ehc = 0; -// PscuHeader *phc = 0; -// TC->SetBranchAddress("Header", &ehc); -// TC->GetEntry(0); -// phc = ehc->GetPscuHeader(); -// pktfirst = phc->GetCounter(); -// obtfirst = phc->GetOrbitalTime(); -// // -// }; -// }; + // obtfirst = ph->GetOrbitalTime(); // }; // @@ -1617,117 +1788,190 @@ // T0 = 0; // - // stringstream oss; - TString frn = rname.str().c_str(); - frn = frn.ReplaceAll(".root",5,".pam",4); - 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 SPECIAL_FILE='" - << frn.Data() << "';"; - if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; - this->GetGLTABLES()->AddQ(); - pResult = dbc->Query(oss.str().c_str()); - Row = pResult->Next(); // TString name=rname.str().c_str(); UInt_t dworbit = 0; - Int_t nlength = name.Length(); + // Int_t nlength = name.Length(); + delete pResult; // - // Is not a special file + // New code, we have one more column on GL_TIMESYNC so we can trust that one for the Resurs offset // - if ( !Row ){ - delete pResult; - // - // New code, we have one more column on GL_TIMESYNC so we can trust that one for the Resurs offset - // - oss.str(""); - oss << "SELECT ID_RESURS_OFFSET from GL_TIMESYNC where ID_RAW=" << idraw <<";"; - if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; - this->GetGLTABLES()->AddQ(); - pResult = dbc->Query(oss.str().c_str()); - Bool_t fndit = false; - if ( pResult ){ - Row = pResult->Next(); - if ( Row ){ - 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(0) << ";"; - if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; - this->GetGLTABLES()->AddQ(); - 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 ( !fndit ){ - delete pResult; - // - printf(" OK, you got an error because this is the old database\n Using backward compability code, hence you can continue safetly \n"); - // - // Old code, we must trust the filename + 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 ){ // - if ( nlength < 5 ) return; - TString dwo = 0; - for (Int_t i = 0; i<5; i++){ - dwo.Append(name[i],1); - }; - if ( dwo.IsDigit() ){ - dworbit = (UInt_t)dwo.Atoi(); - } else { - dwo=""; - for (Int_t i = 8; i<13; i++){ - dwo.Append(name[i],1); - }; - if ( dwo.IsDigit() ){ - dworbit = (UInt_t)dwo.Atoi(); - } else { - dworbit = 1; - }; - }; + 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 FROM_ORBIT< " - << dworbit << " order by FROM_ORBIT desc limit 1;"; + 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; this->GetGLTABLES()->AddQ(); + delete pResult; pResult = dbc->Query(oss.str().c_str()); - Row = pResult->Next(); - if ( !Row ){ - printf(" ERROR FROM GLTables! cannot determine Resurs offset \n"); - return; + 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(); // - // look for the correct timesync entry + 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 OBT0,TIMESYNC,TYPE FROM GL_TIMESYNC " - << " WHERE ID_RAW = " << idraw - << ";"; + 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(""); + fname << gSystem->ExpandPathName(Row->GetField(0)) << "/" << 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; + // + // 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 != NULL) && ((UInt_t)atoll(Row->GetField(0)) > 0 ) ){ + 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)); - toffset = (UInt_t)atoll(Row->GetField(1)) - (UInt_t)(this->DBobt((UInt_t)atoll(Row->GetField(0)))/1000) + T0; + 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) ) 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; // - file->Close(); + // printf(" T0 %u toffset is %u \n",T0,toffset); + // + if ( file ) file->Close(); + if (Row) + delete Row; delete pResult; }; @@ -1738,6 +1982,7 @@ */ UInt_t GL_TIMESYNC::DBabsTime(UInt_t OBT){ // + // printf(" OBT %u DBobt %llu toffset %u dbabstime %u\n",OBT,this->DBobt(OBT),toffset,((UInt_t)(this->DBobt(OBT)/1000)+toffset)); return(((UInt_t)(this->DBobt(OBT)/1000)+toffset)); // }; @@ -1776,11 +2021,11 @@ */ Long64_t GL_TIMESYNC::DBobt(UInt_t obt){ // - if ( obt < (obtfirst/2) && obtfirst > (numeric_limits::max()/2) ){ + if ( obt < ((Long64_t)obtfirst/2) && obtfirst > (numeric_limits::max()/2) ){ return((Long64_t)obt+(Long64_t)numeric_limits::max()); }; // - if ( obt > (obtfirst*2) && obt > (numeric_limits::max()/2) ){ + if ( obt > ((Long64_t)obtfirst*2) && obt > (numeric_limits::max()/2) ){ return((Long64_t)obt-(Long64_t)numeric_limits::max()); }; // @@ -1997,7 +2242,7 @@ // 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; @@ -2016,7 +2261,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); @@ -2025,7 +2271,8 @@ tleToTime = UINT_MAX; } - delete row; + if (row) + delete row; delete result; return 0;