--- chewbacca/YodaProfiler/src/GLTables.cpp 2008/12/12 11:07:09 1.6 +++ chewbacca/YodaProfiler/src/GLTables.cpp 2009/06/15 11:20:33 1.8 @@ -92,8 +92,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(""); @@ -1652,7 +1654,121 @@ // // printf(" T0 %u toffset is %u \n",T0,toffset); // - file->Close(); + if ( file ) file->Close(); + 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 = 0; + 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() ){ + 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 ){ + 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) ) 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 ( !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(); delete pResult; };