/[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.12 by mocchiut, Fri Nov 28 10:57:44 2008 UTC
# Line 58  string getTleDatetime(cTle*); Line 58  string getTleDatetime(cTle*);
58   * @param debug        debug flag.   * @param debug        debug flag.
59   * @param tlefilename  ascii file with TLE 3 line elements.   * @param tlefilename  ascii file with TLE 3 line elements.
60   */   */
61  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename, UInt_t dwinput, Bool_t staticp, Bool_t gpamela){  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename, UInt_t dwinput, Bool_t staticp, Bool_t gpamela, Bool_t keepenv){
62    //    //
63    chewbacca = false;    chewbacca = false;
64    chminentry = 0;    chminentry = 0;
# Line 76  PamelaDBOperations::PamelaDBOperations(T Line 76  PamelaDBOperations::PamelaDBOperations(T
76      //      //
77    };    };
78    //    //
79      KEEPENV = false;
80      if ( keepenv ) KEEPENV=true;
81    STATIC=false;    STATIC=false;
82    if ( staticp ) STATIC=true;    if ( staticp ) STATIC=true;
83    //    //
# Line 138  void PamelaDBOperations::OpenL0File(TStr Line 140  void PamelaDBOperations::OpenL0File(TStr
140        TString chfile;        TString chfile;
141        UInt_t ridn = 0;        UInt_t ridn = 0;
142        qu.str("");        qu.str("");
143        qu << "SELECT ROOT_ID_N,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL from ROOT_TABLE_MERGING where ID_N=" << chID << ";";        qu << "SELECT ROOT_ID_N,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,INSERTED_BY from ROOT_TABLE_MERGING where ID_N=" << chID << ";";
144        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
145        result = conn->Query(qu.str().c_str());        result = conn->Query(qu.str().c_str());
146        if ( result ){        if ( result ){
# Line 149  void PamelaDBOperations::OpenL0File(TStr Line 151  void PamelaDBOperations::OpenL0File(TStr
151            chpktmax = (UInt_t)atoll(row->GetField(2));            chpktmax = (UInt_t)atoll(row->GetField(2));
152            chobtmin = (UInt_t)atoll(row->GetField(3));            chobtmin = (UInt_t)atoll(row->GetField(3));
153            chobtmax = (UInt_t)atoll(row->GetField(4));            chobtmax = (UInt_t)atoll(row->GetField(4));
154              chiby = (TString)(row->GetField(5));
155          } else {          } else {
156            throw -84;            throw -84;
157          };          };
# Line 163  void PamelaDBOperations::OpenL0File(TStr Line 166  void PamelaDBOperations::OpenL0File(TStr
166        if ( result2 ){        if ( result2 ){
167          row2 = result2->Next();          row2 = result2->Next();
168          if ( row2 ){          if ( row2 ){
169            chpath = (TString)gSystem->ExpandPathName(row2->GetField(0))+'/';            if ( KEEPENV ){
170                chpath = (TString)(row2->GetField(0))+'/';
171              } else {
172                chpath = (TString)gSystem->ExpandPathName(row2->GetField(0))+'/';
173              };
174            chfile = (TString)(row2->GetField(1));            chfile = (TString)(row2->GetField(1));
175            chobtts = (UInt_t)atoll(row2->GetField(2));            chobtts = (UInt_t)atoll(row2->GetField(2));
176            chlastts = (UInt_t)atoll(row2->GetField(3));            chlastts = (UInt_t)atoll(row2->GetField(3));
# Line 435  TString PamelaDBOperations::GetRawPath() Line 442  TString PamelaDBOperations::GetRawPath()
442    
443  TString PamelaDBOperations::GetRootPath(){  TString PamelaDBOperations::GetRootPath(){
444    if ( STATIC ){    if ( STATIC ){
445      return((TString)gSystem->DirName(filerootname.Data())+'/');      return((TString)gSystem->DirName(this->GetRootName().Data())+'/');
446    } else {    } else {    
447      return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');      if ( KEEPENV ){
448          return((TString)gSystem->ExpandPathName(gSystem->DirName(filerootname.Data()))+'/');
449        } else {
450          return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');
451        };
452    };    };
453  };  };
454    
# Line 490  void PamelaDBOperations::SetCommonGLRUN( Line 501  void PamelaDBOperations::SetCommonGLRUN(
501  };  };
502    
503  /**  /**
504     *
505     * Set the variables which belogns to physendrun tree
506     *
507     */
508    void PamelaDBOperations::SetPhysEndRunVariables(){
509      //
510      //
511      //
512      TTree *T = 0;
513      T = (TTree*)file->Get("PhysEndRun");
514      if ( !T || T->IsZombie() ) throw -90;
515      //
516      PhysEndRunEvent *pher= 0;
517      EventHeader *eh = 0;
518      T->SetBranchAddress("PhysEndRun", &pher);
519      T->SetBranchAddress("Header", &eh);
520      //
521      UInt_t phobt = 0;
522      UInt_t phpkt = 0;
523      //
524      glrun->SetPHYSENDRUN_MASK_S3S2S12(0);
525      glrun->SetPHYSENDRUN_MASK_S11CRC(0);
526      //
527      for (Int_t p=0; p<T->GetEntries(); p++){
528        //
529        T->GetEntry(p);
530        //
531        phobt = (UInt_t)eh->GetPscuHeader()->GetOrbitalTime();
532        phpkt = (UInt_t)eh->GetPscuHeader()->GetCounter();
533        //
534        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())  ){
535          if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() || glrun->GetPHYSENDRUN_MASK_S11CRC() ){
536            if ( IsDebug() ) printf(" WARNING while looping in physendrun: found two PhysEndRun packet for the same RUN! \n");
537            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);
538            if ( PEDANTIC && IsDebug() ) printf(" ERROR while looping in physendrun: found two PhysEndRun packet for the same RUN!\n ");
539            if ( PEDANTIC ) throw -91;
540          } else {
541            glrun->SetPHYSENDRUN_MASK_S3S2S12((UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S3S2S12);
542            glrun->SetPHYSENDRUN_MASK_S11CRC((UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S11CRC);
543          };
544        };
545      };
546      //
547    };
548    
549    /**
550   * Patch, look for upper limits to avoid processing retransmitted data   * Patch, look for upper limits to avoid processing retransmitted data
551   */   */
552  Int_t PamelaDBOperations::SetUpperLimits(){  Int_t PamelaDBOperations::SetUpperLimits(){
# Line 603  Int_t PamelaDBOperations::SetUpperLimits Line 660  Int_t PamelaDBOperations::SetUpperLimits
660      UInt_t tjump = 50000;      UInt_t tjump = 50000;
661      //UInt_t tjump = 100;      //UInt_t tjump = 100;
662      while ( tjump > 0 ){      while ( tjump > 0 ){
663        pktlast = numeric_limits<UInt_t>::max();        //      pktlast = numeric_limits<UInt_t>::max();
664        while ( pktlast > chpktmax && (Int_t)(nevent-1-it) >= 0 ){        pktlast = chpktmax + 1;
665          while ( PKT(pktlast) > PKT(chpktmax) && (Int_t)(nevent-1-it) >= 0 ){
666          if ( (Int_t)(nevent-1-it) >= 0 ){          if ( (Int_t)(nevent-1-it) >= 0 ){
667            T->GetEntry(nevent-1-it);            T->GetEntry(nevent-1-it);
668            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
# Line 612  Int_t PamelaDBOperations::SetUpperLimits Line 670  Int_t PamelaDBOperations::SetUpperLimits
670          } else {          } else {
671            pktlast = chpktmax + 1;            pktlast = chpktmax + 1;
672          };          };
673          if ( (!(it%1000) || abs((int)pktlast - (int)chpktmax)<1000 ) && debug ) printf(" look for up %i %i %i nevent %u (nevent-1-it) %i \n",it,pktlast,chpktmax,nevent,(Int_t)(nevent-1-it));          if ( (!(it%1000) || abs((int)(PKT(pktlast) - PKT(chpktmax)))<1000 ) && debug ) printf(" look for up %i %i %i nevent %u (nevent-1-it) %i \n",it,pktlast,chpktmax,nevent,(Int_t)(nevent-1-it));
674          it += tjump;          it += tjump;
675        };        };
676        if ( tjump > 1 ) it -= 2*tjump;        if ( tjump > 1 ) it -= 2*tjump;
# Line 636  Int_t PamelaDBOperations::SetUpperLimits Line 694  Int_t PamelaDBOperations::SetUpperLimits
694      tjump = 50000;      tjump = 50000;
695      //tjump = 100;      //tjump = 100;
696      while ( tjump > 0 ){      while ( tjump > 0 ){
697        pktlast = 0;        //      pktlast = 0;
698        while ( pktlast < chpktmin && it < (Int_t)nevent ){        pktlast = chpktmin - 1;
699          if ( debug ) printf("LLlook for down %i %i %llu %llu \n",it,pktlast,PKT(pktlast),PKT(chpktmin));
700          while ( PKT(pktlast) < PKT(chpktmin) && it < (Int_t)nevent ){
701          if ( it < (Int_t)nevent ){          if ( it < (Int_t)nevent ){
702            T->GetEntry(it);            T->GetEntry(it);
703            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
# Line 739  Int_t PamelaDBOperations::SetUpperLimits Line 799  Int_t PamelaDBOperations::SetUpperLimits
799    };    };
800    //    //
801    if ( chewbacca && nevent < 1 ) {    if ( chewbacca && nevent < 1 ) {
802          pktfirst = chpktmin;      pktfirst = chpktmin;
803          upperpkt = PKT(chpktmax);      upperpkt = PKT(chpktmax);
804          pktlast = chpktmax;      pktlast = chpktmax;
805          obtfirst = chobtmin;      obtfirst = chobtmin;
806          obtlast = chobtmax;      obtlast = chobtmax;
807          upperobt = OBT(chobtmax);      upperobt = OBT(chobtmax);
808    };    };
809    //    //
810    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 812  Int_t PamelaDBOperations::SetUpperLimits
812    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);
813    //    //
814    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)) ){
815          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));
816          if ( PEDANTIC ) throw -88;      if ( PEDANTIC ) throw -88;
817          return(32);      return(32);
818    };    };
819    //    //
820    if ( !nevent ) return(64);    if ( !nevent ) return(64);
# Line 1040  Int_t PamelaDBOperations::SetUpperLimits Line 1100  Int_t PamelaDBOperations::SetUpperLimits
1100          upperpkt = pkth;          upperpkt = pkth;
1101          upperobt = obth;          upperobt = obth;
1102          rhev = lasthead+1;          rhev = lasthead+1;
1103      } else {        } else {
1104          rhev = lasthead;          rhev = lasthead;
1105        };        };
1106        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 1079  UInt_t PamelaDBOperations::AssignRunID() Line 1139  UInt_t PamelaDBOperations::AssignRunID()
1139    stringstream   oss;    stringstream   oss;
1140    //      //  
1141    oss.str("");    oss.str("");
1142    oss << "INSERT INTO _RUNID_GEN VALUES (NULL);";    if ( chewbacca ){// if chewbacca and tag=none then use chewbacca tag (chiby = chewbacca inserted by), if chewbacca and tag!=none then use tag, if not chewbacca use tag.
1143        if ( !strcmp(tag.Data(),"NONE") ){
1144          oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< chiby.Data() <<"');";
1145        } else {
1146          oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');";
1147        };
1148      } else {
1149        oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');";
1150      };
1151    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1152    if ( !result ) throw -10;    if ( !result ) throw -10;
1153    oss.str("");    oss.str("");
# Line 1227  void PamelaDBOperations::LockTables(){   Line 1295  void PamelaDBOperations::LockTables(){  
1295    stringstream oss;    stringstream oss;
1296    //      //  
1297    oss.str("");    oss.str("");
1298    oss << "lock table GL_RUN write, GL_ROOT write, GL_RAW write, GL_TIMESYNC write, GL_RESURS_OFFSET write, GL_PARAM write, GL_TLE write, GL_RUN_FRAGMENTS write, GL_RUN_TRASH write, GL_CALO_CALIB write, GL_CALOPULSE_CALIB write, GL_TRK_CALIB write, GL_S4_CALIB write, ROOT_TABLE_MERGING write, ROOT_TABLE write, _RUNID_GEN write;";    oss << "lock table GL_RUN write, GL_ROOT write, GL_RAW write, GL_TIMESYNC write, GL_RESURS_OFFSET write, GL_PARAM write, GL_TLE write, GL_RUN_FRAGMENTS write, GL_RUN_TRASH write, GL_CALO_CALIB write, GL_CALOPULSE_CALIB write, GL_TRK_CALIB write, GL_S4_CALIB write, ROOT_TABLE_MERGING write, ROOT_TABLE_BAD write, ROOT_TABLE write, _RUNID_GEN write;";
1299    TSQLResult *result = 0;    TSQLResult *result = 0;
1300    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1301    if ( !result ) throw -10;    if ( !result ) throw -10;
# Line 1260  void PamelaDBOperations::UnLockTables(){ Line 1328  void PamelaDBOperations::UnLockTables(){
1328   */   */
1329  Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){    Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){  
1330    //    //
1331  //  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));
1332    //    //
1333    if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2)  ){    if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2)  ){
1334  //    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);
1335      return((Long64_t)pkt_num+16777215LL);      return((Long64_t)pkt_num+16777215LL);
1336    };    };
1337    //    //
1338    if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){    if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){
1339  //    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);
1340      return((Long64_t)pkt_num-16777215LL);      return((Long64_t)pkt_num-16777215LL);
1341    };    };
1342    //    //
1343  //  if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);    //  if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
1344    return((Long64_t)pkt_num);    return((Long64_t)pkt_num);
1345    //    //
1346  };  };
# Line 1282  Long64_t PamelaDBOperations::PKT(UInt_t Line 1350  Long64_t PamelaDBOperations::PKT(UInt_t
1350   */   */
1351  Long64_t PamelaDBOperations::OBT(UInt_t obt){    Long64_t PamelaDBOperations::OBT(UInt_t obt){  
1352    //    //
1353  //  if ( IsDebug() ) printf(" obt conversion: obt is %u obtfirst is %u  (numeric_limits<UInt_t>::max()/2) is %u  \n",obt,pobtfirst,(UInt_t)(numeric_limits<UInt_t>::max()/2));    //  if ( IsDebug() ) printf(" obt conversion: obt is %u obtfirst is %u  (numeric_limits<UInt_t>::max()/2) is %u  \n",obt,pobtfirst,(UInt_t)(numeric_limits<UInt_t>::max()/2));
1354    //    //
1355    if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){    if ( obt < ((Long64_t)pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){
1356  //    if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());      //    if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());
1357      return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());      return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());
1358    };    };
1359    //    //
1360    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) ){
1361  //    if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2));      //    if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2));
1362  //    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());
1363      return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());      return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
1364    };    };
1365    //    //
1366  //  if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);    //  if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);
1367    return((Long64_t)obt);    return((Long64_t)obt);
1368  };  };
1369    
# Line 1331  void PamelaDBOperations::FillClass(Bool_ Line 1399  void PamelaDBOperations::FillClass(Bool_
1399    UInt_t lastPkt = 0;    UInt_t lastPkt = 0;
1400    UInt_t rhtime = 0;    UInt_t rhtime = 0;
1401    UInt_t rttime = 0;    UInt_t rttime = 0;
1402      //
1403      if ( IsDebug() ) printf(" A firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1404      //
1405    if ( !mishead ){    if ( !mishead ){
1406      codh = ehh->GetCounter();      codh = ehh->GetCounter();
1407      if ( lastev+1 == firstev || (lastev == firstev && lastev != -1) ){            if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){      
1408          if ( IsDebug() ) printf(" B firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1409        firstev = 1;        firstev = 1;
1410        lastev = 0;        lastev = 0;
1411      } else {      } else {
# Line 1346  void PamelaDBOperations::FillClass(Bool_ Line 1418  void PamelaDBOperations::FillClass(Bool_
1418    };    };
1419    if ( !mistrail ){    if ( !mistrail ){
1420      codt = eht->GetCounter();      codt = eht->GetCounter();
1421      if ( lastev+1 == firstev || (lastev == firstev && lastev != -1)){            if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1)){      
1422          if ( IsDebug() ) printf(" C firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1423        lastev = 0;        lastev = 0;
1424        firstev = lastev+1;        firstev = lastev+1;
1425      } else {      } else {
# Line 1363  void PamelaDBOperations::FillClass(Bool_ Line 1436  void PamelaDBOperations::FillClass(Bool_
1436    if ( mishead ) {    if ( mishead ) {
1437      glrun->Set_GL_RUNH0();      glrun->Set_GL_RUNH0();
1438      //      //
1439      if ( lastev+1 == firstev || (lastev == firstev && lastev != -1) ){            if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){      
1440        firstObt = lastObt;        firstObt = lastObt;
1441        firstPkt = lastPkt;        firstPkt = lastPkt;
1442        rhtime = rttime;        rhtime = rttime;
# Line 1382  void PamelaDBOperations::FillClass(Bool_ Line 1455  void PamelaDBOperations::FillClass(Bool_
1455    if ( mistrail ){    if ( mistrail ){
1456      glrun->Set_GL_RUNT0();      glrun->Set_GL_RUNT0();
1457      //      //
1458      if ( lastev+1 == firstev || (lastev == firstev && lastev != -1) ){            if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){      
1459        lastObt = firstObt;        lastObt = firstObt;
1460        lastPkt = firstPkt;        lastPkt = firstPkt;
1461        rttime = rhtime;        rttime = rhtime;
# Line 1405  void PamelaDBOperations::FillClass(Bool_ Line 1478  void PamelaDBOperations::FillClass(Bool_
1478    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);
1479    //    //
1480    this->SetCommonGLRUN(rhtime,rttime);    this->SetCommonGLRUN(rhtime,rttime);
1481      this->SetPhysEndRunVariables();
1482    //    //
1483  };  };
1484    
# Line 1416  void PamelaDBOperations::FillClass(Bool_ Line 1490  void PamelaDBOperations::FillClass(Bool_
1490   * Insert a new row into GL_RAW table.   * Insert a new row into GL_RAW table.
1491   */   */
1492  Int_t PamelaDBOperations::insertPamelaRawFile(){  Int_t PamelaDBOperations::insertPamelaRawFile(){
1493    
1494        //
1495        Bool_t idr =  this->SetID_RAW();
1496        if ( idr ) return(1);
1497    
1498        GL_RAW glraw = GL_RAW();
1499        
1500        glraw.PATH = GetRawPath();
1501        glraw.NAME = GetRawFile();
1502        //    glraw.BOOT_NUMBER = 0;//???
1503        glraw.BOOT_NUMBER = this->GetBOOTnumber();
1504        
1505        if( insertPamelaRawFile(&glraw) )return(1);
1506        //
1507        idr =  this->SetID_RAW();
1508        if ( !idr ) throw -11;
1509        
1510        return(0);
1511    }
1512    /**
1513     * Insert a new row into GL_RAW table.
1514     */
1515    Int_t PamelaDBOperations::insertPamelaRawFile(GL_RAW *glraw){
1516    //    //
1517    stringstream oss;      if(!glraw)return(1);//?? ok I think
1518    //      //
1519    Bool_t idr =  this->SetID_RAW();      stringstream oss;
1520    if ( idr ) return(1);      //
1521    //      oss.str("");
1522    oss.str("");      if ( STATIC ){
1523    if ( STATIC ){          oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
1524      oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"              << glraw->PATH << "', '" << glraw->NAME << "')";
1525          << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";      } else {
1526    } else {          oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << glraw->NAME << "')";
1527      oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')";      };
1528    };      if ( debug ) cout <<oss.str().c_str() <<endl;
1529    if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;      if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1530    //      //
1531    idr =  this->SetID_RAW();      oss.str("");
1532    if ( !idr ) throw -11;      oss << "SELECT ID FROM GL_RAW WHERE NAME=\""<<glraw->NAME<<"\";";
1533    //      if ( debug ) cout << oss.str().c_str()<<endl;
1534    return(0);      if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1535        //
1536        TSQLResult *result = 0;
1537        TSQLRow    *row    = 0;
1538        result = conn->Query(oss.str().c_str());
1539        if ( result == NULL ) throw -4;
1540        row = result->Next();
1541        if ( !row ) return(1);
1542        glraw->ID = (UInt_t)atoll(row->GetField(0));
1543        if ( debug ) printf(" The ID of the RAW file is %u \n",glraw->ID);
1544        delete result;
1545        delete row;
1546        //
1547        return(0);
1548  }  }
1549    
1550    
# Line 1746  Int_t PamelaDBOperations::insertPamelaGL Line 1856  Int_t PamelaDBOperations::insertPamelaGL
1856   * The raw file indicates in the parameters should be already been stored in the database.   * The raw file indicates in the parameters should be already been stored in the database.
1857   */   */
1858  Int_t PamelaDBOperations::insertPamelaRootFile(){  Int_t PamelaDBOperations::insertPamelaRootFile(){
1859    stringstream oss;  
1860    TSQLResult *result = 0;      stringstream oss;
1861    TSQLRow    *row    = 0;      TSQLResult *result = 0;
1862    UInt_t idtimesync = 0;      TSQLRow    *row    = 0;
   //  
   //  
   if ( chewbacca ){  
     oss.str("");  
     oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";  
     if ( debug ) printf(" query is %s \n",oss.str().c_str());  
     result = conn->Query(oss.str().c_str());  
     //  
     if ( !result ) throw -3;  
1863      //      //
1864      row = result->Next();      // ----------------------
1865        // determine the timesync
1866        // ----------------------
1867        UInt_t idtimesync = 0;
1868      //      //
1869      if ( !row ) throw -3;      if ( chewbacca ){
1870      idtimesync = (UInt_t)atoll(row->GetField(0));          oss.str("");
1871    } else {          oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";
1872      oss.str("");          if ( debug ) printf(" %s \n",oss.str().c_str());
1873      if ( STATIC ){          result = conn->Query(oss.str().c_str());
1874        oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "          //
1875            << " LEFT JOIN GL_ROOT "          if ( !result ) throw -3;
1876            << " ON GL_RAW.ID = GL_ROOT.ID_RAW "          //
1877            << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "          row = result->Next();
1878            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";          //
1879            if ( !row ) throw -3;
1880            idtimesync = (UInt_t)atoll(row->GetField(0));
1881      } else {      } else {
1882        oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "          oss.str("");
1883            << " LEFT JOIN GL_ROOT "          if ( STATIC ){
1884            << " ON GL_RAW.ID = GL_ROOT.ID_RAW "              oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1885            << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "                  << " LEFT JOIN GL_ROOT "
1886            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";                  << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1887                    << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1888                    << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1889            } else {
1890                oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1891                    << " LEFT JOIN GL_ROOT "
1892                    << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1893                    << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "
1894                    << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1895            };
1896            result = conn->Query(oss.str().c_str());
1897            //
1898            if ( !result ) throw -12;
1899            //
1900            row = result->Next();
1901            //
1902            if ( !row ) throw -10;
1903            if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){
1904                idroot = (UInt_t)atoll(row->GetField(2));
1905                delete row;
1906                delete result;
1907                return(1);
1908            };
1909            //
1910            // determine which timesync has to be used
1911            //
1912            oss.str("");
1913            oss << "SELECT GL_TIMESYNC.ID FROM GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW ORDER BY GL_TIMESYNC.ID DESC LIMIT 1;";
1914            result = conn->Query(oss.str().c_str());
1915            //
1916            if ( !result ) throw -3;
1917            //
1918            row = result->Next();
1919            //
1920            if ( !row ) throw -3;
1921            idtimesync = (UInt_t)atoll(row->GetField(0));
1922      };      };
1923      result = conn->Query(oss.str().c_str());      
1924      //      delete row;
1925      if ( !result ) throw -12;      delete result;
1926      //  
1927      row = result->Next();      // ----------------------
1928      //      // insert root file
1929      if ( !row ) throw -10;      // ----------------------
1930      if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){  
1931        idroot = (UInt_t)atoll(row->GetField(2));      GL_ROOT glroot = GL_ROOT();
1932        return(1);      
1933        glroot.ID_RAW = GetID_RAW();
1934        glroot.ID_TIMESYNC = idtimesync;
1935        if ( STATIC ){
1936          glroot.PATH = GetRootPath();
1937        } else {
1938          if ( KEEPENV ){
1939            glroot.PATH = gSystem->DirName(filerootname.Data());
1940          } else {
1941            glroot.PATH = "$PAM_L0";
1942          };
1943      };      };
1944      //      glroot.NAME = GetRootFile();
1945      // determine which timesync has to be used  
1946      //      if ( insertPamelaRootFile(&glroot) )return 1;
1947      oss.str("");  
1948      oss << "SELECT GL_TIMESYNC.ID FROM GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW ORDER BY GL_TIMESYNC.ID DESC LIMIT 1;";      SetID_ROOT(glroot.ID);
1949      result = conn->Query(oss.str().c_str());      
1950      //  
1951      if ( !result ) throw -3;      return (0);
1952      //  }
1953      row = result->Next();  /**
1954      //   * Insert all the new rows into GL_ROOT.
1955      if ( !row ) throw -3;   * The raw file indicates in the parameters should be already been stored in the database.
1956      idtimesync = (UInt_t)atoll(row->GetField(0));   */
1957    };  Int_t PamelaDBOperations::insertPamelaRootFile(GL_ROOT *glroot){
1958      stringstream oss;
1959      TSQLResult *result = 0;
1960      TSQLRow    *row    = 0;
1961      //
1962    //    //
1963    oss.str("");    oss.str("");
1964    if ( STATIC ){    oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1965      oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"        << glroot->ID_RAW << "', '" << glroot->ID_TIMESYNC << "', '" << glroot->PATH << "', '" << glroot->NAME << "')";
         << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";  
   } else {  
     oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"  
         << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')";  
   };  
1966    //    //
1967    if ( debug ) printf(" query is %s \n",oss.str().c_str());    if ( debug ) printf("%s \n",oss.str().c_str());
1968    if (conn->Query(oss.str().c_str()) == 0) throw -4;    if (conn->Query(oss.str().c_str()) == 0) throw -4;
1969    //    //
1970    delete result;    delete result;
1971    //    //
1972    oss.str("");    oss.str("");
1973    //  oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";    //  oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";
1974    if ( STATIC ){    oss << "SELECT ID FROM GL_ROOT WHERE PATH='" << glroot->PATH << "' and NAME='"<< glroot->NAME <<"';";
     oss << "SELECT ID FROM GL_ROOT WHERE PATH='" << this->GetRootPath().Data() << "' and NAME='"<< this->GetRootFile().Data() <<"';";  
   } else {  
     oss << "SELECT ID FROM GL_ROOT WHERE PATH='$PAM_L0' and NAME='"<< this->GetRootFile().Data() <<"';";  
   };  
1975    //    //
1976    if ( debug ) printf(" query is %s \n",oss.str().c_str());    if ( debug ) printf("%s \n",oss.str().c_str());
1977    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1978    if ( !result ) throw -12;    if ( !result ) throw -12;
1979    row = result->Next();    row = result->Next();
1980    if ( !row ) throw -3;    if ( !row ) throw -3;
1981    this->SetID_ROOT((UInt_t)atoll(row->GetField(0)));  
1982    if ( debug ) printf(" The ID of the ROOT file is %u \n",this->GetID_ROOT());    glroot->ID = (UInt_t)atoll(row->GetField(0));
1983    
1984      if ( debug ) printf(" The ID of the ROOT file is %u \n",glroot->ID);
1985    //    //
1986    delete result;    delete result;
1987    //    //
# Line 2061  Int_t PamelaDBOperations::insertPamelaRU Line 2209  Int_t PamelaDBOperations::insertPamelaRU
2209            cod = ehh->GetCounter();            cod = ehh->GetCounter();
2210            tcod = (UInt_t)cod->Get(pctp->Physics);            tcod = (UInt_t)cod->Get(pctp->Physics);
2211            evbefh = TMath::Max(chminentry,tcod);            evbefh = TMath::Max(chminentry,tcod);
2212  //        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
2213            if ( (UInt_t)evbefh == upperentry || !upperentry ) evbefh = upperentry + 1;            if ( (UInt_t)evbefh == upperentry || !upperentry ) evbefh = upperentry + 1;
2214  //            //
2215          if ( debug ) printf(" evbefh %i upperentry %u \n",evbefh,upperentry);            if ( debug ) printf(" evbefh %i upperentry %u \n",evbefh,upperentry);
2216            //            //
2217            this->HandleRunFragments(false,true,evbefh,upperentry);            this->HandleRunFragments(false,true,evbefh,upperentry);
2218            //            //
# Line 2169  Int_t PamelaDBOperations::insertPamelaRU Line 2317  Int_t PamelaDBOperations::insertPamelaRU
2317                //                //
2318                this->HandleRunFragments(true,true,chminentry,(evbefh));                this->HandleRunFragments(true,true,chminentry,(evbefh));
2319                //                //
2320                  // what about pth=ptht-1
2321              };              };
2322            };            };
2323            //            //
# Line 2231  Int_t PamelaDBOperations::insertPamelaRU Line 2380  Int_t PamelaDBOperations::insertPamelaRU
2380          //      if ( ptt+1 == rtev){            //      if ( ptt+1 == rtev){  
2381          if ( conptt+1 == nrtev ){            if ( conptt+1 == nrtev ){  
2382            //    if ( conptt+1 == (nrtev+nrtbef )){              //    if ( conptt+1 == (nrtev+nrtbef )){  
2383              if ( IsDebug() ) printf(" >>>>>>>>>>> %i %u %i %u \n",ptht,rhev,nrtev,conptt);
2384            ptht++;            ptht++;
2385            if ( ptht < rhev ){            if ( ptht < rhev ){
2386              rh->GetEntry(ptht);              rh->GetEntry(ptht);
2387                //pth++;
2388                //if ( pth < rhev ){
2389                //rh->GetEntry(pth);
2390              phh = ehh->GetPscuHeader();              phh = ehh->GetPscuHeader();
2391              pkth = phh->GetCounter();              pkth = phh->GetCounter();
2392              obth = phh->GetOrbitalTime();              obth = phh->GetOrbitalTime();
# Line 2258  Int_t PamelaDBOperations::insertPamelaRU Line 2411  Int_t PamelaDBOperations::insertPamelaRU
2411                if ( IsDebug() ) printf(" Piece of run at the end of the file with NO RUNHEADER! evbeft %u upperentry-1 %u \n",(UInt_t)evbeft,upperentry-1);                if ( IsDebug() ) printf(" Piece of run at the end of the file with NO RUNHEADER! evbeft %u upperentry-1 %u \n",(UInt_t)evbeft,upperentry-1);
2412                //                //
2413                if ( (ptt-1) < 0 ) throw -15; // should never arrive here!                if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
2414                rt->GetEntry(ptt-1);                //              rt->GetEntry(ptt-1);
2415                  rt->GetEntry(rtev-1);
2416                cod = eht->GetCounter();                cod = eht->GetCounter();
2417                tcod = (UInt_t)cod->Get(pctp->Physics);                tcod = (UInt_t)cod->Get(pctp->Physics)+1;
2418                evbefh = TMath::Max(chminentry,tcod);                evbefh = TMath::Max(chminentry,tcod);
2419                //            evbefh = cod->Get(pctp->Physics);                //            evbefh = cod->Get(pctp->Physics);
2420                rt->GetEntry(ptt);                rt->GetEntry(ptt);
# Line 2367  Bool_t PamelaDBOperations::IsRunAlreadyI Line 2521  Bool_t PamelaDBOperations::IsRunAlreadyI
2521        //        //
2522        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");
2523        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2524  //       oss.str("");        //       oss.str("");
2525  //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2526  //       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());      
2527  //       conn->Query(oss.str().c_str());        //       conn->Query(oss.str().c_str());
2528        if ( signal ) signal = false;        if ( signal ) signal = false;
2529        goto gonext;              goto gonext;      
2530        //        //
# Line 2385  Bool_t PamelaDBOperations::IsRunAlreadyI Line 2539  Bool_t PamelaDBOperations::IsRunAlreadyI
2539        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");
2540        //        //
2541        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2542  //       oss.str("");        //       oss.str("");
2543  //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2544  //       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());      
2545  //       conn->Query(oss.str().c_str());        //       conn->Query(oss.str().c_str());
2546        //        //
2547        if ( signal ) signal = false;        if ( signal ) signal = false;
2548        goto gonext;        goto gonext;
# Line 2403  Bool_t PamelaDBOperations::IsRunAlreadyI Line 2557  Bool_t PamelaDBOperations::IsRunAlreadyI
2557        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");
2558        //        //
2559        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2560  //       oss.str("");        //       oss.str("");
2561  //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2562  //       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());      
2563  //       conn->Query(oss.str().c_str());        //       conn->Query(oss.str().c_str());
2564        if ( signal ) signal = false;        if ( signal ) signal = false;
2565        //        //
2566      };      };
# Line 2488  void PamelaDBOperations::HandleRunFragme Line 2642  void PamelaDBOperations::HandleRunFragme
2642      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");
2643      //      //
2644    } 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 ){  
2645      //      //
2646      // 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
2647      //      //
2648      if ( IsDebug() ) printf(" The run is new \n");      if ( rhfirstev != firstev && !mishead ) mishead = true;
2649      if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");      if ( rtlastev != lastev && !mistrail ) mistrail = true;
2650      //      //
2651      glrun->SetID(this->AssignRunID());      this->FillClass(mishead,mistrail,firstev,lastev);
     glrun->SetID_RUN_FRAG(0);  
     glrun->Fill_GL_RUN_FRAGMENTS(conn);  
2652      //      //
2653    } else {      if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
2654      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());
2655      if ( PEDANTIC ) throw -69;      //
2656      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  
2657      //      //
2658      oss.str("");      oss.str("");
2659      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 "
2660          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2661          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "          << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2662          << " ID != " << glrun->ID          << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2663          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2664            << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2665            << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2666            << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2667            << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2668            << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2669            << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2670            << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2671            << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2672            << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2673      //      //
2674      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());
2675      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
2676      //      //
2677      if ( !result ) throw -4;      if ( !result ) throw -4;
2678      //      //
2679      row = result->Next();      row = result->Next();
2680      //      //
2681      if ( !row && NoFrag() ){      if ( !row ){
2682          //
2683          // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
2684          //
2685          if ( IsDebug() ) printf(" The run is new \n");
2686          if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
2687          //
2688          glrun->SetID(this->AssignRunID());
2689          glrun->SetID_RUN_FRAG(0);
2690          glrun->Fill_GL_RUN_FRAGMENTS(conn);
2691          //
2692        } else {
2693          if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
2694          if ( PEDANTIC ) throw -69;
2695          return;
2696        };
2697        //
2698        if ( chewbacca && mishead && mistrail ) goto justcheck;
2699        //
2700        // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
2701        //    
2702        if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
2703          // missing it no way we can found a piece in the frag table
2704        //        //
2705        oss.str("");        oss.str("");
2706        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 "
2707            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2708            << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "            << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2709            << " ID != " << glrun->ID            << " ID != " << glrun->ID
2710            << " 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!!
2711        //        //
2712        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());
2713        result = conn->Query(oss.str().c_str());        result = conn->Query(oss.str().c_str());
2714        //        //
2715        if ( !result ) throw -4;        if ( !result ) throw -4;
2716        //        //
       foundinrun = true;  
       //  
2717        row = result->Next();        row = result->Next();
2718        //        //
2719      };        if ( !row && NoFrag() ){
     //  
     if ( !row ){  
       if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");  
       found = false;  
     } else {  
       //  
       found = false; // default value  
       //  
       if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");  
       //  
       // if we have both runheader and runtrailer we can check with pkt_counter:  
       //  
       if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){  
         ULong64_t chkpkt = 0;    
         ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());  
         ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));  
2720          //          //
2721          chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;            oss.str("");
2722            oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
2723                << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2724                << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2725                << " ID != " << glrun->ID
2726                << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
2727            //
2728            if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2729            result = conn->Query(oss.str().c_str());
2730            //
2731            if ( !result ) throw -4;
2732            //
2733            foundinrun = true;
2734            //
2735            row = result->Next();
2736          //          //
         if ( labs(chkpkt-pktt)<2 ){  
           //  
           if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);  
           //  
           found = true;  
           //  
         } else {  
           //  
           if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);        
           //  
           found = false;  
           //  
         };  
2737        };        };
2738        if ( !found && chewbacca ) goto justcheck;        //
2739        if ( !found ){        if ( !row ){
2740            if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2741            found = false;
2742          } else {
2743          //          //
2744          // 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          found = false; // default value
2745          //          //
2746          ULong64_t chkpkt1 = 0;          if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
         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);  
2747          //          //
2748          if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){          // if we have both runheader and runtrailer we can check with pkt_counter:
2749            //    if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){          //
2750            //          if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
2751            if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);            ULong64_t chkpkt = 0;  
2752              ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
2753              ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
2754            //            //
2755            found = true;            chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;  
2756            //            //
2757          } else {            if ( labs(chkpkt-pktt)<2 ){
2758                //
2759                if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2760                //
2761                found = true;
2762                //
2763              } else {
2764                //
2765                if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);            
2766                //
2767                found = false;
2768                //
2769              };
2770            };
2771            if ( !found && chewbacca ) goto justcheck;
2772            if ( !found ){
2773            //            //
2774            if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);            // 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
2775            //            //
2776            found = false;            ULong64_t chkpkt1 = 0;
2777              ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2778              ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2779              chkpkt1 = labs(orunh1-dbrunt1);
2780            //            //
2781              ULong64_t chkpkt2 = 0;
2782              ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
2783              ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2784              chkpkt2 = labs(orunh2-dbrunt2);
2785              //
2786              ULong64_t chkpkt3 = 0;
2787              ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
2788              ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2789              chkpkt3 = labs(orunh3-dbrunt3);
2790              //
2791              if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2792                //  if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
2793                //
2794                if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2795                //
2796                found = true;
2797                //
2798              } else {
2799                //
2800                if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2801                //
2802                found = false;
2803                //
2804              };
2805          };          };
2806        };        };
     };  
     //  
     if ( found ){  
       //  
       // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table  
       //  
       if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");  
       //  
       if ( foundinrun ){  
         glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");  
         glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");  
       };  
2807        //        //
2808        GL_RUN *glrun1 = new GL_RUN();        if ( found ){
2809        //          //
2810        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));          // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table
2811        //          //
2812        oss.str("");          if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2813        oss << " ID="<<row->GetField(0)<<";";          //
2814        //          if ( foundinrun ){
2815        glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2816        //            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2817        // merge infos          };
2818        //          //
2819        UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());          GL_RUN *glrun1 = new GL_RUN();
2820        ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());          //
2821        UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());          //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2822        ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());          //
2823        if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);          oss.str("");
2824        TTree *T= 0;          oss << " ID="<<row->GetField(0)<<";";
2825        T = (TTree*)file->Get("Physics");          //
2826        if ( !T || T->IsZombie() ) throw -16;          glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
2827        EventHeader *eh = 0;          //
2828        PscuHeader *ph = 0;          // merge infos
2829        T->SetBranchAddress("Header", &eh);          //
2830        while ( apkt > bpkt && aobt > bobt && firstev < lastev ){          UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
2831          T->GetEntry(firstev);          ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
2832          ph = eh->GetPscuHeader();          UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
2833          bpkt = PKT(ph->GetCounter());          ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
2834          bobt = OBT(ph->GetOrbitalTime());                if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2835          firstev++;          TTree *T= 0;
2836          if ( PEDANTIC ) throw -71;          T = (TTree*)file->Get("Physics");
2837        };          if ( !T || T->IsZombie() ) throw -16;
2838        if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);          EventHeader *eh = 0;
2839        //          PscuHeader *ph = 0;
2840        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());          T->SetBranchAddress("Header", &eh);
2841        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());          while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
2842        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());            T->GetEntry(firstev);
2843        glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());            ph = eh->GetPscuHeader();
2844        glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());            bpkt = PKT(ph->GetCounter());
2845        //            bobt = OBT(ph->GetOrbitalTime());    
2846        glrun->SetEV_FROM(firstev);            firstev++;
2847        glrun->SetNEVENTS(lastev-firstev+1);            if ( PEDANTIC ) throw -71;
2848        //          };
2849        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());          if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2850        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());          //
2851        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());          glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2852        glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());          glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
2853        glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());          glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
2854        glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());          glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
2855        glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());          glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
2856        glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());          //
2857        glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());          glrun->SetEV_FROM(firstev);
2858        glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());          glrun->SetNEVENTS(lastev-firstev+1);
2859        glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());            //
2860        glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());            glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
2861        glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());            glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
2862        glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());            glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
2863        glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());            glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
2864        glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());            glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
2865        //          glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
2866        if ( !IsRunAlreadyInserted() ){          glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
2867            glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
2868            glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
2869            glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
2870            glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());  
2871            glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());  
2872            glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());  
2873            glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());  
2874            glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());  
2875            glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());  
2876            //
2877            if ( glrun1->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glrun1->GetPHYSENDRUN_MASK_S3S2S12());
2878            if ( glrun1->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glrun1->GetPHYSENDRUN_MASK_S11CRC());
2879            //
2880            if ( !IsRunAlreadyInserted() ){
2881              //
2882              //    glrun->SetID(this->AssignRunID());
2883              glrun->SetID_RUN_FRAG(glrun1->GetID());
2884              glrun->Fill_GL_RUN(conn);
2885              //
2886              // set id number
2887              //
2888              glrun1->SetID_RUN_FRAG(glrun->GetID());
2889              glrun1->Fill_GL_RUN(conn);
2890              //
2891            };
2892            // delete old entry in fragment table
2893            //
2894            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2895            glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2896          //          //
2897          //      glrun->SetID(this->AssignRunID());          delete glrun1;
         glrun->SetID_RUN_FRAG(glrun1->GetID());  
         glrun->Fill_GL_RUN(conn);  
