/[PAMELA software]/chewbacca/YodaProfiler/src/PamelaDBOperations.cpp
ViewVC logotype

Diff of /chewbacca/YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1.1.1 by mocchiut, Tue Sep 23 07:20:33 2008 UTC revision 1.4 by pam-fi, Fri Oct 31 11:21:44 2008 UTC
# Line 490  void PamelaDBOperations::SetCommonGLRUN( Line 490  void PamelaDBOperations::SetCommonGLRUN(
490  };  };
491    
492  /**  /**
493     *
494     * Set the variables which belogns to physendrun tree
495     *
496     */
497    void PamelaDBOperations::SetPhysEndRunVariables(){
498      //
499      //
500      //
501      TTree *T = 0;
502      T = (TTree*)file->Get("PhysEndRun");
503      if ( !T || T->IsZombie() ) throw -90;
504      //
505      PhysEndRunEvent *pher= 0;
506      EventHeader *eh = 0;
507      T->SetBranchAddress("PhysEndRun", &pher);
508      T->SetBranchAddress("Header", &eh);
509      //
510      UInt_t phobt = 0;
511      UInt_t phpkt = 0;
512      //
513      glrun->SetPHYSENDRUN_MASK_S3S2S12(0);
514      glrun->SetPHYSENDRUN_MASK_S11CRC(0);
515      //
516      for (Int_t p=0; p<T->GetEntries(); p++){
517        //
518        T->GetEntry(p);
519        //
520        phobt = (UInt_t)eh->GetPscuHeader()->GetOrbitalTime();
521        phpkt = (UInt_t)eh->GetPscuHeader()->GetCounter();
522        //
523        if ( this->PKT(phpkt) >= this->PKT(glrun->GetRUNHEADER_PKT()) && this->PKT(phpkt) <= this->PKT(glrun->GetRUNTRAILER_PKT()) && this->OBT(phobt) >= this->OBT(glrun->GetRUNHEADER_OBT()) && this->OBT(phobt) <= this->OBT(glrun->GetRUNTRAILER_OBT())  ){
524          if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() || glrun->GetPHYSENDRUN_MASK_S11CRC() ){
525            if ( IsDebug() ) printf(" WARNING while looping in physendrun: found two PhysEndRun packet for the same RUN! \n");
526            if ( IsDebug() ) printf(" Actual values: %X %X New values %X %X \n ",glrun->GetPHYSENDRUN_MASK_S3S2S12(),glrun->GetPHYSENDRUN_MASK_S11CRC(),(UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S3S2S12,(UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S11CRC);
527            if ( PEDANTIC && IsDebug() ) printf(" ERROR while looping in physendrun: found two PhysEndRun packet for the same RUN!\n ");
528            if ( PEDANTIC ) throw -91;
529          } else {
530            glrun->SetPHYSENDRUN_MASK_S3S2S12((UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S3S2S12);
531            glrun->SetPHYSENDRUN_MASK_S11CRC((UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S11CRC);
532          };
533        };
534      };
535      //
536    };
537    
538    /**
539   * Patch, look for upper limits to avoid processing retransmitted data   * Patch, look for upper limits to avoid processing retransmitted data
540   */   */
541  Int_t PamelaDBOperations::SetUpperLimits(){  Int_t PamelaDBOperations::SetUpperLimits(){
# Line 739  Int_t PamelaDBOperations::SetUpperLimits Line 785  Int_t PamelaDBOperations::SetUpperLimits
785    };    };
786    //    //
787    if ( chewbacca && nevent < 1 ) {    if ( chewbacca && nevent < 1 ) {
788          pktfirst = chpktmin;      pktfirst = chpktmin;
789          upperpkt = PKT(chpktmax);      upperpkt = PKT(chpktmax);
790          pktlast = chpktmax;      pktlast = chpktmax;
791          obtfirst = chobtmin;      obtfirst = chobtmin;
792          obtlast = chobtmax;      obtlast = chobtmax;
793          upperobt = OBT(chobtmax);      upperobt = OBT(chobtmax);
794    };    };
795    //    //
796    if ( IsDebug() ) printf(" First entries are: OBT %u pkt_num %u entry %i\n",obtfirst,pktfirst,chminentry);    if ( IsDebug() ) printf(" First entries are: OBT %u pkt_num %u entry %i\n",obtfirst,pktfirst,chminentry);
# Line 752  Int_t PamelaDBOperations::SetUpperLimits Line 798  Int_t PamelaDBOperations::SetUpperLimits
798    if ( IsDebug() ) printf(" Last entries are: OBT %lld pkt_num %lld entry %i\n",upperobt,upperpkt,upperentry);    if ( IsDebug() ) printf(" Last entries are: OBT %lld pkt_num %lld entry %i\n",upperobt,upperpkt,upperentry);
799    //    //
800    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ){    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ){
801          if ( IsDebug() ) printf(" Inconsistent PKT/OBT sequence: \n     (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) %llu < %llu && %llu > %llu \n     OR \n     (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) %llu > %llu && %llu < %llu \n",PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst),PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst));      if ( IsDebug() ) printf(" Inconsistent PKT/OBT sequence: \n     (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) %llu < %llu && %llu > %llu \n     OR \n     (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) %llu > %llu && %llu < %llu \n",PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst),PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst));
802          if ( PEDANTIC ) throw -88;      if ( PEDANTIC ) throw -88;
803          return(32);      return(32);
804    };    };
805    //    //
806    if ( !nevent ) return(64);    if ( !nevent ) return(64);
# Line 1040  Int_t PamelaDBOperations::SetUpperLimits Line 1086  Int_t PamelaDBOperations::SetUpperLimits
1086          upperpkt = pkth;          upperpkt = pkth;
1087          upperobt = obth;          upperobt = obth;
1088          rhev = lasthead+1;          rhev = lasthead+1;
1089      } else {        } else {
1090          rhev = lasthead;          rhev = lasthead;
1091        };        };
1092        if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);        if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
# Line 1227  void PamelaDBOperations::LockTables(){   Line 1273  void PamelaDBOperations::LockTables(){  
1273    stringstream oss;    stringstream oss;
1274    //      //  
1275    oss.str("");    oss.str("");
1276    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;";
1277    TSQLResult *result = 0;    TSQLResult *result = 0;
1278    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1279    if ( !result ) throw -10;    if ( !result ) throw -10;
# Line 1260  void PamelaDBOperations::UnLockTables(){ Line 1306  void PamelaDBOperations::UnLockTables(){
1306   */   */
1307  Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){    Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){  
1308    //    //
1309  //  if ( IsDebug() ) printf(" pkt conversion: pkt_num is %u pktfirst is %u  (UInt_t)(16777214/2)) is %u  \n",pkt_num,ppktfirst,(UInt_t)(16777214/2));    //  if ( IsDebug() ) printf(" pkt conversion: pkt_num is %u pktfirst is %u  (UInt_t)(16777214/2)) is %u  \n",pkt_num,ppktfirst,(UInt_t)(16777214/2));
1310    //    //
1311    if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2)  ){    if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2)  ){
1312  //    if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL);      //    if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL);
1313      return((Long64_t)pkt_num+16777215LL);      return((Long64_t)pkt_num+16777215LL);
1314    };    };
1315    //    //
1316    if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){    if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){
1317  //    if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL);      //    if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL);
1318      return((Long64_t)pkt_num-16777215LL);      return((Long64_t)pkt_num-16777215LL);
1319    };    };
1320    //    //
1321  //  if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);    //  if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
1322    return((Long64_t)pkt_num);    return((Long64_t)pkt_num);
1323    //    //
1324  };  };
# Line 1282  Long64_t PamelaDBOperations::PKT(UInt_t Line 1328  Long64_t PamelaDBOperations::PKT(UInt_t
1328   */   */
1329  Long64_t PamelaDBOperations::OBT(UInt_t obt){    Long64_t PamelaDBOperations::OBT(UInt_t obt){  
1330    //    //
1331  //  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));
1332    //    //
1333    if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){    if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){
1334  //    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());
1335      return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());      return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());
1336    };    };
1337    //    //
1338    if ( obt > ((Long64_t)pobtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){    if ( obt > ((Long64_t)pobtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
1339  //    if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2));      //    if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2));
1340  //    if ( IsDebug() ) printf(" rise down pktnum %lld \n", (Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());      //    if ( IsDebug() ) printf(" rise down pktnum %lld \n", (Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
1341      return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());      return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
1342    };    };
1343    //    //
1344  //  if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);    //  if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);
1345    return((Long64_t)obt);    return((Long64_t)obt);
1346  };  };
1347    
# Line 1331  void PamelaDBOperations::FillClass(Bool_ Line 1377  void PamelaDBOperations::FillClass(Bool_
1377    UInt_t lastPkt = 0;    UInt_t lastPkt = 0;
1378    UInt_t rhtime = 0;    UInt_t rhtime = 0;
1379    UInt_t rttime = 0;    UInt_t rttime = 0;
1380      //
1381      if ( IsDebug() ) printf(" A firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1382      //
1383    if ( !mishead ){    if ( !mishead ){
1384      codh = ehh->GetCounter();      codh = ehh->GetCounter();
1385      if ( lastev+1 == firstev || (lastev == firstev && lastev != -1) ){            if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){      
1386          if ( IsDebug() ) printf(" B firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1387        firstev = 1;        firstev = 1;
1388        lastev = 0;        lastev = 0;
1389      } else {      } else {
# Line 1346  void PamelaDBOperations::FillClass(Bool_ Line 1396  void PamelaDBOperations::FillClass(Bool_
1396    };    };
1397    if ( !mistrail ){    if ( !mistrail ){
1398      codt = eht->GetCounter();      codt = eht->GetCounter();
1399      if ( lastev+1 == firstev || (lastev == firstev && lastev != -1)){            if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1)){      
1400          if ( IsDebug() ) printf(" C firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1401        lastev = 0;        lastev = 0;
1402        firstev = lastev+1;        firstev = lastev+1;
1403      } else {      } else {
# Line 1363  void PamelaDBOperations::FillClass(Bool_ Line 1414  void PamelaDBOperations::FillClass(Bool_
1414    if ( mishead ) {    if ( mishead ) {
1415      glrun->Set_GL_RUNH0();      glrun->Set_GL_RUNH0();
1416      //      //
1417      if ( lastev+1 == firstev || (lastev == firstev && lastev != -1) ){            if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){      
1418        firstObt = lastObt;        firstObt = lastObt;
1419        firstPkt = lastPkt;        firstPkt = lastPkt;
1420        rhtime = rttime;        rhtime = rttime;
# Line 1382  void PamelaDBOperations::FillClass(Bool_ Line 1433  void PamelaDBOperations::FillClass(Bool_
1433    if ( mistrail ){    if ( mistrail ){
1434      glrun->Set_GL_RUNT0();      glrun->Set_GL_RUNT0();
1435      //      //
1436      if ( lastev+1 == firstev || (lastev == firstev && lastev != -1) ){            if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){      
1437        lastObt = firstObt;        lastObt = firstObt;
1438        lastPkt = firstPkt;        lastPkt = firstPkt;
1439        rttime = rhtime;        rttime = rhtime;
# Line 1405  void PamelaDBOperations::FillClass(Bool_ Line 1456  void PamelaDBOperations::FillClass(Bool_
1456    if ( IsDebug() ) printf(" firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);    if ( IsDebug() ) printf(" firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1457    //    //
1458    this->SetCommonGLRUN(rhtime,rttime);    this->SetCommonGLRUN(rhtime,rttime);
1459      this->SetPhysEndRunVariables();
1460    //    //
1461  };  };
1462    
# Line 1412  void PamelaDBOperations::FillClass(Bool_ Line 1464  void PamelaDBOperations::FillClass(Bool_
1464  // PUBLIC FUNCTIONS  // PUBLIC FUNCTIONS
1465  //  //
1466    
1467    // /**
1468    //  * Insert a new row into GL_RAW table.
1469    //  */
1470    // Int_t PamelaDBOperations::insertPamelaRawFile(){
1471    //   //
1472    //   stringstream oss;
1473    //   //
1474    //   Bool_t idr =  this->SetID_RAW();
1475    //   if ( idr ) return(1);
1476    //   //
1477    //   oss.str("");
1478    //   if ( STATIC ){
1479    //     oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
1480    //      << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";
1481    //   } else {
1482    //     oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')";
1483    //   };
1484    //   if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1485    //   //
1486    //   idr =  this->SetID_RAW();
1487    //   if ( !idr ) throw -11;
1488    //   //
1489    //   return(0);
1490    // }
1491  /**  /**
1492   * Insert a new row into GL_RAW table.   * Insert a new row into GL_RAW table.
1493   */   */
1494  Int_t PamelaDBOperations::insertPamelaRawFile(){  Int_t PamelaDBOperations::insertPamelaRawFile(){
1495    
1496        //
1497        Bool_t idr =  this->SetID_RAW();
1498        if ( idr ) return(1);
1499    
1500        GL_RAW glraw = GL_RAW();
1501        
1502        glraw.PATH = GetRawPath();
1503        glraw.NAME = GetRawFile();
1504        glraw.BOOT_NUMBER = 0;//???
1505        
1506        if( insertPamelaRawFile(&glraw) )return(1);
1507        //
1508        idr =  this->SetID_RAW();
1509        if ( !idr ) throw -11;
1510        
1511        return(0);
1512    }
1513    /**
1514     * Insert a new row into GL_RAW table.
1515     */
1516    Int_t PamelaDBOperations::insertPamelaRawFile(GL_RAW *glraw){
1517    //    //
1518    stringstream oss;      if(!glraw)return(1);//??
1519    //      //
1520    Bool_t idr =  this->SetID_RAW();      stringstream oss;
1521    if ( idr ) return(1);      //
1522    //      oss.str("");
1523    oss.str("");      if ( STATIC ){
1524    if ( STATIC ){          oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
1525      oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"              << glraw->PATH << "', '" << glraw->NAME << "')";
1526          << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";      } else {
1527    } else {          oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << glraw->NAME << "')";
1528      oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')";      };
1529    };      if ( debug ) cout <<oss.str().c_str() <<endl;
1530    if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;      if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1531    //      //
1532    idr =  this->SetID_RAW();      oss.str("");
1533    if ( !idr ) throw -11;      oss << "SELECT ID FROM GL_RAW WHERE NAME=\""<<glraw->NAME<<"\";";
1534    //      if ( debug ) cout << oss.str().c_str()<<endl;
1535    return(0);      if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1536        //
1537        TSQLResult *result = 0;
1538        TSQLRow    *row    = 0;
1539        result = conn->Query(oss.str().c_str());
1540        if ( result == NULL ) throw -4;
1541        row = result->Next();
1542        if ( !row ) return(1);
1543        glraw->ID = (UInt_t)atoll(row->GetField(0));
1544        if ( debug ) printf(" The ID of the RAW file is %u \n",glraw->ID);
1545        delete result;
1546        delete row;
1547        //
1548        return(0);
1549  }  }
1550    
1551    
# Line 1741  Int_t PamelaDBOperations::insertPamelaGL Line 1852  Int_t PamelaDBOperations::insertPamelaGL
1852    return(signal);    return(signal);
1853  }  }
1854    
1855    // /**
1856    //  * Insert all the new rows into GL_ROOT.
1857    //  * The raw file indicates in the parameters should be already been stored in the database.
1858    //  */
1859    // Int_t PamelaDBOperations::insertPamelaRootFile(){
1860    //   stringstream oss;
1861    //   TSQLResult *result = 0;
1862    //   TSQLRow    *row    = 0;
1863    //   UInt_t idtimesync = 0;
1864    //   //
1865    //   //
1866    //   if ( chewbacca ){
1867    //     oss.str("");
1868    //     oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";
1869    //     if ( debug ) printf(" query is %s \n",oss.str().c_str());
1870    //     result = conn->Query(oss.str().c_str());
1871    //     //
1872    //     if ( !result ) throw -3;
1873    //     //
1874    //     row = result->Next();
1875    //     //
1876    //     if ( !row ) throw -3;
1877    //     idtimesync = (UInt_t)atoll(row->GetField(0));
1878    //   } else {
1879    //     oss.str("");
1880    //     if ( STATIC ){
1881    //       oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1882    //        << " LEFT JOIN GL_ROOT "
1883    //        << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1884    //        << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1885    //        << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1886    //     } else {
1887    //       oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1888    //        << " LEFT JOIN GL_ROOT "
1889    //        << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1890    //        << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "
1891    //        << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1892    //     };
1893    //     result = conn->Query(oss.str().c_str());
1894    //     //
1895    //     if ( !result ) throw -12;
1896    //     //
1897    //     row = result->Next();
1898    //     //
1899    //     if ( !row ) throw -10;
1900    //     if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){
1901    //       idroot = (UInt_t)atoll(row->GetField(2));
1902    //       return(1);
1903    //     };
1904    //     //
1905    //     // determine which timesync has to be used
1906    //     //
1907    //     oss.str("");
1908    //     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;";
1909    //     result = conn->Query(oss.str().c_str());
1910    //     //
1911    //     if ( !result ) throw -3;
1912    //     //
1913    //     row = result->Next();
1914    //     //
1915    //     if ( !row ) throw -3;
1916    //     idtimesync = (UInt_t)atoll(row->GetField(0));
1917    //   };
1918    //   //
1919    //   oss.str("");
1920    //   if ( STATIC ){
1921    //     oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1922    //      << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";
1923    //   } else {
1924    //     oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1925    //      << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')";
1926    //   };
1927    //   //
1928    //   if ( debug ) printf(" query is %s \n",oss.str().c_str());
1929    //   if (conn->Query(oss.str().c_str()) == 0) throw -4;
1930    //   //
1931    //   delete result;
1932    //   //
1933    //   oss.str("");
1934    //   //  oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";
1935    //   if ( STATIC ){
1936    //     oss << "SELECT ID FROM GL_ROOT WHERE PATH='" << this->GetRootPath().Data() << "' and NAME='"<< this->GetRootFile().Data() <<"';";
1937    //   } else {
1938    //     oss << "SELECT ID FROM GL_ROOT WHERE PATH='$PAM_L0' and NAME='"<< this->GetRootFile().Data() <<"';";
1939    //   };
1940    //   //
1941    //   if ( debug ) printf(" query is %s \n",oss.str().c_str());
1942    //   result = conn->Query(oss.str().c_str());
1943    //   if ( !result ) throw -12;
1944    //   row = result->Next();
1945    //   if ( !row ) throw -3;
1946    //   this->SetID_ROOT((UInt_t)atoll(row->GetField(0)));
1947    //   if ( debug ) printf(" The ID of the ROOT file is %u \n",this->GetID_ROOT());
1948    //   //
1949    //   delete result;
1950    //   //
1951    //   return(0);
1952    // }
1953  /**  /**
1954   * Insert all the new rows into GL_ROOT.   * Insert all the new rows into GL_ROOT.
1955   * 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.
1956   */   */
1957  Int_t PamelaDBOperations::insertPamelaRootFile(){  Int_t PamelaDBOperations::insertPamelaRootFile(){
1958    
1959        stringstream oss;
1960        TSQLResult *result = 0;
1961        TSQLRow    *row    = 0;
1962      //
1963        // ----------------------
1964        // determine the timesync
1965        // ----------------------
1966        UInt_t idtimesync = 0;
1967        //
1968        if ( chewbacca ){
1969            oss.str("");
1970            oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";
1971            if ( debug ) printf(" %s \n",oss.str().c_str());
1972            result = conn->Query(oss.str().c_str());
1973            //
1974            if ( !result ) throw -3;
1975            //
1976            row = result->Next();
1977            //
1978            if ( !row ) throw -3;
1979            idtimesync = (UInt_t)atoll(row->GetField(0));
1980        } else {
1981            oss.str("");
1982            if ( STATIC ){
1983                oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1984                    << " LEFT JOIN GL_ROOT "
1985                    << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1986                    << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1987                    << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1988            } else {
1989                oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1990                    << " LEFT JOIN GL_ROOT "
1991                    << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1992                    << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "
1993                    << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1994            };
1995            result = conn->Query(oss.str().c_str());
1996            //
1997            if ( !result ) throw -12;
1998            //
1999            row = result->Next();
2000            //
2001            if ( !row ) throw -10;
2002            if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){
2003                idroot = (UInt_t)atoll(row->GetField(2));
2004                delete row;
2005                delete result;
2006                return(1);
2007            };
2008            //
2009            // determine which timesync has to be used
2010            //
2011            oss.str("");
2012            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;";
2013            result = conn->Query(oss.str().c_str());
2014            //
2015            if ( !result ) throw -3;
2016            //
2017            row = result->Next();
2018            //
2019            if ( !row ) throw -3;
2020            idtimesync = (UInt_t)atoll(row->GetField(0));
2021        };
2022        
2023        delete row;
2024        delete result;
2025    
2026        // ----------------------
2027        // insert root file
2028        // ----------------------
2029    
2030        GL_ROOT glroot = GL_ROOT();
2031        
2032        glroot.ID_RAW = GetID_RAW();
2033        glroot.ID_TIMESYNC = idtimesync;
2034        glroot.PATH = GetRootPath();
2035        glroot.NAME = GetRootFile();
2036    
2037        if ( insertPamelaRootFile(&glroot) )return 1;
2038    
2039        SetID_ROOT(glroot.ID);
2040        
2041    
2042        return (0);
2043    }
2044    /**
2045     * Insert all the new rows into GL_ROOT.
2046     * The raw file indicates in the parameters should be already been stored in the database.
2047     */
2048    Int_t PamelaDBOperations::insertPamelaRootFile(GL_ROOT *glroot){
2049    
2050    
2051    
2052    stringstream oss;    stringstream oss;
2053    TSQLResult *result = 0;    TSQLResult *result = 0;
2054    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
   UInt_t idtimesync = 0;  
2055    //    //
2056    //    //
   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;  
     //  
     row = result->Next();  
     //  
     if ( !row ) throw -3;  
     idtimesync = (UInt_t)atoll(row->GetField(0));  
   } else {  
     oss.str("");  
     if ( STATIC ){  
       oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "  
           << " LEFT JOIN GL_ROOT "  
           << " ON GL_RAW.ID = GL_ROOT.ID_RAW "  
           << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "  
           << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";  
     } else {  
       oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "  
           << " LEFT JOIN GL_ROOT "  
           << " ON GL_RAW.ID = GL_ROOT.ID_RAW "  
           << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "  
           << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";  
     };  
     result = conn->Query(oss.str().c_str());  
     //  
     if ( !result ) throw -12;  
     //  
     row = result->Next();  
     //  
     if ( !row ) throw -10;  
     if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){  
       idroot = (UInt_t)atoll(row->GetField(2));  
       return(1);  
     };  
     //  
     // determine which timesync has to be used  
     //  
     oss.str("");  
     oss << "SELECT GL_TIMESYNC.ID FROM GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW ORDER BY GL_TIMESYNC.ID DESC LIMIT 1;";  
     result = conn->Query(oss.str().c_str());  
     //  
     if ( !result ) throw -3;  
     //  
     row = result->Next();  
     //  
     if ( !row ) throw -3;  
     idtimesync = (UInt_t)atoll(row->GetField(0));  
   };  
   //  
2057    oss.str("");    oss.str("");
2058    if ( STATIC ){    if ( STATIC ){
2059      oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"      oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
2060          << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";          << glroot->ID_RAW << "', '" << glroot->ID_TIMESYNC << "', '" << glroot->PATH << "', '" << glroot->NAME << "')";
2061    } else {    } else {
2062      oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"      oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
2063          << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')";          << glroot->ID_RAW << "', '" << glroot->ID_TIMESYNC << "', '$PAM_L0', '" << glroot->NAME << "')";
2064    };    };
2065    //    //
2066    if ( debug ) printf(" query is %s \n",oss.str().c_str());    if ( debug ) printf("%s \n",oss.str().c_str());
2067    if (conn->Query(oss.str().c_str()) == 0) throw -4;    if (conn->Query(oss.str().c_str()) == 0) throw -4;
2068    //    //
2069    delete result;    delete result;
# Line 1822  Int_t PamelaDBOperations::insertPamelaRo Line 2071  Int_t PamelaDBOperations::insertPamelaRo
2071    oss.str("");    oss.str("");
2072    //  oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";    //  oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";
2073    if ( STATIC ){    if ( STATIC ){
2074      oss << "SELECT ID FROM GL_ROOT WHERE PATH='" << this->GetRootPath().Data() << "' and NAME='"<< this->GetRootFile().Data() <<"';";      oss << "SELECT ID FROM GL_ROOT WHERE PATH='" << glroot->PATH << "' and NAME='"<< glroot->NAME <<"';";
2075    } else {    } else {
2076      oss << "SELECT ID FROM GL_ROOT WHERE PATH='$PAM_L0' and NAME='"<< this->GetRootFile().Data() <<"';";      oss << "SELECT ID FROM GL_ROOT WHERE PATH='$PAM_L0' and NAME='"<< glroot->NAME <<"';";
2077    };    };
2078    //    //
2079    if ( debug ) printf(" query is %s \n",oss.str().c_str());    if ( debug ) printf("%s \n",oss.str().c_str());
2080    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
2081    if ( !result ) throw -12;    if ( !result ) throw -12;
2082    row = result->Next();    row = result->Next();
2083    if ( !row ) throw -3;    if ( !row ) throw -3;
2084    this->SetID_ROOT((UInt_t)atoll(row->GetField(0)));  
2085    if ( debug ) printf(" The ID of the ROOT file is %u \n",this->GetID_ROOT());    glroot->ID = (UInt_t)atoll(row->GetField(0));
2086    
2087      if ( debug ) printf(" The ID of the ROOT file is %u \n",glroot->ID);
2088    //    //
2089    delete result;    delete result;
2090    //    //
# Line 2061  Int_t PamelaDBOperations::insertPamelaRU Line 2312  Int_t PamelaDBOperations::insertPamelaRU
2312            cod = ehh->GetCounter();            cod = ehh->GetCounter();
2313            tcod = (UInt_t)cod->Get(pctp->Physics);            tcod = (UInt_t)cod->Get(pctp->Physics);
2314            evbefh = TMath::Max(chminentry,tcod);            evbefh = TMath::Max(chminentry,tcod);
2315  //        if ( (UInt_t)evbefh == upperentry ) evbefh = upperentry + 1;  // this does not work due to the Counter bug in chewbacca            //      if ( (UInt_t)evbefh == upperentry ) evbefh = upperentry + 1;  // this does not work due to the Counter bug in chewbacca
2316            if ( (UInt_t)evbefh == upperentry || !upperentry ) evbefh = upperentry + 1;            if ( (UInt_t)evbefh == upperentry || !upperentry ) evbefh = upperentry + 1;
2317  //            //
2318          if ( debug ) printf(" evbefh %i upperentry %u \n",evbefh,upperentry);            if ( debug ) printf(" evbefh %i upperentry %u \n",evbefh,upperentry);
2319            //            //
2320            this->HandleRunFragments(false,true,evbefh,upperentry);            this->HandleRunFragments(false,true,evbefh,upperentry);
2321            //            //
# Line 2367  Bool_t PamelaDBOperations::IsRunAlreadyI Line 2618  Bool_t PamelaDBOperations::IsRunAlreadyI
2618        //        //
2619        if ( IsDebug() ) printf(" The new run has more events than the old one \n");        if ( IsDebug() ) printf(" The new run has more events than the old one \n");
2620        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2621  //       oss.str("");        //       oss.str("");
2622  //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2623  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());              //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
2624  //       conn->Query(oss.str().c_str());        //       conn->Query(oss.str().c_str());
2625        if ( signal ) signal = false;        if ( signal ) signal = false;
2626        goto gonext;              goto gonext;      
2627        //        //
# Line 2385  Bool_t PamelaDBOperations::IsRunAlreadyI Line 2636  Bool_t PamelaDBOperations::IsRunAlreadyI
2636        if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");        if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");
2637        //        //
2638        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2639  //       oss.str("");        //       oss.str("");
2640  //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2641  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());              //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
2642  //       conn->Query(oss.str().c_str());        //       conn->Query(oss.str().c_str());
2643        //        //
2644        if ( signal ) signal = false;        if ( signal ) signal = false;
2645        goto gonext;        goto gonext;
# Line 2403  Bool_t PamelaDBOperations::IsRunAlreadyI Line 2654  Bool_t PamelaDBOperations::IsRunAlreadyI
2654        if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");        if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");
2655        //        //
2656        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2657  //       oss.str("");        //       oss.str("");
2658  //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2659  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());              //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
2660  //       conn->Query(oss.str().c_str());        //       conn->Query(oss.str().c_str());
2661        if ( signal ) signal = false;        if ( signal ) signal = false;
2662        //        //
2663      };      };
# Line 2488  void PamelaDBOperations::HandleRunFragme Line 2739  void PamelaDBOperations::HandleRunFragme
2739      if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");      if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
2740      //      //
2741    } else {    } else {
   //  
   // we have now the good first piece of a run, fill the glrun object  
   //  
   if ( rhfirstev != firstev && !mishead ) mishead = true;  
   if ( rtlastev != lastev && !mistrail ) mistrail = true;  
   //  
   this->FillClass(mishead,mistrail,firstev,lastev);  
   //  
   if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");  
   if ( IsDebug() ) printf(" C THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());  
   //  
   // First of all insert the run in the fragment table...  
   //  
   oss.str("");  
   oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "  
       << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("  
       << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "  
       << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("  
       << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "  
       << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
       << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "  
       << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "  
       << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "  
       << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("  
       << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "  
       << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
       << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "  
       << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";  
   //  
   if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());  
   result = conn->Query(oss.str().c_str());  
   //  
   if ( !result ) throw -4;  
   //  
   row = result->Next();  
   //  
   if ( !row ){  
2742      //      //
2743      // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)      // we have now the good first piece of a run, fill the glrun object
2744      //      //
2745      if ( IsDebug() ) printf(" The run is new \n");      if ( rhfirstev != firstev && !mishead ) mishead = true;
2746      if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");      if ( rtlastev != lastev && !mistrail ) mistrail = true;
2747      //      //
2748      glrun->SetID(this->AssignRunID());      this->FillClass(mishead,mistrail,firstev,lastev);
     glrun->SetID_RUN_FRAG(0);  
     glrun->Fill_GL_RUN_FRAGMENTS(conn);  
2749      //      //
2750    } else {      if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
2751      if ( IsDebug() ) printf(" The run is already present in the fragment table \n");      if ( IsDebug() ) printf(" C THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());
2752      if ( PEDANTIC ) throw -69;      //
2753      return;      // First of all insert the run in the fragment table...
   };  
   //  
   if ( chewbacca && mishead && mistrail ) goto justcheck;  
   //  
   // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?  
   //      
   if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is  
                                            // missing it no way we can found a piece in the frag table  
2754      //      //
2755      oss.str("");      oss.str("");
2756      oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
2757          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2758          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "          << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2759          << " ID != " << glrun->ID          << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2760          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2761            << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2762            << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2763            << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2764            << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2765            << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2766            << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2767            << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2768            << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2769            << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2770      //      //
2771      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
2772      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
2773      //      //
2774      if ( !result ) throw -4;      if ( !result ) throw -4;
2775      //      //
2776      row = result->Next();      row = result->Next();
2777      //      //
2778      if ( !row && NoFrag() ){      if ( !row ){
2779          //
2780          // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
2781          //
2782          if ( IsDebug() ) printf(" The run is new \n");
2783          if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
2784          //
2785          glrun->SetID(this->AssignRunID());
2786          glrun->SetID_RUN_FRAG(0);
2787          glrun->Fill_GL_RUN_FRAGMENTS(conn);
2788          //
2789        } else {
2790          if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
2791          if ( PEDANTIC ) throw -69;
2792          return;
2793        };
2794        //
2795        if ( chewbacca && mishead && mistrail ) goto justcheck;
2796        //
2797        // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
2798        //    
2799        if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
2800          // missing it no way we can found a piece in the frag table
2801        //        //
2802        oss.str("");        oss.str("");
2803        oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "        oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
2804            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2805            << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "            << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2806            << " ID != " << glrun->ID            << " ID != " << glrun->ID
2807            << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!            << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
2808        //        //
2809        if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());        if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
2810        result = conn->Query(oss.str().c_str());        result = conn->Query(oss.str().c_str());
2811        //        //
2812        if ( !result ) throw -4;        if ( !result ) throw -4;
2813        //        //
       foundinrun = true;  
       //  
2814        row = result->Next();        row = result->Next();
2815        //        //
2816      };        if ( !row && NoFrag() ){
     //  
     if ( !row ){  
       if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");  
       found = false;  
     } else {  
       //  
       found = false; // default value  
       //  
       if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");  
       //  
       // if we have both runheader and runtrailer we can check with pkt_counter:  
       //  
       if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){  
         ULong64_t chkpkt = 0;    
         ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());  
         ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));  
2817          //          //
2818          chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;            oss.str("");
2819            oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
2820                << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2821                << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2822                << " ID != " << glrun->ID
2823                << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
2824          //          //
2825          if ( labs(chkpkt-pktt)<2 ){          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2826            //          result = conn->Query(oss.str().c_str());
           if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);  
           //  
           found = true;  
           //  
         } else {  
           //  
           if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);        
           //  
           found = false;  
           //  
         };  
       };  
       if ( !found && chewbacca ) goto justcheck;  
       if ( !found ){  
2827          //          //
2828          // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts          if ( !result ) throw -4;
2829          //          //
2830          ULong64_t chkpkt1 = 0;          foundinrun = true;
         ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());  
         ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));  
         chkpkt1 = labs(orunh1-dbrunt1);  
         //  
         ULong64_t chkpkt2 = 0;  
         ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());  
         ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));  
         chkpkt2 = labs(orunh2-dbrunt2);  
         //  
         ULong64_t chkpkt3 = 0;  
         ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());  
         ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));  
         chkpkt3 = labs(orunh3-dbrunt3);  
