| 58 | * @param debug        debug flag. | * @param debug        debug flag. | 
| 59 | * @param tlefilename  ascii file with TLE 3 line elements. | * @param tlefilename  ascii file with TLE 3 line elements. | 
| 60 | */ | */ | 
| 61 | 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){ | 
| 62 | // | // | 
| 63 | chewbacca = false; | chewbacca = false; | 
| 64 | chminentry = 0; | chminentry = 0; | 
| 76 | // | // | 
| 77 | }; | }; | 
| 78 | // | // | 
| 79 |  | KEEPENV = false; | 
| 80 |  | if ( keepenv ) KEEPENV=true; | 
| 81 | STATIC=false; | STATIC=false; | 
| 82 | if ( staticp ) STATIC=true; | if ( staticp ) STATIC=true; | 
| 83 | // | // | 
| 140 | TString chfile; | TString chfile; | 
| 141 | UInt_t ridn = 0; | UInt_t ridn = 0; | 
| 142 | qu.str(""); | qu.str(""); | 
| 143 | qu << "SELECT ROOT_ID_N,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL from ROOT_TABLE_MERGING where ID_N=" << chID << ";"; | qu << "SELECT ROOT_ID_N,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,INSERTED_BY from ROOT_TABLE_MERGING where ID_N=" << chID << ";"; | 
| 144 | if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str()); | if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str()); | 
| 145 | result = conn->Query(qu.str().c_str()); | result = conn->Query(qu.str().c_str()); | 
| 146 | if ( result ){ | if ( result ){ | 
| 151 | chpktmax = (UInt_t)atoll(row->GetField(2)); | chpktmax = (UInt_t)atoll(row->GetField(2)); | 
| 152 | chobtmin = (UInt_t)atoll(row->GetField(3)); | chobtmin = (UInt_t)atoll(row->GetField(3)); | 
| 153 | chobtmax = (UInt_t)atoll(row->GetField(4)); | chobtmax = (UInt_t)atoll(row->GetField(4)); | 
| 154 |  | chiby = (TString)(row->GetField(5)); | 
| 155 | } else { | } else { | 
| 156 | throw -84; | throw -84; | 
| 157 | }; | }; | 
| 166 | if ( result2 ){ | if ( result2 ){ | 
| 167 | row2 = result2->Next(); | row2 = result2->Next(); | 
| 168 | if ( row2 ){ | if ( row2 ){ | 
| 169 | chpath = (TString)gSystem->ExpandPathName(row2->GetField(0))+'/'; | if ( KEEPENV ){ | 
| 170 |  | chpath = (TString)(row2->GetField(0))+'/'; | 
| 171 |  | } else { | 
| 172 |  | chpath = (TString)gSystem->ExpandPathName(row2->GetField(0))+'/'; | 
| 173 |  | }; | 
| 174 | chfile = (TString)(row2->GetField(1)); | chfile = (TString)(row2->GetField(1)); | 
| 175 | chobtts = (UInt_t)atoll(row2->GetField(2)); | chobtts = (UInt_t)atoll(row2->GetField(2)); | 
| 176 | chlastts = (UInt_t)atoll(row2->GetField(3)); | chlastts = (UInt_t)atoll(row2->GetField(3)); | 
| 442 |  |  | 
| 443 | TString PamelaDBOperations::GetRootPath(){ | TString PamelaDBOperations::GetRootPath(){ | 
| 444 | if ( STATIC ){ | if ( STATIC ){ | 
| 445 | return((TString)gSystem->DirName(filerootname.Data())+'/'); | return((TString)gSystem->DirName(this->GetRootName().Data())+'/'); | 
| 446 | } else { | } else { | 
| 447 | return((TString)gSystem->ExpandPathName("$PAM_L0")+'/'); | if ( KEEPENV ){ | 
| 448 |  | return((TString)gSystem->ExpandPathName(gSystem->DirName(filerootname.Data()))+'/'); | 
| 449 |  | } else { | 
| 450 |  | return((TString)gSystem->ExpandPathName("$PAM_L0")+'/'); | 
| 451 |  | }; | 
| 452 | }; | }; | 
| 453 | }; | }; | 
| 454 |  |  | 
| 660 | UInt_t tjump = 50000; | UInt_t tjump = 50000; | 
| 661 | //UInt_t tjump = 100; | //UInt_t tjump = 100; | 
| 662 | while ( tjump > 0 ){ | while ( tjump > 0 ){ | 
| 663 | pktlast = numeric_limits<UInt_t>::max(); | //      pktlast = numeric_limits<UInt_t>::max(); | 
| 664 | while ( pktlast > chpktmax && (Int_t)(nevent-1-it) >= 0 ){ | pktlast = chpktmax + 1; | 
| 665 |  | while ( PKT(pktlast) > PKT(chpktmax) && (Int_t)(nevent-1-it) >= 0 ){ | 
| 666 | if ( (Int_t)(nevent-1-it) >= 0 ){ | if ( (Int_t)(nevent-1-it) >= 0 ){ | 
| 667 | T->GetEntry(nevent-1-it); | T->GetEntry(nevent-1-it); | 
| 668 | ph = eh->GetPscuHeader(); | ph = eh->GetPscuHeader(); | 
| 670 | } else { | } else { | 
| 671 | pktlast = chpktmax + 1; | pktlast = chpktmax + 1; | 
| 672 | }; | }; | 
| 673 | 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)); | 
| 674 | it += tjump; | it += tjump; | 
| 675 | }; | }; | 
| 676 | if ( tjump > 1 ) it -= 2*tjump; | if ( tjump > 1 ) it -= 2*tjump; | 
| 694 | tjump = 50000; | tjump = 50000; | 
| 695 | //tjump = 100; | //tjump = 100; | 
| 696 | while ( tjump > 0 ){ | while ( tjump > 0 ){ | 
| 697 | pktlast = 0; | //      pktlast = 0; | 
| 698 | while ( pktlast < chpktmin && it < (Int_t)nevent ){ | pktlast = chpktmin - 1; | 
| 699 |  | if ( debug ) printf("LLlook for down %i %i %llu %llu \n",it,pktlast,PKT(pktlast),PKT(chpktmin)); | 
| 700 |  | while ( PKT(pktlast) < PKT(chpktmin) && it < (Int_t)nevent ){ | 
| 701 | if ( it < (Int_t)nevent ){ | if ( it < (Int_t)nevent ){ | 
| 702 | T->GetEntry(it); | T->GetEntry(it); | 
| 703 | ph = eh->GetPscuHeader(); | ph = eh->GetPscuHeader(); | 
| 1139 | stringstream   oss; | stringstream   oss; | 
| 1140 | // | // | 
| 1141 | oss.str(""); | oss.str(""); | 
| 1142 | 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. | 
| 1143 |  | if ( !strcmp(tag.Data(),"NONE") ){ | 
| 1144 |  | oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< chiby.Data() <<"');"; | 
| 1145 |  | } else { | 
| 1146 |  | oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');"; | 
| 1147 |  | }; | 
| 1148 |  | } else { | 
| 1149 |  | oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');"; | 
| 1150 |  | }; | 
| 1151 | result = conn->Query(oss.str().c_str()); | result = conn->Query(oss.str().c_str()); | 
| 1152 | if ( !result ) throw -10; | if ( !result ) throw -10; | 
| 1153 | oss.str(""); | oss.str(""); | 
| 1295 | stringstream oss; | stringstream oss; | 
| 1296 | // | // | 
| 1297 | oss.str(""); | oss.str(""); | 
| 1298 | 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;"; | 
| 1299 | TSQLResult *result = 0; | TSQLResult *result = 0; | 
| 1300 | result = conn->Query(oss.str().c_str()); | result = conn->Query(oss.str().c_str()); | 
| 1301 | if ( !result ) throw -10; | if ( !result ) throw -10; | 
| 1352 | // | // | 
| 1353 | //  if ( IsDebug() ) printf(" obt conversion: obt is %u obtfirst is %u  (numeric_limits<UInt_t>::max()/2) is %u  \n",obt,pobtfirst,(UInt_t)(numeric_limits<UInt_t>::max()/2)); | //  if ( IsDebug() ) printf(" obt conversion: obt is %u obtfirst is %u  (numeric_limits<UInt_t>::max()/2) is %u  \n",obt,pobtfirst,(UInt_t)(numeric_limits<UInt_t>::max()/2)); | 
| 1354 | // | // | 
| 1355 | if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){ | if ( obt < ((Long64_t)pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){ | 
| 1356 | //    if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max()); | //    if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max()); | 
| 1357 | return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max()); | return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max()); | 
| 1358 | }; | }; | 
| 1399 | UInt_t lastPkt = 0; | UInt_t lastPkt = 0; | 
| 1400 | UInt_t rhtime = 0; | UInt_t rhtime = 0; | 
| 1401 | UInt_t rttime = 0; | UInt_t rttime = 0; | 
| 1402 |  | // | 
| 1403 |  | if ( IsDebug() ) printf(" A firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1); | 
| 1404 |  | // | 
| 1405 | if ( !mishead ){ | if ( !mishead ){ | 
| 1406 | codh = ehh->GetCounter(); | codh = ehh->GetCounter(); | 
| 1407 | if ( lastev+1 == firstev || (lastev == firstev && lastev != -1) ){ | if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){ | 
| 1408 |  | if ( IsDebug() ) printf(" B firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1); | 
| 1409 | firstev = 1; | firstev = 1; | 
| 1410 | lastev = 0; | lastev = 0; | 
| 1411 | } else { | } else { | 
| 1418 | }; | }; | 
| 1419 | if ( !mistrail ){ | if ( !mistrail ){ | 
| 1420 | codt = eht->GetCounter(); | codt = eht->GetCounter(); | 
| 1421 | if ( lastev+1 == firstev || (lastev == firstev && lastev != -1)){ | if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1)){ | 
| 1422 |  | if ( IsDebug() ) printf(" C firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1); | 
| 1423 | lastev = 0; | lastev = 0; | 
| 1424 | firstev = lastev+1; | firstev = lastev+1; | 
| 1425 | } else { | } else { | 
| 1436 | if ( mishead ) { | if ( mishead ) { | 
| 1437 | glrun->Set_GL_RUNH0(); | glrun->Set_GL_RUNH0(); | 
| 1438 | // | // | 
| 1439 | if ( lastev+1 == firstev || (lastev == firstev && lastev != -1) ){ | if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){ | 
| 1440 | firstObt = lastObt; | firstObt = lastObt; | 
| 1441 | firstPkt = lastPkt; | firstPkt = lastPkt; | 
| 1442 | rhtime = rttime; | rhtime = rttime; | 
| 1455 | if ( mistrail ){ | if ( mistrail ){ | 
| 1456 | glrun->Set_GL_RUNT0(); | glrun->Set_GL_RUNT0(); | 
| 1457 | // | // | 
| 1458 | if ( lastev+1 == firstev || (lastev == firstev && lastev != -1) ){ | if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){ | 
| 1459 | lastObt = firstObt; | lastObt = firstObt; | 
| 1460 | lastPkt = firstPkt; | lastPkt = firstPkt; | 
| 1461 | rttime = rhtime; | rttime = rhtime; | 
| 1490 | * Insert a new row into GL_RAW table. | * Insert a new row into GL_RAW table. | 
| 1491 | */ | */ | 
| 1492 | Int_t PamelaDBOperations::insertPamelaRawFile(){ | Int_t PamelaDBOperations::insertPamelaRawFile(){ | 
| 1493 |  |  | 
| 1494 |  | // | 
| 1495 |  | Bool_t idr =  this->SetID_RAW(); | 
| 1496 |  | if ( idr ) return(1); | 
| 1497 |  |  | 
| 1498 |  | GL_RAW glraw = GL_RAW(); | 
| 1499 |  |  | 
| 1500 |  | glraw.PATH = GetRawPath(); | 
| 1501 |  | glraw.NAME = GetRawFile(); | 
| 1502 |  | //    glraw.BOOT_NUMBER = 0;//??? | 
| 1503 |  | glraw.BOOT_NUMBER = this->GetBOOTnumber(); | 
| 1504 |  |  | 
| 1505 |  | if( insertPamelaRawFile(&glraw) )return(1); | 
| 1506 |  | // | 
| 1507 |  | idr =  this->SetID_RAW(); | 
| 1508 |  | if ( !idr ) throw -11; | 
| 1509 |  |  | 
| 1510 |  | return(0); | 
| 1511 |  | } | 
| 1512 |  | /** | 
| 1513 |  | * Insert a new row into GL_RAW table. | 
| 1514 |  | */ | 
| 1515 |  | Int_t PamelaDBOperations::insertPamelaRawFile(GL_RAW *glraw){ | 
| 1516 | // | // | 
| 1517 | stringstream oss; | if(!glraw)return(1);//?? ok I think | 
| 1518 | // | // | 
| 1519 | Bool_t idr =  this->SetID_RAW(); | stringstream oss; | 
| 1520 | if ( idr ) return(1); | // | 
| 1521 | // | oss.str(""); | 
| 1522 | oss.str(""); | if ( STATIC ){ | 
| 1523 | if ( STATIC ){ | oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('" | 
| 1524 | oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('" | << glraw->PATH << "', '" << glraw->NAME << "')"; | 
| 1525 | << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')"; | } else { | 
| 1526 | } else { | oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << glraw->NAME << "')"; | 
| 1527 | oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')"; | }; | 
| 1528 | }; | if ( debug ) cout <<oss.str().c_str() <<endl; | 
| 1529 | if ( conn->Query(oss.str().c_str()) == 0 ) throw -4; | if ( conn->Query(oss.str().c_str()) == 0 ) throw -4; | 
| 1530 | // | // | 
| 1531 | idr =  this->SetID_RAW(); | oss.str(""); | 
| 1532 | if ( !idr ) throw -11; | oss << "SELECT ID FROM GL_RAW WHERE NAME=\""<<glraw->NAME<<"\";"; | 
| 1533 | // | if ( debug ) cout << oss.str().c_str()<<endl; | 
| 1534 | return(0); | if ( conn->Query(oss.str().c_str()) == 0 ) throw -4; | 
| 1535 |  | // | 
| 1536 |  | TSQLResult *result = 0; | 
| 1537 |  | TSQLRow    *row    = 0; | 
| 1538 |  | result = conn->Query(oss.str().c_str()); | 
| 1539 |  | if ( result == NULL ) throw -4; | 
| 1540 |  | row = result->Next(); | 
| 1541 |  | if ( !row ) return(1); | 
| 1542 |  | glraw->ID = (UInt_t)atoll(row->GetField(0)); | 
| 1543 |  | if ( debug ) printf(" The ID of the RAW file is %u \n",glraw->ID); | 
| 1544 |  | delete result; | 
| 1545 |  | delete row; | 
| 1546 |  | // | 
| 1547 |  | return(0); | 
| 1548 | } | } | 
| 1549 |  |  | 
| 1550 |  |  | 
| 1856 | * The raw file indicates in the parameters should be already been stored in the database. | * The raw file indicates in the parameters should be already been stored in the database. | 
| 1857 | */ | */ | 
| 1858 | Int_t PamelaDBOperations::insertPamelaRootFile(){ | Int_t PamelaDBOperations::insertPamelaRootFile(){ | 
| 1859 | stringstream oss; |  | 
| 1860 | TSQLResult *result = 0; | stringstream oss; | 
| 1861 | TSQLRow    *row    = 0; | TSQLResult *result = 0; | 
| 1862 | UInt_t idtimesync = 0; | TSQLRow    *row    = 0; | 
|  | // |  | 
|  | // |  | 
|  | if ( chewbacca ){ |  | 
|  | oss.str(""); |  | 
|  | oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;"; |  | 
|  | if ( debug ) printf(" query is %s \n",oss.str().c_str()); |  | 
|  | result = conn->Query(oss.str().c_str()); |  | 
|  | // |  | 
|  | if ( !result ) throw -3; |  | 
| 1863 | // | // | 
| 1864 | row = result->Next(); | // ---------------------- | 
| 1865 |  | // determine the timesync | 
| 1866 |  | // ---------------------- | 
| 1867 |  | UInt_t idtimesync = 0; | 
| 1868 | // | // | 
| 1869 | if ( !row ) throw -3; | if ( chewbacca ){ | 
| 1870 | idtimesync = (UInt_t)atoll(row->GetField(0)); | oss.str(""); | 
| 1871 | } else { | oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;"; | 
| 1872 | oss.str(""); | if ( debug ) printf(" %s \n",oss.str().c_str()); | 
| 1873 | if ( STATIC ){ | result = conn->Query(oss.str().c_str()); | 
| 1874 | oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW " | // | 
| 1875 | << " LEFT JOIN GL_ROOT " | if ( !result ) throw -3; | 
| 1876 | << " ON GL_RAW.ID = GL_ROOT.ID_RAW " | // | 
| 1877 | << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND " | row = result->Next(); | 
| 1878 | << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID "; | // | 
| 1879 |  | if ( !row ) throw -3; | 
| 1880 |  | idtimesync = (UInt_t)atoll(row->GetField(0)); | 
| 1881 | } else { | } else { | 
| 1882 | oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW " | oss.str(""); | 
| 1883 | << " LEFT JOIN GL_ROOT " | if ( STATIC ){ | 
| 1884 | << " ON GL_RAW.ID = GL_ROOT.ID_RAW " | oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW " | 
| 1885 | << " WHERE GL_RAW.PATH = '$PAM_RAW' AND " | << " LEFT JOIN GL_ROOT " | 
| 1886 | << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID "; | << " ON GL_RAW.ID = GL_ROOT.ID_RAW " | 
| 1887 |  | << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND " | 
| 1888 |  | << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID "; | 
| 1889 |  | } else { | 
| 1890 |  | oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW " | 
| 1891 |  | << " LEFT JOIN GL_ROOT " | 
| 1892 |  | << " ON GL_RAW.ID = GL_ROOT.ID_RAW " | 
| 1893 |  | << " WHERE GL_RAW.PATH = '$PAM_RAW' AND " | 
| 1894 |  | << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID "; | 
| 1895 |  | }; | 
| 1896 |  | result = conn->Query(oss.str().c_str()); | 
| 1897 |  | // | 
| 1898 |  | if ( !result ) throw -12; | 
| 1899 |  | // | 
| 1900 |  | row = result->Next(); | 
| 1901 |  | // | 
| 1902 |  | if ( !row ) throw -10; | 
| 1903 |  | if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){ | 
| 1904 |  | idroot = (UInt_t)atoll(row->GetField(2)); | 
| 1905 |  | delete row; | 
| 1906 |  | delete result; | 
| 1907 |  | return(1); | 
| 1908 |  | }; | 
| 1909 |  | // | 
| 1910 |  | // determine which timesync has to be used | 
| 1911 |  | // | 
| 1912 |  | oss.str(""); | 
| 1913 |  | 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;"; | 
| 1914 |  | result = conn->Query(oss.str().c_str()); | 
| 1915 |  | // | 
| 1916 |  | if ( !result ) throw -3; | 
| 1917 |  | // | 
| 1918 |  | row = result->Next(); | 
| 1919 |  | // | 
| 1920 |  | if ( !row ) throw -3; | 
| 1921 |  | idtimesync = (UInt_t)atoll(row->GetField(0)); | 
| 1922 | }; | }; | 
| 1923 | result = conn->Query(oss.str().c_str()); |  | 
| 1924 | // | delete row; | 
| 1925 | if ( !result ) throw -12; | delete result; | 
| 1926 | // |  | 
| 1927 | row = result->Next(); | // ---------------------- | 
| 1928 | // | // insert root file | 
| 1929 | if ( !row ) throw -10; | // ---------------------- | 
| 1930 | if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){ |  | 
| 1931 | idroot = (UInt_t)atoll(row->GetField(2)); | GL_ROOT glroot = GL_ROOT(); | 
| 1932 | return(1); |  | 
| 1933 |  | glroot.ID_RAW = GetID_RAW(); | 
| 1934 |  | glroot.ID_TIMESYNC = idtimesync; | 
| 1935 |  | if ( STATIC ){ | 
| 1936 |  | glroot.PATH = GetRootPath(); | 
| 1937 |  | } else { | 
| 1938 |  | if ( KEEPENV ){ | 
| 1939 |  | glroot.PATH = gSystem->DirName(filerootname.Data()); | 
| 1940 |  | } else { | 
| 1941 |  | glroot.PATH = "$PAM_L0"; | 
| 1942 |  | }; | 
| 1943 | }; | }; | 
| 1944 | // | glroot.NAME = GetRootFile(); | 
| 1945 | // determine which timesync has to be used |  | 
| 1946 | // | if ( insertPamelaRootFile(&glroot) )return 1; | 
| 1947 | oss.str(""); |  | 
| 1948 | 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;"; | SetID_ROOT(glroot.ID); | 
| 1949 | result = conn->Query(oss.str().c_str()); |  | 
| 1950 | // |  | 
| 1951 | if ( !result ) throw -3; | return (0); | 
| 1952 | // | } | 
| 1953 | row = result->Next(); | /** | 
| 1954 | // | * Insert all the new rows into GL_ROOT. | 
| 1955 | if ( !row ) throw -3; | * The raw file indicates in the parameters should be already been stored in the database. | 
| 1956 | idtimesync = (UInt_t)atoll(row->GetField(0)); | */ | 
| 1957 | }; | Int_t PamelaDBOperations::insertPamelaRootFile(GL_ROOT *glroot){ | 
| 1958 |  | stringstream oss; | 
| 1959 |  | TSQLResult *result = 0; | 
| 1960 |  | TSQLRow    *row    = 0; | 
| 1961 |  | // | 
| 1962 | // | // | 
| 1963 | oss.str(""); | oss.str(""); | 
| 1964 | if ( STATIC ){ | oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('" | 
| 1965 | oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('" | << glroot->ID_RAW << "', '" << glroot->ID_TIMESYNC << "', '" << glroot->PATH << "', '" << glroot->NAME << "')"; | 
|  | << 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() << "')"; |  | 
|  | }; |  | 
| 1966 | // | // | 
| 1967 | if ( debug ) printf(" query is %s \n",oss.str().c_str()); | if ( debug ) printf("%s \n",oss.str().c_str()); | 
| 1968 | if (conn->Query(oss.str().c_str()) == 0) throw -4; | if (conn->Query(oss.str().c_str()) == 0) throw -4; | 
| 1969 | // | // | 
| 1970 | delete result; | delete result; | 
| 1971 | // | // | 
| 1972 | oss.str(""); | oss.str(""); | 
| 1973 | //  oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";"; | //  oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";"; | 
| 1974 | if ( STATIC ){ | oss << "SELECT ID FROM GL_ROOT WHERE PATH='" << glroot->PATH << "' and NAME='"<< glroot->NAME <<"';"; | 
|  | 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() <<"';"; |  | 
|  | }; |  | 
| 1975 | // | // | 
| 1976 | if ( debug ) printf(" query is %s \n",oss.str().c_str()); | if ( debug ) printf("%s \n",oss.str().c_str()); | 
| 1977 | result = conn->Query(oss.str().c_str()); | result = conn->Query(oss.str().c_str()); | 
| 1978 | if ( !result ) throw -12; | if ( !result ) throw -12; | 
| 1979 | row = result->Next(); | row = result->Next(); | 
| 1980 | if ( !row ) throw -3; | if ( !row ) throw -3; | 
| 1981 | this->SetID_ROOT((UInt_t)atoll(row->GetField(0))); |  | 
| 1982 | if ( debug ) printf(" The ID of the ROOT file is %u \n",this->GetID_ROOT()); | glroot->ID = (UInt_t)atoll(row->GetField(0)); | 
| 1983 |  |  | 
| 1984 |  | if ( debug ) printf(" The ID of the ROOT file is %u \n",glroot->ID); | 
| 1985 | // | // | 
| 1986 | delete result; | delete result; | 
| 1987 | // | // | 
| 2317 | // | // | 
| 2318 | this->HandleRunFragments(true,true,chminentry,(evbefh)); | this->HandleRunFragments(true,true,chminentry,(evbefh)); | 
| 2319 | // | // | 
| 2320 |  | // what about pth=ptht-1 | 
| 2321 | }; | }; | 
| 2322 | }; | }; | 
| 2323 | // | // | 
| 2380 | //      if ( ptt+1 == rtev){ | //      if ( ptt+1 == rtev){ | 
| 2381 | if ( conptt+1 == nrtev ){ | if ( conptt+1 == nrtev ){ | 
| 2382 | //    if ( conptt+1 == (nrtev+nrtbef )){ | //    if ( conptt+1 == (nrtev+nrtbef )){ | 
| 2383 |  | if ( IsDebug() ) printf(" >>>>>>>>>>> %i %u %i %u \n",ptht,rhev,nrtev,conptt); | 
| 2384 | ptht++; | ptht++; | 
| 2385 | if ( ptht < rhev ){ | if ( ptht < rhev ){ | 
| 2386 | rh->GetEntry(ptht); | rh->GetEntry(ptht); | 
| 2387 |  | //pth++; | 
| 2388 |  | //if ( pth < rhev ){ | 
| 2389 |  | //rh->GetEntry(pth); | 
| 2390 | phh = ehh->GetPscuHeader(); | phh = ehh->GetPscuHeader(); | 
| 2391 | pkth = phh->GetCounter(); | pkth = phh->GetCounter(); | 
| 2392 | obth = phh->GetOrbitalTime(); | obth = phh->GetOrbitalTime(); | 
| 2411 | 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 ( 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); | 
| 2412 | // | // | 
| 2413 | if ( (ptt-1) < 0 ) throw -15; // should never arrive here! | if ( (ptt-1) < 0 ) throw -15; // should never arrive here! | 
| 2414 | rt->GetEntry(ptt-1); | //              rt->GetEntry(ptt-1); | 
| 2415 |  | rt->GetEntry(rtev-1); | 
| 2416 | cod = eht->GetCounter(); | cod = eht->GetCounter(); | 
| 2417 | tcod = (UInt_t)cod->Get(pctp->Physics); | tcod = (UInt_t)cod->Get(pctp->Physics)+1; | 
| 2418 | evbefh = TMath::Max(chminentry,tcod); | evbefh = TMath::Max(chminentry,tcod); | 
| 2419 | //            evbefh = cod->Get(pctp->Physics); | //            evbefh = cod->Get(pctp->Physics); | 
| 2420 | rt->GetEntry(ptt); | rt->GetEntry(ptt); | 
| 3248 | // | // | 
| 3249 | } else { | } else { | 
| 3250 | // | // | 
| 3251 | if ( IsDebug() ) printf(" There are no-phyics packets inside the run!\n"); | if ( IsDebug() ) printf(" There are no-physics packets inside the run!\n"); | 
| 3252 | // | // | 
| 3253 | // HERE WE MUST HANDLE THAT RUNS AND GO BACK | // HERE WE MUST HANDLE THAT RUNS AND GO BACK | 
| 3254 | // | // | 
| 3258 | UInt_t check = 0; | UInt_t check = 0; | 
| 3259 | UInt_t lastevtemp = lastev; | UInt_t lastevtemp = lastev; | 
| 3260 | UInt_t firstevno = firstev; | UInt_t firstevno = firstev; | 
| 3261 |  | UInt_t rhchko=0; | 
| 3262 |  | UInt_t rhchk=0; | 
| 3263 | // | // | 
| 3264 | for (UInt_t i=firstev; i<=lastev; i++){ | for (UInt_t i=firstev; i<=lastev; i++){ | 
| 3265 | // | // | 
| 3268 | // | // | 
| 3269 | check = 0; | check = 0; | 
| 3270 | // | // | 
| 3271 |  |  | 
| 3272 |  | // if we have a runheader set lastev then exit | 
| 3273 |  | // | 
| 3274 | for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){ | for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){ | 
| 3275 | if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter)); | if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter)); | 
| 3276 | }; | }; | 
| 3277 |  | // check here if we have a runheader | 
| 3278 |  | rhchko = rhchk; | 
| 3279 |  | rhchk = code->Get(GetPacketType("RunHeader")); | 
| 3280 | // | // | 
| 3281 | if ( checkfirst < check || i == lastev ){ | if ( checkfirst < check || i == lastev ){ | 
| 3282 | // | // | 
| 3360 | //      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 | //      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 | 
| 3361 | // | // | 
| 3362 |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
| 3363 | mishead = true; | mishead = true; | 
| 3364 |  |  | 
| 3365 |  |  | 
| 3679 | }; // EEE | }; // EEE | 
| 3680 |  |  | 
| 3681 |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
|  |  |  | 
| 3682 | } else { | } else { | 
| 3683 | if ( !IsRunAlreadyInserted() ){ | if ( !IsRunAlreadyInserted() ){ | 
| 3684 | glrun->SetID(this->AssignRunID()); | glrun->SetID(this->AssignRunID()); | 
| 3693 | // | // | 
| 3694 | }; | }; | 
| 3695 | // | // | 
| 3696 |  | if ( i > firstev ){ | 
| 3697 |  | if ( rhchko != rhchk ){ | 
| 3698 |  | if ( IsDebug() ) printf("oh oh... we have a runheader! stop here and handle later the remaining piece\n"); | 
| 3699 |  | lastev = i; | 
| 3700 |  | return(false); | 
| 3701 |  | }; | 
| 3702 |  | }; | 
| 3703 |  | // | 
| 3704 | if ( check == checklast && i != lastev ){ | if ( check == checklast && i != lastev ){ | 
| 3705 | lastevtemp = i - 1; | lastevtemp = i - 1; | 
| 3706 | i = lastev - 1; | i = lastev - 1; | 
| 4521 | * Fill the GL_TRK_CALIB table | * Fill the GL_TRK_CALIB table | 
| 4522 | */ | */ | 
| 4523 | void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){ | void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){ | 
| 4524 | // |  | 
| 4525 | TSQLResult *result = 0; | GL_TRK_CALIB *glcal = new GL_TRK_CALIB(); | 
|  | 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; |  | 
| 4526 | // | // | 
| 4527 | } else { | glcal->ID         = 0; | 
| 4528 |  | glcal->ID_ROOT_L0 = GetID_ROOT(); | 
| 4529 |  | glcal->EV_ROOT_CALIBTRK1 = t1; | 
| 4530 |  | glcal->EV_ROOT_CALIBTRK2 = t2; | 
| 4531 |  | glcal->FROM_TIME = fromtime; | 
| 4532 |  | glcal->TO_TIME   = 0; | 
| 4533 |  | glcal->OBT1      = obt1; | 
| 4534 |  | glcal->OBT2      = obt2; | 
| 4535 |  | glcal->PKT1      = pkt1; | 
| 4536 |  | glcal->PKT2      = pkt2; | 
| 4537 |  | glcal->BOOT_NUMBER = GetBOOTnumber(); | 
| 4538 |  | glcal->VALIDATION = valid; | 
| 4539 | // | // | 
| 4540 | // we have to insert a new calibration, check where to place it | HandleTRK_CALIB(glcal); | 
| 4541 | // | // | 
| 4542 | oss.str(""); | delete glcal; | 
| 4543 | oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE " | } | 
| 4544 | << " FROM_TIME < "<< fromtime << " AND " | /** | 
| 4545 | << " TO_TIME > "<< fromtime << ";"; | * Fill the GL_TRK_CALIB table | 
| 4546 |  | */ | 
| 4547 |  | void PamelaDBOperations::HandleTRK_CALIB(GL_TRK_CALIB *glcal){ | 
| 4548 |  |  | 
| 4549 |  | Bool_t pk1 = (glcal->OBT1>0&&glcal->PKT1>0); | 
| 4550 |  | Bool_t pk2 = (glcal->OBT2>0&&glcal->PKT2>0); | 
| 4551 |  | UInt_t boot_number = glcal->BOOT_NUMBER; | 
| 4552 |  | UInt_t obt1 = glcal->OBT1; | 
| 4553 |  | UInt_t obt2 = glcal->OBT2; | 
| 4554 |  | UInt_t pkt1 = glcal->PKT1; | 
| 4555 |  | UInt_t pkt2 = glcal->PKT2; | 
| 4556 |  | UInt_t fromtime = glcal->FROM_TIME; | 
| 4557 |  | UInt_t totime = 0; | 
| 4558 |  | UInt_t idroot = glcal->ID_ROOT_L0; | 
| 4559 |  | UInt_t t1 = glcal->EV_ROOT_CALIBTRK1; | 
| 4560 |  | UInt_t t2 = glcal->EV_ROOT_CALIBTRK2; | 
| 4561 |  | UInt_t valid = glcal->VALIDATION; | 
| 4562 | // | // | 
| 4563 | if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str()); | TSQLResult *result = 0; | 
| 4564 | result = conn->Query(oss.str().c_str()); | TSQLRow    *row    = 0; | 
| 4565 | // | // | 
| 4566 | if ( !result ) throw -4; | stringstream oss; | 
| 4567 |  | oss.str(""); | 
| 4568 | // | // | 
|  | row = result->Next(); |  | 
| 4569 | // | // | 
| 4570 | if ( !row ){ | if ( !pk1 && !pk2 ){ | 
| 4571 | // | if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n"); | 
| 4572 | // no calibrations in the db contain our calibration | return; | 
|  | // |  | 
|  | 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<UInt_t>::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; |  | 
|  | // |  | 
| 4573 | }; | }; | 
| 4574 | // | // | 
| 4575 | oss.str(""); | // check if the calibration has already been inserted | 
|  | 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 << "',"; |  | 
| 4576 | // | // | 
| 4577 | if ( !pk1 ){ | oss.str(""); | 
| 4578 | oss << "NULL,"; | oss << " SELECT ID FROM GL_TRK_CALIB WHERE " | 
| 4579 |  | << " BOOT_NUMBER = "<< boot_number; // | 
| 4580 |  | oss << " AND FROM_TIME="<<fromtime; /// NEWNEWNEW -- VA BENE ?!?!?!?! | 
| 4581 |  | oss << " AND ( ( "; | 
| 4582 |  | if ( pk1 ){ | 
| 4583 |  | oss << " OBT1 = "<< obt1 << " AND " | 
| 4584 |  | << " PKT1 = "<< pkt1 | 
| 4585 |  | << " ) OR ( "; | 
| 4586 | } else { | } else { | 
| 4587 | oss << "'" | oss << " PKT1 = "<< pkt2-1 | 
| 4588 | << t1 << "',"; | << " ) OR ( "; | 
| 4589 | }; | }; | 
| 4590 | // | if ( pk2 ){ | 
| 4591 | if ( !pk2 ){ | oss << " OBT2 = "<< obt2 << " AND " | 
| 4592 | oss << "NULL,'"; | << " PKT2 = "<< pkt2; | 
| 4593 | } else { | } else { | 
| 4594 | oss << "'" | oss << " PKT2 = "<< pkt1+1; | 
| 4595 | << t2 << "','"; | }; | 
| 4596 | }; | oss << " ) );"; | 
|  | // |  | 
|  | 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()); |  | 
| 4597 | // | // | 
| 4598 |  | if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str()); | 
| 4599 | result = conn->Query(oss.str().c_str()); | result = conn->Query(oss.str().c_str()); | 
| 4600 | // | // | 
| 4601 | if ( !result ) throw -4; | if ( !result ) throw -4; | 
| 4602 | // | // | 
| 4603 | }; | row = result->Next(); | 
|  | // |  | 
|  | }; |  | 
|  |  |  | 
|  | /** |  | 
|  | * 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 |  | 
|  | // |  | 
| 4604 | // | // | 
| 4605 | if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){ | if ( row ){ | 
|  | //    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 --> "<<valid<<endl; |  | 
|  | // |  | 
|  | // Do we have the second calibration packet? |  | 
|  | // |  | 
|  | while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1 |  | 
|  | // |  | 
|  | t2++; |  | 
|  | // |  | 
|  | pret2 = t2 - 1; // EMILIANO |  | 
| 4606 | // | // | 
| 4607 | if ( t2 < nevents2 ){ | if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n"); | 
| 4608 | tr2->GetEntry(t2); | if ( PEDANTIC ) throw -80; | 
|  | 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; |  | 
|  | //      }; |  | 
|  |  |  | 
|  |  |  | 
| 4609 | // | // | 
| 4610 | }; | } else { | 
|  | // |  | 
|  | 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 ){ |  | 
| 4611 | // | // | 
| 4612 | if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2); | // we have to insert a new calibration, check where to place it | 
|  | if ( IsDebug() ) printf(" The trk calibration2 at obt %u pkt %u t2 is %u is good \n",obt2,pkt2,t2); |  | 
| 4613 | // | // | 
| 4614 | UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 ); | oss.str(""); | 
| 4615 | if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl; | oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE " | 
| 4616 | valid = valid & valid2; | << " FROM_TIME < "<< fromtime << " AND " | 
| 4617 |  | << " TO_TIME > "<< fromtime << ";"; | 
| 4618 | // | // | 
| 4619 | // Handle good calib | if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str()); | 
| 4620 |  | result = conn->Query(oss.str().c_str()); | 
| 4621 | // | // | 
| 4622 | this->HandleTRK_CALIB(true,true); | if ( !result ) throw -4; | 
| 4623 | // | // | 
| 4624 | // Check for missing calibtrk1 | row = result->Next(); | 
| 4625 | // | // | 
| 4626 | if ( t2 != pret2+1 ){ | if ( !row ){ | 
|  | // |  | 
|  | 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 ){ |  | 
| 4627 | // | // | 
| 4628 | // handle missing calib1 | // no calibrations in the db contain our calibration | 
| 4629 | // | // | 
| 4630 | pret2++; | if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n"); | 
| 4631 |  | 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 | 
| 4632 | // | // | 
| 4633 | obt1 = 0; | oss.str(""); | 
| 4634 | pkt1 = 0; | oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE " | 
| 4635 |  | << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;"; | 
| 4636 | // | // | 
| 4637 | tr2->GetEntry(pret2); | if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str()); | 
| 4638 | ph2 = eh2->GetPscuHeader(); | result = conn->Query(oss.str().c_str()); | 
|  | obt2 = ph2->GetOrbitalTime(); |  | 
|  | pkt2 = ph2->GetCounter(); |  | 
| 4639 | // | // | 
| 4640 | fromtime = this->GetAbsTime(ph2->GetOrbitalTime()); | if ( !result ) throw -4; | 
| 4641 | // | // | 
| 4642 | valid = 0; | row = result->Next(); | 
| 4643 | this->HandleTRK_CALIB(false,true); | if ( !row ){ | 
| 4644 |  | totime = numeric_limits<UInt_t>::max(); | 
| 4645 |  | } else { | 
| 4646 |  | totime = (UInt_t)atoll(row->GetField(0)); | 
| 4647 |  | }; | 
| 4648 | // | // | 
| 4649 | }; | } else { | 
| 4650 | // | // | 
| 4651 |  | // determine upper and lower limits and make space for the new calibration | 
| 4652 |  | // | 
| 4653 |  | totime = (UInt_t)atoll(row->GetField(1)); | 
| 4654 |  | // | 
| 4655 |  | oss.str(""); | 
| 4656 |  | oss << " UPDATE GL_TRK_CALIB SET " | 
| 4657 |  | << " TO_TIME = "<< fromtime << " WHERE "  // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[ | 
| 4658 |  | << " ID = "<< row->GetField(0) << ";"; | 
| 4659 |  | // | 
| 4660 |  | if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str()); | 
| 4661 |  | result = conn->Query(oss.str().c_str()); | 
| 4662 |  | // | 
| 4663 |  | if ( !result ) throw -4; | 
| 4664 |  | // | 
| 4665 |  | }; | 
| 4666 |  | // | 
| 4667 |  | oss.str(""); | 
| 4668 |  | 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) " | 
| 4669 |  | << " VALUES (NULL,' " | 
| 4670 |  | << idroot << "',"; | 
| 4671 |  | // | 
| 4672 |  | if ( !pk1 ){ | 
| 4673 |  | oss << "NULL,"; | 
| 4674 |  | } else { | 
| 4675 |  | oss << "'" | 
| 4676 |  | << t1 << "',"; | 
| 4677 | }; | }; | 
| 4678 | // | // | 
| 4679 | } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){ | if ( !pk2 ){ | 
| 4680 |  | oss << "NULL,'"; | 
| 4681 |  | } else { | 
| 4682 |  | oss << "'" | 
| 4683 |  | << t2 << "','"; | 
| 4684 |  | }; | 
| 4685 | // | // | 
| 4686 | // Check for missing calibtrk2 | oss << fromtime << "','" | 
| 4687 |  | << totime << "','" | 
| 4688 |  | << obt1 << "','" | 
| 4689 |  | << pkt1 << "','" | 
| 4690 |  | << obt2 << "','" | 
| 4691 |  | << pkt2 << "','" | 
| 4692 |  | << boot_number << "','" | 
| 4693 |  | << valid << "');"; | 
| 4694 | // | // | 
| 4695 | if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2); | if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str()); | 
|  | t2 = pret2; |  | 
| 4696 | // | // | 
| 4697 | // handle missing calib2 | result = conn->Query(oss.str().c_str()); | 
| 4698 | // | // | 
| 4699 | obt2 = 0; | if ( !result ) throw -4; | 
|  | pkt2 = 0; |  | 
|  | valid = 0; |  | 
|  | this->HandleTRK_CALIB(true,false); |  | 
| 4700 | // | // | 
|  | }; |  | 
|  | // |  | 
|  | } 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; |  | 
|  | // |  | 
| 4701 | }; | }; | 
| 4702 | // |  | 
| 4703 | }; | oss.str(""); | 
| 4704 | // | oss << " SELECT ID FROM GL_TRK_CALIB ORDER BY ID DESC LIMIT 1 ;"; | 
| 4705 | // we have one more calib pkt2 ! | if ( IsDebug() ) cout << oss.str().c_str() << endl; | 
| 4706 | // | result = conn->Query(oss.str().c_str()); | 
| 4707 | t2++; | if ( !result ) throw -4;; | 
| 4708 | while ( t2 < nevents2 ){ | row = result->Next(); | 
| 4709 |  | if(row)glcal->ID = (UInt_t)atoll(row->GetField(0)); | 
| 4710 |  |  | 
| 4711 |  | // | 
| 4712 |  | }; | 
| 4713 |  |  | 
| 4714 |  | /** | 
| 4715 |  | * Scan tracker calibrations packets, fill the GL_TRK_CALIB table | 
| 4716 |  | */ | 
| 4717 |  | Int_t PamelaDBOperations::insertTRK_CALIB(){ | 
| 4718 | // | // | 
| 4719 | // handle missing calib1 | CalibTrk1Event *caltrk1 = 0; | 
| 4720 |  | CalibTrk2Event *caltrk2 = 0; | 
| 4721 |  | TTree *tr1 = 0; | 
| 4722 |  | TTree *tr2 = 0; | 
| 4723 |  | EventHeader *eh1 = 0; | 
| 4724 |  | PscuHeader *ph1 = 0; | 
| 4725 |  | EventHeader *eh2 = 0; | 
| 4726 |  | PscuHeader *ph2 = 0; | 
| 4727 |  | // | 
| 4728 |  | PacketType *pctp=0; | 
| 4729 |  | EventCounter *codt2=0; | 
| 4730 |  | // | 
| 4731 |  | Int_t nevents1 = 0; | 
| 4732 |  | Int_t nevents2 = 0; | 
| 4733 |  | // | 
| 4734 |  | fromtime = 0; | 
| 4735 | // | // | 
|  | if ( IsDebug() ) printf(" t2 is %u nevents2 is %u \n",t2,nevents2); |  | 
| 4736 | obt1 = 0; | obt1 = 0; | 
| 4737 | pkt1 = 0; | pkt1 = 0; | 
| 4738 |  | obt2 = 0; | 
| 4739 |  | pkt2 = 0; | 
| 4740 | // | // | 
| 4741 | tr2->GetEntry(t2); | tr1 = (TTree*)file->Get("CalibTrk1"); | 
| 4742 | ph2 = eh2->GetPscuHeader(); | if ( !tr1 || tr1->IsZombie() ) throw -22; | 
| 4743 | obt2 = ph2->GetOrbitalTime(); | tr2 = (TTree*)file->Get("CalibTrk2"); | 
| 4744 | pkt2 = ph2->GetCounter(); | if ( !tr2 || tr2->IsZombie() ) throw -23; | 
| 4745 | // | // | 
| 4746 | fromtime = this->GetAbsTime(ph2->GetOrbitalTime()); | tr1->SetBranchAddress("CalibTrk1", &caltrk1); | 
| 4747 | valid = 0; | tr1->SetBranchAddress("Header", &eh1); | 
| 4748 | //  if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){ | nevents1 = tr1->GetEntries(); | 
| 4749 | // EMILIANO | tr2->SetBranchAddress("CalibTrk2", &caltrk2); | 
| 4750 | if ( this->PKT(pkt2) >= this->PKT(pktfirst) && this->PKT(pkt2 <= upperpkt) && this->OBT(obt2) >= this->OBT(obtfirst) && this->OBT(obt2) <= upperobt ){ | tr2->SetBranchAddress("Header", &eh2); | 
| 4751 | //    if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){ | nevents2 = tr2->GetEntries(); | 
| 4752 | // | // | 
| 4753 | if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2); | if ( !nevents1 && !nevents2 ) return(1); | 
| 4754 | // | // | 
| 4755 | this->HandleTRK_CALIB(false,true); | t2 = -1; | 
| 4756 | // | Int_t pret2 = 0; | 
| 4757 | }; | Int_t t2t1cal = 0; | 
| 4758 |  | // | 
| 4759 |  | bool MISSING_pkt1 = true; | 
| 4760 |  | bool MISSING_pkt2 = true; | 
| 4761 |  | int  ncalib = 0; | 
| 4762 |  | bool try_to_recover = false; | 
| 4763 | // | // | 
| 4764 | t2++; | for (t1=0; t1 < nevents1; t1++){//loop over packet1 | 
| 4765 |  | // | 
| 4766 |  | pret2 = t2; | 
| 4767 |  | tr1->GetEntry(t1); | 
| 4768 |  | // | 
| 4769 |  | ph1 = eh1->GetPscuHeader(); | 
| 4770 |  | obt1 = ph1->GetOrbitalTime(); | 
| 4771 |  | pkt1 = ph1->GetCounter(); | 
| 4772 |  | fromtime = GetAbsTime(ph1->GetOrbitalTime()); | 
| 4773 |  | // | 
| 4774 |  | // chek if the packet number and obt are consistent with the other packets ??? | 
| 4775 |  | // | 
| 4776 |  | if ( PKT(pkt1) >= PKT(pktfirst) && PKT(pkt1) <= upperpkt && OBT(obt1) >= OBT(obtfirst) && OBT(obt1) <= upperobt ){ | 
| 4777 |  | //    if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){ | 
| 4778 |  | // | 
| 4779 |  | if ( IsDebug() ) printf("\n Trk calibration1 %u at time %u obt %u pkt %u \n",t1,fromtime,obt1,pkt1); | 
| 4780 |  | // | 
| 4781 |  | valid = ValidateTrkCalib( caltrk1, eh1 ); | 
| 4782 |  | if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl; | 
| 4783 |  | // | 
| 4784 |  | // Do we have the second calibration packet? | 
| 4785 |  | // | 
| 4786 |  | if ( IsDebug() ) cout << " Loop over calibration2 to search associated calibration: "<<endl; | 
| 4787 |  | while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1 | 
| 4788 |  | // | 
| 4789 |  | t2++; | 
| 4790 |  | // | 
| 4791 |  | pret2 = t2 - 1; // EMILIANO | 
| 4792 |  | // | 
| 4793 |  | if ( t2 < nevents2 ){ | 
| 4794 |  | tr2->GetEntry(t2); | 
| 4795 |  | codt2 = eh2->GetCounter(); | 
| 4796 |  | t2t1cal = codt2->Get(pctp->CalibTrk1); | 
| 4797 |  | // | 
| 4798 |  | ph2 = eh2->GetPscuHeader(); | 
| 4799 |  | obt2 = ph2->GetOrbitalTime(); | 
| 4800 |  | pkt2 = ph2->GetCounter(); | 
| 4801 |  | // | 
| 4802 |  | if ( IsDebug() ) printf(" >> trk calibration2 at obt %u pkt %u t2 is %u , t2t1cal is %u \n",obt2,pkt2,t2,t2t1cal); | 
| 4803 |  | //    if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2 | 
| 4804 |  | // | 
| 4805 |  | } else { | 
| 4806 |  | // | 
| 4807 |  | // running out of vector without finding the corresponding calibration, sig | 
| 4808 |  | // | 
| 4809 |  | if ( IsDebug() ) printf(" t2 >= nevents2 \n"); | 
| 4810 |  | pret2 = t2; | 
| 4811 |  | obt2 = 0; | 
| 4812 |  | //    pkt2 = pkt1+2; | 
| 4813 |  | pkt2 = 0; | 
| 4814 |  | t2t1cal = t1+1; | 
| 4815 |  | }; | 
| 4816 |  | //      if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) && (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){ | 
| 4817 |  |  | 
| 4818 |  | // EMILIANO | 
| 4819 |  | //      if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) || (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){ | 
| 4820 |  | //        //    if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){ | 
| 4821 |  | //        if ( IsDebug() ) printf(" running out of vector without finding the corresponding calibration, sig \n"); | 
| 4822 |  | //        // | 
| 4823 |  | //        // running out of vector without finding the corresponding calibration, sig | 
| 4824 |  | //        // | 
| 4825 |  | //        pret2 = t2; | 
| 4826 |  | //        obt2 = 0; | 
| 4827 |  | //        //      pkt2 = pkt1+2; | 
| 4828 |  | //        pkt2 = 0; | 
| 4829 |  | //        t2t1cal = t1+1; | 
| 4830 |  | //      }; | 
| 4831 |  |  | 
| 4832 |  |  | 
| 4833 |  | // | 
| 4834 |  | }; | 
| 4835 |  | // | 
| 4836 |  | if ( IsDebug() ) printf(" Check if trk calibration2 is the right one \n"); | 
| 4837 |  | // | 
| 4838 |  | // EMILIANO | 
| 4839 |  | if ( ( PKT(pkt2) < PKT(pktfirst) || PKT(pkt2) > upperpkt) || (OBT(obt2) < OBT(obtfirst) || OBT(obt2) > upperobt) ){ | 
| 4840 |  | //      if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){ | 
| 4841 |  | if ( IsDebug() ) printf(" *WARNING*  The calibration found is outside the interval, sig \n"); | 
| 4842 |  | // | 
| 4843 |  | // running out of vector without finding the corresponding calibration, sig | 
| 4844 |  | // | 
| 4845 |  | pret2 = t2; | 
| 4846 |  | obt2 = 0; | 
| 4847 |  | pkt2 = 0; | 
| 4848 |  | }; | 
| 4849 |  | if ( PKT(pkt2) == PKT(pkt1)+1 ){ | 
| 4850 |  | if ( IsDebug() ) cout << " ...OK"<<endl; | 
| 4851 |  | // ======================= | 
| 4852 |  | // The calibration is good | 
| 4853 |  | // ======================= | 
| 4854 |  | // | 
| 4855 |  | //      if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2); | 
| 4856 |  | //      if ( IsDebug() ) printf(" Trk calibration2 at obt %u pkt %u t2 is %u is good \n",obt2,pkt2,t2); | 
| 4857 |  | //        if ( IsDebug() ) printf("\n Trk calibration2 at time %u obt %u pkt %u \n",fromtime,obt2,pkt2); | 
| 4858 |  | if ( IsDebug() ) printf(" Trk calibration2 %u at time %u obt %u pkt %u \n",t2,fromtime,obt2,pkt2); | 
| 4859 |  | // | 
| 4860 |  | UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 ); | 
| 4861 |  | if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl; | 
| 4862 |  | //      valid = valid & valid2; | 
| 4863 |  | valid = valid & valid2; //QUESTO VA CAMBIATO | 
| 4864 |  | // | 
| 4865 |  | // Handle good calib | 
| 4866 |  | // | 
| 4867 |  | MISSING_pkt1 = false; | 
| 4868 |  | MISSING_pkt2 = false; | 
| 4869 |  | //              this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2); | 
| 4870 |  | // | 
| 4871 |  | // Check for missing calibtrk1 | 
| 4872 |  | // | 
| 4873 |  | if ( t2 != pret2+1 ){ | 
| 4874 |  | // | 
| 4875 |  | 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); | 
| 4876 |  | // | 
| 4877 |  | while ( t2 > pret2+1 ){ | 
| 4878 |  | // | 
| 4879 |  | // handle missing calib1 | 
| 4880 |  | // | 
| 4881 |  | pret2++; | 
| 4882 |  | // | 
| 4883 |  | obt1 = 0; | 
| 4884 |  | pkt1 = 0; | 
| 4885 |  | // | 
| 4886 |  | tr2->GetEntry(pret2); | 
| 4887 |  | ph2 = eh2->GetPscuHeader(); | 
| 4888 |  | obt2 = ph2->GetOrbitalTime(); | 
| 4889 |  | pkt2 = ph2->GetCounter(); | 
| 4890 |  | // | 
| 4891 |  | fromtime = this->GetAbsTime(ph2->GetOrbitalTime()); | 
| 4892 |  | // | 
| 4893 |  | valid = 0; | 
| 4894 |  | MISSING_pkt1 = true; | 
| 4895 |  | MISSING_pkt2 = false; | 
| 4896 |  | //                      this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2); | 
| 4897 |  | // | 
| 4898 |  | }; | 
| 4899 |  | // | 
| 4900 |  | }; | 
| 4901 |  | // | 
| 4902 |  | } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){ | 
| 4903 |  | // | 
| 4904 |  | // Check for missing calibtrk2 | 
| 4905 |  | // | 
| 4906 |  | if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2); | 
| 4907 |  | t2 = pret2; | 
| 4908 |  | // | 
| 4909 |  | // handle missing calib2 | 
| 4910 |  | // | 
| 4911 |  | obt2 = 0; | 
| 4912 |  | pkt2 = 0; | 
| 4913 |  | valid = 0; | 
| 4914 |  | MISSING_pkt1 = false; | 
| 4915 |  | MISSING_pkt2 = true; | 
| 4916 |  | //              this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2); | 
| 4917 |  | // | 
| 4918 |  | }; | 
| 4919 |  | // | 
| 4920 |  |  | 
| 4921 |  | if( !(MISSING_pkt1&MISSING_pkt2) ){ | 
| 4922 |  | this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2); | 
| 4923 |  | ncalib++; | 
| 4924 |  | if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true; | 
| 4925 |  | } | 
| 4926 |  |  | 
| 4927 |  |  | 
| 4928 |  | } else { | 
| 4929 |  | // | 
| 4930 |  | if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u OUTSIDE the considered time interval \n",fromtime,obt1,pkt1); | 
| 4931 |  | //      if ( PEDANTIC ) throw -79; | 
| 4932 |  | // | 
| 4933 |  | }; | 
| 4934 |  | // | 
| 4935 |  |  | 
| 4936 |  | }; //end loop on pkt1 | 
| 4937 |  |  | 
| 4938 |  |  | 
| 4939 |  |  | 
| 4940 | // | // | 
| 4941 | }; | // we have one more calib pkt2 ! | 
| 4942 |  | // | 
| 4943 |  | t2++; | 
| 4944 |  | while ( t2 < nevents2 ){ | 
| 4945 |  | // | 
| 4946 |  | // handle missing calib1 | 
| 4947 |  | // | 
| 4948 |  | if ( IsDebug() ) printf(" t2 is %u nevents2 is %u \n",t2,nevents2); | 
| 4949 |  | obt1 = 0; | 
| 4950 |  | pkt1 = 0; | 
| 4951 |  | // | 
| 4952 |  | tr2->GetEntry(t2); | 
| 4953 |  | ph2 = eh2->GetPscuHeader(); | 
| 4954 |  | obt2 = ph2->GetOrbitalTime(); | 
| 4955 |  | pkt2 = ph2->GetCounter(); | 
| 4956 |  | // | 
| 4957 |  | fromtime = this->GetAbsTime(ph2->GetOrbitalTime()); | 
| 4958 |  | valid = 0; | 
| 4959 |  | //  if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){ | 
| 4960 |  | // EMILIANO | 
| 4961 |  | if ( this->PKT(pkt2) >= this->PKT(pktfirst) && this->PKT(pkt2 <= upperpkt) && this->OBT(obt2) >= this->OBT(obtfirst) && this->OBT(obt2) <= upperobt ){ | 
| 4962 |  | //    if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){ | 
| 4963 |  | // | 
| 4964 |  | if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2); | 
| 4965 |  | // | 
| 4966 |  | MISSING_pkt1 = true; | 
| 4967 |  | MISSING_pkt2 = false; | 
| 4968 |  | this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2); | 
| 4969 |  | ncalib++; | 
| 4970 |  | if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true; | 
| 4971 |  | // | 
| 4972 |  | }; | 
| 4973 |  | // | 
| 4974 |  | t2++; | 
| 4975 |  | // | 
| 4976 |  | }; | 
| 4977 |  |  | 
| 4978 |  | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
| 4979 |  | // ----------------------------------------------------------------- | 
| 4980 |  | // in case of corruption, check if the calibration can be recovered | 
| 4981 |  | // from another chewbacca file | 
| 4982 |  | // ----------------------------------------------------------------- | 
| 4983 |  | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
| 4984 |  |  | 
| 4985 |  | //    cout <<" TRY TO RECOVER ?? "<<try_to_recover<<endl; | 
| 4986 |  |  | 
| 4987 |  | if(chewbacca&&try_to_recover){ | 
| 4988 |  |  | 
| 4989 |  |  | 
| 4990 |  | if ( IsDebug() ) cout << endl << ">>>> TRY TO RECOVER TRACKER CALIBRATIONS <<<<"<<endl; | 
| 4991 |  |  | 
| 4992 |  | TSQLResult *result = 0; | 
| 4993 |  | TSQLRow    *row    = 0; | 
| 4994 |  | // | 
| 4995 |  | stringstream oss; | 
| 4996 |  | oss.str(""); | 
| 4997 |  | // | 
| 4998 |  |  | 
| 4999 |  | //////////////////////////////////////////////////////////////////////// | 
| 5000 |  | // retrieve the name of the current file: | 
| 5001 |  | //////////////////////////////////////////////////////////////////////// | 
| 5002 |  | oss.str(""); | 
| 5003 |  | oss << "SELECT NAME FROM GL_ROOT where ID=" << GetID_ROOT() <<";"; | 
| 5004 |  | if ( IsDebug() ) cout << oss.str().c_str() << endl; | 
| 5005 |  |  | 
| 5006 |  | result = conn->Query(oss.str().c_str()); | 
| 5007 |  | if ( !result ) throw -4;; | 
| 5008 |  | row = result->Next(); | 
| 5009 |  | TString thisfilename = (TString)row->GetField(0); | 
| 5010 |  | if ( IsDebug() ) cout << "Current file ==> "<<thisfilename<<endl; | 
| 5011 |  |  | 
| 5012 |  | //////////////////////////////////////////////////////////////////////// | 
| 5013 |  | // read all the calibrations inserted | 
| 5014 |  | //////////////////////////////////////////////////////////////////////// | 
| 5015 |  | oss.str(""); | 
| 5016 |  | oss << " SELECT "; | 
| 5017 |  | oss << " ID,FROM_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,TO_TIME"; | 
| 5018 |  | oss << " FROM GL_TRK_CALIB "; | 
| 5019 |  | oss << " ORDER BY ID DESC LIMIT "<<ncalib<<"; "; | 
| 5020 |  | if ( IsDebug() ) cout << oss.str().c_str() << endl; | 
| 5021 |  |  | 
| 5022 |  | result = conn->Query(oss.str().c_str()); | 
| 5023 |  | if ( !result ) throw -4;; | 
| 5024 |  | if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl; | 
| 5025 |  |  | 
| 5026 |  | // ----------------------------------- | 
| 5027 |  | // loop over calibrations ... | 
| 5028 |  | // ----------------------------------- | 
| 5029 |  | UInt_t nn=0; | 
| 5030 |  | do { | 
| 5031 |  | row = result->Next(); | 
| 5032 |  | if(!row)break; | 
| 5033 |  |  | 
| 5034 |  | UInt_t id       = (UInt_t)atoll(row->GetField(0)); | 
| 5035 |  | UInt_t fromtime = (UInt_t)atoll(row->GetField(1)); | 
| 5036 |  | UInt_t obt1     = (UInt_t)atoll(row->GetField(2)); | 
| 5037 |  | UInt_t pkt1     = (UInt_t)atoll(row->GetField(3)); | 
| 5038 |  | UInt_t obt2     = (UInt_t)atoll(row->GetField(4)); | 
| 5039 |  | UInt_t pkt2     = (UInt_t)atoll(row->GetField(5)); | 
| 5040 |  | UInt_t boot     = (UInt_t)atoll(row->GetField(6)); | 
| 5041 |  | UInt_t valid    = (UInt_t)atoll(row->GetField(7)); | 
| 5042 |  | bool MISSING_pkt1 = (row->GetFieldLength(8)==0); | 
| 5043 |  | bool MISSING_pkt2 = (row->GetFieldLength(9)==0); | 
| 5044 |  | UInt_t totime   = (UInt_t)atoll(row->GetField(10)); | 
| 5045 |  |  | 
| 5046 |  | // ------------------------------------- | 
| 5047 |  | // ...check if the entry is corrupted... | 
| 5048 |  | // ------------------------------------- | 
| 5049 |  | cout <<"*** "<< MISSING_pkt1 << MISSING_pkt2 << valid <<endl; | 
| 5050 |  | bool CORRUPTED = (MISSING_pkt1||MISSING_pkt2||!valid); | 
| 5051 |  |  | 
| 5052 |  | if ( IsDebug() ) cout << "("<<nn<<")  ID = "<<id<<" from GL_TRK_CALIB  ==>  corrupted ? "<<CORRUPTED<<endl; | 
| 5053 |  |  | 
| 5054 |  | //          if( !CORRUPTED  )continue; // nothing to do | 
| 5055 |  |  | 
| 5056 |  | ///////////////////////////////////////////////////////// | 
| 5057 |  | // if it is corrupted, ...look for ather chewbacca files | 
| 5058 |  | // containing the same calibrations ... | 
| 5059 |  | ///////////////////////////////////////////////////////// | 
| 5060 |  |  | 
| 5061 |  | bool this_MISSING_pkt1 = false; | 
| 5062 |  | bool this_MISSING_pkt2 = false; | 
| 5063 |  | int  this_t1=0; | 
| 5064 |  | int  this_t2=0;; | 
| 5065 |  | UInt_t this_valid = 0; | 
| 5066 |  |  | 
| 5067 |  | TString path       = ""; | 
| 5068 |  | TString name       = ""; | 
| 5069 |  | TString raw        = ""; | 
| 5070 |  | UInt_t obt0        = 0; | 
| 5071 |  | UInt_t timesync    = 0; | 
| 5072 |  | UInt_t boot_number = 0; | 
| 5073 |  | bool   FOUND       = false; | 
| 5074 |  |  | 
| 5075 |  | if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl; | 
| 5076 |  |  | 
| 5077 |  | //          for(int itable=0; itable<2; itable++){ | 
| 5078 |  | for(int itable=0; itable<1; itable++){ | 
| 5079 |  |  | 
| 5080 |  | // ------------------------------------------------------ | 
| 5081 |  | // loop over both ROOT_TABLE and ROOT_TABLE_BAD | 
| 5082 |  | // ------------------------------------------------------ | 
| 5083 |  |  | 
| 5084 |  | TString table = "ROOT_TABLE"; | 
| 5085 |  | if(itable==1)table = "ROOT_TABLE_BAD"; | 
| 5086 |  |  | 
| 5087 |  | oss.str(""); | 
| 5088 |  | oss << " SELECT "; | 
| 5089 |  | oss << " FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,BOOT_NUMBER,INPUT_NAME "; | 
| 5090 |  | oss << " FROM "<<table; | 
| 5091 |  | oss << " WHERE 1 " << endl; | 
| 5092 |  | oss << " AND FILE_NAME != \""<< thisfilename<<"\""; | 
| 5093 |  | if( !MISSING_pkt1 ){ | 
| 5094 |  | oss << " AND "; | 
| 5095 |  | oss << " PKT_NUMBER_INIT < "<<pkt1; | 
| 5096 |  | oss << " AND "; | 
| 5097 |  | oss << " PKT_NUMBER_FINAL > "<<pkt1; | 
| 5098 |  | oss << " AND "; | 
| 5099 |  | oss << " PKT_OBT_INIT < "<<obt1; | 
| 5100 |  | oss << " AND "; | 
| 5101 |  | oss << " PKT_OBT_FINAL > "<<obt1; | 
| 5102 |  | }else{ | 
| 5103 |  | if(pkt2>1) pkt1 = pkt2-1;//serve dopo | 
| 5104 |  | } | 
| 5105 |  | if( !MISSING_pkt2 ){ | 
| 5106 |  | oss << " AND "; | 
| 5107 |  | oss << " PKT_NUMBER_INIT < "<<pkt2; | 
| 5108 |  | oss << " AND "; | 
| 5109 |  | oss << " PKT_NUMBER_FINAL > "<<pkt2; | 
| 5110 |  | oss << " AND "; | 
| 5111 |  | oss << " PKT_OBT_INIT < "<<obt2; | 
| 5112 |  | oss << " AND "; | 
| 5113 |  | oss << " PKT_OBT_FINAL > "<<obt2; | 
| 5114 |  | }else{ | 
| 5115 |  | if(pkt1>0) pkt2 = pkt1+1;//serve dopo | 
| 5116 |  | } | 
| 5117 |  | if( boot> 0 ){ | 
| 5118 |  | oss << " AND "; | 
| 5119 |  | oss << " BOOT_NUMBER = "<<boot; | 
| 5120 |  | }else{ | 
| 5121 |  | } | 
| 5122 |  | oss << " ORDER BY BAD_PKT_CALREAD ASC; "; | 
| 5123 |  |  | 
| 5124 |  | TSQLResult *result2 = 0; | 
| 5125 |  | TSQLRow    *row2    = 0; | 
| 5126 |  |  | 
| 5127 |  | if ( IsDebug() ) cout << oss.str().c_str() << endl; | 
| 5128 |  | result2 = conn->Query(oss.str().c_str()); | 
| 5129 |  | if ( !result2 ) throw -4;; | 
| 5130 |  | if ( IsDebug() ) cout <<"Rows: "<<result2->GetRowCount()<<endl; | 
| 5131 |  |  | 
| 5132 |  | // ------------------------------------------------------ | 
| 5133 |  | // loop over files containing repetition (if any) | 
| 5134 |  | // ------------------------------------------------------ | 
| 5135 |  | do { | 
| 5136 |  | row2 = result2->Next(); | 
| 5137 |  | if(!row2)break; | 
| 5138 |  |  | 
| 5139 |  | // ------------------------------------------------------ | 
| 5140 |  | // ... a repetition is found ... | 
| 5141 |  | // ------------------------------------------------------ | 
| 5142 |  | path       = (TString)row2->GetField(0); | 
| 5143 |  | name       = (TString)row2->GetField(1); | 
| 5144 |  | raw        = (TString)row2->GetField(5); | 
| 5145 |  | obt0        = (UInt_t)atoll(row2->GetField(2)); | 
| 5146 |  | timesync    = (UInt_t)atoll(row2->GetField(3)); | 
| 5147 |  | boot_number = (UInt_t)atoll(row2->GetField(4)); | 
| 5148 |  |  | 
| 5149 |  | if ( IsDebug() ) cout << "- - - - - - - - - - -" <<endl; | 
| 5150 |  | //                  cout << path <<endl; | 
| 5151 |  | //                  cout << "File    : " <<name <<endl; | 
| 5152 |  | //                  cout << obt0 <<endl; | 
| 5153 |  | //                  cout << timesync <<endl; | 
| 5154 |  | //                  cout << "boot n. : "<<boot_number <<endl; | 
| 5155 |  | //                  cout << raw <<endl; | 
| 5156 |  |  | 
| 5157 |  | // ------------------------------------------------------ | 
| 5158 |  | // ... retrieve the calibration packets. | 
| 5159 |  | // ------------------------------------------------------ | 
| 5160 |  | if ( IsDebug() ) printf(" file is %s/%s \n",((TString)gSystem->ExpandPathName(path.Data())).Data(),name.Data()); | 
| 5161 |  | TFile *file = new TFile(((TString)gSystem->ExpandPathName(path.Data()))+"/"+name); // EM, path could be symbolic and we must expand it | 
| 5162 |  | if(!file)throw -100; | 
| 5163 |  | if(file->IsZombie())throw -100; | 
| 5164 |  | // | 
| 5165 |  | tr1 = (TTree*)file->Get("CalibTrk1"); | 
| 5166 |  | if ( !tr1 || tr1->IsZombie() ) throw -22; | 
| 5167 |  | tr2 = (TTree*)file->Get("CalibTrk2"); | 
| 5168 |  | if ( !tr2 || tr2->IsZombie() ) throw -23; | 
| 5169 |  | // | 
| 5170 |  | tr1->SetBranchAddress("CalibTrk1", &caltrk1); | 
| 5171 |  | tr1->SetBranchAddress("Header", &eh1); | 
| 5172 |  | nevents1 = tr1->GetEntries(); | 
| 5173 |  | tr2->SetBranchAddress("CalibTrk2", &caltrk2); | 
| 5174 |  | tr2->SetBranchAddress("Header", &eh2); | 
| 5175 |  | nevents2 = tr2->GetEntries(); | 
| 5176 |  | for(this_t1=0; this_t1<nevents1; this_t1++){ | 
| 5177 |  | tr1->GetEntry(this_t1); | 
| 5178 |  | if( | 
| 5179 |  | (UInt_t)eh1->GetPscuHeader()->GetCounter() == pkt1 && | 
| 5180 |  | true) break; | 
| 5181 |  | this_MISSING_pkt1 = true; | 
| 5182 |  | } | 
| 5183 |  | for(this_t2=0; this_t2<nevents2; this_t2++){ | 
| 5184 |  | tr2->GetEntry(this_t2); | 
| 5185 |  | if( | 
| 5186 |  | (UInt_t)eh2->GetPscuHeader()->GetCounter() == pkt2 && | 
| 5187 |  | true) break; | 
| 5188 |  | this_MISSING_pkt2 = true; | 
| 5189 |  | } | 
| 5190 |  | this_valid = | 
| 5191 |  | ValidateTrkCalib( caltrk1, eh1, file ) | 
| 5192 |  | * | 
| 5193 |  | ValidateTrkCalib( caltrk2, eh2, file ); | 
| 5194 |  |  | 
| 5195 |  | // --------------------------------------------------------------------- | 
| 5196 |  | // accept the calibration if it is better than the previous: | 
| 5197 |  | // | 
| 5198 |  | // - if the new calibration is perfect (both valid packets) | 
| 5199 |  | // - if the new calibration has both the packets and the previous does not | 
| 5200 |  | // --------------------------------------------------------------------- | 
| 5201 |  | if( | 
| 5202 |  | ( !this_MISSING_pkt1&&!this_MISSING_pkt2&&this_valid )|| | 
| 5203 |  | ( (MISSING_pkt1||MISSING_pkt2) && (!this_MISSING_pkt1&&!this_MISSING_pkt2) )|| | 
| 5204 |  | false)FOUND=true; | 
| 5205 |  |  | 
| 5206 |  | if(file)file->Close(); | 
| 5207 |  |  | 
| 5208 |  | if(FOUND)break; | 
| 5209 |  |  | 
| 5210 |  | }while(1);//endl loop over root table entries | 
| 5211 |  |  | 
| 5212 |  | if(FOUND)break; | 
| 5213 |  |  | 
| 5214 |  | }//end loop over tables | 
| 5215 |  |  | 
| 5216 |  | if(FOUND){ | 
| 5217 |  |  | 
| 5218 |  | if ( IsDebug() ) cout << " >>> REPETITION FOUND :-) <<<" <<endl; | 
| 5219 |  |  | 
| 5220 |  | //////////////////////////////////////////// | 
| 5221 |  | // insert a new entry in GL_TRK_CALIB and | 
| 5222 |  | // modify the time-tag of the previous one | 
| 5223 |  | //////////////////////////////////////////// | 
| 5224 |  |  | 
| 5225 |  | // --------------------------------------------------------------------- | 
| 5226 |  | // step 1: insert a new raw file in GL_RAW | 
| 5227 |  | // --------------------------------------------------------------------- | 
| 5228 |  | // | 
| 5229 |  | // check if the raw file already exist | 
| 5230 |  | // | 
| 5231 |  | UInt_t id_raw = 0;   // EM GL_RAW is there only for backward compatibility so we do not need to fill it when in "chewbacca" mode | 
| 5232 |  | //              oss.str(""); | 
| 5233 |  | //              oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";"; | 
| 5234 |  | //              if ( IsDebug() ) cout << oss.str().c_str() << endl; | 
| 5235 |  |  | 
| 5236 |  | //              result = conn->Query(oss.str().c_str()); | 
| 5237 |  | //              if ( !result ) throw -4;; | 
| 5238 |  | //              if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl; | 
| 5239 |  | //              if( result->GetRowCount() == 0){ | 
| 5240 |  | //                  if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl; | 
| 5241 |  | //                  // - - - - - - - - - - - | 
| 5242 |  | //                  // insert new raw file | 
| 5243 |  | //                  // - - - - - - - - - - - | 
| 5244 |  | //                  GL_RAW glraw = GL_RAW(); | 
| 5245 |  | //                  glraw.PATH        = gSystem->DirName(raw.Data()); | 
| 5246 |  | //                  glraw.NAME        = gSystem->BaseName(raw.Data()); | 
| 5247 |  | //                  glraw.BOOT_NUMBER = boot_number; | 
| 5248 |  | //                  // | 
| 5249 |  | //                  insertPamelaRawFile( &glraw ); | 
| 5250 |  | //                  // | 
| 5251 |  | //                  id_raw = glraw.ID; | 
| 5252 |  | //              }else{ | 
| 5253 |  | //                  row = result->Next(); | 
| 5254 |  | //                  id_raw = (UInt_t)atoll(row->GetField(0)); | 
| 5255 |  | //              } | 
| 5256 |  | //              if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl; | 
| 5257 |  |  | 
| 5258 |  | // --------------------------------------------------------------------- | 
| 5259 |  | // step 1(bis): retrieve the timesync id associated to the file | 
| 5260 |  | // (NB, uso lo stesso associato al file iniziale) | 
| 5261 |  | // --------------------------------------------------------------------- | 
| 5262 |  | UInt_t idtimesync = 0; | 
| 5263 |  | oss.str(""); | 
| 5264 |  | oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;"; | 
| 5265 |  | if ( debug ) printf(" %s \n",oss.str().c_str()); | 
| 5266 |  | result = conn->Query(oss.str().c_str()); | 
| 5267 |  | if ( !result ) throw -3; | 
| 5268 |  | row = result->Next(); | 
| 5269 |  | if ( !row ) throw -3; | 
| 5270 |  | idtimesync = (UInt_t)atoll(row->GetField(0)); | 
| 5271 |  | if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl; | 
| 5272 |  |  | 
| 5273 |  | // --------------------------------------------------------------------- | 
| 5274 |  | // step 2: insert a new root file in GL_ROOT | 
| 5275 |  | // --------------------------------------------------------------------- | 
| 5276 |  | // | 
| 5277 |  | // check if the root file already exist | 
| 5278 |  | // | 
| 5279 |  | UInt_t id_root = 0; | 
| 5280 |  | oss.str(""); | 
| 5281 |  | oss << "SELECT ID FROM GL_ROOT where NAME=\"" << gSystem->BaseName(name.Data()) <<"\";"; | 
| 5282 |  | if ( IsDebug() ) cout << oss.str().c_str() << endl; | 
| 5283 |  |  | 
| 5284 |  | result = conn->Query(oss.str().c_str()); | 
| 5285 |  | if ( !result ) throw -4;; | 
| 5286 |  | if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl; | 
| 5287 |  | if( result->GetRowCount() == 0){ | 
| 5288 |  | if ( IsDebug() ) cout << " << Insert new ROOT file >> "<<endl; | 
| 5289 |  | // - - - - - - - - - - - | 
| 5290 |  | // insert new root file | 
| 5291 |  | // - - - - - - - - - - - | 
| 5292 |  | GL_ROOT glroot = GL_ROOT(); | 
| 5293 |  | glroot.ID_RAW      = id_raw; | 
| 5294 |  | glroot.ID_TIMESYNC = idtimesync; | 
| 5295 |  | // | 
| 5296 |  | // EM STATIC = the full expanded path must be put in the DB, KEEPENV = the path given as input (or found in ROOT_TABLE) must be used, | 
| 5297 |  | //             NOT STATIC NOT KEEPENV = $PAM_L0 must be used in the DB | 
| 5298 |  | // | 
| 5299 |  | if ( STATIC ){ | 
| 5300 |  | glroot.PATH        = (TString)gSystem->ExpandPathName(path); | 
| 5301 |  | } else { | 
| 5302 |  | if ( KEEPENV ){ | 
| 5303 |  | glroot.PATH      = path; | 
| 5304 |  | } else { | 
| 5305 |  | glroot.PATH      = "$PAM_L0"; | 
| 5306 |  | }; | 
| 5307 |  | }; | 
| 5308 |  | //              glroot.PATH        = path; | 
| 5309 |  | glroot.NAME        = name; | 
| 5310 |  | // | 
| 5311 |  | insertPamelaRootFile( &glroot ); | 
| 5312 |  | // | 
| 5313 |  | id_root = glroot.ID; | 
| 5314 |  | }else{ | 
| 5315 |  | row = result->Next(); | 
| 5316 |  | if(row)id_root = (UInt_t)atoll(row->GetField(0)); | 
| 5317 |  | } | 
| 5318 |  | if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl; | 
| 5319 |  |  | 
| 5320 |  | // --------------------------------------------------------------------- | 
| 5321 |  | // step 3: modify time-tag of corrupted GL_TRK_CALIB entry | 
| 5322 |  | // --------------------------------------------------------------------- | 
| 5323 |  | if ( IsDebug() ) cout << " << Modify time-tag of calibration ID="<<id<<" >> "<<endl; | 
| 5324 |  | oss.str(""); | 
| 5325 |  | oss << " UPDATE GL_TRK_CALIB SET " | 
| 5326 |  | << " TO_TIME=0 , FROM_TIME=0  WHERE " | 
| 5327 |  | << " ID = "<< id  << ";"; | 
| 5328 |  | if ( IsDebug() ) cout << oss.str().c_str() << endl; | 
| 5329 |  | result = conn->Query(oss.str().c_str()); | 
| 5330 |  | if ( !result ) throw -4;; | 
| 5331 |  |  | 
| 5332 |  | // --------------------------------------------------------------------- | 
| 5333 |  | // step 4: insert the new calibration: | 
| 5334 |  | // --------------------------------------------------------------------- | 
| 5335 |  | if ( IsDebug() ) cout << " << Insert new TRK calibration >> "<<endl; | 
| 5336 |  | // | 
| 5337 |  | GL_TRK_CALIB glcal = GL_TRK_CALIB(); | 
| 5338 |  | // | 
| 5339 |  | glcal.ID_ROOT_L0        = id_root; | 
| 5340 |  | glcal.EV_ROOT_CALIBTRK1 = this_t1; | 
| 5341 |  | glcal.EV_ROOT_CALIBTRK2 = this_t2; | 
| 5342 |  | glcal.FROM_TIME         = fromtime; | 
| 5343 |  | glcal.TO_TIME           = totime; | 
| 5344 |  | glcal.OBT1              = obt1; | 
| 5345 |  | glcal.OBT2              = obt2; | 
| 5346 |  | glcal.PKT1              = pkt1; | 
| 5347 |  | glcal.PKT2              = pkt1; | 
| 5348 |  | glcal.BOOT_NUMBER       = GetBOOTnumber(); | 
| 5349 |  | glcal.VALIDATION        = this_valid; | 
| 5350 |  | // | 
| 5351 |  | HandleTRK_CALIB(&glcal); | 
| 5352 |  | if ( IsDebug() ) cout << "ID = "<<glcal.ID<<endl; | 
| 5353 |  | // | 
| 5354 |  |  | 
| 5355 |  | } | 
| 5356 |  | if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl; | 
| 5357 |  |  | 
| 5358 |  | }while(1);//end loop over calibrations | 
| 5359 |  |  | 
| 5360 |  |  | 
| 5361 |  | if( result )delete result; | 
| 5362 |  | if( row    )delete row; | 
| 5363 |  |  | 
| 5364 |  |  | 
| 5365 |  |  | 
| 5366 |  |  | 
| 5367 |  |  | 
| 5368 |  |  | 
| 5369 |  | } | 
| 5370 |  |  | 
| 5371 |  |  | 
| 5372 |  | //      // ------------------------------ | 
| 5373 |  | //      // try to recover the calibration | 
| 5374 |  | //      // ------------------------------ | 
| 5375 |  | //      cout << "TRY TO RECOVER TRACKER CALIBRATION"<<endl; | 
| 5376 |  | //      // | 
| 5377 |  | //      ULong64_t time  = 0; //absolute time | 
| 5378 |  | //      string    path[100]; //mettere un limite massimo | 
| 5379 |  | //      int       nrows = 0; | 
| 5380 |  | //      UInt_t    pkt   = 0; | 
| 5381 |  | //      UInt_t    obt   = 0; | 
| 5382 |  | //      char     *type  = ""; | 
| 5383 |  | //      EventHeader *eh   = new EventHeader(); | 
| 5384 |  | //      CalibTrk1Event *c = new CalibTrk1Event(); | 
| 5385 |  |  | 
| 5386 |  | //       // | 
| 5387 |  | //       if(which_is_not_valid==1 || which_is_not_valid==3){ | 
| 5388 |  | //        // | 
| 5389 |  | //        cout << "PKT1 --> missing or corrupted "<<endl; | 
| 5390 |  | //        type = "CalibTrk1"; | 
| 5391 |  | //        pkt  = pkt1; | 
| 5392 |  | //        obt  = obt1; | 
| 5393 |  | //        time = this->GetAbsTime(obt1); | 
| 5394 |  | //        if( pkt1 == 0 ){//missing | 
| 5395 |  | //            time = this->GetAbsTime(obt2); | 
| 5396 |  | //            pkt  = pkt2-1; | 
| 5397 |  | //        } | 
| 5398 |  | //        // | 
| 5399 |  | //       }else if (which_is_not_valid==2 || which_is_not_valid==3){ | 
| 5400 |  | //        // | 
| 5401 |  | //        cout << "PKT2--> missing or corrupted "<<endl; | 
| 5402 |  | //        type = "CalibTrk2 "; | 
| 5403 |  | //        pkt  = pkt2; | 
| 5404 |  | //        obt  = obt2; | 
| 5405 |  | //        time = this->GetAbsTime(obt2); | 
| 5406 |  | //        if( pkt2 == 0 ){//missing | 
| 5407 |  | //            time = this->GetAbsTime(obt1); | 
| 5408 |  | //            pkt  = pkt1+1; | 
| 5409 |  | //        } | 
| 5410 |  | //        // | 
| 5411 |  | //       }else{ | 
| 5412 |  | //        cout << "this should not happen!!! "<<endl; | 
| 5413 |  | //        trow -666; | 
| 5414 |  | //       } | 
| 5415 |  |  | 
| 5416 |  | //       nrows = Query_ROOT_TABLE(time,conn,path);// get the list of file which might contain the packet | 
| 5417 |  |  | 
| 5418 |  |  | 
| 5419 |  | //       for(int r=0; r<nrows; r++){ //loop over rows | 
| 5420 |  | //        if(path)cout << r << " >>>> "<<(path+r)->c_str() << endl; | 
| 5421 |  | //        /// verifica che il file non sia quello gia` aperto | 
| 5422 |  | //       } | 
| 5423 |  |  | 
| 5424 |  | //       //////////////////////////////////////////////////////////////////////// | 
| 5425 |  |  | 
| 5426 |  | //       TSQLResult *result = 0; | 
| 5427 |  | //       TSQLRow    *row    = 0; | 
| 5428 |  | //       // | 
| 5429 |  | //       stringstream oss; | 
| 5430 |  | //       oss.str(""); | 
| 5431 |  | //       // ---------------------------------------- | 
| 5432 |  | //       // read the id of last calibration inserted | 
| 5433 |  | //       // ---------------------------------------- | 
| 5434 |  | //       oss.str(""); | 
| 5435 |  | //       oss << " SELECT "; | 
| 5436 |  | //       oss << " (ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION) "; | 
| 5437 |  | //       oss << " ORDER BY ID DESC LIMIT 1; "; | 
| 5438 |  |  | 
| 5439 |  | //       result = conn->Query(oss.str().c_str()); | 
| 5440 |  | //       row = result->Next(); | 
| 5441 |  | //       if( !row )throw -666; | 
| 5442 |  |  | 
| 5443 |  | //       if( result )delete result; | 
| 5444 |  | //       if( row    )delete row; | 
| 5445 |  |  | 
| 5446 |  | //       UInt_t id = (UInt_t)atoll(row->GetField(0)); | 
| 5447 |  |  | 
| 5448 |  | //       // ------------------------------------- | 
| 5449 |  | //       // ...and modify it with new parameters | 
| 5450 |  | //       // ------------------------------------- | 
| 5451 |  |  | 
| 5452 |  |  | 
| 5453 |  | //   } | 
| 5454 | // | // | 
| 5455 | return(0); | return(0); | 
| 5456 | }; | }; | 
| 6082 | // 2) get the OBT of the last validated run | // 2) get the OBT of the last validated run | 
| 6083 | // -------------------------------------------------------------- | // -------------------------------------------------------------- | 
| 6084 | oss.str(""); | oss.str(""); | 
| 6085 | 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 | 
| 6086 |  | oss << " SELECT * FROM GL_RUN  WHERE VALIDATION>0 AND RUNHEADER_TIME<="<< t_start | 
| 6087 | <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; | <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; | 
| 6088 | if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str()); | if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str()); | 
| 6089 | result = conn->Query(oss.str().c_str()); | result = conn->Query(oss.str().c_str()); | 
| 6226 | if( interval >= calibtime )CHECK = true;                        //more than calibtime s => there might be a calibration | if( interval >= calibtime )CHECK = true;                        //more than calibtime s => there might be a calibration | 
| 6227 |  |  | 
| 6228 | if( !CHECK && this_run->VALIDATION ){ | if( !CHECK && this_run->VALIDATION ){ | 
| 6229 | for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true); | //      for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true); | 
| 6230 |  | for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],this_run->VALIDATION); | 
| 6231 | nseq=0; | nseq=0; | 
| 6232 | } | } | 
| 6233 |  |  | 
| 6248 | if( CHECK ){ | if( CHECK ){ | 
| 6249 | // check if calibration exists | // check if calibration exists | 
| 6250 | if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval); | if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval); | 
| 6251 | Bool_t MISSING = MissingTRK_CALIB(t1,t2); | //      Bool_t MISSING = MissingTRK_CALIB(t1,t2); | 
| 6252 | for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING); | UInt_t MISSING = MissingTRK_CALIB(t1,t2); | 
| 6253 |  | UInt_t val = 0; | 
| 6254 |  | if ( MISSING == 1 ) val = 0; | 
| 6255 |  | if ( MISSING == 0 ) val = 1; | 
| 6256 |  | if ( MISSING == 2 ) val = 2; | 
| 6257 |  | for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],val); | 
| 6258 | nseq=0; | nseq=0; | 
| 6259 | }; | }; | 
| 6260 | //-------------- | //-------------- | 
| 6284 | * @param t2 To absolute time | * @param t2 To absolute time | 
| 6285 | * @return true if there might be a missing calibration | * @return true if there might be a missing calibration | 
| 6286 | */ | */ | 
| 6287 | Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){ | //Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){ | 
| 6288 |  | UInt_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){ | 
| 6289 |  |  | 
| 6290 | GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB(); | GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB(); | 
| 6291 |  |  | 
| 6292 | // get the closest VALIDATED calibration before the run start (t2) | // get the closest calibration before the run start (t2) | 
| 6293 | if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);    //>>> missing | //  if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);        //>>> missing | 
| 6294 |  | if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(1);       //>>> missing | 
| 6295 |  |  | 
| 6296 | if ( trkcalib->TO_TIME  < t2 ) return(true);                                  //>>> missing | //  if ( trkcalib->TO_TIME  < t2 ) return(true);                                      //>>> missing | 
| 6297 |  | if ( trkcalib->TO_TIME  < t2 ) return(1);                                     //>>> missing | 
| 6298 |  |  | 
| 6299 | //============================================================== | //============================================================== | 
| 6300 | // Check is done first on the basis of time between calibration, | // Check is done first on the basis of time between calibration, | 
| 6311 | //============================================================== | //============================================================== | 
| 6312 | Bool_t DOWNLOAD = false; | Bool_t DOWNLOAD = false; | 
| 6313 | // check if the calib was skipped becouse of download .... DA FARE!! | // check if the calib was skipped becouse of download .... DA FARE!! | 
| 6314 | if(DOWNLOAD)return(false); | //    if(DOWNLOAD)return(false); | 
| 6315 |  | if(DOWNLOAD)return(0); | 
| 6316 |  |  | 
| 6317 | return(true);                                       //>>> missing | //    return(true);                                         //>>> missing | 
| 6318 |  | return(1);                                  //>>> missing | 
| 6319 |  |  | 
| 6320 | }; | }; | 
| 6321 |  |  | 
| 6325 | //============================================================== | //============================================================== | 
| 6326 | // the long time interval bewteen runs might be due to download | // the long time interval bewteen runs might be due to download | 
| 6327 | if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME); | if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME); | 
| 6328 | return(false); | if ( trkcalib->VALIDATION ) return(0); | 
| 6329 |  | if ( IsDebug() )printf("Calibration is not validated... :-/ ==> OK but with VALIDATION=2! \n"); | 
| 6330 |  | return(2); | 
| 6331 |  |  | 
| 6332 | }; | }; | 
| 6333 | /** | /** | 
| 6336 | * @param validation true/false | * @param validation true/false | 
| 6337 | */ | */ | 
| 6338 | Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){ | Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){ | 
| 6339 |  | return(this->assignVALIDATION(idrun,(UInt_t)validation)); | 
| 6340 |  | } | 
| 6341 |  |  | 
| 6342 |  | Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, UInt_t validation){ | 
| 6343 | TSQLResult *result = 0; | TSQLResult *result = 0; | 
| 6344 | stringstream oss; | stringstream oss; | 
| 6345 | oss.str(""); | oss.str(""); | 
| 6536 | stringstream myquery; | stringstream myquery; | 
| 6537 | // | // | 
| 6538 | myquery.str(""); | myquery.str(""); | 
| 6539 | myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';"; | myquery << " SELECT ID,ID_RAW,ID_TIMESYNC FROM GL_ROOT where NAME='"<<remfile.Data() <<"';"; | 
| 6540 | // | // | 
| 6541 | pResult = conn->Query(myquery.str().c_str()); | pResult = conn->Query(myquery.str().c_str()); | 
| 6542 | // | // | 
| 6552 | // | // | 
| 6553 | this->SetID_ROOT((UInt_t)atoll(Row->GetField(0))); | this->SetID_ROOT((UInt_t)atoll(Row->GetField(0))); | 
| 6554 | this->SetID_RAW((UInt_t)atoll(Row->GetField(1))); | this->SetID_RAW((UInt_t)atoll(Row->GetField(1))); | 
| 6555 |  | UInt_t idtsy=(UInt_t)atoll(Row->GetField(2)); | 
| 6556 | // | // | 
| 6557 | this->ValidationOFF(); | this->ValidationOFF(); | 
| 6558 | // | // | 
| 6560 | // | // | 
| 6561 | this->RemoveRUNS(); | this->RemoveRUNS(); | 
| 6562 | // | // | 
| 6563 | this->RemoveFILES(); | this->RemoveFILES(idtsy); | 
| 6564 | // | // | 
| 6565 | this->SetID_ROOT(0); | this->SetID_ROOT(0); | 
| 6566 | this->SetID_RAW(0); | this->SetID_RAW(0); | 
| 6780 | * Rearrange calibration tables | * Rearrange calibration tables | 
| 6781 | * | * | 
| 6782 | **/ | **/ | 
| 6783 | void PamelaDBOperations::RemoveFILES(){ | void PamelaDBOperations::RemoveFILES(UInt_t idtsy){ | 
| 6784 | stringstream myquery; | stringstream myquery; | 
| 6785 | // | // | 
| 6786 | myquery.str(""); | myquery.str(""); | 
| 6790 | // | // | 
| 6791 | conn->Query(myquery.str().c_str()); | conn->Query(myquery.str().c_str()); | 
| 6792 | // | // | 
| 6793 |  | myquery.str(""); | 
| 6794 |  | myquery << " DELETE FROM GL_ROOT WHERE ID=" <<this->GetID_ROOT() <<";"; | 
| 6795 |  | // | 
| 6796 |  | if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str()); | 
| 6797 |  | // | 
| 6798 |  | conn->Query(myquery.str().c_str());  // | 
| 6799 |  | // | 
| 6800 |  | if ( !chewbacca ){ | 
| 6801 |  | myquery.str(""); | 
| 6802 |  | myquery << " DELETE FROM GL_TIMESYNC WHERE ID=" << idtsy <<";"; | 
| 6803 |  | // | 
| 6804 |  | if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str()); | 
| 6805 |  | // | 
| 6806 |  | conn->Query(myquery.str().c_str()); | 
| 6807 |  | }; | 
| 6808 |  | // | 
| 6809 | }; | }; | 
| 6810 |  |  | 
| 6811 | /** | /** | 
| 7010 | * Rearrange calibration tables | * Rearrange calibration tables | 
| 7011 | * | * | 
| 7012 | **/ | **/ | 
| 7013 | UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){ | UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh , TFile *file){ | 
| 7014 |  |  | 
| 7015 | Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0}; | if(!caltrk) return 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; // :-) |  | 
| 7016 |  |  | 
| 7017 | //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl; | if ( IsDebug() ) cout << "ValidateTrkCalib:"<<endl; | 
| 7018 |  |  | 
| 7019 | //  ----------------------------------------------- | UInt_t validate = 1; | 
| 7020 | //  If missing packets: check the acq configuration | Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0}; | 
| 7021 | //  (some DSPs might be excluded from acquisition) | UInt_t timeaftercalib=120000; //2000; | 
| 7022 | //  ----------------------------------------------- | TString classname = caltrk->GetName(); | 
| 7023 |  |  | 
| 7024 | //  ----------------------------------------------- | //  ---------------------------------- | 
| 7025 | //  retrieve the first run header after calib | //  Check CRCs and failed calibrations | 
| 7026 | //  ----------------------------------------------- | //  ---------------------------------- | 
| 7027 | PacketType *pctp; | for(Int_t ipkt=0; ipkt<6; ipkt++){ | 
| 7028 | EventCounter *cod; | if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){ | 
| 7029 | cod = eh->GetCounter(); | if( caltrk->crc_hcal[ipkt] ){ | 
| 7030 | Int_t irun = cod->Get(pctp->RunHeader); | //              if(IsDebug())cout<<"(CRC Header)"; | 
| 7031 | TTree *rh=(TTree*)file->Get("RunHeader"); | validate = 0; | 
| 7032 | if ( !rh || rh->IsZombie() ) throw -17; | if(IsDebug())cout <<endl<<" *** CRC *** (header DSPn "<<caltrk->DSPnumber[ipkt]<<")"; | 
| 7033 | if( rh->GetEntries() == irun ){ |  | 
| 7034 | if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl; | } | 
| 7035 | return 0; // :-( | for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] ){ | 
| 7036 | } | //              if(IsDebug())cout<<"(CRC Pkt-"<<ilad<<")"; | 
| 7037 |  | if(IsDebug())cout <<endl<<" *** CRC *** (data DSPn "<<caltrk->DSPnumber[ipkt]<<" ladder "<<ilad<<")"; | 
| 7038 |  | validate = 0; | 
| 7039 |  | } | 
| 7040 |  | if( !(caltrk->ncalib_event[ipkt]==0 && caltrk->cal_flag[ipkt]==0) ){ | 
| 7041 |  | if(IsDebug())cout <<endl<<" *** FAILURE ***     (data DSPn "<<caltrk->DSPnumber[ipkt]<<")"; | 
| 7042 |  | validate = 0; | 
| 7043 |  | } | 
| 7044 |  | }else{ | 
| 7045 |  | //          validate=0; | 
| 7046 |  | if(IsDebug())cout <<endl<<" *** DSPn *** ("<< caltrk->DSPnumber[ipkt] <<" @pkt "<<ipkt<<")"; | 
| 7047 |  | } | 
| 7048 |  | } | 
| 7049 |  |  | 
| 7050 | RunHeaderEvent *run  = 0; | //  ----------------------- | 
| 7051 | EventHeader    *hrun = 0; | //  Check missing packets: | 
| 7052 | rh->SetBranchAddress("RunHeader", &run); | //  ----------------------- | 
| 7053 | rh->SetBranchAddress("Header", &hrun); | //    Readout order: | 
| 7054 | rh->GetEntry(irun); | //    ------------------ | 
| 7055 | //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl; | //    DSP   packet board | 
| 7056 |  | //    ------------------ | 
| 7057 | if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){ | //    12    0      1 | 
| 7058 | if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl; | //    10    1      1 | 
| 7059 | return 0; // :-( | //     8    2      1 | 
| 7060 | } | //     4    3      1 | 
| 7061 |  | //     6    4      1 | 
| 7062 | if( !run->RM_ACQ_AFTER_CALIB ){ | //     2    5      1 | 
| 7063 | if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0    -- cannot validate :-( "<<endl; | //    ------------------ | 
| 7064 | return 0; // :-( | //    11    0      2 | 
| 7065 | } | //     9    1      2 | 
| 7066 |  | //     7    2      2 | 
| 7067 |  | //     3    3      2 | 
| 7068 |  | //     5    4      2 | 
| 7069 |  | //     1    5      2 | 
| 7070 |  | //    ------------------ | 
| 7071 |  | //  ------------------------------------------------- | 
| 7072 |  | //  Check if it is first or second calibration packet | 
| 7073 |  | //  ------------------------------------------------- | 
| 7074 |  | UInt_t build=0; | 
| 7075 |  | UInt_t base=0; | 
| 7076 |  | UInt_t mask=0; | 
| 7077 |  | if(classname.Contains("CalibTrk1Event")){ | 
| 7078 |  | base=12; | 
| 7079 |  | mask=0x03F000; | 
| 7080 |  | } | 
| 7081 |  | if(classname.Contains("CalibTrk2Event")){ | 
| 7082 |  | base=18; | 
| 7083 |  | mask=0xFC0000; | 
| 7084 |  | } | 
| 7085 |  | //  ---------------------------------------------------- | 
| 7086 |  | //  Count number of valid packets and set build variable | 
| 7087 |  | //  ---------------------------------------------------- | 
| 7088 |  | if(IsDebug())cout <<endl<< " DSP: "; | 
| 7089 |  | Int_t  npkts=0; | 
| 7090 |  | for(Int_t ipkt=0; ipkt<6; ipkt++){ | 
| 7091 |  | if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){ | 
| 7092 |  | if(IsDebug())cout <<" "<<caltrk->DSPnumber[ipkt]; | 
| 7093 |  | npkts++; | 
| 7094 |  | build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) ); | 
| 7095 |  | //          cout << caltrk->DSPnumber[ipkt] | 
| 7096 |  | }; | 
| 7097 |  | } | 
| 7098 |  | if(IsDebug())cout << " ==> "<< hex << build << dec; | 
| 7099 |  | //  ---------------------------------------------------- | 
| 7100 |  | //  If the number of valid packets is 6, ok exit... | 
| 7101 |  | //  ---------------------------------------------------- | 
| 7102 |  | if( npkts==6 ){ | 
| 7103 |  | return validate; // exit | 
| 7104 |  | } | 
| 7105 |  | //////////////////////////////////////////////////////// | 
| 7106 |  | //  ...otherwise there might be some missing packets | 
| 7107 |  | // | 
| 7108 |  | //  In this case check the acq configuration | 
| 7109 |  | //  (some DSPs might be excluded from acquisition) | 
| 7110 |  | //////////////////////////////////////////////////////// | 
| 7111 |  |  | 
| 7112 |  | if(!eh || !file || (file&&file->IsZombie()) ){ | 
| 7113 |  | if ( IsDebug() )cout << " *** MISSING VIEW *** eh="<<eh<<" file="<<file<<" cannot validate"<<endl; | 
| 7114 |  | return (0); | 
| 7115 |  | } | 
| 7116 |  |  | 
| 7117 |  | //  ----------------------------------------------- | 
| 7118 |  | //  retrieve the first run header after calib | 
| 7119 |  | //  ----------------------------------------------- | 
| 7120 |  |  | 
| 7121 |  | PacketType *pctp; | 
| 7122 |  | EventCounter *cod; | 
| 7123 |  | cod = eh->GetCounter(); | 
| 7124 |  | Int_t irun = cod->Get(pctp->RunHeader); | 
| 7125 |  | TTree *rh=(TTree*)file->Get("RunHeader"); | 
| 7126 |  | if ( !rh || rh->IsZombie() ) throw -17; | 
| 7127 |  | if( rh->GetEntries() <= irun ){ | 
| 7128 |  | if ( IsDebug() ) cout << " *** MISSING-PKT *** no runs after calib (1) -- cannot validate :-( "<<endl; | 
| 7129 |  | return 0; // :-( | 
| 7130 |  | } | 
| 7131 |  | RunHeaderEvent *run  = 0; | 
| 7132 |  | EventHeader    *hrun = 0; | 
| 7133 |  | rh->SetBranchAddress("RunHeader", &run); | 
| 7134 |  | rh->SetBranchAddress("Header", &hrun); | 
| 7135 |  | rh->GetEntry(irun); | 
| 7136 |  | if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){ | 
| 7137 |  | if ( IsDebug() ) cout << " *** MISSING-PKT *** no runs after calib (2) -- cannot validate :-( "<<endl; | 
| 7138 |  | return 0; // :-( | 
| 7139 |  | } | 
| 7140 |  |  | 
| 7141 | UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime()); | UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime()); | 
| 7142 | if( dtime > timeaftercalib ){ | if( dtime > timeaftercalib ){ | 
| 7143 | if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl; | if ( IsDebug() ) cout << " *** MISSING-PKT *** run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl; | 
| 7144 | return 0; // :-( | return 0; // :-( | 
| 7145 | } | } | 
|  |  |  | 
| 7146 |  |  | 
| 7147 |  | if ( IsDebug() ) cout <<endl<< " ACQ_BUILD_INFO ==> "<<hex<<(run->ACQ_BUILD_INFO & mask)<<dec; | 
| 7148 |  |  | 
| 7149 |  | if( (run->ACQ_BUILD_INFO & mask) != build ){ | 
| 7150 |  | validate=0; // :-( | 
| 7151 |  | cout <<endl<< " *** MISSING-PKT *** packet mismatch: ACQ_BUILD_INFO="<<hex<<(run->ACQ_BUILD_INFO&mask)<<" != "<<build<<dec; | 
| 7152 |  | }; | 
| 7153 |  |  | 
| 7154 |  | return validate; | 
| 7155 |  |  | 
|  | if( (run->ACQ_BUILD_INFO & mask) != build ){ |  | 
|  | if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl; |  | 
|  | return 0; // :-( |  | 
|  | } |  | 
|  | return 1; // :-) |  | 
| 7156 |  |  | 
| 7157 | } | } | 
| 7158 |  |  |