2898          //          //
         // set id number  
2899          //          //
2900          glrun1->SetID_RUN_FRAG(glrun->GetID());          return;
         glrun1->Fill_GL_RUN(conn);  
2901          //          //
2902        };        };
       // delete old entry in fragment table  
       //  
       glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");  
       glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");  
       //  
       delete glrun1;  
       //  
       //  
       return;  
2903        //        //
2904      };      };
2905      //      //
2906    };      if ( mistrail && ( rtlastev == lastev || chewbacca )) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
2907    //        // 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() ){  
2908        //        //
2909        oss.str("");        oss.str("");
2910        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 "
2911            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2912            << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "            << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2913            << " ID != " << glrun->ID            << " ID != " << glrun->ID
2914            << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";            << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2915        //        //
2916        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());
2917        result = conn->Query(oss.str().c_str());        result = conn->Query(oss.str().c_str());
2918        //        //
2919        if ( !result ) throw -4;        if ( !result ) throw -4;
2920        //        //
       foundinrun = true;  
2921        row = result->Next();        row = result->Next();
2922        //        //
2923      };        if ( !row && NoFrag() ){
2924      //          //
2925      if ( !row ){          oss.str("");
2926        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 "
2927        found = false;              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2928      } else {              << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2929        //              << " ID != " << glrun->ID
2930        found = false; // default value              << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2931        //          //
2932        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());
2933        //          result = conn->Query(oss.str().c_str());
2934        // if we have both runheader and runtrailer we can check with pkt_counter:          //
2935            if ( !result ) throw -4;
2936            //
2937            foundinrun = true;
2938            row = result->Next();
2939            //
2940          };
2941        //        //
2942        if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){        if ( !row ){
2943          ULong64_t chkpkt = 0;            if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2944          ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));          found = false;
2945          ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());        } else {
2946          //          //
2947          chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;            found = false; // default value
2948          //          //
2949          if ( labs(chkpkt-pktt)<2 ){          if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
2950            //
2951            // if we have both runheader and runtrailer we can check with pkt_counter:
2952            //
2953            if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){
2954              ULong64_t chkpkt = 0;  
2955              ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
2956              ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2957            //            //
2958            if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);            chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;  
2959            //            //
2960            found = true;            if ( labs(chkpkt-pktt)<2 ){
2961                //
2962                if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2963                //
2964                found = true;
2965                //
2966              } else {
2967                //
2968                if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);            
2969                //
2970                found = false;
2971                //
2972              };
2973            };
2974            if ( !found && chewbacca ) goto justcheck;
2975            if ( !found ){
2976            //            //
2977          } 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
2978            //            //
2979            if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);                  ULong64_t chkpkt1 = 0;
2980              ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
2981              ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2982              chkpkt1 = labs(orunh1-dbrunt1);
2983            //            //
2984            found = false;            ULong64_t chkpkt2 = 0;
2985              ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT());
2986              ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2987              chkpkt2 = labs(orunh2-dbrunt2);
2988              //
2989              ULong64_t chkpkt3 = 0;
2990              ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME());
2991              ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2992              chkpkt3 = labs(orunh3-dbrunt3);
2993            //            //
2994              if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2995                //
2996                if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2997                //
2998                found = true;
2999                //
3000              } else {
3001                //
3002                if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
3003                //
3004                found = false;
3005                //
3006              };
3007          };          };
3008        };        };
3009        if ( !found && chewbacca ) goto justcheck;        //
3010        if ( !found ){        if ( found ){
3011            //
3012            // 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
3013            //
3014            if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
3015          //          //
3016          // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts          if ( foundinrun ){
3017              glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3018              glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
3019            };
3020          //          //
3021          ULong64_t chkpkt1 = 0;          GL_RUN *glrun1 = new GL_RUN();
         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);  
