--- YodaProfiler/src/PamelaDBOperations.cpp 2006/09/11 14:04:51 1.7 +++ YodaProfiler/src/PamelaDBOperations.cpp 2006/10/31 15:36:04 1.16 @@ -31,11 +31,18 @@ #include #include // +#include + #include // using namespace std; using namespace pamela; +// Some function to work with cTle stuff. +bool compTLE(cTle* tle1, cTle *tle2); +float getTleJulian(cTle *); +string getTleDatetime(cTle*); + /** * Constructor. * @param host hostname for the SQL connection. @@ -47,8 +54,9 @@ * @param obt0 file obt0. * @param tsync file timesync. * @param debug debug flag. + * @param tlefilename ascii file with TLE 3 line elements. */ -PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug){ +PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename){ // // SetConnection(host,user,password); @@ -62,14 +70,26 @@ SetTsync(tsync); SetObt0(obt0); // + SetTLEPath(tlefilename); // - SetRootName(filerootname); - SetRawName(filerawname); // - this->OpenFile(); + INSERT_RAW =!filerawname.IsNull(); + if(INSERT_RAW)SetRawName(filerawname); + // + INSERT_ROOT = !filerootname.IsNull(); + if( INSERT_ROOT ){ + this->SetRootName(filerootname); + this->SetOrbitNo(); + file = TFile::Open(this->GetRootName().Data()); + } else { + this->SetRootName(""); + }; // this->SetID_RAW(0); this->SetID_ROOT(0); + + VALIDATE = false; + // }; @@ -78,6 +98,7 @@ */ void PamelaDBOperations::Close(){ if( conn && conn->IsConnected() ) conn->Close(); + delete clean_time; delete glrun; delete this; }; @@ -86,6 +107,19 @@ // SETTERS // +// +// must be out of the constructor in order to FORCE the validation of the latest runs in case you run the validation together with the latest file +// +void PamelaDBOperations::CheckValidate(Long64_t olderthan){ + clean_time = new TDatime(); + if(olderthan >= 0){ + VALIDATE = true; + UInt_t timelim = 0; + timelim = (UInt_t)clean_time->Convert() - olderthan; + clean_time->Set(timelim,false); + }; +}; + /** * Open the DB connection * @param host hostname for the SQL connection. @@ -93,6 +127,7 @@ * @param password password for the SQL connection. */ void PamelaDBOperations::SetConnection(TString host, TString user, TString password){ + if ( IsDebug() ) printf(" Connecting using host = %s user = %s password = %s \n",host.Data(),user.Data(),password.Data()); conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data()); }; @@ -121,6 +156,14 @@ }; /** + * Set the nofrag flag + * + */ +void PamelaDBOperations::SetNoFrag(Bool_t nf){ + NOFRAG = nf; +}; + +/** * Store the BOOT number of the RAW file. * @param boot BOOT number of the RAW file */ @@ -161,6 +204,33 @@ }; /** + * Store the downlink orbit number from filename. + */ +void PamelaDBOperations::SetOrbitNo(){ + dworbit = 0; + TString name = this->GetRootFile(); + 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(); + }; + if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data()); + return; +}; + + + +/** * Store the NOBOOT flag. * @param noboot true/false. */ @@ -169,12 +239,19 @@ }; /** + * Store path to the TLE file. + */ +void PamelaDBOperations::SetTLEPath(TString str){ + tlefilename = str; +}; + +/** * Store the olderthan variable * @param olderthan */ -void PamelaDBOperations::SetOlderThan(Long64_t oldthan){ - olderthan = oldthan; -}; +// void PamelaDBOperations::SetOlderThan(Long64_t oldthan){ +// olderthan = oldthan; +// }; /** * Retrieve the ID_RAW, if exists, returns NULL if does not exist. @@ -225,6 +302,9 @@ UInt_t jump = 50000; // was 5000 EventCounter *code=0; // + UInt_t deltapkt = 5000; + ULong64_t deltaobt = 50000; + // // pcksList packetsNames; // pcksList::iterator Iter; // getPacketsNames(packetsNames); @@ -271,7 +351,8 @@ // if ( nevent < 2 ) return(4); // - if ( PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst) ){ + if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))Query(oss.str().c_str()); + if ( !result ) throw -10; + oss.str(""); + oss << "SELECT ID FROM _RUNID_GEN ORDER BY ID DESC LIMIT 1;"; + result = conn->Query(oss.str().c_str()); + if ( !result ) throw -10; + // + row = result->Next(); + // + if ( !row ) throw -28; + // + runid = (UInt_t)atoll(row->GetField(0)); + // + return(runid); +}; + // // GETTERS // @@ -628,26 +741,29 @@ // PRIVATE FUNCTIONS // -/** - * Open the ROOT filename for reading - */ -void PamelaDBOperations::OpenFile(){ - file = TFile::Open(this->GetRootName().Data()); +// /** +// * Open the ROOT filename for reading +// */ +// void PamelaDBOperations::OpenFile(){ +// file = TFile::Open(this->GetRootName().Data()); +// // + +void PamelaDBOperations::CheckFile(){ + if ( !file ) throw -12; }; /** * Check if LEVEL0 file and DB connection have really be opened */ -void PamelaDBOperations::CheckFile(){ - // - if ( !file ) throw -12; +void PamelaDBOperations::CheckConnection(){ // // check connection // - if( !conn ) throw -1; + if( !conn ) throw -1; bool connect = conn->IsConnected(); - if( !connect ) throw -1; + if( !connect ) throw -1; + if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27; }; /** @@ -827,14 +943,23 @@ if ( this->GetID_RAW() == 0 ) throw -11; // oss.str(""); - oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ID_RAW<= " - << id << " AND TO_ID_RAW >= " - << id << ";"; + oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='" + << this->GetRawFile().Data() << "';"; if ( IsDebug() ) printf(" %s \n",oss.str().c_str()); result = conn->Query(oss.str().c_str()); if ( !result ) throw -10; row = result->Next(); - if ( !row ) throw -10; + // + if ( !row ){ + oss.str(""); + oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< " + << dworbit << " order by FROM_ORBIT desc limit 1;"; + if ( IsDebug() ) printf(" %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + if ( !result ) throw -10; + row = result->Next(); + if ( !row ) throw -10; + }; // t0 = (UInt_t)TDatime(row->GetField(0)).Convert(); /* @@ -908,6 +1033,8 @@ // TYPE = 55;//224; // + if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT); + // if ( TSYNC && OBT ){ existsts = true; goto eout; @@ -930,35 +1057,49 @@ // rt->SetBranchAddress("RunTrailer", &runt); // - if ( rhev > 0 ){ - rh->GetEntry(0); - // - TSYNC = runh->LAST_TIME_SYNC_INFO; - OBT = runh->OBT_TIME_SYNC * 1000; - // - TYPE = 20; - // - if ( TSYNC && OBT ){ - existsts = true; - goto eout; + Int_t nrhev = rh->GetEntries(); + Int_t nrtev = rt->GetEntries(); + if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev); + // + if ( nrhev > 0 ){ + for (Int_t i=0; iGetEntry(i); + // + TSYNC = runh->LAST_TIME_SYNC_INFO; + OBT = runh->OBT_TIME_SYNC * 1000; + // + TYPE = 20; + // + if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT); + // + if ( TSYNC && OBT ){ + existsts = true; + goto eout; + }; }; // }; - if ( rtev > 0 ){ + if ( nrtev > 0 ){ // if ( IsDebug() ) printf(" No runheader \n"); signal = 6; // - rt->GetEntry(0); - // - TSYNC = runt->LAST_TYME_SYNC_INFO; - OBT = runt->OBT_TYME_SYNC * 1000; - // - TYPE = 21; - // - if ( TSYNC && OBT ){ - existsts = true; - goto eout; + for (Int_t i=0; iGetEntry(i); + // + TSYNC = runt->LAST_TYME_SYNC_INFO; + OBT = runt->OBT_TYME_SYNC * 1000; + // + TYPE = 21; + // + if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT); + // + if ( TSYNC && OBT ){ + existsts = true; + goto eout; + }; }; // } else { @@ -1425,10 +1566,11 @@ if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){ // if ( IsDebug() ) printf(" The new run has more events than the old one \n"); - oss.str(""); - oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";"; - if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str()); - conn->Query(oss.str().c_str()); + glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN"); +// oss.str(""); +// oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";"; +// if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str()); +// conn->Query(oss.str().c_str()); if ( signal ) signal = false; goto gonext; // @@ -1442,10 +1584,11 @@ // if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n"); // - oss.str(""); - oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";"; - if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str()); - conn->Query(oss.str().c_str()); + glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN"); +// oss.str(""); +// oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";"; +// if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str()); +// conn->Query(oss.str().c_str()); // if ( signal ) signal = false; goto gonext; @@ -1459,10 +1602,11 @@ // if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n"); // - oss.str(""); - oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";"; - if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str()); - conn->Query(oss.str().c_str()); + glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN"); +// oss.str(""); +// oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";"; +// if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str()); +// conn->Query(oss.str().c_str()); if ( signal ) signal = false; // }; @@ -1498,7 +1642,11 @@ // this->FillClass(); // - if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn); + if ( !IsRunAlreadyInserted() ){ + glrun->SetID(this->AssignRunID()); + glrun->SetID_RUN_FRAG(0); + glrun->Fill_GL_RUN(conn); + }; } else { // if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt); @@ -1521,6 +1669,7 @@ UInt_t rhfirstev = firstev; UInt_t rtlastev = lastev; Bool_t found = false; + Bool_t foundinrun = false; // TSQLResult *result = 0; TSQLRow *row = 0; @@ -1547,6 +1696,47 @@ // if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n"); // + // First of all insert the run in the fragment table... + // + oss.str(""); + oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE " + << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND (" + << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND " + << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND (" + << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR " + << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND (" + << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR " + << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR " + << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " + << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND (" + << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR " + << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND (" + << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR " + << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));"; + // + if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + // + if ( !result ) throw -4; + // + row = result->Next(); + // + if ( !row ){ + // + // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!) + // + if ( IsDebug() ) printf(" The run is new \n"); + if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n"); + // + glrun->SetID(this->AssignRunID()); + glrun->SetID_RUN_FRAG(0); + glrun->Fill_GL_RUN_FRAGMENTS(conn); + // + } else { + if ( IsDebug() ) printf(" The run is already present in the fragment table \n"); + }; + // + // // can we find the other piece of the run in the GL_RUN_FRAGMENTS table? // if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is @@ -1555,7 +1745,8 @@ oss.str(""); oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE " << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND " - << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() + << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " + << " ID != " << glrun->ID << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!! // if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str()); @@ -1565,6 +1756,26 @@ // row = result->Next(); // + if ( !row && NoFrag() ){ + // + oss.str(""); + oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE " + << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND " + << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " + << " ID != " << glrun->ID + << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!! + // + if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + // + if ( !result ) throw -4; + // + foundinrun = true; + // + row = result->Next(); + // + }; + // if ( !row ){ if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n"); found = false; @@ -1639,9 +1850,14 @@ // if ( IsDebug() ) printf(" now you can handle the piece of the run \n "); // + if ( foundinrun ){ + glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS"); + glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN"); + }; + // GL_RUN *glrun1 = new GL_RUN(); // - UInt_t idfrag = (UInt_t)atoll(row->GetField(0)); + // UInt_t idfrag = (UInt_t)atoll(row->GetField(0)); // oss.str(""); oss << " ID="<GetField(0)<<";"; @@ -1670,7 +1886,6 @@ }; if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev); // - glrun1->SetID(0); glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER()); glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER()); glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME()); @@ -1679,6 +1894,7 @@ // glrun->SetEV_FROM(firstev); glrun->SetNEVENTS(lastev-firstev+1); + // glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME()); glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT()); glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT()); @@ -1698,67 +1914,23 @@ // if ( !IsRunAlreadyInserted() ){ // + // glrun->SetID(this->AssignRunID()); + glrun->SetID_RUN_FRAG(glrun1->GetID()); glrun->Fill_GL_RUN(conn); // - // get id number - // - oss.str(""); - oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<GetBOOTnumber()<<" AND " - << " RUNHEADER_OBT="<GetRUNHEADER_OBT()<<" AND " - << " RUNTRAILER_OBT="<GetRUNTRAILER_OBT()<<";"; - // - if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str()); - // - result = conn->Query(oss.str().c_str()); - if ( !result ) throw -4; - row = result->Next(); - // - UInt_t idrun0 = 0; - if ( !row ){ - throw -10; - } else { - idrun0 = (UInt_t)atoll(row->GetField(0)); - }; - // - glrun1->SetID_RUN_FRAG(idrun0); + // set id number // + glrun1->SetID_RUN_FRAG(glrun->GetID()); glrun1->Fill_GL_RUN(conn); // - oss.str(""); - oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<Query(oss.str().c_str()); - if ( !result ) throw -4; - row = result->Next(); - // - UInt_t idrun1 = 0; - if ( !row ){ - throw -10; - } else { - idrun1 = (UInt_t)atoll(row->GetField(0)); - }; - // - oss.str(""); - oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<Query(oss.str().c_str()); - if ( !result ) throw -4; - // }; - // - delete glrun1; - // // delete old entry in fragment table // - oss.str(""); + glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); + glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); // - oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";"; + delete glrun1; // - if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str()); - result = conn->Query(oss.str().c_str()); - if ( !result ) throw -4; // return; // @@ -1767,12 +1939,13 @@ }; // if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is - // missing it no way we can found a piece in the frag table + // missing it no way we can found a piece in the frag table // oss.str(""); oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE " << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND " - << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() + << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND " + << " ID != " << glrun->ID << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;"; // if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str()); @@ -1782,6 +1955,25 @@ // row = result->Next(); // + if ( !row && NoFrag() ){ + // + oss.str(""); + oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE " + << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND " + << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND " + << " ID != " << glrun->ID + << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;"; + // + if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + // + if ( !result ) throw -4; + // + foundinrun = true; + row = result->Next(); + // + }; + // if ( !row ){ if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n"); found = false; @@ -1855,9 +2047,14 @@ // if ( IsDebug() ) printf(" now you can handle the piece of the run \n "); // + if ( foundinrun ){ + glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS"); + glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN"); + }; + // GL_RUN *glrun1 = new GL_RUN(); // - UInt_t idfrag = (UInt_t)atoll(row->GetField(0)); + // UInt_t idfrag = (UInt_t)atoll(row->GetField(0)); // oss.str(""); oss << " ID="<GetField(0)<<";"; @@ -1894,7 +2091,6 @@ glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT()); glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT()); // - glrun1->SetID(0); glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME()); glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT()); glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT()); @@ -1914,68 +2110,24 @@ // if ( !IsRunAlreadyInserted() ){ // - glrun->Fill_GL_RUN(conn); - // - // get id number - // - oss.str(""); - oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<GetBOOTnumber()<<" AND " - << " RUNHEADER_OBT="<GetRUNHEADER_OBT()<<" AND " - << " RUNTRAILER_OBT="<GetRUNTRAILER_OBT()<<";"; - // - if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str()); - // - result = conn->Query(oss.str().c_str()); - if ( !result ) throw -4; - row = result->Next(); + // glrun->SetID(this->AssignRunID()); // - UInt_t idrun0 = 0; - if ( !row ){ - throw -10; - } else { - idrun0 = (UInt_t)atoll(row->GetField(0)); - }; + glrun->SetID_RUN_FRAG(glrun1->GetID()); + glrun->Fill_GL_RUN(conn); // - glrun1->SetID_RUN_FRAG(idrun0); + // set id number // + glrun1->SetID_RUN_FRAG(glrun->GetID()); glrun1->Fill_GL_RUN(conn); // - oss.str(""); - oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<Query(oss.str().c_str()); - if ( !result ) throw -4; - row = result->Next(); - // - UInt_t idrun1 = 0; - if ( !row ){ - throw -10; - } else { - idrun1 = (UInt_t)atoll(row->GetField(0)); - }; - // - oss.str(""); - oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<Query(oss.str().c_str()); - if ( !result ) throw -4; - // }; // - delete glrun1; - // - // delete old entry in fragment table - // - oss.str(""); - // - oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";"; + // delete old entries in fragment table // - if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str()); - result = conn->Query(oss.str().c_str()); - if ( !result ) throw -4; + glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); + glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); // + delete glrun1; // return; // @@ -2012,45 +2164,14 @@ // row = result->Next(); // - if ( !row ){ - // - oss.str(""); - oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE " - << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND (" - << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND " - << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND (" - << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR " - << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND (" - << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR " - << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR " - << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " - << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND (" - << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR " - << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND (" - << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR " - << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));"; - // - if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str()); - result = conn->Query(oss.str().c_str()); - // - if ( !result ) throw -4; - // - row = result->Next(); - // - if ( !row ){ - // - // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!) - // - if ( IsDebug() ) printf(" The run is new \n"); - if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n"); - // - glrun->Fill_GL_RUN_FRAGMENTS(conn); - // - } else { - if ( IsDebug() ) printf(" The run is already present in the fragment table \n"); - }; - } else { + if ( row ){ if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n"); + } else { + if ( NoFrag() ){ + glrun->SetID_RUN_FRAG(glrun->GetID()); + glrun->Fill_GL_RUN(conn); + glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); + }; }; }; // @@ -2076,7 +2197,11 @@ // this->FillClass(mishead,mistrail,firstev,lastev); // - if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn); + if ( !IsRunAlreadyInserted() ){ + glrun->SetID(this->AssignRunID()); + glrun->SetID_RUN_FRAG(0); + glrun->Fill_GL_RUN(conn); + }; // }; // @@ -2243,7 +2368,11 @@ // this->SetCommonGLRUN(firstTime,lastTime); // - if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn); + if ( !IsRunAlreadyInserted() ){ + glrun->SetID(this->AssignRunID()); + glrun->SetID_RUN_FRAG(0); + glrun->Fill_GL_RUN(conn); + }; // firstevno = lastentry + 1; // @@ -2319,7 +2448,11 @@ if ( IsDebug() ) printf(" Checking but no events in the run! \n"); // this->FillClass(); - if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn); + if ( !IsRunAlreadyInserted() ){ + glrun->SetID(this->AssignRunID()); + glrun->SetID_RUN_FRAG(0); + glrun->Fill_GL_RUN(conn); + }; // } else { // @@ -2347,7 +2480,11 @@ if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n"); // this->FillClass(); - if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn); + if ( !IsRunAlreadyInserted() ){ + glrun->SetID(this->AssignRunID()); + glrun->SetID_RUN_FRAG(0); + glrun->Fill_GL_RUN(conn); + }; // } else { // @@ -2445,7 +2582,11 @@ // this->SetCommonGLRUN(firstTime,lastTime); // - if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn); + if ( !IsRunAlreadyInserted() ){ + glrun->SetID(this->AssignRunID()); + glrun->SetID_RUN_FRAG(0); + glrun->Fill_GL_RUN(conn); + }; // if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check... // @@ -2480,7 +2621,11 @@ // this->SetCommonGLRUN(firstTime,lastTime); // - if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn); + if ( !IsRunAlreadyInserted() ){ + glrun->SetID(this->AssignRunID()); + glrun->SetID_RUN_FRAG(0); + glrun->Fill_GL_RUN(conn); + }; }; // firstevno = lastentry + 1; @@ -3161,6 +3306,13 @@ * Scan the fragment table and move old fragments to the GL_RUN table */ Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){ + return(this->CleanGL_RUN_FRAGMENTS("")); +}; + +/** + * Scan the fragment table and move old fragments to the GL_RUN table + */ +Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){ // TSQLResult *result = 0; TSQLRow *row = 0; @@ -3168,39 +3320,50 @@ TSQLRow *row2 = 0; // UInt_t moved = 0; - UInt_t timelim = 0; - TDatime *time = new TDatime(); // stringstream oss; oss.str(""); // - // - // - if ( olderthan < 0 ){ - if ( IsDebug() ) printf(" Do not clean GL_RUN_FRAGMENTS table \n"); - return(1); + if ( !strcmp(fcleanfile.Data(),"") ){ + // + // check if there are entries older than "olderthan" seconds from now + // + oss.str(""); + oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE" + << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';"; + // + if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + // + } else { + oss.str(""); + oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';"; + if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + // + if ( result ){ + // + row = result->Next(); + // + if ( row ){ + oss.str(""); + oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE" + << " ID_ROOT_L0=" << row->GetField(0) << ";"; + // + if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + // + }; + } else { + return(2); + }; }; // - // timelim = now - olderthan - // - time->Set(); - timelim = (UInt_t)time->Convert() - olderthan; - time->Set(timelim,false); - // - // check if there are entries older than "olderthan" seconds from now - // - oss.str(""); - oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE" - << " INSERT_TIME <= '" << time->AsSQLString() << "';"; - // - if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",time->AsSQLString(),oss.str().c_str()); - result = conn->Query(oss.str().c_str()); - // if ( result ){ // row = result->Next(); // - if ( row ){ + while ( row ){ // oss.str(""); oss << " ID= "<< row->GetField(0); @@ -3235,32 +3398,33 @@ if ( IsDebug() ) printf(" The run is new \n"); if ( IsDebug() ) printf(" -> fill the DB \n"); // - glrun->SetID(0); + // glrun->SetID(this->AssignRunID()); we use the old run number! + glrun->SetID_RUN_FRAG(glrun->GetID()); glrun->Fill_GL_RUN(conn); // - oss.str(""); - oss << " SELECT ID FROM GL_RUN WHERE " - << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND " - << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND " - << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND " - << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND " - << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; "; - // - if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str()); - result2 = conn->Query(oss.str().c_str()); - // - if ( !result2 ) throw -4; - // - row2 = result2->Next(); - // - if ( !row2 ) throw -25; - // - oss.str(""); - oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0); - if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str()); - result2 = conn->Query(oss.str().c_str()); - // - if ( !result2 ) throw -4; +// oss.str(""); +// oss << " SELECT ID FROM GL_RUN WHERE " +// << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND " +// << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND " +// << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND " +// << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND " +// << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; "; +// // +// if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str()); +// result2 = conn->Query(oss.str().c_str()); +// // +// if ( !result2 ) throw -4; +// // +// row2 = result2->Next(); +// // +// if ( !row2 ) throw -25; +// // +// oss.str(""); +// oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0); +// if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str()); +// result2 = conn->Query(oss.str().c_str()); +// // +// if ( !result2 ) throw -4; // moved++; // @@ -3270,13 +3434,15 @@ if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0)); // // - oss.str(""); - oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0); - if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str()); - result2 = conn->Query(oss.str().c_str()); - // - if ( !result2 ) throw -4; - // + glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS"); +// oss.str(""); +// oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0); +// if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str()); +// result2 = conn->Query(oss.str().c_str()); +// // +// if ( !result2 ) throw -4; +// // + row = result->Next(); }; }; if ( IsDebug() ) printf(" Moved %u runs\n",moved); @@ -3287,85 +3453,139 @@ * Check if runs are good, i.e. if the tracker calibration is correctly associated.. */ Int_t PamelaDBOperations::ValidateRuns(){ + return(this->ValidateRuns("")); +}; + +/** + * Check if runs are good, i.e. if the tracker calibration is correctly associated.. + */ +Int_t PamelaDBOperations::ValidateRuns(TString valfile){ // TSQLResult *result = 0; TSQLRow *row = 0; // - UInt_t timelim = 0; - TDatime *time = new TDatime(); + UInt_t calibtime = 50; // stringstream oss; oss.str(""); // - // - // - if ( olderthan < 0 ){ - if ( IsDebug() ) printf(" Do not validate runs \n"); - return(1); - }; - // - // timelim = now - olderthan - // - time->Set(); - timelim = (UInt_t)time->Convert() - olderthan; - time->Set(timelim,false); - - printf("Validate runs older than %s \n",time->AsSQLString()); - // ======================================================= // validate runs by checking missing calibrations // ======================================================= UInt_t t_stop = 0; UInt_t t_start = 0; - // -------------------------------------------------------------- - // 1) get the OBT of the last run inserted after clean-time limit - // -------------------------------------------------------------- - oss.str(""); - oss << " SELECT * FROM GL_RUN WHERE INSERT_TIME <= '" << time->AsSQLString() - << "' ORDER BY INSERT_TIME DESC LIMIT 1;"; - printf(" Get start validation-time: query is \n %s \n",oss.str().c_str()); - result = conn->Query(oss.str().c_str()); - if ( !result ) throw -4; - if ( !result->GetRowCount() ) { - printf(" No runs to validate \n"); - return(1); - }else{ - row = result->Next(); - t_start = (UInt_t)atoll(row->GetField(4)); - printf("t_start %i\n",t_start); - }; - // -------------------------------------------------------------- - // 2) get the OBT of the last validated run - // -------------------------------------------------------------- - oss.str(""); - oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start - <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; - printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str()); - result = conn->Query(oss.str().c_str()); - if ( !result ) throw -4; - if ( result->GetRowCount() ){ - row = result->Next(); - t_stop = (UInt_t)atoll(row->GetField(4)); + if ( !strcmp(valfile.Data(),"") ) { + // -------------------------------------------------------------- + // 1) get the OBT of the last run inserted after clean-time limit + // -------------------------------------------------------------- + oss.str(""); + oss << " SELECT * FROM GL_RUN WHERE INSERT_TIME <= '" << clean_time->AsSQLString() + << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; + if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + if ( !result ) throw -4; + if ( !result->GetRowCount() ) { + printf(" No runs to validate \n"); + return(1); + }else{ + row = result->Next(); + t_start = (UInt_t)atoll(row->GetField(4)); + }; + // -------------------------------------------------------------- + // 2) get the OBT of the last validated run + // -------------------------------------------------------------- + oss.str(""); + oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start + <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; + if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + if ( !result ) throw -4; + if ( result->GetRowCount() ){ + row = result->Next(); + t_stop = (UInt_t)atoll(row->GetField(4)); + }; + if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start); + // -------------------------------------------------------------- + // now retrieves runs to be validated + // -------------------------------------------------------------- + oss.str(""); + oss << " SELECT * FROM GL_RUN WHERE RUNHEADER_TIME <=" << t_start; + oss << " AND RUNHEADER_TIME >="<< t_stop; + oss << " ORDER BY RUNHEADER_TIME DESC;"; + if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + } else { + // + stringstream myquery; + UInt_t myid = 0; + myquery.str(""); + myquery << " SELECT ID FROM GL_ROOT where NAME='"<Query(myquery.str().c_str()); + // + row = result->Next(); + if( !row ){ + if ( strcmp(valfile.Data(),GetRootName().Data()) ){ + if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n"); + return(2); + }; + if ( IsDebug() ) printf(" No file to be validated (force mode)! \n"); + return(0); + }; + myid=(UInt_t)atoll(row->GetField(0)); + // + myquery.str(""); + myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";"; + // + result = conn->Query(myquery.str().c_str()); + // + row = result->Next(); + if( !row->GetField(0) || !row->GetField(1)){ + // + if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n"); + // + return(0); + // + } else { + // + UInt_t runhtime = (UInt_t)atoll(row->GetField(0)); + UInt_t runttime = (UInt_t)atoll(row->GetField(1)); + UInt_t caltime = 0; + // + myquery.str(""); + myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <Query(myquery.str().c_str()); + // + row = result->Next(); + if( !row ){ + caltime = runhtime; + } else { + caltime = (UInt_t)atoll(row->GetField(0)); + }; + // + myquery.str(""); + myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ; + myquery << caltime << " order by RUNHEADER_TIME DESC"; + // + if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str()); + // + result = conn->Query(myquery.str().c_str()); + // + }; }; - printf("t_stop %i\n",t_stop); - // -------------------------------------------------------------- - // now retrieves runs to be validated - // -------------------------------------------------------------- - oss.str(""); - oss << " SELECT * FROM GL_RUN WHERE RUNHEADER_TIME <=" << t_start; - oss << " AND RUNHEADER_TIME >="<< t_stop; - oss << " ORDER BY RUNHEADER_TIME DESC;"; -// if ( IsDebug() ) - printf(" Check runs for validation: query is \n %s \n",oss.str().c_str()); - result = conn->Query(oss.str().c_str()); + // if ( !result ) throw -4; - if ( !result->GetRowCount() ) printf(" No runs to validate \n"); - printf("------------------------------------------------------------------------------- \n"); - + if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n"); + // Int_t nrow = 0; GL_RUN* this_run = new GL_RUN(); GL_RUN* next_run = new GL_RUN(); - Int_t nseq_max = 100; + Int_t nseq_max = 1000; // UInt_t* sequence = new UInt_t[100]; vector sequence(nseq_max); Int_t nseq = 0; @@ -3375,7 +3595,7 @@ UInt_t t1=0,t2=0; // --------------------------------------------------------------------------------- // - loop over runs, back in time, - // - select sequences of runs close in time (less than 60 s apart), + // - select sequences of runs close in time (less than calibtime s apart), // which could be preceeded by a calibration // - check if there might be a missing calibration // --------------------------------------------------------------------------------- @@ -3388,14 +3608,12 @@ //get run info //------------ this_run->Set_GL_RUN(row); - - printf(" RUN ID %i --- TRK_CALIB_USED %i --- RM_ACQ_AFTER_CALIB %i --- TIME %i %i \n",this_run->ID,this_run->TRK_CALIB_USED,this_run->RM_ACQ_AFTER_CALIB,this_run->RUNHEADER_TIME, this_run->RUNTRAILER_TIME); - + Bool_t this_BAD = false; if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true; else if (this_run->GetTRK_CALIB_USED() == 104) this_ONLINE = false; else{ - printf("Missing or corrupted header!! \n"); +// printf("Missing or corrupted header!! \n"); this_ONLINE = false; this_BAD = true; }; @@ -3417,11 +3635,7 @@ if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0; //=> run fragments - if( interval >= 60 )CHECK = true; //more than 60 s => there might be a calibration - -/* if( CHECK && !next_run->GetRM_ACQ_AFTER_CALIB() ) - printf(" ValidateRuns ***WARNING*** : DT = %i but RM_ACQ_AFTER_CALIB = %i \n",(t2-t1),next_run->GetRM_ACQ_AFTER_CALIB());*/ - + if( interval >= calibtime )CHECK = true; //more than calibtime s => there might be a calibration if( !CHECK && this_run->VALIDATION ){ for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true); @@ -3431,7 +3645,6 @@ }else if( !this_ONLINE && next_ONLINE) { // this: DEFAULT + next:ON-LINE CHECK = true; -// if( interval < 60 ) printf(" ValidateRuns ***WARNING*** : kkkkkk DT = %i \n",interval); }else if( !next_ONLINE ){ // this:ANY + next:DEFAULT @@ -3445,11 +3658,11 @@ //---------------------------- if( CHECK ){ // check if calibration exists - printf("DT %i ===> CHECK Missing calibration\n",interval); + if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval); Bool_t MISSING = MissingTRK_CALIB(t1,t2); for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING); nseq=0; - } else printf("DT %i\n",interval); + }; //-------------- //store run info //-------------- @@ -3462,6 +3675,7 @@ }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max); }; + if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID); nrow++; }; @@ -3490,7 +3704,7 @@ // which should be equal to the time between ascending-nodes. //============================================================== if ( t2 - trkcalib->FROM_TIME > 5700) { - printf("Long time to previous calib %i :-( \n",t2 - trkcalib->FROM_TIME); + if ( IsDebug() )printf("Long time between calib and run start %i :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME); //============================================================== // there might be a missing calibration, due to: // - MM full @@ -3511,7 +3725,7 @@ // it is enough to say that there are no missing calibrations //============================================================== // the long time interval bewteen runs might be due to download - printf("Short time to previous calib %i :-) \n",t2 - trkcalib->FROM_TIME); + if ( IsDebug() )printf("Short time between calib and run start %i :-) ==> OK! \n",t2 - trkcalib->FROM_TIME); return(false); }; @@ -3524,15 +3738,594 @@ TSQLResult *result = 0; stringstream oss; oss.str(""); - oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << "';"; + oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";"; // // if ( IsDebug() ) - printf(" Set VALIDATION = %i for run %i \n",validation,idrun); -// printf(" Set VALIDATION = %i for run %i : query is \n %s \n",validation,idrun,oss.str().c_str()); -// result = conn->Query(oss.str().c_str()); -// if ( !result ) throw -4; +// printf(" Set VALIDATION = %i for run %i \n",validation,idrun); + if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + if ( !result ) throw -4; return(0); } +// Insert TLEs from file tlefilename in the table GL_TLE in the db +// opened by conn, sorting them by date from older to newer, if each +// TLE has not been alread inserted. +Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile) +{ + fstream tlefile(tlefilename, ios::in); + + if ( !tlefile ) throw -7; + + vector ctles; + vector::iterator iter; + int present = 0; + + // Get three lines from tlefile, create a cTle object and put it + // into ctles + while(1) { + cTle *tlef; + string str1, str2, str3; + + getline(tlefile, str1); + if(tlefile.eof()) break; + + getline(tlefile, str2); + if(tlefile.eof()) break; + + getline(tlefile, str3); + if(tlefile.eof()) break; + + // We now have three good lines for a cTle. + tlef = new cTle(str1, str2, str3); + ctles.push_back(tlef); + } + + tlefile.close(); + + // Sort by date + sort(ctles.begin(), ctles.end(), compTLE); + + // Now we insert each TLE into the db + for(iter = ctles.begin(); iter != ctles.end(); iter++) { + cTle *tle = *iter; + + // Do nothing if it's already present in the db. Just increase + // the counter present. + if (! isTlePresent(tle)) + { + int status = insertTle(tle); + + // Insert query failed. Return 1. + if(status == EXIT_FAILURE) { + + if( IsDebug() ) { + cerr << "Error: inserting TLE:" << endl + << tle->getName() << endl + << tle->getLine1() << endl + << tle->getLine2() << endl; + } + + throw -4; + return 1; + } + + } + else + present++; + + } + + int inserted = ctles.size() - present; // Number of inserted TLE. + if ( IsDebug() ) + cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl + << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl; + + ctles.clear(); + + + // Return 2 if no new TLE has been inserted. 0 otherwise. + if(! inserted ) return 2; + return 0; +} + + +// Insert tle in the table GL_TLE using the connection conn. +Int_t PamelaDBOperations::insertTle(cTle *tle) +{ + stringstream oss; + TSQLResult *result = 0; + + oss.str(""); + oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)" + << " VALUES ( '" + << tle->getName() << "', '" + << tle->getLine1() << "', '" + << tle->getLine2() << "', '" + << getTleDatetime(tle) << "')"; + + // cout << oss.str().c_str() << endl; + result = conn->Query(oss.str().c_str()); + if (result == NULL) + return EXIT_FAILURE; + + return EXIT_SUCCESS; +} + + +// Return whether tle is already in the db connected by conn. +bool PamelaDBOperations::isTlePresent(cTle *tle) +{ + stringstream oss; + TSQLResult *result = 0; + + oss.str(""); + oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '" + << getTleDatetime(tle) << "'"; + + result = conn->Query(oss.str().c_str()); + if (result == NULL) throw -4; + + if (result->GetRowCount()) + return true; + else + return false; +} + + +// Return whether the first TLE is dated early than the second +bool compTLE (cTle *tle1, cTle *tle2) +{ + return getTleJulian(tle1) < getTleJulian(tle2); +} + + +// Return the date of the tle using the format (year-2000)*1e3 + +// julian day. e.g. 6365 is the 31th Dec 2006. +// It does *not* return a cJulian date. +float getTleJulian(cTle *tle) { + return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY); +} + + +// Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime +// format. +string getTleDatetime(cTle *tle) +{ + int year, mon, day, hh, mm, ss; + double dom; // day of month (is double!) + stringstream date; // date in datetime format + + // create a cJulian from the date in tle + cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY)); + + // get year, month, day of month + jdate.getComponent(&year, &mon, &dom); + + // build a datetime YYYY-MM-DD hh:mm:ss + date.str(""); + day = (int) floor(dom); + hh = (int) floor( (dom - day) * 24); + mm = (int) floor( ((dom - day) * 24 - hh) * 60); + ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60)); + // ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000); + + date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss; + + return date.str(); +} + +/** + * Remove a file from the DB, delete on cascade all entries related to that file + * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following + * calibration + **/ +Int_t PamelaDBOperations::removeFile(TString remfile){ + // + // Determine ID_ROOT_L0 and ID_RAW + // + TSQLResult *pResult; + TSQLRow *Row; + stringstream myquery; + // + myquery.str(""); + myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( !Row ){ + if ( strcmp(remfile.Data(),GetRootName().Data()) ){ + if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n"); + return(1); + }; + if ( IsDebug() ) printf(" No file to be removed (force mode)! \n"); + return(0); + }; + // + this->SetID_ROOT((UInt_t)atoll(Row->GetField(0))); + this->SetID_RAW((UInt_t)atoll(Row->GetField(1))); + // + this->ValidationOFF(); + // + this->RemoveCALIBS(); + // + this->RemoveRUNS(); + // + this->RemoveFILES(); + // + this->SetID_ROOT(0); + this->SetID_RAW(0); + // + return(0); +}; + +/** + * + * Set validation bit to zero for runs following the removing file till + * 1) a run with TRK_CALIB_USED=140 + * 2) a run with VALIDATION = 0 + * 3) the next calibration + * + **/ +void PamelaDBOperations::ValidationOFF(){ + TSQLResult *pResult; + TSQLRow *Row; + stringstream myquery; + Int_t unv = 0; + //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1; + myquery.str(""); + myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( !Row->GetField(0) ){ + // + if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n"); + // + } else { + // + UInt_t runhtime = (UInt_t)atoll(Row->GetField(0)); + UInt_t caltime = 0; + // + myquery.str(""); + myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( !Row ){ + caltime = runhtime; + } else { + caltime = (UInt_t)atoll(Row->GetField(0)); + }; + // + myquery.str(""); + myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ; + myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1"; + // + if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str()); + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( !Row ){ + // + if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n"); + // + } else { + myquery.str(""); + myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND "; + myquery << " RUNHEADER_TIME>=" <Query(myquery.str().c_str()); + // + Row = pResult->Next(); + while ( Row ){ + // + unv++; + this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false); + Row = pResult->Next(); + // + }; + }; + }; + if ( IsDebug() ) printf(" %i runs have been unvalidated \n",unv); +}; + +/** + * + * Rearrange GL_RUN table and remove runs + * + **/ +void PamelaDBOperations::RemoveRUNS(){ + TSQLResult *pResult; + TSQLRow *Row; + stringstream myquery; + UInt_t drun = 0; + GL_RUN *delrun = new GL_RUN(); + // + myquery.str(""); + myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <GetID_ROOT() <<";"; + // + if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str()); + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + // + // + if ( !Row ){ + if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n"); + } else { + if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n"); + while ( Row ){ + delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN"); + if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0))); + drun++; + Row = pResult->Next(); + }; + }; + // + // + myquery.str(""); + myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <GetID_ROOT() <<";"; + // + if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str()); + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + // + if ( !Row ){ + if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n"); + } else { + if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n"); + while ( Row ){ + if ( IsDebug() ) printf(" restore run %i \n",(UInt_t)atoll(Row->GetField(1))); + delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS"); + if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(1))); + delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN"); + if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){ + if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0))); + delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN"); + }; + drun++; + Row = pResult->Next(); + }; + }; + // + if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun); + // + // + // + drun = 0; + // + myquery.str(""); + myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <GetID_ROOT() <<";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + // + if ( !Row ){ + if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n"); + } else { + if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n"); + while ( Row ){ + if ( IsDebug() ) printf(" del run idtrash %i \n",(UInt_t)atoll(Row->GetField(0))); + myquery.str(""); + myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";"; + conn->Query(myquery.str().c_str()); + drun++; + Row = pResult->Next(); + }; + }; + // + if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_TRASH table \n",drun); + // + // + // + drun = 0; + // + myquery.str(""); + myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <GetID_ROOT() <<";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + // + if ( !Row ){ + if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n"); + } else { + if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n"); + while ( Row ){ + if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0))); + myquery.str(""); + myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";"; + conn->Query(myquery.str().c_str()); + drun++; + Row = pResult->Next(); + }; + }; + // + if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_FRAGMENTS table \n",drun); + // + // + // + delete delrun; + // +}; + + +/** + * + * Rearrange calibration tables + * + **/ +void PamelaDBOperations::RemoveFILES(){ + stringstream myquery; + // + myquery.str(""); + myquery << " DELETE FROM GL_RAW WHERE ID=" <GetID_RAW() <<";"; + // + if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str()); + // + conn->Query(myquery.str().c_str()); + // +}; + +/** + * + * Rearrange calibration tables + * + **/ +void PamelaDBOperations::RemoveCALIBS(){ + TSQLResult *pResult; + TSQLRow *Row; + stringstream myquery; + // + // + // Calorimeter + // + for (Int_t section = 0; section < 4; section++){ + myquery.str(""); + myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND "; + myquery << " SECTION=" << section << ";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( !Row->GetField(0) || !Row->GetField(1) ){ + // + if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section); + // + } else { + // + myquery.str(""); + myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1); + myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND "; + myquery << " SECTION=" << section << ";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + if( !pResult ){ + // + if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n"); + // + throw -4; + // + }; + // + }; + }; + myquery.str(""); + myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT(); + // + pResult = conn->Query(myquery.str().c_str()); + // + if( !pResult ){ + // + if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n"); + // + throw -4; + // + }; + // + // Tracker + // + myquery.str(""); + myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( !Row->GetField(0) || !Row->GetField(1) ){ + // + if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n"); + // + } else { + // + myquery.str(""); + myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1); + myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + if( !pResult ){ + // + if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n"); + // + throw -4; + // + }; + // + myquery.str(""); + myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT(); + // + pResult = conn->Query(myquery.str().c_str()); + // + if( !pResult ){ + // + if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n"); + // + throw -4; + // + }; + }; + // + // + // S4 + // + myquery.str(""); + myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( !Row->GetField(0) || !Row->GetField(1) ){ + // + if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n"); + // + } else { + // + myquery.str(""); + myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1); + myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + if( !pResult ){ + // + if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n"); + // + throw -4; + // + }; + // + myquery.str(""); + myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT(); + // + pResult = conn->Query(myquery.str().c_str()); + // + if( !pResult ){ + // + if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n"); + // + throw -4; + // + }; + // + }; +};