--- YodaProfiler/src/GLTables.cpp 2007/01/23 17:04:11 1.21 +++ YodaProfiler/src/GLTables.cpp 2007/04/18 13:55:00 1.27 @@ -17,6 +17,7 @@ #include #include // +ClassImp(GL_TABLES); ClassImp(GL_TRK_CALIB); ClassImp(GL_RUN); ClassImp(GL_ROOT); @@ -28,6 +29,98 @@ // using namespace std; +GL_TABLES::GL_TABLES(){ +}; + +GL_TABLES::GL_TABLES(TString host, TString user, TString psw){ + fHost = new TString(host.Data()); + fUser = new TString(user.Data()); + fPsw = new TString(psw.Data()); + fSet = true; + fNquery = 0; +}; + + +void GL_TABLES::Set(TString host, TString user, TString psw){ + fHost = new TString(host.Data()); + fUser = new TString(user.Data()); + fPsw = new TString(psw.Data()); + fSet = true; + fNquery = 0; +}; + +Bool_t GL_TABLES::IsConnected(TSQLServer *dbc){ + // + // + // + if ( !fSet ){ + return true; + }; + // + // + // + // printf(" host is %s \n",fHost->Data()); + // + stringstream myquery; + myquery.str(""); + myquery << "show databases;"; + dbc->Query(myquery.str().c_str()); + fNquery++; + // + if ( dbc->GetErrorCode() != 2013 && dbc->GetErrorCode() != 2006 ){ + // is connected + return true; + } else { + // + printf(" WARNING: Lost connection to DB! try to recover... \n"); + // + TString host = fHost->Data(); + TString user = fUser->Data(); + TString psw = fPsw->Data(); + delete dbc; + dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data()); + // + myquery.str(""); + myquery << "show databases;"; + dbc->Query(myquery.str().c_str()); + fNquery++; + // + if ( dbc->GetErrorCode() != 2013 && dbc->GetErrorCode() != 2006 ){ + // + printf(" ...connection recovered, I can continue! \n"); + // + myquery.str(""); + myquery << "SET time_zone='+0:00'"; + dbc->Query(myquery.str().c_str()); + fNquery++; + myquery.str(""); + myquery << "SET wait_timeout=173000;"; + dbc->Query(myquery.str().c_str()); + fNquery++; + return true; + }; + }; + // + printf(" GLTABLES: connection is gone away, query will fail\n"); + // + return false; + // +}; + +void GL_TABLES::ResetCounters(){ + fNquery = 0; +}; + +void GL_TABLES::AddQ(){ + if ( fSet ) fNquery++; +}; + +UInt_t GL_TABLES::GetNqueries(){ + UInt_t rn = 0; + rn += (UInt_t&)fNquery; + return(rn); +}; + GL_RUN::GL_RUN() { ID = 0; ID_RUN_FRAG = 0; @@ -425,12 +518,16 @@ // // printf("1myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // // retrieve this ID_TRASH // myquery.str(""); myquery << " SELECT ID_TRASH,ID_ROOT_L0,ID_ROOT_L2 FROM GL_RUN_TRASH ORDER BY ID_TRASH DESC LIMIT 1"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // UInt_t idtrash = 0; @@ -452,6 +549,8 @@ // // printf("2myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // Row = pResult->Next(); @@ -467,6 +566,8 @@ // // printf("3myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // Row = pResult->Next(); @@ -483,6 +584,8 @@ // // printf("4myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // myquery.str(""); @@ -492,6 +595,8 @@ // // printf("4myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // myquery.str(""); @@ -502,6 +607,8 @@ // printf("4myquery is %s \n",myquery.str().c_str()); // // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // myquery.str(""); @@ -512,6 +619,8 @@ // printf("5myquery is %s \n",myquery.str().c_str()); // // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // return 0; @@ -543,6 +652,8 @@ // printf(" qui? myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // Row = pResult->Next(); @@ -626,6 +737,8 @@ // // printf("5myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // // @@ -633,6 +746,8 @@ myquery << " DELETE FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID="; myquery << IDRUN << ";"; // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // return 0; @@ -715,6 +830,8 @@ // // printf("myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // return 0; @@ -791,6 +908,8 @@ // // printf("myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // return 0; @@ -848,6 +967,8 @@ myquery << " from GL_RUN where ID=" << run << ";"; // // printf(" myquery is %s \n",myquery.str().c_str()); + if ( !this->IsConnected(dbc) ) return -57; + this->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // // printf(" getrowcount %i \n",pResult->GetRowCount()); @@ -946,6 +1067,8 @@ // // printf(" query is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())return(-50); for( r=0; r < 1000; r++){ @@ -1011,6 +1134,8 @@ myquery << ",NAME"; myquery << " from GL_ROOT where ID=" << id << ";"; // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())return (-51); for( r=0; r < 1000; r++){ @@ -1020,7 +1145,7 @@ 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 = Row->GetField(t); + if(t==3) PATH = (TString)gSystem->ExpandPathName(Row->GetField(t))+'/'; if(t==4) NAME = Row->GetField(t); }; }; @@ -1045,8 +1170,11 @@ // ---------------- myquery.str(""); myquery << "select ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,OBT2,PKT1,PKT2,BOOT_NUMBER,VALIDATION from GL_TRK_CALIB where FROM_TIME <= "<< time; - myquery << " AND VALIDATION=1 ORDER BY FROM_TIME DESC LIMIT 1;"; +// myquery << " AND VALIDATION=1 ORDER BY FROM_TIME DESC LIMIT 1;"; // RIVEDERE LA VALIDAZIONE!!! + myquery << " ORDER BY FROM_TIME DESC LIMIT 1;"; // myquery << " ORDER BY FROM_TIME DESC LIMIT 1;"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())return (-53); for( r=0; r < 1000; r++){ @@ -1104,6 +1232,8 @@ myquery << " ;"; //myquery << " and VALIDATION=1;"; // + 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()); // @@ -1123,6 +1253,8 @@ 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; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // printf(" mysquery is %s\n",myquery.str().c_str()); // @@ -1165,6 +1297,8 @@ myquery << "select * from GL_S4_CALIB where FROM_TIME <= "<< time; myquery << " ORDER BY FROM_TIME DESC LIMIT 1;"; // myquery << " ORDER BY FROM_TIME DESC LIMIT 1;"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())return (-55);//throw -55; for( r=0; r < 1000; r++){ @@ -1205,12 +1339,14 @@ // ---------------- myquery.str(""); myquery << " select "; - myquery << " ID, PATH, NAME, DESCR, TYPE, FROM_TIME,TO_TIME "; + myquery << " ID, PATH, NAME, DESCR, FROM_TIME,TO_TIME, TYPE "; myquery << " from GL_PARAM "; myquery << " where TYPE = '"<GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())return (-52); for( r=0; r < 1000; r++){ @@ -1218,7 +1354,7 @@ 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 = Row->GetField(t);// put in fpath the path to that file + 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)); @@ -1264,6 +1400,9 @@ TSQLRow *Row; stringstream myquery; stringstream rname; + // pcksList packetsNames; + // pcksList::iterator Iter; + // getPacketsNames(packetsNames); rname.str(""); // ---------------- myquery.str(""); @@ -1274,13 +1413,15 @@ myquery << type.Data(); myquery << "=" << id << ";"; // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if( pResult->GetRowCount() ){ Row = pResult->Next(); if( Row ){ stringstream fname; fname.str(""); - fname << Row->GetField(0) << "/" << Row->GetField(1); + 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)); @@ -1297,6 +1438,27 @@ 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(); +// // +// }; +// }; + // }; // // look for Resurs offset @@ -1305,9 +1467,13 @@ // // 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='" - << rname.str().c_str() << "';"; + << frn.Data() << "';"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(oss.str().c_str()); Row = pResult->Next(); // @@ -1326,13 +1492,20 @@ for (Int_t i = 8; i<13; i++){ dwo.Append(name[i],1); }; - if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi(); + if ( dwo.IsDigit() ){ + dworbit = (UInt_t)dwo.Atoi(); + } else { + dworbit = 1; + }; }; // + // 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 FROM_ORBIT< " << dworbit << " order by FROM_ORBIT desc limit 1;"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(oss.str().c_str()); Row = pResult->Next(); if ( !Row ){ @@ -1350,6 +1523,8 @@ myquery << " SELECT OBT0,TIMESYNC,TYPE FROM GL_TIMESYNC " << " WHERE ID_RAW = " << idraw << ";"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if ( pResult ){ Row = pResult->Next(); @@ -1635,6 +1810,8 @@ dbc->Query("SET time_zone = '+0:00'"); // Do the query + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); result = dbc->Query(myquery.Data()); if(! result->GetRowCount() ) { cerr << "GL_TLE: query failed: " << myquery.Data() << endl;