2831          //          //
2832          if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){          row = result->Next();
2833            //    if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){          //
2834            //        };
2835            if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);        //
2836            //        if ( !row ){
2837            found = true;          if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2838            //          found = false;
2839          } else {        } else {
2840            //
2841            found = false; // default value
2842            //
2843            if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
2844            //
2845            // if we have both runheader and runtrailer we can check with pkt_counter:
2846            //
2847            if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
2848              ULong64_t chkpkt = 0;  
2849              ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
2850              ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
2851            //            //
2852            if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);            chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;  
2853            //            //
2854            found = false;            if ( labs(chkpkt-pktt)<2 ){
2855                //
2856                if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2857                //
2858                found = true;
2859                //
2860              } else {
2861                //
2862                if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);            
2863                //
2864                found = false;
2865                //
2866              };
2867            };
2868            if ( !found && chewbacca ) goto justcheck;
2869            if ( !found ){
2870              //
2871              // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts
2872              //
2873              ULong64_t chkpkt1 = 0;
2874              ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2875              ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2876              chkpkt1 = labs(orunh1-dbrunt1);
2877            //            //
2878              ULong64_t chkpkt2 = 0;
2879              ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
2880              ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2881              chkpkt2 = labs(orunh2-dbrunt2);
2882              //
2883              ULong64_t chkpkt3 = 0;
2884              ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
2885              ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2886              chkpkt3 = labs(orunh3-dbrunt3);
2887              //
2888              if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2889                //  if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
2890                //
2891                if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2892                //
2893                found = true;
2894                //
2895              } else {
2896                //
2897                if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2898                //
2899                found = false;
2900                //
2901              };
2902          };          };
2903        };        };
     };  
     //  
     if ( found ){  
       //  
       // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table  
       //  
       if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");  
       //  
       if ( foundinrun ){  
         glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");  
         glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");  
       };  
2904        //        //
2905        GL_RUN *glrun1 = new GL_RUN();        if ( found ){
2906        //          //
2907        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));          // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table
2908        //          //
2909        oss.str("");          if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2910        oss << " ID="<<row->GetField(0)<<";";          //
2911        //          if ( foundinrun ){
2912        glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2913        //            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2914        // merge infos          };
2915        //          //
2916        UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());          GL_RUN *glrun1 = new GL_RUN();
       ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());  
       UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());  
       ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());  
       if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);  
       TTree *T= 0;  
       T = (TTree*)file->Get("Physics");  
       if ( !T || T->IsZombie() ) throw -16;  
       EventHeader *eh = 0;  
       PscuHeader *ph = 0;  
       T->SetBranchAddress("Header", &eh);  
       while ( apkt > bpkt && aobt > bobt && firstev < lastev ){  
         T->GetEntry(firstev);  
         ph = eh->GetPscuHeader();  
         bpkt = PKT(ph->GetCounter());  
         bobt = OBT(ph->GetOrbitalTime());        
         firstev++;  
         if ( PEDANTIC ) throw -71;  
       };  
       if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);  
       //  
       glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());  
       glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());  
       glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());  
       glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());  
       glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());  
       //  
       glrun->SetEV_FROM(firstev);  
       glrun->SetNEVENTS(lastev-firstev+1);  
       //  
       glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());  
       glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());  
       glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());  
       glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());  
       glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());  
       glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());  
       glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());  
       glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());  
       glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());  
       glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());  
       glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());    
       glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());    
       glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());    
       glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());    
       glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());    
       glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());    
       //  
       if ( !IsRunAlreadyInserted() ){  
2917          //          //
2918          //      glrun->SetID(this->AssignRunID());          //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
         glrun->SetID_RUN_FRAG(glrun1->GetID());  
         glrun->Fill_GL_RUN(conn);  
2919          //          //
2920          // set id number          oss.str("");
2921            oss << " ID="<<row->GetField(0)<<";";
2922            //
2923            glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
2924            //
2925            // merge infos
2926            //
2927            UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
2928            ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
2929            UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
2930            ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
2931            if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2932            TTree *T= 0;
2933            T = (TTree*)file->Get("Physics");
2934            if ( !T || T->IsZombie() ) throw -16;
2935            EventHeader *eh = 0;
2936            PscuHeader *ph = 0;
2937            T->SetBranchAddress("Header", &eh);
2938            while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
2939              T->GetEntry(firstev);
2940              ph = eh->GetPscuHeader();
2941              bpkt = PKT(ph->GetCounter());
2942              bobt = OBT(ph->GetOrbitalTime());    
2943              firstev++;
2944              if ( PEDANTIC ) throw -71;
2945            };
2946            if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2947            //
2948            glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2949            glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
2950            glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
2951            glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
2952            glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
2953            //
2954            glrun->SetEV_FROM(firstev);
2955            glrun->SetNEVENTS(lastev-firstev+1);
2956            //
2957            glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
2958            glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
2959            glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
2960            glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
2961            glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
2962            glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
2963            glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
2964            glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
2965            glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
2966            glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
2967            glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());  
2968            glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());  
2969            glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());  
2970            glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());  
2971            glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());  
2972            glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());  
2973            //
2974            if ( glrun1->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glrun1->GetPHYSENDRUN_MASK_S3S2S12());
2975            if ( glrun1->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glrun1->GetPHYSENDRUN_MASK_S11CRC());
2976            //
2977            if ( !IsRunAlreadyInserted() ){
2978              //
2979              //    glrun->SetID(this->AssignRunID());
2980              glrun->SetID_RUN_FRAG(glrun1->GetID());
2981              glrun->Fill_GL_RUN(conn);
2982              //
2983              // set id number
2984              //
2985              glrun1->SetID_RUN_FRAG(glrun->GetID());
2986              glrun1->Fill_GL_RUN(conn);
2987              //
2988            };
2989            // delete old entry in fragment table
2990            //
2991            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2992            glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2993          //          //
2994          glrun1->SetID_RUN_FRAG(glrun->GetID());          delete glrun1;
2995          glrun1->Fill_GL_RUN(conn);          //
2996            //
2997            return;
2998          //          //
2999        };        };
       // delete old entry in fragment table  
       //  
       glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");  
       glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");  
       //  
       delete glrun1;  
       //  
       //  
       return;  
