/[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 by mocchiut, Tue Sep 23 07:20:33 2008 UTC revision 1.3 by mocchiut, Thu Sep 25 15:30:10 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 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 2061  Int_t PamelaDBOperations::insertPamelaRU Line 2113  Int_t PamelaDBOperations::insertPamelaRU
2113            cod = ehh->GetCounter();            cod = ehh->GetCounter();
2114            tcod = (UInt_t)cod->Get(pctp->Physics);            tcod = (UInt_t)cod->Get(pctp->Physics);
2115            evbefh = TMath::Max(chminentry,tcod);            evbefh = TMath::Max(chminentry,tcod);
2116  //        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
2117            if ( (UInt_t)evbefh == upperentry || !upperentry ) evbefh = upperentry + 1;            if ( (UInt_t)evbefh == upperentry || !upperentry ) evbefh = upperentry + 1;
2118  //            //
2119          if ( debug ) printf(" evbefh %i upperentry %u \n",evbefh,upperentry);            if ( debug ) printf(" evbefh %i upperentry %u \n",evbefh,upperentry);
2120            //            //
2121            this->HandleRunFragments(false,true,evbefh,upperentry);            this->HandleRunFragments(false,true,evbefh,upperentry);
2122            //            //
# Line 2367  Bool_t PamelaDBOperations::IsRunAlreadyI Line 2419  Bool_t PamelaDBOperations::IsRunAlreadyI
2419        //        //
2420        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");
2421        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2422  //       oss.str("");        //       oss.str("");
2423  //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2424  //       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());      
2425  //       conn->Query(oss.str().c_str());        //       conn->Query(oss.str().c_str());
2426        if ( signal ) signal = false;        if ( signal ) signal = false;
2427        goto gonext;              goto gonext;      
2428        //        //
# Line 2385  Bool_t PamelaDBOperations::IsRunAlreadyI Line 2437  Bool_t PamelaDBOperations::IsRunAlreadyI
2437        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");
2438        //        //
2439        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2440  //       oss.str("");        //       oss.str("");
2441  //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2442  //       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());      
2443  //       conn->Query(oss.str().c_str());        //       conn->Query(oss.str().c_str());
2444        //        //
2445        if ( signal ) signal = false;        if ( signal ) signal = false;
2446        goto gonext;        goto gonext;
# Line 2403  Bool_t PamelaDBOperations::IsRunAlreadyI Line 2455  Bool_t PamelaDBOperations::IsRunAlreadyI
2455        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");
2456        //        //
2457        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2458  //       oss.str("");        //       oss.str("");
2459  //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2460  //       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());      
2461  //       conn->Query(oss.str().c_str());        //       conn->Query(oss.str().c_str());
2462        if ( signal ) signal = false;        if ( signal ) signal = false;
2463        //        //
2464      };      };
# Line 2488  void PamelaDBOperations::HandleRunFragme Line 2540  void PamelaDBOperations::HandleRunFragme
2540      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");
2541      //      //
2542    } 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 ){  
2543      //      //
2544      // 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
2545      //      //
2546      if ( IsDebug() ) printf(" The run is new \n");      if ( rhfirstev != firstev && !mishead ) mishead = true;
2547      if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");      if ( rtlastev != lastev && !mistrail ) mistrail = true;
2548      //      //
2549      glrun->SetID(this->AssignRunID());      this->FillClass(mishead,mistrail,firstev,lastev);
     glrun->SetID_RUN_FRAG(0);  
     glrun->Fill_GL_RUN_FRAGMENTS(conn);  
2550      //      //
2551    } else {      if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
2552      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());
2553      if ( PEDANTIC ) throw -69;      //
2554      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  
2555      //      //
2556      oss.str("");      oss.str("");
2557      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 "
2558          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2559          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "          << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2560          << " ID != " << glrun->ID          << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2561          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2562            << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2563            << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2564            << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2565            << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2566            << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2567            << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2568            << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2569            << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2570            << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2571      //      //
2572      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());
2573      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
2574      //      //
2575      if ( !result ) throw -4;      if ( !result ) throw -4;
2576      //      //
2577      row = result->Next();      row = result->Next();
2578      //      //
2579      if ( !row && NoFrag() ){      if ( !row ){
2580          //
2581          // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
2582          //
2583          if ( IsDebug() ) printf(" The run is new \n");
2584          if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
2585          //
2586          glrun->SetID(this->AssignRunID());
2587          glrun->SetID_RUN_FRAG(0);
2588          glrun->Fill_GL_RUN_FRAGMENTS(conn);
2589          //
2590        } else {
2591          if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
2592          if ( PEDANTIC ) throw -69;
2593          return;
2594        };
2595        //
2596        if ( chewbacca && mishead && mistrail ) goto justcheck;
2597        //
2598        // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
2599        //    
2600        if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
2601          // missing it no way we can found a piece in the frag table
2602        //        //
2603        oss.str("");        oss.str("");
2604        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 "
2605            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2606            << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "            << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2607            << " ID != " << glrun->ID            << " ID != " << glrun->ID
2608            << " 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!!
2609        //        //
2610        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());
2611        result = conn->Query(oss.str().c_str());        result = conn->Query(oss.str().c_str());
2612        //        //
2613        if ( !result ) throw -4;        if ( !result ) throw -4;
2614        //        //
       foundinrun = true;  
       //  
2615        row = result->Next();        row = result->Next();
2616        //        //
2617      };        if ( !row && NoFrag() ){
2618      //          //
2619      if ( !row ){          oss.str("");
2620        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");          oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
2621        found = false;              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2622      } else {              << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2623        //              << " ID != " << glrun->ID
2624        found = false; // default value              << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
2625        //          //
2626        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());
2627        //          result = conn->Query(oss.str().c_str());
2628        // if we have both runheader and runtrailer we can check with pkt_counter:          //
2629            if ( !result ) throw -4;
2630            //
2631            foundinrun = true;
2632            //
2633            row = result->Next();
2634            //
2635          };
2636        //        //
2637        if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){        if ( !row ){
2638          ULong64_t chkpkt = 0;            if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2639          ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());          found = false;
2640          ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));        } else {
2641            //
2642            found = false; // default value
2643            //
2644            if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
2645          //          //
2646          chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;            // if we have both runheader and runtrailer we can check with pkt_counter:
2647          //          //
2648          if ( labs(chkpkt-pktt)<2 ){          if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
2649              ULong64_t chkpkt = 0;  
2650              ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
2651              ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
2652            //            //
2653            if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);            chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;  
2654            //            //
2655            found = true;            if ( labs(chkpkt-pktt)<2 ){
2656                //
2657                if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2658                //
2659                found = true;
2660                //
2661              } else {
2662                //
2663                if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);            
2664                //
2665                found = false;
2666                //
2667              };
2668            };
2669            if ( !found && chewbacca ) goto justcheck;
2670            if ( !found ){
2671            //            //
2672          } 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
2673            //            //
2674            if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);                  ULong64_t chkpkt1 = 0;
2675              ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2676              ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2677              chkpkt1 = labs(orunh1-dbrunt1);
2678            //            //
2679            found = false;            ULong64_t chkpkt2 = 0;
2680              ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
2681              ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2682              chkpkt2 = labs(orunh2-dbrunt2);
2683              //
2684              ULong64_t chkpkt3 = 0;
2685              ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
2686              ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2687              chkpkt3 = labs(orunh3-dbrunt3);
2688            //            //
2689              if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2690                //  if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
2691                //
2692                if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2693                //
2694                found = true;
2695                //
2696              } else {
2697                //
2698                if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2699                //
2700                found = false;
2701                //
2702              };
2703          };          };
2704        };        };
2705        if ( !found && chewbacca ) goto justcheck;        //
2706        if ( !found ){        if ( found ){
2707            //
2708            // 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
2709            //
2710            if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2711            //
2712            if ( foundinrun ){
2713              glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2714              glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2715            };
2716          //          //
2717          // 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();
2718          //          //
2719          ULong64_t chkpkt1 = 0;          //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
         ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());  
         ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));  
         chkpkt1 = labs(orunh1-dbrunt1);  
         //  
         ULong64_t chkpkt2 = 0;  
         ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());  
         ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));  
         chkpkt2 = labs(orunh2-dbrunt2);  
         //  
         ULong64_t chkpkt3 = 0;  
         ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());  
         ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));  
         chkpkt3 = labs(orunh3-dbrunt3);  