3022          //          //
3023          ULong64_t chkpkt2 = 0;          //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
         ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT());  
         ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));  
         chkpkt2 = labs(orunh2-dbrunt2);  
3024          //          //
3025          ULong64_t chkpkt3 = 0;          oss.str("");
3026          ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME());          oss << " ID="<<row->GetField(0)<<";";
         ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));  
         chkpkt3 = labs(orunh3-dbrunt3);  
3027          //          //
3028          if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){          glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runtrailer infos
3029            //          //
3030            if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);          // merge infos
3031            //          //
3032            found = true;          UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT());
3033            //          ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
3034          } else {          UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
3035            ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
3036            if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
3037            TTree *T= 0;
3038            T = (TTree*)file->Get("Physics");
3039            if ( !T || T->IsZombie() ) throw -16;
3040            EventHeader *eh = 0;
3041            PscuHeader *ph = 0;
3042            T->SetBranchAddress("Header", &eh);
3043            while ( apkt > bpkt && aobt > bobt && lastev > 0 ){
3044              T->GetEntry(lastev);
3045              ph = eh->GetPscuHeader();
3046              apkt = PKT(ph->GetCounter());
3047              aobt = OBT(ph->GetOrbitalTime());    
3048              lastev--;
3049              if ( PEDANTIC ) throw -72;
3050            };
3051            if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
3052            //
3053            glrun->SetEV_TO(lastev);
3054            glrun->SetNEVENTS(lastev-firstev+1);
3055            glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());
3056            glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER());
3057            glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME());
3058            glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
3059            glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
3060            //
3061            glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
3062            glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
3063            glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
3064            glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP());
3065            glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE());
3066            glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE());
3067            glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A());
3068            glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B());
3069            glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO());
3070            glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO());
3071            glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB());  
3072            glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE());  
3073            glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED());  
3074            glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK());  
3075            glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC());  
3076            glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC());  
3077            //
3078            if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ) glrun1->SetPHYSENDRUN_MASK_S3S2S12(glrun->GetPHYSENDRUN_MASK_S3S2S12());
3079            if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) glrun1->SetPHYSENDRUN_MASK_S11CRC(glrun->GetPHYSENDRUN_MASK_S11CRC());
3080            //
3081            if ( !IsRunAlreadyInserted() ){
3082              //
3083              //    glrun->SetID(this->AssignRunID());
3084              //
3085              glrun->SetID_RUN_FRAG(glrun1->GetID());
3086              glrun->Fill_GL_RUN(conn);
3087            //            //
3088            if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);            // set id number
3089            //            //
3090            found = false;            glrun1->SetID_RUN_FRAG(glrun->GetID());
3091              glrun1->Fill_GL_RUN(conn);
3092            //            //
3093          };          };
3094            //
3095            // delete old entries in fragment table
3096            //
3097            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3098            glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3099            //
3100            delete glrun1;
3101            //
3102            return;
3103            //
3104        };        };
3105          //
3106      };      };
3107      //      //
3108      if ( found ){    justcheck:
3109        //      //
3110        // 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");  
       };  
3111        //        //
3112        GL_RUN *glrun1 = new GL_RUN();        if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
3113        //        //
3114        //      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?
3115        //        //
3116        oss.str("");        oss.str("");
3117        oss << " ID="<<row->GetField(0)<<";";        oss << " SELECT ID FROM GL_RUN WHERE "
3118              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3119              << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3120              << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3121              << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3122              << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3123              << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3124              << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3125              << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3126              << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3127              << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3128              << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3129              << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3130              << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3131        //        //
3132        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());
3133          result = conn->Query(oss.str().c_str());
3134        //        //
3135        // merge infos        if ( !result ) throw -4;
3136        //        //
3137        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());    
3138        //        //
3139        if ( !IsRunAlreadyInserted() ){        if ( row ){
3140          //          if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
3141          //      glrun->SetID(this->AssignRunID());          if ( PEDANTIC ) throw -70;
3142          //        } else {
3143          glrun->SetID_RUN_FRAG(glrun1->GetID());          if ( NoFrag() ){
3144          glrun->Fill_GL_RUN(conn);            glrun->SetID_RUN_FRAG(glrun->GetID());
3145          //            glrun->Fill_GL_RUN(conn);    
3146          // set id number            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3147          //          };    
         glrun1->SetID_RUN_FRAG(glrun->GetID());  
         glrun1->Fill_GL_RUN(conn);  
         //  
3148        };        };
       //  
       // 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");  
       };      
3149      };      };
   };  
