--- chewbacca/YodaProfiler/src/PamelaDBOperations.cpp 2008/09/23 07:20:33 1.1 +++ chewbacca/YodaProfiler/src/PamelaDBOperations.cpp 2008/11/04 17:11:41 1.9 @@ -58,7 +58,7 @@ * @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, TString tlefilename, UInt_t dwinput, Bool_t staticp, Bool_t gpamela){ +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, UInt_t dwinput, Bool_t staticp, Bool_t gpamela, Bool_t keepenv){ // chewbacca = false; chminentry = 0; @@ -76,6 +76,8 @@ // }; // + KEEPENV = false; + if ( keepenv ) KEEPENV=true; STATIC=false; if ( staticp ) STATIC=true; // @@ -138,7 +140,7 @@ TString chfile; UInt_t ridn = 0; qu.str(""); - qu << "SELECT ROOT_ID_N,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL from ROOT_TABLE_MERGING where ID_N=" << chID << ";"; + qu << "SELECT ROOT_ID_N,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,INSERTED_BY from ROOT_TABLE_MERGING where ID_N=" << chID << ";"; if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str()); result = conn->Query(qu.str().c_str()); if ( result ){ @@ -149,6 +151,7 @@ chpktmax = (UInt_t)atoll(row->GetField(2)); chobtmin = (UInt_t)atoll(row->GetField(3)); chobtmax = (UInt_t)atoll(row->GetField(4)); + chiby = (TString)(row->GetField(5)); } else { throw -84; }; @@ -163,7 +166,11 @@ if ( result2 ){ row2 = result2->Next(); if ( row2 ){ - chpath = (TString)gSystem->ExpandPathName(row2->GetField(0))+'/'; + if ( KEEPENV ){ + chpath = (TString)(row2->GetField(0))+'/'; + } else { + chpath = (TString)gSystem->ExpandPathName(row2->GetField(0))+'/'; + }; chfile = (TString)(row2->GetField(1)); chobtts = (UInt_t)atoll(row2->GetField(2)); chlastts = (UInt_t)atoll(row2->GetField(3)); @@ -435,9 +442,13 @@ TString PamelaDBOperations::GetRootPath(){ if ( STATIC ){ - return((TString)gSystem->DirName(filerootname.Data())+'/'); - } else { - return((TString)gSystem->ExpandPathName("$PAM_L0")+'/'); + return((TString)gSystem->DirName(this->GetRootName().Data())+'/'); + } else { + if ( KEEPENV ){ + return((TString)gSystem->ExpandPathName(gSystem->DirName(filerootname.Data()))+'/'); + } else { + return((TString)gSystem->ExpandPathName("$PAM_L0")+'/'); + }; }; }; @@ -490,6 +501,52 @@ }; /** + * + * Set the variables which belogns to physendrun tree + * + */ +void PamelaDBOperations::SetPhysEndRunVariables(){ + // + // + // + TTree *T = 0; + T = (TTree*)file->Get("PhysEndRun"); + if ( !T || T->IsZombie() ) throw -90; + // + PhysEndRunEvent *pher= 0; + EventHeader *eh = 0; + T->SetBranchAddress("PhysEndRun", &pher); + T->SetBranchAddress("Header", &eh); + // + UInt_t phobt = 0; + UInt_t phpkt = 0; + // + glrun->SetPHYSENDRUN_MASK_S3S2S12(0); + glrun->SetPHYSENDRUN_MASK_S11CRC(0); + // + for (Int_t p=0; pGetEntries(); p++){ + // + T->GetEntry(p); + // + phobt = (UInt_t)eh->GetPscuHeader()->GetOrbitalTime(); + phpkt = (UInt_t)eh->GetPscuHeader()->GetCounter(); + // + if ( this->PKT(phpkt) >= this->PKT(glrun->GetRUNHEADER_PKT()) && this->PKT(phpkt) <= this->PKT(glrun->GetRUNTRAILER_PKT()) && this->OBT(phobt) >= this->OBT(glrun->GetRUNHEADER_OBT()) && this->OBT(phobt) <= this->OBT(glrun->GetRUNTRAILER_OBT()) ){ + if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() || glrun->GetPHYSENDRUN_MASK_S11CRC() ){ + if ( IsDebug() ) printf(" WARNING while looping in physendrun: found two PhysEndRun packet for the same RUN! \n"); + if ( IsDebug() ) printf(" Actual values: %X %X New values %X %X \n ",glrun->GetPHYSENDRUN_MASK_S3S2S12(),glrun->GetPHYSENDRUN_MASK_S11CRC(),(UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S3S2S12,(UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S11CRC); + if ( PEDANTIC && IsDebug() ) printf(" ERROR while looping in physendrun: found two PhysEndRun packet for the same RUN!\n "); + if ( PEDANTIC ) throw -91; + } else { + glrun->SetPHYSENDRUN_MASK_S3S2S12((UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S3S2S12); + glrun->SetPHYSENDRUN_MASK_S11CRC((UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S11CRC); + }; + }; + }; + // +}; + +/** * Patch, look for upper limits to avoid processing retransmitted data */ Int_t PamelaDBOperations::SetUpperLimits(){ @@ -603,8 +660,9 @@ UInt_t tjump = 50000; //UInt_t tjump = 100; while ( tjump > 0 ){ - pktlast = numeric_limits::max(); - while ( pktlast > chpktmax && (Int_t)(nevent-1-it) >= 0 ){ + // pktlast = numeric_limits::max(); + pktlast = chpktmax + 1; + while ( PKT(pktlast) > PKT(chpktmax) && (Int_t)(nevent-1-it) >= 0 ){ if ( (Int_t)(nevent-1-it) >= 0 ){ T->GetEntry(nevent-1-it); ph = eh->GetPscuHeader(); @@ -612,7 +670,7 @@ } else { pktlast = chpktmax + 1; }; - if ( (!(it%1000) || abs((int)pktlast - (int)chpktmax)<1000 ) && debug ) printf(" look for up %i %i %i nevent %u (nevent-1-it) %i \n",it,pktlast,chpktmax,nevent,(Int_t)(nevent-1-it)); + if ( (!(it%1000) || abs((int)(PKT(pktlast) - PKT(chpktmax)))<1000 ) && debug ) printf(" look for up %i %i %i nevent %u (nevent-1-it) %i \n",it,pktlast,chpktmax,nevent,(Int_t)(nevent-1-it)); it += tjump; }; if ( tjump > 1 ) it -= 2*tjump; @@ -636,8 +694,10 @@ tjump = 50000; //tjump = 100; while ( tjump > 0 ){ - pktlast = 0; - while ( pktlast < chpktmin && it < (Int_t)nevent ){ + // pktlast = 0; + pktlast = chpktmin - 1; + if ( debug ) printf("LLlook for down %i %i %llu %llu \n",it,pktlast,PKT(pktlast),PKT(chpktmin)); + while ( PKT(pktlast) < PKT(chpktmin) && it < (Int_t)nevent ){ if ( it < (Int_t)nevent ){ T->GetEntry(it); ph = eh->GetPscuHeader(); @@ -739,12 +799,12 @@ }; // if ( chewbacca && nevent < 1 ) { - pktfirst = chpktmin; - upperpkt = PKT(chpktmax); - pktlast = chpktmax; - obtfirst = chobtmin; - obtlast = chobtmax; - upperobt = OBT(chobtmax); + pktfirst = chpktmin; + upperpkt = PKT(chpktmax); + pktlast = chpktmax; + obtfirst = chobtmin; + obtlast = chobtmax; + upperobt = OBT(chobtmax); }; // if ( IsDebug() ) printf(" First entries are: OBT %u pkt_num %u entry %i\n",obtfirst,pktfirst,chminentry); @@ -752,9 +812,9 @@ if ( IsDebug() ) printf(" Last entries are: OBT %lld pkt_num %lld entry %i\n",upperobt,upperpkt,upperentry); // if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ){ - if ( IsDebug() ) printf(" Inconsistent PKT/OBT sequence: \n (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) %llu < %llu && %llu > %llu \n OR \n (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) %llu > %llu && %llu < %llu \n",PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst),PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst)); - if ( PEDANTIC ) throw -88; - return(32); + if ( IsDebug() ) printf(" Inconsistent PKT/OBT sequence: \n (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) %llu < %llu && %llu > %llu \n OR \n (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) %llu > %llu && %llu < %llu \n",PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst),PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst)); + if ( PEDANTIC ) throw -88; + return(32); }; // if ( !nevent ) return(64); @@ -1040,7 +1100,7 @@ upperpkt = pkth; upperobt = obth; rhev = lasthead+1; - } else { + } else { rhev = lasthead; }; if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt); @@ -1079,7 +1139,15 @@ stringstream oss; // oss.str(""); - oss << "INSERT INTO _RUNID_GEN VALUES (NULL);"; + if ( chewbacca ){// if chewbacca and tag=none then use chewbacca tag (chiby = chewbacca inserted by), if chewbacca and tag!=none then use tag, if not chewbacca use tag. + if ( !strcmp(tag.Data(),"NONE") ){ + oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< chiby.Data() <<"');"; + } else { + oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');"; + }; + } else { + oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');"; + }; result = conn->Query(oss.str().c_str()); if ( !result ) throw -10; oss.str(""); @@ -1227,7 +1295,7 @@ stringstream oss; // oss.str(""); - oss << "lock table GL_RUN write, GL_ROOT write, GL_RAW write, GL_TIMESYNC write, GL_RESURS_OFFSET write, GL_PARAM write, GL_TLE write, GL_RUN_FRAGMENTS write, GL_RUN_TRASH write, GL_CALO_CALIB write, GL_CALOPULSE_CALIB write, GL_TRK_CALIB write, GL_S4_CALIB write, ROOT_TABLE_MERGING write, ROOT_TABLE write, _RUNID_GEN write;"; + oss << "lock table GL_RUN write, GL_ROOT write, GL_RAW write, GL_TIMESYNC write, GL_RESURS_OFFSET write, GL_PARAM write, GL_TLE write, GL_RUN_FRAGMENTS write, GL_RUN_TRASH write, GL_CALO_CALIB write, GL_CALOPULSE_CALIB write, GL_TRK_CALIB write, GL_S4_CALIB write, ROOT_TABLE_MERGING write, ROOT_TABLE_BAD write, ROOT_TABLE write, _RUNID_GEN write;"; TSQLResult *result = 0; result = conn->Query(oss.str().c_str()); if ( !result ) throw -10; @@ -1260,19 +1328,19 @@ */ Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){ // -// if ( IsDebug() ) printf(" pkt conversion: pkt_num is %u pktfirst is %u (UInt_t)(16777214/2)) is %u \n",pkt_num,ppktfirst,(UInt_t)(16777214/2)); + // if ( IsDebug() ) printf(" pkt conversion: pkt_num is %u pktfirst is %u (UInt_t)(16777214/2)) is %u \n",pkt_num,ppktfirst,(UInt_t)(16777214/2)); // if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2) ){ -// if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL); + // if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL); return((Long64_t)pkt_num+16777215LL); }; // if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){ -// if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL); + // if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL); return((Long64_t)pkt_num-16777215LL); }; // -// if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num); + // if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num); return((Long64_t)pkt_num); // }; @@ -1282,20 +1350,20 @@ */ Long64_t PamelaDBOperations::OBT(UInt_t obt){ // -// if ( IsDebug() ) printf(" obt conversion: obt is %u obtfirst is %u (numeric_limits::max()/2) is %u \n",obt,pobtfirst,(UInt_t)(numeric_limits::max()/2)); + // if ( IsDebug() ) printf(" obt conversion: obt is %u obtfirst is %u (numeric_limits::max()/2) is %u \n",obt,pobtfirst,(UInt_t)(numeric_limits::max()/2)); // if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits::max()/2) ){ -// if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)obt+(Long64_t)numeric_limits::max()); + // if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)obt+(Long64_t)numeric_limits::max()); return((Long64_t)obt+(Long64_t)numeric_limits::max()); }; // if ( obt > ((Long64_t)pobtfirst*2) && obt > (numeric_limits::max()/2) ){ -// if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2)); -// if ( IsDebug() ) printf(" rise down pktnum %lld \n", (Long64_t)obt-(Long64_t)numeric_limits::max()); + // if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2)); + // if ( IsDebug() ) printf(" rise down pktnum %lld \n", (Long64_t)obt-(Long64_t)numeric_limits::max()); return((Long64_t)obt-(Long64_t)numeric_limits::max()); }; // -// if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt); + // if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt); return((Long64_t)obt); }; @@ -1331,9 +1399,13 @@ UInt_t lastPkt = 0; UInt_t rhtime = 0; UInt_t rttime = 0; + // + if ( IsDebug() ) printf(" A firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1); + // if ( !mishead ){ codh = ehh->GetCounter(); - if ( lastev+1 == firstev || (lastev == firstev && lastev != -1) ){ + if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){ + if ( IsDebug() ) printf(" B firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1); firstev = 1; lastev = 0; } else { @@ -1346,7 +1418,8 @@ }; if ( !mistrail ){ codt = eht->GetCounter(); - if ( lastev+1 == firstev || (lastev == firstev && lastev != -1)){ + if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1)){ + if ( IsDebug() ) printf(" C firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1); lastev = 0; firstev = lastev+1; } else { @@ -1363,7 +1436,7 @@ if ( mishead ) { glrun->Set_GL_RUNH0(); // - if ( lastev+1 == firstev || (lastev == firstev && lastev != -1) ){ + if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){ firstObt = lastObt; firstPkt = lastPkt; rhtime = rttime; @@ -1382,7 +1455,7 @@ if ( mistrail ){ glrun->Set_GL_RUNT0(); // - if ( lastev+1 == firstev || (lastev == firstev && lastev != -1) ){ + if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){ lastObt = firstObt; lastPkt = firstPkt; rttime = rhtime; @@ -1405,6 +1478,7 @@ if ( IsDebug() ) printf(" firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1); // this->SetCommonGLRUN(rhtime,rttime); + this->SetPhysEndRunVariables(); // }; @@ -1416,25 +1490,61 @@ * Insert a new row into GL_RAW table. */ Int_t PamelaDBOperations::insertPamelaRawFile(){ + + // + Bool_t idr = this->SetID_RAW(); + if ( idr ) return(1); + + GL_RAW glraw = GL_RAW(); + + glraw.PATH = GetRawPath(); + glraw.NAME = GetRawFile(); + // glraw.BOOT_NUMBER = 0;//??? + glraw.BOOT_NUMBER = this->GetBOOTnumber(); + + if( insertPamelaRawFile(&glraw) )return(1); + // + idr = this->SetID_RAW(); + if ( !idr ) throw -11; + + return(0); +} +/** + * Insert a new row into GL_RAW table. + */ +Int_t PamelaDBOperations::insertPamelaRawFile(GL_RAW *glraw){ // - stringstream oss; - // - Bool_t idr = this->SetID_RAW(); - if ( idr ) return(1); - // - oss.str(""); - if ( STATIC ){ - oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('" - << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')"; - } else { - oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')"; - }; - if ( conn->Query(oss.str().c_str()) == 0 ) throw -4; - // - idr = this->SetID_RAW(); - if ( !idr ) throw -11; - // - return(0); + if(!glraw)return(1);//?? ok I think + // + stringstream oss; + // + oss.str(""); + if ( STATIC ){ + oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('" + << glraw->PATH << "', '" << glraw->NAME << "')"; + } else { + oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << glraw->NAME << "')"; + }; + if ( debug ) cout <Query(oss.str().c_str()) == 0 ) throw -4; + // + oss.str(""); + oss << "SELECT ID FROM GL_RAW WHERE NAME=\""<NAME<<"\";"; + if ( debug ) cout << oss.str().c_str()<Query(oss.str().c_str()) == 0 ) throw -4; + // + TSQLResult *result = 0; + TSQLRow *row = 0; + result = conn->Query(oss.str().c_str()); + if ( result == NULL ) throw -4; + row = result->Next(); + if ( !row ) return(1); + glraw->ID = (UInt_t)atoll(row->GetField(0)); + if ( debug ) printf(" The ID of the RAW file is %u \n",glraw->ID); + delete result; + delete row; + // + return(0); } @@ -1746,94 +1856,132 @@ * The raw file indicates in the parameters should be already been stored in the database. */ Int_t PamelaDBOperations::insertPamelaRootFile(){ - stringstream oss; - TSQLResult *result = 0; - TSQLRow *row = 0; - UInt_t idtimesync = 0; - // - // - if ( chewbacca ){ - oss.str(""); - oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<Query(oss.str().c_str()); - // - if ( !result ) throw -3; + + stringstream oss; + TSQLResult *result = 0; + TSQLRow *row = 0; // - row = result->Next(); + // ---------------------- + // determine the timesync + // ---------------------- + UInt_t idtimesync = 0; // - if ( !row ) throw -3; - idtimesync = (UInt_t)atoll(row->GetField(0)); - } else { - oss.str(""); - if ( STATIC ){ - oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW " - << " LEFT JOIN GL_ROOT " - << " ON GL_RAW.ID = GL_ROOT.ID_RAW " - << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND " - << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID "; + if ( chewbacca ){ + oss.str(""); + oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<Query(oss.str().c_str()); + // + if ( !result ) throw -3; + // + row = result->Next(); + // + if ( !row ) throw -3; + idtimesync = (UInt_t)atoll(row->GetField(0)); } else { - oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW " - << " LEFT JOIN GL_ROOT " - << " ON GL_RAW.ID = GL_ROOT.ID_RAW " - << " WHERE GL_RAW.PATH = '$PAM_RAW' AND " - << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID "; + oss.str(""); + if ( STATIC ){ + oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW " + << " LEFT JOIN GL_ROOT " + << " ON GL_RAW.ID = GL_ROOT.ID_RAW " + << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND " + << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID "; + } else { + oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW " + << " LEFT JOIN GL_ROOT " + << " ON GL_RAW.ID = GL_ROOT.ID_RAW " + << " WHERE GL_RAW.PATH = '$PAM_RAW' AND " + << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID "; + }; + result = conn->Query(oss.str().c_str()); + // + if ( !result ) throw -12; + // + row = result->Next(); + // + if ( !row ) throw -10; + if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){ + idroot = (UInt_t)atoll(row->GetField(2)); + delete row; + delete result; + return(1); + }; + // + // determine which timesync has to be used + // + oss.str(""); + oss << "SELECT GL_TIMESYNC.ID FROM GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW ORDER BY GL_TIMESYNC.ID DESC LIMIT 1;"; + result = conn->Query(oss.str().c_str()); + // + if ( !result ) throw -3; + // + row = result->Next(); + // + if ( !row ) throw -3; + idtimesync = (UInt_t)atoll(row->GetField(0)); }; - result = conn->Query(oss.str().c_str()); - // - if ( !result ) throw -12; - // - row = result->Next(); - // - if ( !row ) throw -10; - if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){ - idroot = (UInt_t)atoll(row->GetField(2)); - return(1); + + delete row; + delete result; + + // ---------------------- + // insert root file + // ---------------------- + + GL_ROOT glroot = GL_ROOT(); + + glroot.ID_RAW = GetID_RAW(); + glroot.ID_TIMESYNC = idtimesync; + if ( STATIC ){ + glroot.PATH = GetRootPath(); + } else { + if ( KEEPENV ){ + glroot.PATH = gSystem->DirName(filerootname.Data()); + } else { + glroot.PATH = "$PAM_L0"; + }; }; - // - // determine which timesync has to be used - // - oss.str(""); - oss << "SELECT GL_TIMESYNC.ID FROM GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW ORDER BY GL_TIMESYNC.ID DESC LIMIT 1;"; - result = conn->Query(oss.str().c_str()); - // - if ( !result ) throw -3; - // - row = result->Next(); - // - if ( !row ) throw -3; - idtimesync = (UInt_t)atoll(row->GetField(0)); - }; + glroot.NAME = GetRootFile(); + + if ( insertPamelaRootFile(&glroot) )return 1; + + SetID_ROOT(glroot.ID); + + + return (0); +} +/** + * Insert all the new rows into GL_ROOT. + * The raw file indicates in the parameters should be already been stored in the database. + */ +Int_t PamelaDBOperations::insertPamelaRootFile(GL_ROOT *glroot){ + stringstream oss; + TSQLResult *result = 0; + TSQLRow *row = 0; + // // oss.str(""); - if ( STATIC ){ - oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('" - << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')"; - } else { - oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('" - << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')"; - }; + oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('" + << glroot->ID_RAW << "', '" << glroot->ID_TIMESYNC << "', '" << glroot->PATH << "', '" << glroot->NAME << "')"; // - if ( debug ) printf(" query is %s \n",oss.str().c_str()); + if ( debug ) printf("%s \n",oss.str().c_str()); if (conn->Query(oss.str().c_str()) == 0) throw -4; // delete result; // oss.str(""); // oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";"; - if ( STATIC ){ - oss << "SELECT ID FROM GL_ROOT WHERE PATH='" << this->GetRootPath().Data() << "' and NAME='"<< this->GetRootFile().Data() <<"';"; - } else { - oss << "SELECT ID FROM GL_ROOT WHERE PATH='$PAM_L0' and NAME='"<< this->GetRootFile().Data() <<"';"; - }; + oss << "SELECT ID FROM GL_ROOT WHERE PATH='" << glroot->PATH << "' and NAME='"<< glroot->NAME <<"';"; // - if ( debug ) printf(" query is %s \n",oss.str().c_str()); + if ( debug ) printf("%s \n",oss.str().c_str()); result = conn->Query(oss.str().c_str()); if ( !result ) throw -12; row = result->Next(); if ( !row ) throw -3; - this->SetID_ROOT((UInt_t)atoll(row->GetField(0))); - if ( debug ) printf(" The ID of the ROOT file is %u \n",this->GetID_ROOT()); + + glroot->ID = (UInt_t)atoll(row->GetField(0)); + + if ( debug ) printf(" The ID of the ROOT file is %u \n",glroot->ID); // delete result; // @@ -2061,10 +2209,10 @@ cod = ehh->GetCounter(); tcod = (UInt_t)cod->Get(pctp->Physics); evbefh = TMath::Max(chminentry,tcod); -// if ( (UInt_t)evbefh == upperentry ) evbefh = upperentry + 1; // this does not work due to the Counter bug in chewbacca + // if ( (UInt_t)evbefh == upperentry ) evbefh = upperentry + 1; // this does not work due to the Counter bug in chewbacca if ( (UInt_t)evbefh == upperentry || !upperentry ) evbefh = upperentry + 1; -// - if ( debug ) printf(" evbefh %i upperentry %u \n",evbefh,upperentry); + // + if ( debug ) printf(" evbefh %i upperentry %u \n",evbefh,upperentry); // this->HandleRunFragments(false,true,evbefh,upperentry); // @@ -2367,10 +2515,10 @@ // if ( IsDebug() ) printf(" The new run has more events than the old one \n"); 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()); + // 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; // @@ -2385,10 +2533,10 @@ if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n"); // 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()); + // 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; @@ -2403,10 +2551,10 @@ if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n"); // 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()); + // 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; // }; @@ -2488,505 +2636,511 @@ if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n"); // } else { - // - // we have now the good first piece of a run, fill the glrun object - // - if ( rhfirstev != firstev && !mishead ) mishead = true; - if ( rtlastev != lastev && !mistrail ) mistrail = true; - // - this->FillClass(mishead,mistrail,firstev,lastev); - // - if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n"); - if ( IsDebug() ) printf(" C THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT()); - // - // 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!) + // we have now the good first piece of a run, fill the glrun object // - if ( IsDebug() ) printf(" The run is new \n"); - if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n"); + if ( rhfirstev != firstev && !mishead ) mishead = true; + if ( rtlastev != lastev && !mistrail ) mistrail = true; // - glrun->SetID(this->AssignRunID()); - glrun->SetID_RUN_FRAG(0); - glrun->Fill_GL_RUN_FRAGMENTS(conn); + this->FillClass(mishead,mistrail,firstev,lastev); // - } else { - if ( IsDebug() ) printf(" The run is already present in the fragment table \n"); - if ( PEDANTIC ) throw -69; - return; - }; - // - if ( chewbacca && mishead && mistrail ) goto justcheck; - // - // can we find the other piece of the run in the GL_RUN_FRAGMENTS table? - // - if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is - // missing it no way we can found a piece in the frag table + if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n"); + if ( IsDebug() ) printf(" C THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT()); + // + // First of all insert the run in the fragment table... // 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() << " AND " - << " ID != " << glrun->ID - << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!! + 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(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str()); + 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 && NoFrag() ){ + 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"); + if ( PEDANTIC ) throw -69; + return; + }; + // + if ( chewbacca && mishead && mistrail ) goto justcheck; + // + // can we find the other piece of the run in the GL_RUN_FRAGMENTS table? + // + if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is + // missing it no way we can found a piece in the frag table // oss.str(""); - oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE " + 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() << " AND " << " ID != " << glrun->ID - << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!! + << " 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()); + if ( IsDebug() ) printf(" look for runheader in the fragments 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; - } else { - // - found = false; // default value - // - if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n"); - // - // if we have both runheader and runtrailer we can check with pkt_counter: - // - if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){ - ULong64_t chkpkt = 0; - ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT()); - ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4))); + if ( !row && NoFrag() ){ // - chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL; + 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 ( labs(chkpkt-pktt)<2 ){ - // - if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt); - // - found = true; - // - } else { - // - if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt); - // - found = false; - // - }; }; - if ( !found && chewbacca ) goto justcheck; - if ( !found ){ + // + if ( !row ){ + if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n"); + found = false; + } else { // - // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts + found = false; // default value // - ULong64_t chkpkt1 = 0; - ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT()); - ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5))); - chkpkt1 = labs(orunh1-dbrunt1); - // - ULong64_t chkpkt2 = 0; - ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT()); - ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3))); - chkpkt2 = labs(orunh2-dbrunt2); - // - ULong64_t chkpkt3 = 0; - ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME()); - ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2))); - chkpkt3 = labs(orunh3-dbrunt3); + if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n"); // - if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){ - // if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){ - // - if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3); + // if we have both runheader and runtrailer we can check with pkt_counter: + // + if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){ + ULong64_t chkpkt = 0; + ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT()); + ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4))); // - found = true; + chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL; // - } else { + if ( labs(chkpkt-pktt)<2 ){ + // + if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt); + // + found = true; + // + } else { + // + if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt); + // + found = false; + // + }; + }; + if ( !found && chewbacca ) goto justcheck; + if ( !found ){ // - if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3); + // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts // - found = false; + ULong64_t chkpkt1 = 0; + ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT()); + ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5))); + chkpkt1 = labs(orunh1-dbrunt1); // + ULong64_t chkpkt2 = 0; + ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT()); + ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3))); + chkpkt2 = labs(orunh2-dbrunt2); + // + ULong64_t chkpkt3 = 0; + ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME()); + ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2))); + chkpkt3 = labs(orunh3-dbrunt3); + // + if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){ + // if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){ + // + if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3); + // + found = true; + // + } else { + // + if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3); + // + found = false; + // + }; }; }; - }; - // - if ( found ){ - // - // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table - // - 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)); - // - oss.str(""); - oss << " ID="<GetField(0)<<";"; - // - glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos - // - // merge infos - // - UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT()); - ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT()); - UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT()); - ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT()); - if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev); - TTree *T= 0; - T = (TTree*)file->Get("Physics"); - if ( !T || T->IsZombie() ) throw -16; - EventHeader *eh = 0; - PscuHeader *ph = 0; - T->SetBranchAddress("Header", &eh); - while ( apkt > bpkt && aobt > bobt && firstev < lastev ){ - T->GetEntry(firstev); - ph = eh->GetPscuHeader(); - bpkt = PKT(ph->GetCounter()); - bobt = OBT(ph->GetOrbitalTime()); - firstev++; - if ( PEDANTIC ) throw -71; - }; - if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev); - // - glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER()); - glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER()); - glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME()); - glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT()); - glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT()); - // - 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()); - glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP()); - glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE()); - glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE()); - glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A()); - glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B()); - glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO()); - glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO()); - glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB()); - glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE()); - glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED()); - glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK()); - glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC()); - glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC()); - // - if ( !IsRunAlreadyInserted() ){ + if ( found ){ + // + // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table + // + 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)); + // + oss.str(""); + oss << " ID="<GetField(0)<<";"; + // + glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos + // + // merge infos + // + UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT()); + ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT()); + UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT()); + ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT()); + if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev); + TTree *T= 0; + T = (TTree*)file->Get("Physics"); + if ( !T || T->IsZombie() ) throw -16; + EventHeader *eh = 0; + PscuHeader *ph = 0; + T->SetBranchAddress("Header", &eh); + while ( apkt > bpkt && aobt > bobt && firstev < lastev ){ + T->GetEntry(firstev); + ph = eh->GetPscuHeader(); + bpkt = PKT(ph->GetCounter()); + bobt = OBT(ph->GetOrbitalTime()); + firstev++; + if ( PEDANTIC ) throw -71; + }; + if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev); + // + glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER()); + glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER()); + glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME()); + glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT()); + glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT()); + // + 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()); + glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP()); + glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE()); + glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE()); + glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A()); + glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B()); + glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO()); + glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO()); + glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB()); + glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE()); + glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED()); + glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK()); + glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC()); + glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC()); + // + if ( glrun1->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glrun1->GetPHYSENDRUN_MASK_S3S2S12()); + if ( glrun1->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glrun1->GetPHYSENDRUN_MASK_S11CRC()); + // + if ( !IsRunAlreadyInserted() ){ + // + // glrun->SetID(this->AssignRunID()); + glrun->SetID_RUN_FRAG(glrun1->GetID()); + glrun->Fill_GL_RUN(conn); + // + // set id number + // + glrun1->SetID_RUN_FRAG(glrun->GetID()); + glrun1->Fill_GL_RUN(conn); + // + }; + // delete old entry in fragment table + // + glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); + glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); // - // glrun->SetID(this->AssignRunID()); - glrun->SetID_RUN_FRAG(glrun1->GetID()); - glrun->Fill_GL_RUN(conn); + delete glrun1; // - // set id number // - glrun1->SetID_RUN_FRAG(glrun->GetID()); - glrun1->Fill_GL_RUN(conn); + return; // }; - // delete old entry in fragment table - // - glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); - glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); - // - delete glrun1; - // - // - return; // }; // - }; - // - if ( mistrail && ( rtlastev == lastev || chewbacca )) { // 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 - // - 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() << " 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()); - result = conn->Query(oss.str().c_str()); - // - if ( !result ) throw -4; - // - row = result->Next(); - // - if ( !row && NoFrag() ){ + if ( mistrail && ( rtlastev == lastev || chewbacca )) { // 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 // oss.str(""); - oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE " + 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() << " AND " << " ID != " << glrun->ID - << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;"; + << " 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()); + if ( IsDebug() ) printf(" look for runtrailer in the fragments 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; - } else { - // - found = false; // default value - // - if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n"); - // - // if we have both runheader and runtrailer we can check with pkt_counter: + 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 ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){ - ULong64_t chkpkt = 0; - ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4))); - ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT()); + if ( !row ){ + if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n"); + found = false; + } else { + // + found = false; // default value + // + if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n"); // - chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL; + // if we have both runheader and runtrailer we can check with pkt_counter: // - if ( labs(chkpkt-pktt)<2 ){ + if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){ + ULong64_t chkpkt = 0; + ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4))); + ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT()); // - if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt); + chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL; // - found = true; + if ( labs(chkpkt-pktt)<2 ){ + // + if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt); + // + found = true; + // + } else { + // + if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt); + // + found = false; + // + }; + }; + if ( !found && chewbacca ) goto justcheck; + if ( !found ){ // - } else { + // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts // - if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt); + ULong64_t chkpkt1 = 0; + ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT()); + ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5))); + chkpkt1 = labs(orunh1-dbrunt1); // - found = false; + ULong64_t chkpkt2 = 0; + ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT()); + ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3))); + chkpkt2 = labs(orunh2-dbrunt2); // + ULong64_t chkpkt3 = 0; + ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME()); + ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2))); + chkpkt3 = labs(orunh3-dbrunt3); + // + if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){ + // + if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3); + // + found = true; + // + } else { + // + if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3); + // + found = false; + // + }; }; }; - if ( !found && chewbacca ) goto justcheck; - if ( !found ){ + // + if ( found ){ + // + // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table + // + 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"); + }; // - // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts + GL_RUN *glrun1 = new GL_RUN(); // - ULong64_t chkpkt1 = 0; - ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT()); - ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5))); - chkpkt1 = labs(orunh1-dbrunt1); + // UInt_t idfrag = (UInt_t)atoll(row->GetField(0)); // - ULong64_t chkpkt2 = 0; - ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT()); - ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3))); - chkpkt2 = labs(orunh2-dbrunt2); + oss.str(""); + oss << " ID="<GetField(0)<<";"; // - ULong64_t chkpkt3 = 0; - ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME()); - ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2))); - chkpkt3 = labs(orunh3-dbrunt3); + glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runtrailer infos // - if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){ - // - if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3); - // - found = true; - // - } else { + // merge infos + // + UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT()); + ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT()); + UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT()); + ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT()); + if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev); + TTree *T= 0; + T = (TTree*)file->Get("Physics"); + if ( !T || T->IsZombie() ) throw -16; + EventHeader *eh = 0; + PscuHeader *ph = 0; + T->SetBranchAddress("Header", &eh); + while ( apkt > bpkt && aobt > bobt && lastev > 0 ){ + T->GetEntry(lastev); + ph = eh->GetPscuHeader(); + apkt = PKT(ph->GetCounter()); + aobt = OBT(ph->GetOrbitalTime()); + lastev--; + if ( PEDANTIC ) throw -72; + }; + if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev); + // + glrun->SetEV_TO(lastev); + glrun->SetNEVENTS(lastev-firstev+1); + glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER()); + glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER()); + glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME()); + glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT()); + glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT()); + // + glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME()); + glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT()); + glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT()); + glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP()); + glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE()); + glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE()); + glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A()); + glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B()); + glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO()); + glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO()); + glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB()); + glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE()); + glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED()); + glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK()); + glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC()); + glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC()); + // + if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ) glrun1->SetPHYSENDRUN_MASK_S3S2S12(glrun->GetPHYSENDRUN_MASK_S3S2S12()); + if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) glrun1->SetPHYSENDRUN_MASK_S11CRC(glrun->GetPHYSENDRUN_MASK_S11CRC()); + // + if ( !IsRunAlreadyInserted() ){ + // + // glrun->SetID(this->AssignRunID()); + // + glrun->SetID_RUN_FRAG(glrun1->GetID()); + glrun->Fill_GL_RUN(conn); // - if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3); + // set id number // - found = false; + glrun1->SetID_RUN_FRAG(glrun->GetID()); + glrun1->Fill_GL_RUN(conn); // }; + // + // delete old entries in fragment table + // + glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); + glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); + // + delete glrun1; + // + return; + // }; + // }; // - if ( found ){ - // - // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table - // - 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"); - }; + justcheck: + // + if ( !found ){ // - GL_RUN *glrun1 = new GL_RUN(); + if ( IsDebug() ) printf(" not found, check if we have already processed the file \n "); // - // UInt_t idfrag = (UInt_t)atoll(row->GetField(0)); + // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table? // oss.str(""); - oss << " ID="<GetField(0)<<";"; + oss << " SELECT ID FROM GL_RUN 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() << ") ));"; // - glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runtrailer infos + 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()); // - // merge infos + if ( !result ) throw -4; // - UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT()); - ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT()); - UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT()); - ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT()); - if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev); - TTree *T= 0; - T = (TTree*)file->Get("Physics"); - if ( !T || T->IsZombie() ) throw -16; - EventHeader *eh = 0; - PscuHeader *ph = 0; - T->SetBranchAddress("Header", &eh); - while ( apkt > bpkt && aobt > bobt && lastev > 0 ){ - T->GetEntry(lastev); - ph = eh->GetPscuHeader(); - apkt = PKT(ph->GetCounter()); - aobt = OBT(ph->GetOrbitalTime()); - lastev--; - if ( PEDANTIC ) throw -72; - }; - if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev); - // - glrun->SetEV_TO(lastev); - glrun->SetNEVENTS(lastev-firstev+1); - glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER()); - glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER()); - glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME()); - glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT()); - glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT()); - // - glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME()); - glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT()); - glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT()); - glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP()); - glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE()); - glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE()); - glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A()); - glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B()); - glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO()); - glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO()); - glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB()); - glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE()); - glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED()); - glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK()); - glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC()); - glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC()); + row = result->Next(); // - if ( !IsRunAlreadyInserted() ){ - // - // glrun->SetID(this->AssignRunID()); - // - glrun->SetID_RUN_FRAG(glrun1->GetID()); - glrun->Fill_GL_RUN(conn); - // - // set id number - // - glrun1->SetID_RUN_FRAG(glrun->GetID()); - glrun1->Fill_GL_RUN(conn); - // + if ( row ){ + if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n"); + if ( PEDANTIC ) throw -70; + } else { + if ( NoFrag() ){ + glrun->SetID_RUN_FRAG(glrun->GetID()); + glrun->Fill_GL_RUN(conn); + glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); + }; }; - // - // delete old entries in fragment table - // - glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); - glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); - // - delete glrun1; - // - return; - // - }; - // - }; - // - justcheck: - // - if ( !found ){ - // - if ( IsDebug() ) printf(" not found, check if we have already processed the file \n "); - // - // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table? - // - oss.str(""); - oss << " SELECT ID FROM GL_RUN 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 ){ - if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n"); - if ( PEDANTIC ) throw -70; - } else { - if ( NoFrag() ){ - glrun->SetID_RUN_FRAG(glrun->GetID()); - glrun->Fill_GL_RUN(conn); - glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); - }; }; - }; }; // EEE // return; @@ -3068,16 +3222,16 @@ code = eh->GetCounter(); checkfirst = 0; for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){ - if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter)); - }; + if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter)); + }; if ( IsDebug() ) printf(" Check first is %i firstev is %i\n",checkfirst,firstev); // T->GetEntry(lastev); code = eh->GetCounter(); checklast = 0; for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){ - if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter)); - }; + if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter)); + }; if ( IsDebug() ) printf(" Check last is %i lastev is %i\n",checklast,lastev); // if ( checkfirst == checklast ){ @@ -3092,7 +3246,7 @@ // // HERE WE MUST HANDLE THAT RUNS AND GO BACK // -// if ( IsDebug() ) printf(" Never seen this case, try to handle it anyway, it was throw -95\n"); + // if ( IsDebug() ) printf(" Never seen this case, try to handle it anyway, it was throw -95\n"); // Bool_t emptyruns = false; UInt_t check = 0; @@ -3184,378 +3338,339 @@ }; // this->SetCommonGLRUN(firstTime,lastTime); + this->SetPhysEndRunVariables(); // if ( chminentry == firstentry ){ // EEE if ( IsDebug() ) printf(" Inside isrunconsistent found a fragment of run at the beginning of the file, put it in the fragment table \n"); -// -// this->HandleRunFragments(true,mistrail,firstentry,lastentry); // cannot call it here since it enters a loop which will destroy the already stored variables if we arrive here from HandleRunFragments -// - - - - - - - - - - - - - - mishead = true; - - - UInt_t rhfirstev = firstentry; -// UInt_t rtlastev = lastentry; - Bool_t found = false; - Bool_t foundinrun = false; - // - TSQLResult *result = 0; - TSQLRow *row = 0; - // - stringstream oss; - oss.str(""); - // - // we have now the good first piece of a run, fill the glrun object - // -// if ( rhfirstev != firstev && !mishead ) mishead = true; -// if ( rtlastev != lastev && !mistrail ) mistrail = true; - // -// this->FillClass(mishead,mistrail,firstev,lastev); - // - if ( IsDebug() ) printf("zz The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n"); - if ( IsDebug() ) printf("zz C THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT()); - // - // 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"); - if ( PEDANTIC ) throw -69; -// return; - }; - // - if ( chewbacca && mishead && mistrail ) goto zjustcheck; - // - // can we find the other piece of the run in the GL_RUN_FRAGMENTS table? - // - if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is - // missing it no way we can found a piece in the frag table - // - 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() << " 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()); - result = conn->Query(oss.str().c_str()); - // - if ( !result ) throw -4; - // - 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; - } else { - // - found = false; // default value - // - if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n"); - // - // if we have both runheader and runtrailer we can check with pkt_counter: - // - if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){ - ULong64_t chkpkt = 0; - ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT()); - ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4))); - // - chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL; - // - if ( labs(chkpkt-pktt)<2 ){ - // - if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt); - // - found = true; - // - } else { - // - if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt); - // - found = false; - // - }; - }; - if ( !found && chewbacca ) goto zjustcheck; - if ( !found ){ - // - // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts - // - ULong64_t chkpkt1 = 0; - ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT()); - ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5))); - chkpkt1 = labs(orunh1-dbrunt1); - // - ULong64_t chkpkt2 = 0; - ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT()); - ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3))); - chkpkt2 = labs(orunh2-dbrunt2); - // - ULong64_t chkpkt3 = 0; - ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME()); - ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2))); - chkpkt3 = labs(orunh3-dbrunt3); - // - if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){ - // if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){ - // - if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3); - // - found = true; - // - } else { - // - if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3); - // - found = false; - // - }; - }; - }; - // - if ( found ){ - // - // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table - // - 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)); - // - oss.str(""); - oss << " ID="<GetField(0)<<";"; - // - glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos - // - // merge infos - // - UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT()); - ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT()); - UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT()); - ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT()); - if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev); - TTree *T= 0; - T = (TTree*)file->Get("Physics"); - if ( !T || T->IsZombie() ) throw -16; - EventHeader *eh = 0; - PscuHeader *ph = 0; - T->SetBranchAddress("Header", &eh); - while ( apkt > bpkt && aobt > bobt && firstev < lastev ){ - T->GetEntry(firstev); - ph = eh->GetPscuHeader(); - bpkt = PKT(ph->GetCounter()); - bobt = OBT(ph->GetOrbitalTime()); - firstev++; - if ( PEDANTIC ) throw -71; - }; - if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev); - // - glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER()); - glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER()); - glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME()); - glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT()); - glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT()); - // - 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()); - glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP()); - glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE()); - glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE()); - glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A()); - glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B()); - glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO()); - glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO()); - glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB()); - glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE()); - glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED()); - glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK()); - glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC()); - glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC()); - // - if ( !IsRunAlreadyInserted() ){ - // - // glrun->SetID(this->AssignRunID()); - glrun->SetID_RUN_FRAG(glrun1->GetID()); - glrun->Fill_GL_RUN(conn); - // - // set id number - // - glrun1->SetID_RUN_FRAG(glrun->GetID()); - glrun1->Fill_GL_RUN(conn); - // - }; - // delete old entry in fragment table - // - glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); - glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); - // - delete glrun1; - // - // -// return; - // - }; - // - }; - // - // - zjustcheck: - // - if ( !found ){ - // - if ( IsDebug() ) printf(" not found, check if we have already processed the file \n "); - // - // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table? - // - oss.str(""); - oss << " SELECT ID FROM GL_RUN 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 ){ - if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n"); - if ( PEDANTIC ) throw -70; - } else { - if ( NoFrag() ){ - glrun->SetID_RUN_FRAG(glrun->GetID()); - glrun->Fill_GL_RUN(conn); - glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); - }; - }; - }; // EEE - - - - - - - - - - - - - - - - - - - - - - - - - - - - + // + // this->HandleRunFragments(true,mistrail,firstentry,lastentry); // cannot call it here since it enters a loop which will destroy the already stored variables if we arrive here from HandleRunFragments + // + mishead = true; + UInt_t rhfirstev = firstentry; + // UInt_t rtlastev = lastentry; + Bool_t found = false; + Bool_t foundinrun = false; + // + TSQLResult *result = 0; + TSQLRow *row = 0; + // + stringstream oss; + oss.str(""); + // + // we have now the good first piece of a run, fill the glrun object + // + // if ( rhfirstev != firstev && !mishead ) mishead = true; + // if ( rtlastev != lastev && !mistrail ) mistrail = true; + // + // this->FillClass(mishead,mistrail,firstev,lastev); + // + if ( IsDebug() ) printf("zz The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n"); + if ( IsDebug() ) printf("zz C THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT()); + // + // 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"); + if ( PEDANTIC ) throw -69; + // return; + }; + // + if ( chewbacca && mishead && mistrail ) goto zjustcheck; + // + // can we find the other piece of the run in the GL_RUN_FRAGMENTS table? + // + if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is + // missing it no way we can found a piece in the frag table + // + 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() << " 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()); + result = conn->Query(oss.str().c_str()); + // + if ( !result ) throw -4; + // + 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; + } else { + // + found = false; // default value + // + if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n"); + // + // if we have both runheader and runtrailer we can check with pkt_counter: + // + if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){ + ULong64_t chkpkt = 0; + ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT()); + ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4))); + // + chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL; + // + if ( labs(chkpkt-pktt)<2 ){ + // + if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt); + // + found = true; + // + } else { + // + if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt); + // + found = false; + // + }; + }; + if ( !found && chewbacca ) goto zjustcheck; + if ( !found ){ + // + // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts + // + ULong64_t chkpkt1 = 0; + ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT()); + ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5))); + chkpkt1 = labs(orunh1-dbrunt1); + // + ULong64_t chkpkt2 = 0; + ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT()); + ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3))); + chkpkt2 = labs(orunh2-dbrunt2); + // + ULong64_t chkpkt3 = 0; + ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME()); + ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2))); + chkpkt3 = labs(orunh3-dbrunt3); + // + if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){ + // if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){ + // + if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3); + // + found = true; + // + } else { + // + if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3); + // + found = false; + // + }; + }; + }; + // + if ( found ){ + // + // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table + // + 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)); + // + oss.str(""); + oss << " ID="<GetField(0)<<";"; + // + glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos + // + // merge infos + // + UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT()); + ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT()); + UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT()); + ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT()); + if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev); + TTree *T= 0; + T = (TTree*)file->Get("Physics"); + if ( !T || T->IsZombie() ) throw -16; + EventHeader *eh = 0; + PscuHeader *ph = 0; + T->SetBranchAddress("Header", &eh); + while ( apkt > bpkt && aobt > bobt && firstev < lastev ){ + T->GetEntry(firstev); + ph = eh->GetPscuHeader(); + bpkt = PKT(ph->GetCounter()); + bobt = OBT(ph->GetOrbitalTime()); + firstev++; + if ( PEDANTIC ) throw -71; + }; + if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev); + // + glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER()); + glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER()); + glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME()); + glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT()); + glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT()); + // + 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()); + glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP()); + glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE()); + glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE()); + glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A()); + glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B()); + glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO()); + glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO()); + glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB()); + glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE()); + glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED()); + glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK()); + glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC()); + glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC()); + // + if ( glrun1->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glrun1->GetPHYSENDRUN_MASK_S3S2S12()); + if ( glrun1->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glrun1->GetPHYSENDRUN_MASK_S11CRC()); + // + if ( !IsRunAlreadyInserted() ){ + // + // glrun->SetID(this->AssignRunID()); + glrun->SetID_RUN_FRAG(glrun1->GetID()); + glrun->Fill_GL_RUN(conn); + // + // set id number + // + glrun1->SetID_RUN_FRAG(glrun->GetID()); + glrun1->Fill_GL_RUN(conn); + // + }; + // delete old entry in fragment table + // + glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); + glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); + // + delete glrun1; + // + // + // return; + // + }; + // + }; + // + // + zjustcheck: + // + if ( !found ){ + // + if ( IsDebug() ) printf(" not found, check if we have already processed the file \n "); + // + // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table? + // + oss.str(""); + oss << " SELECT ID FROM GL_RUN 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 ){ + if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n"); + if ( PEDANTIC ) throw -70; + } else { + if ( NoFrag() ){ + glrun->SetID_RUN_FRAG(glrun->GetID()); + glrun->Fill_GL_RUN(conn); + glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); + }; + }; + }; // EEE } else { - if ( !IsRunAlreadyInserted() ){ - glrun->SetID(this->AssignRunID()); - glrun->SetID_RUN_FRAG(0); - glrun->Fill_GL_RUN(conn); - }; + if ( !IsRunAlreadyInserted() ){ + glrun->SetID(this->AssignRunID()); + glrun->SetID_RUN_FRAG(0); + glrun->Fill_GL_RUN(conn); + }; }; // EEE // firstevno = lastentry + 1; @@ -3647,17 +3762,17 @@ code = eh->GetCounter(); checkfirst = 0; for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){ - if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter)); - if ( !strcmp(*Iter,"RunHeader") ) nrunh1++; - }; + if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter)); + if ( !strcmp(*Iter,"RunHeader") ) nrunh1++; + }; if ( IsDebug() ) printf(" Check first is %i \n",checkfirst); // T->GetEntry(lastev); code = eh->GetCounter(); checklast = 0; for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){ - if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter)); - }; + if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter)); + }; if ( IsDebug() ) printf(" Check last is %i \n",checklast); // if ( checkfirst == checklast ){ @@ -3769,6 +3884,7 @@ }; // this->SetCommonGLRUN(firstTime,lastTime); + this->SetPhysEndRunVariables(); // if ( !IsRunAlreadyInserted() ){ glrun->SetID(this->AssignRunID()); @@ -3808,6 +3924,7 @@ if ( IsDebug() ) printf(" We have the runtrailer \n"); // this->SetCommonGLRUN(firstTime,lastTime); + this->SetPhysEndRunVariables(); // if ( !IsRunAlreadyInserted() ){ glrun->SetID(this->AssignRunID()); @@ -4382,383 +4499,936 @@ * Fill the GL_TRK_CALIB table */ void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){ - // - TSQLResult *result = 0; - TSQLRow *row = 0; - // - stringstream oss; - oss.str(""); - // - UInt_t totime = 0; - // - if ( !pk1 && !pk2 ){ - if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n"); - return; - }; - // - // check if the calibration has already been inserted - // - oss.str(""); - oss << " SELECT ID FROM GL_TRK_CALIB WHERE " - << " BOOT_NUMBER = "<< this->GetBOOTnumber(); // - oss << " AND ( ( "; - if ( pk1 ){ - oss << " OBT1 = "<< obt1 << " AND " - << " PKT1 = "<< pkt1 - << " ) OR ( "; - } else { - oss << " PKT1 = "<< pkt2-1 - << " ) OR ( "; - }; - if ( pk2 ){ - oss << " OBT2 = "<< obt2 << " AND " - << " PKT2 = "<< pkt2; - } else { - oss << " PKT2 = "<< pkt1+1; - }; - oss << " ) );"; - // - if ( IsDebug() ) printf(" Check if the trk calibration has already 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 ){ - // - if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n"); - if ( PEDANTIC ) throw -80; + + GL_TRK_CALIB *glcal = new GL_TRK_CALIB(); // - } else { + glcal->ID = 0; + glcal->ID_ROOT_L0 = GetID_ROOT(); + glcal->EV_ROOT_CALIBTRK1 = t1; + glcal->EV_ROOT_CALIBTRK2 = t2; + glcal->FROM_TIME = fromtime; + glcal->TO_TIME = 0; + glcal->OBT1 = obt1; + glcal->OBT2 = obt2; + glcal->PKT1 = pkt1; + glcal->PKT2 = pkt2; + glcal->BOOT_NUMBER = GetBOOTnumber(); + glcal->VALIDATION = valid; // - // we have to insert a new calibration, check where to place it + HandleTRK_CALIB(glcal); // - oss.str(""); - oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE " - << " FROM_TIME < "<< fromtime << " AND " - << " TO_TIME > "<< fromtime << ";"; + delete glcal; +} +/** + * Fill the GL_TRK_CALIB table + */ +void PamelaDBOperations::HandleTRK_CALIB(GL_TRK_CALIB *glcal){ + + Bool_t pk1 = (glcal->OBT1>0&&glcal->PKT1>0); + Bool_t pk2 = (glcal->OBT2>0&&glcal->PKT2>0); + UInt_t boot_number = glcal->BOOT_NUMBER; + UInt_t obt1 = glcal->OBT1; + UInt_t obt2 = glcal->OBT2; + UInt_t pkt1 = glcal->PKT1; + UInt_t pkt2 = glcal->PKT2; + UInt_t fromtime = glcal->FROM_TIME; + UInt_t totime = 0; + UInt_t idroot = glcal->ID_ROOT_L0; + UInt_t t1 = glcal->EV_ROOT_CALIBTRK1; + UInt_t t2 = glcal->EV_ROOT_CALIBTRK2; + UInt_t valid = glcal->VALIDATION; // - if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str()); - result = conn->Query(oss.str().c_str()); + TSQLResult *result = 0; + TSQLRow *row = 0; // - if ( !result ) throw -4; + stringstream oss; + oss.str(""); // - row = result->Next(); // - if ( !row ){ - // - // no calibrations in the db contain our calibration - // - if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n"); - if ( fromtime < 1150871000 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode - // - oss.str(""); - oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE " - << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;"; - // - if ( IsDebug() ) printf(" Check the upper limit for calibration: 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 ){ - totime = numeric_limits::max(); - } else { - totime = (UInt_t)atoll(row->GetField(0)); - }; - // - } else { - // - // determine upper and lower limits and make space for the new calibration - // - totime = (UInt_t)atoll(row->GetField(1)); - // - oss.str(""); - oss << " UPDATE GL_TRK_CALIB SET " - << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[ - << " ID = "<< row->GetField(0) << ";"; - // - if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str()); - result = conn->Query(oss.str().c_str()); - // - if ( !result ) throw -4; - // + if ( !pk1 && !pk2 ){ + if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n"); + return; }; // - oss.str(""); - oss << " INSERT INTO GL_TRK_CALIB (ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION) " - << " VALUES (NULL,' " - << idroot << "',"; + // check if the calibration has already been inserted // - if ( !pk1 ){ - oss << "NULL,"; + oss.str(""); + oss << " SELECT ID FROM GL_TRK_CALIB WHERE " + << " BOOT_NUMBER = "<< boot_number; // + oss << " AND FROM_TIME="<GetBOOTnumber() << "','" - << valid << "');"; - // - if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str()); + oss << " PKT2 = "<< pkt1+1; + }; + oss << " ) );"; // + if ( IsDebug() ) printf(" Check if the trk calibration has already 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 ){ + // + if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n"); + if ( PEDANTIC ) throw -80; + // + } else { + // + // we have to insert a new calibration, check where to place it + // + oss.str(""); + oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE " + << " FROM_TIME < "<< fromtime << " AND " + << " TO_TIME > "<< fromtime << ";"; + // + if ( IsDebug() ) printf(" Check where to place the trk calibration: 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 calibrations in the db contain our calibration + // + if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n"); + if ( fromtime < 1150871000 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode + // + oss.str(""); + oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE " + << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;"; + // + if ( IsDebug() ) printf(" Check the upper limit for calibration: 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 ){ + totime = numeric_limits::max(); + } else { + totime = (UInt_t)atoll(row->GetField(0)); + }; + // + } else { + // + // determine upper and lower limits and make space for the new calibration + // + totime = (UInt_t)atoll(row->GetField(1)); + // + oss.str(""); + oss << " UPDATE GL_TRK_CALIB SET " + << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[ + << " ID = "<< row->GetField(0) << ";"; + // + if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + // + if ( !result ) throw -4; + // + }; + // + oss.str(""); + oss << " INSERT INTO GL_TRK_CALIB (ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION) " + << " VALUES (NULL,' " + << idroot << "',"; + // + if ( !pk1 ){ + oss << "NULL,"; + } else { + oss << "'" + << t1 << "',"; + }; + // + if ( !pk2 ){ + oss << "NULL,'"; + } else { + oss << "'" + << t2 << "','"; + }; + // + oss << fromtime << "','" + << totime << "','" + << obt1 << "','" + << pkt1 << "','" + << obt2 << "','" + << pkt2 << "','" + << boot_number << "','" + << valid << "');"; + // + if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str()); + // + result = conn->Query(oss.str().c_str()); + // + if ( !result ) throw -4; + // + }; + + oss.str(""); + oss << " SELECT ID FROM GL_TRK_CALIB ORDER BY ID DESC LIMIT 1 ;"; + if ( IsDebug() ) cout << oss.str().c_str() << endl; + result = conn->Query(oss.str().c_str()); + if ( !result ) throw -4;; + row = result->Next(); + if(row)glcal->ID = (UInt_t)atoll(row->GetField(0)); + + // }; -/** - * Scan tracker calibrations packets, fill the GL_TRK_CALIB table - */ -Int_t PamelaDBOperations::insertTRK_CALIB(){ - // - CalibTrk1Event *caltrk1 = 0; - CalibTrk2Event *caltrk2 = 0; - TTree *tr1 = 0; - TTree *tr2 = 0; - EventHeader *eh1 = 0; - PscuHeader *ph1 = 0; - EventHeader *eh2 = 0; - PscuHeader *ph2 = 0; - // - PacketType *pctp=0; - EventCounter *codt2=0; - // - Int_t nevents1 = 0; - Int_t nevents2 = 0; - // - fromtime = 0; - // - obt1 = 0; - pkt1 = 0; - obt2 = 0; - pkt2 = 0; - // - tr1 = (TTree*)file->Get("CalibTrk1"); - if ( !tr1 || tr1->IsZombie() ) throw -22; - tr2 = (TTree*)file->Get("CalibTrk2"); - if ( !tr2 || tr2->IsZombie() ) throw -23; - // - tr1->SetBranchAddress("CalibTrk1", &caltrk1); - tr1->SetBranchAddress("Header", &eh1); - nevents1 = tr1->GetEntries(); - tr2->SetBranchAddress("CalibTrk2", &caltrk2); - tr2->SetBranchAddress("Header", &eh2); - nevents2 = tr2->GetEntries(); - // - if ( !nevents1 && !nevents2 ) return(1); - // - t2 = -1; - Int_t pret2 = 0; - Int_t t2t1cal = 0; - // - for (t1=0; t1 < nevents1; t1++){ +/** + * Scan tracker calibrations packets, fill the GL_TRK_CALIB table + */ +Int_t PamelaDBOperations::insertTRK_CALIB(){ // - pret2 = t2; - tr1->GetEntry(t1); + CalibTrk1Event *caltrk1 = 0; + CalibTrk2Event *caltrk2 = 0; + TTree *tr1 = 0; + TTree *tr2 = 0; + EventHeader *eh1 = 0; + PscuHeader *ph1 = 0; + EventHeader *eh2 = 0; + PscuHeader *ph2 = 0; // - ph1 = eh1->GetPscuHeader(); - obt1 = ph1->GetOrbitalTime(); - pkt1 = ph1->GetCounter(); - fromtime = this->GetAbsTime(ph1->GetOrbitalTime()); + PacketType *pctp=0; + EventCounter *codt2=0; // -// valid = 1; -// // -// if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1 + Int_t nevents1 = 0; + Int_t nevents2 = 0; // + fromtime = 0; // - if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){ - // if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){ - // - if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u \n",fromtime,obt1,pkt1); - // - valid = ValidateTrkCalib( caltrk1, eh1 ); - if ( IsDebug() ) cout << " pkt1 validation --> "<Get("CalibTrk1"); + if ( !tr1 || tr1->IsZombie() ) throw -22; + tr2 = (TTree*)file->Get("CalibTrk2"); + if ( !tr2 || tr2->IsZombie() ) throw -23; + // + tr1->SetBranchAddress("CalibTrk1", &caltrk1); + tr1->SetBranchAddress("Header", &eh1); + nevents1 = tr1->GetEntries(); + tr2->SetBranchAddress("CalibTrk2", &caltrk2); + tr2->SetBranchAddress("Header", &eh2); + nevents2 = tr2->GetEntries(); + // + if ( !nevents1 && !nevents2 ) return(1); + // + t2 = -1; + Int_t pret2 = 0; + Int_t t2t1cal = 0; + // + bool MISSING_pkt1 = true; + bool MISSING_pkt2 = true; + int ncalib = 0; + bool try_to_recover = false; + // + for (t1=0; t1 < nevents1; t1++){//loop over packet1 // - t2++; + pret2 = t2; + tr1->GetEntry(t1); // - pret2 = t2 - 1; // EMILIANO + ph1 = eh1->GetPscuHeader(); + obt1 = ph1->GetOrbitalTime(); + pkt1 = ph1->GetCounter(); + fromtime = GetAbsTime(ph1->GetOrbitalTime()); // - if ( t2 < nevents2 ){ - tr2->GetEntry(t2); - codt2 = eh2->GetCounter(); - t2t1cal = codt2->Get(pctp->CalibTrk1); - // - ph2 = eh2->GetPscuHeader(); - obt2 = ph2->GetOrbitalTime(); - pkt2 = ph2->GetCounter(); - // - if ( IsDebug() ) printf(" This is a trk calibration2 at obt %u pkt %u t2 is %u , t2t1cal is %u \n",obt2,pkt2,t2,t2t1cal); -// if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2 - // + // chek if the packet number and obt are consistent with the other packets ??? + // + if ( PKT(pkt1) >= PKT(pktfirst) && PKT(pkt1) <= upperpkt && OBT(obt1) >= OBT(obtfirst) && OBT(obt1) <= upperobt ){ + // if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){ + // + if ( IsDebug() ) printf("\n Trk calibration1 %u at time %u obt %u pkt %u \n",t1,fromtime,obt1,pkt1); + // + valid = ValidateTrkCalib( caltrk1, eh1 ); + if ( IsDebug() ) cout << " pkt1 validation --> "<GetEntry(t2); + codt2 = eh2->GetCounter(); + t2t1cal = codt2->Get(pctp->CalibTrk1); + // + ph2 = eh2->GetPscuHeader(); + obt2 = ph2->GetOrbitalTime(); + pkt2 = ph2->GetCounter(); + // + if ( IsDebug() ) printf(" >> trk calibration2 at obt %u pkt %u t2 is %u , t2t1cal is %u \n",obt2,pkt2,t2,t2t1cal); + // if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2 + // + } else { + // + // running out of vector without finding the corresponding calibration, sig + // + if ( IsDebug() ) printf(" t2 >= nevents2 \n"); + pret2 = t2; + obt2 = 0; + // pkt2 = pkt1+2; + pkt2 = 0; + t2t1cal = t1+1; + }; + // if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) && (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){ + + // EMILIANO + // if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) || (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){ + // // if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){ + // if ( IsDebug() ) printf(" running out of vector without finding the corresponding calibration, sig \n"); + // // + // // running out of vector without finding the corresponding calibration, sig + // // + // pret2 = t2; + // obt2 = 0; + // // pkt2 = pkt1+2; + // pkt2 = 0; + // t2t1cal = t1+1; + // }; + + + // + }; + // + if ( IsDebug() ) printf(" Check if trk calibration2 is the right one \n"); + // + // EMILIANO + if ( ( PKT(pkt2) < PKT(pktfirst) || PKT(pkt2) > upperpkt) || (OBT(obt2) < OBT(obtfirst) || OBT(obt2) > upperobt) ){ + // if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){ + if ( IsDebug() ) printf(" *WARNING* The calibration found is outside the interval, sig \n"); + // + // running out of vector without finding the corresponding calibration, sig + // + pret2 = t2; + obt2 = 0; + pkt2 = 0; + }; + if ( PKT(pkt2) == PKT(pkt1)+1 ){ + if ( IsDebug() ) cout << " ...OK"< "<HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2); + // + // Check for missing calibtrk1 + // + if ( t2 != pret2+1 ){ + // + if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u pret2 is %u \n",obt2,pkt2,t2,pret2); + // + while ( t2 > pret2+1 ){ + // + // handle missing calib1 + // + pret2++; + // + obt1 = 0; + pkt1 = 0; + // + tr2->GetEntry(pret2); + ph2 = eh2->GetPscuHeader(); + obt2 = ph2->GetOrbitalTime(); + pkt2 = ph2->GetCounter(); + // + fromtime = this->GetAbsTime(ph2->GetOrbitalTime()); + // + valid = 0; + MISSING_pkt1 = true; + MISSING_pkt2 = false; +// this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2); + // + }; + // + }; + // + } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){ + // + // Check for missing calibtrk2 + // + if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2); + t2 = pret2; + // + // handle missing calib2 + // + obt2 = 0; + pkt2 = 0; + valid = 0; + MISSING_pkt1 = false; + MISSING_pkt2 = true; +// this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2); + // + }; + // + + if( !(MISSING_pkt1&MISSING_pkt2) ){ + this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2); + ncalib++; + if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true; + } + + } else { // - // running out of vector without finding the corresponding calibration, sig - // - if ( IsDebug() ) printf(" t2 >= nevents2 \n"); - pret2 = t2; - obt2 = 0; - // pkt2 = pkt1+2; - pkt2 = 0; - t2t1cal = t1+1; + if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u OUTSIDE the considered time interval \n",fromtime,obt1,pkt1); + // if ( PEDANTIC ) throw -79; + // }; - // if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) && (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){ + // - // EMILIANO -// if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) || (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){ -// // if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){ -// if ( IsDebug() ) printf(" running out of vector without finding the corresponding calibration, sig \n"); -// // -// // running out of vector without finding the corresponding calibration, sig -// // -// pret2 = t2; -// obt2 = 0; -// // pkt2 = pkt1+2; -// pkt2 = 0; -// t2t1cal = t1+1; -// }; + }; //end loop on pkt1 + + // + // we have one more calib pkt2 ! + // + t2++; + while ( t2 < nevents2 ){ // - }; - // - if ( IsDebug() ) printf(" Check if trk calibration2 exists \n"); - // - // EMILIANO - if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) || (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){ - // if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){ - if ( IsDebug() ) printf(" The calibration found is outside the interval, sig \n"); - // - // running out of vector without finding the corresponding calibration, sig - // - pret2 = t2; - obt2 = 0; - pkt2 = 0; - }; - // - // The calibration is good - // - if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){ - // - if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2); - if ( IsDebug() ) printf(" The trk calibration2 at obt %u pkt %u t2 is %u is good \n",obt2,pkt2,t2); - // - UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 ); - if ( IsDebug() ) cout << " pkt2 validation --> "<HandleTRK_CALIB(true,true); + if ( IsDebug() ) printf(" t2 is %u nevents2 is %u \n",t2,nevents2); + obt1 = 0; + pkt1 = 0; // - // Check for missing calibtrk1 + tr2->GetEntry(t2); + ph2 = eh2->GetPscuHeader(); + obt2 = ph2->GetOrbitalTime(); + pkt2 = ph2->GetCounter(); // - if ( t2 != pret2+1 ){ - // - if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u pret2 is %u \n",obt2,pkt2,t2,pret2); - // - while ( t2 > pret2+1 ){ - // - // handle missing calib1 - // - pret2++; - // - obt1 = 0; - pkt1 = 0; - // - tr2->GetEntry(pret2); - ph2 = eh2->GetPscuHeader(); - obt2 = ph2->GetOrbitalTime(); - pkt2 = ph2->GetCounter(); + fromtime = this->GetAbsTime(ph2->GetOrbitalTime()); + valid = 0; + // if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){ + // EMILIANO + if ( this->PKT(pkt2) >= this->PKT(pktfirst) && this->PKT(pkt2 <= upperpkt) && this->OBT(obt2) >= this->OBT(obtfirst) && this->OBT(obt2) <= upperobt ){ + // if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){ // - fromtime = this->GetAbsTime(ph2->GetOrbitalTime()); + if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2); // - valid = 0; - this->HandleTRK_CALIB(false,true); + MISSING_pkt1 = true; + MISSING_pkt2 = false; + this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2); + ncalib++; + if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true; // - }; - // }; // - } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){ - // - // Check for missing calibtrk2 - // - if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2); - t2 = pret2; + t2++; // - // handle missing calib2 + }; + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// ----------------------------------------------------------------- +// in case of corruption, check if the calibration can be recovered +// from another chewbacca file +// ----------------------------------------------------------------- +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +// cout <<" TRY TO RECOVER ?? "<>>> TRY TO RECOVER TRACKER CALIBRATIONS <<<<"<HandleTRK_CALIB(true,false); + stringstream oss; + oss.str(""); // - }; - // - } else { - // - if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u OUTSIDE the considered time interval \n",fromtime,obt1,pkt1); - // if ( PEDANTIC ) throw -79; - // - }; - // - }; - // - // we have one more calib pkt2 ! - // - t2++; - while ( t2 < nevents2 ){ - // - // handle missing calib1 - // - if ( IsDebug() ) printf(" t2 is %u nevents2 is %u \n",t2,nevents2); - obt1 = 0; - pkt1 = 0; - // - tr2->GetEntry(t2); - ph2 = eh2->GetPscuHeader(); - obt2 = ph2->GetOrbitalTime(); - pkt2 = ph2->GetCounter(); - // - fromtime = this->GetAbsTime(ph2->GetOrbitalTime()); - valid = 0; -// if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){ - // EMILIANO - if ( this->PKT(pkt2) >= this->PKT(pktfirst) && this->PKT(pkt2 <= upperpkt) && this->OBT(obt2) >= this->OBT(obtfirst) && this->OBT(obt2) <= upperobt ){ - // if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){ - // - if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2); - // - this->HandleTRK_CALIB(false,true); - // - }; - // - t2++; - // - }; + + //////////////////////////////////////////////////////////////////////// + // retrieve the name of the current file: + //////////////////////////////////////////////////////////////////////// + oss.str(""); + oss << "SELECT NAME FROM GL_ROOT where ID=" << GetID_ROOT() <<";"; + if ( IsDebug() ) cout << oss.str().c_str() << endl; + + result = conn->Query(oss.str().c_str()); + if ( !result ) throw -4;; + row = result->Next(); + TString thisfilename = (TString)row->GetField(0); + if ( IsDebug() ) cout << "Current file ==> "<Query(oss.str().c_str()); + if ( !result ) throw -4;; + if ( IsDebug() ) cout <<"Rows: "<GetRowCount()<Next(); + if(!row)break; + + UInt_t id = (UInt_t)atoll(row->GetField(0)); + UInt_t fromtime = (UInt_t)atoll(row->GetField(1)); + UInt_t obt1 = (UInt_t)atoll(row->GetField(2)); + UInt_t pkt1 = (UInt_t)atoll(row->GetField(3)); + UInt_t obt2 = (UInt_t)atoll(row->GetField(4)); + UInt_t pkt2 = (UInt_t)atoll(row->GetField(5)); + UInt_t boot = (UInt_t)atoll(row->GetField(6)); + UInt_t valid = (UInt_t)atoll(row->GetField(7)); + bool MISSING_pkt1 = (row->GetFieldLength(8)==0); + bool MISSING_pkt2 = (row->GetFieldLength(9)==0); + UInt_t totime = (UInt_t)atoll(row->GetField(10)); + + // ------------------------------------- + // ...check if the entry is corrupted... + // ------------------------------------- + cout <<"*** "<< MISSING_pkt1 << MISSING_pkt2 << valid < corrupted ? "< "< "<1) pkt1 = pkt2-1;//serve dopo + } + if( !MISSING_pkt2 ){ + oss << " AND "; + oss << " PKT_NUMBER_INIT < "< "< "<0) pkt2 = pkt1+1;//serve dopo + } + if( boot> 0 ){ + oss << " AND "; + oss << " BOOT_NUMBER = "<Query(oss.str().c_str()); + if ( !result2 ) throw -4;; + if ( IsDebug() ) cout <<"Rows: "<GetRowCount()<Next(); + if(!row2)break; + + // ------------------------------------------------------ + // ... a repetition is found ... + // ------------------------------------------------------ + path = (TString)row2->GetField(0); + name = (TString)row2->GetField(1); + raw = (TString)row2->GetField(5); + obt0 = (UInt_t)atoll(row2->GetField(2)); + timesync = (UInt_t)atoll(row2->GetField(3)); + boot_number = (UInt_t)atoll(row2->GetField(4)); + + if ( IsDebug() ) cout << "- - - - - - - - - - -" <ExpandPathName(path.Data())).Data(),name.Data()); + TFile *file = new TFile(((TString)gSystem->ExpandPathName(path.Data()))+"/"+name); // EM, path could be symbolic and we must expand it + if(!file)throw -100; + if(file->IsZombie())throw -100; + // + tr1 = (TTree*)file->Get("CalibTrk1"); + if ( !tr1 || tr1->IsZombie() ) throw -22; + tr2 = (TTree*)file->Get("CalibTrk2"); + if ( !tr2 || tr2->IsZombie() ) throw -23; + // + tr1->SetBranchAddress("CalibTrk1", &caltrk1); + tr1->SetBranchAddress("Header", &eh1); + nevents1 = tr1->GetEntries(); + tr2->SetBranchAddress("CalibTrk2", &caltrk2); + tr2->SetBranchAddress("Header", &eh2); + nevents2 = tr2->GetEntries(); + for(this_t1=0; this_t1GetEntry(this_t1); + if( + (UInt_t)eh1->GetPscuHeader()->GetCounter() == pkt1 && + true) break; + this_MISSING_pkt1 = true; + } + for(this_t2=0; this_t2GetEntry(this_t2); + if( + (UInt_t)eh2->GetPscuHeader()->GetCounter() == pkt2 && + true) break; + this_MISSING_pkt2 = true; + } + this_valid = + ValidateTrkCalib( caltrk1, eh1, file ) + * + ValidateTrkCalib( caltrk2, eh2, file ); + + // --------------------------------------------------------------------- + // accept the calibration if it is better than the previous: + // + // - if the new calibration is perfect (both valid packets) + // - if the new calibration has both the packets and the previous does not + // --------------------------------------------------------------------- + if( + ( !this_MISSING_pkt1&&!this_MISSING_pkt2&&this_valid )|| + ( (MISSING_pkt1||MISSING_pkt2) && (!this_MISSING_pkt1&&!this_MISSING_pkt2) )|| + false)FOUND=true; + + if(file)file->Close(); + + if(FOUND)break; + + }while(1);//endl loop over root table entries + + if(FOUND)break; + + }//end loop over tables + + if(FOUND){ + + if ( IsDebug() ) cout << " >>> REPETITION FOUND :-) <<<" <BaseName(raw.Data()) <<"\";"; +// if ( IsDebug() ) cout << oss.str().c_str() << endl; + +// result = conn->Query(oss.str().c_str()); +// if ( !result ) throw -4;; +// if ( IsDebug() ) cout <<"Rows: "<GetRowCount()<GetRowCount() == 0){ +// if ( IsDebug() ) cout << " << Insert new RAW file >> "<DirName(raw.Data()); +// glraw.NAME = gSystem->BaseName(raw.Data()); +// glraw.BOOT_NUMBER = boot_number; +// // +// insertPamelaRawFile( &glraw ); +// // +// id_raw = glraw.ID; +// }else{ +// row = result->Next(); +// id_raw = (UInt_t)atoll(row->GetField(0)); +// } +// if ( IsDebug() ) cout << "ID_RAW = "<Query(oss.str().c_str()); + if ( !result ) throw -3; + row = result->Next(); + if ( !row ) throw -3; + idtimesync = (UInt_t)atoll(row->GetField(0)); + if ( IsDebug() ) cout << "ID_TIMESYNC = "<BaseName(name.Data()) <<"\";"; + if ( IsDebug() ) cout << oss.str().c_str() << endl; + + result = conn->Query(oss.str().c_str()); + if ( !result ) throw -4;; + if ( IsDebug() ) cout <<"Rows: "<GetRowCount()<GetRowCount() == 0){ + if ( IsDebug() ) cout << " << Insert new ROOT file >> "<ExpandPathName(path); + } else { + if ( KEEPENV ){ + glroot.PATH = path; + } else { + glroot.PATH = "$PAM_L0"; + }; + }; + // glroot.PATH = path; + glroot.NAME = name; + // + insertPamelaRootFile( &glroot ); + // + id_root = glroot.ID; + }else{ + row = result->Next(); + if(row)id_root = (UInt_t)atoll(row->GetField(0)); + } + if ( IsDebug() ) cout << "ID_ROOT = "<> "<Query(oss.str().c_str()); + if ( !result ) throw -4;; + + // --------------------------------------------------------------------- + // step 4: insert the new calibration: + // --------------------------------------------------------------------- + if ( IsDebug() ) cout << " << Insert new TRK calibration >> "< missing or corrupted "<GetAbsTime(obt1); +// if( pkt1 == 0 ){//missing +// time = this->GetAbsTime(obt2); +// pkt = pkt2-1; +// } +// // +// }else if (which_is_not_valid==2 || which_is_not_valid==3){ +// // +// cout << "PKT2--> missing or corrupted "<GetAbsTime(obt2); +// if( pkt2 == 0 ){//missing +// time = this->GetAbsTime(obt1); +// pkt = pkt1+1; +// } +// // +// }else{ +// cout << "this should not happen!!! "<>>> "<<(path+r)->c_str() << endl; +// /// verifica che il file non sia quello gia` aperto +// } + +// //////////////////////////////////////////////////////////////////////// + +// TSQLResult *result = 0; +// TSQLRow *row = 0; +// // +// stringstream oss; +// oss.str(""); +// // ---------------------------------------- +// // read the id of last calibration inserted +// // ---------------------------------------- +// oss.str(""); +// oss << " SELECT "; +// oss << " (ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION) "; +// oss << " ORDER BY ID DESC LIMIT 1; "; + +// result = conn->Query(oss.str().c_str()); +// row = result->Next(); +// if( !row )throw -666; + +// if( result )delete result; +// if( row )delete row; + +// UInt_t id = (UInt_t)atoll(row->GetField(0)); + +// // ------------------------------------- +// // ...and modify it with new parameters +// // ------------------------------------- + + +// } // return(0); }; @@ -5061,9 +5731,15 @@ << " RM_ACQ_SETTING_MODE=" << glrun->GetRM_ACQ_SETTING_MODE()<< " , " << " TRK_CALIB_USED=" << glrun->GetTRK_CALIB_USED()<< " , " << " CAL_DSP_MASK=" << glrun->GetCAL_DSP_MASK()<< " , " - << " LAST_TIMESYNC=" << glrun->GetLAST_TIMESYNC()<< " , " - << " OBT_TIMESYNC=" << glrun->GetOBT_TIMESYNC() - << " WHERE ID=" << myid[u] << ";"; + << " LAST_TIMESYNC=" << glrun->GetLAST_TIMESYNC()<< " , "; + // + if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ) + oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12() << " , "; + if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) + oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC() << " , "; + // + oss << " OBT_TIMESYNC=" << glrun->GetOBT_TIMESYNC(); + oss << " WHERE ID=" << myid[u] << ";"; conn->Query(oss.str().c_str()); }; // @@ -5074,8 +5750,14 @@ for (UInt_t u=1; u <= nid ; u++){ oss.str(""); oss << "UPDATE GL_RUN_FRAGMENTS SET " - << " RUNHEADER_TIME=" << glrun->GetRUNHEADER_TIME()<< " , " - << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , " + << " RUNHEADER_TIME=" << glrun->GetRUNHEADER_TIME()<< " , "; + // + if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ) + oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; + if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) + oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; + // + oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , " << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< ";"; conn->Query(oss.str().c_str()); }; @@ -5098,8 +5780,14 @@ << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , " << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , " << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< " , " - << " PKT_COUNTER=" << glrun->GetPKT_COUNTER()<< " , " - << " PKT_READY_COUNTER=" << glrun->GetPKT_READY_COUNTER() + << " PKT_COUNTER=" << glrun->GetPKT_COUNTER()<< " , "; + // + if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ){ + oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; }; + if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) { + oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; }; + // + oss << " PKT_READY_COUNTER=" << glrun->GetPKT_READY_COUNTER() << " WHERE ID=" << myid[u] << ";"; conn->Query(oss.str().c_str()); }; @@ -5112,8 +5800,14 @@ oss.str(""); oss << "UPDATE GL_RUN_FRAGMENTS SET " << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , " - << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , " - << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< ";"; + << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , "; + // + if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ){ + oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; }; + if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){ + oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; }; + // + oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< ";"; conn->Query(oss.str().c_str()); }; }; @@ -5272,29 +5966,29 @@ 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++; // @@ -5306,13 +6000,13 @@ // // 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; -// // + // 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(); }; }; @@ -5457,7 +6151,7 @@ GL_RUN* this_run = new GL_RUN(); GL_RUN* next_run = new GL_RUN(); Int_t nseq_max = 1000; -// UInt_t* sequence = new UInt_t[100]; + // UInt_t* sequence = new UInt_t[100]; vector sequence(nseq_max); Int_t nseq = 0; Bool_t CHECK = false; @@ -5472,82 +6166,82 @@ // --------------------------------------------------------------------------------- while(1){ - row = result->Next(); - if( row == NULL ) break; + row = result->Next(); + if( row == NULL ) break; - //------------ - //get run info - //------------ - this_run->Set_GL_RUN(row); + //------------ + //get run info + //------------ + this_run->Set_GL_RUN(row); - 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"); - this_ONLINE = false; - this_BAD = true; - }; + 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"); + this_ONLINE = false; + this_BAD = true; + }; - //----------------------------------- - //compare with previous(next in time) - //----------------------------------- - CHECK = false; - UInt_t interval=0; + //----------------------------------- + //compare with previous(next in time) + //----------------------------------- + CHECK = false; + UInt_t interval=0; - if( nrow != 0){ + if( nrow != 0){ - t1 = this_run->GetRUNTRAILER_TIME(); - t2 = next_run->GetRUNHEADER_TIME(); - interval = (t2-t1); + t1 = this_run->GetRUNTRAILER_TIME(); + t2 = next_run->GetRUNHEADER_TIME(); + interval = (t2-t1); - if(this_ONLINE && next_ONLINE){ // this: ON-LINE + next: ON-LINE + if(this_ONLINE && next_ONLINE){ // this: ON-LINE + next: ON-LINE - if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0; //=> run fragments + if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0; //=> run fragments - if( interval >= calibtime )CHECK = true; //more than calibtime s => there might be a calibration + 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); - nseq=0; - } + if( !CHECK && this_run->VALIDATION ){ + for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true); + nseq=0; + } - }else if( !this_ONLINE && next_ONLINE) { // this: DEFAULT + next:ON-LINE + }else if( !this_ONLINE && next_ONLINE) { // this: DEFAULT + next:ON-LINE - CHECK = true; + CHECK = true; - }else if( !next_ONLINE ){ // this:ANY + next:DEFAULT + }else if( !next_ONLINE ){ // this:ANY + next:DEFAULT - assignVALIDATION(next_run->ID,true); - nseq=0; - } - } + assignVALIDATION(next_run->ID,true); + nseq=0; + } + } - //---------------------------- - //check run sequence for calib - //---------------------------- - if( CHECK ){ - // check if calibration exists - 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; - }; - //-------------- - //store run info - //-------------- - *next_run = *this_run; - next_ONLINE = this_ONLINE; - if( !this_BAD ){ - if(nseq < nseq_max){ - sequence[nseq] = this_run->ID; - nseq++; - }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max); - }; + //---------------------------- + //check run sequence for calib + //---------------------------- + if( CHECK ){ + // check if calibration exists + 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; + }; + //-------------- + //store run info + //-------------- + *next_run = *this_run; + next_ONLINE = this_ONLINE; + if( !this_BAD ){ + if(nseq < nseq_max){ + sequence[nseq] = this_run->ID; + nseq++; + }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max); + }; - if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID); - nrow++; + if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID); + nrow++; }; delete this_run; @@ -5563,41 +6257,41 @@ */ Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){ - GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB(); + GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB(); - // get the closest VALIDATED calibration before the run start (t2) - if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true); //>>> missing + // get the closest calibration before the run start (t2) + if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true); //>>> missing - if ( trkcalib->TO_TIME < t2 ) return(true); //>>> missing + if ( trkcalib->TO_TIME < t2 ) return(true); //>>> missing - //============================================================== - // Check is done first on the basis of time between calibration, - // which should be equal to the time between ascending-nodes. - //============================================================== - if ( t2 - trkcalib->FROM_TIME > 5700) { - if ( IsDebug() )printf("Long time between calib and run start %u :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME); - //============================================================== - // there might be a missing calibration, due to: - // - MM full - // - corrupted packets - // - loss of data - // There is an exception in case a download was done during ascending node - //============================================================== - Bool_t DOWNLOAD = false; - // check if the calib was skipped becouse of download .... DA FARE!! - if(DOWNLOAD)return(false); + //============================================================== + // Check is done first on the basis of time between calibration, + // which should be equal to the time between ascending-nodes. + //============================================================== + if ( t2 - trkcalib->FROM_TIME > 5700) { + if ( IsDebug() )printf("Long time between calib and run start %u :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME); + //============================================================== + // there might be a missing calibration, due to: + // - MM full + // - corrupted packets + // - loss of data + // There is an exception in case a download was done during ascending node + //============================================================== + Bool_t DOWNLOAD = false; + // check if the calib was skipped becouse of download .... DA FARE!! + if(DOWNLOAD)return(false); - return(true); //>>> missing + return(true); //>>> missing - }; + }; - //============================================================== - // If the last calibration is close to the run less than this time, - // it is enough to say that there are no missing calibrations - //============================================================== - // the long time interval bewteen runs might be due to download - if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME); - return(false); + //============================================================== + // If the last calibration is close to the run less than this time, + // it is enough to say that there are no missing calibrations + //============================================================== + // the long time interval bewteen runs might be due to download + if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME); + return(false); }; /** @@ -5606,17 +6300,17 @@ * @param validation true/false */ Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){ - TSQLResult *result = 0; - stringstream oss; - oss.str(""); - oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";"; - // -// if ( IsDebug() ) -// 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); + TSQLResult *result = 0; + stringstream oss; + oss.str(""); + oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";"; + // + // if ( IsDebug() ) + // 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); } @@ -6259,17 +6953,43 @@ * Rearrange calibration tables * **/ -UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){ +UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh , TFile *file){ + if(!caltrk) return 0; + + if ( IsDebug() ) cout << "ValidateTrkCalib:"<GetName(); + +// ---------------------------------- +// Check CRCs and failed calibrations +// ---------------------------------- for(Int_t ipkt=0; ipkt<6; ipkt++){ - if( caltrk->crc_hcal[ipkt] )return 0; // :-( - for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-( + if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){ + if( caltrk->crc_hcal[ipkt] ){ +// if(IsDebug())cout<<"(CRC Header)"; + validate = 0; + if(IsDebug())cout <DSPnumber[ipkt]<<")"; + + } + for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] ){ +// if(IsDebug())cout<<"(CRC Pkt-"<DSPnumber[ipkt]<<" ladder "<ncalib_event[ipkt]==0 && caltrk->cal_flag[ipkt]==0) ){ + if(IsDebug())cout <DSPnumber[ipkt]<<")"; + validate = 0; + } + }else{ +// validate=0; + if(IsDebug())cout <DSPnumber[ipkt] <<" @pkt "<GetName(); UInt_t base=0; UInt_t mask=0; if(classname.Contains("CalibTrk1Event")){ @@ -6306,69 +7025,77 @@ base=18; mask=0xFC0000; } -// ------------------------------------------------- -// Count number of packets and set build variable -// ------------------------------------------------- +// ---------------------------------------------------- +// Count number of valid packets and set build variable +// ---------------------------------------------------- + if(IsDebug())cout <DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){ + if(IsDebug())cout <<" "<DSPnumber[ipkt]; npkts++; build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) ); - } +// cout << caltrk->DSPnumber[ipkt] + }; } -// if( npkts==6 )return 1; // :-) - -// cout << classname << " "<GetPscuHeader()->GetOrbitalTime()< "<< hex << build << dec; +// ---------------------------------------------------- +// If the number of valid packets is 6, ok exit... +// ---------------------------------------------------- + if( npkts==6 ){ + return validate; // exit + } +//////////////////////////////////////////////////////// +// ...otherwise there might be some missing packets +// +// In this case check the acq configuration // (some DSPs might be excluded from acquisition) -// ----------------------------------------------- +//////////////////////////////////////////////////////// + if(!eh || !file || (file&&file->IsZombie()) ){ + if ( IsDebug() )cout << " *** MISSING VIEW *** eh="<GetCounter(); - Int_t irun = cod->Get(pctp->RunHeader); - TTree *rh=(TTree*)file->Get("RunHeader"); - if ( !rh || rh->IsZombie() ) throw -17; - if( rh->GetEntries() == irun ){ - if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1) -- cannot validate :-( "<SetBranchAddress("RunHeader", &run); - rh->SetBranchAddress("Header", &hrun); - rh->GetEntry(irun); -// cout << classname << " "<GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){ - if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<RM_ACQ_AFTER_CALIB ){ - if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0 -- cannot validate :-( "<GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime()); - if( dtime > timeaftercalib ){ - if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<ACQ_BUILD_INFO & mask) != build ){ - if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<ACQ_BUILD_INFO&mask) << " != "<< build << dec<GetCounter(); + Int_t irun = cod->Get(pctp->RunHeader); + TTree *rh=(TTree*)file->Get("RunHeader"); + if ( !rh || rh->IsZombie() ) throw -17; + if( rh->GetEntries() <= irun ){ + if ( IsDebug() ) cout << " *** MISSING-PKT *** no runs after calib (1) -- cannot validate :-( "<SetBranchAddress("RunHeader", &run); + rh->SetBranchAddress("Header", &hrun); + rh->GetEntry(irun); + if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){ + if ( IsDebug() ) cout << " *** MISSING-PKT *** no runs after calib (2) -- cannot validate :-( "<GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime()); + if( dtime > timeaftercalib ){ + if ( IsDebug() ) cout << " *** MISSING-PKT *** run after calib too far ( "< "<ACQ_BUILD_INFO & mask)<ACQ_BUILD_INFO & mask) != build ){ + validate=0; // :-( + cout < 0 && nid <= from ) goto ss; if ( to > 0 && nid >= to ) goto ss; -// + // if ( (UInt_t)atoll(row->GetField(4)) > 1 ){ // // @@ -6455,7 +7182,7 @@ printf(" CHECK n.4 RUNs %u and %u HAVE SAME RUNHEADER \n",thisid,(UInt_t)atoll(row3->GetField(0))); row3 = result3->Next(); }; -// delete result3; + // delete result3; }; // @@ -6476,7 +7203,7 @@ printf(" CHECK n.5 RUNs %u and %u HAVE SAME RUNTRAILER \n",thisid,(UInt_t)atoll(row3->GetField(0))); row3 = result3->Next(); }; -// delete result3; + // delete result3; }; // oss.str(""); @@ -6496,7 +7223,7 @@ printf(" CHECK n.7 RUNs %u and %u HAVE SAME RUNTRAILER AND RUNHEADER (ARE THE SAME?) \n",thisid,(UInt_t)atoll(row3->GetField(0))); row3 = result3->Next(); }; - // delete result3; + // delete result3; }; }; @@ -6517,7 +7244,7 @@ printf(" CHECK n.6 RUN %u CONTAINS RUN %u \n",thisid,(UInt_t)atoll(row3->GetField(0))); row3 = result3->Next(); }; -// delete result3; + // delete result3; }; // }; @@ -6543,7 +7270,7 @@ thisf = (TString)row2->GetField(0); if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data()); test = 1; -// delete result2; + // delete result2; }; // if ( (thisrtt < prevrht) && (thisrht != prevrht) ){ @@ -6565,7 +7292,7 @@ thisf = (TString)row2->GetField(0); if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data()); test = 1; -// delete result2; + // delete result2; }; // if ( (thisrht > thisrtt) && (thisrht != prevrht) ){ @@ -6587,17 +7314,17 @@ thisf = (TString)row2->GetField(0); if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data()); test = 1; -// delete result2; + // delete result2; }; -ss: + ss: // prevrht = thisrht; prevrtt = thisrtt; previd = thisid; prevl0id = thisl0id; row = result->Next(); -// if ( result2 ) delete result2; -// if ( result3 ) delete result3; + // if ( result2 ) delete result2; + // if ( result3 ) delete result3; }; // return(test);