3000        //        //
3001      };      };
3002      //      //
3003    };      if ( mistrail && ( rtlastev == lastev || chewbacca )) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
3004    //        // missing it no way we can found a piece in the frag table
   if ( mistrail && ( rtlastev == lastev || chewbacca )) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is  
     // missing it no way we can found a piece in the frag table  
     //  
     oss.str("");  
     oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "  
         << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "  
         << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "  
         << " ID != " << glrun->ID  
         << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";  
     //  
     if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());  
     result = conn->Query(oss.str().c_str());  
     //  
     if ( !result ) throw -4;  
     //  
     row = result->Next();  
     //  
     if ( !row && NoFrag() ){  
3005        //        //
3006        oss.str("");        oss.str("");
3007        oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "        oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "
3008            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3009            << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "            << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
3010            << " ID != " << glrun->ID            << " ID != " << glrun->ID
3011            << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";            << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
3012        //        //
3013        if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());        if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
3014        result = conn->Query(oss.str().c_str());        result = conn->Query(oss.str().c_str());
3015        //        //
3016        if ( !result ) throw -4;        if ( !result ) throw -4;
3017        //        //
       foundinrun = true;  
3018        row = result->Next();        row = result->Next();
3019        //        //
3020      };        if ( !row && NoFrag() ){
3021      //          //
3022      if ( !row ){          oss.str("");
3023        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");          oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
3024        found = false;              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3025      } else {              << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
3026        //              << " ID != " << glrun->ID
3027        found = false; // default value              << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
3028        //          //
3029        if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
3030        //          result = conn->Query(oss.str().c_str());
3031        // if we have both runheader and runtrailer we can check with pkt_counter:          //
3032            if ( !result ) throw -4;
3033            //
3034            foundinrun = true;
3035            row = result->Next();
3036            //
3037          };
3038        //        //
3039        if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){        if ( !row ){
3040          ULong64_t chkpkt = 0;            if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
3041          ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));          found = false;
3042          ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());        } else {
3043            //
3044            found = false; // default value
3045          //          //
3046          chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;            if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
3047          //          //
3048          if ( labs(chkpkt-pktt)<2 ){          // if we have both runheader and runtrailer we can check with pkt_counter:
3049            //
3050            if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){
3051              ULong64_t chkpkt = 0;  
3052              ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
3053              ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
3054            //            //
3055            if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);            chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;  
3056            //            //
3057            found = true;            if ( labs(chkpkt-pktt)<2 ){
3058                //
3059                if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
3060                //
3061                found = true;
3062                //
3063              } else {
3064                //
3065                if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);            
3066                //
3067                found = false;
3068                //
3069              };
3070            };
3071            if ( !found && chewbacca ) goto justcheck;
3072            if ( !found ){
3073            //            //
3074          } else {            // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts
3075            //            //
3076            if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);                  ULong64_t chkpkt1 = 0;
3077              ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
3078              ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
3079              chkpkt1 = labs(orunh1-dbrunt1);
3080            //            //
3081            found = false;            ULong64_t chkpkt2 = 0;
3082              ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT());
3083              ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
3084              chkpkt2 = labs(orunh2-dbrunt2);
3085              //
3086              ULong64_t chkpkt3 = 0;
3087              ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME());
3088              ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
3089              chkpkt3 = labs(orunh3-dbrunt3);
3090            //            //
3091              if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
3092                //
3093                if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
3094                //
3095                found = true;
3096                //
3097              } else {
3098                //
3099                if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
3100                //
3101                found = false;
3102                //
3103              };
3104          };          };
3105        };        };
3106        if ( !found && chewbacca ) goto justcheck;        //
3107        if ( !found ){        if ( found ){
3108            //
3109            // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table
3110            //
3111            if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
3112            //
3113            if ( foundinrun ){
3114              glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3115              glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
3116            };
3117          //          //
3118          // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts          GL_RUN *glrun1 = new GL_RUN();
3119          //          //
3120          ULong64_t chkpkt1 = 0;          //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
         ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());  
         ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));  
         chkpkt1 = labs(orunh1-dbrunt1);  
3121          //          //
3122          ULong64_t chkpkt2 = 0;          oss.str("");
3123          ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT());          oss << " ID="<<row->GetField(0)<<";";
         ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));  
         chkpkt2 = labs(orunh2-dbrunt2);  
3124          //          //
3125          ULong64_t chkpkt3 = 0;          glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runtrailer infos
         ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME());  
         ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));  
         chkpkt3 = labs(orunh3-dbrunt3);  
3126          //          //
3127          if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){          // merge infos
3128            //          //
3129            if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);          UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT());
3130            //          ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
3131            found = true;          UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
3132            ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
3133            if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
3134            TTree *T= 0;
3135            T = (TTree*)file->Get("Physics");
3136            if ( !T || T->IsZombie() ) throw -16;
3137            EventHeader *eh = 0;
3138            PscuHeader *ph = 0;
3139            T->SetBranchAddress("Header", &eh);
3140            while ( apkt > bpkt && aobt > bobt && lastev > 0 ){
3141              T->GetEntry(lastev);
3142              ph = eh->GetPscuHeader();
3143              apkt = PKT(ph->GetCounter());
3144              aobt = OBT(ph->GetOrbitalTime());    
3145              lastev--;
3146              if ( PEDANTIC ) throw -72;
3147            };
3148            if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
3149            //
3150            glrun->SetEV_TO(lastev);
3151            glrun->SetNEVENTS(lastev-firstev+1);
3152            glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());
3153            glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER());
3154            glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME());
3155            glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
3156            glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
3157            //
3158            glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
3159            glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
3160            glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
3161            glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP());
3162            glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE());
3163            glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE());
3164            glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A());
3165            glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B());
3166            glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO());
3167            glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO());
3168            glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB());  
3169            glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE());  
3170            glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED());  
3171            glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK());  
3172            glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC());  
3173            glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC());  
3174            //
3175            if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ) glrun1->SetPHYSENDRUN_MASK_S3S2S12(glrun->GetPHYSENDRUN_MASK_S3S2S12());
3176            if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) glrun1->SetPHYSENDRUN_MASK_S11CRC(glrun->GetPHYSENDRUN_MASK_S11CRC());
3177            //
3178            if ( !IsRunAlreadyInserted() ){
3179              //
3180              //    glrun->SetID(this->AssignRunID());
3181            //            //
3182          } else {            glrun->SetID_RUN_FRAG(glrun1->GetID());
3183              glrun->Fill_GL_RUN(conn);
3184            //            //
3185            if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);            // set id number
3186            //            //
3187            found = false;            glrun1->SetID_RUN_FRAG(glrun->GetID());
3188              glrun1->Fill_GL_RUN(conn);
3189            //            //
3190          };          };
3191            //
3192            // delete old entries in fragment table
3193            //
3194            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3195            glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3196            //
3197            delete glrun1;
3198            //
3199            return;
3200            //
3201        };        };
3202          //
3203      };      };
3204      //      //
3205      if ( found ){    justcheck:
3206        //      //
3207        // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table      if ( !found ){      
       //  
       if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");  
       //  
       if ( foundinrun ){  
         glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");  
         glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");  
       };  
3208        //        //
3209        GL_RUN *glrun1 = new GL_RUN();        if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
3210        //        //
3211        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
3212        //        //
3213        oss.str("");        oss.str("");
3214        oss << " ID="<<row->GetField(0)<<";";        oss << " SELECT ID FROM GL_RUN WHERE "
3215              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3216              << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3217              << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3218              << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3219              << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3220              << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3221              << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3222              << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3223              << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3224              << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3225              << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3226              << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3227              << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3228        //        //
3229        glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runtrailer infos        if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3230          result = conn->Query(oss.str().c_str());
3231        //        //
3232        // merge infos        if ( !result ) throw -4;
3233        //        //
3234        UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT());        row = result->Next();
       ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());  
       UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());  
       ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());  
       if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);  
       TTree *T= 0;  
       T = (TTree*)file->Get("Physics");  
       if ( !T || T->IsZombie() ) throw -16;  
       EventHeader *eh = 0;  
       PscuHeader *ph = 0;  
       T->SetBranchAddress("Header", &eh);  
       while ( apkt > bpkt && aobt > bobt && lastev > 0 ){  
         T->GetEntry(lastev);  
         ph = eh->GetPscuHeader();  
         apkt = PKT(ph->GetCounter());  
         aobt = OBT(ph->GetOrbitalTime());        
         lastev--;  
         if ( PEDANTIC ) throw -72;  
       };  
       if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);  
       //  
       glrun->SetEV_TO(lastev);  
       glrun->SetNEVENTS(lastev-firstev+1);  
       glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());  
       glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER());  
       glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME());  
       glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());  
       glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());  
       //  
       glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());  
       glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());  
       glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());  
       glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP());  
       glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE());  
       glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE());  
       glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A());  
       glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B());  
       glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO());  
       glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO());  
       glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB());    
       glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE());    
       glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED());    
       glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK());    
       glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC());    
       glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC());    