3150    }; // EEE    }; // EEE
3151    //    //
3152    return;    return;
# Line 3068  Bool_t PamelaDBOperations::IsRunConsiste Line 3228  Bool_t PamelaDBOperations::IsRunConsiste
3228      code = eh->GetCounter();      code = eh->GetCounter();
3229      checkfirst = 0;      checkfirst = 0;
3230      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3231         if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
3232       };      };
3233      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);
3234      //      //
3235      T->GetEntry(lastev);      T->GetEntry(lastev);
3236      code = eh->GetCounter();      code = eh->GetCounter();
3237      checklast = 0;      checklast = 0;
3238      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3239         if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
3240       };      };
3241      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);
3242      //      //
3243      if ( checkfirst == checklast ){      if ( checkfirst == checklast ){
# Line 3088  Bool_t PamelaDBOperations::IsRunConsiste Line 3248  Bool_t PamelaDBOperations::IsRunConsiste
3248        //        //
3249      } else {      } else {
3250        //        //
3251        if ( IsDebug() ) printf(" There are no-phyics packets inside the run!\n");        if ( IsDebug() ) printf(" There are no-physics packets inside the run!\n");
3252        //        //
3253        // HERE WE MUST HANDLE THAT RUNS AND GO BACK        // HERE WE MUST HANDLE THAT RUNS AND GO BACK
3254        //        //
3255  //      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");
3256        //        //
3257        Bool_t emptyruns = false;        Bool_t emptyruns = false;
3258        UInt_t check = 0;        UInt_t check = 0;
3259        UInt_t lastevtemp = lastev;        UInt_t lastevtemp = lastev;
3260        UInt_t firstevno = firstev;        UInt_t firstevno = firstev;
3261          UInt_t rhchko=0;
3262          UInt_t rhchk=0;
3263        //        //
3264        for (UInt_t i=firstev; i<=lastev; i++){        for (UInt_t i=firstev; i<=lastev; i++){
3265          //          //
# Line 3106  Bool_t PamelaDBOperations::IsRunConsiste Line 3268  Bool_t PamelaDBOperations::IsRunConsiste
3268          //          //
3269          check = 0;          check = 0;
3270          //          //
3271    
3272            // if we have a runheader set lastev then exit
3273            //
3274          for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){          for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3275            if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));            if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
3276          };          };
3277            // check here if we have a runheader
3278            rhchko = rhchk;  
3279            rhchk = code->Get(GetPacketType("RunHeader"));    
3280          //          //
3281          if ( checkfirst < check || i == lastev ){          if ( checkfirst < check || i == lastev ){
3282            //            //
# Line 3184  Bool_t PamelaDBOperations::IsRunConsiste Line 3352  Bool_t PamelaDBOperations::IsRunConsiste
3352            };            };
3353            //                  //      
3354            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
3355              this->SetPhysEndRunVariables();
3356            //            //
3357            if ( chminentry == firstentry  ){ // EEE            if ( chminentry == firstentry  ){ // EEE
3358              if ( IsDebug() ) printf(" Inside isrunconsistent found a fragment of run at the beginning of the file, put it in the fragment table \n");              if ( IsDebug() ) printf(" Inside isrunconsistent found a fragment of run at the beginning of the file, put it in the fragment table \n");
3359  //              //
3360  //          this->HandleRunFragments(true,mistrail,firstentry,lastentry); // cannot call it here since it enters a loop which will destroy the already stored variables if we arrive here from HandleRunFragments              //      this->HandleRunFragments(true,mistrail,firstentry,lastentry); // cannot call it here since it enters a loop which will destroy the already stored variables if we arrive here from HandleRunFragments
3361  //              //
   
   
   
   
   
   
   
   
   
   
   
   
   
   mishead = true;  
   
   
   UInt_t rhfirstev = firstentry;  
 //  UInt_t rtlastev = lastentry;  
   Bool_t found = false;  
   Bool_t foundinrun = false;  
   //  
   TSQLResult *result = 0;  
   TSQLRow    *row    = 0;  
   //  
   stringstream oss;  
   oss.str("");  
   //  
   // 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("zz The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");  
   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());  
   //  
   // 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 ){  
     //  
     // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)  
     //  
     if ( IsDebug() ) printf(" The run is new \n");  
     if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");  
     //  
     glrun->SetID(this->AssignRunID());  
     glrun->SetID_RUN_FRAG(0);  
     glrun->Fill_GL_RUN_FRAGMENTS(conn);  
     //  
   } else {  
     if ( IsDebug() ) printf(" The run is already present in the fragment table \n");  
     if ( PEDANTIC ) throw -69;  
 //    return;  
   };  
   //  
   if ( chewbacca && mishead && mistrail ) goto zjustcheck;  
   //  
   // 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  
     //  
     oss.str("");  
     oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "  
         << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "  
         << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "  
         << " ID != " << glrun->ID  
         << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!  
     //  
     if ( IsDebug() ) printf(" look for runheader 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() ){  
       //  
       oss.str("");  
       oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "  
           << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "  
           << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "  
           << " ID != " << glrun->ID  
           << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!  
       //  
       if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());  
       result = conn->Query(oss.str().c_str());  
       //  
       if ( !result ) throw -4;  
       //  
       foundinrun = true;  
       //  
       row = result->Next();  
       //  
     };  
     //  
     if ( !row ){  
       if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");  
       found = false;  
     } else {  
       //  
       found = false; // default value  
       //  
       if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");  
       //  
       // if we have both runheader and runtrailer we can check with pkt_counter:  
       //  
       if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){  
         ULong64_t chkpkt = 0;  
         ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());  
         ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));  
         //  
         chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;  
         //  
         if ( labs(chkpkt-pktt)<2 ){  
           //  
           if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);  
           //  
           found = true;  
           //  
         } else {  
           //  
           if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);  
           //  
           found = false;  
           //  
         };  
       };  
       if ( !found && chewbacca ) goto zjustcheck;  
       if ( !found ){  
         //  
         // 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  
         //  
         ULong64_t chkpkt1 = 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);  
         //  
         if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){  
           //    if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){  
           //  
           if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);  
           //  
           found = true;  
           //  
         } else {  
           //  
           if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);  
           //  
           found = false;  
           //  
         };  
       };  
     };  
     //  
     if ( found ){  
       //  
       // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table  
       //  
       if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");  
       //  
       if ( foundinrun ){  
         glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");  
         glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");  
       };  
       //  
       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() ){  
         //  
         //      glrun->SetID(this->AssignRunID());  
         glrun->SetID_RUN_FRAG(glrun1->GetID());  
         glrun->Fill_GL_RUN(conn);  
         //  
         // set id number  
         //  
         glrun1->SetID_RUN_FRAG(glrun->GetID());  
         glrun1->Fill_GL_RUN(conn);  
         //  
       };  
       // delete old entry in fragment table  
       //  
       glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");  
       glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");  
       //  
       delete glrun1;  
       //  
       //  
 //      return;  
       //  
     };  
     //  
   };  
   //  
   //  
  zjustcheck:  
   //  
   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");  
       };  
     };  
   }; // EEE  
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
3362    
3363                mishead = true;
3364    
3365    
3366                UInt_t rhfirstev = firstentry;
3367                //  UInt_t rtlastev = lastentry;
3368                Bool_t found = false;
3369                Bool_t foundinrun = false;
3370                //
3371                TSQLResult *result = 0;
3372                TSQLRow    *row    = 0;
3373                //
3374                stringstream oss;
3375                oss.str("");
3376                //
3377                // we have now the good first piece of a run, fill the glrun object
3378                //
3379                //  if ( rhfirstev != firstev && !mishead ) mishead = true;
3380                //  if ( rtlastev != lastev && !mistrail ) mistrail = true;
3381                //
3382                //  this->FillClass(mishead,mistrail,firstev,lastev);
3383                //
3384                if ( IsDebug() ) printf("zz The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
3385                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());
3386                //
3387                // First of all insert the run in the fragment table...
3388                //
3389                oss.str("");
3390                oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
3391                    << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3392                    << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3393                    << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3394                    << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3395                    << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3396                    << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3397                    << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3398                    << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3399                    << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3400                    << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3401                    << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3402                    << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3403                    << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3404                //
3405                if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3406                result = conn->Query(oss.str().c_str());
3407                //
3408                if ( !result ) throw -4;
3409                //
3410                row = result->Next();
3411                //
3412                if ( !row ){
3413                  //
3414                  // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
3415                  //
3416                  if ( IsDebug() ) printf(" The run is new \n");
3417                  if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
3418                  //
3419                  glrun->SetID(this->AssignRunID());
3420                  glrun->SetID_RUN_FRAG(0);
3421                  glrun->Fill_GL_RUN_FRAGMENTS(conn);
3422                  //
3423                } else {
3424                  if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
3425                  if ( PEDANTIC ) throw -69;
3426                  //    return;
3427                };
3428                //
3429                if ( chewbacca && mishead && mistrail ) goto zjustcheck;
3430                //
3431                // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
3432                //
3433                if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
3434                  // missing it no way we can found a piece in the frag table
3435                  //
3436                  oss.str("");
3437                  oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
3438                      << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3439                      << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3440                      << " ID != " << glrun->ID
3441                      << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
3442                  //
3443                  if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
3444                  result = conn->Query(oss.str().c_str());
3445                  //
3446                  if ( !result ) throw -4;
3447                  //
3448                  row = result->Next();
3449                  //
3450                  if ( !row && NoFrag() ){
3451                    //
3452                    oss.str("");
3453                    oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
3454                        << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3455                        << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3456                        << " ID != " << glrun->ID
3457                        << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
3458                    //
3459                    if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
3460                    result = conn->Query(oss.str().c_str());
3461                    //
3462                    if ( !result ) throw -4;
3463                    //
3464                    foundinrun = true;
3465                    //
3466                    row = result->Next();
3467                    //
3468                  };
3469                  //
3470                  if ( !row ){
3471                    if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
3472                    found = false;
3473                  } else {
3474                    //
3475                    found = false; // default value
3476                    //
3477                    if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
3478                    //
3479                    // if we have both runheader and runtrailer we can check with pkt_counter:
3480                    //
3481                    if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
3482                      ULong64_t chkpkt = 0;
3483                      ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
3484                      ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
3485                      //
3486                      chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;
3487                      //
3488                      if ( labs(chkpkt-pktt)<2 ){
3489                        //
3490                        if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
3491                        //
3492                        found = true;
3493                        //
3494                      } else {
3495                        //
3496                        if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
3497                        //
3498                        found = false;
3499                        //
3500                      };
3501                    };
3502                    if ( !found && chewbacca ) goto zjustcheck;
3503                    if ( !found ){
3504                      //
3505                      // 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
3506                      //
3507                      ULong64_t chkpkt1 = 0;
3508                      ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
3509                      ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
3510                      chkpkt1 = labs(orunh1-dbrunt1);
3511                      //
3512                      ULong64_t chkpkt2 = 0;
3513                      ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
3514                      ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
3515                      chkpkt2 = labs(orunh2-dbrunt2);
3516                      //
3517                      ULong64_t chkpkt3 = 0;
3518                      ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
3519                      ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
3520                      chkpkt3 = labs(orunh3-dbrunt3);
3521                      //
3522                      if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
3523                        //    if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
3524                        //
3525                        if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
3526                        //
3527                        found = true;
3528                        //
3529                      } else {
3530                        //
3531                        if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
3532                        //
3533                        found = false;
3534                        //
3535                      };
3536                    };
3537                  };
3538                  //
3539                  if ( found ){
3540                    //
3541                    // 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
3542                    //
3543                    if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
3544                    //
3545                    if ( foundinrun ){
3546                      glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3547                      glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
3548                    };
3549                    //
3550                    GL_RUN *glrun1 = new GL_RUN();
3551                    //
3552                    //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
3553                    //
3554                    oss.str("");
3555                    oss << " ID="<<row->GetField(0)<<";";
3556                    //
3557                    glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
3558                    //
3559                    // merge infos
3560                    //
3561                    UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
3562                    ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
3563                    UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
3564                    ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
3565                    if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
3566                    TTree *T= 0;
3567                    T = (TTree*)file->Get("Physics");
3568                    if ( !T || T->IsZombie() ) throw -16;
3569                    EventHeader *eh = 0;
3570                    PscuHeader *ph = 0;
3571                    T->SetBranchAddress("Header", &eh);
3572                    while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
3573                      T->GetEntry(firstev);
3574                      ph = eh->GetPscuHeader();
3575                      bpkt = PKT(ph->GetCounter());
3576                      bobt = OBT(ph->GetOrbitalTime());
3577                      firstev++;
3578                      if ( PEDANTIC ) throw -71;
3579                    };
3580                    if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
3581                    //
3582                    glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
3583                    glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
3584                    glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
3585                    glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
3586                    glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
3587                    //
3588                    glrun->SetEV_FROM(firstev);
3589                    glrun->SetNEVENTS(lastev-firstev+1);
3590                    //
3591                    glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
3592                    glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
3593                    glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
3594                    glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
3595                    glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
3596                    glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
3597                    glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
3598                    glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
3599                    glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
3600                    glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
3601                    glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());
3602                    glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());
3603                    glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());
3604                    glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());
3605                    glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());
3606                    glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());
3607                    //
3608                    if ( glrun1->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glrun1->GetPHYSENDRUN_MASK_S3S2S12());
3609                    if ( glrun1->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glrun1->GetPHYSENDRUN_MASK_S11CRC());
3610                    //
3611                    if ( !IsRunAlreadyInserted() ){
3612                      //
3613                      //      glrun->SetID(this->AssignRunID());
3614                      glrun->SetID_RUN_FRAG(glrun1->GetID());
3615                      glrun->Fill_GL_RUN(conn);
3616                      //
3617                      // set id number
3618                      //
3619                      glrun1->SetID_RUN_FRAG(glrun->GetID());
3620                      glrun1->Fill_GL_RUN(conn);
3621                      //
3622                    };
3623                    // delete old entry in fragment table
3624                    //
3625                    glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3626                    glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3627                    //
3628                    delete glrun1;
3629                    //
3630                    //
3631                    //      return;
3632                    //
3633                  };
3634                  //
3635                };
3636                //
3637                //
3638              zjustcheck:
3639                //
3640                if ( !found ){
3641                  //
3642                  if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
3643                  //
3644                  // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
3645                  //
3646                  oss.str("");
3647                  oss << " SELECT ID FROM GL_RUN WHERE "
3648                      << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3649                      << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3650                      << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3651                      << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3652                      << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3653                      << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3654                      << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3655                      << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3656                      << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3657                      << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3658                      << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3659                      << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3660                      << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3661                  //
3662                  if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3663                  result = conn->Query(oss.str().c_str());
3664                  //
3665                  if ( !result ) throw -4;
3666                  //
3667                  row = result->Next();
3668                  //
3669                  if ( row ){
3670                    if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
3671                    if ( PEDANTIC ) throw -70;
3672                  } else {
3673                    if ( NoFrag() ){
3674                      glrun->SetID_RUN_FRAG(glrun->GetID());
3675                      glrun->Fill_GL_RUN(conn);
3676                      glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3677                    };
3678                  };
3679                }; // EEE
3680    
3681    
3682            } else {            } else {
3683            if ( !IsRunAlreadyInserted() ){              if ( !IsRunAlreadyInserted() ){
3684              glrun->SetID(this->AssignRunID());                glrun->SetID(this->AssignRunID());
3685              glrun->SetID_RUN_FRAG(0);                glrun->SetID_RUN_FRAG(0);
3686              glrun->Fill_GL_RUN(conn);                glrun->Fill_GL_RUN(conn);
3687            };              };
3688            }; // EEE            }; // EEE
3689            //            //
3690            firstevno = lastentry + 1;            firstevno = lastentry + 1;
# Line 3564  Bool_t PamelaDBOperations::IsRunConsiste Line 3693  Bool_t PamelaDBOperations::IsRunConsiste
3693            //            //
3694          };          };
3695          //          //
3696            if ( i > firstev ){
3697              if ( rhchko != rhchk ){
3698                if ( IsDebug() ) printf("oh oh... we have a runheader! stop here and handle later the remaining piece\n");
3699                lastev = i;
3700                return(false);
3701              };
3702            };
3703            //
3704          if ( check == checklast && i != lastev ){          if ( check == checklast && i != lastev ){
3705            lastevtemp = i - 1;            lastevtemp = i - 1;
3706            i = lastev - 1;            i = lastev - 1;
# Line 3647  void PamelaDBOperations::HandleSuspiciou Line 3784  void PamelaDBOperations::HandleSuspiciou
3784      code = eh->GetCounter();      code = eh->GetCounter();
3785      checkfirst = 0;      checkfirst = 0;
3786      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3787         if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
3788         if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;        if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;
3789       };      };
3790      if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);      if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);
3791      //      //
3792      T->GetEntry(lastev);      T->GetEntry(lastev);
3793      code = eh->GetCounter();      code = eh->GetCounter();
3794      checklast = 0;      checklast = 0;
3795      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3796         if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
3797       };      };
3798      if ( IsDebug() ) printf(" Check last is %i \n",checklast);      if ( IsDebug() ) printf(" Check last is %i \n",checklast);
3799      //      //
3800      if ( checkfirst == checklast ){      if ( checkfirst == checklast ){
# Line 3769  void PamelaDBOperations::HandleSuspiciou Line 3906  void PamelaDBOperations::HandleSuspiciou
3906            };            };
3907            //                  //      
3908            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
3909              this->SetPhysEndRunVariables();
3910            //            //
3911            if ( !IsRunAlreadyInserted() ){            if ( !IsRunAlreadyInserted() ){
3912              glrun->SetID(this->AssignRunID());              glrun->SetID(this->AssignRunID());
# Line 3808  void PamelaDBOperations::HandleSuspiciou Line 3946  void PamelaDBOperations::HandleSuspiciou
3946              if ( IsDebug() ) printf(" We have the runtrailer \n");              if ( IsDebug() ) printf(" We have the runtrailer \n");
3947              //                  //    
3948              this->SetCommonGLRUN(firstTime,lastTime);              this->SetCommonGLRUN(firstTime,lastTime);
3949                this->SetPhysEndRunVariables();
3950              //              //
3951              if ( !IsRunAlreadyInserted() ){              if ( !IsRunAlreadyInserted() ){
3952                glrun->SetID(this->AssignRunID());                glrun->SetID(this->AssignRunID());
# Line 4382  Int_t PamelaDBOperations::insertCALOPULS Line 4521  Int_t PamelaDBOperations::insertCALOPULS
4521   * Fill the GL_TRK_CALIB table   * Fill the GL_TRK_CALIB table
4522   */   */
4523  void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){  void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){
4524    //      
4525    TSQLResult *result = 0;      GL_TRK_CALIB *glcal = new GL_TRK_CALIB();
   TSQLRow    *row    = 0;  
   //  
   stringstream oss;  
   oss.str("");  
   //  
   UInt_t totime = 0;  
   //  
   if ( !pk1 && !pk2 ){  
     if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n");  
     return;  
   };  
   //  
   // check if the calibration has already been inserted  
   //  
   oss.str("");  
   oss << " SELECT ID FROM GL_TRK_CALIB WHERE "  
       << " BOOT_NUMBER = "<< this->GetBOOTnumber(); //  
   oss << " AND ( ( ";    
   if ( pk1 ){  
     oss << " OBT1 = "<< obt1 << " AND "  
         << " PKT1 = "<< pkt1  
         << " ) OR ( ";  
   } else {  
     oss << " PKT1 = "<< pkt2-1  
         << " ) OR ( ";    
   };        
   if ( pk2 ){  
     oss << " OBT2 = "<< obt2 << " AND "  
         << " PKT2 = "<< pkt2;  
   } else {  
     oss << " PKT2 = "<< pkt1+1;  
   };        
   oss << " ) );";    
   //  
   if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str());  
   result = conn->Query(oss.str().c_str());  
   //  
   if ( !result ) throw -4;  
   //  
   row = result->Next();  
   //  
   if ( row ){  
     //  
     if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");  
     if ( PEDANTIC ) throw -80;  
4526      //      //
4527    } else {      glcal->ID         = 0;
4528        glcal->ID_ROOT_L0 = GetID_ROOT();
4529        glcal->EV_ROOT_CALIBTRK1 = t1;
4530        glcal->EV_ROOT_CALIBTRK2 = t2;
4531        glcal->FROM_TIME = fromtime;
4532        glcal->TO_TIME   = 0;
4533        glcal->OBT1      = obt1;
4534        glcal->OBT2      = obt2;
4535        glcal->PKT1      = pkt1;
4536        glcal->PKT2      = pkt2;
4537        glcal->BOOT_NUMBER = GetBOOTnumber();
4538        glcal->VALIDATION = valid;
4539      //      //
4540      // we have to insert a new calibration, check where to place it      HandleTRK_CALIB(glcal);
4541      //      //
4542      oss.str("");      delete glcal;
4543      oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "  }
4544          << " FROM_TIME < "<< fromtime << " AND "  /**
4545          << " TO_TIME > "<< fromtime << ";";   * Fill the GL_TRK_CALIB table
4546     */
4547    void PamelaDBOperations::HandleTRK_CALIB(GL_TRK_CALIB *glcal){
4548    
4549        Bool_t pk1 = (glcal->OBT1>0&&glcal->PKT1>0);
4550        Bool_t pk2 = (glcal->OBT2>0&&glcal->PKT2>0);
4551        UInt_t boot_number = glcal->BOOT_NUMBER;
4552        UInt_t obt1 = glcal->OBT1;
4553        UInt_t obt2 = glcal->OBT2;
4554        UInt_t pkt1 = glcal->PKT1;
4555        UInt_t pkt2 = glcal->PKT2;
4556        UInt_t fromtime = glcal->FROM_TIME;
4557        UInt_t totime = 0;
4558        UInt_t idroot = glcal->ID_ROOT_L0;
4559        UInt_t t1 = glcal->EV_ROOT_CALIBTRK1;
4560        UInt_t t2 = glcal->EV_ROOT_CALIBTRK2;
4561        UInt_t valid = glcal->VALIDATION;
4562      //      //
4563      if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());      TSQLResult *result = 0;
4564      result = conn->Query(oss.str().c_str());      TSQLRow    *row    = 0;
4565      //      //
4566      if ( !result ) throw -4;      stringstream oss;
4567        oss.str("");
4568      //      //
     row = result->Next();  
4569      //      //
4570      if ( !row ){      if ( !pk1 && !pk2 ){
4571        //          if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n");
4572        // no calibrations in the db contain our calibration          return;
       //  
       if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");  
       if ( fromtime < 1150871000 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode  
       //  
       oss.str("");  
       oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "  
           << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";  
       //  
       if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());  
       result = conn->Query(oss.str().c_str());  
       //  
       if ( !result ) throw -4;  
       //  
       row = result->Next();  
       if ( !row ){  
         totime = numeric_limits<UInt_t>::max();  
       } else {  
         totime = (UInt_t)atoll(row->GetField(0));  
       };  
       //  
     } else {  
       //  
       // determine upper and lower limits and make space for the new calibration  
       //  
       totime = (UInt_t)atoll(row->GetField(1));  
       //  
       oss.str("");  
       oss << " UPDATE GL_TRK_CALIB SET "    
           << " TO_TIME = "<< fromtime << " WHERE "  // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[  
           << " ID = "<< row->GetField(0) << ";";  
       //  
       if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str());  
       result = conn->Query(oss.str().c_str());  
       //  
       if ( !result ) throw -4;  
       //  
4573      };      };
4574      //      //
4575      oss.str("");      // check if the calibration has already been inserted
     oss << " INSERT INTO GL_TRK_CALIB (ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION) "  
         << " VALUES (NULL,' "  
         << idroot << "',";  
4576      //      //
4577      if ( !pk1 ){      oss.str("");
4578        oss << "NULL,";      oss << " SELECT ID FROM GL_TRK_CALIB WHERE "
4579            << " BOOT_NUMBER = "<< boot_number; //
4580        oss << " AND FROM_TIME="<<fromtime; /// NEWNEWNEW -- VA BENE ?!?!?!?!
4581        oss << " AND ( ( ";  
4582        if ( pk1 ){
4583            oss << " OBT1 = "<< obt1 << " AND "
4584                << " PKT1 = "<< pkt1
4585                << " ) OR ( ";
4586      } else {      } else {
4587        oss << "'"          oss << " PKT1 = "<< pkt2-1
4588            << t1 << "',";              << " ) OR ( ";  
4589      };      };      
4590      //      if ( pk2 ){
4591      if ( !pk2 ){          oss << " OBT2 = "<< obt2 << " AND "
4592        oss << "NULL,'";              << " PKT2 = "<< pkt2;
4593      } else {      } else {
4594        oss << "'"          oss << " PKT2 = "<< pkt1+1;
4595            << t2 << "','";      };      
4596      };      oss << " ) );";  
     //  
     oss << fromtime << "','"  
         << totime << "','"  
         << obt1 << "','"  
         << pkt1 << "','"  
         << obt2 << "','"  
         << pkt2 << "','"  
         << this->GetBOOTnumber() << "','"  
         << valid << "');";  
     //  
     if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());  
4597      //      //
4598        if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str());
4599      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
4600      //      //
4601      if ( !result ) throw -4;      if ( !result ) throw -4;
4602      //      //
4603    };      row = result->Next();
4604    //      //
4605        if ( row ){
4606            //
4607            if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");
4608            if ( PEDANTIC ) throw -80;
4609            //
4610        } else {
4611            //
4612            // we have to insert a new calibration, check where to place it
4613            //
4614            oss.str("");
4615            oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "
4616                << " FROM_TIME < "<< fromtime << " AND "
4617                << " TO_TIME > "<< fromtime << ";";
4618            //
4619            if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());
4620            result = conn->Query(oss.str().c_str());
4621            //
4622            if ( !result ) throw -4;
4623            //
4624            row = result->Next();
4625            //
4626            if ( !row ){
4627                //
4628                // no calibrations in the db contain our calibration
4629                //
4630                if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
4631                if ( fromtime < 1150871000 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode
4632                //
4633                oss.str("");
4634                oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
4635                    << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
4636                //
4637                if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
4638                result = conn->Query(oss.str().c_str());
4639                //
4640                if ( !result ) throw -4;
4641                //
4642                row = result->Next();
4643                if ( !row ){
4644                    totime = numeric_limits<UInt_t>::max();
4645                } else {
4646                    totime = (UInt_t)atoll(row->GetField(0));
4647                };
4648                //
4649            } else {
4650                //
4651                // determine upper and lower limits and make space for the new calibration
4652                //
4653                totime = (UInt_t)atoll(row->GetField(1));
4654                //
4655                oss.str("");
4656                oss << " UPDATE GL_TRK_CALIB SET "  
4657                    << " TO_TIME = "<< fromtime << " WHERE "  // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
4658                    << " ID = "<< row->GetField(0) << ";";
4659                //
4660                if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str());
4661                result = conn->Query(oss.str().c_str());
4662                //
4663                if ( !result ) throw -4;
4664                //
4665            };
4666            //
4667            oss.str("");
4668            oss << " INSERT INTO GL_TRK_CALIB (ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION) "
4669                << " VALUES (NULL,' "
4670                << idroot << "',";
4671            //
4672            if ( !pk1 ){
4673                oss << "NULL,";
4674            } else {
4675                oss << "'"
4676                    << t1 << "',";
4677            };
4678            //
4679            if ( !pk2 ){
4680                oss << "NULL,'";
4681            } else {
4682                oss << "'"
4683                    << t2 << "','";
4684            };
4685            //
4686            oss << fromtime << "','"
4687                << totime << "','"
4688                << obt1 << "','"
4689                << pkt1 << "','"
4690                << obt2 << "','"
4691                << pkt2 << "','"
4692                << boot_number << "','"
4693                << valid << "');";
4694            //
4695            if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());
4696            //
4697            result = conn->Query(oss.str().c_str());
4698            //
4699            if ( !result ) throw -4;
4700            //
4701        };
4702        
4703        oss.str("");
4704        oss << " SELECT ID FROM GL_TRK_CALIB ORDER BY ID DESC LIMIT 1 ;";  
4705        if ( IsDebug() ) cout << oss.str().c_str() << endl;
4706        result = conn->Query(oss.str().c_str());
4707        if ( !result ) throw -4;;
4708        row = result->Next();
4709        if(row)glcal->ID = (UInt_t)atoll(row->GetField(0));
4710    
4711        //
4712  };  };
4713    
4714  /**  /**
4715   * Scan tracker calibrations packets, fill the GL_TRK_CALIB table   * Scan tracker calibrations packets, fill the GL_TRK_CALIB table
4716   */   */
4717  Int_t PamelaDBOperations::insertTRK_CALIB(){  Int_t PamelaDBOperations::insertTRK_CALIB(){
   //  
   CalibTrk1Event *caltrk1 = 0;  
   CalibTrk2Event *caltrk2 = 0;  
   TTree *tr1 = 0;  
   TTree *tr2 = 0;  
   EventHeader *eh1 = 0;  
   PscuHeader *ph1 = 0;  
   EventHeader *eh2 = 0;  
   PscuHeader *ph2 = 0;  
   //  
   PacketType *pctp=0;  
   EventCounter *codt2=0;  
   //  
   Int_t nevents1 = 0;  
   Int_t nevents2 = 0;  
   //  
   fromtime = 0;  
   //  
   obt1 = 0;  
   pkt1 = 0;  
   obt2 = 0;  
   pkt2 = 0;  
   //  
   tr1 = (TTree*)file->Get("CalibTrk1");  
   if ( !tr1 || tr1->IsZombie() ) throw -22;  
   tr2 = (TTree*)file->Get("CalibTrk2");  
   if ( !tr2 || tr2->IsZombie() ) throw -23;  
   //  
   tr1->SetBranchAddress("CalibTrk1", &caltrk1);  
   tr1->SetBranchAddress("Header", &eh1);  
   nevents1 = tr1->GetEntries();  
   tr2->SetBranchAddress("CalibTrk2", &caltrk2);  
   tr2->SetBranchAddress("Header", &eh2);  
   nevents2 = tr2->GetEntries();  
   //  
   if ( !nevents1 && !nevents2 ) return(1);  
   //  
   t2 = -1;  
   Int_t pret2 = 0;  
   Int_t t2t1cal = 0;  
   //  
   for (t1=0; t1 < nevents1; t1++){  
4718      //      //
4719      pret2 = t2;      CalibTrk1Event *caltrk1 = 0;
4720      tr1->GetEntry(t1);      CalibTrk2Event *caltrk2 = 0;
4721        TTree *tr1 = 0;
4722        TTree *tr2 = 0;
4723        EventHeader *eh1 = 0;
4724        PscuHeader *ph1 = 0;
4725        EventHeader *eh2 = 0;
4726        PscuHeader *ph2 = 0;
4727      //      //
4728      ph1 = eh1->GetPscuHeader();      PacketType *pctp=0;
4729      obt1 = ph1->GetOrbitalTime();        EventCounter *codt2=0;
     pkt1 = ph1->GetCounter();    
     fromtime = this->GetAbsTime(ph1->GetOrbitalTime());    
4730      //      //
4731  //     valid = 1;      Int_t nevents1 = 0;
4732  //     //      Int_t nevents2 = 0;
 //     if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1  
4733      //      //
4734        fromtime = 0;
4735      //      //
4736      if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){      obt1 = 0;
4737        //    if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){      pkt1 = 0;
4738        //      obt2 = 0;
4739        if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u \n",fromtime,obt1,pkt1);      pkt2 = 0;
4740        //            //
4741        valid = ValidateTrkCalib( caltrk1, eh1 );      tr1 = (TTree*)file->Get("CalibTrk1");
4742        if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;      if ( !tr1 || tr1->IsZombie() ) throw -22;
4743        //      tr2 = (TTree*)file->Get("CalibTrk2");
4744        // Do we have the second calibration packet?      if ( !tr2 || tr2->IsZombie() ) throw -23;
4745        //      //
4746        while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1      tr1->SetBranchAddress("CalibTrk1", &caltrk1);
4747        tr1->SetBranchAddress("Header", &eh1);
4748        nevents1 = tr1->GetEntries();
4749        tr2->SetBranchAddress("CalibTrk2", &caltrk2);
4750        tr2->SetBranchAddress("Header", &eh2);
4751        nevents2 = tr2->GetEntries();
4752        //
4753        if ( !nevents1 && !nevents2 ) return(1);
4754        //
4755        t2 = -1;
4756        Int_t pret2 = 0;
4757        Int_t t2t1cal = 0;
4758        //
4759        bool MISSING_pkt1 = true;
4760        bool MISSING_pkt2 = true;
4761        int  ncalib = 0;
4762        bool try_to_recover = false;
4763        //
4764        for (t1=0; t1 < nevents1; t1++){//loop over packet1
4765            //
4766            pret2 = t2;
4767            tr1->GetEntry(t1);
4768          //          //
4769          t2++;          ph1 = eh1->GetPscuHeader();
4770            obt1 = ph1->GetOrbitalTime();  
4771            pkt1 = ph1->GetCounter();  
4772            fromtime = GetAbsTime(ph1->GetOrbitalTime());  
4773          //          //
4774          pret2 = t2 - 1; // EMILIANO          // chek if the packet number and obt are consistent with the other packets ???
4775          //          //
4776          if ( t2 < nevents2 ){          if ( PKT(pkt1) >= PKT(pktfirst) && PKT(pkt1) <= upperpkt && OBT(obt1) >= OBT(obtfirst) && OBT(obt1) <= upperobt ){
4777            tr2->GetEntry(t2);              //    if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){
4778            codt2 = eh2->GetCounter();              //
4779            t2t1cal = codt2->Get(pctp->CalibTrk1);              if ( IsDebug() ) printf("\n Trk calibration1 %u at time %u obt %u pkt %u \n",t1,fromtime,obt1,pkt1);
4780            //              //      
4781            ph2 = eh2->GetPscuHeader();              valid = ValidateTrkCalib( caltrk1, eh1 );
4782            obt2 = ph2->GetOrbitalTime();                if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
4783            pkt2 = ph2->GetCounter();                //
4784            //              // Do we have the second calibration packet?
4785            if ( IsDebug() ) printf(" This is a trk calibration2 at obt %u pkt %u t2 is %u , t2t1cal is %u \n",obt2,pkt2,t2,t2t1cal);                    //
4786  //        if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2              if ( IsDebug() ) cout << " Loop over calibration2 to search associated calibration: "<<endl;
4787            //                    while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1
4788                    //
4789                    t2++;
4790                    //
4791                    pret2 = t2 - 1; // EMILIANO
4792                    //
4793                    if ( t2 < nevents2 ){
4794                        tr2->GetEntry(t2);
4795                        codt2 = eh2->GetCounter();
4796                        t2t1cal = codt2->Get(pctp->CalibTrk1);
4797                        //
4798                        ph2 = eh2->GetPscuHeader();
4799                        obt2 = ph2->GetOrbitalTime();  
4800                        pkt2 = ph2->GetCounter();  
4801                        //
4802                        if ( IsDebug() ) printf(" >> trk calibration2 at obt %u pkt %u t2 is %u , t2t1cal is %u \n",obt2,pkt2,t2,t2t1cal);    
4803                        //    if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2
4804                        //    
4805                    } else {
4806                        //
4807                        // running out of vector without finding the corresponding calibration, sig
4808                        //
4809                        if ( IsDebug() ) printf(" t2 >= nevents2 \n");
4810                        pret2 = t2;
4811                        obt2 = 0;
4812                        //    pkt2 = pkt1+2;
4813                        pkt2 = 0;
4814                        t2t1cal = t1+1;
4815                    };
4816                    //      if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) && (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){
4817    
4818                    // EMILIANO
4819                    //      if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) || (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){
4820                    //        //    if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
4821                    //        if ( IsDebug() ) printf(" running out of vector without finding the corresponding calibration, sig \n");
4822                    //        //
4823                    //        // running out of vector without finding the corresponding calibration, sig
4824                    //        //
4825                    //        pret2 = t2;
4826                    //        obt2 = 0;
4827                    //        //      pkt2 = pkt1+2;
4828                    //        pkt2 = 0;
4829                    //        t2t1cal = t1+1;
4830                    //      };
4831    
4832    
4833                    //
4834                };
4835                //
4836                if ( IsDebug() ) printf(" Check if trk calibration2 is the right one \n");
4837                //
4838                // EMILIANO
4839                if ( ( PKT(pkt2) < PKT(pktfirst) || PKT(pkt2) > upperpkt) || (OBT(obt2) < OBT(obtfirst) || OBT(obt2) > upperobt) ){
4840                    //      if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
4841                    if ( IsDebug() ) printf(" *WARNING*  The calibration found is outside the interval, sig \n");
4842                    //
4843                    // running out of vector without finding the corresponding calibration, sig
4844                    //
4845                    pret2 = t2;
4846                    obt2 = 0;
4847                    pkt2 = 0;
4848                };
4849                if ( PKT(pkt2) == PKT(pkt1)+1 ){
4850                    if ( IsDebug() ) cout << " ...OK"<<endl;
4851                    // =======================
4852                    // The calibration is good
4853                    // =======================
4854                    //
4855    //      if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2);
4856    //      if ( IsDebug() ) printf(" Trk calibration2 at obt %u pkt %u t2 is %u is good \n",obt2,pkt2,t2);
4857    //        if ( IsDebug() ) printf("\n Trk calibration2 at time %u obt %u pkt %u \n",fromtime,obt2,pkt2);
4858                    if ( IsDebug() ) printf(" Trk calibration2 %u at time %u obt %u pkt %u \n",t2,fromtime,obt2,pkt2);
4859                    //
4860                    UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
4861                    if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
4862    //      valid = valid & valid2;
4863                    valid = valid & valid2; //QUESTO VA CAMBIATO
4864                    //
4865                    // Handle good calib
4866                    //
4867                    MISSING_pkt1 = false;
4868                    MISSING_pkt2 = false;
4869    //              this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
4870                    //
4871                    // Check for missing calibtrk1
4872                    //
4873                    if ( t2 != pret2+1 ){
4874                        //
4875                        if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u pret2 is %u \n",obt2,pkt2,t2,pret2);
4876                        //
4877                        while ( t2 > pret2+1 ){
4878                            //
4879                            // handle missing calib1
4880                            //
4881                            pret2++;
4882                            //
4883                            obt1 = 0;
4884                            pkt1 = 0;
4885                            //
4886                            tr2->GetEntry(pret2);
4887                            ph2 = eh2->GetPscuHeader();
4888                            obt2 = ph2->GetOrbitalTime();  
4889                            pkt2 = ph2->GetCounter();  
4890                            //
4891                            fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
4892                            //
4893                            valid = 0;
4894                            MISSING_pkt1 = true;
4895                            MISSING_pkt2 = false;
4896    //                      this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
4897                            //
4898                        };
4899                        //
4900                    };
4901                    //
4902                } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){
4903                    //
4904                    // Check for missing calibtrk2
4905                    //
4906                    if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
4907                    t2 = pret2;
4908                    //
4909                    // handle missing calib2
4910                    //
4911                    obt2 = 0;
4912                    pkt2 = 0;
4913                    valid = 0;
4914                    MISSING_pkt1 = false;
4915                    MISSING_pkt2 = true;
4916    //              this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
4917                    //
4918                };
4919                //
4920    
4921                if( !(MISSING_pkt1&MISSING_pkt2) ){
4922                  this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
4923                  ncalib++;
4924                  if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;
4925                }
4926    
4927    
4928          } else {          } else {
4929            //            //
4930            // running out of vector without finding the corresponding calibration, sig              if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u OUTSIDE the considered time interval \n",fromtime,obt1,pkt1);
4931            //              //      if ( PEDANTIC ) throw -79;
4932            if ( IsDebug() ) printf(" t2 >= nevents2 \n");              //
           pret2 = t2;  
           obt2 = 0;  
           //      pkt2 = pkt1+2;  
           pkt2 = 0;  
           t2t1cal = t1+1;  
4933          };          };
4934          //      if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) && (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){          //    
4935    
4936          // EMILIANO      }; //end loop on pkt1
 //      if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) || (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){  
 //        //    if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){  
 //        if ( IsDebug() ) printf(" running out of vector without finding the corresponding calibration, sig \n");  
 //        //  
 //        // running out of vector without finding the corresponding calibration, sig  
 //        //  
 //        pret2 = t2;  
 //        obt2 = 0;  
 //        //      pkt2 = pkt1+2;  
 //        pkt2 = 0;  
 //        t2t1cal = t1+1;  
 //      };  
4937    
4938    
4939    
4940        //
4941        // we have one more calib pkt2 !
4942        //
4943        t2++;
4944        while ( t2 < nevents2 ){
4945          //          //
4946        };          // handle missing calib1
       //  
       if ( IsDebug() ) printf(" Check if trk calibration2 exists \n");  
       //  
       // EMILIANO  
       if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) || (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){  
         //      if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){  
         if ( IsDebug() ) printf(" The calibration found is outside the interval, sig \n");  
         //  
         // running out of vector without finding the corresponding calibration, sig  
         //  
         pret2 = t2;  
         obt2 = 0;  
         pkt2 = 0;  
       };  
       //  
       // The calibration is good  
       //  
       if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){  
         //  
         if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2);  
         if ( IsDebug() ) printf(" The trk calibration2 at obt %u pkt %u t2 is %u is good \n",obt2,pkt2,t2);  
         //  
         UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );  
         if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;  
         valid = valid & valid2;  
         //  
         // Handle good calib  