2720          //          //
2721          if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){          oss.str("");
2722            //    if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){          oss << " ID="<<row->GetField(0)<<";";
2723            //          //
2724            if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);          glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
2725            //          //
2726            found = true;          // merge infos
2727            //          //
2728          } else {          UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
2729            ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
2730            UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
2731            ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
2732            if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2733            TTree *T= 0;
2734            T = (TTree*)file->Get("Physics");
2735            if ( !T || T->IsZombie() ) throw -16;
2736            EventHeader *eh = 0;
2737            PscuHeader *ph = 0;
2738            T->SetBranchAddress("Header", &eh);
2739            while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
2740              T->GetEntry(firstev);
2741              ph = eh->GetPscuHeader();
2742              bpkt = PKT(ph->GetCounter());
2743              bobt = OBT(ph->GetOrbitalTime());    
2744              firstev++;
2745              if ( PEDANTIC ) throw -71;
2746            };
2747            if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2748            //
2749            glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2750            glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
2751            glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
2752            glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
2753            glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
2754            //
2755            glrun->SetEV_FROM(firstev);
2756            glrun->SetNEVENTS(lastev-firstev+1);
2757            //
2758            glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
2759            glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
2760            glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
2761            glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
2762            glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
2763            glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
2764            glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
2765            glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
2766            glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
2767            glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
2768            glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());  
2769            glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());  
2770            glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());  
2771            glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());  
2772            glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());  
2773            glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());  
2774            //
2775            if ( glrun1->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glrun1->GetPHYSENDRUN_MASK_S3S2S12());
2776            if ( glrun1->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glrun1->GetPHYSENDRUN_MASK_S11CRC());
2777            //
2778            if ( !IsRunAlreadyInserted() ){
2779              //
2780              //    glrun->SetID(this->AssignRunID());
2781              glrun->SetID_RUN_FRAG(glrun1->GetID());
2782              glrun->Fill_GL_RUN(conn);
2783            //            //
2784            if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);            // set id number
2785            //            //
2786            found = false;            glrun1->SetID_RUN_FRAG(glrun->GetID());
2787              glrun1->Fill_GL_RUN(conn);
2788            //            //
2789          };          };
2790        };          // delete old entry in fragment table
2791      };          //
2792      //          glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2793      if ( found ){          glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
       //  
       // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table  
       //  
       if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");  
       //  
       if ( foundinrun ){  
         glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");  
         glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");  
       };  
       //  
       GL_RUN *glrun1 = new GL_RUN();  
       //  
       //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));  
       //  
       oss.str("");  
       oss << " ID="<<row->GetField(0)<<";";  
       //  
       glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos  
       //  
       // merge infos  
       //  
       UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());  
       ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());  
       UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());  
       ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());  
       if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);  
       TTree *T= 0;  
       T = (TTree*)file->Get("Physics");  
       if ( !T || T->IsZombie() ) throw -16;  
       EventHeader *eh = 0;  
       PscuHeader *ph = 0;  
       T->SetBranchAddress("Header", &eh);  
       while ( apkt > bpkt && aobt > bobt && firstev < lastev ){  
         T->GetEntry(firstev);  
         ph = eh->GetPscuHeader();  
         bpkt = PKT(ph->GetCounter());  
         bobt = OBT(ph->GetOrbitalTime());        
         firstev++;  
         if ( PEDANTIC ) throw -71;  
       };  
       if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);  
       //  
       glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());  
       glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());  
       glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());  
       glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());  
       glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());  
       //  
       glrun->SetEV_FROM(firstev);  
       glrun->SetNEVENTS(lastev-firstev+1);  
       //  
       glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());  
       glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());  
       glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());  
       glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());  
       glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());  
       glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());  
       glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());  
       glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());  
       glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());  
       glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());  
       glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());    
       glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());    
       glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());    
       glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());    
       glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());    
       glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());    
       //  
       if ( !IsRunAlreadyInserted() ){  
2794          //          //
2795          //      glrun->SetID(this->AssignRunID());          delete glrun1;
         glrun->SetID_RUN_FRAG(glrun1->GetID());  
         glrun->Fill_GL_RUN(conn);  
2796          //          //
         // set id number  
2797          //          //
2798          glrun1->SetID_RUN_FRAG(glrun->GetID());          return;
         glrun1->Fill_GL_RUN(conn);  
2799          //          //
2800        };        };
       // delete old entry in fragment table  
       //  
       glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");  
       glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");  
       //  
       delete glrun1;  
       //  
       //  
       return;  
2801        //        //
2802      };      };
2803      //      //
2804    };      if ( mistrail && ( rtlastev == lastev || chewbacca )) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
2805    //        // 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() ){  
2806        //        //
2807        oss.str("");        oss.str("");
2808        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 "
2809            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2810            << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "            << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2811            << " ID != " << glrun->ID            << " ID != " << glrun->ID
2812            << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";            << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2813        //        //
2814        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());
2815        result = conn->Query(oss.str().c_str());        result = conn->Query(oss.str().c_str());
2816        //        //
2817        if ( !result ) throw -4;        if ( !result ) throw -4;
2818        //        //
       foundinrun = true;  
2819        row = result->Next();        row = result->Next();
2820        //        //
2821      };        if ( !row && NoFrag() ){
2822      //          //
2823      if ( !row ){          oss.str("");
2824        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 "
2825        found = false;              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2826      } else {              << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2827        //              << " ID != " << glrun->ID
2828        found = false; // default value              << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2829        //          //
2830        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());
2831        //          result = conn->Query(oss.str().c_str());
2832        // if we have both runheader and runtrailer we can check with pkt_counter:          //
2833            if ( !result ) throw -4;
2834            //
2835            foundinrun = true;
2836            row = result->Next();
2837            //
2838          };
2839        //        //
2840        if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){        if ( !row ){
2841          ULong64_t chkpkt = 0;            if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2842          ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));          found = false;
2843          ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());        } else {
2844          //          //
2845          chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;            found = false; // default value
2846          //          //
2847          if ( labs(chkpkt-pktt)<2 ){          if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
2848            //
2849            // if we have both runheader and runtrailer we can check with pkt_counter:
2850            //
2851            if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){
2852              ULong64_t chkpkt = 0;  
2853              ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
2854              ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2855            //            //
2856            if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);            chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;  
2857            //            //
2858            found = true;            if ( labs(chkpkt-pktt)<2 ){
2859                //
2860                if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2861                //
2862                found = true;
2863                //
2864              } else {
2865                //
2866                if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);            
2867                //
2868                found = false;
2869                //
2870              };
2871            };
2872            if ( !found && chewbacca ) goto justcheck;
2873            if ( !found ){
2874            //            //
2875          } 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
2876            //            //
2877            if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);                  ULong64_t chkpkt1 = 0;
2878              ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
2879              ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2880              chkpkt1 = labs(orunh1-dbrunt1);
2881            //            //
2882            found = false;            ULong64_t chkpkt2 = 0;
2883              ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT());
2884              ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2885              chkpkt2 = labs(orunh2-dbrunt2);
2886            //            //
2887              ULong64_t chkpkt3 = 0;
2888              ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME());
2889              ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2890              chkpkt3 = labs(orunh3-dbrunt3);
2891              //
2892              if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2893                //
2894                if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2895                //
2896                found = true;
2897                //
2898              } else {
2899                //
2900                if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2901                //
2902                found = false;
2903                //
2904              };
2905          };          };
2906        };        };
2907        if ( !found && chewbacca ) goto justcheck;        //
2908        if ( !found ){        if ( found ){
2909            //
2910            // 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
2911            //
2912            if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2913            //
2914            if ( foundinrun ){
2915              glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2916              glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2917            };
2918          //          //
2919          // 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();
2920          //          //
2921          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);  
2922          //          //
2923          ULong64_t chkpkt2 = 0;          oss.str("");
2924          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);  
2925          //          //
2926          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);  
2927          //          //
2928          if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){          // merge infos
2929            //          //
2930            if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);          UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT());
2931            ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
2932            UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
2933            ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
2934            if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2935            TTree *T= 0;
2936            T = (TTree*)file->Get("Physics");
2937            if ( !T || T->IsZombie() ) throw -16;
2938            EventHeader *eh = 0;
2939            PscuHeader *ph = 0;
2940            T->SetBranchAddress("Header", &eh);
2941            while ( apkt > bpkt && aobt > bobt && lastev > 0 ){
2942              T->GetEntry(lastev);
2943              ph = eh->GetPscuHeader();
2944              apkt = PKT(ph->GetCounter());
2945              aobt = OBT(ph->GetOrbitalTime());    
2946              lastev--;
2947              if ( PEDANTIC ) throw -72;
2948            };
2949            if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2950            //
2951            glrun->SetEV_TO(lastev);
2952            glrun->SetNEVENTS(lastev-firstev+1);
2953            glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());
2954            glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER());
2955            glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME());
2956            glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
2957            glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
2958            //
2959            glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
2960            glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
2961            glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
2962            glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP());
2963            glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE());
2964            glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE());
2965            glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A());
2966            glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B());
2967            glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO());
2968            glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO());
2969            glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB());  
2970            glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE());  
2971            glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED());  
2972            glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK());  
2973            glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC());  
2974            glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC());  
2975            //
2976            if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ) glrun1->SetPHYSENDRUN_MASK_S3S2S12(glrun->GetPHYSENDRUN_MASK_S3S2S12());
2977            if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) glrun1->SetPHYSENDRUN_MASK_S11CRC(glrun->GetPHYSENDRUN_MASK_S11CRC());
2978            //
2979            if ( !IsRunAlreadyInserted() ){
2980              //
2981              //    glrun->SetID(this->AssignRunID());
2982            //            //
2983            found = true;            glrun->SetID_RUN_FRAG(glrun1->GetID());
2984              glrun->Fill_GL_RUN(conn);
2985            //            //
2986          } else {            // set id number
2987            //            //
2988            if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);            glrun1->SetID_RUN_FRAG(glrun->GetID());
2989            //            glrun1->Fill_GL_RUN(conn);
           found = false;  