3235        //        //
3236        if ( !IsRunAlreadyInserted() ){        if ( row ){
3237          //          if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
3238          //      glrun->SetID(this->AssignRunID());          if ( PEDANTIC ) throw -70;
3239          //        } else {
3240          glrun->SetID_RUN_FRAG(glrun1->GetID());          if ( NoFrag() ){
3241          glrun->Fill_GL_RUN(conn);            glrun->SetID_RUN_FRAG(glrun->GetID());
3242          //            glrun->Fill_GL_RUN(conn);    
3243          // set id number            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3244          //          };    
         glrun1->SetID_RUN_FRAG(glrun->GetID());  
         glrun1->Fill_GL_RUN(conn);  
         //  
3245        };        };
       //  
       // delete old entries in fragment table  
       //  
       glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");  
       glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");  
       //  
       delete glrun1;  
       //  
       return;  
       //  
     };  
     //  
   };  
   //  
  justcheck:  
   //  
   if ( !found ){        
     //  
     if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");  
     //  
     // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?  
     //  
     oss.str("");  
     oss << " SELECT ID FROM GL_RUN WHERE "  
         << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("  
         << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "  
         << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("  
         << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "  
         << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
         << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "  
         << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "  
         << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "  
         << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("  
         << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "  
         << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
         << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "  
         << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";  
     //  
     if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());  
     result = conn->Query(oss.str().c_str());  
     //  
     if ( !result ) throw -4;  
     //  
     row = result->Next();  
     //  
     if ( row ){  
       if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");  
       if ( PEDANTIC ) throw -70;  
     } else {  
       if ( NoFrag() ){  
         glrun->SetID_RUN_FRAG(glrun->GetID());  
         glrun->Fill_GL_RUN(conn);        
         glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");  
       };      
3246      };      };
   };  
3247    }; // EEE    }; // EEE
3248    //    //
3249    return;    return;
# Line 3068  Bool_t PamelaDBOperations::IsRunConsiste Line 3325  Bool_t PamelaDBOperations::IsRunConsiste
3325      code = eh->GetCounter();      code = eh->GetCounter();
3326      checkfirst = 0;      checkfirst = 0;
3327      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3328         if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
3329       };      };
3330      if ( IsDebug() ) printf(" Check first is %i firstev is %i\n",checkfirst,firstev);      if ( IsDebug() ) printf(" Check first is %i firstev is %i\n",checkfirst,firstev);
3331      //      //
3332      T->GetEntry(lastev);      T->GetEntry(lastev);
3333      code = eh->GetCounter();      code = eh->GetCounter();
3334      checklast = 0;      checklast = 0;
3335      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3336         if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
3337       };      };
3338      if ( IsDebug() ) printf(" Check last is %i lastev is %i\n",checklast,lastev);      if ( IsDebug() ) printf(" Check last is %i lastev is %i\n",checklast,lastev);
3339      //      //
3340      if ( checkfirst == checklast ){      if ( checkfirst == checklast ){
# Line 3092  Bool_t PamelaDBOperations::IsRunConsiste Line 3349  Bool_t PamelaDBOperations::IsRunConsiste
3349        //        //
3350        // HERE WE MUST HANDLE THAT RUNS AND GO BACK        // HERE WE MUST HANDLE THAT RUNS AND GO BACK
3351        //        //
3352  //      if ( IsDebug() ) printf(" Never seen this case, try to handle it anyway, it was throw -95\n");        //      if ( IsDebug() ) printf(" Never seen this case, try to handle it anyway, it was throw -95\n");
3353        //        //
3354        Bool_t emptyruns = false;        Bool_t emptyruns = false;
3355        UInt_t check = 0;        UInt_t check = 0;
# Line 3184  Bool_t PamelaDBOperations::IsRunConsiste Line 3441  Bool_t PamelaDBOperations::IsRunConsiste
3441            };            };
3442            //                  //      
3443            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
3444              this->SetPhysEndRunVariables();
3445            //            //
3446            if ( chminentry == firstentry  ){ // EEE            if ( chminentry == firstentry  ){ // EEE
3447              if ( IsDebug() ) printf(" Inside isrunconsistent found a fragment of run at the beginning of the file, put it in the fragment table \n");              if ( IsDebug() ) printf(" Inside isrunconsistent found a fragment of run at the beginning of the file, put it in the fragment table \n");
3448  //              //
3449  //          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
3450  //              //
3451    
3452    
3453    
# Line 3203  Bool_t PamelaDBOperations::IsRunConsiste Line 3461  Bool_t PamelaDBOperations::IsRunConsiste
3461    
3462    
3463    
3464    mishead = true;              mishead = true;
3465    
3466    
3467    UInt_t rhfirstev = firstentry;              UInt_t rhfirstev = firstentry;
3468  //  UInt_t rtlastev = lastentry;              //  UInt_t rtlastev = lastentry;
3469    Bool_t found = false;              Bool_t found = false;
3470    Bool_t foundinrun = false;              Bool_t foundinrun = false;
3471    //              //
3472    TSQLResult *result = 0;              TSQLResult *result = 0;
3473    TSQLRow    *row    = 0;              TSQLRow    *row    = 0;
3474    //              //
3475    stringstream oss;              stringstream oss;
3476    oss.str("");              oss.str("");
3477    //              //
3478    // we have now the good first piece of a run, fill the glrun object              // we have now the good first piece of a run, fill the glrun object
3479    //              //
3480  //  if ( rhfirstev != firstev && !mishead ) mishead = true;              //  if ( rhfirstev != firstev && !mishead ) mishead = true;
3481  //  if ( rtlastev != lastev && !mistrail ) mistrail = true;              //  if ( rtlastev != lastev && !mistrail ) mistrail = true;
3482    //              //
3483  //  this->FillClass(mishead,mistrail,firstev,lastev);              //  this->FillClass(mishead,mistrail,firstev,lastev);
3484    //              //
3485    if ( IsDebug() ) printf("zz The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");              if ( IsDebug() ) printf("zz The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
3486    if ( IsDebug() ) printf("zz C THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());              if ( IsDebug() ) printf("zz C THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());
3487    //              //
3488    // First of all insert the run in the fragment table...              // First of all insert the run in the fragment table...
3489    //              //
3490    oss.str("");              oss.str("");
3491    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "              oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
3492        << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("                  << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3493        << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "                  << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3494        << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("                  << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3495        << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "                  << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3496        << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("                  << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3497        << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "                  << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3498        << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "                  << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3499        << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                  << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3500        << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("                  << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3501        << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "                  << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3502        << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("                  << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3503        << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "                  << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3504        << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";                  << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3505    //              //
3506    if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());              if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3507    result = conn->Query(oss.str().c_str());              result = conn->Query(oss.str().c_str());
3508    //              //
3509    if ( !result ) throw -4;              if ( !result ) throw -4;
3510    //              //
3511    row = result->Next();              row = result->Next();
3512    //              //
3513    if ( !row ){              if ( !row ){
3514      //                //
3515      // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)                // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
3516      //                //
3517      if ( IsDebug() ) printf(" The run is new \n");                if ( IsDebug() ) printf(" The run is new \n");
3518      if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");                if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
3519      //                //
3520      glrun->SetID(this->AssignRunID());                glrun->SetID(this->AssignRunID());
3521      glrun->SetID_RUN_FRAG(0);                glrun->SetID_RUN_FRAG(0);
3522      glrun->Fill_GL_RUN_FRAGMENTS(conn);                glrun->Fill_GL_RUN_FRAGMENTS(conn);
3523      //                //
3524    } else {              } else {
3525      if ( IsDebug() ) printf(" The run is already present in the fragment table \n");                if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
3526      if ( PEDANTIC ) throw -69;                if ( PEDANTIC ) throw -69;
3527  //    return;                //    return;
3528    };              };
3529    //              //
3530    if ( chewbacca && mishead && mistrail ) goto zjustcheck;              if ( chewbacca && mishead && mistrail ) goto zjustcheck;
3531    //              //
3532    // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?              // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
3533    //              //
3534    if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is              if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
3535                                             // missing it no way we can found a piece in the frag table                // missing it no way we can found a piece in the frag table
3536      //                //
3537      oss.str("");                oss.str("");
3538      oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "                oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
3539          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "                    << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3540          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                    << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3541          << " ID != " << glrun->ID                    << " ID != " << glrun->ID
3542          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!                    << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
3543      //                //
3544      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());                if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
3545      result = conn->Query(oss.str().c_str());                result = conn->Query(oss.str().c_str());
3546      //                //
3547      if ( !result ) throw -4;                if ( !result ) throw -4;
3548      //                //
3549      row = result->Next();                row = result->Next();
3550      //                //
3551      if ( !row && NoFrag() ){                if ( !row && NoFrag() ){
3552        //                  //
3553        oss.str("");                  oss.str("");
3554        oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "                  oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
3555            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "                      << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3556            << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                      << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3557            << " ID != " << glrun->ID                      << " ID != " << glrun->ID
3558            << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!                      << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
3559        //                  //
3560        if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());                  if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
3561        result = conn->Query(oss.str().c_str());                  result = conn->Query(oss.str().c_str());
3562        //                  //
3563        if ( !result ) throw -4;                  if ( !result ) throw -4;
3564        //                  //
3565        foundinrun = true;                  foundinrun = true;
3566        //                  //
3567        row = result->Next();                  row = result->Next();
3568        //                  //
3569      };                };
3570      //                //
3571      if ( !row ){                if ( !row ){
3572        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");                  if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
3573        found = false;                  found = false;
3574      } else {                } else {
3575        //                  //
3576        found = false; // default value                  found = false; // default value
3577        //                  //
3578        if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");                  if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
3579        //                  //
3580        // if we have both runheader and runtrailer we can check with pkt_counter:                  // if we have both runheader and runtrailer we can check with pkt_counter:
3581        //                  //
3582        if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){                  if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
3583          ULong64_t chkpkt = 0;                    ULong64_t chkpkt = 0;
3584          ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());                    ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
3585          ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));                    ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
3586          //                    //
3587          chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;                    chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;
3588          //                    //
3589          if ( labs(chkpkt-pktt)<2 ){                    if ( labs(chkpkt-pktt)<2 ){
3590            //                      //
3591            if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);                      if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
3592            //                      //
3593            found = true;                      found = true;
3594            //                      //
3595          } else {                    } else {
3596            //                      //
3597            if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);                      if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
3598            //                      //
3599            found = false;                      found = false;
3600            //                      //
3601          };                    };
3602        };                  };
3603        if ( !found && chewbacca ) goto zjustcheck;                  if ( !found && chewbacca ) goto zjustcheck;
3604        if ( !found ){                  if ( !found ){
3605          //                    //
3606          // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts                    // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts
3607          //                    //
3608          ULong64_t chkpkt1 = 0;                    ULong64_t chkpkt1 = 0;
3609          ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());                    ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
3610          ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));                    ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
3611          chkpkt1 = labs(orunh1-dbrunt1);                    chkpkt1 = labs(orunh1-dbrunt1);
3612          //                    //
3613          ULong64_t chkpkt2 = 0;                    ULong64_t chkpkt2 = 0;
3614          ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());                    ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
3615          ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));                    ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
3616          chkpkt2 = labs(orunh2-dbrunt2);                    chkpkt2 = labs(orunh2-dbrunt2);
3617          //                    //
3618          ULong64_t chkpkt3 = 0;                    ULong64_t chkpkt3 = 0;
3619          ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());                    ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
3620          ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));                    ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
3621          chkpkt3 = labs(orunh3-dbrunt3);                    chkpkt3 = labs(orunh3-dbrunt3);
3622          //                    //
3623          if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){                    if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
3624            //    if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){                      //    if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
3625            //                      //
3626            if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);                      if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
3627            //                      //
3628            found = true;                      found = true;
3629            //                      //
3630          } else {                    } else {
3631            //                      //
3632            if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);                      if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
3633            //                      //
3634            found = false;                      found = false;
3635            //                      //
3636          };                    };
3637        };                  };
3638      };                };
3639      //                //
3640      if ( found ){                if ( found ){
3641        //                  //
3642        // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table                  // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table
3643        //                  //
3644        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");                  if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
3645        //                  //
3646        if ( foundinrun ){                  if ( foundinrun ){
3647          glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");                    glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3648          glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");                    glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
3649        };                  };
3650        //                  //
3651        GL_RUN *glrun1 = new GL_RUN();                  GL_RUN *glrun1 = new GL_RUN();
3652        //                  //
3653        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));                  //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
3654        //                  //
3655        oss.str("");                  oss.str("");
3656        oss << " ID="<<row->GetField(0)<<";";                  oss << " ID="<<row->GetField(0)<<";";
3657        //                  //
3658        glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos                  glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
3659        //                  //
3660        // merge infos                  // merge infos
3661        //                  //
3662        UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());                  UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
3663        ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());                  ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
3664        UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());                  UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
3665        ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());                  ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
3666        if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);                  if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
3667        TTree *T= 0;                  TTree *T= 0;
3668        T = (TTree*)file->Get("Physics");                  T = (TTree*)file->Get("Physics");
3669        if ( !T || T->IsZombie() ) throw -16;                  if ( !T || T->IsZombie() ) throw -16;
3670        EventHeader *eh = 0;                  EventHeader *eh = 0;
3671        PscuHeader *ph = 0;                  PscuHeader *ph = 0;
3672        T->SetBranchAddress("Header", &eh);                  T->SetBranchAddress("Header", &eh);
3673        while ( apkt > bpkt && aobt > bobt && firstev < lastev ){                  while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
3674          T->GetEntry(firstev);                    T->GetEntry(firstev);
3675          ph = eh->GetPscuHeader();                    ph = eh->GetPscuHeader();
3676          bpkt = PKT(ph->GetCounter());                    bpkt = PKT(ph->GetCounter());
3677          bobt = OBT(ph->GetOrbitalTime());                    bobt = OBT(ph->GetOrbitalTime());
3678          firstev++;                    firstev++;
3679          if ( PEDANTIC ) throw -71;                    if ( PEDANTIC ) throw -71;
3680        };                  };
3681        if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);                  if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
3682        //                  //
3683        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());                  glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
3684        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());                  glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
3685        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());                  glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
3686        glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());                  glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
3687        glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());                  glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
3688        //                  //
3689        glrun->SetEV_FROM(firstev);                  glrun->SetEV_FROM(firstev);
3690        glrun->SetNEVENTS(lastev-firstev+1);                  glrun->SetNEVENTS(lastev-firstev+1);
3691        //                  //
3692        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());                  glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
3693        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());                  glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
3694        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());                  glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
3695        glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());                  glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
3696        glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());                  glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
3697        glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());                  glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
3698        glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());                  glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
3699        glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());                  glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
3700        glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());                  glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
3701        glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());                  glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
3702        glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());                  glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());
3703        glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());                  glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());
3704        glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());                  glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());
3705        glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());                  glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());
3706        glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());                  glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());
3707        glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());                  glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());
3708        //                  //
3709        if ( !IsRunAlreadyInserted() ){                  if ( glrun1->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glrun1->GetPHYSENDRUN_MASK_S3S2S12());
3710          //                  if ( glrun1->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glrun1->GetPHYSENDRUN_MASK_S11CRC());
3711          //      glrun->SetID(this->AssignRunID());                  //
3712          glrun->SetID_RUN_FRAG(glrun1->GetID());                  if ( !IsRunAlreadyInserted() ){
3713          glrun->Fill_GL_RUN(conn);                    //
3714          //                    //      glrun->SetID(this->AssignRunID());
3715          // set id number                    glrun->SetID_RUN_FRAG(glrun1->GetID());
3716          //                    glrun->Fill_GL_RUN(conn);
3717          glrun1->SetID_RUN_FRAG(glrun->GetID());                    //
3718          glrun1->Fill_GL_RUN(conn);                    // set id number
3719          //                    //
3720        };                    glrun1->SetID_RUN_FRAG(glrun->GetID());
3721        // delete old entry in fragment table                    glrun1->Fill_GL_RUN(conn);
3722        //                    //
3723        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");                  };
3724        glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");                  // delete old entry in fragment table
3725        //                  //
3726        delete glrun1;                  glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3727        //                  glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3728        //                  //
3729  //      return;                  delete glrun1;
3730        //                  //
3731      };                  //
3732      //                  //      return;
3733    };                  //
3734    //                };
3735    //                //
3736   zjustcheck:              };
3737    //              //
3738    if ( !found ){              //
3739      //            zjustcheck:
3740      if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");              //
3741      //              if ( !found ){
3742      // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?                //
3743      //                if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
3744      oss.str("");                //
3745      oss << " SELECT ID FROM GL_RUN WHERE "                // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
3746          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("                //
3747          << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "                oss.str("");
3748          << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("                oss << " SELECT ID FROM GL_RUN WHERE "
3749          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "                    << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3750          << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("                    << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3751          << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "                    << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3752          << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "                    << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3753          << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                    << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3754          << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("                    << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3755          << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "                    << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3756          << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("                    << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3757          << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "                    << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3758          << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";                    << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3759      //                    << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3760      if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());                    << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3761      result = conn->Query(oss.str().c_str());                    << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3762      //                //
3763      if ( !result ) throw -4;                if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3764      //                result = conn->Query(oss.str().c_str());
3765      row = result->Next();                //
3766      //                if ( !result ) throw -4;
3767      if ( row ){                //
3768        if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");                row = result->Next();
3769        if ( PEDANTIC ) throw -70;                //
3770      } else {                if ( row ){
3771        if ( NoFrag() ){                  if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
3772          glrun->SetID_RUN_FRAG(glrun->GetID());                  if ( PEDANTIC ) throw -70;
3773          glrun->Fill_GL_RUN(conn);                } else {
3774          glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");                  if ( NoFrag() ){
3775        };                    glrun->SetID_RUN_FRAG(glrun->GetID());
3776      };                    glrun->Fill_GL_RUN(conn);
3777    }; // EEE                    glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3778                    };
3779                  };
3780                }; // EEE
3781    
3782    
3783    
# Line 3551  Bool_t PamelaDBOperations::IsRunConsiste Line 3812  Bool_t PamelaDBOperations::IsRunConsiste
3812    
3813    
3814            } else {            } else {
3815            if ( !IsRunAlreadyInserted() ){              if ( !IsRunAlreadyInserted() ){
3816              glrun->SetID(this->AssignRunID());                glrun->SetID(this->AssignRunID());
3817              glrun->SetID_RUN_FRAG(0);                glrun->SetID_RUN_FRAG(0);
3818              glrun->Fill_GL_RUN(conn);                glrun->Fill_GL_RUN(conn);
3819            };              };
3820            }; // EEE            }; // EEE
3821            //            //
3822            firstevno = lastentry + 1;            firstevno = lastentry + 1;
# Line 3647  void PamelaDBOperations::HandleSuspiciou Line 3908  void PamelaDBOperations::HandleSuspiciou
3908      code = eh->GetCounter();      code = eh->GetCounter();
3909      checkfirst = 0;      checkfirst = 0;
3910      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3911         if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
3912         if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;        if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;
3913       };      };
3914      if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);      if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);
3915      //      //
3916      T->GetEntry(lastev);      T->GetEntry(lastev);
3917      code = eh->GetCounter();      code = eh->GetCounter();
3918      checklast = 0;      checklast = 0;
3919      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3920         if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
3921       };      };
3922      if ( IsDebug() ) printf(" Check last is %i \n",checklast);      if ( IsDebug() ) printf(" Check last is %i \n",checklast);
3923      //      //
3924      if ( checkfirst == checklast ){      if ( checkfirst == checklast ){
# Line 3769  void PamelaDBOperations::HandleSuspiciou Line 4030  void PamelaDBOperations::HandleSuspiciou
4030            };            };
4031            //                  //      
4032            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
4033              this->SetPhysEndRunVariables();
4034            //            //
4035            if ( !IsRunAlreadyInserted() ){            if ( !IsRunAlreadyInserted() ){
4036              glrun->SetID(this->AssignRunID());              glrun->SetID(this->AssignRunID());
# Line 3808  void PamelaDBOperations::HandleSuspiciou Line 4070  void PamelaDBOperations::HandleSuspiciou
4070              if ( IsDebug() ) printf(" We have the runtrailer \n");              if ( IsDebug() ) printf(" We have the runtrailer \n");
4071              //                  //    
4072              this->SetCommonGLRUN(firstTime,lastTime);              this->SetCommonGLRUN(firstTime,lastTime);
4073                this->SetPhysEndRunVariables();
4074              //              //
4075              if ( !IsRunAlreadyInserted() ){              if ( !IsRunAlreadyInserted() ){
4076                glrun->SetID(this->AssignRunID());                glrun->SetID(this->AssignRunID());
# Line 4378  Int_t PamelaDBOperations::insertCALOPULS Line 4641  Int_t PamelaDBOperations::insertCALOPULS
4641    return(0);    return(0);
4642  };  };
4643    
4644    // /**
4645    //  * Fill the GL_TRK_CALIB table
4646    //  */
4647    // void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){
4648    //   //
4649    //   TSQLResult *result = 0;
4650    //   TSQLRow    *row    = 0;
4651    //   //
4652    //   stringstream oss;
4653    //   oss.str("");
4654    //   //
4655    //   UInt_t totime = 0;
4656    //   //
4657    //   if ( !pk1 && !pk2 ){
4658    //     if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n");
4659    //     return;
4660    //   };
4661    //   //
4662    //   // check if the calibration has already been inserted
4663    //   //
4664    //   oss.str("");
4665    //   oss << " SELECT ID FROM GL_TRK_CALIB WHERE "
4666    //       << " BOOT_NUMBER = "<< this->GetBOOTnumber(); //
4667    //   oss << " AND ( ( ";  
4668    //   if ( pk1 ){
4669    //     oss << " OBT1 = "<< obt1 << " AND "
4670    //      << " PKT1 = "<< pkt1
4671    //      << " ) OR ( ";
4672    //   } else {
4673    //     oss << " PKT1 = "<< pkt2-1
4674    //      << " ) OR ( ";  
4675    //   };      
4676    //   if ( pk2 ){
4677    //     oss << " OBT2 = "<< obt2 << " AND "
4678    //      << " PKT2 = "<< pkt2;
4679    //   } else {
4680    //     oss << " PKT2 = "<< pkt1+1;
4681    //   };      
4682    //   oss << " ) );";  
4683    //   //
4684    //   if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str());
4685    //   result = conn->Query(oss.str().c_str());
4686    //   //
4687    //   if ( !result ) throw -4;
4688    //   //
4689    //   row = result->Next();
4690    //   //
4691    //   if ( row ){
4692    //     //
4693    //     if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");
4694    //     if ( PEDANTIC ) throw -80;
4695    //     //
4696    //   } else {
4697    //     //
4698    //     // we have to insert a new calibration, check where to place it
4699    //     //
4700    //     oss.str("");
4701    //     oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "
4702    //      << " FROM_TIME < "<< fromtime << " AND "
4703    //      << " TO_TIME > "<< fromtime << ";";
4704    //     //
4705    //     if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());
4706    //     result = conn->Query(oss.str().c_str());
4707    //     //
4708    //     if ( !result ) throw -4;
4709    //     //
4710    //     row = result->Next();
4711    //     //
4712    //     if ( !row ){
4713    //       //
4714    //       // no calibrations in the db contain our calibration
4715    //       //
4716    //       if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
4717    //       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
4718    //       //
4719    //       oss.str("");
4720    //       oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
4721    //        << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
4722    //       //
4723    //       if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
4724    //       result = conn->Query(oss.str().c_str());
4725    //       //
4726    //       if ( !result ) throw -4;
4727    //       //
4728    //       row = result->Next();
4729    //       if ( !row ){
4730    //      totime = numeric_limits<UInt_t>::max();
4731    //       } else {
4732    //      totime = (UInt_t)atoll(row->GetField(0));
4733    //       };
4734    //       //
4735    //     } else {
4736    //       //
4737    //       // determine upper and lower limits and make space for the new calibration
4738    //       //
4739    //       totime = (UInt_t)atoll(row->GetField(1));
4740    //       //
4741    //       oss.str("");
4742    //       oss << " UPDATE GL_TRK_CALIB SET "  
4743    //        << " TO_TIME = "<< fromtime << " WHERE "  // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
4744    //        << " ID = "<< row->GetField(0) << ";";
4745    //       //
4746    //       if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str());
4747    //       result = conn->Query(oss.str().c_str());
4748    //       //
4749    //       if ( !result ) throw -4;
4750    //       //
4751    //     };
4752    //     //
4753    //     oss.str("");
4754    //     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) "
4755    //      << " VALUES (NULL,' "
4756    //      << idroot << "',";
4757    //     //
4758    //     if ( !pk1 ){
4759    //       oss << "NULL,";
4760    //     } else {
4761    //       oss << "'"
4762    //        << t1 << "',";
4763    //     };
4764    //     //
4765    //     if ( !pk2 ){
4766    //       oss << "NULL,'";
4767    //     } else {
4768    //       oss << "'"
4769    //        << t2 << "','";
4770    //     };
4771    //     //
4772    //     oss << fromtime << "','"
4773    //      << totime << "','"
4774    //      << obt1 << "','"
4775    //      << pkt1 << "','"
4776    //      << obt2 << "','"
4777    //      << pkt2 << "','"
4778    //      << this->GetBOOTnumber() << "','"
4779    //      << valid << "');";
4780    //     //
4781    //     if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());
4782    //     //
4783    //     result = conn->Query(oss.str().c_str());
4784    //     //
4785    //     if ( !result ) throw -4;
4786    //     //
4787    //   };
4788    //   //
4789    // };
4790  /**  /**
4791   * Fill the GL_TRK_CALIB table   * Fill the GL_TRK_CALIB table
4792   */   */
4793  void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){  void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){
4794    //      
4795    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 ){  
4796      //      //
4797      if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");      glcal->ID         = 0;
4798      if ( PEDANTIC ) throw -80;      glcal->ID_ROOT_L0 = GetID_ROOT();
4799      //      glcal->EV_ROOT_CALIBTRK1 = t1;
4800    } else {      glcal->EV_ROOT_CALIBTRK2 = t2;
4801        glcal->FROM_TIME = fromtime;
4802        glcal->TO_TIME   = 0;
4803        glcal->OBT1      = obt1;
4804        glcal->OBT2      = obt2;
4805        glcal->PKT1      = pkt1;
4806        glcal->PKT2      = pkt2;
4807        glcal->BOOT_NUMBER = GetBOOTnumber();
4808        glcal->VALIDATION = valid;
4809      //      //
4810      // we have to insert a new calibration, check where to place it      HandleTRK_CALIB(glcal);
4811      //      //
4812      oss.str("");      delete glcal;
4813      oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "  }
4814          << " FROM_TIME < "<< fromtime << " AND "  /**
4815          << " TO_TIME > "<< fromtime << ";";   * Fill the GL_TRK_CALIB table
4816     */
4817    void PamelaDBOperations::HandleTRK_CALIB(GL_TRK_CALIB *glcal){
4818    
4819        Bool_t pk1 = (glcal->OBT1>0&&glcal->PKT1>0);
4820        Bool_t pk2 = (glcal->OBT2>0&&glcal->PKT2>0);
4821        UInt_t boot_number = glcal->BOOT_NUMBER;
4822        UInt_t obt1 = glcal->OBT1;
4823        UInt_t obt2 = glcal->OBT2;
4824        UInt_t pkt1 = glcal->PKT1;
4825        UInt_t pkt2 = glcal->PKT2;
4826        UInt_t fromtime = glcal->FROM_TIME;
4827        UInt_t totime = 0;
4828        UInt_t idroot = glcal->ID_ROOT_L0;
4829        UInt_t t1 = glcal->EV_ROOT_CALIBTRK1;
4830        UInt_t t2 = glcal->EV_ROOT_CALIBTRK2;
4831        UInt_t valid = glcal->VALIDATION;
4832      //      //
4833      if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());      TSQLResult *result = 0;
4834      result = conn->Query(oss.str().c_str());      TSQLRow    *row    = 0;
4835      //      //
4836      if ( !result ) throw -4;      stringstream oss;
4837        oss.str("");
4838      //      //
     row = result->Next();  
4839      //      //
4840      if ( !row ){      if ( !pk1 && !pk2 ){
4841        //          if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n");
4842        // 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;  
       //  
4843      };      };
4844      //      //
4845      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 << "',";  
4846      //      //
4847      if ( !pk1 ){      oss.str("");
4848        oss << "NULL,";      oss << " SELECT ID FROM GL_TRK_CALIB WHERE "
4849            << " BOOT_NUMBER = "<< boot_number; //
4850        oss << " AND FROM_TIME="<<fromtime; /// NEWNEWNEW -- VA BENE ?!?!?!?!
4851        oss << " AND ( ( ";  
4852        if ( pk1 ){
4853            oss << " OBT1 = "<< obt1 << " AND "
4854                << " PKT1 = "<< pkt1
4855                << " ) OR ( ";
4856      } else {      } else {
4857        oss << "'"          oss << " PKT1 = "<< pkt2-1
4858            << t1 << "',";              << " ) OR ( ";  
4859      };      };      
4860      //      if ( pk2 ){
4861      if ( !pk2 ){          oss << " OBT2 = "<< obt2 << " AND "
4862        oss << "NULL,'";              << " PKT2 = "<< pkt2;
4863      } else {      } else {
4864        oss << "'"          oss << " PKT2 = "<< pkt1+1;
4865            << t2 << "','";      };      
4866      };      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());  
4867      //      //
4868        if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str());
4869      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
4870      //      //
4871      if ( !result ) throw -4;      if ( !result ) throw -4;
4872      //      //
4873    };      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  
     //  