4947          //          //
4948          this->HandleTRK_CALIB(true,true);          if ( IsDebug() ) printf(" t2 is %u nevents2 is %u \n",t2,nevents2);
4949            obt1 = 0;
4950            pkt1 = 0;
4951          //          //
4952          // Check for missing calibtrk1          tr2->GetEntry(t2);
4953            ph2 = eh2->GetPscuHeader();
4954            obt2 = ph2->GetOrbitalTime();  
4955            pkt2 = ph2->GetCounter();  
4956          //          //
4957          if ( t2 != pret2+1 ){          fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
4958            //          valid = 0;
4959            if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u pret2 is %u \n",obt2,pkt2,t2,pret2);          //  if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){
4960            //          // EMILIANO
4961            while ( t2 > pret2+1 ){          if ( this->PKT(pkt2) >= this->PKT(pktfirst) && this->PKT(pkt2 <= upperpkt) && this->OBT(obt2) >= this->OBT(obtfirst) && this->OBT(obt2) <= upperobt ){
4962              //              //    if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){
             // handle missing calib1  
             //  
             pret2++;  
             //  
             obt1 = 0;  
             pkt1 = 0;  
             //  
             tr2->GetEntry(pret2);  
             ph2 = eh2->GetPscuHeader();  
             obt2 = ph2->GetOrbitalTime();    
             pkt2 = ph2->GetCounter();    
4963              //              //
4964              fromtime = this->GetAbsTime(ph2->GetOrbitalTime());              if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
4965              //              //
4966              valid = 0;              MISSING_pkt1 = true;
4967              this->HandleTRK_CALIB(false,true);              MISSING_pkt2 = false;
4968                this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
4969                ncalib++;
4970                if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;
4971              //              //
           };  
           //  
4972          };          };
4973          //          //
4974        } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){          t2++;
         //  
         // Check for missing calibtrk2  
         //  
         if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);  
         t2 = pret2;  