2990            //            //
2991          };          };
2992            //
2993            // delete old entries in fragment table
2994            //
2995            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2996            glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2997            //
2998            delete glrun1;
2999            //
3000            return;
3001            //
3002        };        };
3003          //
3004      };      };
3005      //      //
3006      if ( found ){    justcheck:
3007        //      //
3008        // 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");  
       };  
3009        //        //
3010        GL_RUN *glrun1 = new GL_RUN();        if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
3011        //        //
3012        //      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?
3013        //        //
3014        oss.str("");        oss.str("");
3015        oss << " ID="<<row->GetField(0)<<";";        oss << " SELECT ID FROM GL_RUN WHERE "
3016              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3017              << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3018              << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3019              << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3020              << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3021              << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3022              << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3023              << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3024              << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3025              << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3026              << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3027              << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3028              << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3029        //        //
3030        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());
3031          result = conn->Query(oss.str().c_str());
3032        //        //
3033        // merge infos        if ( !result ) throw -4;
3034        //        //
3035        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());    
3036        //        //
3037        if ( !IsRunAlreadyInserted() ){        if ( row ){
3038          //          if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
3039          //      glrun->SetID(this->AssignRunID());          if ( PEDANTIC ) throw -70;
3040          //        } else {
3041          glrun->SetID_RUN_FRAG(glrun1->GetID());          if ( NoFrag() ){
3042          glrun->Fill_GL_RUN(conn);            glrun->SetID_RUN_FRAG(glrun->GetID());
3043          //            glrun->Fill_GL_RUN(conn);    
3044          // set id number            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3045          //          };    
         glrun1->SetID_RUN_FRAG(glrun->GetID());  
         glrun1->Fill_GL_RUN(conn);  
         //  
3046        };        };
       //  
       // 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");  
       };      
3047      };      };
   };  
