--- chewbacca/YodaProfiler/src/PamelaDBOperations.cpp 2008/09/25 12:13:54 1.2 +++ chewbacca/YodaProfiler/src/PamelaDBOperations.cpp 2008/10/31 11:21:44 1.4 @@ -1273,7 +1273,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; @@ -1377,9 +1377,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 { @@ -1392,7 +1396,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 { @@ -1409,7 +1414,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; @@ -1428,7 +1433,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; @@ -1459,29 +1464,88 @@ // PUBLIC FUNCTIONS // +// /** +// * Insert a new row into GL_RAW table. +// */ +// Int_t PamelaDBOperations::insertPamelaRawFile(){ +// // +// 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); +// } /** * 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;//??? + + 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);//?? + // + 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); } @@ -1788,80 +1852,218 @@ return(signal); } +// /** +// * 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(){ +// 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; +// // +// row = result->Next(); +// // +// 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 "; +// } 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)); +// 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)); +// }; +// // +// 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() << "')"; +// }; +// // +// if ( debug ) printf(" query is %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() <<"';"; +// }; +// // +// if ( debug ) printf(" query is %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()); +// // +// delete result; +// // +// 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(){ + + stringstream oss; + TSQLResult *result = 0; + TSQLRow *row = 0; + // + // ---------------------- + // determine the timesync + // ---------------------- + 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; + // + row = result->Next(); + // + 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 "; + } 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)); + }; + + delete row; + delete result; + + // ---------------------- + // insert root file + // ---------------------- + + GL_ROOT glroot = GL_ROOT(); + + glroot.ID_RAW = GetID_RAW(); + glroot.ID_TIMESYNC = idtimesync; + glroot.PATH = GetRootPath(); + 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; - 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; - // - row = result->Next(); - // - 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 "; - } 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)); - 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)); - }; - // 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() << "')"; + << glroot->ID_RAW << "', '" << glroot->ID_TIMESYNC << "', '" << glroot->PATH << "', '" << glroot->NAME << "')"; } else { oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('" - << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')"; + << glroot->ID_RAW << "', '" << glroot->ID_TIMESYNC << "', '$PAM_L0', '" << 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; @@ -1869,18 +2071,20 @@ 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() <<"';"; + oss << "SELECT ID FROM GL_ROOT WHERE PATH='" << glroot->PATH << "' and NAME='"<< glroot->NAME <<"';"; } else { - oss << "SELECT ID FROM GL_ROOT WHERE PATH='$PAM_L0' and NAME='"<< this->GetRootFile().Data() <<"';"; + oss << "SELECT ID FROM GL_ROOT WHERE PATH='$PAM_L0' 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; // @@ -4437,387 +4641,1070 @@ return(0); }; +// /** +// * 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; +// // +// } 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 << "','" +// << this->GetBOOTnumber() << "','" +// << 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; +// // +// }; +// // +// }; /** * 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; + 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; // - } else { + HandleTRK_CALIB(glcal); // - // 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 << ";"; + 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; // - }; - // -}; - -/** - * 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); - // - ph1 = eh1->GetPscuHeader(); - obt1 = ph1->GetOrbitalTime(); - pkt1 = ph1->GetCounter(); - fromtime = this->GetAbsTime(ph1->GetOrbitalTime()); - // - // valid = 1; - // // - // if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1 - // + row = result->Next(); // - 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 --> "<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 - // - } 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 ( row ){ // - }; - // - 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 + if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n"); + if ( PEDANTIC ) throw -80; // - pret2 = t2; - obt2 = 0; - pkt2 = 0; - }; - // - // The calibration is good - // - if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){ + } else { // - 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); + // we have to insert a new calibration, check where to place it // - UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 ); - if ( IsDebug() ) cout << " pkt2 validation --> "< "<< fromtime << ";"; // - // Handle good calib + 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()); // - this->HandleTRK_CALIB(true,true); + if ( !result ) throw -4; // - // Check for missing calibtrk1 + row = result->Next(); // - 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 ){ + 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 // - // handle missing calib1 + oss.str(""); + oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE " + << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;"; // - pret2++; + 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()); // - obt1 = 0; - pkt1 = 0; + if ( !result ) throw -4; // - tr2->GetEntry(pret2); - ph2 = eh2->GetPscuHeader(); - obt2 = ph2->GetOrbitalTime(); - pkt2 = ph2->GetCounter(); + row = result->Next(); + if ( !row ){ + totime = numeric_limits::max(); + } else { + totime = (UInt_t)atoll(row->GetField(0)); + }; // - fromtime = this->GetAbsTime(ph2->GetOrbitalTime()); + } else { // - valid = 0; - this->HandleTRK_CALIB(false,true); + // 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; // - }; - // }; // - } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){ + 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 for missing calibtrk2 + if ( !pk1 ){ + oss << "NULL,"; + } else { + oss << "'" + << t1 << "',"; + }; // - if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2); - t2 = pret2; + if ( !pk2 ){ + oss << "NULL,'"; + } else { + oss << "'" + << t2 << "','"; + }; // - // handle missing calib2 + oss << fromtime << "','" + << totime << "','" + << obt1 << "','" + << pkt1 << "','" + << obt2 << "','" + << pkt2 << "','" + << boot_number << "','" + << valid << "');"; // - obt2 = 0; - pkt2 = 0; - valid = 0; - this->HandleTRK_CALIB(true,false); + 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; // - }; - // - } 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 ){ + + 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)); + // - // handle missing calib1 +}; + +/** + * 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; // - if ( IsDebug() ) printf(" t2 is %u nevents2 is %u \n",t2,nevents2); obt1 = 0; pkt1 = 0; + obt2 = 0; + pkt2 = 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); - // - }; + 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; + // + bool MISSING_pkt1 = true; + bool MISSING_pkt2 = true; + int ncalib = 0; + bool try_to_recover = false; // - t2++; + for (t1=0; t1 < nevents1; t1++){//loop over packet1 + // + pret2 = t2; + tr1->GetEntry(t1); + // + ph1 = eh1->GetPscuHeader(); + obt1 = ph1->GetOrbitalTime(); + pkt1 = ph1->GetCounter(); + fromtime = GetAbsTime(ph1->GetOrbitalTime()); + // + // 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); + // + }; + // + } 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; + // + }; + // + + if( !(MISSING_pkt1&MISSING_pkt2) ){ + this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2); + ncalib++; + if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true; + } + + }; //end loop on pkt1 + + + // - }; + // 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); + // + MISSING_pkt1 = true; + MISSING_pkt2 = false; + this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2); + ncalib++; + if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true; + // + }; + // + t2++; + // + }; + +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// ----------------------------------------------------------------- +// in case of corruption, check if the calibration can be recovered +// from another chewbacca file +// ----------------------------------------------------------------- +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +// cout <<" TRY TO RECOVER ?? "<>>> TRY TO RECOVER TRACKER CALIBRATIONS <<<<"<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 << "- - - - - - - - - - -" <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 >> "<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); }; @@ -5648,7 +6535,7 @@ GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB(); - // get the closest VALIDATED calibration before the run start (t2) + // 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 @@ -6342,118 +7229,265 @@ * 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<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; // :-) + +// // 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 :-( "<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<