4874      //      //
4875      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  
4876          //          //
4877          t2++;          if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");
4878            if ( PEDANTIC ) throw -80;
4879          //          //
4880          pret2 = t2 - 1; // EMILIANO      } else {
4881          //          //
4882          if ( t2 < nevents2 ){          // we have to insert a new calibration, check where to place it
4883            tr2->GetEntry(t2);          //
4884            codt2 = eh2->GetCounter();          oss.str("");
4885            t2t1cal = codt2->Get(pctp->CalibTrk1);          oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "
4886            //              << " FROM_TIME < "<< fromtime << " AND "
4887            ph2 = eh2->GetPscuHeader();              << " TO_TIME > "<< fromtime << ";";
4888            obt2 = ph2->GetOrbitalTime();            //
4889            pkt2 = ph2->GetCounter();            if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());
4890            //          result = conn->Query(oss.str().c_str());
4891            if ( IsDebug() ) printf(" This is a trk calibration2 at obt %u pkt %u t2 is %u , t2t1cal is %u \n",obt2,pkt2,t2,t2t1cal);                //
4892  //        if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2          if ( !result ) throw -4;
4893            //                //
4894            row = result->Next();
4895            //
4896            if ( !row ){
4897                //
4898                // no calibrations in the db contain our calibration
4899                //
4900                if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
4901                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
4902                //
4903                oss.str("");
4904                oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
4905                    << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
4906                //
4907                if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
4908                result = conn->Query(oss.str().c_str());
4909                //
4910                if ( !result ) throw -4;
4911                //
4912                row = result->Next();
4913                if ( !row ){
4914                    totime = numeric_limits<UInt_t>::max();
4915                } else {
4916                    totime = (UInt_t)atoll(row->GetField(0));
4917                };
4918                //
4919          } else {          } else {
4920            //              //
4921            // running out of vector without finding the corresponding calibration, sig              // determine upper and lower limits and make space for the new calibration
4922            //              //
4923            if ( IsDebug() ) printf(" t2 >= nevents2 \n");              totime = (UInt_t)atoll(row->GetField(1));
4924            pret2 = t2;              //
4925            obt2 = 0;              oss.str("");
4926            //      pkt2 = pkt1+2;              oss << " UPDATE GL_TRK_CALIB SET "  
4927            pkt2 = 0;                  << " TO_TIME = "<< fromtime << " WHERE "  // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
4928            t2t1cal = t1+1;                  << " ID = "<< row->GetField(0) << ";";
4929                //
4930                if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str());
4931                result = conn->Query(oss.str().c_str());
4932                //
4933                if ( !result ) throw -4;
4934                //
4935          };          };
         //      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;  
 //      };  
   
   
4936          //          //
4937        };          oss.str("");
4938        //          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) "
4939        if ( IsDebug() ) printf(" Check if trk calibration2 exists \n");              << " VALUES (NULL,' "
4940        //              << idroot << "',";
4941        // EMILIANO          //
4942        if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) || (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){          if ( !pk1 ){
4943          //      if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){              oss << "NULL,";
4944          if ( IsDebug() ) printf(" The calibration found is outside the interval, sig \n");          } else {
4945                oss << "'"
4946                    << t1 << "',";
4947            };
4948            //
4949            if ( !pk2 ){
4950                oss << "NULL,'";
4951            } else {
4952                oss << "'"
4953                    << t2 << "','";
4954            };
4955          //          //
4956          // running out of vector without finding the corresponding calibration, sig          oss << fromtime << "','"
4957                << totime << "','"
4958                << obt1 << "','"
4959                << pkt1 << "','"
4960                << obt2 << "','"
4961                << pkt2 << "','"
4962                << boot_number << "','"
4963                << valid << "');";
4964          //          //
4965          pret2 = t2;          if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());
         obt2 = 0;  
         pkt2 = 0;  
       };  
       //  
       // The calibration is good  
       //  
       if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){  
4966          //          //
4967          if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2);          result = conn->Query(oss.str().c_str());
         if ( IsDebug() ) printf(" The trk calibration2 at obt %u pkt %u t2 is %u is good \n",obt2,pkt2,t2);  
4968          //          //
4969          UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );          if ( !result ) throw -4;
         if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;  
         valid = valid & valid2;  
4970          //          //
4971          // Handle good calib      };
4972        
4973        oss.str("");
4974        oss << " SELECT ID FROM GL_TRK_CALIB ORDER BY ID DESC LIMIT 1 ;";  
4975        if ( IsDebug() ) cout << oss.str().c_str() << endl;
4976        result = conn->Query(oss.str().c_str());
4977        if ( !result ) throw -4;;
4978        row = result->Next();
4979        if(row)glcal->ID = (UInt_t)atoll(row->GetField(0));
4980    
4981        //
4982    };
4983    
4984    /**
4985     * Scan tracker calibrations packets, fill the GL_TRK_CALIB table
4986     */
4987    Int_t PamelaDBOperations::insertTRK_CALIB(){
4988        //
4989        CalibTrk1Event *caltrk1 = 0;
4990        CalibTrk2Event *caltrk2 = 0;
4991        TTree *tr1 = 0;
4992        TTree *tr2 = 0;
4993        EventHeader *eh1 = 0;
4994        PscuHeader *ph1 = 0;
4995        EventHeader *eh2 = 0;
4996        PscuHeader *ph2 = 0;
4997        //
4998        PacketType *pctp=0;
4999        EventCounter *codt2=0;
5000        //
5001        Int_t nevents1 = 0;
5002        Int_t nevents2 = 0;
5003        //
5004        fromtime = 0;
5005        //
5006        obt1 = 0;
5007        pkt1 = 0;
5008        obt2 = 0;
5009        pkt2 = 0;
5010        //
5011        tr1 = (TTree*)file->Get("CalibTrk1");
5012        if ( !tr1 || tr1->IsZombie() ) throw -22;
5013        tr2 = (TTree*)file->Get("CalibTrk2");
5014        if ( !tr2 || tr2->IsZombie() ) throw -23;
5015        //
5016        tr1->SetBranchAddress("CalibTrk1", &caltrk1);
5017        tr1->SetBranchAddress("Header", &eh1);
5018        nevents1 = tr1->GetEntries();
5019        tr2->SetBranchAddress("CalibTrk2", &caltrk2);
5020        tr2->SetBranchAddress("Header", &eh2);
5021        nevents2 = tr2->GetEntries();
5022        //
5023        if ( !nevents1 && !nevents2 ) return(1);
5024        //
5025        t2 = -1;
5026        Int_t pret2 = 0;
5027        Int_t t2t1cal = 0;
5028        //
5029        bool MISSING_pkt1 = true;
5030        bool MISSING_pkt2 = true;
5031        int  ncalib = 0;
5032        bool try_to_recover = false;
5033        //
5034        for (t1=0; t1 < nevents1; t1++){//loop over packet1
5035          //          //
5036          this->HandleTRK_CALIB(true,true);          pret2 = t2;
5037            tr1->GetEntry(t1);
5038          //          //
5039          // Check for missing calibtrk1          ph1 = eh1->GetPscuHeader();
5040            obt1 = ph1->GetOrbitalTime();  
5041            pkt1 = ph1->GetCounter();  
5042            fromtime = GetAbsTime(ph1->GetOrbitalTime());  
5043          //          //
5044          if ( t2 != pret2+1 ){          // chek if the packet number and obt are consistent with the other packets ???
5045            //          //
5046            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);          if ( PKT(pkt1) >= PKT(pktfirst) && PKT(pkt1) <= upperpkt && OBT(obt1) >= OBT(obtfirst) && OBT(obt1) <= upperobt ){
5047            //              //    if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){
           while ( t2 > pret2+1 ){  
5048              //              //
5049              // handle missing calib1              if ( IsDebug() ) printf("\n Trk calibration1 %u at time %u obt %u pkt %u \n",t1,fromtime,obt1,pkt1);
5050                //      
5051                valid = ValidateTrkCalib( caltrk1, eh1 );
5052                if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
5053              //              //
5054              pret2++;              // Do we have the second calibration packet?
5055              //              //
5056              obt1 = 0;              if ( IsDebug() ) cout << " Loop over calibration2 to search associated calibration: "<<endl;
5057              pkt1 = 0;              while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1
5058                    //
5059                    t2++;
5060                    //
5061                    pret2 = t2 - 1; // EMILIANO
5062                    //
5063                    if ( t2 < nevents2 ){
5064                        tr2->GetEntry(t2);
5065                        codt2 = eh2->GetCounter();
5066                        t2t1cal = codt2->Get(pctp->CalibTrk1);
5067                        //
5068                        ph2 = eh2->GetPscuHeader();
5069                        obt2 = ph2->GetOrbitalTime();  
5070                        pkt2 = ph2->GetCounter();  
5071                        //
5072                        if ( IsDebug() ) printf(" >> trk calibration2 at obt %u pkt %u t2 is %u , t2t1cal is %u \n",obt2,pkt2,t2,t2t1cal);    
5073                        //    if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2
5074                        //    
5075                    } else {
5076                        //
5077                        // running out of vector without finding the corresponding calibration, sig
5078                        //
5079                        if ( IsDebug() ) printf(" t2 >= nevents2 \n");
5080                        pret2 = t2;
5081                        obt2 = 0;
5082                        //    pkt2 = pkt1+2;
5083                        pkt2 = 0;
5084                        t2t1cal = t1+1;
5085                    };
5086                    //      if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) && (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){
5087    
5088                    // EMILIANO
5089                    //      if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) || (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){
5090                    //        //    if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
5091                    //        if ( IsDebug() ) printf(" running out of vector without finding the corresponding calibration, sig \n");
5092                    //        //
5093                    //        // running out of vector without finding the corresponding calibration, sig
5094                    //        //
5095                    //        pret2 = t2;
5096                    //        obt2 = 0;
5097                    //        //      pkt2 = pkt1+2;
5098                    //        pkt2 = 0;
5099                    //        t2t1cal = t1+1;
5100                    //      };
5101    
5102    
5103                    //
5104                };
5105              //              //
5106              tr2->GetEntry(pret2);              if ( IsDebug() ) printf(" Check if trk calibration2 is the right one \n");
             ph2 = eh2->GetPscuHeader();  
             obt2 = ph2->GetOrbitalTime();    
             pkt2 = ph2->GetCounter();    
5107              //              //
5108              fromtime = this->GetAbsTime(ph2->GetOrbitalTime());              // EMILIANO
5109                if ( ( PKT(pkt2) < PKT(pktfirst) || PKT(pkt2) > upperpkt) || (OBT(obt2) < OBT(obtfirst) || OBT(obt2) > upperobt) ){
5110                    //      if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
5111                    if ( IsDebug() ) printf(" *WARNING*  The calibration found is outside the interval, sig \n");
5112                    //
5113                    // running out of vector without finding the corresponding calibration, sig
5114                    //
5115                    pret2 = t2;
5116                    obt2 = 0;
5117                    pkt2 = 0;
5118                };
5119                if ( PKT(pkt2) == PKT(pkt1)+1 ){
5120                    if ( IsDebug() ) cout << " ...OK"<<endl;
5121                    // =======================
5122                    // The calibration is good
5123                    // =======================
5124                    //
5125    //      if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2);
5126    //      if ( IsDebug() ) printf(" Trk calibration2 at obt %u pkt %u t2 is %u is good \n",obt2,pkt2,t2);
5127    //        if ( IsDebug() ) printf("\n Trk calibration2 at time %u obt %u pkt %u \n",fromtime,obt2,pkt2);
5128                    if ( IsDebug() ) printf(" Trk calibration2 %u at time %u obt %u pkt %u \n",t2,fromtime,obt2,pkt2);
5129                    //
5130                    UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
5131                    if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
5132    //      valid = valid & valid2;
5133                    valid = valid & valid2; //QUESTO VA CAMBIATO
5134                    //
5135                    // Handle good calib
5136                    //
5137                    MISSING_pkt1 = false;
5138                    MISSING_pkt2 = false;
5139    //              this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5140                    //
5141                    // Check for missing calibtrk1
5142                    //
5143                    if ( t2 != pret2+1 ){
5144                        //
5145                        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);
5146                        //
5147                        while ( t2 > pret2+1 ){
5148                            //
5149                            // handle missing calib1
5150                            //
5151                            pret2++;
5152                            //
5153                            obt1 = 0;
5154                            pkt1 = 0;
5155                            //
5156                            tr2->GetEntry(pret2);
5157                            ph2 = eh2->GetPscuHeader();
5158                            obt2 = ph2->GetOrbitalTime();  
5159                            pkt2 = ph2->GetCounter();  
5160                            //
5161                            fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
5162                            //
5163                            valid = 0;
5164                            MISSING_pkt1 = true;
5165                            MISSING_pkt2 = false;
5166    //                      this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5167                            //
5168                        };
5169                        //
5170                    };
5171                    //
5172                } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){
5173                    //
5174                    // Check for missing calibtrk2
5175                    //
5176                    if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
5177                    t2 = pret2;
5178                    //
5179                    // handle missing calib2
5180                    //
5181                    obt2 = 0;
5182                    pkt2 = 0;
5183                    valid = 0;
5184                    MISSING_pkt1 = false;
5185                    MISSING_pkt2 = true;
5186    //              this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5187                    //
5188                };
5189              //              //
5190              valid = 0;          } else {
5191              this->HandleTRK_CALIB(false,true);              //
5192                if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u OUTSIDE the considered time interval \n",fromtime,obt1,pkt1);
5193                //      if ( PEDANTIC ) throw -79;
5194              //              //
           };  
           //  
