--- YodaProfiler/src/GLTables.cpp 2007/03/19 16:14:18 1.24 +++ YodaProfiler/src/GLTables.cpp 2008/03/07 10:35:15 1.38 @@ -24,6 +24,7 @@ ClassImp(GL_PARAM); ClassImp(GL_S4_CALIB); ClassImp(GL_CALO_CALIB); +ClassImp(GL_CALOPULSE_CALIB); ClassImp(GL_TIMESYNC); ClassImp(GL_TLE); // @@ -38,6 +39,9 @@ fPsw = new TString(psw.Data()); fSet = true; fNquery = 0; + mh = host.Data(); + mu = user.Data(); + mp = psw.Data(); }; @@ -47,9 +51,12 @@ fPsw = new TString(psw.Data()); fSet = true; fNquery = 0; + mh = host.Data(); + mu = user.Data(); + mp = psw.Data(); }; -Bool_t GL_TABLES::IsConnected(TSQLServer *dbc){ +Bool_t GL_TABLES::IsConnected(TSQLServer *&dbc){ // // // @@ -57,26 +64,35 @@ return true; }; // - // - // - // printf(" host is %s \n",fHost->Data()); + // printf(" host is %s \n",fHost->Data()); // stringstream myquery; myquery.str(""); myquery << "show databases;"; - dbc->Query(myquery.str().c_str()); - fNquery++; + if ( dbc ){ + if ( dbc->IsConnected() ){ + dbc->Query(myquery.str().c_str()); + fNquery++; + if ( !(dbc->GetErrorCode()) ){ + // printf("ok\n"); + // + // if ( (dbc->GetErrorCode() != 2013 && dbc->GetErrorCode() != 2006) ){ + // if ( !(dbc->GetErrorCode()) ){ + // is connected + return true; + // }; + }; + }; + }; // - if ( dbc->GetErrorCode() != 2013 && dbc->GetErrorCode() != 2006 ){ - // is connected - return true; - } else { + if ( true ) { // printf(" WARNING: Lost connection to DB! try to recover... \n"); // TString host = fHost->Data(); TString user = fUser->Data(); TString psw = fPsw->Data(); + dbc->Close(); delete dbc; dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data()); // @@ -84,8 +100,8 @@ myquery << "show databases;"; dbc->Query(myquery.str().c_str()); fNquery++; - // - if ( dbc->GetErrorCode() != 2013 && dbc->GetErrorCode() != 2006 ){ + // if ( dbc->GetErrorCode() != 2013 && dbc->GetErrorCode() != 2006 ){ + if ( !(dbc->GetErrorCode()) ){ // printf(" ...connection recovered, I can continue! \n"); // @@ -156,7 +172,7 @@ } -void GL_RUN::Clear() { +void GL_RUN::Clear(Option_t *t) { ID = 0; ID_RUN_FRAG = 0; ID_ROOT_L0 = 0; @@ -231,6 +247,25 @@ FROM_TIME = 0; TO_TIME = 0; SECTION = 0; + OBT = 0; + PKT = 0; + BOOT_NUMBER = 0; + VALIDATION = 0; +} + +GL_CALOPULSE_CALIB::GL_CALOPULSE_CALIB(){ + ID = 0; + ID_ROOT_L0 = 0; + EV_ROOT = 0; + FROM_TIME = 0; + TO_TIME = 0; + SECTION = 0; + PULSED_STRIP = 0; + PULSE_AMPLITUDE = 0; + OBT = 0; + PKT = 0; + BOOT_NUMBER = 0; + VALIDATION = 0; } GL_S4_CALIB::GL_S4_CALIB(){ @@ -239,16 +274,17 @@ EV_ROOT = 0; FROM_TIME = 0; TO_TIME = 0; - PARAM_FIT0 = 0.; - PARAM_FIT1 = 0.; + OBT = 0; + PKT = 0; + BOOT_NUMBER = 0; } GL_TIMESYNC::GL_TIMESYNC(){ obtfirst = 0; pktfirst = 0; - toffset = 0; - // ID = 0; - // ID_RAW = 0; + ID_RESURS_OFFSET = 0; + ID = 0; + ID_RAW = 0; OBT0 = 0; TIMESYNC = 0; TYPE = 0; @@ -1145,7 +1181,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); }; }; @@ -1171,6 +1207,7 @@ 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;"; // RIVEDERE LA VALIDAZIONE!!! + myquery << " and EV_ROOT_CALIBTRK1>=0 and EV_ROOT_CALIBTRK2>=0 "; // EM! SE MANCA UN PACCHETTO DEI DUE IL PROCESSAMENTO CRASHA... DA 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; @@ -1243,6 +1280,9 @@ // uptime = (UInt_t)atoll(Row->GetField(2)); // + UInt_t myfromtime = (UInt_t)atoll(Row->GetField(1)); + UInt_t mytotime = (UInt_t)atoll(Row->GetField(2)); + // // if it is corrupted validation is 0 and we have no results from the query... // if( atoi(Row->GetField(4)) == 0 ){ // if validation = 0 @@ -1264,6 +1304,28 @@ // Row = pResult->Next(); // + myfromtime = (UInt_t)atoll(Row->GetField(1)); + // + }; + // + // if the selected calibration is too old (more than 5 orbits old) try to take the closest not corrupted one + // + if ( (time-myfromtime)>28500 ){ + // + 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; + 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); + // + Row = pResult->Next(); + // }; // // store infos and exit @@ -1271,6 +1333,52 @@ if( Row == NULL ) 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(); + return 0; +}; + + +// **************************************************** +/** + * Function to query the GL_CALOPULSE_CALIB table of the DB. + * + * \param run starting time + * \return struct of type GL_CALOPULSE_CALIB_data, which stores the query result + */ +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; + int t; + stringstream myquery; + // + // select the correct calibration i.e. the closest to our time + // + myquery.str(""); + myquery << "select ID_ROOT_L0, FROM_TIME, TO_TIME, EV_ROOT from GL_CALOPULSE_CALIB where SECTION=" << section; + myquery << " and PULSE_AMPLITUDE=" << pampli; + myquery << " and VALIDATION=1 and (PULSED_STRIP IS NULL OR PULSED_STRIP<100) ORDER BY ABS(" << time << "-FROM_TIME) asc limit 1;"; + // printf(" myq 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 ) return(-54); + // + Row = pResult->Next(); + // + if( !Row ) return (-54); + // + // store infos and exit + // + for( t = 0; t < pResult->GetFieldCount(); t++){ + // printf(" field %i is %s \n",t,Row->GetField(t)); + if (t==0) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t)); 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)); @@ -1278,6 +1386,8 @@ pResult->Delete(); return 0; }; + + // **************************************************** /** * Function to query the GL_S4_CALIB table of the DB. @@ -1294,7 +1404,7 @@ stringstream myquery; // ---------------- myquery.str(""); - myquery << "select * from GL_S4_CALIB where FROM_TIME <= "<< time; + myquery << "select ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME 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; @@ -1306,12 +1416,10 @@ 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_ROOT_L0 = (UInt_t)atoll(Row->GetField(t)); - if (t==2) EV_ROOT = (UInt_t)atoll(Row->GetField(t)); - if (t==3) FROM_TIME = (UInt_t)atoll(Row->GetField(t)); - if (t==4) TO_TIME = (UInt_t)atoll(Row->GetField(t)); - if (t==5) PARAM_FIT0 = atof(Row->GetField(t)); - if (t==6) PARAM_FIT1 = atof(Row->GetField(t)); + if (t==1) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t)); + if (t==2) EV_ROOT = (UInt_t)atoll(Row->GetField(t)); + if (t==3) FROM_TIME = (UInt_t)atoll(Row->GetField(t)); + if (t==4) TO_TIME = (UInt_t)atoll(Row->GetField(t)); }; }; delete pResult; @@ -1339,7 +1447,7 @@ // ---------------- 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 = '"<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)); @@ -1467,9 +1575,11 @@ // // 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()); @@ -1478,37 +1588,76 @@ TString name=rname.str().c_str(); UInt_t dworbit = 0; Int_t nlength = name.Length(); - 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; - }; - }; // + // Is not a special file // 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 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 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()); - Row = pResult->Next(); - if ( !Row ){ - printf(" ERROR FROM GLTables! cannot determine Resurs offset \n"); - return; + 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 + // + 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; + }; + }; + // + 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 ){ + printf(" ERROR FROM GLTables! cannot determine Resurs offset \n"); + return; + }; }; }; // @@ -1583,7 +1732,9 @@ */ Long64_t GL_TIMESYNC::DBobt(UInt_t obt){ // - if ( obt < (obtfirst/2) && obtfirst > (numeric_limits::max()/2) ) return((Long64_t)(obt+numeric_limits::max())); + if ( obt < (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) ){ return((Long64_t)obt-(Long64_t)numeric_limits::max()); @@ -1805,10 +1956,10 @@ TSQLRow *row; // Set the right time_zone (otherwise horrible things will occur! :) + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; 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() ) {