3048    }; // EEE    }; // EEE
3049    //    //
3050    return;    return;
# Line 3068  Bool_t PamelaDBOperations::IsRunConsiste Line 3126  Bool_t PamelaDBOperations::IsRunConsiste
3126      code = eh->GetCounter();      code = eh->GetCounter();
3127      checkfirst = 0;      checkfirst = 0;
3128      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3129         if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
3130       };      };
3131      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);
3132      //      //
3133      T->GetEntry(lastev);      T->GetEntry(lastev);
3134      code = eh->GetCounter();      code = eh->GetCounter();
3135      checklast = 0;      checklast = 0;
3136      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3137         if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
3138       };      };
3139      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);
3140      //      //
3141      if ( checkfirst == checklast ){      if ( checkfirst == checklast ){
# Line 3092  Bool_t PamelaDBOperations::IsRunConsiste Line 3150  Bool_t PamelaDBOperations::IsRunConsiste
3150        //        //
3151        // HERE WE MUST HANDLE THAT RUNS AND GO BACK        // HERE WE MUST HANDLE THAT RUNS AND GO BACK
3152        //        //
3153  //      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");
3154        //        //
3155        Bool_t emptyruns = false;        Bool_t emptyruns = false;
3156        UInt_t check = 0;        UInt_t check = 0;
# Line 3178  Bool_t PamelaDBOperations::IsRunConsiste Line 3236  Bool_t PamelaDBOperations::IsRunConsiste
3236                lastObt = firstObt;                lastObt = firstObt;
3237                lastPkt = firstPkt;                lastPkt = firstPkt;
3238              };              };
3239              glrun->SetRUNTRAILER_PKT(lastPkt);              glrun->SetRUNTRAILER_PKT(lastPkt);
3240              glrun->SetRUNTRAILER_OBT(lastObt);              glrun->SetRUNTRAILER_OBT(lastObt);
3241              lastentry++;              lastentry++;
3242            };            };
3243            //                  //      
3244            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
3245            //            this->SetPhysEndRunVariables();
3246            if ( chminentry == firstentry  ){ // EEE            //
3247              if ( IsDebug() ) printf(" Inside isrunconsistent found a fragment of run at the beginning of the file, put it in the fragment table \n");            if ( chminentry == firstentry  ){ // EEE
3248  //              if ( IsDebug() ) printf(" Inside isrunconsistent found a fragment of run at the beginning of the file, put it in the fragment table \n");
3249  //          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              //
3250  //              //      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
3251                //
3252    
3253    
3254    
3255    
3256    
3257    
3258    
3259    
3260    
3261    
3262    
3263    
3264    mishead = true;  
3265                mishead = true;
3266    
3267    UInt_t rhfirstev = firstentry;  
3268  //  UInt_t rtlastev = lastentry;              UInt_t rhfirstev = firstentry;
3269    Bool_t found = false;              //  UInt_t rtlastev = lastentry;
3270    Bool_t foundinrun = false;              Bool_t found = false;
3271    //              Bool_t foundinrun = false;
3272    TSQLResult *result = 0;              //
3273    TSQLRow    *row    = 0;              TSQLResult *result = 0;
3274    //              TSQLRow    *row    = 0;
3275    stringstream oss;              //
3276    oss.str("");              stringstream oss;
3277    //              oss.str("");
3278    // we have now the good first piece of a run, fill the glrun object              //
3279    //              // we have now the good first piece of a run, fill the glrun object
3280  //  if ( rhfirstev != firstev && !mishead ) mishead = true;              //
3281  //  if ( rtlastev != lastev && !mistrail ) mistrail = true;              //  if ( rhfirstev != firstev && !mishead ) mishead = true;
3282    //              //  if ( rtlastev != lastev && !mistrail ) mistrail = true;
3283  //  this->FillClass(mishead,mistrail,firstev,lastev);              //
3284    //              //  this->FillClass(mishead,mistrail,firstev,lastev);
3285    if ( IsDebug() ) printf("zz The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");              //
3286    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 The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
3287    //              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());
3288    // First of all insert the run in the fragment table...              //
3289    //              // First of all insert the run in the fragment table...
3290    oss.str("");              //
3291    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "              oss.str("");
3292        << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("              oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
3293        << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "                  << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3294        << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("                  << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3295        << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "                  << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3296        << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("                  << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3297        << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "                  << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3298        << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "                  << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3299        << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                  << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3300        << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("                  << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3301        << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "                  << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3302        << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("                  << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3303        << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "                  << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3304        << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";                  << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3305    //                  << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3306    if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());              //
3307    result = conn->Query(oss.str().c_str());              if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3308    //              result = conn->Query(oss.str().c_str());
3309    if ( !result ) throw -4;              //
3310    //              if ( !result ) throw -4;
3311    row = result->Next();              //
3312    //              row = result->Next();
3313    if ( !row ){              //
3314      //              if ( !row ){
3315      // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)                //
3316      //                // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
3317      if ( IsDebug() ) printf(" The run is new \n");                //
3318      if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");                if ( IsDebug() ) printf(" The run is new \n");
3319      //                if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
3320      glrun->SetID(this->AssignRunID());                //
3321      glrun->SetID_RUN_FRAG(0);                glrun->SetID(this->AssignRunID());
3322      glrun->Fill_GL_RUN_FRAGMENTS(conn);                glrun->SetID_RUN_FRAG(0);
3323      //                glrun->Fill_GL_RUN_FRAGMENTS(conn);
3324    } else {                //
3325      if ( IsDebug() ) printf(" The run is already present in the fragment table \n");              } else {
3326      if ( PEDANTIC ) throw -69;                if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
3327  //    return;                if ( PEDANTIC ) throw -69;
3328    };                //    return;
3329    //              };
3330    if ( chewbacca && mishead && mistrail ) goto zjustcheck;              //
3331    //              if ( chewbacca && mishead && mistrail ) goto zjustcheck;
3332    // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?              //
3333    //              // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
3334    if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is              //
3335                                             // missing it no way we can found a piece in the frag 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
3336      //                // missing it no way we can found a piece in the frag table
3337      oss.str("");                //
3338      oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "                oss.str("");
3339          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "                oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
3340          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                    << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3341          << " ID != " << glrun->ID                    << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3342          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!                    << " ID != " << glrun->ID
3343      //                    << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
3344      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());                //
3345      result = conn->Query(oss.str().c_str());                if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
3346      //                result = conn->Query(oss.str().c_str());
3347      if ( !result ) throw -4;                //
3348      //                if ( !result ) throw -4;
3349      row = result->Next();                //
3350      //                row = result->Next();
3351      if ( !row && NoFrag() ){                //
3352        //                if ( !row && NoFrag() ){
3353        oss.str("");                  //
3354        oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "                  oss.str("");
3355            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "                  oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
3356            << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                      << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3357            << " ID != " << glrun->ID                      << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3358            << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!                      << " ID != " << glrun->ID
3359        //                      << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
3360        if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());                  //
3361        result = conn->Query(oss.str().c_str());                  if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
3362        //                  result = conn->Query(oss.str().c_str());
3363        if ( !result ) throw -4;                  //
3364        //                  if ( !result ) throw -4;
3365        foundinrun = true;                  //
3366        //                  foundinrun = true;
3367        row = result->Next();                  //
3368        //                  row = result->Next();
3369      };                  //
3370      //                };
3371      if ( !row ){                //
3372        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");                if ( !row ){
3373        found = false;                  if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
3374      } else {                  found = false;
3375        //                } else {
3376        found = false; // default value                  //
3377        //                  found = false; // default value
3378        if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");                  //
3379        //                  if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
3380        // if we have both runheader and runtrailer we can check with pkt_counter:                  //
3381        //                  // if we have both runheader and runtrailer we can check with pkt_counter:
3382        if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){                  //
3383          ULong64_t chkpkt = 0;                  if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
3384          ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());                    ULong64_t chkpkt = 0;
3385          ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));                    ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
3386          //                    ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
3387          chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;                    //
3388          //                    chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;
3389          if ( labs(chkpkt-pktt)<2 ){                    //
3390            //                    if ( labs(chkpkt-pktt)<2 ){
3391            if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);                      //
3392            //                      if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
3393            found = true;                      //
3394            //                      found = true;
3395          } else {                      //
3396            //                    } else {
3397            if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);                      //
3398            //                      if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
3399            found = false;                      //
3400            //                      found = false;
3401          };                      //
3402        };                    };
3403        if ( !found && chewbacca ) goto zjustcheck;                  };
3404        if ( !found ){                  if ( !found && chewbacca ) goto zjustcheck;
3405          //                  if ( !found ){
3406          // 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                    //
3407          //                    // 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
3408          ULong64_t chkpkt1 = 0;                    //
3409          ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());                    ULong64_t chkpkt1 = 0;
3410          ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));                    ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
3411          chkpkt1 = labs(orunh1-dbrunt1);                    ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
3412          //                    chkpkt1 = labs(orunh1-dbrunt1);
3413          ULong64_t chkpkt2 = 0;                    //
3414          ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());                    ULong64_t chkpkt2 = 0;
3415          ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));                    ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
3416          chkpkt2 = labs(orunh2-dbrunt2);                    ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
3417          //                    chkpkt2 = labs(orunh2-dbrunt2);
3418          ULong64_t chkpkt3 = 0;                    //
3419          ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());                    ULong64_t chkpkt3 = 0;
3420          ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));                    ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
3421          chkpkt3 = labs(orunh3-dbrunt3);                    ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
3422          //                    chkpkt3 = labs(orunh3-dbrunt3);
3423          if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){                    //
3424            //    if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){                    if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
3425            //                      //    if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
3426            if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);                      //
3427            //                      if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
3428            found = true;                      //
3429            //                      found = true;
3430          } else {                      //
3431            //                    } else {
3432            if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);                      //
3433            //                      if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
3434            found = false;                      //
3435            //                      found = false;
3436          };                      //
3437        };                    };
3438      };                  };
3439      //                };
3440      if ( found ){                //
3441        //                if ( found ){
3442        // 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                  //
3443        //                  // 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
3444        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");                  //
3445        //                  if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
3446        if ( foundinrun ){                  //
3447          glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");                  if ( foundinrun ){
3448          glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");                    glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3449        };                    glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
3450        //                  };
3451        GL_RUN *glrun1 = new GL_RUN();                  //
3452        //                  GL_RUN *glrun1 = new GL_RUN();
3453        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));                  //
3454        //                  //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
3455        oss.str("");                  //
3456        oss << " ID="<<row->GetField(0)<<";";                  oss.str("");
3457        //                  oss << " ID="<<row->GetField(0)<<";";
3458        glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos                  //
3459        //                  glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
3460        // merge infos                  //
3461        //                  // merge infos
3462        UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());                  //
3463        ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());                  UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
3464        UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());                  ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
3465        ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());                  UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
3466        if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);                  ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
3467        TTree *T= 0;                  if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
3468        T = (TTree*)file->Get("Physics");                  TTree *T= 0;
3469        if ( !T || T->IsZombie() ) throw -16;                  T = (TTree*)file->Get("Physics");
3470        EventHeader *eh = 0;                  if ( !T || T->IsZombie() ) throw -16;
3471        PscuHeader *ph = 0;                  EventHeader *eh = 0;
3472        T->SetBranchAddress("Header", &eh);                  PscuHeader *ph = 0;
3473        while ( apkt > bpkt && aobt > bobt && firstev < lastev ){                  T->SetBranchAddress("Header", &eh);
3474          T->GetEntry(firstev);                  while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
3475          ph = eh->GetPscuHeader();                    T->GetEntry(firstev);
3476          bpkt = PKT(ph->GetCounter());                    ph = eh->GetPscuHeader();
3477          bobt = OBT(ph->GetOrbitalTime());                    bpkt = PKT(ph->GetCounter());
3478          firstev++;                    bobt = OBT(ph->GetOrbitalTime());
3479          if ( PEDANTIC ) throw -71;                    firstev++;
3480        };                    if ( PEDANTIC ) throw -71;
3481        if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);                  };
3482        //                  if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
3483        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());                  //
3484        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());                  glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
3485        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());                  glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
3486        glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());                  glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
3487        glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());                  glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
3488        //                  glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
3489        glrun->SetEV_FROM(firstev);                  //
3490        glrun->SetNEVENTS(lastev-firstev+1);                  glrun->SetEV_FROM(firstev);
3491        //                  glrun->SetNEVENTS(lastev-firstev+1);
3492        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());                  //
3493        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());                  glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
3494        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());                  glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
3495        glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());                  glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
3496        glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());                  glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
3497        glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());                  glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
3498        glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());                  glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
3499        glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());                  glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
3500        glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());                  glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
3501        glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());                  glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
3502        glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());                  glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
3503        glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());                  glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());
3504        glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());                  glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());
3505        glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());                  glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());
3506        glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());                  glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());
3507        glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());                  glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());
3508        //                  glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());
3509        if ( !IsRunAlreadyInserted() ){                  //
3510          //                  if ( glrun1->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glrun1->GetPHYSENDRUN_MASK_S3S2S12());
3511          //      glrun->SetID(this->AssignRunID());                  if ( glrun1->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glrun1->GetPHYSENDRUN_MASK_S11CRC());
3512          glrun->SetID_RUN_FRAG(glrun1->GetID());                  //
3513          glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
3514          //                    //
3515          // set id number                    //      glrun->SetID(this->AssignRunID());
3516          //                    glrun->SetID_RUN_FRAG(glrun1->GetID());
3517          glrun1->SetID_RUN_FRAG(glrun->GetID());                    glrun->Fill_GL_RUN(conn);
3518          glrun1->Fill_GL_RUN(conn);                    //
3519          //                    // set id number
3520        };                    //
3521        // delete old entry in fragment table                    glrun1->SetID_RUN_FRAG(glrun->GetID());
3522        //                    glrun1->Fill_GL_RUN(conn);
3523        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");                    //
3524        glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");                  };
3525        //                  // delete old entry in fragment table
3526        delete glrun1;                  //
3527        //                  glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3528        //                  glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3529  //      return;                  //
3530        //                  delete glrun1;
3531      };                  //
3532      //                  //
3533    };                  //      return;
3534    //                  //
3535    //                };
3536   zjustcheck:                //
3537    //              };
3538    if ( !found ){              //
3539      //              //
3540      if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");            zjustcheck:
3541      //              //
3542      // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?              if ( !found ){
3543      //                //
3544      oss.str("");                if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
3545      oss << " SELECT ID FROM GL_RUN WHERE "                //
3546          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("                // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
3547          << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "                //
3548          << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("                oss.str("");
3549          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "                oss << " SELECT ID FROM GL_RUN WHERE "
3550          << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("                    << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3551          << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "                    << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3552          << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "                    << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3553          << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                    << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3554          << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("                    << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3555          << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "                    << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3556          << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("                    << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3557          << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "                    << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3558          << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";                    << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3559      //                    << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3560      if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());                    << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3561      result = conn->Query(oss.str().c_str());                    << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3562      //                    << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3563      if ( !result ) throw -4;                //
3564      //                if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3565      row = result->Next();                result = conn->Query(oss.str().c_str());
3566      //                //
3567      if ( row ){                if ( !result ) throw -4;
3568        if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");                //
3569        if ( PEDANTIC ) throw -70;                row = result->Next();
3570      } else {                //
3571        if ( NoFrag() ){                if ( row ){
3572          glrun->SetID_RUN_FRAG(glrun->GetID());                  if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
3573          glrun->Fill_GL_RUN(conn);                  if ( PEDANTIC ) throw -70;
3574          glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");                } else {
3575        };                  if ( NoFrag() ){
3576      };                    glrun->SetID_RUN_FRAG(glrun->GetID());
3577    }; // EEE                    glrun->Fill_GL_RUN(conn);
3578                      glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3579                    };
3580                  };
3581                }; // EEE
3582    
3583    
3584    
# Line 3551  Bool_t PamelaDBOperations::IsRunConsiste Line 3613  Bool_t PamelaDBOperations::IsRunConsiste
3613    
3614    
3615            } else {            } else {
3616            if ( !IsRunAlreadyInserted() ){              if ( !IsRunAlreadyInserted() ){
3617              glrun->SetID(this->AssignRunID());                glrun->SetID(this->AssignRunID());
3618              glrun->SetID_RUN_FRAG(0);                glrun->SetID_RUN_FRAG(0);
3619              glrun->Fill_GL_RUN(conn);                glrun->Fill_GL_RUN(conn);
3620            };              };
3621            }; // EEE            }; // EEE
3622            //            //
3623            firstevno = lastentry + 1;            firstevno = lastentry + 1;
# Line 3647  void PamelaDBOperations::HandleSuspiciou Line 3709  void PamelaDBOperations::HandleSuspiciou
3709      code = eh->GetCounter();      code = eh->GetCounter();
3710      checkfirst = 0;      checkfirst = 0;
3711      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3712         if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
3713         if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;        if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;
3714       };      };
3715      if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);      if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);
3716      //      //
3717      T->GetEntry(lastev);      T->GetEntry(lastev);
3718      code = eh->GetCounter();      code = eh->GetCounter();
3719      checklast = 0;      checklast = 0;
3720      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3721         if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
3722       };      };
3723      if ( IsDebug() ) printf(" Check last is %i \n",checklast);      if ( IsDebug() ) printf(" Check last is %i \n",checklast);
3724      //      //
3725      if ( checkfirst == checklast ){      if ( checkfirst == checklast ){
# Line 3769  void PamelaDBOperations::HandleSuspiciou Line 3831  void PamelaDBOperations::HandleSuspiciou
3831            };            };
3832            //                  //      
3833            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
3834              this->SetPhysEndRunVariables();
3835            //            //
3836            if ( !IsRunAlreadyInserted() ){            if ( !IsRunAlreadyInserted() ){
3837              glrun->SetID(this->AssignRunID());              glrun->SetID(this->AssignRunID());
# Line 3808  void PamelaDBOperations::HandleSuspiciou Line 3871  void PamelaDBOperations::HandleSuspiciou
3871              if ( IsDebug() ) printf(" We have the runtrailer \n");              if ( IsDebug() ) printf(" We have the runtrailer \n");
3872              //                  //    
3873              this->SetCommonGLRUN(firstTime,lastTime);              this->SetCommonGLRUN(firstTime,lastTime);
3874                this->SetPhysEndRunVariables();
3875              //              //
3876              if ( !IsRunAlreadyInserted() ){              if ( !IsRunAlreadyInserted() ){
3877                glrun->SetID(this->AssignRunID());                glrun->SetID(this->AssignRunID());
# Line 4580  Int_t PamelaDBOperations::insertTRK_CALI Line 4644  Int_t PamelaDBOperations::insertTRK_CALI
4644      pkt1 = ph1->GetCounter();        pkt1 = ph1->GetCounter();  
4645      fromtime = this->GetAbsTime(ph1->GetOrbitalTime());        fromtime = this->GetAbsTime(ph1->GetOrbitalTime());  
4646      //      //
4647  //     valid = 1;      //     valid = 1;
4648  //     //      //     //
4649  //     if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1      //     if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1
4650      //      //
4651      //      //
4652      if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){      if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){
# Line 4611  Int_t PamelaDBOperations::insertTRK_CALI Line 4675  Int_t PamelaDBOperations::insertTRK_CALI
4675            pkt2 = ph2->GetCounter();              pkt2 = ph2->GetCounter();  
4676            //            //
4677            if ( IsDebug() ) printf(" This is a trk calibration2 at obt %u pkt %u t2 is %u , t2t1cal is %u \n",obt2,pkt2,t2,t2t1cal);                  if ( IsDebug() ) printf(" This is a trk calibration2 at obt %u pkt %u t2 is %u , t2t1cal is %u \n",obt2,pkt2,t2,t2t1cal);      
4678  //        if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2            //      if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2
4679            //                  //      
4680          } else {          } else {
4681            //            //
# Line 4627  Int_t PamelaDBOperations::insertTRK_CALI Line 4691  Int_t PamelaDBOperations::insertTRK_CALI
4691          //      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->PKT(pkt2) > upperpkt) && (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){
4692    
4693          // EMILIANO          // EMILIANO
4694  //      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->PKT(pkt2) > upperpkt) || (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){
4695  //        //    if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){          //        //    if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
4696  //        if ( IsDebug() ) printf(" running out of vector without finding the corresponding calibration, sig \n");          //        if ( IsDebug() ) printf(" running out of vector without finding the corresponding calibration, sig \n");
4697  //        //          //        //
4698  //        // running out of vector without finding the corresponding calibration, sig          //        // running out of vector without finding the corresponding calibration, sig
4699  //        //          //        //
4700  //        pret2 = t2;          //        pret2 = t2;
4701  //        obt2 = 0;          //        obt2 = 0;
4702  //        //      pkt2 = pkt1+2;          //        //      pkt2 = pkt1+2;
4703  //        pkt2 = 0;          //        pkt2 = 0;
4704  //        t2t1cal = t1+1;          //        t2t1cal = t1+1;
4705  //      };          //      };
4706    
4707    
4708          //          //
# Line 4745  Int_t PamelaDBOperations::insertTRK_CALI Line 4809  Int_t PamelaDBOperations::insertTRK_CALI
4809      //      //
4810      fromtime = this->GetAbsTime(ph2->GetOrbitalTime());      fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
4811      valid = 0;      valid = 0;
4812  //  if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){      //  if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){
4813   // EMILIANO      // EMILIANO
4814      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->PKT(pkt2 <= upperpkt) && this->OBT(obt2) >= this->OBT(obtfirst) && this->OBT(obt2) <= upperobt ){
4815        //    if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){        //    if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){
4816        //        //
# Line 5061  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 5125  Int_t PamelaDBOperations::CleanGL_RUN_FR
5125                    << " RM_ACQ_SETTING_MODE=" << glrun->GetRM_ACQ_SETTING_MODE()<< " , "                      << " RM_ACQ_SETTING_MODE=" << glrun->GetRM_ACQ_SETTING_MODE()<< " , "  
5126                    << " TRK_CALIB_USED=" << glrun->GetTRK_CALIB_USED()<< " , "                      << " TRK_CALIB_USED=" << glrun->GetTRK_CALIB_USED()<< " , "  
5127                    << " CAL_DSP_MASK=" << glrun->GetCAL_DSP_MASK()<< " , "                      << " CAL_DSP_MASK=" << glrun->GetCAL_DSP_MASK()<< " , "  
5128                    << " LAST_TIMESYNC=" << glrun->GetLAST_TIMESYNC()<< " , "                      << " LAST_TIMESYNC=" << glrun->GetLAST_TIMESYNC()<< " , ";
5129                    << " OBT_TIMESYNC=" << glrun->GetOBT_TIMESYNC()                //
5130                    << " WHERE ID=" << myid[u] << ";";                if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() )
5131                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
5132                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() )
5133                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC() << " , ";
5134                  //              
5135                  oss << " OBT_TIMESYNC=" << glrun->GetOBT_TIMESYNC();
5136                  oss << " WHERE ID=" << myid[u] << ";";
5137                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
5138              };              };
5139              //              //
# Line 5074  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 5144  Int_t PamelaDBOperations::CleanGL_RUN_FR
5144              for (UInt_t u=1; u <= nid ; u++){              for (UInt_t u=1; u <= nid ; u++){
5145                oss.str("");                oss.str("");
5146                oss << "UPDATE GL_RUN_FRAGMENTS SET "                oss << "UPDATE GL_RUN_FRAGMENTS SET "
5147                    << " RUNHEADER_TIME=" << glrun->GetRUNHEADER_TIME()<< " , "                    << " RUNHEADER_TIME=" << glrun->GetRUNHEADER_TIME()<< " , ";
5148                    << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "                //
5149                  if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() )
5150                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
5151                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() )
5152                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
5153                  //              
5154                  oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "
5155                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< ";";                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< ";";
5156                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
5157              };                  };    
# Line 5098  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 5174  Int_t PamelaDBOperations::CleanGL_RUN_FR
5174                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "
5175                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , "                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , "
5176                    << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< " , "                    << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< " , "
5177                    << " PKT_COUNTER=" << glrun->GetPKT_COUNTER()<< " , "                    << " PKT_COUNTER=" << glrun->GetPKT_COUNTER()<< " , ";
5178                    << " PKT_READY_COUNTER=" << glrun->GetPKT_READY_COUNTER()                //
5179                  if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ){
5180                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
5181                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) {
5182                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
5183                  //              
5184                  oss << " PKT_READY_COUNTER=" << glrun->GetPKT_READY_COUNTER()
5185                    << " WHERE ID=" << myid[u] << ";";                    << " WHERE ID=" << myid[u] << ";";
5186                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
5187              };              };
# Line 5112  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 5194  Int_t PamelaDBOperations::CleanGL_RUN_FR
5194                oss.str("");                oss.str("");
5195                oss << "UPDATE GL_RUN_FRAGMENTS SET "                oss << "UPDATE GL_RUN_FRAGMENTS SET "
5196                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "
5197                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , "                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , ";
5198                    << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< ";";                //
5199                  if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ){
5200                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
5201                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){
5202                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
5203                  //              
5204                  oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< ";";
5205                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
5206              };                  };    
5207            };            };
# Line 5272  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 5360  Int_t PamelaDBOperations::CleanGL_RUN_FR
5360          glrun->SetID_RUN_FRAG(glrun->GetID());          glrun->SetID_RUN_FRAG(glrun->GetID());
5361          glrun->Fill_GL_RUN(conn);            glrun->Fill_GL_RUN(conn);  
5362          //          //
5363  //      oss.str("");          //      oss.str("");
5364  //      oss << " SELECT ID FROM GL_RUN WHERE "          //      oss << " SELECT ID FROM GL_RUN WHERE "
5365  //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "          //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
5366  //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "          //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
5367  //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "          //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
5368  //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "          //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
5369  //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";          //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
5370  //      //          //      //
5371  //      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());
5372  //      result2 = conn->Query(oss.str().c_str());          //      result2 = conn->Query(oss.str().c_str());
5373  //      //          //      //
5374  //      if ( !result2 ) throw -4;          //      if ( !result2 ) throw -4;
5375  //      //          //      //
5376  //      row2 = result2->Next();          //      row2 = result2->Next();
5377  //      //          //      //
5378  //      if ( !row2 ) throw -25;          //      if ( !row2 ) throw -25;
5379  //      //          //      //
5380  //      oss.str("");          //      oss.str("");
5381  //      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);
5382  //      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());
5383  //      result2 = conn->Query(oss.str().c_str());          //      result2 = conn->Query(oss.str().c_str());
5384  //      //          //      //
5385  //      if ( !result2 ) throw -4;          //      if ( !result2 ) throw -4;
5386          //          //
5387          moved++;          moved++;
5388          //          //
# Line 5306  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 5394  Int_t PamelaDBOperations::CleanGL_RUN_FR
5394        //        //
5395        //        //
5396        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
5397  //      oss.str("");        //      oss.str("");
5398  //       oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);        //       oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
5399  //       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());
5400  //       result2 = conn->Query(oss.str().c_str());        //       result2 = conn->Query(oss.str().c_str());
5401  //      //        //      //
5402  //      if ( !result2 ) throw -4;        //      if ( !result2 ) throw -4;
5403  //      //        //      //
5404        row = result->Next();        row = result->Next();
5405      };      };
5406    };    };
# Line 5457  Int_t PamelaDBOperations::ValidateRuns(T Line 5545  Int_t PamelaDBOperations::ValidateRuns(T
5545    GL_RUN* this_run = new GL_RUN();    GL_RUN* this_run = new GL_RUN();
5546    GL_RUN* next_run = new GL_RUN();    GL_RUN* next_run = new GL_RUN();
5547    Int_t   nseq_max = 1000;    Int_t   nseq_max = 1000;
5548  //  UInt_t* sequence = new UInt_t[100];    //  UInt_t* sequence = new UInt_t[100];
5549    vector<UInt_t> sequence(nseq_max);    vector<UInt_t> sequence(nseq_max);
5550    Int_t   nseq = 0;    Int_t   nseq = 0;
5551    Bool_t CHECK = false;    Bool_t CHECK = false;
# Line 5472  Int_t PamelaDBOperations::ValidateRuns(T Line 5560  Int_t PamelaDBOperations::ValidateRuns(T
5560    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
5561    while(1){    while(1){
5562                        
5563            row = result->Next();      row = result->Next();
5564            if( row == NULL ) break;      if( row == NULL ) break;
5565                        
5566            //------------      //------------
5567            //get run info      //get run info
5568            //------------      //------------
5569            this_run->Set_GL_RUN(row);      this_run->Set_GL_RUN(row);
5570                                        
5571            Bool_t this_BAD = false;      Bool_t this_BAD = false;
5572            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;
5573            else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;      else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;
5574            else{      else{
5575  //                printf("Missing or corrupted header!! \n");        //                  printf("Missing or corrupted header!! \n");
5576                    this_ONLINE = false;        this_ONLINE = false;
5577                    this_BAD = true;        this_BAD = true;
5578            };      };
5579    
5580            //-----------------------------------      //-----------------------------------
5581            //compare with previous(next in time)      //compare with previous(next in time)
5582            //-----------------------------------      //-----------------------------------
5583            CHECK = false;      CHECK = false;
5584            UInt_t interval=0;      UInt_t interval=0;
5585                        
5586            if( nrow != 0){      if( nrow != 0){
5587                    
5588                                        
5589                    t1 = this_run->GetRUNTRAILER_TIME();        t1 = this_run->GetRUNTRAILER_TIME();
5590                    t2 = next_run->GetRUNHEADER_TIME();        t2 = next_run->GetRUNHEADER_TIME();
5591                    interval = (t2-t1);        interval = (t2-t1);
5592                                        
5593                    if(this_ONLINE && next_ONLINE){                               // this: ON-LINE + next: ON-LINE        if(this_ONLINE && next_ONLINE){                           // this: ON-LINE + next: ON-LINE
5594                                                        
5595                            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
5596                                                        
5597                            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
5598                                                        
5599                            if( !CHECK && this_run->VALIDATION ){          if( !CHECK && this_run->VALIDATION ){
5600                                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);            for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
5601                                    nseq=0;            nseq=0;
5602                            }          }
5603                                        
5604                    }else if( !this_ONLINE && next_ONLINE) {              // this: DEFAULT + next:ON-LINE        }else if( !this_ONLINE && next_ONLINE) {          // this: DEFAULT + next:ON-LINE
5605                                                        
5606                            CHECK = true;          CHECK = true;
5607    
5608                    }else if( !next_ONLINE ){                                             // this:ANY + next:DEFAULT        }else if( !next_ONLINE ){                                         // this:ANY + next:DEFAULT
5609                                                        
5610                            assignVALIDATION(next_run->ID,true);          assignVALIDATION(next_run->ID,true);
5611                            nseq=0;          nseq=0;
5612                    }        }
5613            }      }
5614    
5615            //----------------------------      //----------------------------
5616            //check run sequence for calib      //check run sequence for calib
5617            //----------------------------      //----------------------------
5618            if( CHECK ){      if( CHECK ){
5619                    // check if calibration exists        // check if calibration exists
5620                    if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);        if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
5621                    Bool_t MISSING = MissingTRK_CALIB(t1,t2);        Bool_t MISSING = MissingTRK_CALIB(t1,t2);
5622                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);        for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
5623                    nseq=0;        nseq=0;
5624            };      };
5625            //--------------      //--------------
5626            //store run info      //store run info
5627            //--------------      //--------------
5628            *next_run   = *this_run;      *next_run   = *this_run;
5629            next_ONLINE = this_ONLINE;      next_ONLINE = this_ONLINE;
5630            if( !this_BAD ){      if( !this_BAD ){
5631                    if(nseq < nseq_max){        if(nseq < nseq_max){
5632                            sequence[nseq] = this_run->ID;          sequence[nseq] = this_run->ID;
5633                            nseq++;          nseq++;
5634                    }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);
5635            };      };
5636                        
5637            if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);      if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);
5638            nrow++;      nrow++;
5639                        
5640    };    };
5641    delete this_run;    delete this_run;
# Line 5563  Int_t PamelaDBOperations::ValidateRuns(T Line 5651  Int_t PamelaDBOperations::ValidateRuns(T
5651   */   */
5652  Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){  Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
5653                    
5654          GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();    GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
5655                    
5656          // get the closest VALIDATED calibration before the run start (t2)    // get the closest VALIDATED calibration before the run start (t2)
5657          if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);      //>>> missing    if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);    //>>> missing
5658                    
5659          if ( trkcalib->TO_TIME  < t2 ) return(true);                                    //>>> missing    if ( trkcalib->TO_TIME  < t2 ) return(true);                                  //>>> missing
5660                    
5661          //==============================================================    //==============================================================
5662          // Check is done first on the basis of time between calibration,    // Check is done first on the basis of time between calibration,
5663          // which should be equal to the time between ascending-nodes.    // which should be equal to the time between ascending-nodes.
5664          //==============================================================    //==============================================================
5665          if ( t2 - trkcalib->FROM_TIME > 5700) {    if ( t2 - trkcalib->FROM_TIME > 5700) {
5666                  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);
5667          //==============================================================      //==============================================================
5668          // there might be a missing calibration, due to:      // there might be a missing calibration, due to:
5669          // - MM full      // - MM full
5670          // - corrupted packets      // - corrupted packets
5671          // - loss of data      // - loss of data
5672          // 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
5673          //==============================================================      //==============================================================
5674                  Bool_t DOWNLOAD = false;      Bool_t DOWNLOAD = false;
5675                  // check if the calib was skipped becouse of download .... DA FARE!!      // check if the calib was skipped becouse of download .... DA FARE!!
5676                  if(DOWNLOAD)return(false);      if(DOWNLOAD)return(false);
5677                                    
5678                  return(true);                                   //>>> missing      return(true);                                       //>>> missing
5679                                    
5680          };    };
5681                    
5682          //==============================================================    //==============================================================
5683          // 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,
5684          // it is enough to say that there are no missing calibrations    // it is enough to say that there are no missing calibrations
5685          //==============================================================    //==============================================================
5686          // the long time interval bewteen runs might be due to download    // the long time interval bewteen runs might be due to download
5687          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);
5688          return(false);    return(false);
5689                    
5690  };  };
5691  /**  /**
# Line 5606  Bool_t PamelaDBOperations::MissingTRK_CA Line 5694  Bool_t PamelaDBOperations::MissingTRK_CA
5694   * @param validation true/false   * @param validation true/false
5695   */   */
5696  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
5697          TSQLResult *result = 0;    TSQLResult *result = 0;
5698          stringstream oss;    stringstream oss;
5699          oss.str("");    oss.str("");
5700          oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";    oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
5701          //    //
5702  //      if ( IsDebug() )    //    if ( IsDebug() )
5703  //      printf(" Set VALIDATION = %i for run %i \n",validation,idrun);    //    printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
5704          if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());    if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
5705          result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
5706          if ( !result ) throw -4;    if ( !result ) throw -4;
5707          return(0);    return(0);
5708  }  }
5709    
5710    
# Line 6261  void PamelaDBOperations::RemoveCALIBS(){ Line 6349  void PamelaDBOperations::RemoveCALIBS(){
6349   **/   **/
6350  UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){  UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
6351    
6352      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};
6353      UInt_t timeaftercalib=120000; //2000;    UInt_t timeaftercalib=120000; //2000;
6354  //  ----------    //  ----------
6355  //  Check CRCs    //  Check CRCs
6356  //  ----------    //  ----------
6357      for(Int_t ipkt=0; ipkt<6; ipkt++){    for(Int_t ipkt=0; ipkt<6; ipkt++){
6358          if( caltrk->crc_hcal[ipkt] )return 0; // :-(      if( caltrk->crc_hcal[ipkt] )return 0; // :-(
6359          for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(      for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
6360      }    }
6361  //  -----------------------    //  -----------------------
6362  //  Check missing packets:    //  Check missing packets:
6363  //  -----------------------    //  -----------------------
6364  //    Readout order:    //    Readout order:
6365  //    ------------------    //    ------------------
6366  //    DSP   packet board    //    DSP   packet board
6367  //    ------------------    //    ------------------
6368  //    12    0      1    //    12    0      1
6369  //    10    1      1    //    10    1      1
6370  //     8    2      1    //     8    2      1
6371  //     4    3      1    //     4    3      1
6372  //     6    4      1    //     6    4      1
6373  //     2    5      1    //     2    5      1
6374  //    ------------------    //    ------------------
6375  //    11    0      2    //    11    0      2
6376  //     9    1      2    //     9    1      2
6377  //     7    2      2    //     7    2      2
6378  //     3    3      2    //     3    3      2
6379  //     5    4      2    //     5    4      2
6380  //     1    5      2    //     1    5      2
6381  //    ------------------    //    ------------------
6382  //  -------------------------------------------------    //  -------------------------------------------------
6383  //  Check if it is first or second calibration packet    //  Check if it is first or second calibration packet
6384  //  -------------------------------------------------    //  -------------------------------------------------
6385      UInt_t build=0;    UInt_t build=0;
6386      TString classname = caltrk->GetName();    TString classname = caltrk->GetName();
6387      UInt_t base=0;    UInt_t base=0;
6388      UInt_t mask=0;    UInt_t mask=0;
6389      if(classname.Contains("CalibTrk1Event")){    if(classname.Contains("CalibTrk1Event")){
6390          base=12;      base=12;
6391          mask=0x03F000;      mask=0x03F000;
6392      }    }
6393      if(classname.Contains("CalibTrk2Event")){    if(classname.Contains("CalibTrk2Event")){
6394          base=18;      base=18;
6395          mask=0xFC0000;      mask=0xFC0000;
6396      }    }
6397  //  -------------------------------------------------    //  -------------------------------------------------
6398  //  Count number of packets and set build variable    //  Count number of packets and set build variable
6399  //  -------------------------------------------------    //  -------------------------------------------------
6400      Int_t  npkts=0;    Int_t  npkts=0;
6401      for(Int_t ipkt=0; ipkt<6; ipkt++){    for(Int_t ipkt=0; ipkt<6; ipkt++){
6402          if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){      if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
6403              npkts++;        npkts++;
6404              build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );        build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
6405          }      }
6406      }    }
6407  //    if( npkts==6 )return 1; // :-)    //    if( npkts==6 )return 1; // :-)
6408    
6409      //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
6410    
6411  //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;    //  -----------------------------------------------
6412      //  If missing packets: check the acq configuration
6413      //  (some DSPs might be excluded from acquisition)
6414      //  -----------------------------------------------
6415    
6416      //  -----------------------------------------------
6417      //  retrieve the first run header after calib
6418      //  -----------------------------------------------
6419      PacketType *pctp;
6420      EventCounter *cod;
6421      cod = eh->GetCounter();
6422      Int_t irun = cod->Get(pctp->RunHeader);
6423      TTree *rh=(TTree*)file->Get("RunHeader");
6424      if ( !rh || rh->IsZombie() ) throw -17;
6425      if( rh->GetEntries() == irun ){
6426        if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;
6427        return 0; // :-(
6428      }
6429    
6430  //  -----------------------------------------------    RunHeaderEvent *run  = 0;
6431  //  If missing packets: check the acq configuration    EventHeader    *hrun = 0;
6432  //  (some DSPs might be excluded from acquisition)    rh->SetBranchAddress("RunHeader", &run);
6433  //  -----------------------------------------------    rh->SetBranchAddress("Header", &hrun);
6434      rh->GetEntry(irun);
6435  //  -----------------------------------------------    //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
6436  //  retrieve the first run header after calib  
6437  //  -----------------------------------------------    if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
6438       PacketType *pctp;      if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
6439       EventCounter *cod;      return 0; // :-(
6440       cod = eh->GetCounter();    }
      Int_t irun = cod->Get(pctp->RunHeader);  
      TTree *rh=(TTree*)file->Get("RunHeader");  
      if ( !rh || rh->IsZombie() ) throw -17;  
      if( rh->GetEntries() == irun ){  
           if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;  
          return 0; // :-(  
      }  
   
      RunHeaderEvent *run  = 0;  
      EventHeader    *hrun = 0;  
      rh->SetBranchAddress("RunHeader", &run);  
      rh->SetBranchAddress("Header", &hrun);  
      rh->GetEntry(irun);  
 //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;  
   
      if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){  
           if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;  
          return 0; // :-(  
      }  