5195          };          };
5196            //    
5197    
5198            if( !(MISSING_pkt1&MISSING_pkt2) ){
5199                this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5200                ncalib++;
5201                if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;
5202            }
5203    
5204        }; //end loop on pkt1
5205    
5206    
5207    
5208        //
5209        // we have one more calib pkt2 !
5210        //
5211        t2++;
5212        while ( t2 < nevents2 ){
5213          //          //
5214        } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){          // handle missing calib1
         //  
         // Check for missing calibtrk2  
5215          //          //
5216          if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);          if ( IsDebug() ) printf(" t2 is %u nevents2 is %u \n",t2,nevents2);
5217          t2 = pret2;          obt1 = 0;
5218            pkt1 = 0;
5219          //          //
5220          // handle missing calib2          tr2->GetEntry(t2);
5221            ph2 = eh2->GetPscuHeader();
5222            obt2 = ph2->GetOrbitalTime();  
5223            pkt2 = ph2->GetCounter();  
5224          //          //
5225          obt2 = 0;          fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
         pkt2 = 0;  
5226          valid = 0;          valid = 0;
5227          this->HandleTRK_CALIB(true,false);                //  if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){
5228            // EMILIANO
5229            if ( this->PKT(pkt2) >= this->PKT(pktfirst) && this->PKT(pkt2 <= upperpkt) && this->OBT(obt2) >= this->OBT(obtfirst) && this->OBT(obt2) <= upperobt ){
5230                //    if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){
5231                //
5232                if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
5233                //
5234                MISSING_pkt1 = true;
5235                MISSING_pkt2 = false;
5236                this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5237                ncalib++;
5238                if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;
5239                //
5240            };
5241            //
5242            t2++;
5243          //          //
       };  
       //  
     } else {  
       //  
       if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u OUTSIDE the considered time interval \n",fromtime,obt1,pkt1);  
       //      if ( PEDANTIC ) throw -79;  
       //  
     };  
     //      
   };  
   //  
   // we have one more calib pkt2 !  
   //  
   t2++;  
   while ( t2 < nevents2 ){  
     //  
     // handle missing calib1  
     //  
     if ( IsDebug() ) printf(" t2 is %u nevents2 is %u \n",t2,nevents2);  
     obt1 = 0;  
     pkt1 = 0;  
     //  
     tr2->GetEntry(t2);  
     ph2 = eh2->GetPscuHeader();  
     obt2 = ph2->GetOrbitalTime();    
     pkt2 = ph2->GetCounter();    
     //  
     fromtime = this->GetAbsTime(ph2->GetOrbitalTime());  
     valid = 0;  
 //  if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){  
  // EMILIANO  
     if ( this->PKT(pkt2) >= this->PKT(pktfirst) && this->PKT(pkt2 <= upperpkt) && this->OBT(obt2) >= this->OBT(obtfirst) && this->OBT(obt2) <= upperobt ){  
       //    if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){  
       //  
       if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);  
       //  
       this->HandleTRK_CALIB(false,true);  
       //  
5244      };      };
5245      //  
5246      t2++;  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    
5247      //  // -----------------------------------------------------------------
5248    };  // in case of corruption, check if the calibration can be recovered
5249    // from another chewbacca file
5250    // -----------------------------------------------------------------
5251    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    
5252    
5253    //    cout <<" TRY TO RECOVER ?? "<<try_to_recover<<endl;
5254        
5255        if(chewbacca&&try_to_recover){
5256    
5257    
5258            if ( IsDebug() ) cout << endl << ">>>> TRY TO RECOVER TRACKER CALIBRATIONS <<<<"<<endl;
5259    
5260            TSQLResult *result = 0;
5261            TSQLRow    *row    = 0;
5262            //
5263            stringstream oss;
5264            oss.str("");
5265            //
5266    
5267            ////////////////////////////////////////////////////////////////////////
5268            // retrieve the name of the current file:
5269            ////////////////////////////////////////////////////////////////////////        
5270            oss.str("");
5271            oss << "SELECT NAME FROM GL_ROOT where ID=" << GetID_ROOT() <<";";
5272            if ( IsDebug() ) cout << oss.str().c_str() << endl;
5273    
5274            result = conn->Query(oss.str().c_str());
5275            if ( !result ) throw -4;;
5276            row = result->Next();
5277            TString thisfilename = (TString)row->GetField(0);
5278            if ( IsDebug() ) cout << "Current file ==> "<<thisfilename<<endl;      
5279    
5280            ////////////////////////////////////////////////////////////////////////        
5281            // read all the calibrations inserted
5282            ////////////////////////////////////////////////////////////////////////        
5283            oss.str("");
5284            oss << " SELECT ";
5285            oss << " ID,FROM_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,TO_TIME";
5286            oss << " FROM GL_TRK_CALIB ";
5287            oss << " ORDER BY ID DESC LIMIT "<<ncalib<<"; ";        
5288            if ( IsDebug() ) cout << oss.str().c_str() << endl;
5289    
5290            result = conn->Query(oss.str().c_str());
5291            if ( !result ) throw -4;;
5292            if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5293            
5294            // -----------------------------------
5295            // loop over calibrations ...
5296            // -----------------------------------
5297            UInt_t nn=0;
5298            do {
5299                row = result->Next();
5300                if(!row)break;
5301                
5302                UInt_t id       = (UInt_t)atoll(row->GetField(0));
5303                UInt_t fromtime = (UInt_t)atoll(row->GetField(1));
5304                UInt_t obt1     = (UInt_t)atoll(row->GetField(2));
5305                UInt_t pkt1     = (UInt_t)atoll(row->GetField(3));
5306                UInt_t obt2     = (UInt_t)atoll(row->GetField(4));
5307                UInt_t pkt2     = (UInt_t)atoll(row->GetField(5));
5308                UInt_t boot     = (UInt_t)atoll(row->GetField(6));
5309                UInt_t valid    = (UInt_t)atoll(row->GetField(7));
5310                bool MISSING_pkt1 = (row->GetFieldLength(8)==0);
5311                bool MISSING_pkt2 = (row->GetFieldLength(9)==0);
5312                UInt_t totime   = (UInt_t)atoll(row->GetField(10));
5313    
5314                // -------------------------------------
5315                // ...check if the entry is corrupted...
5316                // -------------------------------------
5317                cout <<"*** "<< MISSING_pkt1 << MISSING_pkt2 << valid <<endl;
5318                bool CORRUPTED = (MISSING_pkt1||MISSING_pkt2||!valid);
5319    
5320                if ( IsDebug() ) cout << "("<<nn<<")  ID = "<<id<<" from GL_TRK_CALIB  ==>  corrupted ? "<<CORRUPTED<<endl;
5321    
5322    //          if( !CORRUPTED  )continue; // nothing to do
5323    
5324                /////////////////////////////////////////////////////////  
5325                // if it is corrupted, ...look for ather chewbacca files
5326                // containing the same calibrations ...
5327                /////////////////////////////////////////////////////////
5328    
5329                bool this_MISSING_pkt1 = false;
5330                bool this_MISSING_pkt2 = false;
5331                int  this_t1=0;
5332                int  this_t2=0;;
5333                UInt_t this_valid = 0;
5334    
5335                TString path       = "";
5336                TString name       = "";
5337                TString raw        = "";
5338                UInt_t obt0        = 0;
5339                UInt_t timesync    = 0;
5340                UInt_t boot_number = 0;
5341                bool   FOUND       = false;
5342    
5343                if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;
5344    
5345    //          for(int itable=0; itable<2; itable++){
5346                for(int itable=0; itable<1; itable++){
5347    
5348                    // ------------------------------------------------------
5349                    // loop over both ROOT_TABLE and ROOT_TABLE_BAD
5350                    // ------------------------------------------------------
5351    
5352                    TString table = "ROOT_TABLE";
5353                    if(itable==1)table = "ROOT_TABLE_BAD";
5354    
5355                    oss.str("");
5356                    oss << " SELECT ";
5357                    oss << " FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,BOOT_NUMBER,INPUT_NAME ";
5358                    oss << " FROM "<<table;
5359                    oss << " WHERE 1 " << endl;
5360                    oss << " AND FILE_NAME != \""<< thisfilename<<"\"";
5361                    if( !MISSING_pkt1 ){
5362                        oss << " AND ";
5363                        oss << " PKT_NUMBER_INIT < "<<pkt1;
5364                        oss << " AND ";
5365                        oss << " PKT_NUMBER_FINAL > "<<pkt1;
5366                        oss << " AND ";
5367                        oss << " PKT_OBT_INIT < "<<obt1;
5368                        oss << " AND ";
5369                        oss << " PKT_OBT_FINAL > "<<obt1;
5370                    }else{
5371                        if(pkt2>1) pkt1 = pkt2-1;//serve dopo                  
5372                    }
5373                    if( !MISSING_pkt2 ){
5374                        oss << " AND ";
5375                        oss << " PKT_NUMBER_INIT < "<<pkt2;
5376                        oss << " AND ";
5377                        oss << " PKT_NUMBER_FINAL > "<<pkt2;
5378                        oss << " AND ";
5379                        oss << " PKT_OBT_INIT < "<<obt2;
5380                        oss << " AND ";
5381                        oss << " PKT_OBT_FINAL > "<<obt2;
5382                    }else{
5383                        if(pkt1>0) pkt2 = pkt1+1;//serve dopo
5384                    }
5385                    if( boot> 0 ){
5386                        oss << " AND ";
5387                        oss << " BOOT_NUMBER = "<<boot;
5388                    }else{
5389                    }
5390                    oss << " ORDER BY BAD_PKT_CALREAD ASC; ";
5391                
5392                    TSQLResult *result2 = 0;
5393                    TSQLRow    *row2    = 0;
5394    
5395                    if ( IsDebug() ) cout << oss.str().c_str() << endl;        
5396                    result2 = conn->Query(oss.str().c_str());
5397                    if ( !result2 ) throw -4;;
5398                    if ( IsDebug() ) cout <<"Rows: "<<result2->GetRowCount()<<endl;
5399    
5400                    // ------------------------------------------------------
5401                    // loop over files containing repetition (if any)
5402                    // ------------------------------------------------------
5403                    do {
5404                        row2 = result2->Next();
5405                        if(!row2)break;
5406                    
5407                        // ------------------------------------------------------
5408                        // ... a repetition is found ...
5409                        // ------------------------------------------------------
5410                        path       = (TString)row2->GetField(0);
5411                        name       = (TString)row2->GetField(1);
5412                        raw        = (TString)row2->GetField(5);
5413                        obt0        = (UInt_t)atoll(row2->GetField(2));
5414                        timesync    = (UInt_t)atoll(row2->GetField(3));
5415                        boot_number = (UInt_t)atoll(row2->GetField(4));
5416                    
5417                        if ( IsDebug() ) cout << "- - - - - - - - - - -" <<endl;
5418    //                  cout << path <<endl;
5419                        cout << "File    : " <<name <<endl;
5420    //                  cout << obt0 <<endl;
5421    //                  cout << timesync <<endl;
5422                        cout << "boot n. : "<<boot_number <<endl;
5423    //                  cout << raw <<endl;
5424    
5425                        // ------------------------------------------------------
5426                        // ... retrieve the calibration packets.
5427                        // ------------------------------------------------------
5428                        TFile *file = new TFile(path+"/"+name);
5429                        if(!file)throw -100;
5430                        if(file->IsZombie())throw -100;
5431                        //
5432                        tr1 = (TTree*)file->Get("CalibTrk1");
5433                        if ( !tr1 || tr1->IsZombie() ) throw -22;
5434                        tr2 = (TTree*)file->Get("CalibTrk2");
5435                        if ( !tr2 || tr2->IsZombie() ) throw -23;
5436                        //
5437                        tr1->SetBranchAddress("CalibTrk1", &caltrk1);
5438                        tr1->SetBranchAddress("Header", &eh1);
5439                        nevents1 = tr1->GetEntries();
5440                        tr2->SetBranchAddress("CalibTrk2", &caltrk2);
5441                        tr2->SetBranchAddress("Header", &eh2);
5442                        nevents2 = tr2->GetEntries();
5443                        for(this_t1=0; this_t1<nevents1; this_t1++){
5444                            tr1->GetEntry(this_t1);
5445                            if(
5446                                (UInt_t)eh1->GetPscuHeader()->GetCounter() == pkt1 &&
5447                                true) break;
5448                            this_MISSING_pkt1 = true;
5449                        }
5450                        for(this_t2=0; this_t2<nevents2; this_t2++){
5451                            tr2->GetEntry(this_t2);
5452                            if(
5453                                (UInt_t)eh2->GetPscuHeader()->GetCounter() == pkt2 &&
5454                                true) break;
5455                            this_MISSING_pkt2 = true;
5456                        }
5457                        this_valid =
5458                            ValidateTrkCalib( caltrk1, eh1, file )
5459                            *
5460                            ValidateTrkCalib( caltrk2, eh2, file );
5461                                
5462                        // ---------------------------------------------------------------------
5463                        // accept the calibration if it is better than the previous:
5464                        //
5465                        // - if the new calibration is perfect (both valid packets)
5466                        // - if the new calibration has both the packets and the previous does not
5467                        // ---------------------------------------------------------------------
5468                        if(
5469                            ( !this_MISSING_pkt1&&!this_MISSING_pkt2&&this_valid )||
5470                            ( (MISSING_pkt1||MISSING_pkt2) && (!this_MISSING_pkt1&&!this_MISSING_pkt2) )||
5471                            false)FOUND=true;      
5472                
5473                        if(file)file->Close();
5474    
5475                        if(FOUND)break;
5476    
5477                    }while(1);//endl loop over root table entries
5478    
5479                    if(FOUND)break;
5480    
5481                }//end loop over tables
5482    
5483                if(FOUND){
5484    
5485                if ( IsDebug() ) cout << " >>> REPETITION FOUND :-) <<<" <<endl;
5486    
5487                    ////////////////////////////////////////////
5488                    // insert a new entry in GL_TRK_CALIB and
5489                    // modify the time-tag of the previous one
5490                    ////////////////////////////////////////////
5491    
5492                    // ---------------------------------------------------------------------
5493                    // step 1: insert a new raw file in GL_RAW
5494                    // ---------------------------------------------------------------------
5495                    //
5496                    // check if the raw file already exist
5497                    //
5498                    UInt_t id_raw = 0;
5499                    oss.str("");
5500                    oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";";
5501                    if ( IsDebug() ) cout << oss.str().c_str() << endl;
5502                    
5503                    result = conn->Query(oss.str().c_str());
5504                    if ( !result ) throw -4;;
5505                    if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5506                    if( result->GetRowCount() == 0){
5507                        if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl;
5508                        // - - - - - - - - - - -
5509                        // insert new raw file
5510                        // - - - - - - - - - - -
5511                        GL_RAW glraw = GL_RAW();    
5512                        glraw.PATH        = gSystem->DirName(raw.Data());
5513                        glraw.NAME        = gSystem->BaseName(raw.Data());
5514                        glraw.BOOT_NUMBER = boot_number;
5515                        //
5516                        insertPamelaRawFile( &glraw );
5517                        //              
5518                        id_raw = glraw.ID;              
5519                    }else{
5520                        row = result->Next();
5521                        id_raw = (UInt_t)atoll(row->GetField(0));
5522                    }
5523                    if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl;
5524    
5525                    // ---------------------------------------------------------------------
5526                    // step 1(bis): retrieve the timesync id associated to the file
5527                    // (NB, uso lo stesso associato al file iniziale)
5528                    // ---------------------------------------------------------------------
5529                    UInt_t idtimesync = 0;
5530                    oss.str("");
5531                    oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";
5532                    if ( debug ) printf(" %s \n",oss.str().c_str());
5533                    result = conn->Query(oss.str().c_str());
5534                    if ( !result ) throw -3;
5535                    row = result->Next();
5536                    if ( !row ) throw -3;
5537                    idtimesync = (UInt_t)atoll(row->GetField(0));
5538                    if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl;
5539                    
5540                    // ---------------------------------------------------------------------
5541                    // step 2: insert a new root file in GL_ROOT
5542                    // ---------------------------------------------------------------------
5543                    //
5544                    // check if the raw file already exist
5545                    //
5546                    UInt_t id_root = 0;
5547                    oss.str("");
5548                    oss << "SELECT ID FROM GL_ROOT where NAME=\"" << gSystem->BaseName(name.Data()) <<"\";";
5549                    if ( IsDebug() ) cout << oss.str().c_str() << endl;
5550                    
5551                    result = conn->Query(oss.str().c_str());
5552                    if ( !result ) throw -4;;
5553                    if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5554                    if( result->GetRowCount() == 0){
5555                        if ( IsDebug() ) cout << " << Insert new ROOT file >> "<<endl;
5556                        // - - - - - - - - - - -
5557                        // insert new root file
5558                        // - - - - - - - - - - -
5559                        GL_ROOT glroot = GL_ROOT();            
5560                        glroot.ID_RAW      = id_raw;
5561                        glroot.ID_TIMESYNC = idtimesync;
5562                        glroot.PATH        = path;
5563                        glroot.NAME        = name;
5564                        //
5565                        insertPamelaRootFile( &glroot );
5566                        //              
5567                        id_root = glroot.ID;                    
5568                    }else{
5569                        row = result->Next();
5570                        if(row)id_root = (UInt_t)atoll(row->GetField(0));
5571                    }
5572                    if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl;
5573    
5574                    // ---------------------------------------------------------------------
5575                    // step 3: modify time-tag of corrupted GL_TRK_CALIB entry
5576                    // ---------------------------------------------------------------------
5577                    if ( IsDebug() ) cout << " << Modify time-tag of calibration ID="<<id<<" >> "<<endl;
5578                    oss.str("");
5579                    oss << " UPDATE GL_TRK_CALIB SET "  
5580                        << " TO_TIME=0 , FROM_TIME=0  WHERE "
5581                        << " ID = "<< id  << ";";
5582                    if ( IsDebug() ) cout << oss.str().c_str() << endl;
5583                    result = conn->Query(oss.str().c_str());
5584                    if ( !result ) throw -4;;
5585    
5586                    // ---------------------------------------------------------------------
5587                    // step 4: insert the new calibration:
5588                    // ---------------------------------------------------------------------
5589                    if ( IsDebug() ) cout << " << Insert new TRK calibration >> "<<endl;
5590                    //
5591                    GL_TRK_CALIB glcal = GL_TRK_CALIB();        
5592                    //
5593                    glcal.ID_ROOT_L0        = id_root;
5594                    glcal.EV_ROOT_CALIBTRK1 = this_t1;
5595                    glcal.EV_ROOT_CALIBTRK2 = this_t2;
5596                    glcal.FROM_TIME         = fromtime;
5597                    glcal.TO_TIME           = totime;
5598                    glcal.OBT1              = obt1;
5599                    glcal.OBT2              = obt2;
5600                    glcal.PKT1              = pkt1;
5601                    glcal.PKT2              = pkt1;
5602                    glcal.BOOT_NUMBER       = GetBOOTnumber();
5603                    glcal.VALIDATION        = this_valid;
5604                    //
5605                    HandleTRK_CALIB(&glcal);
5606                    if ( IsDebug() ) cout << "ID = "<<glcal.ID<<endl;
5607                    //
5608                    
5609                }
5610                if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;
5611    
5612            }while(1);//end loop over calibrations
5613            
5614            
5615            if( result )delete result;
5616            if( row    )delete row;
5617            
5618            
5619    
5620            
5621    
5622    
5623        }
5624    
5625    
5626    //      // ------------------------------
5627    //      // try to recover the calibration
5628    //      // ------------------------------
5629    //      cout << "TRY TO RECOVER TRACKER CALIBRATION"<<endl;
5630    //      //
5631    //      ULong64_t time  = 0; //absolute time
5632    //      string    path[100]; //mettere un limite massimo
5633    //      int       nrows = 0;
5634    //      UInt_t    pkt   = 0;
5635    //      UInt_t    obt   = 0;
5636    //      char     *type  = "";
5637    //      EventHeader *eh   = new EventHeader();
5638    //      CalibTrk1Event *c = new CalibTrk1Event();
5639            
5640    //       //
5641    //       if(which_is_not_valid==1 || which_is_not_valid==3){
5642    //        //
5643    //        cout << "PKT1 --> missing or corrupted "<<endl;
5644    //        type = "CalibTrk1";
5645    //        pkt  = pkt1;
5646    //        obt  = obt1;
5647    //        time = this->GetAbsTime(obt1);
5648    //        if( pkt1 == 0 ){//missing
5649    //            time = this->GetAbsTime(obt2);
5650    //            pkt  = pkt2-1;          
5651    //        }
5652    //        //
5653    //       }else if (which_is_not_valid==2 || which_is_not_valid==3){
5654    //        //
5655    //        cout << "PKT2--> missing or corrupted "<<endl;
5656    //        type = "CalibTrk2 ";
5657    //        pkt  = pkt2;
5658    //        obt  = obt2;
5659    //        time = this->GetAbsTime(obt2);
5660    //        if( pkt2 == 0 ){//missing
5661    //            time = this->GetAbsTime(obt1);
5662    //            pkt  = pkt1+1;          
5663    //        }
5664    //        //
5665    //       }else{
5666    //        cout << "this should not happen!!! "<<endl;
5667    //        trow -666;
5668    //       }
5669          
5670    //       nrows = Query_ROOT_TABLE(time,conn,path);// get the list of file which might contain the packet
5671          
5672    
5673    //       for(int r=0; r<nrows; r++){ //loop over rows
5674    //        if(path)cout << r << " >>>> "<<(path+r)->c_str() << endl;
5675    //        /// verifica che il file non sia quello gia` aperto
5676    //       }
5677    
5678    //       ////////////////////////////////////////////////////////////////////////
5679    
5680    //       TSQLResult *result = 0;
5681    //       TSQLRow    *row    = 0;
5682    //       //
5683    //       stringstream oss;
5684    //       oss.str("");
5685    //       // ----------------------------------------
5686    //       // read the id of last calibration inserted
5687    //       // ----------------------------------------
5688    //       oss.str("");
5689    //       oss << " SELECT ";
5690    //       oss << " (ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION) ";
5691    //       oss << " ORDER BY ID DESC LIMIT 1; ";
5692          
5693    //       result = conn->Query(oss.str().c_str());
5694    //       row = result->Next();
5695    //       if( !row )throw -666;
5696    
5697    //       if( result )delete result;
5698    //       if( row    )delete row;
5699    
5700    //       UInt_t id = (UInt_t)atoll(row->GetField(0));
5701    
5702    //       // -------------------------------------
5703    //       // ...and modify it with new parameters
5704    //       // -------------------------------------
5705          
5706    
5707    //   }
5708    //    //
5709    return(0);    return(0);
5710  };  };
# Line 5061  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6007  Int_t PamelaDBOperations::CleanGL_RUN_FR
6007                    << " RM_ACQ_SETTING_MODE=" << glrun->GetRM_ACQ_SETTING_MODE()<< " , "                      << " RM_ACQ_SETTING_MODE=" << glrun->GetRM_ACQ_SETTING_MODE()<< " , "  
6008                    << " TRK_CALIB_USED=" << glrun->GetTRK_CALIB_USED()<< " , "                      << " TRK_CALIB_USED=" << glrun->GetTRK_CALIB_USED()<< " , "  
6009                    << " CAL_DSP_MASK=" << glrun->GetCAL_DSP_MASK()<< " , "                      << " CAL_DSP_MASK=" << glrun->GetCAL_DSP_MASK()<< " , "  
6010                    << " LAST_TIMESYNC=" << glrun->GetLAST_TIMESYNC()<< " , "                      << " LAST_TIMESYNC=" << glrun->GetLAST_TIMESYNC()<< " , ";
6011                    << " OBT_TIMESYNC=" << glrun->GetOBT_TIMESYNC()                //
6012                    << " WHERE ID=" << myid[u] << ";";                if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() )
6013                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
6014                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() )
6015                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC() << " , ";
6016                  //              
6017                  oss << " OBT_TIMESYNC=" << glrun->GetOBT_TIMESYNC();
6018                  oss << " WHERE ID=" << myid[u] << ";";
6019                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6020              };              };
6021              //              //
# Line 5074  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6026  Int_t PamelaDBOperations::CleanGL_RUN_FR
6026              for (UInt_t u=1; u <= nid ; u++){              for (UInt_t u=1; u <= nid ; u++){
6027                oss.str("");                oss.str("");
6028                oss << "UPDATE GL_RUN_FRAGMENTS SET "                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6029                    << " RUNHEADER_TIME=" << glrun->GetRUNHEADER_TIME()<< " , "                    << " RUNHEADER_TIME=" << glrun->GetRUNHEADER_TIME()<< " , ";
6030                    << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "                //
6031                  if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() )
6032                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
6033                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() )
6034                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6035                  //              
6036                  oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "
6037                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< ";";                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< ";";
6038                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6039              };                  };    
# Line 5098  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6056  Int_t PamelaDBOperations::CleanGL_RUN_FR
6056                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "
6057                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , "                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , "
6058                    << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< " , "                    << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< " , "
6059                    << " PKT_COUNTER=" << glrun->GetPKT_COUNTER()<< " , "                    << " PKT_COUNTER=" << glrun->GetPKT_COUNTER()<< " , ";
6060                    << " PKT_READY_COUNTER=" << glrun->GetPKT_READY_COUNTER()                //
6061                  if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ){
6062                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6063                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) {
6064                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6065                  //              
6066                  oss << " PKT_READY_COUNTER=" << glrun->GetPKT_READY_COUNTER()
6067                    << " WHERE ID=" << myid[u] << ";";                    << " WHERE ID=" << myid[u] << ";";
6068                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6069              };              };
# Line 5112  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6076  Int_t PamelaDBOperations::CleanGL_RUN_FR
6076                oss.str("");                oss.str("");
6077                oss << "UPDATE GL_RUN_FRAGMENTS SET "                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6078                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "
6079                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , "                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , ";
6080                    << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< ";";                //
6081                  if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ){
6082                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6083                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){
6084                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6085                  //              
6086                  oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< ";";
6087                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6088              };                  };    
6089            };            };
# Line 5272  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6242  Int_t PamelaDBOperations::CleanGL_RUN_FR
6242          glrun->SetID_RUN_FRAG(glrun->GetID());          glrun->SetID_RUN_FRAG(glrun->GetID());
6243          glrun->Fill_GL_RUN(conn);            glrun->Fill_GL_RUN(conn);  
6244          //          //
6245  //      oss.str("");          //      oss.str("");
6246  //      oss << " SELECT ID FROM GL_RUN WHERE "          //      oss << " SELECT ID FROM GL_RUN WHERE "
6247  //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "          //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
6248  //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "          //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
6249  //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "          //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
6250  //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "          //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
6251  //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";          //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
6252  //      //          //      //
6253  //      if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());          //      if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
6254  //      result2 = conn->Query(oss.str().c_str());          //      result2 = conn->Query(oss.str().c_str());
6255  //      //          //      //
6256  //      if ( !result2 ) throw -4;          //      if ( !result2 ) throw -4;
6257  //      //          //      //
6258  //      row2 = result2->Next();          //      row2 = result2->Next();
6259  //      //          //      //
6260  //      if ( !row2 ) throw -25;          //      if ( !row2 ) throw -25;
6261  //      //          //      //
6262  //      oss.str("");          //      oss.str("");
6263  //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);          //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);
6264  //      if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());          //      if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());
6265  //      result2 = conn->Query(oss.str().c_str());          //      result2 = conn->Query(oss.str().c_str());
6266  //      //          //      //
6267  //      if ( !result2 ) throw -4;          //      if ( !result2 ) throw -4;
6268          //          //
6269          moved++;          moved++;
6270          //          //
# Line 5306  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6276  Int_t PamelaDBOperations::CleanGL_RUN_FR
6276        //        //
6277        //        //
6278        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
6279  //      oss.str("");        //      oss.str("");
6280  //       oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);        //       oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
6281  //       if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());        //       if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
6282  //       result2 = conn->Query(oss.str().c_str());        //       result2 = conn->Query(oss.str().c_str());
6283  //      //        //      //
6284  //      if ( !result2 ) throw -4;        //      if ( !result2 ) throw -4;
6285  //      //        //      //
6286        row = result->Next();        row = result->Next();
6287      };      };
6288    };    };
# Line 5457  Int_t PamelaDBOperations::ValidateRuns(T Line 6427  Int_t PamelaDBOperations::ValidateRuns(T
6427    GL_RUN* this_run = new GL_RUN();    GL_RUN* this_run = new GL_RUN();
6428    GL_RUN* next_run = new GL_RUN();    GL_RUN* next_run = new GL_RUN();
6429    Int_t   nseq_max = 1000;    Int_t   nseq_max = 1000;
6430  //  UInt_t* sequence = new UInt_t[100];    //  UInt_t* sequence = new UInt_t[100];
6431    vector<UInt_t> sequence(nseq_max);    vector<UInt_t> sequence(nseq_max);
6432    Int_t   nseq = 0;    Int_t   nseq = 0;
6433    Bool_t CHECK = false;    Bool_t CHECK = false;
# Line 5472  Int_t PamelaDBOperations::ValidateRuns(T Line 6442  Int_t PamelaDBOperations::ValidateRuns(T
6442    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
6443    while(1){    while(1){
6444                        
6445            row = result->Next();      row = result->Next();
6446            if( row == NULL ) break;      if( row == NULL ) break;
6447                        
6448            //------------      //------------
6449            //get run info      //get run info
6450            //------------      //------------
6451            this_run->Set_GL_RUN(row);      this_run->Set_GL_RUN(row);
6452                                        
6453            Bool_t this_BAD = false;      Bool_t this_BAD = false;
6454            if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;      if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;
6455            else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;      else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;
6456            else{      else{
6457  //                printf("Missing or corrupted header!! \n");        //                  printf("Missing or corrupted header!! \n");
6458                    this_ONLINE = false;        this_ONLINE = false;
6459                    this_BAD = true;        this_BAD = true;
6460            };      };
6461    
6462            //-----------------------------------      //-----------------------------------
6463            //compare with previous(next in time)      //compare with previous(next in time)
6464            //-----------------------------------      //-----------------------------------
6465            CHECK = false;      CHECK = false;
6466            UInt_t interval=0;      UInt_t interval=0;
6467                        
6468            if( nrow != 0){      if( nrow != 0){
6469                    
6470                                        
6471                    t1 = this_run->GetRUNTRAILER_TIME();        t1 = this_run->GetRUNTRAILER_TIME();
6472                    t2 = next_run->GetRUNHEADER_TIME();        t2 = next_run->GetRUNHEADER_TIME();
6473                    interval = (t2-t1);        interval = (t2-t1);
6474                                        
6475                    if(this_ONLINE && next_ONLINE){                               // this: ON-LINE + next: ON-LINE        if(this_ONLINE && next_ONLINE){                           // this: ON-LINE + next: ON-LINE
6476                                                        
6477                            if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0;     //=> run fragments          if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0;       //=> run fragments
6478                                                        
6479                            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
6480                                                        
6481                            if( !CHECK && this_run->VALIDATION ){          if( !CHECK && this_run->VALIDATION ){
6482                                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);            for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
6483                                    nseq=0;            nseq=0;
6484                            }          }
6485                                        
6486                    }else if( !this_ONLINE && next_ONLINE) {              // this: DEFAULT + next:ON-LINE        }else if( !this_ONLINE && next_ONLINE) {          // this: DEFAULT + next:ON-LINE
6487                                                        
6488                            CHECK = true;          CHECK = true;
6489    
6490                    }else if( !next_ONLINE ){                                             // this:ANY + next:DEFAULT        }else if( !next_ONLINE ){                                         // this:ANY + next:DEFAULT
6491                                                        
6492                            assignVALIDATION(next_run->ID,true);          assignVALIDATION(next_run->ID,true);
6493                            nseq=0;          nseq=0;
6494                    }        }
6495            }      }
6496    
6497            //----------------------------      //----------------------------
6498            //check run sequence for calib      //check run sequence for calib
6499            //----------------------------      //----------------------------
6500            if( CHECK ){      if( CHECK ){
6501                    // check if calibration exists        // check if calibration exists
6502                    if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);        if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
6503                    Bool_t MISSING = MissingTRK_CALIB(t1,t2);        Bool_t MISSING = MissingTRK_CALIB(t1,t2);
6504                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);        for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
6505                    nseq=0;        nseq=0;
6506            };      };
6507            //--------------      //--------------
6508            //store run info      //store run info
6509            //--------------      //--------------
6510            *next_run   = *this_run;      *next_run   = *this_run;
6511            next_ONLINE = this_ONLINE;      next_ONLINE = this_ONLINE;
6512            if( !this_BAD ){      if( !this_BAD ){
6513                    if(nseq < nseq_max){        if(nseq < nseq_max){
6514                            sequence[nseq] = this_run->ID;          sequence[nseq] = this_run->ID;
6515                            nseq++;          nseq++;
6516                    }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);        }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
6517            };      };
6518                        
6519            if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);      if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);
6520            nrow++;      nrow++;
6521                        
6522    };    };
6523    delete this_run;    delete this_run;
# Line 5563  Int_t PamelaDBOperations::ValidateRuns(T Line 6533  Int_t PamelaDBOperations::ValidateRuns(T
6533   */   */
6534  Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){  Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
6535                    
6536          GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();    GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
6537                    
6538          // get the closest VALIDATED calibration before the run start (t2)    // get the closest calibration before the run start (t2)
6539          if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);      //>>> missing    if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);    //>>> missing
6540                    
6541          if ( trkcalib->TO_TIME  < t2 ) return(true);                                    //>>> missing    if ( trkcalib->TO_TIME  < t2 ) return(true);                                  //>>> missing
6542                    
6543          //==============================================================    //==============================================================
6544          // Check is done first on the basis of time between calibration,    // Check is done first on the basis of time between calibration,
6545          // which should be equal to the time between ascending-nodes.    // which should be equal to the time between ascending-nodes.
6546          //==============================================================    //==============================================================
6547          if ( t2 - trkcalib->FROM_TIME > 5700) {    if ( t2 - trkcalib->FROM_TIME > 5700) {
6548                  if ( IsDebug() )printf("Long time between calib and run start %u :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);      if ( IsDebug() )printf("Long time between calib and run start %u :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);
6549          //==============================================================      //==============================================================
6550          // there might be a missing calibration, due to:      // there might be a missing calibration, due to:
6551          // - MM full      // - MM full
6552          // - corrupted packets      // - corrupted packets
6553          // - loss of data      // - loss of data
6554          // There is an exception in case a download was done during ascending node      // There is an exception in case a download was done during ascending node
6555          //==============================================================      //==============================================================
6556                  Bool_t DOWNLOAD = false;      Bool_t DOWNLOAD = false;
6557                  // check if the calib was skipped becouse of download .... DA FARE!!      // check if the calib was skipped becouse of download .... DA FARE!!
6558                  if(DOWNLOAD)return(false);      if(DOWNLOAD)return(false);
6559                                    
6560                  return(true);                                   //>>> missing      return(true);                                       //>>> missing
6561                                    
6562          };    };
6563                    
6564          //==============================================================    //==============================================================
6565          // If the last calibration is close to the run less than this time,    // If the last calibration is close to the run less than this time,
6566          // it is enough to say that there are no missing calibrations    // it is enough to say that there are no missing calibrations
6567          //==============================================================    //==============================================================
6568          // the long time interval bewteen runs might be due to download    // the long time interval bewteen runs might be due to download
6569          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);
6570          return(false);    return(false);
6571                    
6572  };  };
6573  /**  /**
# Line 5606  Bool_t PamelaDBOperations::MissingTRK_CA Line 6576  Bool_t PamelaDBOperations::MissingTRK_CA
6576   * @param validation true/false   * @param validation true/false
6577   */   */
6578  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
6579          TSQLResult *result = 0;    TSQLResult *result = 0;
6580          stringstream oss;    stringstream oss;
6581          oss.str("");    oss.str("");
6582          oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";    oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
6583          //    //
6584  //      if ( IsDebug() )    //    if ( IsDebug() )
6585  //      printf(" Set VALIDATION = %i for run %i \n",validation,idrun);    //    printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
6586          if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());    if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
6587          result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
6588          if ( !result ) throw -4;    if ( !result ) throw -4;
6589          return(0);    return(0);
6590  }  }
6591    
6592    
# Line 6259  void PamelaDBOperations::RemoveCALIBS(){ Line 7229  void PamelaDBOperations::RemoveCALIBS(){
7229   * Rearrange calibration tables   * Rearrange calibration tables
7230   *   *
7231   **/   **/
7232  UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){  UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh , TFile *file){
7233    
7234        if(!caltrk) return 0;
7235    
7236        if ( IsDebug() ) cout << "ValidateTrkCalib:"<<endl;
7237    
7238        UInt_t validate = 1;
7239      Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};      Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
7240      UInt_t timeaftercalib=120000; //2000;      UInt_t timeaftercalib=120000; //2000;
7241  //  ----------      TString classname = caltrk->GetName();
7242  //  Check CRCs  
7243  //  ----------  //  ----------------------------------
7244    //  Check CRCs and failed calibrations
7245    //  ----------------------------------
7246      for(Int_t ipkt=0; ipkt<6; ipkt++){      for(Int_t ipkt=0; ipkt<6; ipkt++){
7247          if( caltrk->crc_hcal[ipkt] )return 0; // :-(          if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
7248          for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(              if( caltrk->crc_hcal[ipkt] ){
7249    //              if(IsDebug())cout<<"(CRC Header)";
7250                    validate = 0;
7251                    if(IsDebug())cout <<endl<<" *** CRC *** (header DSPn "<<caltrk->DSPnumber[ipkt]<<")";
7252            
7253                }
7254                for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] ){
7255    //              if(IsDebug())cout<<"(CRC Pkt-"<<ilad<<")";
7256                    if(IsDebug())cout <<endl<<" *** CRC *** (data DSPn "<<caltrk->DSPnumber[ipkt]<<" ladder "<<ilad<<")";
7257                    validate = 0;
7258                }
7259                if( !(caltrk->ncalib_event[ipkt]==0 && caltrk->cal_flag[ipkt]==0) ){
7260                    if(IsDebug())cout <<endl<<" *** FAILURE ***     (data DSPn "<<caltrk->DSPnumber[ipkt]<<")";
7261                    validate = 0;
7262                }
7263            }else{
7264    //          validate=0;
7265                if(IsDebug())cout <<endl<<" *** DSPn *** ("<< caltrk->DSPnumber[ipkt] <<" @pkt "<<ipkt<<")";
7266            }
7267      }      }
7268    
7269  //  -----------------------  //  -----------------------
7270  //  Check missing packets:  //  Check missing packets:
7271  //  -----------------------  //  -----------------------
# Line 6295  UInt_t PamelaDBOperations::ValidateTrkCa Line 7291  UInt_t PamelaDBOperations::ValidateTrkCa
7291  //  Check if it is first or second calibration packet  //  Check if it is first or second calibration packet
7292  //  -------------------------------------------------  //  -------------------------------------------------
7293      UInt_t build=0;      UInt_t build=0;
     TString classname = caltrk->GetName();  