4975          //          //
4976          // handle missing calib2      };
4977    
4978    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    
4979    // -----------------------------------------------------------------
4980    // in case of corruption, check if the calibration can be recovered
4981    // from another chewbacca file
4982    // -----------------------------------------------------------------
4983    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    
4984    
4985    //    cout <<" TRY TO RECOVER ?? "<<try_to_recover<<endl;
4986        
4987        if(chewbacca&&try_to_recover){
4988    
4989    
4990            if ( IsDebug() ) cout << endl << ">>>> TRY TO RECOVER TRACKER CALIBRATIONS <<<<"<<endl;
4991    
4992            TSQLResult *result = 0;
4993            TSQLRow    *row    = 0;
4994          //          //
4995          obt2 = 0;          stringstream oss;
4996          pkt2 = 0;          oss.str("");
         valid = 0;  
         this->HandleTRK_CALIB(true,false);        
4997          //          //
4998        };  
4999        //          ////////////////////////////////////////////////////////////////////////
5000      } else {          // retrieve the name of the current file:
5001        //          ////////////////////////////////////////////////////////////////////////        
5002        if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u OUTSIDE the considered time interval \n",fromtime,obt1,pkt1);          oss.str("");
5003        //      if ( PEDANTIC ) throw -79;          oss << "SELECT NAME FROM GL_ROOT where ID=" << GetID_ROOT() <<";";
5004        //          if ( IsDebug() ) cout << oss.str().c_str() << endl;
5005      };  
5006      //              result = conn->Query(oss.str().c_str());
5007    };          if ( !result ) throw -4;;
5008    //          row = result->Next();
5009    // we have one more calib pkt2 !          TString thisfilename = (TString)row->GetField(0);
5010    //          if ( IsDebug() ) cout << "Current file ==> "<<thisfilename<<endl;      
5011    t2++;  
5012    while ( t2 < nevents2 ){          ////////////////////////////////////////////////////////////////////////        
5013      //          // read all the calibrations inserted
5014      // handle missing calib1          ////////////////////////////////////////////////////////////////////////        
5015      //          oss.str("");
5016      if ( IsDebug() ) printf(" t2 is %u nevents2 is %u \n",t2,nevents2);          oss << " SELECT ";
5017      obt1 = 0;          oss << " ID,FROM_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,TO_TIME";
5018      pkt1 = 0;          oss << " FROM GL_TRK_CALIB ";
5019      //          oss << " ORDER BY ID DESC LIMIT "<<ncalib<<"; ";        
5020      tr2->GetEntry(t2);          if ( IsDebug() ) cout << oss.str().c_str() << endl;
5021      ph2 = eh2->GetPscuHeader();  
5022      obt2 = ph2->GetOrbitalTime();            result = conn->Query(oss.str().c_str());
5023      pkt2 = ph2->GetCounter();            if ( !result ) throw -4;;
5024      //          if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5025      fromtime = this->GetAbsTime(ph2->GetOrbitalTime());          
5026      valid = 0;          // -----------------------------------
5027  //  if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){          // loop over calibrations ...
5028   // EMILIANO          // -----------------------------------
5029      if ( this->PKT(pkt2) >= this->PKT(pktfirst) && this->PKT(pkt2 <= upperpkt) && this->OBT(obt2) >= this->OBT(obtfirst) && this->OBT(obt2) <= upperobt ){          UInt_t nn=0;
5030        //    if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){          do {
5031        //              row = result->Next();
5032        if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);              if(!row)break;
5033        //              
5034        this->HandleTRK_CALIB(false,true);              UInt_t id       = (UInt_t)atoll(row->GetField(0));
5035        //              UInt_t fromtime = (UInt_t)atoll(row->GetField(1));
5036      };              UInt_t obt1     = (UInt_t)atoll(row->GetField(2));
5037      //              UInt_t pkt1     = (UInt_t)atoll(row->GetField(3));
5038      t2++;              UInt_t obt2     = (UInt_t)atoll(row->GetField(4));
5039      //              UInt_t pkt2     = (UInt_t)atoll(row->GetField(5));
5040    };              UInt_t boot     = (UInt_t)atoll(row->GetField(6));
5041                UInt_t valid    = (UInt_t)atoll(row->GetField(7));
5042                bool MISSING_pkt1 = (row->GetFieldLength(8)==0);
5043                bool MISSING_pkt2 = (row->GetFieldLength(9)==0);
5044                UInt_t totime   = (UInt_t)atoll(row->GetField(10));
5045    
5046                // -------------------------------------
5047                // ...check if the entry is corrupted...
5048                // -------------------------------------
5049                cout <<"*** "<< MISSING_pkt1 << MISSING_pkt2 << valid <<endl;
5050                bool CORRUPTED = (MISSING_pkt1||MISSING_pkt2||!valid);
5051    
5052                if ( IsDebug() ) cout << "("<<nn<<")  ID = "<<id<<" from GL_TRK_CALIB  ==>  corrupted ? "<<CORRUPTED<<endl;
5053    
5054    //          if( !CORRUPTED  )continue; // nothing to do
5055    
5056                /////////////////////////////////////////////////////////  
5057                // if it is corrupted, ...look for ather chewbacca files
5058                // containing the same calibrations ...
5059                /////////////////////////////////////////////////////////
5060    
5061                bool this_MISSING_pkt1 = false;
5062                bool this_MISSING_pkt2 = false;
5063                int  this_t1=0;
5064                int  this_t2=0;;
5065                UInt_t this_valid = 0;
5066    
5067                TString path       = "";
5068                TString name       = "";
5069                TString raw        = "";
5070                UInt_t obt0        = 0;
5071                UInt_t timesync    = 0;
5072                UInt_t boot_number = 0;
5073                bool   FOUND       = false;
5074    
5075                if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;
5076    
5077    //          for(int itable=0; itable<2; itable++){
5078                for(int itable=0; itable<1; itable++){
5079    
5080                    // ------------------------------------------------------
5081                    // loop over both ROOT_TABLE and ROOT_TABLE_BAD
5082                    // ------------------------------------------------------
5083    
5084                    TString table = "ROOT_TABLE";
5085                    if(itable==1)table = "ROOT_TABLE_BAD";
5086    
5087                    oss.str("");
5088                    oss << " SELECT ";
5089                    oss << " FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,BOOT_NUMBER,INPUT_NAME ";
5090                    oss << " FROM "<<table;
5091                    oss << " WHERE 1 " << endl;
5092                    oss << " AND FILE_NAME != \""<< thisfilename<<"\"";
5093                    if( !MISSING_pkt1 ){
5094                        oss << " AND ";
5095                        oss << " PKT_NUMBER_INIT < "<<pkt1;
5096                        oss << " AND ";
5097                        oss << " PKT_NUMBER_FINAL > "<<pkt1;
5098                        oss << " AND ";
5099                        oss << " PKT_OBT_INIT < "<<obt1;
5100                        oss << " AND ";
5101                        oss << " PKT_OBT_FINAL > "<<obt1;
5102                    }else{
5103                        if(pkt2>1) pkt1 = pkt2-1;//serve dopo                  
5104                    }
5105                    if( !MISSING_pkt2 ){
5106                        oss << " AND ";
5107                        oss << " PKT_NUMBER_INIT < "<<pkt2;
5108                        oss << " AND ";
5109                        oss << " PKT_NUMBER_FINAL > "<<pkt2;
5110                        oss << " AND ";
5111                        oss << " PKT_OBT_INIT < "<<obt2;
5112                        oss << " AND ";
5113                        oss << " PKT_OBT_FINAL > "<<obt2;
5114                    }else{
5115                        if(pkt1>0) pkt2 = pkt1+1;//serve dopo
5116                    }
5117                    if( boot> 0 ){
5118                        oss << " AND ";
5119                        oss << " BOOT_NUMBER = "<<boot;
5120                    }else{
5121                    }
5122                    oss << " ORDER BY BAD_PKT_CALREAD ASC; ";
5123                
5124                    TSQLResult *result2 = 0;
5125                    TSQLRow    *row2    = 0;
5126    
5127                    if ( IsDebug() ) cout << oss.str().c_str() << endl;        
5128                    result2 = conn->Query(oss.str().c_str());
5129                    if ( !result2 ) throw -4;;
5130                    if ( IsDebug() ) cout <<"Rows: "<<result2->GetRowCount()<<endl;
5131    
5132                    // ------------------------------------------------------
5133                    // loop over files containing repetition (if any)
5134                    // ------------------------------------------------------
5135                    do {
5136                        row2 = result2->Next();
5137                        if(!row2)break;
5138                    
5139                        // ------------------------------------------------------
5140                        // ... a repetition is found ...
5141                        // ------------------------------------------------------
5142                        path       = (TString)row2->GetField(0);
5143                        name       = (TString)row2->GetField(1);
5144                        raw        = (TString)row2->GetField(5);
5145                        obt0        = (UInt_t)atoll(row2->GetField(2));
5146                        timesync    = (UInt_t)atoll(row2->GetField(3));
5147                        boot_number = (UInt_t)atoll(row2->GetField(4));
5148                    
5149                        if ( IsDebug() ) cout << "- - - - - - - - - - -" <<endl;
5150    //                  cout << path <<endl;
5151    //                  cout << "File    : " <<name <<endl;
5152    //                  cout << obt0 <<endl;
5153    //                  cout << timesync <<endl;
5154    //                  cout << "boot n. : "<<boot_number <<endl;
5155    //                  cout << raw <<endl;
5156    
5157                        // ------------------------------------------------------
5158                        // ... retrieve the calibration packets.
5159                        // ------------------------------------------------------
5160                        if ( IsDebug() ) printf(" file is %s/%s \n",((TString)gSystem->ExpandPathName(path.Data())).Data(),name.Data());
5161                        TFile *file = new TFile(((TString)gSystem->ExpandPathName(path.Data()))+"/"+name); // EM, path could be symbolic and we must expand it
5162                        if(!file)throw -100;
5163                        if(file->IsZombie())throw -100;
5164                        //
5165                        tr1 = (TTree*)file->Get("CalibTrk1");
5166                        if ( !tr1 || tr1->IsZombie() ) throw -22;
5167                        tr2 = (TTree*)file->Get("CalibTrk2");
5168                        if ( !tr2 || tr2->IsZombie() ) throw -23;
5169                        //
5170                        tr1->SetBranchAddress("CalibTrk1", &caltrk1);
5171                        tr1->SetBranchAddress("Header", &eh1);
5172                        nevents1 = tr1->GetEntries();
5173                        tr2->SetBranchAddress("CalibTrk2", &caltrk2);
5174                        tr2->SetBranchAddress("Header", &eh2);
5175                        nevents2 = tr2->GetEntries();
5176                        for(this_t1=0; this_t1<nevents1; this_t1++){
5177                            tr1->GetEntry(this_t1);
5178                            if(
5179                                (UInt_t)eh1->GetPscuHeader()->GetCounter() == pkt1 &&
5180                                true) break;
5181                            this_MISSING_pkt1 = true;
5182                        }
5183                        for(this_t2=0; this_t2<nevents2; this_t2++){
5184                            tr2->GetEntry(this_t2);
5185                            if(
5186                                (UInt_t)eh2->GetPscuHeader()->GetCounter() == pkt2 &&
5187                                true) break;
5188                            this_MISSING_pkt2 = true;
5189                        }
5190                        this_valid =
5191                            ValidateTrkCalib( caltrk1, eh1, file )
5192                            *
5193                            ValidateTrkCalib( caltrk2, eh2, file );
5194                                
5195                        // ---------------------------------------------------------------------
5196                        // accept the calibration if it is better than the previous:
5197                        //
5198                        // - if the new calibration is perfect (both valid packets)
5199                        // - if the new calibration has both the packets and the previous does not
5200                        // ---------------------------------------------------------------------
5201                        if(
5202                            ( !this_MISSING_pkt1&&!this_MISSING_pkt2&&this_valid )||
5203                            ( (MISSING_pkt1||MISSING_pkt2) && (!this_MISSING_pkt1&&!this_MISSING_pkt2) )||
5204                            false)FOUND=true;      
5205                
5206                        if(file)file->Close();
5207    
5208                        if(FOUND)break;
5209    
5210                    }while(1);//endl loop over root table entries
5211    
5212                    if(FOUND)break;
5213    
5214                }//end loop over tables
5215    
5216                if(FOUND){
5217    
5218                if ( IsDebug() ) cout << " >>> REPETITION FOUND :-) <<<" <<endl;
5219    
5220                    ////////////////////////////////////////////
5221                    // insert a new entry in GL_TRK_CALIB and
5222                    // modify the time-tag of the previous one
5223                    ////////////////////////////////////////////
5224    
5225                    // ---------------------------------------------------------------------
5226                    // step 1: insert a new raw file in GL_RAW
5227                    // ---------------------------------------------------------------------
5228                    //
5229                    // check if the raw file already exist
5230                    //
5231                    UInt_t id_raw = 0;   // EM GL_RAW is there only for backward compatibility so we do not need to fill it when in "chewbacca" mode
5232    //              oss.str("");
5233    //              oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";";
5234    //              if ( IsDebug() ) cout << oss.str().c_str() << endl;
5235                    
5236    //              result = conn->Query(oss.str().c_str());
5237    //              if ( !result ) throw -4;;
5238    //              if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5239    //              if( result->GetRowCount() == 0){
5240    //                  if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl;
5241    //                  // - - - - - - - - - - -
5242    //                  // insert new raw file
5243    //                  // - - - - - - - - - - -
5244    //                  GL_RAW glraw = GL_RAW();    
5245    //                  glraw.PATH        = gSystem->DirName(raw.Data());
5246    //                  glraw.NAME        = gSystem->BaseName(raw.Data());
5247    //                  glraw.BOOT_NUMBER = boot_number;
5248    //                  //
5249    //                  insertPamelaRawFile( &glraw );
5250    //                  //              
5251    //                  id_raw = glraw.ID;              
5252    //              }else{
5253    //                  row = result->Next();
5254    //                  id_raw = (UInt_t)atoll(row->GetField(0));
5255    //              }
5256    //              if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl;
5257    
5258                    // ---------------------------------------------------------------------
5259                    // step 1(bis): retrieve the timesync id associated to the file
5260                    // (NB, uso lo stesso associato al file iniziale)
5261                    // ---------------------------------------------------------------------
5262                    UInt_t idtimesync = 0;
5263                    oss.str("");
5264                    oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";
5265                    if ( debug ) printf(" %s \n",oss.str().c_str());
5266                    result = conn->Query(oss.str().c_str());
5267                    if ( !result ) throw -3;
5268                    row = result->Next();
5269                    if ( !row ) throw -3;
5270                    idtimesync = (UInt_t)atoll(row->GetField(0));
5271                    if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl;
5272                    
5273                    // ---------------------------------------------------------------------
5274                    // step 2: insert a new root file in GL_ROOT
5275                    // ---------------------------------------------------------------------
5276                    //
5277                    // check if the root file already exist
5278                    //
5279                    UInt_t id_root = 0;
5280                    oss.str("");
5281                    oss << "SELECT ID FROM GL_ROOT where NAME=\"" << gSystem->BaseName(name.Data()) <<"\";";
5282                    if ( IsDebug() ) cout << oss.str().c_str() << endl;
5283                    
5284                    result = conn->Query(oss.str().c_str());
5285                    if ( !result ) throw -4;;
5286                    if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5287                    if( result->GetRowCount() == 0){
5288                        if ( IsDebug() ) cout << " << Insert new ROOT file >> "<<endl;
5289                        // - - - - - - - - - - -
5290                        // insert new root file
5291                        // - - - - - - - - - - -
5292                        GL_ROOT glroot = GL_ROOT();            
5293                        glroot.ID_RAW      = id_raw;
5294                        glroot.ID_TIMESYNC = idtimesync;
5295                        //
5296                        // EM STATIC = the full expanded path must be put in the DB, KEEPENV = the path given as input (or found in ROOT_TABLE) must be used,
5297                        //             NOT STATIC NOT KEEPENV = $PAM_L0 must be used in the DB
5298                        //
5299                        if ( STATIC ){
5300                          glroot.PATH        = (TString)gSystem->ExpandPathName(path);
5301                        } else {
5302                          if ( KEEPENV ){
5303                            glroot.PATH      = path;
5304                          } else {
5305                            glroot.PATH      = "$PAM_L0";
5306                          };
5307                        };
5308                        //              glroot.PATH        = path;
5309                        glroot.NAME        = name;
5310                        //
5311                        insertPamelaRootFile( &glroot );
5312                        //              
5313                        id_root = glroot.ID;                    
5314                    }else{
5315                        row = result->Next();
5316                        if(row)id_root = (UInt_t)atoll(row->GetField(0));
5317                    }
5318                    if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl;
5319    
5320                    // ---------------------------------------------------------------------
5321                    // step 3: modify time-tag of corrupted GL_TRK_CALIB entry
5322                    // ---------------------------------------------------------------------
5323                    if ( IsDebug() ) cout << " << Modify time-tag of calibration ID="<<id<<" >> "<<endl;
5324                    oss.str("");
5325                    oss << " UPDATE GL_TRK_CALIB SET "  
5326                        << " TO_TIME=0 , FROM_TIME=0  WHERE "
5327                        << " ID = "<< id  << ";";
5328                    if ( IsDebug() ) cout << oss.str().c_str() << endl;
5329                    result = conn->Query(oss.str().c_str());
5330                    if ( !result ) throw -4;;
5331    
5332                    // ---------------------------------------------------------------------
5333                    // step 4: insert the new calibration:
5334                    // ---------------------------------------------------------------------
5335                    if ( IsDebug() ) cout << " << Insert new TRK calibration >> "<<endl;
5336                    //
5337                    GL_TRK_CALIB glcal = GL_TRK_CALIB();        
5338                    //
5339                    glcal.ID_ROOT_L0        = id_root;
5340                    glcal.EV_ROOT_CALIBTRK1 = this_t1;
5341                    glcal.EV_ROOT_CALIBTRK2 = this_t2;
5342                    glcal.FROM_TIME         = fromtime;
5343                    glcal.TO_TIME           = totime;
5344                    glcal.OBT1              = obt1;
5345                    glcal.OBT2              = obt2;
5346                    glcal.PKT1              = pkt1;
5347                    glcal.PKT2              = pkt1;
5348                    glcal.BOOT_NUMBER       = GetBOOTnumber();
5349                    glcal.VALIDATION        = this_valid;
5350                    //
5351                    HandleTRK_CALIB(&glcal);
5352                    if ( IsDebug() ) cout << "ID = "<<glcal.ID<<endl;
5353                    //
5354                    
5355                }
5356                if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;
5357    
5358            }while(1);//end loop over calibrations
5359            
5360            
5361            if( result )delete result;
5362            if( row    )delete row;
5363            
5364            
5365    
5366            
5367    
5368    
5369        }
5370    
5371    
5372    //      // ------------------------------
5373    //      // try to recover the calibration
5374    //      // ------------------------------
5375    //      cout << "TRY TO RECOVER TRACKER CALIBRATION"<<endl;
5376    //      //
5377    //      ULong64_t time  = 0; //absolute time
5378    //      string    path[100]; //mettere un limite massimo
5379    //      int       nrows = 0;
5380    //      UInt_t    pkt   = 0;
5381    //      UInt_t    obt   = 0;
5382    //      char     *type  = "";
5383    //      EventHeader *eh   = new EventHeader();
5384    //      CalibTrk1Event *c = new CalibTrk1Event();
5385            
5386    //       //
5387    //       if(which_is_not_valid==1 || which_is_not_valid==3){
5388    //        //
5389    //        cout << "PKT1 --> missing or corrupted "<<endl;
5390    //        type = "CalibTrk1";
5391    //        pkt  = pkt1;
5392    //        obt  = obt1;
5393    //        time = this->GetAbsTime(obt1);
5394    //        if( pkt1 == 0 ){//missing
5395    //            time = this->GetAbsTime(obt2);
5396    //            pkt  = pkt2-1;          
5397    //        }
5398    //        //
5399    //       }else if (which_is_not_valid==2 || which_is_not_valid==3){
5400    //        //
5401    //        cout << "PKT2--> missing or corrupted "<<endl;
5402    //        type = "CalibTrk2 ";
5403    //        pkt  = pkt2;
5404    //        obt  = obt2;
5405    //        time = this->GetAbsTime(obt2);
5406    //        if( pkt2 == 0 ){//missing
5407    //            time = this->GetAbsTime(obt1);
5408    //            pkt  = pkt1+1;          
5409    //        }
5410    //        //
5411    //       }else{
5412    //        cout << "this should not happen!!! "<<endl;
5413    //        trow -666;
5414    //       }
5415          
5416    //       nrows = Query_ROOT_TABLE(time,conn,path);// get the list of file which might contain the packet
5417          
5418    
5419    //       for(int r=0; r<nrows; r++){ //loop over rows
5420    //        if(path)cout << r << " >>>> "<<(path+r)->c_str() << endl;
5421    //        /// verifica che il file non sia quello gia` aperto
5422    //       }
5423    
5424    //       ////////////////////////////////////////////////////////////////////////
5425    
5426    //       TSQLResult *result = 0;
5427    //       TSQLRow    *row    = 0;
5428    //       //
5429    //       stringstream oss;
5430    //       oss.str("");
5431    //       // ----------------------------------------
5432    //       // read the id of last calibration inserted
5433    //       // ----------------------------------------
5434    //       oss.str("");
5435    //       oss << " SELECT ";
5436    //       oss << " (ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION) ";
5437    //       oss << " ORDER BY ID DESC LIMIT 1; ";
5438          
5439    //       result = conn->Query(oss.str().c_str());
5440    //       row = result->Next();
5441    //       if( !row )throw -666;
5442    
5443    //       if( result )delete result;
5444    //       if( row    )delete row;
5445    
5446    //       UInt_t id = (UInt_t)atoll(row->GetField(0));
5447    
5448    //       // -------------------------------------
5449    //       // ...and modify it with new parameters
5450    //       // -------------------------------------
5451          
5452    
5453    //   }
5454    //    //
5455    return(0);    return(0);
5456  };  };
# Line 5061  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 5753  Int_t PamelaDBOperations::CleanGL_RUN_FR
5753                    << " RM_ACQ_SETTING_MODE=" << glrun->GetRM_ACQ_SETTING_MODE()<< " , "                      << " RM_ACQ_SETTING_MODE=" << glrun->GetRM_ACQ_SETTING_MODE()<< " , "  
5754                    << " TRK_CALIB_USED=" << glrun->GetTRK_CALIB_USED()<< " , "                      << " TRK_CALIB_USED=" << glrun->GetTRK_CALIB_USED()<< " , "  
5755                    << " CAL_DSP_MASK=" << glrun->GetCAL_DSP_MASK()<< " , "                      << " CAL_DSP_MASK=" << glrun->GetCAL_DSP_MASK()<< " , "  
5756                    << " LAST_TIMESYNC=" << glrun->GetLAST_TIMESYNC()<< " , "                      << " LAST_TIMESYNC=" << glrun->GetLAST_TIMESYNC()<< " , ";
5757                    << " OBT_TIMESYNC=" << glrun->GetOBT_TIMESYNC()                //
5758                    << " WHERE ID=" << myid[u] << ";";                if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() )
5759                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
5760                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() )
5761                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC() << " , ";
5762                  //              
5763                  oss << " OBT_TIMESYNC=" << glrun->GetOBT_TIMESYNC();
5764                  oss << " WHERE ID=" << myid[u] << ";";
5765                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
5766              };              };
5767              //              //
# Line 5074  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 5772  Int_t PamelaDBOperations::CleanGL_RUN_FR
5772              for (UInt_t u=1; u <= nid ; u++){              for (UInt_t u=1; u <= nid ; u++){
5773                oss.str("");                oss.str("");
5774                oss << "UPDATE GL_RUN_FRAGMENTS SET "                oss << "UPDATE GL_RUN_FRAGMENTS SET "
5775                    << " RUNHEADER_TIME=" << glrun->GetRUNHEADER_TIME()<< " , "                    << " RUNHEADER_TIME=" << glrun->GetRUNHEADER_TIME()<< " , ";
5776                    << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "                //
5777                  if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() )
5778                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
5779                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() )
5780                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
5781                  //              
5782                  oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "
5783                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< ";";                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< ";";
5784                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
5785              };                  };    
# Line 5098  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 5802  Int_t PamelaDBOperations::CleanGL_RUN_FR
5802                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "
5803                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , "                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , "
5804                    << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< " , "                    << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< " , "
5805                    << " PKT_COUNTER=" << glrun->GetPKT_COUNTER()<< " , "                    << " PKT_COUNTER=" << glrun->GetPKT_COUNTER()<< " , ";
5806                    << " PKT_READY_COUNTER=" << glrun->GetPKT_READY_COUNTER()                //
5807                  if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ){
5808                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
5809                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) {
5810                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
5811                  //              
5812                  oss << " PKT_READY_COUNTER=" << glrun->GetPKT_READY_COUNTER()
5813                    << " WHERE ID=" << myid[u] << ";";                    << " WHERE ID=" << myid[u] << ";";
5814                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
5815              };              };
# Line 5112  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 5822  Int_t PamelaDBOperations::CleanGL_RUN_FR
5822                oss.str("");                oss.str("");
5823                oss << "UPDATE GL_RUN_FRAGMENTS SET "                oss << "UPDATE GL_RUN_FRAGMENTS SET "
5824                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "
5825                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , "                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , ";
5826                    << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< ";";                //
5827                  if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ){
5828                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
5829                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){
5830                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
5831                  //              
5832                  oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< ";";
5833                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
5834              };                  };    
5835            };            };
# Line 5272  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 5988  Int_t PamelaDBOperations::CleanGL_RUN_FR
5988          glrun->SetID_RUN_FRAG(glrun->GetID());          glrun->SetID_RUN_FRAG(glrun->GetID());
5989          glrun->Fill_GL_RUN(conn);            glrun->Fill_GL_RUN(conn);  
5990          //          //
5991  //      oss.str("");          //      oss.str("");
5992  //      oss << " SELECT ID FROM GL_RUN WHERE "          //      oss << " SELECT ID FROM GL_RUN WHERE "
5993  //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "          //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
5994  //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "          //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
5995  //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "          //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
5996  //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "          //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
5997  //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";          //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
5998  //      //          //      //
5999  //      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());
6000  //      result2 = conn->Query(oss.str().c_str());          //      result2 = conn->Query(oss.str().c_str());
6001  //      //          //      //
6002  //      if ( !result2 ) throw -4;          //      if ( !result2 ) throw -4;
6003  //      //          //      //
6004  //      row2 = result2->Next();          //      row2 = result2->Next();
6005  //      //          //      //
6006  //      if ( !row2 ) throw -25;          //      if ( !row2 ) throw -25;
6007  //      //          //      //
6008  //      oss.str("");          //      oss.str("");
6009  //      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);
6010  //      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());
6011  //      result2 = conn->Query(oss.str().c_str());          //      result2 = conn->Query(oss.str().c_str());
6012  //      //          //      //
6013  //      if ( !result2 ) throw -4;          //      if ( !result2 ) throw -4;
6014          //          //
6015          moved++;          moved++;
6016          //          //
# Line 5306  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6022  Int_t PamelaDBOperations::CleanGL_RUN_FR
6022        //        //
6023        //        //
6024        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
6025  //      oss.str("");        //      oss.str("");
6026  //       oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);        //       oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
6027  //       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());
6028  //       result2 = conn->Query(oss.str().c_str());        //       result2 = conn->Query(oss.str().c_str());
6029  //      //        //      //
6030  //      if ( !result2 ) throw -4;        //      if ( !result2 ) throw -4;
6031  //      //        //      //
6032        row = result->Next();        row = result->Next();
6033      };      };
6034    };    };
# Line 5366  Int_t PamelaDBOperations::ValidateRuns(T Line 6082  Int_t PamelaDBOperations::ValidateRuns(T
6082      // 2) get the OBT of the last validated run      // 2) get the OBT of the last validated run
6083      // --------------------------------------------------------------      // --------------------------------------------------------------
6084      oss.str("");      oss.str("");
6085      oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start      //    oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
6086        oss << " SELECT * FROM GL_RUN  WHERE VALIDATION>0 AND RUNHEADER_TIME<="<< t_start
6087          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
6088      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
6089      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
# Line 5457  Int_t PamelaDBOperations::ValidateRuns(T Line 6174  Int_t PamelaDBOperations::ValidateRuns(T
6174    GL_RUN* this_run = new GL_RUN();    GL_RUN* this_run = new GL_RUN();
6175    GL_RUN* next_run = new GL_RUN();    GL_RUN* next_run = new GL_RUN();
6176    Int_t   nseq_max = 1000;    Int_t   nseq_max = 1000;
6177  //  UInt_t* sequence = new UInt_t[100];    //  UInt_t* sequence = new UInt_t[100];
6178    vector<UInt_t> sequence(nseq_max);    vector<UInt_t> sequence(nseq_max);
6179    Int_t   nseq = 0;    Int_t   nseq = 0;
6180    Bool_t CHECK = false;    Bool_t CHECK = false;
# Line 5472  Int_t PamelaDBOperations::ValidateRuns(T Line 6189  Int_t PamelaDBOperations::ValidateRuns(T
6189    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
6190    while(1){    while(1){
6191                        
6192            row = result->Next();      row = result->Next();
6193            if( row == NULL ) break;      if( row == NULL ) break;
6194                        
6195            //------------      //------------
6196            //get run info      //get run info
6197            //------------      //------------
6198            this_run->Set_GL_RUN(row);      this_run->Set_GL_RUN(row);
6199                                        
6200            Bool_t this_BAD = false;      Bool_t this_BAD = false;
6201            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;
6202            else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;      else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;
6203            else{      else{
6204  //                printf("Missing or corrupted header!! \n");        //                  printf("Missing or corrupted header!! \n");
6205                    this_ONLINE = false;        this_ONLINE = false;
6206                    this_BAD = true;        this_BAD = true;
6207            };      };
6208    
6209            //-----------------------------------      //-----------------------------------
6210            //compare with previous(next in time)      //compare with previous(next in time)
6211            //-----------------------------------      //-----------------------------------
6212            CHECK = false;      CHECK = false;
6213            UInt_t interval=0;      UInt_t interval=0;
6214                        
6215            if( nrow != 0){      if( nrow != 0){
6216                    
6217                                        
6218                    t1 = this_run->GetRUNTRAILER_TIME();        t1 = this_run->GetRUNTRAILER_TIME();
6219                    t2 = next_run->GetRUNHEADER_TIME();        t2 = next_run->GetRUNHEADER_TIME();
6220                    interval = (t2-t1);        interval = (t2-t1);
6221                                        
6222                    if(this_ONLINE && next_ONLINE){                               // this: ON-LINE + next: ON-LINE        if(this_ONLINE && next_ONLINE){                           // this: ON-LINE + next: ON-LINE
6223                                                        
6224                            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
6225                                                        
6226                            if( interval >= calibtime )CHECK = true;                      //more than calibtime s => there might be a calibration          if( interval >= calibtime )CHECK = true;                        //more than calibtime s => there might be a calibration
6227                                                        
6228                            if( !CHECK && this_run->VALIDATION ){          if( !CHECK && this_run->VALIDATION ){
6229                                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);            //      for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
6230                                    nseq=0;            for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],this_run->VALIDATION);
6231                            }            nseq=0;
6232            }
6233                                        
6234                    }else if( !this_ONLINE && next_ONLINE) {              // this: DEFAULT + next:ON-LINE        }else if( !this_ONLINE && next_ONLINE) {          // this: DEFAULT + next:ON-LINE
6235                                                        
6236                            CHECK = true;          CHECK = true;
6237    
6238                    }else if( !next_ONLINE ){                                             // this:ANY + next:DEFAULT        }else if( !next_ONLINE ){                                         // this:ANY + next:DEFAULT
6239                                                        
6240                            assignVALIDATION(next_run->ID,true);          assignVALIDATION(next_run->ID,true);
6241                            nseq=0;          nseq=0;
6242                    }        }
6243            }      }
6244    
6245            //----------------------------      //----------------------------
6246            //check run sequence for calib      //check run sequence for calib
6247            //----------------------------      //----------------------------
6248            if( CHECK ){      if( CHECK ){
6249                    // check if calibration exists        // check if calibration exists
6250                    if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);        if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
6251                    Bool_t MISSING = MissingTRK_CALIB(t1,t2);        //      Bool_t MISSING = MissingTRK_CALIB(t1,t2);
6252                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);        UInt_t MISSING = MissingTRK_CALIB(t1,t2);
6253                    nseq=0;        UInt_t val = 0;
6254            };        if ( MISSING == 1 ) val = 0;
6255            //--------------        if ( MISSING == 0 ) val = 1;
6256            //store run info        if ( MISSING == 2 ) val = 2;
6257            //--------------        for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],val);
6258            *next_run   = *this_run;        nseq=0;
6259            next_ONLINE = this_ONLINE;      };
6260            if( !this_BAD ){      //--------------
6261                    if(nseq < nseq_max){      //store run info
6262                            sequence[nseq] = this_run->ID;      //--------------
6263                            nseq++;      *next_run   = *this_run;
6264                    }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);      next_ONLINE = this_ONLINE;
6265            };      if( !this_BAD ){
6266          if(nseq < nseq_max){
6267            sequence[nseq] = this_run->ID;
6268            nseq++;
6269          }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
6270        };
6271                        
6272            if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);      if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);
6273            nrow++;      nrow++;
6274                        
6275    };    };
6276    delete this_run;    delete this_run;
# Line 5561  Int_t PamelaDBOperations::ValidateRuns(T Line 6284  Int_t PamelaDBOperations::ValidateRuns(T
6284   * @param t2 To absolute time   * @param t2 To absolute time
6285   * @return true if there might be a missing calibration   * @return true if there might be a missing calibration
6286   */   */
6287  Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){  //Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
6288    UInt_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
6289                    
6290          GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();    GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
6291                    
6292          // get the closest VALIDATED calibration before the run start (t2)    // get the closest calibration before the run start (t2)
6293          if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);      //>>> missing    //  if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);        //>>> missing
6294      if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(1);       //>>> missing
6295                    
6296          if ( trkcalib->TO_TIME  < t2 ) return(true);                                    //>>> missing    //  if ( trkcalib->TO_TIME  < t2 ) return(true);                                      //>>> missing
6297      if ( trkcalib->TO_TIME  < t2 ) return(1);                                     //>>> missing
6298                    
6299          //==============================================================    //==============================================================
6300          // Check is done first on the basis of time between calibration,    // Check is done first on the basis of time between calibration,
6301          // which should be equal to the time between ascending-nodes.    // which should be equal to the time between ascending-nodes.
6302          //==============================================================    //==============================================================
6303          if ( t2 - trkcalib->FROM_TIME > 5700) {    if ( t2 - trkcalib->FROM_TIME > 5700) {
6304                  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);
6305          //==============================================================      //==============================================================
6306          // there might be a missing calibration, due to:      // there might be a missing calibration, due to:
6307          // - MM full      // - MM full
6308          // - corrupted packets      // - corrupted packets
6309          // - loss of data      // - loss of data
6310          // 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
6311          //==============================================================      //==============================================================
6312                  Bool_t DOWNLOAD = false;      Bool_t DOWNLOAD = false;
6313                  // check if the calib was skipped becouse of download .... DA FARE!!      // check if the calib was skipped becouse of download .... DA FARE!!
6314                  if(DOWNLOAD)return(false);      //    if(DOWNLOAD)return(false);
6315        if(DOWNLOAD)return(0);
6316                                    
6317                  return(true);                                   //>>> missing      //    return(true);                                         //>>> missing
6318        return(1);                                  //>>> missing
6319                                    
6320          };    };
6321                    
6322          //==============================================================    //==============================================================
6323          // 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,
6324          // it is enough to say that there are no missing calibrations    // it is enough to say that there are no missing calibrations
6325          //==============================================================    //==============================================================
6326          // the long time interval bewteen runs might be due to download    // the long time interval bewteen runs might be due to download
6327          if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);    if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
6328          return(false);    if ( trkcalib->VALIDATION ) return(0);
6329      if ( IsDebug() )printf("Calibration is not validated... :-/ ==> OK but with VALIDATION=2! \n");
6330      return(2);
6331                    
6332  };  };
6333  /**  /**
# Line 5606  Bool_t PamelaDBOperations::MissingTRK_CA Line 6336  Bool_t PamelaDBOperations::MissingTRK_CA
6336   * @param validation true/false   * @param validation true/false
6337   */   */
6338  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
6339          TSQLResult *result = 0;    return(this->assignVALIDATION(idrun,(UInt_t)validation));
6340          stringstream oss;  }
6341          oss.str("");  
6342          oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, UInt_t validation){
6343          //    TSQLResult *result = 0;
6344  //      if ( IsDebug() )    stringstream oss;
6345  //      printf(" Set VALIDATION = %i for run %i \n",validation,idrun);    oss.str("");
6346          if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());    oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
6347          result = conn->Query(oss.str().c_str());    //
6348          if ( !result ) throw -4;    //    if ( IsDebug() )
6349          return(0);    //    printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
6350      if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
6351      result = conn->Query(oss.str().c_str());
6352      if ( !result ) throw -4;
6353      return(0);
6354  }  }
6355    
6356    
# Line 5802  Int_t PamelaDBOperations::removeFile(TSt Line 6536  Int_t PamelaDBOperations::removeFile(TSt
6536    stringstream myquery;    stringstream myquery;
6537    //      //  
6538    myquery.str("");    myquery.str("");
6539    myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";    myquery << " SELECT ID,ID_RAW,ID_TIMESYNC FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
6540    //    //
6541    pResult = conn->Query(myquery.str().c_str());    pResult = conn->Query(myquery.str().c_str());
6542    //    //
# Line 5818  Int_t PamelaDBOperations::removeFile(TSt Line 6552  Int_t PamelaDBOperations::removeFile(TSt
6552    //    //
6553    this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));    this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
6554    this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));    this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
6555      UInt_t idtsy=(UInt_t)atoll(Row->GetField(2));
6556    //    //
6557    this->ValidationOFF();    this->ValidationOFF();
6558    //    //
# Line 5825  Int_t PamelaDBOperations::removeFile(TSt Line 6560  Int_t PamelaDBOperations::removeFile(TSt
6560    //    //
6561    this->RemoveRUNS();    this->RemoveRUNS();
6562    //    //
6563    this->RemoveFILES();    this->RemoveFILES(idtsy);
6564    //    //
6565    this->SetID_ROOT(0);    this->SetID_ROOT(0);
6566    this->SetID_RAW(0);    this->SetID_RAW(0);
# Line 6045  void PamelaDBOperations::RemoveRUNS(){ Line 6780  void PamelaDBOperations::RemoveRUNS(){
6780   * Rearrange calibration tables   * Rearrange calibration tables
6781   *   *
6782   **/   **/
6783  void PamelaDBOperations::RemoveFILES(){  void PamelaDBOperations::RemoveFILES(UInt_t idtsy){
6784    stringstream myquery;    stringstream myquery;
6785    //    //
6786    myquery.str("");    myquery.str("");
# Line 6055  void PamelaDBOperations::RemoveFILES(){ Line 6790  void PamelaDBOperations::RemoveFILES(){
6790    //    //
6791    conn->Query(myquery.str().c_str());    conn->Query(myquery.str().c_str());
6792    //    //
6793      myquery.str("");
6794      myquery << " DELETE FROM GL_ROOT WHERE ID=" <<this->GetID_ROOT() <<";";
6795      //
6796      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
6797      //
6798      conn->Query(myquery.str().c_str());  //
6799      //
6800      if ( !chewbacca ){
6801        myquery.str("");
6802        myquery << " DELETE FROM GL_TIMESYNC WHERE ID=" << idtsy <<";";
6803        //
6804        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
6805        //
6806        conn->Query(myquery.str().c_str());
6807      };
6808      //
6809  };  };
6810    
6811  /**  /**
# Line 6259  void PamelaDBOperations::RemoveCALIBS(){ Line 7010  void PamelaDBOperations::RemoveCALIBS(){
7010   * Rearrange calibration tables   * Rearrange calibration tables
7011   *   *
7012   **/   **/
7013  UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){  UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh , TFile *file){
7014    
7015        if(!caltrk) return 0;
7016    
7017        if ( IsDebug() ) cout << "ValidateTrkCalib:"<<endl;
7018    
7019        UInt_t validate = 1;
7020      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};
7021      UInt_t timeaftercalib=120000; //2000;      UInt_t timeaftercalib=120000; //2000;
7022  //  ----------      TString classname = caltrk->GetName();
7023  //  Check CRCs  
7024  //  ----------  //  ----------------------------------
7025    //  Check CRCs and failed calibrations
7026    //  ----------------------------------
7027      for(Int_t ipkt=0; ipkt<6; ipkt++){      for(Int_t ipkt=0; ipkt<6; ipkt++){
7028          if( caltrk->crc_hcal[ipkt] )return 0; // :-(          if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
7029          for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(              if( caltrk->crc_hcal[ipkt] ){
7030    //              if(IsDebug())cout<<"(CRC Header)";
7031                    validate = 0;
7032                    if(IsDebug())cout <<endl<<" *** CRC *** (header DSPn "<<caltrk->DSPnumber[ipkt]<<")";
7033            
7034                }
7035                for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] ){
7036    //              if(IsDebug())cout<<"(CRC Pkt-"<<ilad<<")";
7037                    if(IsDebug())cout <<endl<<" *** CRC *** (data DSPn "<<caltrk->DSPnumber[ipkt]<<" ladder "<<ilad<<")";
7038                    validate = 0;
7039                }
7040                if( !(caltrk->ncalib_event[ipkt]==0 && caltrk->cal_flag[ipkt]==0) ){
7041                    if(IsDebug())cout <<endl<<" *** FAILURE ***     (data DSPn "<<caltrk->DSPnumber[ipkt]<<")";
7042                    validate = 0;
7043                }
7044            }else{
7045    //          validate=0;
7046                if(IsDebug())cout <<endl<<" *** DSPn *** ("<< caltrk->DSPnumber[ipkt] <<" @pkt "<<ipkt<<")";
7047            }
7048      }      }
7049    
7050  //  -----------------------  //  -----------------------
7051  //  Check missing packets:  //  Check missing packets:
7052  //  -----------------------  //  -----------------------
# Line 6295  UInt_t PamelaDBOperations::ValidateTrkCa Line 7072  UInt_t PamelaDBOperations::ValidateTrkCa
7072  //  Check if it is first or second calibration packet  //  Check if it is first or second calibration packet
7073  //  -------------------------------------------------  //  -------------------------------------------------
7074      UInt_t build=0;      UInt_t build=0;
     TString classname = caltrk->GetName();  
