--- chewbacca/YodaProfiler/src/PamelaDBOperations.cpp 2008/09/25 15:30:10 1.3 +++ chewbacca/YodaProfiler/src/PamelaDBOperations.cpp 2009/01/13 13:38:15 1.23 @@ -58,8 +58,9 @@ * @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){ // + RELAXED = false; chewbacca = false; chminentry = 0; chID = 0; @@ -76,6 +77,8 @@ // }; // + KEEPENV = false; + if ( keepenv ) KEEPENV=true; STATIC=false; if ( staticp ) STATIC=true; // @@ -99,6 +102,7 @@ if (INSERT_RAW) SetRawName(filerawname); // INSERT_ROOT = !filerootname.IsNull(); + if ( INSERT_ROOT ) this->SetRootName(filerootname); this->SetOrbitNo(dwinput); // this->SetID_RAW(0); @@ -132,13 +136,15 @@ stringstream qu; TSQLResult *result = 0; TSQLResult *result2 = 0; + TSQLResult *result3 = 0; TSQLRow *row = 0; TSQLRow *row2 = 0; + TSQLRow *row3 = 0; TString chpath; 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,REAL_TIME_INIT 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 +155,8 @@ 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)); + chrtinit = (UInt_t)atoll(row->GetField(6)); } else { throw -84; }; @@ -157,19 +165,41 @@ }; delete result; qu.str(""); - qu << "SELECT FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,TIME_OFFSET,BOOT_NUMBER,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL from ROOT_TABLE where ID_N=" << ridn << ";"; + qu << "SELECT FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,TIME_OFFSET,BOOT_NUMBER,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,REAL_TIME_INIT from ROOT_TABLE where ID_N=" << ridn << ";"; if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str()); result2 = conn->Query(qu.str().c_str()); 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)); chresursts = (UInt_t)atoll(row2->GetField(4)); chboot = (UInt_t)atoll(row2->GetField(5)); // + if ( !chboot ){ + qu.str(""); + // qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND ABS(LAST_TIME_SYNC_INFO-"<< chlastts <<")<10000 AND ABS(REAL_TIME_INIT-"<< row2->GetField(10) <<")<7200 group by BOOT_NUMBER order by BOOT_NUMBER asc;"; + qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND ABS(LAST_TIME_SYNC_INFO-"<< chlastts <<")<40000 AND ABS(REAL_TIME_INIT-"<< row2->GetField(10) <<")<40000 group by BOOT_NUMBER order by BOOT_NUMBER asc;"; // relax conditions... 090112 [8RED: error -29] + if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str()); + result3 = conn->Query(qu.str().c_str()); + if ( result3 ){ + row3 = result3->Next(); + if ( row3 && result3->GetRowCount() == 1 ){ + chboot = (UInt_t)atoll(row3->GetField(0)); + if ( debug ) printf(" Found boot_number = %u \n",chboot); + } else { + if ( debug ) printf(" AGH CANNOT DETERMINE THE BOOT NUMBER... \n"); + throw -29; + }; + }; + }; + // chpktinit = (UInt_t)atoll(row2->GetField(6)); chpktfinal = (UInt_t)atoll(row2->GetField(7)); chobtinit = (UInt_t)atoll(row2->GetField(8)); @@ -182,13 +212,15 @@ throw -85; }; filerootname = chpath + chfile;// + ".root"; - if ( debug ) printf(" chewbacca: filename is %s \n",filerootname.Data()); }; this->SetRootName(filerootname); - file = TFile::Open(this->GetRootName().Data()); - } else { - this->SetRootName(""); }; + if ( debug ) printf(" Filename is %s \n",filerootname.Data()); + file = TFile::Open(this->GetRootName().Data()); + // } else { + // this->SetRootName(""); + // this->SetRootName(filerootname); + // }; } // @@ -388,23 +420,28 @@ return; }; // - TString name = this->GetRootFile(); - Int_t nlength = name.Length(); - if ( nlength < 5 ) return; - TString dwo = 0; - for (Int_t i = 0; i<5; i++){ - dwo.Append(name[i],1); - }; - if ( dwo.IsDigit() ){ - dworbit = (UInt_t)dwo.Atoi(); - } else { - dwo=""; - for (Int_t i = 8; i<13; i++){ + if ( !chewbacca ){ + TString name = this->GetRootFile(); + Int_t nlength = name.Length(); + if ( nlength < 5 ){ + if ( IsDebug() ) printf(" Agh problems determining the orbit number! name = %s \n",name.Data()); + return; + }; + TString dwo = 0; + for (Int_t i = 0; i<5; i++){ dwo.Append(name[i],1); - }; - if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi(); + }; + if ( dwo.IsDigit() ){ + dworbit = (UInt_t)dwo.Atoi(); + } else { + dwo=""; + for (Int_t i = 8; i<13; i++){ + dwo.Append(name[i],1); + }; + if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi(); + }; + if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data()); }; - if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data()); return; }; @@ -435,9 +472,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")+'/'); + }; }; }; @@ -649,8 +690,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(); @@ -658,7 +700,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; @@ -682,8 +724,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(); @@ -777,6 +821,8 @@ ph = eh->GetPscuHeader(); pktlast = ph->GetCounter(); nevent = upperentry - chminentry; + if ( ((Int_t)upperentry - (Int_t)chminentry) < 0 ) nevent = 0; // nevent is UInt_t! 090112 [8RED: error -88] + if ( IsDebug() ) printf(" Setting nevent to %u - upperentry %i chminentry %i \n",nevent,upperentry,chminentry); // } else { upperpkt = PKT(pktlast); @@ -785,6 +831,7 @@ }; // if ( chewbacca && nevent < 1 ) { + if ( IsDebug() ) printf(" chewbacca and no events in the file \n"); pktfirst = chpktmin; upperpkt = PKT(chpktmax); pktlast = chpktmax; @@ -1125,7 +1172,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(""); @@ -1273,7 +1328,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; @@ -1330,7 +1385,7 @@ // // 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 ( obt < ((Long64_t)pobtfirst/2) && pobtfirst > (numeric_limits::max()/2) ){ // 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()); }; @@ -1449,6 +1504,14 @@ glrun->SetRUNTRAILER_PKT(lastPkt); // }; + // + // ad hoc solution for INF EV_TO [8RED] 090113 + // + if ( !firstev && lastev == -1 ){ + if ( IsDebug() ) printf(" EE firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1); + firstev = 1; + lastev = 0; + }; glrun->SetEV_FROM((UInt_t)firstev); glrun->SetEV_TO((UInt_t)lastev); glrun->SetNEVENTS((UInt_t)lastev-(UInt_t)firstev+1); @@ -1468,25 +1531,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); } @@ -1519,6 +1618,7 @@ result = conn->Query(oss.str().c_str()); if ( !result ) throw -10; row = result->Next(); + if ( !row ) throw -92; idresof = (UInt_t)atoll(row->GetField(0)); existsts = true; goto eout; @@ -1540,7 +1640,7 @@ if ( !row ){ oss.str(""); oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< " - << dworbit << " order by FROM_ORBIT desc limit 1;"; + << dworbit << " AND SPECIAL_FILE='' order by FROM_ORBIT desc limit 1;"; if ( IsDebug() ) printf(" %s \n",oss.str().c_str()); result = conn->Query(oss.str().c_str()); if ( !result ) throw -10; @@ -1798,94 +1898,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; // @@ -2143,7 +2281,7 @@ ptht = cod->Get(pctp->RunHeader) - 1; // evbeft = cod->Get(pctp->Physics); tcod = (UInt_t)cod->Get(pctp->Physics); - if ( !tcod ) tcod = 1; + if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT evbeft = TMath::Min(upperentry,(tcod-1)); if ( debug ) printf(" Loop in runtrailers, evbeft is %u upperentry %u cod->getetc %u \n",evbeft,upperentry,cod->Get(pctp->Physics)); // @@ -2182,7 +2320,7 @@ obth = phh->GetOrbitalTime(); cod = ehh->GetCounter(); tcod = (UInt_t)cod->Get(pctp->Physics); - if ( !tcod ) tcod = 1; + if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT evbefh = TMath::Max(chminentry,(tcod-1)); // if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){ @@ -2221,6 +2359,7 @@ // this->HandleRunFragments(true,true,chminentry,(evbefh)); // + // what about pth=ptht-1 }; }; // @@ -2243,7 +2382,7 @@ cod = ehh->GetCounter(); // evbeft = cod->Get(pctp->Physics); tcod = (UInt_t)cod->Get(pctp->Physics); - if ( !tcod ) tcod = 1; + if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT evbeft = TMath::Min(upperentry,(tcod-1)); rh->GetEntry(pth); phh = ehh->GetPscuHeader(); @@ -2283,9 +2422,13 @@ // if ( ptt+1 == rtev){ if ( conptt+1 == nrtev ){ // if ( conptt+1 == (nrtev+nrtbef )){ + if ( IsDebug() ) printf(" >>>>>>>>>>> %i %u %i %u \n",ptht,rhev,nrtev,conptt); ptht++; - if ( ptht < rhev ){ + if ( ptht < rhev ){ rh->GetEntry(ptht); + //pth++; + //if ( pth < rhev ){ + //rh->GetEntry(pth); phh = ehh->GetPscuHeader(); pkth = phh->GetCounter(); obth = phh->GetOrbitalTime(); @@ -2309,10 +2452,12 @@ if ( (UInt_t)evbeft < upperentry-1 && upperentry>0 ){ if ( IsDebug() ) printf(" Piece of run at the end of the file with NO RUNHEADER! evbeft %u upperentry-1 %u \n",(UInt_t)evbeft,upperentry-1); // - if ( (ptt-1) < 0 ) throw -15; // should never arrive here! - rt->GetEntry(ptt-1); + // if ( (ptt-1) < 0 ) throw -15; // should never arrive here! + if ( (rtev-1) < 0 || ptt < 0 ) throw -15; // should never arrive here! + // rt->GetEntry(ptt-1); + rt->GetEntry(rtev-1); cod = eht->GetCounter(); - tcod = (UInt_t)cod->Get(pctp->Physics); + tcod = (UInt_t)cod->Get(pctp->Physics)+1; evbefh = TMath::Max(chminentry,tcod); // evbefh = cod->Get(pctp->Physics); rt->GetEntry(ptt); @@ -2543,10 +2688,14 @@ // // 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; + if ( firstev != (lastev+1) ){ // could be a problem sometimes (?) + if ( rhfirstev != firstev && !mishead ) mishead = true; + if ( rtlastev != lastev && !mistrail ) mistrail = true; + }; // + if ( IsDebug() ) printf(" bhere firstev is %i lastev is %i \n",firstev,lastev); this->FillClass(mishead,mistrail,firstev,lastev); + if ( IsDebug() ) printf(" chere firstev is %i lastev is %i \n",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()); @@ -2588,9 +2737,14 @@ 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 ( IsDebug() ) printf(" The run is already present in the fragment table, relaxed %u \n",RELAXED); + if ( !RELAXED ){ + if ( PEDANTIC ) throw -69; + return; + } else { + glrun->SetID((UInt_t)atoll(row->GetField(0))); + glrun->SetID_RUN_FRAG(0); + }; }; // if ( chewbacca && mishead && mistrail ) goto justcheck; @@ -2641,7 +2795,7 @@ // found = false; // default value // - if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n"); + if ( IsDebug() ) printf(" A 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: // @@ -2657,6 +2811,7 @@ if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt); // found = true; + if ( IsDebug() ) printf(" where firstev is %i lastev is %i \n",firstev,lastev); // } else { // @@ -2722,6 +2877,7 @@ oss << " ID="<GetField(0)<<";"; // glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos + if ( IsDebug() ) printf(" there firstev is %i lastev is %i \n",firstev,lastev); // // merge infos // @@ -2752,6 +2908,8 @@ glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT()); glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT()); // + if ( IsDebug() ) printf(" here firstev is %i lastev is %i \n",firstev,lastev); + // glrun->SetEV_FROM(firstev); glrun->SetNEVENTS(lastev-firstev+1); // @@ -2844,7 +3002,7 @@ // found = false; // default value // - if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n"); + if ( IsDebug() ) printf(" B 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: // @@ -2944,7 +3102,7 @@ apkt = PKT(ph->GetCounter()); aobt = OBT(ph->GetOrbitalTime()); lastev--; - if ( PEDANTIC ) throw -72; + if ( PEDANTIC && !RELAXED) throw -72; }; if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev); // @@ -3012,7 +3170,7 @@ // 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 " + oss << " SELECT ID,ID_ROOT_L0 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 (" @@ -3035,8 +3193,194 @@ row = result->Next(); // if ( row ){ - if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n"); - if ( PEDANTIC ) throw -70; + // + // we end up here if chewbacca and we have RH---| small gap |---RT, in this case the two pieces are attached and moved to GL_RUN. We are now processing the + // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number. + // + // 090112 [8RED (-70): RUN ALREADY INSERTED] + // + Bool_t OK = false; + UInt_t IDRL2A = 0; + UInt_t IDRL2B = 0; + if ( chewbacca ){ + if ( result->GetRowCount() == 2 ){ + IDRL2A = (UInt_t)atoll(row->GetField(0)); + UInt_t IDRL0A = (UInt_t)atoll(row->GetField(1)); + row = result->Next(); + IDRL2B = (UInt_t)atoll(row->GetField(0)); + UInt_t IDRL0B = (UInt_t)atoll(row->GetField(1)); + if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B); + if ( IDRL0A == IDRL0B ){ + TSQLResult *result2 = 0; + TSQLRow *row2 = 0; + oss.str(""); + oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;"; + if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: 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 ){ + UInt_t PKA = (UInt_t)atoll(row2->GetField(0)); + delete result2; + oss.str(""); + oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;"; + if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: 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 ){ + UInt_t PKB = (UInt_t)atoll(row2->GetField(0)); + // + if ( IsDebug() ) printf(" PKT(PKA) + 1 %llu == runheaderpkt %llu && PKB = runtrailer %llu + 1 %llu \n",PKT(PKA)+1LL, PKT(glrun->GetRUNHEADER_PKT()),PKT(PKB), PKT(glrun->GetRUNTRAILER_PKT())+1LL); + if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){ + if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n"); + OK = true; + }; + }; + }; + }; + }; + }; + if ( OK ){ + // + // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table + // + // we have to update with runheader/trailer infos our run and we have to change the ROOT_ID_FRAG of the header piece ( 1/2 2/1 must become 1/3 3/2 2/1 ) + // + GL_RUN *glA = new GL_RUN(); + glA->Query_GL_RUN(IDRL2A,conn); + // + if ( glA->GetACQ_BUILD_INFO() != 0 ){ + // + // the first piece contains a good runheader we can update all the other runs with correct infos! + // + oss.str(""); + oss << "UPDATE GL_RUN_FRAGMENTS SET " + << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , " + << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , " + << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , " + << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , " + << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , " + << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , " + << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , " + << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , " + << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , " + << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , " + << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , " + << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , " + << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , " + << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , " + << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , "; + // + if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ) + oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , "; + if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) + oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , "; + // + oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC(); + oss << " WHERE ID=" << glrun->GetID() << ";"; + if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str()); + conn->Query(oss.str().c_str()); + // + } else { + // + // sig no runheader, let set anyway what is possible... + // + oss.str(""); + oss << "UPDATE GL_RUN_FRAGMENTS SET " + << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "; + // + if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ) + oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; + if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) + oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; + // + oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , " + << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT(); + oss << " WHERE ID=" << glrun->GetID() << ";"; + if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str()); + conn->Query(oss.str().c_str()); + }; + // + // update runheader ROOT_ID_FRAG + // + oss.str(""); + oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";"; + if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str()); + conn->Query(oss.str().c_str()); + // + // now let's look for runtrailer if any in the last run + // + glA->Query_GL_RUN(IDRL2B,conn); + // + if ( glA->GetPKT_READY_COUNTER() != 0 ){ + // + // the first piece contains a good runtrailer we can update all the other runs with correct infos! + // + oss.str(""); + oss << "UPDATE GL_RUN_FRAGMENTS SET " + << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , " + << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , " + << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , " + << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , "; + // + if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){ + oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; }; + if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) { + oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; }; + // + oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER() + << " WHERE ID=" << glrun->GetID() << ";"; + if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str()); + conn->Query(oss.str().c_str()); + // + } else { + // + // sig no runtrailer, let set anyway what is possible... + // + oss.str(""); + oss << "UPDATE GL_RUN_FRAGMENTS SET " + << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , " + << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "; + // + if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){ + oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; }; + if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){ + oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; }; + // + oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT() + << " WHERE ID=" << glrun->GetID() << ";"; + if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str()); + conn->Query(oss.str().c_str()); + }; + // + UInt_t myi = glrun->GetID(); + oss.str(""); + oss << " ID= "<< myi; + // + glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); + // + // fill the new run in GL_RUN + // + glrun->SetID_RUN_FRAG(IDRL2B); + glrun->Fill_GL_RUN(conn); + glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); + delete glA; + // + } else { + // + // is just a repetition + // + 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()); @@ -3073,7 +3417,7 @@ glrun->SetID(this->AssignRunID()); glrun->SetID_RUN_FRAG(0); glrun->Fill_GL_RUN(conn); // it'ok we arrive here only inside a file hence in the middle of the runs... - }; + }; // }; // @@ -3117,6 +3461,7 @@ if ( firstev == lastev+1 || lastev == firstev ) { // no events inside the run! //if ( firstev <= lastev+1 ) { // no events inside the run! if ( IsDebug() ) printf(" Checking but no events in the run! \n"); + firstev = lastev+1; // this is necessary for files with no Physics entries, should have no influence on other files // return true is correct return(true); // @@ -3146,7 +3491,7 @@ // } else { // - if ( IsDebug() ) printf(" There are no-phyics packets inside the run!\n"); + if ( IsDebug() ) printf(" There are no-physics packets inside the run!\n"); // // HERE WE MUST HANDLE THAT RUNS AND GO BACK // @@ -3156,6 +3501,8 @@ UInt_t check = 0; UInt_t lastevtemp = lastev; UInt_t firstevno = firstev; + UInt_t rhchko=0; + UInt_t rhchk=0; // for (UInt_t i=firstev; i<=lastev; i++){ // @@ -3164,9 +3511,15 @@ // check = 0; // + + // if we have a runheader set lastev then exit + // for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){ if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter)); }; + // check here if we have a runheader + rhchko = rhchk; + rhchk = code->Get(GetPacketType("RunHeader")); // if ( checkfirst < check || i == lastev ){ // @@ -3250,18 +3603,6 @@ // 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; @@ -3376,7 +3717,7 @@ // found = false; // default value // - if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n"); + if ( IsDebug() ) printf(" C 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: // @@ -3581,37 +3922,6 @@ }; // EEE - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - } else { if ( !IsRunAlreadyInserted() ){ glrun->SetID(this->AssignRunID()); @@ -3626,6 +3936,14 @@ // }; // + if ( i > firstev ){ + if ( rhchko != rhchk ){ + if ( IsDebug() ) printf("oh oh... we have a runheader! stop here and handle later the remaining piece\n"); + lastev = i; + return(false); + }; + }; + // if ( check == checklast && i != lastev ){ lastevtemp = i - 1; i = lastev - 1; @@ -4446,383 +4764,941 @@ * 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 ){ + + GL_TRK_CALIB *glcal = new GL_TRK_CALIB(); // - if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n"); - if ( PEDANTIC ) throw -80; - // - } 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(" 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 ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",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)); // - if ( !result ) throw -4; + delete result; // mmm... "Error in : result set closed" solved? 090112 [8RED error messages] // - }; - // }; /** * 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++){ // - 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 = "<: result set closed" solved? 090112 [8RED error messages] + // --------------------------------------------------------------------- + // step 2: insert a new root file in GL_ROOT + // --------------------------------------------------------------------- + // + // check if the root file already exist + // + UInt_t id_root = 0; + oss.str(""); + oss << "SELECT ID FROM GL_ROOT where NAME=\"" << gSystem->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 = "<: result set closed" solved? 090112 [8RED error messages] + // --------------------------------------------------------------------- + // step 3: modify time-tag of corrupted GL_TRK_CALIB entry + // --------------------------------------------------------------------- + if ( IsDebug() ) cout << " << Modify time-tag of calibration ID="<> "<Query(oss.str().c_str()); + if ( !result ) throw -4;; + + delete result; // mmm... "Error in : result set closed" solved? 090112 [8RED error messages] + + // --------------------------------------------------------------------- + // 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); }; @@ -5152,7 +6028,8 @@ oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; // oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , " - << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< ";"; + << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT(); + oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112 conn->Query(oss.str().c_str()); }; }; @@ -5201,7 +6078,8 @@ if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){ oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; }; // - oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< ";"; + oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT(); + oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112 conn->Query(oss.str().c_str()); }; }; @@ -5252,7 +6130,7 @@ row2 = result2->Next(); // if ( row2 ){ - if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n"); + if ( IsDebug() ) printf(" The run already exists in the GL_RUN table! \n"); if ( PEDANTIC ) throw -83; row2->Close(); }; @@ -5329,7 +6207,7 @@ glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // oss.str(""); - oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE " + oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER,ID_ROOT_L0 FROM GL_RUN WHERE " << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND (" << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND " << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND (" @@ -5387,8 +6265,199 @@ moved++; // } else { - if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n"); - if ( PEDANTIC ) throw -83; + + // questa parte mah mah mah... da controllare + + // + // we end up here if chewbacca and we have RH---| small gap |---RT, in this case the two pieces are attached and moved to GL_RUN. We are now processing the + // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number. + // + // 090112 [8RED (-70): RUN ALREADY INSERTED] + // + Bool_t OK = false; + UInt_t IDRL2A = 0; + UInt_t IDRL2B = 0; + if ( result2->GetRowCount() == 2 ){ + IDRL2A = (UInt_t)atoll(row2->GetField(0)); + UInt_t IDRL0A = (UInt_t)atoll(row2->GetField(4)); + row2 = result2->Next(); + IDRL2B = (UInt_t)atoll(row2->GetField(0)); + UInt_t IDRL0B = (UInt_t)atoll(row2->GetField(4)); + if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B); + // if ( IDRL0A == IDRL0B ){ +// TSQLResult *result2a = 0; +// TSQLRow *row2a = 0; +// oss.str(""); +// oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;"; +// if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str()); +// result2a = conn->Query(oss.str().c_str()); +// // +// if ( !result2a ) throw -4; +// // +// row2a = result2a->Next(); +// // +// if ( row2a ){ +// UInt_t PKA = (UInt_t)atoll(row2a->GetField(0)); +// delete result2a; +// oss.str(""); +// oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;"; +// if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str()); +// result2a = conn->Query(oss.str().c_str()); +// // +// if ( !result2a ) throw -4; +// // +// row2a = result2a->Next(); +// // +// if ( row2a ){ +// UInt_t PKB = (UInt_t)atoll(row2a->GetField(0)); +// // +// if ( IsDebug() ) printf(" PKT(PKA) + 1 %llu == runheaderpkt %llu && PKB = runtrailer %llu + 1 %llu \n",PKT(PKA)+1LL, PKT(glrun->GetRUNHEADER_PKT()),PKT(PKB), PKT(glrun->GetRUNTRAILER_PKT())+1LL); +// if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){ +// if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n"); + OK = true; + // }; + // }; + // }; + // }; + }; + if ( OK ){ + // + // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table + // + // we have to update with runheader/trailer infos our run and we have to change the ROOT_ID_FRAG of the header piece ( 1/2 2/1 must become 1/3 3/2 2/1 ) + // + GL_RUN *glA = new GL_RUN(); + glA->Query_GL_RUN(IDRL2A,conn); + // + if ( glA->GetACQ_BUILD_INFO() != 0 ){ + // + // the first piece contains a good runheader we can update all the other runs with correct infos! + // + oss.str(""); + oss << "UPDATE GL_RUN_FRAGMENTS SET " + << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , " + << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , " + << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , " + << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , " + << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , " + << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , " + << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , " + << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , " + << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , " + << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , " + << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , " + << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , " + << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , " + << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , " + << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , "; + // + if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ) + oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , "; + if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) + oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , "; + // + oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC(); + oss << " WHERE ID=" << glrun->GetID() << ";"; + if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str()); + conn->Query(oss.str().c_str()); + // + } else { + // + // sig no runheader, let set anyway what is possible... + // + oss.str(""); + oss << "UPDATE GL_RUN_FRAGMENTS SET " + << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "; + // + if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ) + oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; + if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) + oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; + // + oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , " + << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT(); + oss << " WHERE ID=" << glrun->GetID() << ";"; + if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str()); + conn->Query(oss.str().c_str()); + }; + // + // update runheader ROOT_ID_FRAG + // + oss.str(""); + oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";"; + if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str()); + conn->Query(oss.str().c_str()); + // + // now let's look for runtrailer if any in the last run + // + glA->Query_GL_RUN(IDRL2B,conn); + // + if ( glA->GetPKT_READY_COUNTER() != 0 ){ + // + // the first piece contains a good runtrailer we can update all the other runs with correct infos! + // + oss.str(""); + oss << "UPDATE GL_RUN_FRAGMENTS SET " + << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , " + << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , " + << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , " + << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , "; + // + if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){ + oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; }; + if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) { + oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; }; + // + oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER() + << " WHERE ID=" << glrun->GetID() << ";"; + if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str()); + conn->Query(oss.str().c_str()); + // + } else { + // + // sig no runtrailer, let set anyway what is possible... + // + oss.str(""); + oss << "UPDATE GL_RUN_FRAGMENTS SET " + << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , " + << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "; + // + if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){ + oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; }; + if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){ + oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; }; + // + oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT() + << " WHERE ID=" << glrun->GetID() << ";"; + if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str()); + conn->Query(oss.str().c_str()); + }; + // + UInt_t myi = glrun->GetID(); + oss.str(""); + oss << " ID= "<< myi; + // + glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); + // + // fill the new run in GL_RUN + // + glrun->SetID_RUN_FRAG(IDRL2B); + glrun->Fill_GL_RUN(conn); + glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); + delete glA; + moved++; + // + } else { + // + // is just a repetition + // + if ( IsDebug() ) printf(" The run %u is already present in the GL_RUN table...\n",glrun->GetID()); + // printf(" CCCCCCCCICCCCCCCCCCCCIOOOOOOOOOO si muove Ciccio! %u \n",glrun->GetID()); + // if ( PEDANTIC && glrun->GetID() != 61563 ) throw -83; + if ( PEDANTIC ) throw -83; + }; + // if ( IsDebug() ) printf(" The run already exists in the GL_RUN table! ...\n"); + // if ( PEDANTIC ) throw -83; }; if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0)); // @@ -5454,7 +6523,8 @@ // 2) get the OBT of the last validated run // -------------------------------------------------------------- oss.str(""); - oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start + // oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start + oss << " SELECT * FROM GL_RUN WHERE VALIDATION>0 AND RUNHEADER_TIME<="<< t_start <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str()); result = conn->Query(oss.str().c_str()); @@ -5597,7 +6667,8 @@ 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); + // for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true); + for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],this_run->VALIDATION); nseq=0; } @@ -5618,8 +6689,13 @@ 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); + // Bool_t MISSING = MissingTRK_CALIB(t1,t2); + UInt_t MISSING = MissingTRK_CALIB(t1,t2); + UInt_t val = 0; + if ( MISSING == 1 ) val = 0; + if ( MISSING == 0 ) val = 1; + if ( MISSING == 2 ) val = 2; + for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],val); nseq=0; }; //-------------- @@ -5649,14 +6725,17 @@ * @param t2 To absolute time * @return true if there might be a missing calibration */ -Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){ +//Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){ +UInt_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){ 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->Query_GL_TRK_CALIB(t2, conn) )return(1); //>>> missing - if ( trkcalib->TO_TIME < t2 ) return(true); //>>> missing + // if ( trkcalib->TO_TIME < t2 ) return(true); //>>> missing + if ( trkcalib->TO_TIME < t2 ) return(1); //>>> missing //============================================================== // Check is done first on the basis of time between calibration, @@ -5673,9 +6752,11 @@ //============================================================== Bool_t DOWNLOAD = false; // check if the calib was skipped becouse of download .... DA FARE!! - if(DOWNLOAD)return(false); + // if(DOWNLOAD)return(false); + if(DOWNLOAD)return(0); - return(true); //>>> missing + // return(true); //>>> missing + return(1); //>>> missing }; @@ -5685,7 +6766,9 @@ //============================================================== // 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 ( trkcalib->VALIDATION ) return(0); + if ( IsDebug() )printf("Calibration is not validated... :-/ ==> OK but with VALIDATION=2! \n"); + return(2); }; /** @@ -5694,6 +6777,10 @@ * @param validation true/false */ Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){ + return(this->assignVALIDATION(idrun,(UInt_t)validation)); +} + +Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, UInt_t validation){ TSQLResult *result = 0; stringstream oss; oss.str(""); @@ -5890,7 +6977,7 @@ stringstream myquery; // myquery.str(""); - myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<Query(myquery.str().c_str()); // @@ -5906,6 +6993,7 @@ // this->SetID_ROOT((UInt_t)atoll(Row->GetField(0))); this->SetID_RAW((UInt_t)atoll(Row->GetField(1))); + UInt_t idtsy=(UInt_t)atoll(Row->GetField(2)); // this->ValidationOFF(); // @@ -5913,7 +7001,7 @@ // this->RemoveRUNS(); // - this->RemoveFILES(); + this->RemoveFILES(idtsy); // this->SetID_ROOT(0); this->SetID_RAW(0); @@ -6133,7 +7221,7 @@ * Rearrange calibration tables * **/ -void PamelaDBOperations::RemoveFILES(){ +void PamelaDBOperations::RemoveFILES(UInt_t idtsy){ stringstream myquery; // myquery.str(""); @@ -6143,6 +7231,22 @@ // conn->Query(myquery.str().c_str()); // + myquery.str(""); + myquery << " DELETE FROM GL_ROOT WHERE ID=" <GetID_ROOT() <<";"; + // + if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str()); + // + conn->Query(myquery.str().c_str()); // + // + if ( !chewbacca ){ + myquery.str(""); + myquery << " DELETE FROM GL_TIMESYNC WHERE ID=" << idtsy <<";"; + // + if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str()); + // + conn->Query(myquery.str().c_str()); + }; + // }; /** @@ -6347,116 +7451,149 @@ * Rearrange calibration tables * **/ -UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){ +UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh , TFile *file){ - Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0}; - UInt_t timeaftercalib=120000; //2000; - // ---------- - // Check CRCs - // ---------- - 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; // :-( - } - // ----------------------- - // Check missing packets: - // ----------------------- - // Readout order: - // ------------------ - // DSP packet board - // ------------------ - // 12 0 1 - // 10 1 1 - // 8 2 1 - // 4 3 1 - // 6 4 1 - // 2 5 1 - // ------------------ - // 11 0 2 - // 9 1 2 - // 7 2 2 - // 3 3 2 - // 5 4 2 - // 1 5 2 - // ------------------ - // ------------------------------------------------- - // Check if it is first or second calibration packet - // ------------------------------------------------- - UInt_t build=0; - TString classname = caltrk->GetName(); - UInt_t base=0; - UInt_t mask=0; - if(classname.Contains("CalibTrk1Event")){ - base=12; - mask=0x03F000; - } - if(classname.Contains("CalibTrk2Event")){ - base=18; - mask=0xFC0000; - } - // ------------------------------------------------- - // Count number of packets and set build variable - // ------------------------------------------------- - Int_t npkts=0; - for(Int_t ipkt=0; ipkt<6; ipkt++){ - if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){ - npkts++; - build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) ); - } - } - // if( npkts==6 )return 1; // :-) + if(!caltrk) return 0; - // cout << classname << " "<GetPscuHeader()->GetOrbitalTime()<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 :-( "<GetName(); + +// ---------------------------------- +// Check CRCs and failed calibrations +// ---------------------------------- + for(Int_t ipkt=0; ipkt<6; ipkt++){ + 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 "<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 :-( "<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(IsDebug())cout << " ==> "<< 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 << " *** 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 << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<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 <>> "<ACQ_BUILD_INFO&mask) << " != "<< build << dec<