7294      UInt_t base=0;      UInt_t base=0;
7295      UInt_t mask=0;      UInt_t mask=0;
7296      if(classname.Contains("CalibTrk1Event")){      if(classname.Contains("CalibTrk1Event")){
# Line 6306  UInt_t PamelaDBOperations::ValidateTrkCa Line 7301  UInt_t PamelaDBOperations::ValidateTrkCa
7301          base=18;          base=18;
7302          mask=0xFC0000;          mask=0xFC0000;
7303      }      }
7304  //  -------------------------------------------------  //  ----------------------------------------------------
7305  //  Count number of packets and set build variable  //  Count number of valid packets and set build variable
7306  //  -------------------------------------------------  //  ----------------------------------------------------
7307        if(IsDebug())cout <<endl<< " DSP: ";
7308      Int_t  npkts=0;      Int_t  npkts=0;
7309      for(Int_t ipkt=0; ipkt<6; ipkt++){      for(Int_t ipkt=0; ipkt<6; ipkt++){
7310          if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){          if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
7311                if(IsDebug())cout <<" "<<caltrk->DSPnumber[ipkt];
7312              npkts++;              npkts++;
7313              build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );              build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
7314          }  //          cout << caltrk->DSPnumber[ipkt]
7315            };
7316      }      }
7317  //    if( npkts==6 )return 1; // :-)      if(IsDebug())cout << " ==> "<< hex << build << dec;
7318    //  ----------------------------------------------------
7319  //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;  //  If the number of valid packets is 6, ok exit...
7320    //  ----------------------------------------------------
7321  //  -----------------------------------------------      if( npkts==6 ){
7322  //  If missing packets: check the acq configuration          return validate; // exit
7323        }
7324    ////////////////////////////////////////////////////////
7325    //  ...otherwise there might be some missing packets
7326    //
7327    //  In this case check the acq configuration
7328  //  (some DSPs might be excluded from acquisition)  //  (some DSPs might be excluded from acquisition)
7329  //  -----------------------------------------------  ////////////////////////////////////////////////////////
7330    
7331        if(!eh || !file || (file&&file->IsZombie()) ){
7332            if ( IsDebug() )cout << " *** MISSING VIEW *** eh="<<eh<<" file="<<file<<" cannot validate"<<endl;
7333            return (0);
7334        }
7335        
7336  //  -----------------------------------------------  //  -----------------------------------------------
7337  //  retrieve the first run header after calib  //  retrieve the first run header after calib
7338  //  -----------------------------------------------  //  -----------------------------------------------
7339       PacketType *pctp;      
7340       EventCounter *cod;      PacketType *pctp;
7341       cod = eh->GetCounter();      EventCounter *cod;
7342       Int_t irun = cod->Get(pctp->RunHeader);      cod = eh->GetCounter();
7343       TTree *rh=(TTree*)file->Get("RunHeader");      Int_t irun = cod->Get(pctp->RunHeader);
7344       if ( !rh || rh->IsZombie() ) throw -17;      TTree *rh=(TTree*)file->Get("RunHeader");
7345       if( rh->GetEntries() == irun ){      if ( !rh || rh->IsZombie() ) throw -17;
7346            if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;      if( rh->GetEntries() <= irun ){
7347           return 0; // :-(          if ( IsDebug() ) cout << " *** MISSING-PKT *** no runs after calib (1) -- cannot validate :-( "<<endl;
7348       }          return 0; // :-(
7349        }
7350       RunHeaderEvent *run  = 0;      RunHeaderEvent *run  = 0;
7351       EventHeader    *hrun = 0;      EventHeader    *hrun = 0;
7352       rh->SetBranchAddress("RunHeader", &run);      rh->SetBranchAddress("RunHeader", &run);
7353       rh->SetBranchAddress("Header", &hrun);      rh->SetBranchAddress("Header", &hrun);
7354       rh->GetEntry(irun);      rh->GetEntry(irun);    
7355  //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;      if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
7356            if ( IsDebug() ) cout << " *** MISSING-PKT *** no runs after calib (2) -- cannot validate :-( "<<endl;
7357       if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){          return 0; // :-(
7358            if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;      }
7359           return 0; // :-(  
7360       }      UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
7361        if( dtime > timeaftercalib ){
7362            if ( IsDebug() ) cout << " *** MISSING-PKT *** run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
7363            return 0; // :-(
7364        }
7365    
7366        if ( IsDebug() ) cout <<endl<< " ACQ_BUILD_INFO ==> "<<hex<<(run->ACQ_BUILD_INFO & mask)<<dec;
7367    
7368        if( (run->ACQ_BUILD_INFO & mask) != build ){
7369            validate=0; // :-(
7370            cout <<endl<< " *** MISSING-PKT *** packet mismatch: ACQ_BUILD_INFO="<<hex<<(run->ACQ_BUILD_INFO&mask)<<" != "<<build<<dec;
7371        };
7372    
7373        return validate;
7374    
7375    
7376    }
7377    // UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
7378    
7379    //     if(!caltrk || !eh) return 0;
7380    
7381    //   Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
7382    //   UInt_t timeaftercalib=120000; //2000;
7383    //   //  ----------
7384    //   //  Check CRCs
7385    //   //  ----------
7386    //   for(Int_t ipkt=0; ipkt<6; ipkt++){
7387    //     if( caltrk->crc_hcal[ipkt] )return 0; // :-(
7388    //     for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
7389    //   }
7390    //   //  -----------------------
7391    //   //  Check missing packets:
7392    //   //  -----------------------
7393    //   //    Readout order:
7394    //   //    ------------------
7395    //   //    DSP   packet board
7396    //   //    ------------------
7397    //   //    12    0      1
7398    //   //    10    1      1
7399    //   //     8    2      1
7400    //   //     4    3      1
7401    //   //     6    4      1
7402    //   //     2    5      1
7403    //   //    ------------------
7404    //   //    11    0      2
7405    //   //     9    1      2
7406    //   //     7    2      2
7407    //   //     3    3      2
7408    //   //     5    4      2
7409    //   //     1    5      2
7410    //   //    ------------------
7411    //   //  -------------------------------------------------
7412    //   //  Check if it is first or second calibration packet
7413    //   //  -------------------------------------------------
7414    //   UInt_t build=0;
7415    //   TString classname = caltrk->GetName();
7416    //   UInt_t base=0;
7417    //   UInt_t mask=0;
7418    //   if(classname.Contains("CalibTrk1Event")){
7419    //     base=12;
7420    //     mask=0x03F000;
7421    //   }
7422    //   if(classname.Contains("CalibTrk2Event")){
7423    //     base=18;
7424    //     mask=0xFC0000;
7425    //   }
7426    //   //  -------------------------------------------------
7427    //   //  Count number of packets and set build variable
7428    //   //  -------------------------------------------------
7429    //   Int_t  npkts=0;
7430    //   for(Int_t ipkt=0; ipkt<6; ipkt++){
7431    //     if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
7432    //       npkts++;
7433    //       build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
7434    //     }
7435    //   }
7436    //   //    if( npkts==6 )return 1; // :-)
7437    
7438    //   //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
7439    
7440    //   //  -----------------------------------------------
7441    //   //  If missing packets: check the acq configuration
7442    //   //  (some DSPs might be excluded from acquisition)
7443    //   //  -----------------------------------------------
7444    
7445    //   //  -----------------------------------------------
7446    //   //  retrieve the first run header after calib
7447    //   //  -----------------------------------------------
7448    //   PacketType *pctp;
7449    //   EventCounter *cod;
7450    //   cod = eh->GetCounter();
7451    //   Int_t irun = cod->Get(pctp->RunHeader);
7452    //   TTree *rh=(TTree*)file->Get("RunHeader");
7453    //   if ( !rh || rh->IsZombie() ) throw -17;
7454    //   if( rh->GetEntries() == irun ){
7455    //     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;
7456    //     return 0; // :-(
7457    //   }
7458    
7459    //   RunHeaderEvent *run  = 0;
7460    //   EventHeader    *hrun = 0;
7461    //   rh->SetBranchAddress("RunHeader", &run);
7462    //   rh->SetBranchAddress("Header", &hrun);
7463    //   rh->GetEntry(irun);
7464    //   //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
7465    
7466    //   if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
7467    //     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
7468    //     return 0; // :-(
7469    //   }
7470            
7471       if( !run->RM_ACQ_AFTER_CALIB ){  //   if( !run->RM_ACQ_AFTER_CALIB ){
7472            if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0    -- cannot validate :-( "<<endl;  //     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0    -- cannot validate :-( "<<endl;
7473           return 0; // :-(  //     return 0; // :-(
7474       }  //   }
7475    
7476       UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());  //   UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
7477       if( dtime > timeaftercalib ){  //   if( dtime > timeaftercalib ){
7478           if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;  //     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
7479           return 0; // :-(  //     return 0; // :-(
7480       }  //   }
7481            
7482    
7483    
7484       if( (run->ACQ_BUILD_INFO & mask) != build ){  //   if( (run->ACQ_BUILD_INFO & mask) != build ){
7485           if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;  //     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;
7486           return 0; // :-(  //     return 0; // :-(
7487       }  //   }
7488       return 1; // :-)  //   return 1; // :-)
7489    
7490  }  // }
7491    
7492  /**  /**
7493   *   *
# Line 6378  UInt_t PamelaDBOperations::ValidateTrkCa Line 7495  UInt_t PamelaDBOperations::ValidateTrkCa
7495   *   *
7496   **/   **/
7497  UInt_t PamelaDBOperations::Check(){  UInt_t PamelaDBOperations::Check(){
7498          return(this->Check(0,0));    return(this->Check(0,0));
7499  }  }
7500    
7501  UInt_t PamelaDBOperations::Check(UInt_t from, UInt_t to){  UInt_t PamelaDBOperations::Check(UInt_t from, UInt_t to){
7502    //    //
7503          if ( IsDebug() ) printf(" from %u to %u \n",from,to);    if ( IsDebug() ) printf(" from %u to %u \n",from,to);
7504    //    //
7505    UInt_t test = 0;    UInt_t test = 0;
7506    //    //
# Line 6424  UInt_t PamelaDBOperations::Check(UInt_t Line 7541  UInt_t PamelaDBOperations::Check(UInt_t
7541      //      //
7542      if ( from > 0 && nid <= from ) goto ss;      if ( from > 0 && nid <= from ) goto ss;
7543      if ( to > 0 && nid >= to ) goto ss;      if ( to > 0 && nid >= to ) goto ss;
7544  //      //
7545      if ( (UInt_t)atoll(row->GetField(4)) > 1 ){      if ( (UInt_t)atoll(row->GetField(4)) > 1 ){
7546        //        //
7547        //        //
# Line 6455  UInt_t PamelaDBOperations::Check(UInt_t Line 7572  UInt_t PamelaDBOperations::Check(UInt_t
7572              printf(" CHECK n.4 RUNs %u and %u HAVE SAME RUNHEADER \n",thisid,(UInt_t)atoll(row3->GetField(0)));                printf(" CHECK n.4 RUNs %u and %u HAVE SAME RUNHEADER \n",thisid,(UInt_t)atoll(row3->GetField(0)));  
7573              row3 = result3->Next();              row3 = result3->Next();
7574            };            };
7575  //      delete result3;            //    delete result3;
7576    
7577          };          };
7578          //          //
# Line 6476  UInt_t PamelaDBOperations::Check(UInt_t Line 7593  UInt_t PamelaDBOperations::Check(UInt_t
7593              printf(" CHECK n.5 RUNs %u and %u HAVE SAME RUNTRAILER \n",thisid,(UInt_t)atoll(row3->GetField(0)));              printf(" CHECK n.5 RUNs %u and %u HAVE SAME RUNTRAILER \n",thisid,(UInt_t)atoll(row3->GetField(0)));
7594              row3 = result3->Next();              row3 = result3->Next();
7595            };            };
7596  //      delete result3;            //    delete result3;
7597          };          };
7598          //          //
7599          oss.str("");          oss.str("");
# Line 6496  UInt_t PamelaDBOperations::Check(UInt_t Line 7613  UInt_t PamelaDBOperations::Check(UInt_t
7613              printf(" CHECK n.7 RUNs %u and %u HAVE SAME RUNTRAILER AND RUNHEADER (ARE THE SAME?) \n",thisid,(UInt_t)atoll(row3->GetField(0)));              printf(" CHECK n.7 RUNs %u and %u HAVE SAME RUNTRAILER AND RUNHEADER (ARE THE SAME?) \n",thisid,(UInt_t)atoll(row3->GetField(0)));
7614              row3 = result3->Next();              row3 = result3->Next();
7615            };            };
7616   //     delete result3;            //    delete result3;
7617    
7618          };          };
7619        };        };
# Line 6517  UInt_t PamelaDBOperations::Check(UInt_t Line 7634  UInt_t PamelaDBOperations::Check(UInt_t
7634            printf(" CHECK n.6 RUN %u CONTAINS RUN %u  \n",thisid,(UInt_t)atoll(row3->GetField(0)));            printf(" CHECK n.6 RUN %u CONTAINS RUN %u  \n",thisid,(UInt_t)atoll(row3->GetField(0)));
7635            row3 = result3->Next();            row3 = result3->Next();
7636          };          };
7637  //      delete result3;          //      delete result3;
7638        };        };
7639        //        //
7640      };      };
# Line 6543  UInt_t PamelaDBOperations::Check(UInt_t Line 7660  UInt_t PamelaDBOperations::Check(UInt_t
7660        thisf = (TString)row2->GetField(0);        thisf = (TString)row2->GetField(0);
7661        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
7662        test = 1;        test = 1;
7663  //      delete result2;        //        delete result2;
7664      };      };
7665      //      //
7666      if ( (thisrtt < prevrht) && (thisrht != prevrht) ){      if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
# Line 6565  UInt_t PamelaDBOperations::Check(UInt_t Line 7682  UInt_t PamelaDBOperations::Check(UInt_t
7682        thisf = (TString)row2->GetField(0);        thisf = (TString)row2->GetField(0);
7683        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
7684        test = 1;        test = 1;
7685  //      delete result2;        //        delete result2;
7686      };      };
7687      //      //
7688      if ( (thisrht > thisrtt) && (thisrht != prevrht) ){      if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
# Line 6587  UInt_t PamelaDBOperations::Check(UInt_t Line 7704  UInt_t PamelaDBOperations::Check(UInt_t
7704        thisf = (TString)row2->GetField(0);        thisf = (TString)row2->GetField(0);
7705        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
7706        test = 1;        test = 1;
7707  //      delete result2;        //        delete result2;
7708      };      };
7709  ss:    ss:
7710      //      //
7711      prevrht = thisrht;      prevrht = thisrht;
7712      prevrtt = thisrtt;      prevrtt = thisrtt;
7713      previd = thisid;      previd = thisid;
7714      prevl0id = thisl0id;      prevl0id = thisl0id;
7715      row = result->Next();      row = result->Next();
7716  //    if ( result2 ) delete result2;      //    if ( result2 ) delete result2;
7717  //    if ( result3 ) delete result3;      //    if ( result3 ) delete result3;
7718    };    };
7719    //    //
7720    return(test);    return(test);

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.4

  ViewVC Help
Powered by ViewVC 1.1.23