7075      UInt_t base=0;      UInt_t base=0;
7076      UInt_t mask=0;      UInt_t mask=0;
7077      if(classname.Contains("CalibTrk1Event")){      if(classname.Contains("CalibTrk1Event")){
# Line 6306  UInt_t PamelaDBOperations::ValidateTrkCa Line 7082  UInt_t PamelaDBOperations::ValidateTrkCa
7082          base=18;          base=18;
7083          mask=0xFC0000;          mask=0xFC0000;
7084      }      }
7085  //  -------------------------------------------------  //  ----------------------------------------------------
7086  //  Count number of packets and set build variable  //  Count number of valid packets and set build variable
7087  //  -------------------------------------------------  //  ----------------------------------------------------
7088        if(IsDebug())cout <<endl<< " DSP: ";
7089      Int_t  npkts=0;      Int_t  npkts=0;
7090      for(Int_t ipkt=0; ipkt<6; ipkt++){      for(Int_t ipkt=0; ipkt<6; ipkt++){
7091          if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){          if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
7092                if(IsDebug())cout <<" "<<caltrk->DSPnumber[ipkt];
7093              npkts++;              npkts++;
7094              build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );              build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
7095          }  //          cout << caltrk->DSPnumber[ipkt]
7096            };
7097      }      }
7098  //    if( npkts==6 )return 1; // :-)      if(IsDebug())cout << " ==> "<< hex << build << dec;
7099    //  ----------------------------------------------------
7100  //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;  //  If the number of valid packets is 6, ok exit...
7101    //  ----------------------------------------------------
7102  //  -----------------------------------------------      if( npkts==6 ){
7103  //  If missing packets: check the acq configuration          return validate; // exit
7104        }
7105    ////////////////////////////////////////////////////////
7106    //  ...otherwise there might be some missing packets
7107    //
7108    //  In this case check the acq configuration
7109  //  (some DSPs might be excluded from acquisition)  //  (some DSPs might be excluded from acquisition)
7110  //  -----------------------------------------------  ////////////////////////////////////////////////////////
7111    
7112        if(!eh || !file || (file&&file->IsZombie()) ){
7113            if ( IsDebug() )cout << " *** MISSING VIEW *** eh="<<eh<<" file="<<file<<" cannot validate"<<endl;
7114            return (0);
7115        }
7116        
7117  //  -----------------------------------------------  //  -----------------------------------------------
7118  //  retrieve the first run header after calib  //  retrieve the first run header after calib
7119  //  -----------------------------------------------  //  -----------------------------------------------
7120       PacketType *pctp;      
7121       EventCounter *cod;      PacketType *pctp;
7122       cod = eh->GetCounter();      EventCounter *cod;
7123       Int_t irun = cod->Get(pctp->RunHeader);      cod = eh->GetCounter();
7124       TTree *rh=(TTree*)file->Get("RunHeader");      Int_t irun = cod->Get(pctp->RunHeader);
7125       if ( !rh || rh->IsZombie() ) throw -17;      TTree *rh=(TTree*)file->Get("RunHeader");
7126       if( rh->GetEntries() == irun ){      if ( !rh || rh->IsZombie() ) throw -17;
7127            if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;      if( rh->GetEntries() <= irun ){
7128           return 0; // :-(          if ( IsDebug() ) cout << " *** MISSING-PKT *** no runs after calib (1) -- cannot validate :-( "<<endl;
7129       }          return 0; // :-(
7130        }
7131       RunHeaderEvent *run  = 0;      RunHeaderEvent *run  = 0;
7132       EventHeader    *hrun = 0;      EventHeader    *hrun = 0;
7133       rh->SetBranchAddress("RunHeader", &run);      rh->SetBranchAddress("RunHeader", &run);
7134       rh->SetBranchAddress("Header", &hrun);      rh->SetBranchAddress("Header", &hrun);
7135       rh->GetEntry(irun);      rh->GetEntry(irun);    
7136  //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;      if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
7137            if ( IsDebug() ) cout << " *** MISSING-PKT *** no runs after calib (2) -- cannot validate :-( "<<endl;
7138       if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){          return 0; // :-(
7139            if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;      }
7140           return 0; // :-(  
7141       }      UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
7142            if( dtime > timeaftercalib ){
7143       if( !run->RM_ACQ_AFTER_CALIB ){          if ( IsDebug() ) cout << " *** MISSING-PKT *** run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
7144            if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0    -- cannot validate :-( "<<endl;          return 0; // :-(
7145           return 0; // :-(      }
7146       }  
7147        if ( IsDebug() ) cout <<endl<< " ACQ_BUILD_INFO ==> "<<hex<<(run->ACQ_BUILD_INFO & mask)<<dec;
7148       UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());  
7149       if( dtime > timeaftercalib ){      if( (run->ACQ_BUILD_INFO & mask) != build ){
7150           if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;          validate=0; // :-(
7151           return 0; // :-(          cout <<endl<< " *** MISSING-PKT *** packet mismatch: ACQ_BUILD_INFO="<<hex<<(run->ACQ_BUILD_INFO&mask)<<" != "<<build<<dec;
7152       }      };
7153        
7154        return validate;
7155    
      if( (run->ACQ_BUILD_INFO & mask) != build ){  
          if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;  
          return 0; // :-(  
      }  
      return 1; // :-)  
7156    
7157  }  }
7158    
# Line 6378  UInt_t PamelaDBOperations::ValidateTrkCa Line 7162  UInt_t PamelaDBOperations::ValidateTrkCa
7162   *   *
7163   **/   **/
7164  UInt_t PamelaDBOperations::Check(){  UInt_t PamelaDBOperations::Check(){
7165          return(this->Check(0,0));    return(this->Check(0,0));
7166  }  }
7167    
7168  UInt_t PamelaDBOperations::Check(UInt_t from, UInt_t to){  UInt_t PamelaDBOperations::Check(UInt_t from, UInt_t to){
7169    //    //
7170          if ( IsDebug() ) printf(" from %u to %u \n",from,to);    if ( IsDebug() ) printf(" from %u to %u \n",from,to);
7171    //    //
7172    UInt_t test = 0;    UInt_t test = 0;
7173    //    //
# Line 6424  UInt_t PamelaDBOperations::Check(UInt_t Line 7208  UInt_t PamelaDBOperations::Check(UInt_t
7208      //      //
7209      if ( from > 0 && nid <= from ) goto ss;      if ( from > 0 && nid <= from ) goto ss;
7210      if ( to > 0 && nid >= to ) goto ss;      if ( to > 0 && nid >= to ) goto ss;
7211  //      //
7212      if ( (UInt_t)atoll(row->GetField(4)) > 1 ){      if ( (UInt_t)atoll(row->GetField(4)) > 1 ){
7213        //        //
7214        //        //
# Line 6455  UInt_t PamelaDBOperations::Check(UInt_t Line 7239  UInt_t PamelaDBOperations::Check(UInt_t
7239              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)));  
7240              row3 = result3->Next();              row3 = result3->Next();
7241            };            };
7242  //      delete result3;            //    delete result3;
7243    
7244          };          };
7245          //          //
# Line 6476  UInt_t PamelaDBOperations::Check(UInt_t Line 7260  UInt_t PamelaDBOperations::Check(UInt_t
7260              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)));
7261              row3 = result3->Next();              row3 = result3->Next();
7262            };            };
7263  //      delete result3;            //    delete result3;
7264          };          };
7265          //          //
7266          oss.str("");          oss.str("");
# Line 6496  UInt_t PamelaDBOperations::Check(UInt_t Line 7280  UInt_t PamelaDBOperations::Check(UInt_t
7280              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)));
7281              row3 = result3->Next();              row3 = result3->Next();
7282            };            };
7283   //     delete result3;            //    delete result3;
7284    
7285          };          };
7286        };        };
# Line 6517  UInt_t PamelaDBOperations::Check(UInt_t Line 7301  UInt_t PamelaDBOperations::Check(UInt_t
7301            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)));
7302            row3 = result3->Next();            row3 = result3->Next();
7303          };          };
7304  //      delete result3;          //      delete result3;
7305        };        };
7306        //        //
7307      };      };
# Line 6543  UInt_t PamelaDBOperations::Check(UInt_t Line 7327  UInt_t PamelaDBOperations::Check(UInt_t
7327        thisf = (TString)row2->GetField(0);        thisf = (TString)row2->GetField(0);
7328        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
7329        test = 1;        test = 1;
7330  //      delete result2;        //        delete result2;
7331      };      };
7332      //      //
7333      if ( (thisrtt < prevrht) && (thisrht != prevrht) ){      if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
# Line 6565  UInt_t PamelaDBOperations::Check(UInt_t Line 7349  UInt_t PamelaDBOperations::Check(UInt_t
7349        thisf = (TString)row2->GetField(0);        thisf = (TString)row2->GetField(0);
7350        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
7351        test = 1;        test = 1;
7352  //      delete result2;        //        delete result2;
7353      };      };
7354      //      //
7355      if ( (thisrht > thisrtt) && (thisrht != prevrht) ){      if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
# Line 6587  UInt_t PamelaDBOperations::Check(UInt_t Line 7371  UInt_t PamelaDBOperations::Check(UInt_t
7371        thisf = (TString)row2->GetField(0);        thisf = (TString)row2->GetField(0);
7372        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
7373        test = 1;        test = 1;
7374  //      delete result2;        //        delete result2;
7375      };      };
7376  ss:    ss:
7377      //      //
7378      prevrht = thisrht;      prevrht = thisrht;
7379      prevrtt = thisrtt;      prevrtt = thisrtt;
7380      previd = thisid;      previd = thisid;
7381      prevl0id = thisl0id;      prevl0id = thisl0id;
7382      row = result->Next();      row = result->Next();
7383  //    if ( result2 ) delete result2;      //    if ( result2 ) delete result2;
7384  //    if ( result3 ) delete result3;      //    if ( result3 ) delete result3;
7385    };    };
7386    //    //
7387    return(test);    return(test);

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

  ViewVC Help
Powered by ViewVC 1.1.23