6441            
6442       if( !run->RM_ACQ_AFTER_CALIB ){    if( !run->RM_ACQ_AFTER_CALIB ){
6443            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;
6444           return 0; // :-(      return 0; // :-(
6445       }    }
6446    
6447       UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());    UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
6448       if( dtime > timeaftercalib ){    if( dtime > timeaftercalib ){
6449           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;
6450           return 0; // :-(      return 0; // :-(
6451       }    }
6452            
6453    
6454    
6455       if( (run->ACQ_BUILD_INFO & mask) != build ){    if( (run->ACQ_BUILD_INFO & mask) != build ){
6456           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;
6457           return 0; // :-(      return 0; // :-(
6458       }    }
6459       return 1; // :-)    return 1; // :-)
6460    
6461  }  }
6462    
# Line 6378  UInt_t PamelaDBOperations::ValidateTrkCa Line 6466  UInt_t PamelaDBOperations::ValidateTrkCa
6466   *   *
6467   **/   **/
6468  UInt_t PamelaDBOperations::Check(){  UInt_t PamelaDBOperations::Check(){
6469          return(this->Check(0,0));    return(this->Check(0,0));
6470  }  }
6471    
6472  UInt_t PamelaDBOperations::Check(UInt_t from, UInt_t to){  UInt_t PamelaDBOperations::Check(UInt_t from, UInt_t to){
6473    //    //
6474          if ( IsDebug() ) printf(" from %u to %u \n",from,to);    if ( IsDebug() ) printf(" from %u to %u \n",from,to);
6475    //    //
6476    UInt_t test = 0;    UInt_t test = 0;
6477    //    //
# Line 6424  UInt_t PamelaDBOperations::Check(UInt_t Line 6512  UInt_t PamelaDBOperations::Check(UInt_t
6512      //      //
6513      if ( from > 0 && nid <= from ) goto ss;      if ( from > 0 && nid <= from ) goto ss;
6514      if ( to > 0 && nid >= to ) goto ss;      if ( to > 0 && nid >= to ) goto ss;
6515  //      //
6516      if ( (UInt_t)atoll(row->GetField(4)) > 1 ){      if ( (UInt_t)atoll(row->GetField(4)) > 1 ){
6517        //        //
6518        //        //
# Line 6455  UInt_t PamelaDBOperations::Check(UInt_t Line 6543  UInt_t PamelaDBOperations::Check(UInt_t
6543              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)));  
6544              row3 = result3->Next();              row3 = result3->Next();
6545            };            };
6546  //      delete result3;            //    delete result3;
6547    
6548          };          };
6549          //          //
# Line 6476  UInt_t PamelaDBOperations::Check(UInt_t Line 6564  UInt_t PamelaDBOperations::Check(UInt_t
6564              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)));
6565              row3 = result3->Next();              row3 = result3->Next();
6566            };            };
6567  //      delete result3;            //    delete result3;
6568          };          };
6569          //          //
6570          oss.str("");          oss.str("");
# Line 6496  UInt_t PamelaDBOperations::Check(UInt_t Line 6584  UInt_t PamelaDBOperations::Check(UInt_t
6584              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)));
6585              row3 = result3->Next();              row3 = result3->Next();
6586            };            };
6587   //     delete result3;            //    delete result3;
6588    
6589          };          };
6590        };        };
# Line 6517  UInt_t PamelaDBOperations::Check(UInt_t Line 6605  UInt_t PamelaDBOperations::Check(UInt_t
6605            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)));
6606            row3 = result3->Next();            row3 = result3->Next();
6607          };          };
6608  //      delete result3;          //      delete result3;
6609        };        };
6610        //        //
6611      };      };
# Line 6543  UInt_t PamelaDBOperations::Check(UInt_t Line 6631  UInt_t PamelaDBOperations::Check(UInt_t
6631        thisf = (TString)row2->GetField(0);        thisf = (TString)row2->GetField(0);
6632        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
6633        test = 1;        test = 1;
6634  //      delete result2;        //        delete result2;
6635      };      };
6636      //      //
6637      if ( (thisrtt < prevrht) && (thisrht != prevrht) ){      if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
# Line 6565  UInt_t PamelaDBOperations::Check(UInt_t Line 6653  UInt_t PamelaDBOperations::Check(UInt_t
6653        thisf = (TString)row2->GetField(0);        thisf = (TString)row2->GetField(0);
6654        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
6655        test = 1;        test = 1;
6656  //      delete result2;        //        delete result2;
6657      };      };
6658      //      //
6659      if ( (thisrht > thisrtt) && (thisrht != prevrht) ){      if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
# Line 6587  UInt_t PamelaDBOperations::Check(UInt_t Line 6675  UInt_t PamelaDBOperations::Check(UInt_t
6675        thisf = (TString)row2->GetField(0);        thisf = (TString)row2->GetField(0);
6676        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
6677        test = 1;        test = 1;
6678  //      delete result2;        //        delete result2;
6679      };      };
6680  ss:    ss:
6681      //      //
6682      prevrht = thisrht;      prevrht = thisrht;
6683      prevrtt = thisrtt;      prevrtt = thisrtt;
6684      previd = thisid;      previd = thisid;
6685      prevl0id = thisl0id;      prevl0id = thisl0id;
6686      row = result->Next();      row = result->Next();
6687  //    if ( result2 ) delete result2;      //    if ( result2 ) delete result2;
6688  //    if ( result3 ) delete result3;      //    if ( result3 ) delete result3;
6689    };    };
6690    //    //
6691    return(test);    return(test);

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.3

  ViewVC Help
Powered by ViewVC 1.1.23