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

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

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

revision 1.1.1.1 by mocchiut, Tue Sep 23 07:20:33 2008 UTC revision 1.25 by pam-fi, Wed Aug 5 18:48:44 2009 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      RELAXED = false;
64    chewbacca = false;    chewbacca = false;
65    chminentry = 0;    chminentry = 0;
66    chID = 0;    chID = 0;
# Line 76  PamelaDBOperations::PamelaDBOperations(T Line 77  PamelaDBOperations::PamelaDBOperations(T
77      //      //
78    };    };
79    //    //
80      KEEPENV = false;
81      if ( keepenv ) KEEPENV=true;
82    STATIC=false;    STATIC=false;
83    if ( staticp ) STATIC=true;    if ( staticp ) STATIC=true;
84    //    //
# Line 99  PamelaDBOperations::PamelaDBOperations(T Line 102  PamelaDBOperations::PamelaDBOperations(T
102    if (INSERT_RAW) SetRawName(filerawname);    if (INSERT_RAW) SetRawName(filerawname);
103    //    //
104    INSERT_ROOT = !filerootname.IsNull();    INSERT_ROOT = !filerootname.IsNull();
105      if ( INSERT_ROOT ) this->SetRootName(filerootname);
106    this->SetOrbitNo(dwinput);    this->SetOrbitNo(dwinput);
107    //    //
108    this->SetID_RAW(0);    this->SetID_RAW(0);
# Line 132  void PamelaDBOperations::OpenL0File(TStr Line 136  void PamelaDBOperations::OpenL0File(TStr
136        stringstream qu;        stringstream qu;
137        TSQLResult *result = 0;        TSQLResult *result = 0;
138        TSQLResult *result2 = 0;        TSQLResult *result2 = 0;
139          TSQLResult *result3 = 0;
140        TSQLRow    *row    = 0;            TSQLRow    *row    = 0;    
141        TSQLRow    *row2    = 0;            TSQLRow    *row2    = 0;    
142          TSQLRow    *row3    = 0;    
143        TString chpath;        TString chpath;
144        TString chfile;        TString chfile;
145        UInt_t ridn = 0;        UInt_t ridn = 0;
146        qu.str("");        qu.str("");
147        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,REAL_TIME_INIT from ROOT_TABLE_MERGING where ID_N=" << chID << ";";
148        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
149        result = conn->Query(qu.str().c_str());        result = conn->Query(qu.str().c_str());
150        if ( result ){        if ( result ){
# Line 149  void PamelaDBOperations::OpenL0File(TStr Line 155  void PamelaDBOperations::OpenL0File(TStr
155            chpktmax = (UInt_t)atoll(row->GetField(2));            chpktmax = (UInt_t)atoll(row->GetField(2));
156            chobtmin = (UInt_t)atoll(row->GetField(3));            chobtmin = (UInt_t)atoll(row->GetField(3));
157            chobtmax = (UInt_t)atoll(row->GetField(4));            chobtmax = (UInt_t)atoll(row->GetField(4));
158              chiby = (TString)(row->GetField(5));
159              chrtinit = (UInt_t)atoll(row->GetField(6));
160          } else {          } else {
161            throw -84;            throw -84;
162          };          };
# Line 157  void PamelaDBOperations::OpenL0File(TStr Line 165  void PamelaDBOperations::OpenL0File(TStr
165        };        };
166        delete result;        delete result;
167        qu.str("");        qu.str("");
168        qu << "SELECT FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,TIME_OFFSET,BOOT_NUMBER,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL from ROOT_TABLE where ID_N=" << ridn << ";";        qu << "SELECT FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,TIME_OFFSET,BOOT_NUMBER,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,REAL_TIME_INIT from ROOT_TABLE where ID_N=" << ridn << ";";
169        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
170        result2 = conn->Query(qu.str().c_str());        result2 = conn->Query(qu.str().c_str());
171        if ( result2 ){        if ( result2 ){
172          row2 = result2->Next();          row2 = result2->Next();
173          if ( row2 ){          if ( row2 ){
174            chpath = (TString)gSystem->ExpandPathName(row2->GetField(0))+'/';            if ( KEEPENV ){
175                chpath = (TString)(row2->GetField(0))+'/';
176              } else {
177                chpath = (TString)gSystem->ExpandPathName(row2->GetField(0))+'/';
178              };
179            chfile = (TString)(row2->GetField(1));            chfile = (TString)(row2->GetField(1));
180            chobtts = (UInt_t)atoll(row2->GetField(2));            chobtts = (UInt_t)atoll(row2->GetField(2));
181            chlastts = (UInt_t)atoll(row2->GetField(3));            chlastts = (UInt_t)atoll(row2->GetField(3));
182            chresursts = (UInt_t)atoll(row2->GetField(4));            chresursts = (UInt_t)atoll(row2->GetField(4));
183            chboot = (UInt_t)atoll(row2->GetField(5));            chboot = (UInt_t)atoll(row2->GetField(5));
184            //            //
185              if ( !chboot ){
186                qu.str("");
187                //      qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND ABS(LAST_TIME_SYNC_INFO-"<< chlastts <<")<10000 AND ABS(REAL_TIME_INIT-"<< row2->GetField(10) <<")<7200 group by BOOT_NUMBER order by BOOT_NUMBER asc;";
188                qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND ABS(LAST_TIME_SYNC_INFO-"<< chlastts <<")<40000 AND ABS(REAL_TIME_INIT-"<< row2->GetField(10) <<")<40000 group by BOOT_NUMBER order by BOOT_NUMBER asc;"; // relax conditions... 090112 [8RED: error -29]
189                if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
190                result3 = conn->Query(qu.str().c_str());
191                if ( result3 ){
192                  row3 = result3->Next();
193                  if ( row3 && result3->GetRowCount() == 1 ){
194                    chboot = (UInt_t)atoll(row3->GetField(0));
195                    if ( debug ) printf(" Found boot_number = %u \n",chboot);
196                  } else {
197                    if ( debug ) printf(" AGH CANNOT DETERMINE THE BOOT NUMBER... \n");
198                    throw -29;
199                  };
200                };
201              };
202              //
203            chpktinit = (UInt_t)atoll(row2->GetField(6));            chpktinit = (UInt_t)atoll(row2->GetField(6));
204            chpktfinal = (UInt_t)atoll(row2->GetField(7));            chpktfinal = (UInt_t)atoll(row2->GetField(7));
205            chobtinit = (UInt_t)atoll(row2->GetField(8));            chobtinit = (UInt_t)atoll(row2->GetField(8));
# Line 182  void PamelaDBOperations::OpenL0File(TStr Line 212  void PamelaDBOperations::OpenL0File(TStr
212          throw -85;          throw -85;
213        };        };
214        filerootname = chpath + chfile;// + ".root";        filerootname = chpath + chfile;// + ".root";
       if ( debug ) printf(" chewbacca: filename is %s \n",filerootname.Data());  
215      };      };
216      this->SetRootName(filerootname);      this->SetRootName(filerootname);
     file = TFile::Open(this->GetRootName().Data());  
   } else {  
     this->SetRootName("");  
217    };    };
218      if ( debug ) printf(" Filename is %s \n",filerootname.Data());
219      file = TFile::Open(this->GetRootName().Data());
220      //  } else {
221      //    this->SetRootName("");
222      //    this->SetRootName(filerootname);
223      //  };
224  }  }
225    
226  //  //
# Line 388  void PamelaDBOperations::SetOrbitNo(UInt Line 420  void PamelaDBOperations::SetOrbitNo(UInt
420      return;      return;
421    };    };
422    //    //
423    TString name = this->GetRootFile();    if ( !chewbacca ){
424    Int_t nlength = name.Length();      TString name = this->GetRootFile();
425    if ( nlength < 5 ) return;      Int_t nlength = name.Length();
426    TString dwo = 0;      if ( nlength < 5 ){
427    for (Int_t i = 0; i<5; i++){        if ( IsDebug() ) printf(" Agh problems determining the orbit number! name = %s \n",name.Data());
428      dwo.Append(name[i],1);        return;
429    };      };
430    if ( dwo.IsDigit() ){      TString dwo = "";
431      dworbit = (UInt_t)dwo.Atoi();      for (Int_t i = 0; i<5; i++){
   } else {  
     dwo="";  
     for (Int_t i = 8; i<13; i++){  
432        dwo.Append(name[i],1);        dwo.Append(name[i],1);
433      };          };
434      if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();      if ( dwo.IsDigit() ){
435          dworbit = (UInt_t)dwo.Atoi();
436        } else {
437          dwo="";
438          for (Int_t i = 8; i<13; i++){
439            dwo.Append(name[i],1);
440          };    
441          if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
442        };
443        if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
444    };    };
   if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());  
445    return;    return;
446  };  };
447    
# Line 435  TString PamelaDBOperations::GetRawPath() Line 472  TString PamelaDBOperations::GetRawPath()
472    
473  TString PamelaDBOperations::GetRootPath(){  TString PamelaDBOperations::GetRootPath(){
474    if ( STATIC ){    if ( STATIC ){
475      return((TString)gSystem->DirName(filerootname.Data())+'/');      return((TString)gSystem->DirName(this->GetRootName().Data())+'/');
476    } else {    } else {    
477      return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');      if ( KEEPENV ){
478          return((TString)gSystem->ExpandPathName(gSystem->DirName(filerootname.Data()))+'/');
479        } else {
480          return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');
481        };
482    };    };
483  };  };
484    
# Line 490  void PamelaDBOperations::SetCommonGLRUN( Line 531  void PamelaDBOperations::SetCommonGLRUN(
531  };  };
532    
533  /**  /**
534     *
535     * Set the variables which belogns to physendrun tree
536     *
537     */
538    void PamelaDBOperations::SetPhysEndRunVariables(){
539      //
540      //
541      //
542      TTree *T = 0;
543      T = (TTree*)file->Get("PhysEndRun");
544      if ( !T || T->IsZombie() ) throw -90;
545      //
546      PhysEndRunEvent *pher= 0;
547      EventHeader *eh = 0;
548      T->SetBranchAddress("PhysEndRun", &pher);
549      T->SetBranchAddress("Header", &eh);
550      //
551      UInt_t phobt = 0;
552      UInt_t phpkt = 0;
553      //
554      glrun->SetPHYSENDRUN_MASK_S3S2S12(0);
555      glrun->SetPHYSENDRUN_MASK_S11CRC(0);
556      //
557      for (Int_t p=0; p<T->GetEntries(); p++){
558        //
559        T->GetEntry(p);
560        //
561        phobt = (UInt_t)eh->GetPscuHeader()->GetOrbitalTime();
562        phpkt = (UInt_t)eh->GetPscuHeader()->GetCounter();
563        //
564        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())  ){
565          if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() || glrun->GetPHYSENDRUN_MASK_S11CRC() ){
566            if ( IsDebug() ) printf(" WARNING while looping in physendrun: found two PhysEndRun packet for the same RUN! \n");
567            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);
568            if ( PEDANTIC && IsDebug() ) printf(" ERROR while looping in physendrun: found two PhysEndRun packet for the same RUN!\n ");
569            if ( PEDANTIC ) throw -91;
570          } else {
571            glrun->SetPHYSENDRUN_MASK_S3S2S12((UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S3S2S12);
572            glrun->SetPHYSENDRUN_MASK_S11CRC((UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S11CRC);
573          };
574        };
575      };
576      //
577    };
578    
579    /**
580   * Patch, look for upper limits to avoid processing retransmitted data   * Patch, look for upper limits to avoid processing retransmitted data
581   */   */
582  Int_t PamelaDBOperations::SetUpperLimits(){  Int_t PamelaDBOperations::SetUpperLimits(){
# Line 603  Int_t PamelaDBOperations::SetUpperLimits Line 690  Int_t PamelaDBOperations::SetUpperLimits
690      UInt_t tjump = 50000;      UInt_t tjump = 50000;
691      //UInt_t tjump = 100;      //UInt_t tjump = 100;
692      while ( tjump > 0 ){      while ( tjump > 0 ){
693        pktlast = numeric_limits<UInt_t>::max();        //      pktlast = numeric_limits<UInt_t>::max();
694        while ( pktlast > chpktmax && (Int_t)(nevent-1-it) >= 0 ){        pktlast = chpktmax + 1;
695          while ( PKT(pktlast) > PKT(chpktmax) && (Int_t)(nevent-1-it) >= 0 ){
696          if ( (Int_t)(nevent-1-it) >= 0 ){          if ( (Int_t)(nevent-1-it) >= 0 ){
697            T->GetEntry(nevent-1-it);            T->GetEntry(nevent-1-it);
698            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
# Line 612  Int_t PamelaDBOperations::SetUpperLimits Line 700  Int_t PamelaDBOperations::SetUpperLimits
700          } else {          } else {
701            pktlast = chpktmax + 1;            pktlast = chpktmax + 1;
702          };          };
703          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));
704          it += tjump;          it += tjump;
705        };        };
706        if ( tjump > 1 ) it -= 2*tjump;        if ( tjump > 1 ) it -= 2*tjump;
# Line 636  Int_t PamelaDBOperations::SetUpperLimits Line 724  Int_t PamelaDBOperations::SetUpperLimits
724      tjump = 50000;      tjump = 50000;
725      //tjump = 100;      //tjump = 100;
726      while ( tjump > 0 ){      while ( tjump > 0 ){
727        pktlast = 0;        //      pktlast = 0;
728        while ( pktlast < chpktmin && it < (Int_t)nevent ){        pktlast = chpktmin - 1;
729          if ( debug ) printf("LLlook for down %i %i %llu %llu \n",it,pktlast,PKT(pktlast),PKT(chpktmin));
730          while ( PKT(pktlast) < PKT(chpktmin) && it < (Int_t)nevent ){
731          if ( it < (Int_t)nevent ){          if ( it < (Int_t)nevent ){
732            T->GetEntry(it);            T->GetEntry(it);
733            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
# Line 731  Int_t PamelaDBOperations::SetUpperLimits Line 821  Int_t PamelaDBOperations::SetUpperLimits
821      ph = eh->GetPscuHeader();      ph = eh->GetPscuHeader();
822      pktlast = ph->GetCounter();      pktlast = ph->GetCounter();
823      nevent = upperentry - chminentry;      nevent = upperentry - chminentry;
824        if ( ((Int_t)upperentry - (Int_t)chminentry) < 0 ) nevent = 0; // nevent is UInt_t! 090112 [8RED: error -88]
825        if ( IsDebug() ) printf(" Setting nevent to %u - upperentry %i chminentry %i \n",nevent,upperentry,chminentry);
826      //      //
827    } else {    } else {
828      upperpkt = PKT(pktlast);      upperpkt = PKT(pktlast);
# Line 739  Int_t PamelaDBOperations::SetUpperLimits Line 831  Int_t PamelaDBOperations::SetUpperLimits
831    };    };
832    //    //
833    if ( chewbacca && nevent < 1 ) {    if ( chewbacca && nevent < 1 ) {
834          pktfirst = chpktmin;      if ( IsDebug() ) printf(" chewbacca and no events in the file \n");
835          upperpkt = PKT(chpktmax);      pktfirst = chpktmin;
836          pktlast = chpktmax;      upperpkt = PKT(chpktmax);
837          obtfirst = chobtmin;      pktlast = chpktmax;
838          obtlast = chobtmax;      obtfirst = chobtmin;
839          upperobt = OBT(chobtmax);      obtlast = chobtmax;
840        upperobt = OBT(chobtmax);
841    };    };
842    //    //
843    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 845  Int_t PamelaDBOperations::SetUpperLimits
845    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);
846    //    //
847    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)) ){
848          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));
849          if ( PEDANTIC ) throw -88;      if ( PEDANTIC ) throw -88;
850          return(32);      return(32);
851    };    };
852    //    //
853    if ( !nevent ) return(64);    if ( !nevent ) return(64);
# Line 764  Int_t PamelaDBOperations::SetUpperLimits Line 857  Int_t PamelaDBOperations::SetUpperLimits
857    //  if ( nevent < jump ) jump = int(nevent/10);    //  if ( nevent < jump ) jump = int(nevent/10);
858    //  if ( !jump ) jump = 1;    //  if ( !jump ) jump = 1;
859    //    //
860    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) && nevent > deltapkt ){  
861      if ( ((PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt)) && nevent > deltapkt ){
862      //      //
863      if ( IsDebug() ) printf(" starting jump %i \n",jump);      if ( IsDebug() ) printf(" starting jump %i \n",jump);
864      //    if ( IsDebug() ) printf(" (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(" (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));
# Line 1040  Int_t PamelaDBOperations::SetUpperLimits Line 1134  Int_t PamelaDBOperations::SetUpperLimits
1134          upperpkt = pkth;          upperpkt = pkth;
1135          upperobt = obth;          upperobt = obth;
1136          rhev = lasthead+1;          rhev = lasthead+1;
1137      } else {        } else {
1138          rhev = lasthead;          rhev = lasthead;
1139        };        };
1140        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 1173  UInt_t PamelaDBOperations::AssignRunID()
1173    stringstream   oss;    stringstream   oss;
1174    //      //  
1175    oss.str("");    oss.str("");
1176    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.
1177        if ( !strcmp(tag.Data(),"NONE") ){
1178          oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< chiby.Data() <<"');";
1179        } else {
1180          oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');";
1181        };
1182      } else {
1183        oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');";
1184      };
1185    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1186    if ( !result ) throw -10;    if ( !result ) throw -10;
1187    oss.str("");    oss.str("");
# Line 1227  void PamelaDBOperations::LockTables(){   Line 1329  void PamelaDBOperations::LockTables(){  
1329    stringstream oss;    stringstream oss;
1330    //      //  
1331    oss.str("");    oss.str("");
1332    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;";
1333    TSQLResult *result = 0;    TSQLResult *result = 0;
1334    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1335    if ( !result ) throw -10;    if ( !result ) throw -10;
# Line 1260  void PamelaDBOperations::UnLockTables(){ Line 1362  void PamelaDBOperations::UnLockTables(){
1362   */   */
1363  Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){    Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){  
1364    //    //
1365  //  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));
1366    //    //
1367    if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2)  ){    if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2)  ){
1368  //    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);
1369      return((Long64_t)pkt_num+16777215LL);      return((Long64_t)pkt_num+16777215LL);
1370    };    };
1371    //    //
1372    if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){    if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){
1373  //    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);
1374      return((Long64_t)pkt_num-16777215LL);      return((Long64_t)pkt_num-16777215LL);
1375    };    };
1376    //    //
1377  //  if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);    //  if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
1378    return((Long64_t)pkt_num);    return((Long64_t)pkt_num);
1379    //    //
1380  };  };
# Line 1282  Long64_t PamelaDBOperations::PKT(UInt_t Line 1384  Long64_t PamelaDBOperations::PKT(UInt_t
1384   */   */
1385  Long64_t PamelaDBOperations::OBT(UInt_t obt){    Long64_t PamelaDBOperations::OBT(UInt_t obt){  
1386    //    //
1387  //  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));
1388    //    //
1389    if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){    if ( obt < ((Long64_t)pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){
1390  //    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());
1391      return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());      return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());
1392    };    };
1393    //    //
1394    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) ){
1395  //    if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2));      //    if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2));
1396  //    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());
1397      return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());      return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
1398    };    };
1399    //    //
1400  //  if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);    //  if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);
1401    return((Long64_t)obt);    return((Long64_t)obt);
1402  };  };
1403    
# Line 1331  void PamelaDBOperations::FillClass(Bool_ Line 1433  void PamelaDBOperations::FillClass(Bool_
1433    UInt_t lastPkt = 0;    UInt_t lastPkt = 0;
1434    UInt_t rhtime = 0;    UInt_t rhtime = 0;
1435    UInt_t rttime = 0;    UInt_t rttime = 0;
1436      //
1437      if ( IsDebug() ) printf(" A firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1438      //
1439    if ( !mishead ){    if ( !mishead ){
1440      codh = ehh->GetCounter();      codh = ehh->GetCounter();
1441      if ( lastev+1 == firstev || (lastev == firstev && lastev != -1) ){            if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){      
1442          if ( IsDebug() ) printf(" B firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1443        firstev = 1;        firstev = 1;
1444        lastev = 0;        lastev = 0;
1445      } else {      } else {
# Line 1346  void PamelaDBOperations::FillClass(Bool_ Line 1452  void PamelaDBOperations::FillClass(Bool_
1452    };    };
1453    if ( !mistrail ){    if ( !mistrail ){
1454      codt = eht->GetCounter();      codt = eht->GetCounter();
1455      if ( lastev+1 == firstev || (lastev == firstev && lastev != -1)){            if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1)){      
1456          if ( IsDebug() ) printf(" C firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1457        lastev = 0;        lastev = 0;
1458        firstev = lastev+1;        firstev = lastev+1;
1459      } else {      } else {
# Line 1363  void PamelaDBOperations::FillClass(Bool_ Line 1470  void PamelaDBOperations::FillClass(Bool_
1470    if ( mishead ) {    if ( mishead ) {
1471      glrun->Set_GL_RUNH0();      glrun->Set_GL_RUNH0();
1472      //      //
1473      if ( lastev+1 == firstev || (lastev == firstev && lastev != -1) ){            if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){      
1474        firstObt = lastObt;        firstObt = lastObt;
1475        firstPkt = lastPkt;        firstPkt = lastPkt;
1476        rhtime = rttime;        rhtime = rttime;
# Line 1382  void PamelaDBOperations::FillClass(Bool_ Line 1489  void PamelaDBOperations::FillClass(Bool_
1489    if ( mistrail ){    if ( mistrail ){
1490      glrun->Set_GL_RUNT0();      glrun->Set_GL_RUNT0();
1491      //      //
1492      if ( lastev+1 == firstev || (lastev == firstev && lastev != -1) ){            if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){      
1493        lastObt = firstObt;        lastObt = firstObt;
1494        lastPkt = firstPkt;        lastPkt = firstPkt;
1495        rttime = rhtime;        rttime = rhtime;
# Line 1398  void PamelaDBOperations::FillClass(Bool_ Line 1505  void PamelaDBOperations::FillClass(Bool_
1505      glrun->SetRUNTRAILER_PKT(lastPkt);      glrun->SetRUNTRAILER_PKT(lastPkt);
1506      //          //    
1507    };    };
1508      //
1509      // ad hoc solution for INF EV_TO [8RED] 090113
1510      //
1511      if ( !firstev && lastev == -1 ){
1512        if ( IsDebug() ) printf(" EE firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1513        firstev = 1;
1514        lastev = 0;
1515      };
1516    glrun->SetEV_FROM((UInt_t)firstev);    glrun->SetEV_FROM((UInt_t)firstev);
1517    glrun->SetEV_TO((UInt_t)lastev);    glrun->SetEV_TO((UInt_t)lastev);
1518    glrun->SetNEVENTS((UInt_t)lastev-(UInt_t)firstev+1);    glrun->SetNEVENTS((UInt_t)lastev-(UInt_t)firstev+1);
# Line 1405  void PamelaDBOperations::FillClass(Bool_ Line 1520  void PamelaDBOperations::FillClass(Bool_
1520    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);
1521    //    //
1522    this->SetCommonGLRUN(rhtime,rttime);    this->SetCommonGLRUN(rhtime,rttime);
1523      this->SetPhysEndRunVariables();
1524    //    //
1525  };  };
1526    
# Line 1416  void PamelaDBOperations::FillClass(Bool_ Line 1532  void PamelaDBOperations::FillClass(Bool_
1532   * Insert a new row into GL_RAW table.   * Insert a new row into GL_RAW table.
1533   */   */
1534  Int_t PamelaDBOperations::insertPamelaRawFile(){  Int_t PamelaDBOperations::insertPamelaRawFile(){
1535    
1536        //
1537        Bool_t idr =  this->SetID_RAW();
1538        if ( idr ) return(1);
1539    
1540        GL_RAW glraw = GL_RAW();
1541        
1542        glraw.PATH = GetRawPath();
1543        glraw.NAME = GetRawFile();
1544        //    glraw.BOOT_NUMBER = 0;//???
1545        glraw.BOOT_NUMBER = this->GetBOOTnumber();
1546        
1547        if( insertPamelaRawFile(&glraw) )return(1);
1548        //
1549        idr =  this->SetID_RAW();
1550        if ( !idr ) throw -11;
1551        
1552        return(0);
1553    }
1554    /**
1555     * Insert a new row into GL_RAW table.
1556     */
1557    Int_t PamelaDBOperations::insertPamelaRawFile(GL_RAW *glraw){
1558    //    //
1559    stringstream oss;      if(!glraw)return(1);//?? ok I think
1560    //      //
1561    Bool_t idr =  this->SetID_RAW();      stringstream oss;
1562    if ( idr ) return(1);      //
1563    //      oss.str("");
1564    oss.str("");      if ( STATIC ){
1565    if ( STATIC ){          oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
1566      oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"              << glraw->PATH << "', '" << glraw->NAME << "')";
1567          << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";      } else {
1568    } else {          oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << glraw->NAME << "')";
1569      oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')";      };
1570    };      if ( debug ) cout <<oss.str().c_str() <<endl;
1571    if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;      if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1572    //      //
1573    idr =  this->SetID_RAW();      oss.str("");
1574    if ( !idr ) throw -11;      oss << "SELECT ID FROM GL_RAW WHERE NAME=\""<<glraw->NAME<<"\";";
1575    //      if ( debug ) cout << oss.str().c_str()<<endl;
1576    return(0);      if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1577        //
1578        TSQLResult *result = 0;
1579        TSQLRow    *row    = 0;
1580        result = conn->Query(oss.str().c_str());
1581        if ( result == NULL ) throw -4;
1582        row = result->Next();
1583        if ( !row ) return(1);
1584        glraw->ID = (UInt_t)atoll(row->GetField(0));
1585        if ( debug ) printf(" The ID of the RAW file is %u \n",glraw->ID);
1586        delete result;
1587        delete row;
1588        //
1589        return(0);
1590  }  }
1591    
1592    
# Line 1467  Int_t PamelaDBOperations::insertPamelaGL Line 1619  Int_t PamelaDBOperations::insertPamelaGL
1619      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
1620      if ( !result ) throw -10;      if ( !result ) throw -10;
1621      row = result->Next();      row = result->Next();
1622        if ( !row ) throw -92;
1623      idresof = (UInt_t)atoll(row->GetField(0));      idresof = (UInt_t)atoll(row->GetField(0));
1624      existsts = true;      existsts = true;
1625      goto eout;      goto eout;
# Line 1488  Int_t PamelaDBOperations::insertPamelaGL Line 1641  Int_t PamelaDBOperations::insertPamelaGL
1641      if ( !row ){      if ( !row ){
1642        oss.str("");        oss.str("");
1643        oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "        oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
1644            << dworbit << " order by FROM_ORBIT desc limit 1;";            << dworbit << " AND SPECIAL_FILE='' order by FROM_ORBIT desc limit 1;";
1645        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1646        result = conn->Query(oss.str().c_str());        result = conn->Query(oss.str().c_str());
1647        if ( !result ) throw -10;        if ( !result ) throw -10;
# Line 1746  Int_t PamelaDBOperations::insertPamelaGL Line 1899  Int_t PamelaDBOperations::insertPamelaGL
1899   * 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.
1900   */   */
1901  Int_t PamelaDBOperations::insertPamelaRootFile(){  Int_t PamelaDBOperations::insertPamelaRootFile(){
1902    stringstream oss;  
1903    TSQLResult *result = 0;      stringstream oss;
1904    TSQLRow    *row    = 0;      TSQLResult *result = 0;
1905    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;  
1906      //      //
1907      row = result->Next();      // ----------------------
1908        // determine the timesync
1909        // ----------------------
1910        UInt_t idtimesync = 0;
1911      //      //
1912      if ( !row ) throw -3;      if ( chewbacca ){
1913      idtimesync = (UInt_t)atoll(row->GetField(0));          oss.str("");
1914    } else {          oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";
1915      oss.str("");          if ( debug ) printf(" %s \n",oss.str().c_str());
1916      if ( STATIC ){          result = conn->Query(oss.str().c_str());
1917        oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "          //
1918            << " LEFT JOIN GL_ROOT "          if ( !result ) throw -3;
1919            << " ON GL_RAW.ID = GL_ROOT.ID_RAW "          //
1920            << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "          row = result->Next();
1921            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";          //
1922            if ( !row ) throw -3;
1923            idtimesync = (UInt_t)atoll(row->GetField(0));
1924      } else {      } else {
1925        oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "          oss.str("");
1926            << " LEFT JOIN GL_ROOT "          if ( STATIC ){
1927            << " ON GL_RAW.ID = GL_ROOT.ID_RAW "              oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1928            << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "                  << " LEFT JOIN GL_ROOT "
1929            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";                  << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1930                    << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1931                    << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1932            } else {
1933                oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1934                    << " LEFT JOIN GL_ROOT "
1935                    << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1936                    << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "
1937                    << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1938            };
1939            result = conn->Query(oss.str().c_str());
1940            //
1941            if ( !result ) throw -12;
1942            //
1943            row = result->Next();
1944            //
1945            if ( !row ) throw -10;
1946            if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){
1947                idroot = (UInt_t)atoll(row->GetField(2));
1948                delete row;
1949                delete result;
1950                return(1);
1951            };
1952            //
1953            // determine which timesync has to be used
1954            //
1955            oss.str("");
1956            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;";
1957            result = conn->Query(oss.str().c_str());
1958            //
1959            if ( !result ) throw -3;
1960            //
1961            row = result->Next();
1962            //
1963            if ( !row ) throw -3;
1964            idtimesync = (UInt_t)atoll(row->GetField(0));
1965      };      };
1966      result = conn->Query(oss.str().c_str());      
1967      //      delete row;
1968      if ( !result ) throw -12;      delete result;
1969      //  
1970      row = result->Next();      // ----------------------
1971      //      // insert root file
1972      if ( !row ) throw -10;      // ----------------------
1973      if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){  
1974        idroot = (UInt_t)atoll(row->GetField(2));      GL_ROOT glroot = GL_ROOT();
1975        return(1);      
1976        glroot.ID_RAW = GetID_RAW();
1977        glroot.ID_TIMESYNC = idtimesync;
1978        if ( STATIC ){
1979          glroot.PATH = GetRootPath();
1980        } else {
1981          if ( KEEPENV ){
1982            glroot.PATH = gSystem->DirName(filerootname.Data());
1983          } else {
1984            glroot.PATH = "$PAM_L0";
1985          };
1986      };      };
1987      //      glroot.NAME = GetRootFile();
1988      // determine which timesync has to be used  
1989      //      if ( insertPamelaRootFile(&glroot) )return 1;
1990      oss.str("");  
1991      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);
1992      result = conn->Query(oss.str().c_str());      
1993      //  
1994      if ( !result ) throw -3;      return (0);
1995      //  }
1996      row = result->Next();  /**
1997      //   * Insert all the new rows into GL_ROOT.
1998      if ( !row ) throw -3;   * The raw file indicates in the parameters should be already been stored in the database.
1999      idtimesync = (UInt_t)atoll(row->GetField(0));   */
2000    };  Int_t PamelaDBOperations::insertPamelaRootFile(GL_ROOT *glroot){
2001      stringstream oss;
2002      TSQLResult *result = 0;
2003      TSQLRow    *row    = 0;
2004      //
2005    //    //
2006    oss.str("");    oss.str("");
2007    if ( STATIC ){    oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
2008      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() << "')";  
   };  
2009    //    //
2010    if ( debug ) printf(" query is %s \n",oss.str().c_str());    if ( debug ) printf("%s \n",oss.str().c_str());
2011    if (conn->Query(oss.str().c_str()) == 0) throw -4;    if (conn->Query(oss.str().c_str()) == 0) throw -4;
2012    //    //
2013    delete result;    delete result;
2014    //    //
2015    oss.str("");    oss.str("");
2016    //  oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";    //  oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";
2017    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() <<"';";  
   };  
2018    //    //
2019    if ( debug ) printf(" query is %s \n",oss.str().c_str());    if ( debug ) printf("%s \n",oss.str().c_str());
2020    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
2021    if ( !result ) throw -12;    if ( !result ) throw -12;
2022    row = result->Next();    row = result->Next();
2023    if ( !row ) throw -3;    if ( !row ) throw -3;
2024    this->SetID_ROOT((UInt_t)atoll(row->GetField(0)));  
2025    if ( debug ) printf(" The ID of the ROOT file is %u \n",this->GetID_ROOT());    glroot->ID = (UInt_t)atoll(row->GetField(0));
2026    
2027      if ( debug ) printf(" The ID of the ROOT file is %u \n",glroot->ID);
2028    //    //
2029    delete result;    delete result;
2030    //    //
# Line 2061  Int_t PamelaDBOperations::insertPamelaRU Line 2252  Int_t PamelaDBOperations::insertPamelaRU
2252            cod = ehh->GetCounter();            cod = ehh->GetCounter();
2253            tcod = (UInt_t)cod->Get(pctp->Physics);            tcod = (UInt_t)cod->Get(pctp->Physics);
2254            evbefh = TMath::Max(chminentry,tcod);            evbefh = TMath::Max(chminentry,tcod);
2255  //        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
2256            if ( (UInt_t)evbefh == upperentry || !upperentry ) evbefh = upperentry + 1;            if ( (UInt_t)evbefh == upperentry || !upperentry ) evbefh = upperentry + 1;
2257  //            //
2258          if ( debug ) printf(" evbefh %i upperentry %u \n",evbefh,upperentry);            if ( debug ) printf(" evbefh %i upperentry %u \n",evbefh,upperentry);
2259            //            //
2260            this->HandleRunFragments(false,true,evbefh,upperentry);            this->HandleRunFragments(false,true,evbefh,upperentry);
2261            //            //
# Line 2091  Int_t PamelaDBOperations::insertPamelaRU Line 2282  Int_t PamelaDBOperations::insertPamelaRU
2282          ptht = cod->Get(pctp->RunHeader) - 1;          ptht = cod->Get(pctp->RunHeader) - 1;
2283          //      evbeft = cod->Get(pctp->Physics);          //      evbeft = cod->Get(pctp->Physics);
2284          tcod = (UInt_t)cod->Get(pctp->Physics);          tcod = (UInt_t)cod->Get(pctp->Physics);
2285          if ( !tcod ) tcod = 1;          if ( !tcod ) tcod = 1;    // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2286          evbeft = TMath::Min(upperentry,(tcod-1));          evbeft = TMath::Min(upperentry,(tcod-1));
2287          if ( debug ) printf(" Loop in runtrailers, evbeft is %u upperentry %u cod->getetc %u \n",evbeft,upperentry,cod->Get(pctp->Physics));          if ( debug ) printf(" Loop in runtrailers, evbeft is %u upperentry %u cod->getetc %u \n",evbeft,upperentry,cod->Get(pctp->Physics));
2288          //          //
# Line 2130  Int_t PamelaDBOperations::insertPamelaRU Line 2321  Int_t PamelaDBOperations::insertPamelaRU
2321            obth = phh->GetOrbitalTime();            obth = phh->GetOrbitalTime();
2322            cod = ehh->GetCounter();            cod = ehh->GetCounter();
2323            tcod = (UInt_t)cod->Get(pctp->Physics);            tcod = (UInt_t)cod->Get(pctp->Physics);
2324            if ( !tcod ) tcod = 1;            if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2325            evbefh = TMath::Max(chminentry,(tcod-1));            evbefh = TMath::Max(chminentry,(tcod-1));
2326            //            //
2327            if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){            if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){
# Line 2169  Int_t PamelaDBOperations::insertPamelaRU Line 2360  Int_t PamelaDBOperations::insertPamelaRU
2360                //                //
2361                this->HandleRunFragments(true,true,chminentry,(evbefh));                this->HandleRunFragments(true,true,chminentry,(evbefh));
2362                //                //
2363                  // what about pth=ptht-1
2364              };              };
2365            };            };
2366            //            //
# Line 2191  Int_t PamelaDBOperations::insertPamelaRU Line 2383  Int_t PamelaDBOperations::insertPamelaRU
2383                cod = ehh->GetCounter();                cod = ehh->GetCounter();
2384                //            evbeft = cod->Get(pctp->Physics);                //            evbeft = cod->Get(pctp->Physics);
2385                tcod = (UInt_t)cod->Get(pctp->Physics);                tcod = (UInt_t)cod->Get(pctp->Physics);
2386                if ( !tcod ) tcod = 1;                if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2387                evbeft = TMath::Min(upperentry,(tcod-1));                evbeft = TMath::Min(upperentry,(tcod-1));
2388                rh->GetEntry(pth);                rh->GetEntry(pth);
2389                phh = ehh->GetPscuHeader();                phh = ehh->GetPscuHeader();
# Line 2231  Int_t PamelaDBOperations::insertPamelaRU Line 2423  Int_t PamelaDBOperations::insertPamelaRU
2423          //      if ( ptt+1 == rtev){            //      if ( ptt+1 == rtev){  
2424          if ( conptt+1 == nrtev ){            if ( conptt+1 == nrtev ){  
2425            //    if ( conptt+1 == (nrtev+nrtbef )){              //    if ( conptt+1 == (nrtev+nrtbef )){  
2426              if ( IsDebug() ) printf(" >>>>>>>>>>> %i %u %i %u \n",ptht,rhev,nrtev,conptt);
2427            ptht++;            ptht++;
2428            if ( ptht < rhev ){            if ( ptht < rhev ){
2429              rh->GetEntry(ptht);              rh->GetEntry(ptht);
2430                //pth++;
2431                //if ( pth < rhev ){
2432                //rh->GetEntry(pth);
2433              phh = ehh->GetPscuHeader();              phh = ehh->GetPscuHeader();
2434              pkth = phh->GetCounter();              pkth = phh->GetCounter();
2435              obth = phh->GetOrbitalTime();              obth = phh->GetOrbitalTime();
# Line 2257  Int_t PamelaDBOperations::insertPamelaRU Line 2453  Int_t PamelaDBOperations::insertPamelaRU
2453              if ( (UInt_t)evbeft < upperentry-1 && upperentry>0 ){              if ( (UInt_t)evbeft < upperentry-1 && upperentry>0 ){
2454                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);
2455                //                //
2456                if ( (ptt-1) < 0 ) throw -15; // should never arrive here!                //              if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
2457                rt->GetEntry(ptt-1);                if ( (rtev-1) < 0 || ptt < 0 ) throw -15; // should never arrive here!
2458                  //              rt->GetEntry(ptt-1);
2459                  rt->GetEntry(rtev-1);
2460                cod = eht->GetCounter();                cod = eht->GetCounter();
2461                tcod = (UInt_t)cod->Get(pctp->Physics);                tcod = (UInt_t)cod->Get(pctp->Physics)+1;
2462                evbefh = TMath::Max(chminentry,tcod);                evbefh = TMath::Max(chminentry,tcod);
2463                //            evbefh = cod->Get(pctp->Physics);                //            evbefh = cod->Get(pctp->Physics);
2464                rt->GetEntry(ptt);                rt->GetEntry(ptt);
# Line 2367  Bool_t PamelaDBOperations::IsRunAlreadyI Line 2565  Bool_t PamelaDBOperations::IsRunAlreadyI
2565        //        //
2566        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");
2567        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2568  //       oss.str("");        //       oss.str("");
2569  //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2570  //       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());      
2571  //       conn->Query(oss.str().c_str());        //       conn->Query(oss.str().c_str());
2572        if ( signal ) signal = false;        if ( signal ) signal = false;
2573        goto gonext;              goto gonext;      
2574        //        //
# Line 2385  Bool_t PamelaDBOperations::IsRunAlreadyI Line 2583  Bool_t PamelaDBOperations::IsRunAlreadyI
2583        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");
2584        //        //
2585        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2586  //       oss.str("");        //       oss.str("");
2587  //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2588  //       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());      
2589  //       conn->Query(oss.str().c_str());        //       conn->Query(oss.str().c_str());
2590        //        //
2591        if ( signal ) signal = false;        if ( signal ) signal = false;
2592        goto gonext;        goto gonext;
# Line 2403  Bool_t PamelaDBOperations::IsRunAlreadyI Line 2601  Bool_t PamelaDBOperations::IsRunAlreadyI
2601        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");
2602        //        //
2603        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2604  //       oss.str("");        //       oss.str("");
2605  //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2606  //       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());      
2607  //       conn->Query(oss.str().c_str());        //       conn->Query(oss.str().c_str());
2608        if ( signal ) signal = false;        if ( signal ) signal = false;
2609        //        //
2610      };      };
# Line 2488  void PamelaDBOperations::HandleRunFragme Line 2686  void PamelaDBOperations::HandleRunFragme
2686      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");
2687      //      //
2688    } 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 ){  
2689      //      //
2690      // 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
2691      //      //
2692      if ( IsDebug() ) printf(" The run is new \n");      if ( firstev != (lastev+1) ){ // could be a problem sometimes (?)
2693      if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");        if ( rhfirstev != firstev && !mishead ) mishead = true;
2694          if ( rtlastev != lastev && !mistrail ) mistrail = true;
2695        };
2696        //
2697        if ( IsDebug() ) printf(" bhere firstev is %i lastev is %i \n",firstev,lastev);
2698        this->FillClass(mishead,mistrail,firstev,lastev);
2699        if ( IsDebug() ) printf(" chere firstev is %i lastev is %i \n",firstev,lastev);
2700      //      //
2701      glrun->SetID(this->AssignRunID());      if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
2702      glrun->SetID_RUN_FRAG(0);      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());
     glrun->Fill_GL_RUN_FRAGMENTS(conn);  
2703      //      //
2704    } else {      // First of all insert the run in the fragment table...
     if ( IsDebug() ) printf(" The run is already present in the fragment table \n");  
     if ( PEDANTIC ) throw -69;  
     return;  
   };  
   //  
   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  
2705      //      //
2706      oss.str("");      oss.str("");
2707      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 "
2708          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2709          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "          << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2710          << " ID != " << glrun->ID          << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2711          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2712            << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2713            << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2714            << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2715            << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2716            << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2717            << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2718            << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2719            << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2720            << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2721      //      //
2722      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());
2723      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
2724      //      //
2725      if ( !result ) throw -4;      if ( !result ) throw -4;
2726      //      //
2727      row = result->Next();      row = result->Next();
2728      //      //
2729      if ( !row && NoFrag() ){      if ( !row ){
2730          //
2731          // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
2732          //
2733          if ( IsDebug() ) printf(" The run is new \n");
2734          if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
2735          //
2736          glrun->SetID(this->AssignRunID());
2737          glrun->SetID_RUN_FRAG(0);
2738          glrun->Fill_GL_RUN_FRAGMENTS(conn);
2739          //
2740        } else {
2741          if ( IsDebug() ) printf(" The run is already present in the fragment table, relaxed %u \n",RELAXED);
2742          if ( !RELAXED ){
2743            if ( PEDANTIC ) throw -69;
2744            return;
2745          } else {
2746            glrun->SetID((UInt_t)atoll(row->GetField(0)));
2747            glrun->SetID_RUN_FRAG(0);
2748          };
2749        };
2750        //
2751        if ( chewbacca && mishead && mistrail ) goto justcheck;
2752        //
2753        // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
2754        //    
2755        if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
2756          // missing it no way we can found a piece in the frag table
2757        //        //
2758        oss.str("");        oss.str("");
2759        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 "
2760            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2761            << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "            << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2762            << " ID != " << glrun->ID            << " ID != " << glrun->ID
2763            << " 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!!
2764        //        //
2765        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());
2766        result = conn->Query(oss.str().c_str());        result = conn->Query(oss.str().c_str());
2767        //        //
2768        if ( !result ) throw -4;        if ( !result ) throw -4;
2769        //        //
       foundinrun = true;  
       //  
2770        row = result->Next();        row = result->Next();
2771        //        //
2772      };        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)));  
2773          //          //
2774          chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;            oss.str("");
2775            oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
2776                << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2777                << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2778                << " ID != " << glrun->ID
2779                << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
2780            //
2781            if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2782            result = conn->Query(oss.str().c_str());
2783            //
2784            if ( !result ) throw -4;
2785            //
2786            foundinrun = true;
2787            //
2788            row = result->Next();
2789          //          //
         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;  
           //  
         };  
2790        };        };
2791        if ( !found && chewbacca ) goto justcheck;        //
2792        if ( !found ){        if ( !row ){
2793            if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2794            found = false;
2795          } else {
2796          //          //
2797          // 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
2798          //          //
2799          ULong64_t chkpkt1 = 0;          if ( IsDebug() ) printf(" A 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);  
2800          //          //
2801          if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){          // if we have both runheader and runtrailer we can check with pkt_counter:
2802            //    if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){          //
2803            if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
2804              ULong64_t chkpkt = 0;  
2805              ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
2806              ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
2807            //            //
2808            if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);            chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;  
2809            //            //
2810            found = true;            if ( labs(chkpkt-pktt)<2 ){
2811                //
2812                if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2813                //
2814                found = true;
2815                if ( IsDebug() ) printf(" where firstev is %i lastev is %i \n",firstev,lastev);
2816                //
2817              } else {
2818                //
2819                if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);            
2820                //
2821                found = false;
2822                //
2823              };
2824            };
2825            if ( !found && chewbacca ) goto justcheck;
2826            if ( !found ){
2827            //            //
2828          } 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
2829            //            //
2830            if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);            ULong64_t chkpkt1 = 0;
2831              ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2832              ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2833              chkpkt1 = labs(orunh1-dbrunt1);
2834            //            //
2835            found = false;            ULong64_t chkpkt2 = 0;
2836              ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
2837              ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2838              chkpkt2 = labs(orunh2-dbrunt2);
2839              //
2840              ULong64_t chkpkt3 = 0;
2841              ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
2842              ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2843              chkpkt3 = labs(orunh3-dbrunt3);
2844            //            //
2845              if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2846                //  if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
2847                //
2848                if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2849                //
2850                found = true;
2851                //
2852              } else {
2853                //
2854                if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2855                //
2856                found = false;
2857                //
2858              };
2859          };          };
2860        };        };
     };  
     //  
     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));  
2861        //        //
2862        oss.str("");        if ( found ){
2863        oss << " ID="<<row->GetField(0)<<";";          //
2864        //          // 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
2865        glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos          //
2866        //          if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2867        // merge infos          //
2868        //          if ( foundinrun ){
2869        UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2870        ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2871        UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());          };
2872        ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());          //
2873        if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);          GL_RUN *glrun1 = new GL_RUN();
2874        TTree *T= 0;          //
2875        T = (TTree*)file->Get("Physics");          //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2876        if ( !T || T->IsZombie() ) throw -16;          //
2877        EventHeader *eh = 0;          oss.str("");
2878        PscuHeader *ph = 0;          oss << " ID="<<row->GetField(0)<<";";
2879        T->SetBranchAddress("Header", &eh);          //
2880        while ( apkt > bpkt && aobt > bobt && firstev < lastev ){          glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
2881          T->GetEntry(firstev);          if ( IsDebug() ) printf(" there firstev is %i lastev is %i \n",firstev,lastev);
2882          ph = eh->GetPscuHeader();          //
2883          bpkt = PKT(ph->GetCounter());          // merge infos
         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() ){  
2884          //          //
2885          //      glrun->SetID(this->AssignRunID());          UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
2886          glrun->SetID_RUN_FRAG(glrun1->GetID());          ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
2887          glrun->Fill_GL_RUN(conn);          UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
2888            ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
2889            if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2890            TTree *T= 0;
2891            T = (TTree*)file->Get("Physics");
2892            if ( !T || T->IsZombie() ) throw -16;
2893            EventHeader *eh = 0;
2894            PscuHeader *ph = 0;
2895            T->SetBranchAddress("Header", &eh);
2896            while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
2897              T->GetEntry(firstev);
2898              ph = eh->GetPscuHeader();
2899              bpkt = PKT(ph->GetCounter());
2900              bobt = OBT(ph->GetOrbitalTime());    
2901              firstev++;
2902              if ( PEDANTIC ) throw -71;
2903            };
2904            if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2905            //
2906            glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2907            glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
2908            glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
2909            glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
2910            glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
2911            //
2912            if ( IsDebug() ) printf(" here firstev is %i lastev is %i \n",firstev,lastev);
2913            //
2914            glrun->SetEV_FROM(firstev);
2915            glrun->SetNEVENTS(lastev-firstev+1);
2916            //
2917            glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
2918            glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
2919            glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
2920            glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
2921            glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
2922            glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
2923            glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
2924            glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
2925            glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
2926            glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
2927            glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());  
2928            glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());  
2929            glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());  
2930            glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());  
2931            glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());  
2932            glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());  
2933            //
2934            if ( glrun1->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glrun1->GetPHYSENDRUN_MASK_S3S2S12());
2935            if ( glrun1->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glrun1->GetPHYSENDRUN_MASK_S11CRC());
2936            //
2937            if ( !IsRunAlreadyInserted() ){
2938              //
2939              //    glrun->SetID(this->AssignRunID());
2940              glrun->SetID_RUN_FRAG(glrun1->GetID());
2941              glrun->Fill_GL_RUN(conn);
2942              //
2943              // set id number
2944              //
2945              glrun1->SetID_RUN_FRAG(glrun->GetID());
2946              glrun1->Fill_GL_RUN(conn);
2947              //
2948            };
2949            // delete old entry in fragment table
2950            //
2951            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2952            glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2953            //
2954            delete glrun1;
2955          //          //
         // set id number  
2956          //          //
2957          glrun1->SetID_RUN_FRAG(glrun->GetID());          return;
         glrun1->Fill_GL_RUN(conn);  
2958          //          //
2959        };        };
       // delete old entry in fragment table  
       //  
       glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");  
       glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");  
       //  
       delete glrun1;  
       //  
       //  
       return;  
2960        //        //
2961      };      };
2962      //      //
2963    };      if ( mistrail && ( rtlastev == lastev || chewbacca )) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
2964    //        // 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() ){  
2965        //        //
2966        oss.str("");        oss.str("");
2967        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 "
2968            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2969            << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "            << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2970            << " ID != " << glrun->ID            << " ID != " << glrun->ID
2971            << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";            << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2972        //        //
2973        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());
2974        result = conn->Query(oss.str().c_str());        result = conn->Query(oss.str().c_str());
2975        //        //
2976        if ( !result ) throw -4;        if ( !result ) throw -4;
2977        //        //
       foundinrun = true;  
2978        row = result->Next();        row = result->Next();
2979        //        //
2980      };        if ( !row && NoFrag() ){
2981      //          //
2982      if ( !row ){          oss.str("");
2983        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 "
2984        found = false;              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2985      } else {              << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2986        //              << " ID != " << glrun->ID
2987        found = false; // default value              << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2988        //          //
2989        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());
2990        //          result = conn->Query(oss.str().c_str());
2991        // if we have both runheader and runtrailer we can check with pkt_counter:          //
2992            if ( !result ) throw -4;
2993            //
2994            foundinrun = true;
2995            row = result->Next();
2996            //
2997          };
2998        //        //
2999        if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){        if ( !row ){
3000          ULong64_t chkpkt = 0;            if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
3001          ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));          found = false;
3002          ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());        } else {
3003          //          //
3004          chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;            found = false; // default value
3005          //          //
3006          if ( labs(chkpkt-pktt)<2 ){          if ( IsDebug() ) printf(" B Found a possible candidate, checking if it is the good one... \n");
3007            //
3008            // if we have both runheader and runtrailer we can check with pkt_counter:
3009            //
3010            if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){
3011              ULong64_t chkpkt = 0;  
3012              ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
3013              ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
3014            //            //
3015            if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);            chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;  
3016            //            //
3017            found = true;            if ( labs(chkpkt-pktt)<2 ){
3018                //
3019                if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
3020                //
3021                found = true;
3022                //
3023              } else {
3024                //
3025                if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);            
3026                //
3027                found = false;
3028                //
3029              };
3030            };
3031            if ( !found && chewbacca ) goto justcheck;
3032            if ( !found ){
3033            //            //
3034          } 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
3035            //            //
3036            if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);                  ULong64_t chkpkt1 = 0;
3037              ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
3038              ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
3039              chkpkt1 = labs(orunh1-dbrunt1);
3040            //            //
3041            found = false;            ULong64_t chkpkt2 = 0;
3042              ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT());
3043              ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
3044              chkpkt2 = labs(orunh2-dbrunt2);
3045            //            //
3046              ULong64_t chkpkt3 = 0;
3047              ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME());
3048              ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
3049              chkpkt3 = labs(orunh3-dbrunt3);
3050              //
3051              if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
3052                //
3053                if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
3054                //
3055                found = true;
3056                //
3057              } else {
3058                //
3059                if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
3060                //
3061                found = false;
3062                //
3063              };
3064          };          };
3065        };        };
3066        if ( !found && chewbacca ) goto justcheck;        //
3067        if ( !found ){        if ( found ){
3068            //
3069            // 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
3070            //
3071            if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
3072          //          //
3073          // 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 ){
3074              glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3075              glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
3076            };
3077          //          //
3078          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);  
3079          //          //
3080          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);  
3081          //          //
3082          ULong64_t chkpkt3 = 0;          oss.str("");
3083          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);  
3084          //          //
3085          if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){          glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runtrailer infos
3086            //          //
3087            if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);          // merge infos
3088            //          //
3089            found = true;          UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT());
3090            //          ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
3091          } else {          UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
3092            ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
3093            if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
3094            TTree *T= 0;
3095            T = (TTree*)file->Get("Physics");
3096            if ( !T || T->IsZombie() ) throw -16;
3097            EventHeader *eh = 0;
3098            PscuHeader *ph = 0;
3099            T->SetBranchAddress("Header", &eh);
3100            while ( apkt > bpkt && aobt > bobt && lastev > 0 ){
3101              T->GetEntry(lastev);
3102              ph = eh->GetPscuHeader();
3103              apkt = PKT(ph->GetCounter());
3104              aobt = OBT(ph->GetOrbitalTime());    
3105              lastev--;
3106              if ( PEDANTIC && !RELAXED) throw -72;
3107            };
3108            if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
3109            //
3110            glrun->SetEV_TO(lastev);
3111            glrun->SetNEVENTS(lastev-firstev+1);
3112            glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());
3113            glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER());
3114            glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME());
3115            glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
3116            glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
3117            //
3118            glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
3119            glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
3120            glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
3121            glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP());
3122            glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE());
3123            glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE());
3124            glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A());
3125            glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B());
3126            glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO());
3127            glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO());
3128            glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB());  
3129            glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE());  
3130            glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED());  
3131            glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK());  
3132            glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC());  
3133            glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC());  
3134            //
3135            if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ) glrun1->SetPHYSENDRUN_MASK_S3S2S12(glrun->GetPHYSENDRUN_MASK_S3S2S12());
3136            if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) glrun1->SetPHYSENDRUN_MASK_S11CRC(glrun->GetPHYSENDRUN_MASK_S11CRC());
3137            //
3138            if ( !IsRunAlreadyInserted() ){
3139              //
3140              //    glrun->SetID(this->AssignRunID());
3141              //
3142              glrun->SetID_RUN_FRAG(glrun1->GetID());
3143              glrun->Fill_GL_RUN(conn);
3144            //            //
3145            if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);            // set id number
3146            //            //
3147            found = false;            glrun1->SetID_RUN_FRAG(glrun->GetID());
3148              glrun1->Fill_GL_RUN(conn);
3149            //            //
3150          };          };
3151            //
3152            // delete old entries in fragment table
3153            //
3154            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3155            glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3156            //
3157            delete glrun1;
3158            //
3159            return;
3160            //
3161        };        };
3162          //
3163      };      };
3164      //      //
3165      if ( found ){    justcheck:
3166        //      //
3167        // 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");  
       };  
3168        //        //
3169        GL_RUN *glrun1 = new GL_RUN();        if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
3170        //        //
3171        //      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?
3172        //        //
3173        oss.str("");        oss.str("");
3174        oss << " ID="<<row->GetField(0)<<";";        oss << " SELECT ID,ID_ROOT_L0 FROM GL_RUN WHERE "
3175              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3176              << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3177              << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3178              << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3179              << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3180              << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3181              << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3182              << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3183              << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3184              << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3185              << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3186              << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3187              << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3188        //        //
3189        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());
3190          result = conn->Query(oss.str().c_str());
3191        //        //
3192        // merge infos        if ( !result ) throw -4;
3193        //        //
3194        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());    
3195        //        //
3196        if ( !IsRunAlreadyInserted() ){        if ( row ){
         //  
         //      glrun->SetID(this->AssignRunID());  
3197          //          //
3198          glrun->SetID_RUN_FRAG(glrun1->GetID());          // we end up here if chewbacca and we have RH---| small gap |---RT, in this case the two pieces are attached and moved to GL_RUN. We are now processing the
3199          glrun->Fill_GL_RUN(conn);          // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number.
3200          //          //
3201          // set id number          // 090112 [8RED (-70): RUN ALREADY INSERTED]
3202          //          //
3203          glrun1->SetID_RUN_FRAG(glrun->GetID());          Bool_t OK = false;
3204          glrun1->Fill_GL_RUN(conn);          UInt_t IDRL2A = 0;
3205            UInt_t IDRL2B = 0;
3206            if ( chewbacca ){
3207              if ( result->GetRowCount() == 2 ){
3208                IDRL2A = (UInt_t)atoll(row->GetField(0));
3209                UInt_t IDRL0A = (UInt_t)atoll(row->GetField(1));
3210                row = result->Next();
3211                IDRL2B = (UInt_t)atoll(row->GetField(0));
3212                UInt_t IDRL0B = (UInt_t)atoll(row->GetField(1));
3213                if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B);
3214                if ( IDRL0A == IDRL0B ){
3215                  TSQLResult *result2 = 0;
3216                  TSQLRow    *row2    = 0;
3217                  oss.str("");
3218                  oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;";
3219                  if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3220                  result2 = conn->Query(oss.str().c_str());
3221                  //
3222                  if ( !result2 ) throw -4;
3223                  //
3224                  row2 = result2->Next();
3225                  //
3226                  if ( row2 ){
3227                    UInt_t PKA = (UInt_t)atoll(row2->GetField(0));
3228                    delete result2;
3229                    oss.str("");
3230                    oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;";
3231                    if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3232                    result2 = conn->Query(oss.str().c_str());
3233                    //
3234                    if ( !result2 ) throw -4;
3235                    //
3236                    row2 = result2->Next();
3237                    //
3238                    if ( row2 ){
3239                      UInt_t PKB = (UInt_t)atoll(row2->GetField(0));
3240                      //
3241                      if ( IsDebug() ) printf(" PKT(PKA) + 1 %llu == runheaderpkt %llu && PKB = runtrailer %llu + 1 %llu \n",PKT(PKA)+1LL, PKT(glrun->GetRUNHEADER_PKT()),PKT(PKB), PKT(glrun->GetRUNTRAILER_PKT())+1LL);
3242                      if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){
3243                        if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
3244                        OK = true;
3245                      };
3246                    };
3247                  };                  
3248                };
3249              };      
3250            };
3251            if ( OK ){
3252              //
3253              // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table
3254              //
3255              // we have to update with runheader/trailer infos our run and we have to change the ROOT_ID_FRAG of the header piece ( 1/2 2/1 must become 1/3 3/2 2/1 )
3256              //
3257              GL_RUN *glA = new GL_RUN();
3258              glA->Query_GL_RUN(IDRL2A,conn);
3259              //
3260              if ( glA->GetACQ_BUILD_INFO() != 0 ){
3261                //
3262                // the first piece contains a good runheader we can update all the other runs with correct infos!
3263                //
3264                oss.str("");
3265                oss << "UPDATE GL_RUN_FRAGMENTS SET "
3266                    << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "
3267                    << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3268                    << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , "
3269                    << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , "
3270                    << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , "
3271                    << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , "
3272                    << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , "
3273                    << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , "
3274                    << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , "
3275                    << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , "
3276                    << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , "  
3277                    << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , "  
3278                    << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , "  
3279                    << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , "  
3280                    << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , ";
3281                //
3282                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3283                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
3284                if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3285                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , ";
3286                //        
3287                oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC();
3288                oss << " WHERE ID=" << glrun->GetID() << ";";
3289                if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str());
3290                conn->Query(oss.str().c_str());    
3291                //
3292              } else {
3293                //
3294                // sig no runheader, let set anyway what is possible...
3295                //
3296                oss.str("");
3297                oss << "UPDATE GL_RUN_FRAGMENTS SET "
3298                    << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , ";
3299                //
3300                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3301                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
3302                if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3303                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
3304                //        
3305                oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3306                    << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT();
3307                oss << " WHERE ID=" << glrun->GetID() << ";";
3308                if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str());
3309                conn->Query(oss.str().c_str());    
3310              };
3311              //
3312              // update runheader ROOT_ID_FRAG
3313              //
3314              oss.str("");
3315              oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
3316              if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3317              conn->Query(oss.str().c_str());
3318              //
3319              // now let's look for runtrailer if any in the last run
3320              //
3321              glA->Query_GL_RUN(IDRL2B,conn);
3322              //
3323              if ( glA->GetPKT_READY_COUNTER() != 0 ){
3324                //
3325                // the first piece contains a good runtrailer we can update all the other runs with correct infos!
3326                //
3327                oss.str("");
3328                oss << "UPDATE GL_RUN_FRAGMENTS SET "
3329                    << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3330                    << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "
3331                    << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , "
3332                    << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , ";
3333                //
3334                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3335                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3336                if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) {
3337                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3338                //        
3339                oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER()
3340                    << " WHERE ID=" << glrun->GetID() << ";";
3341                if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str());
3342                conn->Query(oss.str().c_str());    
3343                //
3344              } else {
3345                //
3346                // sig no runtrailer, let set anyway what is possible...
3347                //
3348                oss.str("");
3349                oss << "UPDATE GL_RUN_FRAGMENTS SET "
3350                    << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3351                    << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , ";
3352                  //
3353                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3354                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3355                if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){
3356                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3357                //        
3358                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()
3359                    << " WHERE ID=" << glrun->GetID() << ";";
3360                if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str());
3361                conn->Query(oss.str().c_str());    
3362              };
3363              //
3364              UInt_t myi = glrun->GetID();
3365              oss.str("");
3366              oss << " ID= "<< myi;
3367              //
3368              glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
3369              //
3370              // fill the new run in GL_RUN
3371              //
3372              glrun->SetID_RUN_FRAG(IDRL2B);
3373              glrun->Fill_GL_RUN(conn);    
3374              glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3375              delete glA;
3376              //
3377            } else {
3378              //
3379              // is just a repetition
3380              //
3381              if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");        
3382              if ( PEDANTIC ) throw -70;
3383            };
3384          //          //
3385          } else {
3386            if ( NoFrag() ){
3387              glrun->SetID_RUN_FRAG(glrun->GetID());
3388              glrun->Fill_GL_RUN(conn);    
3389              glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3390            };    
3391        };        };
       //  
       // delete old entries in fragment table  
       //  
       glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");  
       glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");  
       //  
       delete glrun1;  
       //  
       return;  
       //  
3392      };      };
     //  
   };  
   //  
  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");  
       };      
     };  
   };  
3393    }; // EEE    }; // EEE
3394    //    //
3395    return;    return;
# Line 3015  void PamelaDBOperations::HandleMissingHo Line 3418  void PamelaDBOperations::HandleMissingHo
3418        glrun->SetID(this->AssignRunID());        glrun->SetID(this->AssignRunID());
3419        glrun->SetID_RUN_FRAG(0);        glrun->SetID_RUN_FRAG(0);
3420        glrun->Fill_GL_RUN(conn);    // it'ok we arrive here only inside a file hence in the middle of the runs...        glrun->Fill_GL_RUN(conn);    // it'ok we arrive here only inside a file hence in the middle of the runs...
3421      };        };
3422      //      //
3423    };    };
3424    //    //
# Line 3059  Bool_t PamelaDBOperations::IsRunConsiste Line 3462  Bool_t PamelaDBOperations::IsRunConsiste
3462    if ( firstev == lastev+1 || lastev == firstev ) { // no events inside the run!    if ( firstev == lastev+1 || lastev == firstev ) { // no events inside the run!
3463      //if ( firstev <= lastev+1 ) { // no events inside the run!      //if ( firstev <= lastev+1 ) { // no events inside the run!
3464      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
3465        firstev = lastev+1; // this is necessary for files with no Physics entries, should have no influence on other files
3466      // return true is correct      // return true is correct
3467      return(true);      return(true);
3468      //      //
# Line 3068  Bool_t PamelaDBOperations::IsRunConsiste Line 3472  Bool_t PamelaDBOperations::IsRunConsiste
3472      code = eh->GetCounter();      code = eh->GetCounter();
3473      checkfirst = 0;      checkfirst = 0;
3474      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3475         if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
3476       };      };
3477      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);
3478      //      //
3479      T->GetEntry(lastev);      T->GetEntry(lastev);
3480      code = eh->GetCounter();      code = eh->GetCounter();
3481      checklast = 0;      checklast = 0;
3482      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3483         if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
3484       };      };
3485      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);
3486      //      //
3487      if ( checkfirst == checklast ){      if ( checkfirst == checklast ){
# Line 3088  Bool_t PamelaDBOperations::IsRunConsiste Line 3492  Bool_t PamelaDBOperations::IsRunConsiste
3492        //        //
3493      } else {      } else {
3494        //        //
3495        if ( IsDebug() ) printf(" There are no-phyics packets inside the run!\n");        if ( IsDebug() ) printf(" There are no-physics packets inside the run!\n");
3496        //        //
3497        // HERE WE MUST HANDLE THAT RUNS AND GO BACK        // HERE WE MUST HANDLE THAT RUNS AND GO BACK
3498        //        //
3499  //      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");
3500        //        //
3501        Bool_t emptyruns = false;        Bool_t emptyruns = false;
3502        UInt_t check = 0;        UInt_t check = 0;
3503        UInt_t lastevtemp = lastev;        UInt_t lastevtemp = lastev;
3504        UInt_t firstevno = firstev;        UInt_t firstevno = firstev;
3505          UInt_t rhchko=0;
3506          UInt_t rhchk=0;
3507        //        //
3508        for (UInt_t i=firstev; i<=lastev; i++){        for (UInt_t i=firstev; i<=lastev; i++){
3509          //          //
# Line 3106  Bool_t PamelaDBOperations::IsRunConsiste Line 3512  Bool_t PamelaDBOperations::IsRunConsiste
3512          //          //
3513          check = 0;          check = 0;
3514          //          //
3515    
3516            // if we have a runheader set lastev then exit
3517            //
3518          for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){          for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3519            if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));            if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
3520          };          };
3521            // check here if we have a runheader
3522            rhchko = rhchk;  
3523            rhchk = code->Get(GetPacketType("RunHeader"));    
3524          //          //
3525          if ( checkfirst < check || i == lastev ){          if ( checkfirst < check || i == lastev ){
3526            //            //
# Line 3184  Bool_t PamelaDBOperations::IsRunConsiste Line 3596  Bool_t PamelaDBOperations::IsRunConsiste
3596            };            };
3597            //                  //      
3598            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
3599              this->SetPhysEndRunVariables();
3600            //            //
3601            if ( chminentry == firstentry  ){ // EEE            if ( chminentry == firstentry  ){ // EEE
3602              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");
3603  //              //
3604  //          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
3605  //              //
   
   
   
   
   
   
   
   
   
   
   
   
   
   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  
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
3606    
3607                mishead = true;
3608    
3609    
3610                UInt_t rhfirstev = firstentry;
3611                //  UInt_t rtlastev = lastentry;
3612                Bool_t found = false;
3613                Bool_t foundinrun = false;
3614                //
3615                TSQLResult *result = 0;
3616                TSQLRow    *row    = 0;
3617                //
3618                stringstream oss;
3619                oss.str("");
3620                //
3621                // we have now the good first piece of a run, fill the glrun object
3622                //
3623                //  if ( rhfirstev != firstev && !mishead ) mishead = true;
3624                //  if ( rtlastev != lastev && !mistrail ) mistrail = true;
3625                //
3626                //  this->FillClass(mishead,mistrail,firstev,lastev);
3627                //
3628                if ( IsDebug() ) printf("zz The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
3629                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());
3630                //
3631                // First of all insert the run in the fragment table...
3632                //
3633                oss.str("");
3634                oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
3635                    << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3636                    << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3637                    << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3638                    << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3639                    << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3640                    << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3641                    << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3642                    << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3643                    << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3644                    << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3645                    << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3646                    << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3647                    << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3648                //
3649                if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3650                result = conn->Query(oss.str().c_str());
3651                //
3652                if ( !result ) throw -4;
3653                //
3654                row = result->Next();
3655                //
3656                if ( !row ){
3657                  //
3658                  // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
3659                  //
3660                  if ( IsDebug() ) printf(" The run is new \n");
3661                  if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
3662                  //
3663                  glrun->SetID(this->AssignRunID());
3664                  glrun->SetID_RUN_FRAG(0);
3665                  glrun->Fill_GL_RUN_FRAGMENTS(conn);
3666                  //
3667                } else {
3668                  if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
3669                  if ( PEDANTIC ) throw -69;
3670                  //    return;
3671                };
3672                //
3673                if ( chewbacca && mishead && mistrail ) goto zjustcheck;
3674                //
3675                // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
3676                //
3677                if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
3678                  // missing it no way we can found a piece in the frag table
3679                  //
3680                  oss.str("");
3681                  oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
3682                      << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3683                      << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3684                      << " ID != " << glrun->ID
3685                      << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
3686                  //
3687                  if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
3688                  result = conn->Query(oss.str().c_str());
3689                  //
3690                  if ( !result ) throw -4;
3691                  //
3692                  row = result->Next();
3693                  //
3694                  if ( !row && NoFrag() ){
3695                    //
3696                    oss.str("");
3697                    oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
3698                        << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3699                        << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3700                        << " ID != " << glrun->ID
3701                        << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
3702                    //
3703                    if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
3704                    result = conn->Query(oss.str().c_str());
3705                    //
3706                    if ( !result ) throw -4;
3707                    //
3708                    foundinrun = true;
3709                    //
3710                    row = result->Next();
3711                    //
3712                  };
3713                  //
3714                  if ( !row ){
3715                    if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
3716                    found = false;
3717                  } else {
3718                    //
3719                    found = false; // default value
3720                    //
3721                    if ( IsDebug() ) printf(" C Found a possible candidate, checking if it is the good one... \n");
3722                    //
3723                    // if we have both runheader and runtrailer we can check with pkt_counter:
3724                    //
3725                    if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
3726                      ULong64_t chkpkt = 0;
3727                      ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
3728                      ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
3729                      //
3730                      chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;
3731                      //
3732                      if ( labs(chkpkt-pktt)<2 ){
3733                        //
3734                        if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
3735                        //
3736                        found = true;
3737                        //
3738                      } else {
3739                        //
3740                        if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
3741                        //
3742                        found = false;
3743                        //
3744                      };
3745                    };
3746                    if ( !found && chewbacca ) goto zjustcheck;
3747                    if ( !found ){
3748                      //
3749                      // 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
3750                      //
3751                      ULong64_t chkpkt1 = 0;
3752                      ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
3753                      ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
3754                      chkpkt1 = labs(orunh1-dbrunt1);
3755                      //
3756                      ULong64_t chkpkt2 = 0;
3757                      ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
3758                      ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
3759                      chkpkt2 = labs(orunh2-dbrunt2);
3760                      //
3761                      ULong64_t chkpkt3 = 0;
3762                      ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
3763                      ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
3764                      chkpkt3 = labs(orunh3-dbrunt3);
3765                      //
3766                      if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
3767                        //    if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
3768                        //
3769                        if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
3770                        //
3771                        found = true;
3772                        //
3773                      } else {
3774                        //
3775                        if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
3776                        //
3777                        found = false;
3778                        //
3779                      };
3780                    };
3781                  };
3782                  //
3783                  if ( found ){
3784                    //
3785                    // 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
3786                    //
3787                    if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
3788                    //
3789                    if ( foundinrun ){
3790                      glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3791                      glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
3792                    };
3793                    //
3794                    GL_RUN *glrun1 = new GL_RUN();
3795                    //
3796                    //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
3797                    //
3798                    oss.str("");
3799                    oss << " ID="<<row->GetField(0)<<";";
3800                    //
3801                    glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
3802                    //
3803                    // merge infos
3804                    //
3805                    UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
3806                    ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
3807                    UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
3808                    ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
3809                    if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
3810                    TTree *T= 0;
3811                    T = (TTree*)file->Get("Physics");
3812                    if ( !T || T->IsZombie() ) throw -16;
3813                    EventHeader *eh = 0;
3814                    PscuHeader *ph = 0;
3815                    T->SetBranchAddress("Header", &eh);
3816                    while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
3817                      T->GetEntry(firstev);
3818                      ph = eh->GetPscuHeader();
3819                      bpkt = PKT(ph->GetCounter());
3820                      bobt = OBT(ph->GetOrbitalTime());
3821                      firstev++;
3822                      if ( PEDANTIC ) throw -71;
3823                    };
3824                    if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
3825                    //
3826                    glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
3827                    glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
3828                    glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
3829                    glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
3830                    glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
3831                    //
3832                    glrun->SetEV_FROM(firstev);
3833                    glrun->SetNEVENTS(lastev-firstev+1);
3834                    //
3835                    glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
3836                    glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
3837                    glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
3838                    glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
3839                    glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
3840                    glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
3841                    glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
3842                    glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
3843                    glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
3844                    glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
3845                    glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());
3846                    glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());
3847                    glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());
3848                    glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());
3849                    glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());
3850                    glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());
3851                    //
3852                    if ( glrun1->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glrun1->GetPHYSENDRUN_MASK_S3S2S12());
3853                    if ( glrun1->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glrun1->GetPHYSENDRUN_MASK_S11CRC());
3854                    //
3855                    if ( !IsRunAlreadyInserted() ){
3856                      //
3857                      //      glrun->SetID(this->AssignRunID());
3858                      glrun->SetID_RUN_FRAG(glrun1->GetID());
3859                      glrun->Fill_GL_RUN(conn);
3860                      //
3861                      // set id number
3862                      //
3863                      glrun1->SetID_RUN_FRAG(glrun->GetID());
3864                      glrun1->Fill_GL_RUN(conn);
3865                      //
3866                    };
3867                    // delete old entry in fragment table
3868                    //
3869                    glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3870                    glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3871                    //
3872                    delete glrun1;
3873                    //
3874                    //
3875                    //      return;
3876                    //
3877                  };
3878                  //
3879                };
3880                //
3881                //
3882              zjustcheck:
3883                //
3884                if ( !found ){
3885                  //
3886                  if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
3887                  //
3888                  // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
3889                  //
3890                  oss.str("");
3891                  oss << " SELECT ID FROM GL_RUN WHERE "
3892                      << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3893                      << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3894                      << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3895                      << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3896                      << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3897                      << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3898                      << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3899                      << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3900                      << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3901                      << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3902                      << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3903                      << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3904                      << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3905                  //
3906                  if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3907                  result = conn->Query(oss.str().c_str());
3908                  //
3909                  if ( !result ) throw -4;
3910                  //
3911                  row = result->Next();
3912                  //
3913                  if ( row ){
3914                    if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
3915                    if ( PEDANTIC ) throw -70;
3916                  } else {
3917                    if ( NoFrag() ){
3918                      glrun->SetID_RUN_FRAG(glrun->GetID());
3919                      glrun->Fill_GL_RUN(conn);
3920                      glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3921                    };
3922                  };
3923                }; // EEE
3924    
3925    
3926            } else {            } else {
3927            if ( !IsRunAlreadyInserted() ){              if ( !IsRunAlreadyInserted() ){
3928              glrun->SetID(this->AssignRunID());                glrun->SetID(this->AssignRunID());
3929              glrun->SetID_RUN_FRAG(0);                glrun->SetID_RUN_FRAG(0);
3930              glrun->Fill_GL_RUN(conn);                glrun->Fill_GL_RUN(conn);
3931            };              };
3932            }; // EEE            }; // EEE
3933            //            //
3934            firstevno = lastentry + 1;            firstevno = lastentry + 1;
# Line 3564  Bool_t PamelaDBOperations::IsRunConsiste Line 3937  Bool_t PamelaDBOperations::IsRunConsiste
3937            //            //
3938          };          };
3939          //          //
3940            if ( i > firstev ){
3941              if ( rhchko != rhchk ){
3942                if ( IsDebug() ) printf("oh oh... we have a runheader! stop here and handle later the remaining piece\n");
3943                lastev = i;
3944                return(false);
3945              };
3946            };
3947            //
3948          if ( check == checklast && i != lastev ){          if ( check == checklast && i != lastev ){
3949            lastevtemp = i - 1;            lastevtemp = i - 1;
3950            i = lastev - 1;            i = lastev - 1;
# Line 3647  void PamelaDBOperations::HandleSuspiciou Line 4028  void PamelaDBOperations::HandleSuspiciou
4028      code = eh->GetCounter();      code = eh->GetCounter();
4029      checkfirst = 0;      checkfirst = 0;
4030      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
4031         if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
4032         if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;        if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;
4033       };      };
4034      if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);      if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);
4035      //      //
4036      T->GetEntry(lastev);      T->GetEntry(lastev);
4037      code = eh->GetCounter();      code = eh->GetCounter();
4038      checklast = 0;      checklast = 0;
4039      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){      for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
4040         if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));        if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
4041       };      };
4042      if ( IsDebug() ) printf(" Check last is %i \n",checklast);      if ( IsDebug() ) printf(" Check last is %i \n",checklast);
4043      //      //
4044      if ( checkfirst == checklast ){      if ( checkfirst == checklast ){
# Line 3769  void PamelaDBOperations::HandleSuspiciou Line 4150  void PamelaDBOperations::HandleSuspiciou
4150            };            };
4151            //                  //      
4152            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
4153              this->SetPhysEndRunVariables();
4154            //            //
4155            if ( !IsRunAlreadyInserted() ){            if ( !IsRunAlreadyInserted() ){
4156              glrun->SetID(this->AssignRunID());              glrun->SetID(this->AssignRunID());
# Line 3808  void PamelaDBOperations::HandleSuspiciou Line 4190  void PamelaDBOperations::HandleSuspiciou
4190              if ( IsDebug() ) printf(" We have the runtrailer \n");              if ( IsDebug() ) printf(" We have the runtrailer \n");
4191              //                  //    
4192              this->SetCommonGLRUN(firstTime,lastTime);              this->SetCommonGLRUN(firstTime,lastTime);
4193                this->SetPhysEndRunVariables();
4194              //              //
4195              if ( !IsRunAlreadyInserted() ){              if ( !IsRunAlreadyInserted() ){
4196                glrun->SetID(this->AssignRunID());                glrun->SetID(this->AssignRunID());
# Line 4382  Int_t PamelaDBOperations::insertCALOPULS Line 4765  Int_t PamelaDBOperations::insertCALOPULS
4765   * Fill the GL_TRK_CALIB table   * Fill the GL_TRK_CALIB table
4766   */   */
4767  void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){  void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){
4768    //      
4769    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 ){  
4770      //      //
4771      if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");      glcal->ID         = 0;
4772      if ( PEDANTIC ) throw -80;      glcal->ID_ROOT_L0 = GetID_ROOT();
4773        glcal->EV_ROOT_CALIBTRK1 = t1;
4774        glcal->EV_ROOT_CALIBTRK2 = t2;
4775        glcal->FROM_TIME = fromtime;
4776        glcal->TO_TIME   = 0;
4777        glcal->OBT1      = obt1;
4778        glcal->OBT2      = obt2;
4779        glcal->PKT1      = pkt1;
4780        glcal->PKT2      = pkt2;
4781        glcal->BOOT_NUMBER = GetBOOTnumber();
4782        glcal->VALIDATION = valid;
4783      //      //
4784    } else {      HandleTRK_CALIB(glcal);
     //  
     // we have to insert a new calibration, check where to place it  
4785      //      //
4786      oss.str("");      delete glcal;
4787      oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "  }
4788          << " FROM_TIME < "<< fromtime << " AND "  /**
4789          << " TO_TIME > "<< fromtime << ";";   * Fill the GL_TRK_CALIB table
4790     */
4791    void PamelaDBOperations::HandleTRK_CALIB(GL_TRK_CALIB *glcal){
4792    
4793        Bool_t pk1 = (glcal->OBT1>0&&glcal->PKT1>0);
4794        Bool_t pk2 = (glcal->OBT2>0&&glcal->PKT2>0);
4795        UInt_t boot_number = glcal->BOOT_NUMBER;
4796        UInt_t obt1 = glcal->OBT1;
4797        UInt_t obt2 = glcal->OBT2;
4798        UInt_t pkt1 = glcal->PKT1;
4799        UInt_t pkt2 = glcal->PKT2;
4800        UInt_t fromtime = glcal->FROM_TIME;
4801        UInt_t totime = 0;
4802        UInt_t idroot = glcal->ID_ROOT_L0;
4803        UInt_t t1 = glcal->EV_ROOT_CALIBTRK1;
4804        UInt_t t2 = glcal->EV_ROOT_CALIBTRK2;
4805        UInt_t valid = glcal->VALIDATION;
4806      //      //
4807      if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());      TSQLResult *result = 0;
4808      result = conn->Query(oss.str().c_str());      TSQLRow    *row    = 0;
4809      //      //
4810      if ( !result ) throw -4;      stringstream oss;
4811        oss.str("");
4812      //      //
     row = result->Next();  
4813      //      //
4814      if ( !row ){      if ( !pk1 && !pk2 ){
4815        //          if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n");
4816        // 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;  
       //  
4817      };      };
4818      //      //
4819      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 << "',";  
4820      //      //
4821      if ( !pk1 ){      oss.str("");
4822        oss << "NULL,";      oss << " SELECT ID FROM GL_TRK_CALIB WHERE "
4823            << " BOOT_NUMBER = "<< boot_number; //
4824        oss << " AND FROM_TIME="<<fromtime; /// NEWNEWNEW -- VA BENE ?!?!?!?!
4825        oss << " AND ( ( ";  
4826        if ( pk1 ){
4827            oss << " OBT1 = "<< obt1 << " AND "
4828                << " PKT1 = "<< pkt1
4829                << " ) OR ( ";
4830      } else {      } else {
4831        oss << "'"          oss << " PKT1 = "<< pkt2-1
4832            << t1 << "',";              << " ) OR ( ";  
4833      };      };      
4834      //      if ( pk2 ){
4835      if ( !pk2 ){          oss << " OBT2 = "<< obt2 << " AND "
4836        oss << "NULL,'";              << " PKT2 = "<< pkt2;
4837      } else {      } else {
4838        oss << "'"          oss << " PKT2 = "<< pkt1+1;
4839            << t2 << "','";      };      
4840      };      oss << " ) );";  
4841        //
4842        if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str());
4843        result = conn->Query(oss.str().c_str());
4844      //      //
4845      oss << fromtime << "','"      if ( !result ) throw -4;
         << totime << "','"  
         << obt1 << "','"  
         << pkt1 << "','"  
         << obt2 << "','"  
         << pkt2 << "','"  
         << this->GetBOOTnumber() << "','"  
         << valid << "');";  
4846      //      //
4847      if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());      row = result->Next();
4848      //      //
4849        if ( row ){
4850            //
4851            if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");
4852            if ( PEDANTIC ) throw -80;
4853            //
4854        } else {
4855            //
4856            // we have to insert a new calibration, check where to place it
4857            //
4858            oss.str("");
4859            oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "
4860                << " FROM_TIME < "<< fromtime << " AND "
4861                << " TO_TIME > "<< fromtime << ";";
4862            //
4863            if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());
4864            result = conn->Query(oss.str().c_str());
4865            //
4866            if ( !result ) throw -4;
4867            //
4868            row = result->Next();
4869            //
4870            if ( !row ){
4871                //
4872                // no calibrations in the db contain our calibration
4873                //
4874                if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
4875                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
4876                //
4877                oss.str("");
4878                oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
4879                    << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
4880                //
4881                if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
4882                result = conn->Query(oss.str().c_str());
4883                //
4884                if ( !result ) throw -4;
4885                //
4886                row = result->Next();
4887                if ( !row ){
4888                    totime = numeric_limits<UInt_t>::max();
4889                } else {
4890                    totime = (UInt_t)atoll(row->GetField(0));
4891                };
4892                //
4893            } else {
4894                //
4895                // determine upper and lower limits and make space for the new calibration
4896                //
4897                totime = (UInt_t)atoll(row->GetField(1));
4898                //
4899                oss.str("");
4900                oss << " UPDATE GL_TRK_CALIB SET "  
4901                    << " TO_TIME = "<< fromtime << " WHERE "  // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
4902                    << " ID = "<< row->GetField(0) << ";";
4903                //
4904                if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str());
4905                result = conn->Query(oss.str().c_str());
4906                //
4907                if ( !result ) throw -4;
4908                //
4909            };
4910            //
4911            oss.str("");
4912            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) "
4913                << " VALUES (NULL,' "
4914                << idroot << "',";
4915            //
4916            if ( !pk1 ){
4917                oss << "NULL,";
4918            } else {
4919                oss << "'"
4920                    << t1 << "',";
4921            };
4922            //
4923            if ( !pk2 ){
4924                oss << "NULL,'";
4925            } else {
4926                oss << "'"
4927                    << t2 << "','";
4928            };
4929            //
4930            oss << fromtime << "','"
4931                << totime << "','"
4932                << obt1 << "','"
4933                << pkt1 << "','"
4934                << obt2 << "','"
4935                << pkt2 << "','"
4936                << boot_number << "','"
4937                << valid << "');";
4938            //
4939            if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());
4940            //
4941            result = conn->Query(oss.str().c_str());
4942            //
4943            if ( !result ) throw -4;
4944            //
4945        };
4946        
4947        oss.str("");
4948        oss << " SELECT ID FROM GL_TRK_CALIB ORDER BY ID DESC LIMIT 1 ;";  
4949        if ( IsDebug() ) cout << oss.str().c_str() << endl;
4950      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
4951        if ( !result ) throw -4;;
4952        row = result->Next();
4953        if(row)glcal->ID = (UInt_t)atoll(row->GetField(0));
4954      //      //
4955      if ( !result ) throw -4;      delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
4956      //      //
   };  
   //  
4957  };  };
4958    
4959  /**  /**
4960   * Scan tracker calibrations packets, fill the GL_TRK_CALIB table   * Scan tracker calibrations packets, fill the GL_TRK_CALIB table
4961   */   */
4962  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++){  
4963      //      //
4964      pret2 = t2;      CalibTrk1Event *caltrk1 = 0;
4965      tr1->GetEntry(t1);      CalibTrk2Event *caltrk2 = 0;
4966        TTree *tr1 = 0;
4967        TTree *tr2 = 0;
4968        EventHeader *eh1 = 0;
4969        PscuHeader *ph1 = 0;
4970        EventHeader *eh2 = 0;
4971        PscuHeader *ph2 = 0;
4972      //      //
4973      ph1 = eh1->GetPscuHeader();      PacketType *pctp=0;
4974      obt1 = ph1->GetOrbitalTime();        EventCounter *codt2=0;
     pkt1 = ph1->GetCounter();    
     fromtime = this->GetAbsTime(ph1->GetOrbitalTime());    
4975      //      //
4976  //     valid = 1;      Int_t nevents1 = 0;
4977  //     //      Int_t nevents2 = 0;
 //     if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1  
4978      //      //
4979        fromtime = 0;
4980      //      //
4981      if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){      obt1 = 0;
4982        //    if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){      pkt1 = 0;
4983        //      obt2 = 0;
4984        if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u \n",fromtime,obt1,pkt1);      pkt2 = 0;
4985        //            //
4986        valid = ValidateTrkCalib( caltrk1, eh1 );      tr1 = (TTree*)file->Get("CalibTrk1");
4987        if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;      if ( !tr1 || tr1->IsZombie() ) throw -22;
4988        //      tr2 = (TTree*)file->Get("CalibTrk2");
4989        // Do we have the second calibration packet?      if ( !tr2 || tr2->IsZombie() ) throw -23;
4990        //      //
4991        while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1      tr1->SetBranchAddress("CalibTrk1", &caltrk1);
4992        tr1->SetBranchAddress("Header", &eh1);
4993        nevents1 = tr1->GetEntries();
4994        tr2->SetBranchAddress("CalibTrk2", &caltrk2);
4995        tr2->SetBranchAddress("Header", &eh2);
4996        nevents2 = tr2->GetEntries();
4997        //
4998        if ( !nevents1 && !nevents2 ) return(1);
4999        //
5000        t2 = -1;
5001        Int_t pret2 = 0;
5002        Int_t t2t1cal = 0;
5003        //
5004        bool MISSING_pkt1 = true;
5005        bool MISSING_pkt2 = true;
5006        int  ncalib = 0;
5007        bool try_to_recover = false;
5008        //
5009        for (t1=0; t1 < nevents1; t1++){//loop over packet1
5010          //          //
5011          t2++;          pret2 = t2;
5012            tr1->GetEntry(t1);
5013          //          //
5014          pret2 = t2 - 1; // EMILIANO          ph1 = eh1->GetPscuHeader();
5015            obt1 = ph1->GetOrbitalTime();  
5016            pkt1 = ph1->GetCounter();  
5017            fromtime = GetAbsTime(ph1->GetOrbitalTime());  
5018          //          //
5019          if ( t2 < nevents2 ){          // chek if the packet number and obt are consistent with the other packets ???
5020            tr2->GetEntry(t2);          //
5021            codt2 = eh2->GetCounter();          if ( PKT(pkt1) >= PKT(pktfirst) && PKT(pkt1) <= upperpkt && OBT(obt1) >= OBT(obtfirst) && OBT(obt1) <= upperobt ){
5022            t2t1cal = codt2->Get(pctp->CalibTrk1);              //    if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){
5023            //              //
5024            ph2 = eh2->GetPscuHeader();              if ( IsDebug() ) printf("\n Trk calibration1 %u at time %u obt %u pkt %u \n",t1,fromtime,obt1,pkt1);
5025            obt2 = ph2->GetOrbitalTime();                //      
5026            pkt2 = ph2->GetCounter();                valid = ValidateTrkCalib( caltrk1, eh1 );
5027            //              if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
5028            if ( IsDebug() ) printf(" This is a trk calibration2 at obt %u pkt %u t2 is %u , t2t1cal is %u \n",obt2,pkt2,t2,t2t1cal);                    //
5029  //        if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2              // Do we have the second calibration packet?
5030            //                    //
5031                if ( IsDebug() ) cout << " Loop over calibration2 to search associated calibration: "<<endl;
5032                while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1
5033                    //
5034                    t2++;
5035                    //
5036                    pret2 = t2 - 1; // EMILIANO
5037                    //
5038                    if ( t2 < nevents2 ){
5039                        tr2->GetEntry(t2);
5040                        codt2 = eh2->GetCounter();
5041                        t2t1cal = codt2->Get(pctp->CalibTrk1);
5042                        //
5043                        ph2 = eh2->GetPscuHeader();
5044                        obt2 = ph2->GetOrbitalTime();  
5045                        pkt2 = ph2->GetCounter();  
5046                        //
5047                        if ( IsDebug() ) printf(" >> trk calibration2 at obt %u pkt %u t2 is %u , t2t1cal is %u \n",obt2,pkt2,t2,t2t1cal);    
5048                        //    if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2
5049                        //    
5050                    } else {
5051                        //
5052                        // running out of vector without finding the corresponding calibration, sig
5053                        //
5054                        if ( IsDebug() ) printf(" t2 >= nevents2 \n");
5055                        pret2 = t2;
5056                        obt2 = 0;
5057                        //    pkt2 = pkt1+2;
5058                        pkt2 = 0;
5059                        t2t1cal = t1+1;
5060                    };
5061                    //      if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) && (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){
5062    
5063                    // EMILIANO
5064                    //      if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) || (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){
5065                    //        //    if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
5066                    //        if ( IsDebug() ) printf(" running out of vector without finding the corresponding calibration, sig \n");
5067                    //        //
5068                    //        // running out of vector without finding the corresponding calibration, sig
5069                    //        //
5070                    //        pret2 = t2;
5071                    //        obt2 = 0;
5072                    //        //      pkt2 = pkt1+2;
5073                    //        pkt2 = 0;
5074                    //        t2t1cal = t1+1;
5075                    //      };
5076    
5077    
5078                    //
5079                };
5080                //
5081                if ( IsDebug() ) printf(" Check if trk calibration2 is the right one \n");
5082                //
5083                // EMILIANO
5084                if ( ( PKT(pkt2) < PKT(pktfirst) || PKT(pkt2) > upperpkt) || (OBT(obt2) < OBT(obtfirst) || OBT(obt2) > upperobt) ){
5085                    //      if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
5086                    if ( IsDebug() ) printf(" *WARNING*  The calibration found is outside the interval, sig \n");
5087                    //
5088                    // running out of vector without finding the corresponding calibration, sig
5089                    //
5090                    pret2 = t2;
5091                    obt2 = 0;
5092                    pkt2 = 0;
5093                };
5094                if ( PKT(pkt2) == PKT(pkt1)+1 ){
5095                    if ( IsDebug() ) cout << " ...OK"<<endl;
5096                    // =======================
5097                    // The calibration is good
5098                    // =======================
5099                    //
5100    //      if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2);
5101    //      if ( IsDebug() ) printf(" Trk calibration2 at obt %u pkt %u t2 is %u is good \n",obt2,pkt2,t2);
5102    //        if ( IsDebug() ) printf("\n Trk calibration2 at time %u obt %u pkt %u \n",fromtime,obt2,pkt2);
5103                    if ( IsDebug() ) printf(" Trk calibration2 %u at time %u obt %u pkt %u \n",t2,fromtime,obt2,pkt2);
5104                    //
5105                    UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
5106                    if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
5107    //      valid = valid & valid2;
5108                    valid = valid & valid2; //QUESTO VA CAMBIATO
5109                    //
5110                    // Handle good calib
5111                    //
5112                    MISSING_pkt1 = false;
5113                    MISSING_pkt2 = false;
5114    //              this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5115                    //
5116                    // Check for missing calibtrk1
5117                    //
5118                    if ( t2 != pret2+1 ){
5119                        //
5120                        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);
5121                        //
5122                        while ( t2 > pret2+1 ){
5123                            //
5124                            // handle missing calib1
5125                            //
5126                            pret2++;
5127                            //
5128                            obt1 = 0;
5129                            pkt1 = 0;
5130                            //
5131                            tr2->GetEntry(pret2);
5132                            ph2 = eh2->GetPscuHeader();
5133                            obt2 = ph2->GetOrbitalTime();  
5134                            pkt2 = ph2->GetCounter();  
5135                            //
5136                            fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
5137                            //
5138                            valid = 0;
5139                            MISSING_pkt1 = true;
5140                            MISSING_pkt2 = false;
5141    //                      this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5142                            //
5143                        };
5144                        //
5145                    };
5146                    //
5147                } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){
5148                    //
5149                    // Check for missing calibtrk2
5150                    //
5151                    if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
5152                    t2 = pret2;
5153                    //
5154                    // handle missing calib2
5155                    //
5156                    obt2 = 0;
5157                    pkt2 = 0;
5158                    valid = 0;
5159                    MISSING_pkt1 = false;
5160                    MISSING_pkt2 = true;
5161    //              this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5162                    //
5163                };
5164                //
5165    
5166                if( !(MISSING_pkt1&MISSING_pkt2) ){
5167                  this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5168                  ncalib++;
5169                  if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;
5170                }
5171    
5172    
5173          } else {          } else {
5174            //            //
5175            // 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);
5176            //              //      if ( PEDANTIC ) throw -79;
5177            if ( IsDebug() ) printf(" t2 >= nevents2 \n");              //
           pret2 = t2;  
           obt2 = 0;  
           //      pkt2 = pkt1+2;  
           pkt2 = 0;  
           t2t1cal = t1+1;  
5178          };          };
5179          //      if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) && (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){          //    
   
         // EMILIANO  
 //      if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) || (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){  
 //        //    if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){  
 //        if ( IsDebug() ) printf(" running out of vector without finding the corresponding calibration, sig \n");  
 //        //  
 //        // running out of vector without finding the corresponding calibration, sig  
 //        //  
 //        pret2 = t2;  
 //        obt2 = 0;  
 //        //      pkt2 = pkt1+2;  
 //        pkt2 = 0;  
 //        t2t1cal = t1+1;  
 //      };  
5180    
5181        }; //end loop on pkt1
5182    
5183    
5184    
5185        //
5186        // we have one more calib pkt2 !
5187        //
5188        t2++;
5189        while ( t2 < nevents2 ){
5190          //          //
5191        };          // 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  
5192          //          //
5193          this->HandleTRK_CALIB(true,true);          if ( IsDebug() ) printf(" t2 is %u nevents2 is %u \n",t2,nevents2);
5194            obt1 = 0;
5195            pkt1 = 0;
5196          //          //
5197          // Check for missing calibtrk1          tr2->GetEntry(t2);
5198            ph2 = eh2->GetPscuHeader();
5199            obt2 = ph2->GetOrbitalTime();  
5200            pkt2 = ph2->GetCounter();  
5201          //          //
5202          if ( t2 != pret2+1 ){          fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
5203            //          valid = 0;
5204            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 ){
5205            //          // EMILIANO
5206            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 ){
5207              //              //    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();    
5208              //              //
5209              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);
5210              //              //
5211              valid = 0;              MISSING_pkt1 = true;
5212              this->HandleTRK_CALIB(false,true);              MISSING_pkt2 = false;
5213                this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5214                ncalib++;
5215                if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;
5216              //              //
           };  
           //  
5217          };          };
5218          //          //
5219        } 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;  
5220          //          //
5221          // handle missing calib2      };
5222    
5223    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    
5224    // -----------------------------------------------------------------
5225    // in case of corruption, check if the calibration can be recovered
5226    // from another chewbacca file
5227    // -----------------------------------------------------------------
5228    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    
5229    
5230    //    cout <<" TRY TO RECOVER ?? "<<try_to_recover<<endl;
5231        
5232        if(chewbacca&&try_to_recover){
5233    
5234    
5235            if ( IsDebug() ) cout << endl << ">>>> TRY TO RECOVER TRACKER CALIBRATIONS <<<<"<<endl;
5236    
5237            TSQLResult *result = 0;
5238            TSQLRow    *row    = 0;
5239          //          //
5240          obt2 = 0;          stringstream oss;
5241          pkt2 = 0;          oss.str("");
         valid = 0;  
         this->HandleTRK_CALIB(true,false);        
5242          //          //
5243        };  
5244        //          ////////////////////////////////////////////////////////////////////////
5245      } else {          // retrieve the name of the current file:
5246        //          ////////////////////////////////////////////////////////////////////////        
5247        if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u OUTSIDE the considered time interval \n",fromtime,obt1,pkt1);          oss.str("");
5248        //      if ( PEDANTIC ) throw -79;          oss << "SELECT NAME FROM GL_ROOT where ID=" << GetID_ROOT() <<";";
5249        //          if ( IsDebug() ) cout << oss.str().c_str() << endl;
5250      };  
5251      //              result = conn->Query(oss.str().c_str());
5252    };          if ( !result ) throw -4;;
5253    //          row = result->Next();
5254    // we have one more calib pkt2 !          TString thisfilename = (TString)row->GetField(0);
5255    //          if ( IsDebug() ) cout << "Current file ==> "<<thisfilename<<endl;      
5256    t2++;  
5257    while ( t2 < nevents2 ){          ////////////////////////////////////////////////////////////////////////        
5258      //          // read all the calibrations inserted
5259      // handle missing calib1          ////////////////////////////////////////////////////////////////////////        
5260      //          oss.str("");
5261      if ( IsDebug() ) printf(" t2 is %u nevents2 is %u \n",t2,nevents2);          oss << " SELECT ";
5262      obt1 = 0;          oss << " ID,FROM_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,TO_TIME";
5263      pkt1 = 0;          oss << " FROM GL_TRK_CALIB ";
5264      //          oss << " ORDER BY ID DESC LIMIT "<<ncalib<<"; ";        
5265      tr2->GetEntry(t2);          if ( IsDebug() ) cout << oss.str().c_str() << endl;
5266      ph2 = eh2->GetPscuHeader();  
5267      obt2 = ph2->GetOrbitalTime();            result = conn->Query(oss.str().c_str());
5268      pkt2 = ph2->GetCounter();            if ( !result ) throw -4;;
5269      //          if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5270      fromtime = this->GetAbsTime(ph2->GetOrbitalTime());          
5271      valid = 0;          // -----------------------------------
5272  //  if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){          // loop over calibrations ...
5273   // EMILIANO          // -----------------------------------
5274      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;
5275        //    if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){          do {
5276        //              row = result->Next();
5277        if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);              if(!row)break;
5278        //              
5279        this->HandleTRK_CALIB(false,true);              UInt_t id       = (UInt_t)atoll(row->GetField(0));
5280        //              UInt_t fromtime = (UInt_t)atoll(row->GetField(1));
5281      };              UInt_t obt1     = (UInt_t)atoll(row->GetField(2));
5282      //              UInt_t pkt1     = (UInt_t)atoll(row->GetField(3));
5283      t2++;              UInt_t obt2     = (UInt_t)atoll(row->GetField(4));
5284      //              UInt_t pkt2     = (UInt_t)atoll(row->GetField(5));
5285    };              UInt_t boot     = (UInt_t)atoll(row->GetField(6));
5286                UInt_t valid    = (UInt_t)atoll(row->GetField(7));
5287                bool MISSING_pkt1 = (row->GetFieldLength(8)==0);
5288                bool MISSING_pkt2 = (row->GetFieldLength(9)==0);
5289                UInt_t totime   = (UInt_t)atoll(row->GetField(10));
5290    
5291                // -------------------------------------
5292                // ...check if the entry is corrupted...
5293                // -------------------------------------
5294                cout <<"*** "<< MISSING_pkt1 << MISSING_pkt2 << valid <<endl;
5295                bool CORRUPTED = (MISSING_pkt1||MISSING_pkt2||!valid);
5296    
5297                if ( IsDebug() ) cout << "("<<nn<<")  ID = "<<id<<" from GL_TRK_CALIB  ==>  corrupted ? "<<CORRUPTED<<endl;
5298    
5299    //          if( !CORRUPTED  )continue; // nothing to do
5300    
5301                /////////////////////////////////////////////////////////  
5302                // if it is corrupted, ...look for ather chewbacca files
5303                // containing the same calibrations ...
5304                /////////////////////////////////////////////////////////
5305    
5306                bool this_MISSING_pkt1 = false;
5307                bool this_MISSING_pkt2 = false;
5308                int  this_t1=0;
5309                int  this_t2=0;;
5310                UInt_t this_valid = 0;
5311    
5312                TString path       = "";
5313                TString name       = "";
5314                TString raw        = "";
5315                UInt_t obt0        = 0;
5316                UInt_t timesync    = 0;
5317                UInt_t boot_number = 0;
5318                bool   FOUND       = false;
5319    
5320                if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;
5321    
5322    //          for(int itable=0; itable<2; itable++){
5323                for(int itable=0; itable<1; itable++){
5324    
5325                    // ------------------------------------------------------
5326                    // loop over both ROOT_TABLE and ROOT_TABLE_BAD
5327                    // ------------------------------------------------------
5328    
5329                    TString table = "ROOT_TABLE";
5330                    if(itable==1)table = "ROOT_TABLE_BAD";
5331    
5332                    oss.str("");
5333                    oss << " SELECT ";
5334                    oss << " FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,BOOT_NUMBER,INPUT_NAME ";
5335                    oss << " FROM "<<table;
5336                    oss << " WHERE 1 " << endl;
5337                    oss << " AND FILE_NAME != \""<< thisfilename<<"\"";
5338                    if( !MISSING_pkt1 ){
5339                        oss << " AND ";
5340                        oss << " PKT_NUMBER_INIT < "<<pkt1;
5341                        oss << " AND ";
5342                        oss << " PKT_NUMBER_FINAL > "<<pkt1;
5343                        oss << " AND ";
5344                        oss << " PKT_OBT_INIT < "<<obt1;
5345                        oss << " AND ";
5346                        oss << " PKT_OBT_FINAL > "<<obt1;
5347                    }else{
5348                        if(pkt2>1) pkt1 = pkt2-1;//serve dopo                  
5349                    }
5350                    if( !MISSING_pkt2 ){
5351                        oss << " AND ";
5352                        oss << " PKT_NUMBER_INIT < "<<pkt2;
5353                        oss << " AND ";
5354                        oss << " PKT_NUMBER_FINAL > "<<pkt2;
5355                        oss << " AND ";
5356                        oss << " PKT_OBT_INIT < "<<obt2;
5357                        oss << " AND ";
5358                        oss << " PKT_OBT_FINAL > "<<obt2;
5359                    }else{
5360                        if(pkt1>0) pkt2 = pkt1+1;//serve dopo
5361                    }
5362                    if( boot> 0 ){
5363                        oss << " AND ";
5364                        oss << " BOOT_NUMBER = "<<boot;
5365                    }else{
5366                    }
5367                    oss << " ORDER BY BAD_PKT_CALREAD ASC; ";
5368                
5369                    TSQLResult *result2 = 0;
5370                    TSQLRow    *row2    = 0;
5371    
5372                    if ( IsDebug() ) cout << oss.str().c_str() << endl;        
5373                    result2 = conn->Query(oss.str().c_str());
5374                    if ( !result2 ) throw -4;;
5375                    if ( IsDebug() ) cout <<"Rows: "<<result2->GetRowCount()<<endl;
5376    
5377                    // ------------------------------------------------------
5378                    // loop over files containing repetition (if any)
5379                    // ------------------------------------------------------
5380                    do {
5381                        row2 = result2->Next();
5382                        if(!row2)break;
5383                    
5384                        // ------------------------------------------------------
5385                        // ... a repetition is found ...
5386                        // ------------------------------------------------------
5387                        path       = (TString)row2->GetField(0);
5388                        name       = (TString)row2->GetField(1);
5389                        raw        = (TString)row2->GetField(5);
5390                        obt0        = (UInt_t)atoll(row2->GetField(2));
5391                        timesync    = (UInt_t)atoll(row2->GetField(3));
5392                        boot_number = (UInt_t)atoll(row2->GetField(4));
5393                    
5394                        if ( IsDebug() ) cout << "- - - - - - - - - - -" <<endl;
5395    //                  cout << path <<endl;
5396    //                  cout << "File    : " <<name <<endl;
5397    //                  cout << obt0 <<endl;
5398    //                  cout << timesync <<endl;
5399    //                  cout << "boot n. : "<<boot_number <<endl;
5400    //                  cout << raw <<endl;
5401    
5402                        // ------------------------------------------------------
5403                        // ... retrieve the calibration packets.
5404                        // ------------------------------------------------------
5405                        if ( IsDebug() ) printf(" file is %s/%s \n",((TString)gSystem->ExpandPathName(path.Data())).Data(),name.Data());
5406                        TFile *file = new TFile(((TString)gSystem->ExpandPathName(path.Data()))+"/"+name); // EM, path could be symbolic and we must expand it
5407                        if(!file)throw -100;
5408                        if(file->IsZombie())throw -100;
5409                        //
5410                        tr1 = (TTree*)file->Get("CalibTrk1");
5411                        if ( !tr1 || tr1->IsZombie() ) throw -22;
5412                        tr2 = (TTree*)file->Get("CalibTrk2");
5413                        if ( !tr2 || tr2->IsZombie() ) throw -23;
5414                        //
5415                        tr1->SetBranchAddress("CalibTrk1", &caltrk1);
5416                        tr1->SetBranchAddress("Header", &eh1);
5417                        nevents1 = tr1->GetEntries();
5418                        tr2->SetBranchAddress("CalibTrk2", &caltrk2);
5419                        tr2->SetBranchAddress("Header", &eh2);
5420                        nevents2 = tr2->GetEntries();
5421                        for(this_t1=0; this_t1<nevents1; this_t1++){
5422                            tr1->GetEntry(this_t1);
5423                            if(
5424                                (UInt_t)eh1->GetPscuHeader()->GetCounter() == pkt1 &&
5425                                true) break;
5426                            this_MISSING_pkt1 = true;
5427                        }
5428                        for(this_t2=0; this_t2<nevents2; this_t2++){
5429                            tr2->GetEntry(this_t2);
5430                            if(
5431                                (UInt_t)eh2->GetPscuHeader()->GetCounter() == pkt2 &&
5432                                true) break;
5433                            this_MISSING_pkt2 = true;
5434                        }
5435                        this_valid =
5436                            ValidateTrkCalib( caltrk1, eh1, file )
5437                            *
5438                            ValidateTrkCalib( caltrk2, eh2, file );
5439                                
5440                        // ---------------------------------------------------------------------
5441                        // accept the calibration if it is better than the previous:
5442                        //
5443                        // - if the new calibration is perfect (both valid packets)
5444                        // - if the new calibration has both the packets and the previous does not
5445                        // ---------------------------------------------------------------------
5446                        if(
5447                            ( !this_MISSING_pkt1&&!this_MISSING_pkt2&&this_valid )||
5448                            ( (MISSING_pkt1||MISSING_pkt2) && (!this_MISSING_pkt1&&!this_MISSING_pkt2) )||
5449                            false)FOUND=true;      
5450                
5451                        if(file)file->Close();
5452    
5453                        if(FOUND)break;
5454    
5455                    }while(1);//endl loop over root table entries
5456    
5457                    if(FOUND)break;
5458    
5459                }//end loop over tables
5460    
5461                if(FOUND){
5462    
5463                  if ( IsDebug() ) cout << " >>> REPETITION FOUND :-) <<<" <<endl;
5464    
5465                  ////////////////////////////////////////////
5466                  // insert a new entry in GL_TRK_CALIB and
5467                  // modify the time-tag of the previous one
5468                  ////////////////////////////////////////////
5469    
5470                  // ---------------------------------------------------------------------
5471                  // step 1: insert a new raw file in GL_RAW
5472                  // ---------------------------------------------------------------------
5473                  //
5474                  // check if the raw file already exist
5475                  //
5476                  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
5477                  //                oss.str("");
5478                  //                oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";";
5479                  //                if ( IsDebug() ) cout << oss.str().c_str() << endl;
5480                    
5481                  //                result = conn->Query(oss.str().c_str());
5482                  //                if ( !result ) throw -4;;
5483                  //                if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5484                  //                if( result->GetRowCount() == 0){
5485                  //                    if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl;
5486                  //                    // - - - - - - - - - - -
5487                  //                    // insert new raw file
5488                  //                    // - - - - - - - - - - -
5489                  //                    GL_RAW glraw = GL_RAW();    
5490                  //                    glraw.PATH        = gSystem->DirName(raw.Data());
5491                  //                    glraw.NAME        = gSystem->BaseName(raw.Data());
5492                  //                    glraw.BOOT_NUMBER = boot_number;
5493                  //                    //
5494                  //                    insertPamelaRawFile( &glraw );
5495                  //                    //              
5496                  //                    id_raw = glraw.ID;              
5497                  //                }else{
5498                  //                    row = result->Next();
5499                  //                    id_raw = (UInt_t)atoll(row->GetField(0));
5500                  //                }
5501                  //                if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl;
5502    
5503                  // ---------------------------------------------------------------------
5504                  // step 1(bis): retrieve the timesync id associated to the file
5505                  // (NB, uso lo stesso associato al file iniziale)
5506                  // ---------------------------------------------------------------------
5507                  UInt_t idtimesync = 0;
5508                  oss.str("");
5509                  oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";
5510                  if ( debug ) printf(" %s \n",oss.str().c_str());
5511                  result = conn->Query(oss.str().c_str());
5512                  if ( !result ) throw -3;
5513                    row = result->Next();
5514                    if ( !row ) throw -3;
5515                    idtimesync = (UInt_t)atoll(row->GetField(0));
5516                    if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl;
5517                    
5518                    delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5519                    // ---------------------------------------------------------------------
5520                    // step 2: insert a new root file in GL_ROOT
5521                    // ---------------------------------------------------------------------
5522                    //
5523                    // check if the root file already exist
5524                    //
5525                    UInt_t id_root = 0;
5526                    oss.str("");
5527                    oss << "SELECT ID FROM GL_ROOT where NAME=\"" << gSystem->BaseName(name.Data()) <<"\";";
5528                    if ( IsDebug() ) cout << oss.str().c_str() << endl;
5529                    
5530                    result = conn->Query(oss.str().c_str());
5531                    if ( !result ) throw -4;;
5532                    if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5533                    if( result->GetRowCount() == 0){
5534                        if ( IsDebug() ) cout << " << Insert new ROOT file >> "<<endl;
5535                        // - - - - - - - - - - -
5536                        // insert new root file
5537                        // - - - - - - - - - - -
5538                        GL_ROOT glroot = GL_ROOT();            
5539                        glroot.ID_RAW      = id_raw;
5540                        glroot.ID_TIMESYNC = idtimesync;
5541                        //
5542                        // 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,
5543                        //             NOT STATIC NOT KEEPENV = $PAM_L0 must be used in the DB
5544                        //
5545                        if ( STATIC ){
5546                          glroot.PATH        = (TString)gSystem->ExpandPathName(path);
5547                        } else {
5548                          if ( KEEPENV ){
5549                            glroot.PATH      = path;
5550                          } else {
5551                            glroot.PATH      = "$PAM_L0";
5552                          };
5553                        };
5554                        //              glroot.PATH        = path;
5555                        glroot.NAME        = name;
5556                        //
5557                        insertPamelaRootFile( &glroot );
5558                        //              
5559                        id_root = glroot.ID;                    
5560                    }else{
5561                        row = result->Next();
5562                        if(row)id_root = (UInt_t)atoll(row->GetField(0));
5563                    }
5564                    if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl;
5565    
5566                    delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5567                    // ---------------------------------------------------------------------
5568                    // step 3: modify time-tag of corrupted GL_TRK_CALIB entry
5569                    // ---------------------------------------------------------------------
5570                    if ( IsDebug() ) cout << " << Modify time-tag of calibration ID="<<id<<" >> "<<endl;
5571                    oss.str("");
5572                    oss << " UPDATE GL_TRK_CALIB SET "  
5573                        << " TO_TIME=0 , FROM_TIME=0  WHERE "
5574                        << " ID = "<< id  << ";";
5575                    if ( IsDebug() ) cout << oss.str().c_str() << endl;
5576                    result = conn->Query(oss.str().c_str());
5577                    if ( !result ) throw -4;;
5578    
5579                    delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5580    
5581                    // ---------------------------------------------------------------------
5582                    // step 4: insert the new calibration:
5583                    // ---------------------------------------------------------------------
5584                    if ( IsDebug() ) cout << " << Insert new TRK calibration >> "<<endl;
5585                    //
5586                    GL_TRK_CALIB glcal = GL_TRK_CALIB();        
5587                    //
5588                    glcal.ID_ROOT_L0        = id_root;
5589                    glcal.EV_ROOT_CALIBTRK1 = this_t1;
5590                    glcal.EV_ROOT_CALIBTRK2 = this_t2;
5591                    glcal.FROM_TIME         = fromtime;
5592                    glcal.TO_TIME           = totime;
5593                    glcal.OBT1              = obt1;
5594                    glcal.OBT2              = obt2;
5595                    glcal.PKT1              = pkt1;
5596                    glcal.PKT2              = pkt1;
5597                    glcal.BOOT_NUMBER       = GetBOOTnumber();
5598                    glcal.VALIDATION        = this_valid;
5599                    //
5600                    HandleTRK_CALIB(&glcal);
5601                    if ( IsDebug() ) cout << "ID = "<<glcal.ID<<endl;
5602                    //
5603                    
5604                }
5605                if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;
5606    
5607            }while(1);//end loop over calibrations
5608            
5609            
5610            if( result )delete result;
5611            if( row    )delete row;
5612            
5613            
5614    
5615            
5616    
5617    
5618        }
5619    
5620    
5621    //      // ------------------------------
5622    //      // try to recover the calibration
5623    //      // ------------------------------
5624    //      cout << "TRY TO RECOVER TRACKER CALIBRATION"<<endl;
5625    //      //
5626    //      ULong64_t time  = 0; //absolute time
5627    //      string    path[100]; //mettere un limite massimo
5628    //      int       nrows = 0;
5629    //      UInt_t    pkt   = 0;
5630    //      UInt_t    obt   = 0;
5631    //      char     *type  = "";
5632    //      EventHeader *eh   = new EventHeader();
5633    //      CalibTrk1Event *c = new CalibTrk1Event();
5634            
5635    //       //
5636    //       if(which_is_not_valid==1 || which_is_not_valid==3){
5637    //        //
5638    //        cout << "PKT1 --> missing or corrupted "<<endl;
5639    //        type = "CalibTrk1";
5640    //        pkt  = pkt1;
5641    //        obt  = obt1;
5642    //        time = this->GetAbsTime(obt1);
5643    //        if( pkt1 == 0 ){//missing
5644    //            time = this->GetAbsTime(obt2);
5645    //            pkt  = pkt2-1;          
5646    //        }
5647    //        //
5648    //       }else if (which_is_not_valid==2 || which_is_not_valid==3){
5649    //        //
5650    //        cout << "PKT2--> missing or corrupted "<<endl;
5651    //        type = "CalibTrk2 ";
5652    //        pkt  = pkt2;
5653    //        obt  = obt2;
5654    //        time = this->GetAbsTime(obt2);
5655    //        if( pkt2 == 0 ){//missing
5656    //            time = this->GetAbsTime(obt1);
5657    //            pkt  = pkt1+1;          
5658    //        }
5659    //        //
5660    //       }else{
5661    //        cout << "this should not happen!!! "<<endl;
5662    //        trow -666;
5663    //       }
5664          
5665    //       nrows = Query_ROOT_TABLE(time,conn,path);// get the list of file which might contain the packet
5666          
5667    
5668    //       for(int r=0; r<nrows; r++){ //loop over rows
5669    //        if(path)cout << r << " >>>> "<<(path+r)->c_str() << endl;
5670    //        /// verifica che il file non sia quello gia` aperto
5671    //       }
5672    
5673    //       ////////////////////////////////////////////////////////////////////////
5674    
5675    //       TSQLResult *result = 0;
5676    //       TSQLRow    *row    = 0;
5677    //       //
5678    //       stringstream oss;
5679    //       oss.str("");
5680    //       // ----------------------------------------
5681    //       // read the id of last calibration inserted
5682    //       // ----------------------------------------
5683    //       oss.str("");
5684    //       oss << " SELECT ";
5685    //       oss << " (ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION) ";
5686    //       oss << " ORDER BY ID DESC LIMIT 1; ";
5687          
5688    //       result = conn->Query(oss.str().c_str());
5689    //       row = result->Next();
5690    //       if( !row )throw -666;
5691    
5692    //       if( result )delete result;
5693    //       if( row    )delete row;
5694    
5695    //       UInt_t id = (UInt_t)atoll(row->GetField(0));
5696    
5697    //       // -------------------------------------
5698    //       // ...and modify it with new parameters
5699    //       // -------------------------------------
5700          
5701    
5702    //   }
5703    //    //
5704    return(0);    return(0);
5705  };  };
# Line 5061  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6002  Int_t PamelaDBOperations::CleanGL_RUN_FR
6002                    << " RM_ACQ_SETTING_MODE=" << glrun->GetRM_ACQ_SETTING_MODE()<< " , "                      << " RM_ACQ_SETTING_MODE=" << glrun->GetRM_ACQ_SETTING_MODE()<< " , "  
6003                    << " TRK_CALIB_USED=" << glrun->GetTRK_CALIB_USED()<< " , "                      << " TRK_CALIB_USED=" << glrun->GetTRK_CALIB_USED()<< " , "  
6004                    << " CAL_DSP_MASK=" << glrun->GetCAL_DSP_MASK()<< " , "                      << " CAL_DSP_MASK=" << glrun->GetCAL_DSP_MASK()<< " , "  
6005                    << " LAST_TIMESYNC=" << glrun->GetLAST_TIMESYNC()<< " , "                      << " LAST_TIMESYNC=" << glrun->GetLAST_TIMESYNC()<< " , ";
6006                    << " OBT_TIMESYNC=" << glrun->GetOBT_TIMESYNC()                //
6007                    << " WHERE ID=" << myid[u] << ";";                if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() )
6008                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
6009                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() )
6010                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC() << " , ";
6011                  //              
6012                  oss << " OBT_TIMESYNC=" << glrun->GetOBT_TIMESYNC();
6013                  oss << " WHERE ID=" << myid[u] << ";";
6014                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6015              };              };
6016              //              //
# Line 5074  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6021  Int_t PamelaDBOperations::CleanGL_RUN_FR
6021              for (UInt_t u=1; u <= nid ; u++){              for (UInt_t u=1; u <= nid ; u++){
6022                oss.str("");                oss.str("");
6023                oss << "UPDATE GL_RUN_FRAGMENTS SET "                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6024                    << " RUNHEADER_TIME=" << glrun->GetRUNHEADER_TIME()<< " , "                    << " RUNHEADER_TIME=" << glrun->GetRUNHEADER_TIME()<< " , ";
6025                    << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "                //
6026                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< ";";                if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() )
6027                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
6028                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() )
6029                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6030                  //              
6031                  oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "
6032                      << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT();
6033                  oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6034                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6035              };                  };    
6036            };            };
# Line 5098  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6052  Int_t PamelaDBOperations::CleanGL_RUN_FR
6052                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "
6053                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , "                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , "
6054                    << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< " , "                    << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< " , "
6055                    << " PKT_COUNTER=" << glrun->GetPKT_COUNTER()<< " , "                    << " PKT_COUNTER=" << glrun->GetPKT_COUNTER()<< " , ";
6056                    << " PKT_READY_COUNTER=" << glrun->GetPKT_READY_COUNTER()                //
6057                  if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ){
6058                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6059                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) {
6060                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6061                  //              
6062                  oss << " PKT_READY_COUNTER=" << glrun->GetPKT_READY_COUNTER()
6063                    << " WHERE ID=" << myid[u] << ";";                    << " WHERE ID=" << myid[u] << ";";
6064                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6065              };              };
# Line 5112  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6072  Int_t PamelaDBOperations::CleanGL_RUN_FR
6072                oss.str("");                oss.str("");
6073                oss << "UPDATE GL_RUN_FRAGMENTS SET "                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6074                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "                    << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "
6075                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , "                    << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , ";
6076                    << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< ";";                //
6077                  if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ){
6078                    oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6079                  if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){
6080                    oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6081                  //              
6082                  oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT();
6083                  oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6084                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6085              };                  };    
6086            };            };
# Line 5164  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6131  Int_t PamelaDBOperations::CleanGL_RUN_FR
6131                row2 = result2->Next();                row2 = result2->Next();
6132                //                //
6133                if ( row2 ){                if ( row2 ){
6134                  if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");                  if ( IsDebug() ) printf(" The run already exists in the GL_RUN table! \n");
6135                  if ( PEDANTIC ) throw -83;                  if ( PEDANTIC ) throw -83;
6136                  row2->Close();                  row2->Close();
6137                };                };
# Line 5241  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6208  Int_t PamelaDBOperations::CleanGL_RUN_FR
6208        glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);          glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
6209        //        //
6210        oss.str("");        oss.str("");
6211        oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "        oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER,ID_ROOT_L0 FROM GL_RUN WHERE "
6212            << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("            << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
6213            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
6214            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
# Line 5272  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6239  Int_t PamelaDBOperations::CleanGL_RUN_FR
6239          glrun->SetID_RUN_FRAG(glrun->GetID());          glrun->SetID_RUN_FRAG(glrun->GetID());
6240          glrun->Fill_GL_RUN(conn);            glrun->Fill_GL_RUN(conn);  
6241          //          //
6242  //      oss.str("");          //      oss.str("");
6243  //      oss << " SELECT ID FROM GL_RUN WHERE "          //      oss << " SELECT ID FROM GL_RUN WHERE "
6244  //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "          //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
6245  //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "          //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
6246  //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "          //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
6247  //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "          //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
6248  //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";          //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
6249  //      //          //      //
6250  //      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());
6251  //      result2 = conn->Query(oss.str().c_str());          //      result2 = conn->Query(oss.str().c_str());
6252  //      //          //      //
6253  //      if ( !result2 ) throw -4;          //      if ( !result2 ) throw -4;
6254  //      //          //      //
6255  //      row2 = result2->Next();          //      row2 = result2->Next();
6256  //      //          //      //
6257  //      if ( !row2 ) throw -25;          //      if ( !row2 ) throw -25;
6258  //      //          //      //
6259  //      oss.str("");          //      oss.str("");
6260  //      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);
6261  //      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());
6262  //      result2 = conn->Query(oss.str().c_str());          //      result2 = conn->Query(oss.str().c_str());
6263  //      //          //      //
6264  //      if ( !result2 ) throw -4;          //      if ( !result2 ) throw -4;
6265          //          //
6266          moved++;          moved++;
6267          //          //
6268        } else {        } else {
6269          if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");  
6270          if ( PEDANTIC ) throw -83;          // questa parte mah mah mah... da controllare
6271    
6272            //
6273            // we end up here if chewbacca and we have RH---| small gap |---RT, in this case the two pieces are attached and moved to GL_RUN. We are now processing the
6274            // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number.
6275            //
6276            // 090112 [8RED (-70): RUN ALREADY INSERTED]
6277            //
6278            Bool_t OK = false;
6279            UInt_t IDRL2A = 0;
6280            UInt_t IDRL2B = 0;
6281              if ( result2->GetRowCount() == 2 ){
6282                IDRL2A = (UInt_t)atoll(row2->GetField(0));
6283                UInt_t IDRL0A = (UInt_t)atoll(row2->GetField(4));
6284                row2 = result2->Next();
6285                IDRL2B = (UInt_t)atoll(row2->GetField(0));
6286                UInt_t IDRL0B = (UInt_t)atoll(row2->GetField(4));
6287                if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B);
6288                //      if ( IDRL0A == IDRL0B ){
6289    //            TSQLResult *result2a = 0;
6290    //            TSQLRow    *row2a    = 0;
6291    //            oss.str("");
6292    //            oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;";
6293    //            if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
6294    //            result2a = conn->Query(oss.str().c_str());
6295    //            //
6296    //            if ( !result2a ) throw -4;
6297    //            //
6298    //            row2a = result2a->Next();
6299    //            //
6300    //            if ( row2a ){
6301    //              UInt_t PKA = (UInt_t)atoll(row2a->GetField(0));
6302    //              delete result2a;
6303    //              oss.str("");
6304    //              oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;";
6305    //              if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
6306    //              result2a = conn->Query(oss.str().c_str());
6307    //              //
6308    //              if ( !result2a ) throw -4;
6309    //              //
6310    //              row2a = result2a->Next();
6311    //              //
6312    //              if ( row2a ){
6313    //                UInt_t PKB = (UInt_t)atoll(row2a->GetField(0));
6314    //                //
6315    //                if ( IsDebug() ) printf(" PKT(PKA) + 1 %llu == runheaderpkt %llu && PKB = runtrailer %llu + 1 %llu \n",PKT(PKA)+1LL, PKT(glrun->GetRUNHEADER_PKT()),PKT(PKB), PKT(glrun->GetRUNTRAILER_PKT())+1LL);
6316    //                if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){
6317    //                  if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
6318                        OK = true;
6319                        //            };
6320                        //          };
6321                        //        };                  
6322                        //    };
6323              };      
6324            if ( OK ){
6325              //
6326              // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table
6327              //
6328              // we have to update with runheader/trailer infos our run and we have to change the ROOT_ID_FRAG of the header piece ( 1/2 2/1 must become 1/3 3/2 2/1 )
6329              //
6330              GL_RUN *glA = new GL_RUN();
6331              glA->Query_GL_RUN(IDRL2A,conn);
6332              //
6333              if ( glA->GetACQ_BUILD_INFO() != 0 ){
6334                //
6335                // the first piece contains a good runheader we can update all the other runs with correct infos!
6336                //
6337                oss.str("");
6338                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6339                    << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "
6340                    << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
6341                    << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , "
6342                    << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , "
6343                    << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , "
6344                    << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , "
6345                    << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , "
6346                    << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , "
6347                    << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , "
6348                    << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , "
6349                    << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , "  
6350                    << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , "  
6351                    << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , "  
6352                    << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , "  
6353                    << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , ";
6354                //
6355                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
6356                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
6357                if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
6358                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , ";
6359                //        
6360                oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC();
6361                oss << " WHERE ID=" << glrun->GetID() << ";";
6362                if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str());
6363                conn->Query(oss.str().c_str());    
6364                //
6365              } else {
6366                //
6367                // sig no runheader, let set anyway what is possible...
6368                //
6369                oss.str("");
6370                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6371                    << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , ";
6372                //
6373                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
6374                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
6375                if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
6376                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6377                //        
6378                oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
6379                    << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT();
6380                oss << " WHERE ID=" << glrun->GetID() << ";";
6381                if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str());
6382                conn->Query(oss.str().c_str());    
6383              };
6384              //
6385              // update runheader ROOT_ID_FRAG
6386              //
6387              oss.str("");
6388              oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
6389              if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
6390              conn->Query(oss.str().c_str());
6391              //
6392              // now let's look for runtrailer if any in the last run
6393              //
6394              glA->Query_GL_RUN(IDRL2B,conn);
6395              //
6396              if ( glA->GetPKT_READY_COUNTER() != 0 ){
6397                //
6398                // the first piece contains a good runtrailer we can update all the other runs with correct infos!
6399                //
6400                oss.str("");
6401                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6402                    << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
6403                    << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "
6404                    << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , "
6405                    << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , ";
6406                //
6407                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
6408                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6409                if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) {
6410                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6411                //        
6412                oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER()
6413                    << " WHERE ID=" << glrun->GetID() << ";";
6414                if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str());
6415                conn->Query(oss.str().c_str());    
6416                //
6417              } else {
6418                //
6419                // sig no runtrailer, let set anyway what is possible...
6420                //
6421                oss.str("");
6422                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6423                    << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
6424                    << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , ";
6425                  //
6426                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
6427                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6428                if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){
6429                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6430                //        
6431                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()
6432                    << " WHERE ID=" << glrun->GetID() << ";";
6433                if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str());
6434                conn->Query(oss.str().c_str());    
6435              };
6436              //
6437              UInt_t myi = glrun->GetID();
6438              oss.str("");
6439              oss << " ID= "<< myi;
6440              //
6441              glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
6442              //
6443              // fill the new run in GL_RUN
6444              //
6445              glrun->SetID_RUN_FRAG(IDRL2B);
6446              glrun->Fill_GL_RUN(conn);    
6447              glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
6448              delete glA;
6449              moved++;
6450              //
6451            } else {
6452              //
6453              // is just a repetition
6454              //
6455              if ( IsDebug() ) printf(" The run %u is already present in the GL_RUN table...\n",glrun->GetID());    
6456              //      printf(" CCCCCCCCICCCCCCCCCCCCIOOOOOOOOOO si muove Ciccio! %u \n",glrun->GetID());
6457              //      if ( PEDANTIC && glrun->GetID() != 61563 ) throw -83;
6458              if ( PEDANTIC ) throw -83;
6459            };
6460            //      if ( IsDebug() ) printf(" The run already exists in the GL_RUN table! ...\n");
6461            //      if ( PEDANTIC ) throw -83;
6462        };        };
6463        if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));              if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));      
6464        //        //
6465        //        //
6466        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
6467  //      oss.str("");        //      oss.str("");
6468  //       oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);        //       oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
6469  //       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());
6470  //       result2 = conn->Query(oss.str().c_str());        //       result2 = conn->Query(oss.str().c_str());
6471  //      //        //      //
6472  //      if ( !result2 ) throw -4;        //      if ( !result2 ) throw -4;
6473  //      //        //      //
6474        row = result->Next();        row = result->Next();
6475      };      };
6476    };    };
# Line 5366  Int_t PamelaDBOperations::ValidateRuns(T Line 6524  Int_t PamelaDBOperations::ValidateRuns(T
6524      // 2) get the OBT of the last validated run      // 2) get the OBT of the last validated run
6525      // --------------------------------------------------------------      // --------------------------------------------------------------
6526      oss.str("");      oss.str("");
6527      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
6528        oss << " SELECT * FROM GL_RUN  WHERE VALIDATION>0 AND RUNHEADER_TIME<="<< t_start
6529          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
6530      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());
6531      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
# Line 5457  Int_t PamelaDBOperations::ValidateRuns(T Line 6616  Int_t PamelaDBOperations::ValidateRuns(T
6616    GL_RUN* this_run = new GL_RUN();    GL_RUN* this_run = new GL_RUN();
6617    GL_RUN* next_run = new GL_RUN();    GL_RUN* next_run = new GL_RUN();
6618    Int_t   nseq_max = 1000;    Int_t   nseq_max = 1000;
6619  //  UInt_t* sequence = new UInt_t[100];    //  UInt_t* sequence = new UInt_t[100];
6620    vector<UInt_t> sequence(nseq_max);    vector<UInt_t> sequence(nseq_max);
6621    Int_t   nseq = 0;    Int_t   nseq = 0;
6622    Bool_t CHECK = false;    Bool_t CHECK = false;
# Line 5472  Int_t PamelaDBOperations::ValidateRuns(T Line 6631  Int_t PamelaDBOperations::ValidateRuns(T
6631    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
6632    while(1){    while(1){
6633                        
6634            row = result->Next();      row = result->Next();
6635            if( row == NULL ) break;      if( row == NULL ) break;
6636                        
6637            //------------      //------------
6638            //get run info      //get run info
6639            //------------      //------------
6640            this_run->Set_GL_RUN(row);      this_run->Set_GL_RUN(row);
6641                                        
6642            Bool_t this_BAD = false;      Bool_t this_BAD = false;
6643            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;
6644            else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;      else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;
6645            else{      else{
6646  //                printf("Missing or corrupted header!! \n");        //                  printf("Missing or corrupted header!! \n");
6647                    this_ONLINE = false;        this_ONLINE = false;
6648                    this_BAD = true;        this_BAD = true;
6649            };      };
6650    
6651            //-----------------------------------      //-----------------------------------
6652            //compare with previous(next in time)      //compare with previous(next in time)
6653            //-----------------------------------      //-----------------------------------
6654            CHECK = false;      CHECK = false;
6655            UInt_t interval=0;      UInt_t interval=0;
6656                        
6657            if( nrow != 0){      if( nrow != 0){
6658                    
6659                                        
6660                    t1 = this_run->GetRUNTRAILER_TIME();        t1 = this_run->GetRUNTRAILER_TIME();
6661                    t2 = next_run->GetRUNHEADER_TIME();        t2 = next_run->GetRUNHEADER_TIME();
6662                    interval = (t2-t1);        interval = (t2-t1);
6663                                        
6664                    if(this_ONLINE && next_ONLINE){                               // this: ON-LINE + next: ON-LINE        if(this_ONLINE && next_ONLINE){                           // this: ON-LINE + next: ON-LINE
6665                                                        
6666                            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
6667                                                        
6668                            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
6669                                                        
6670                            if( !CHECK && this_run->VALIDATION ){          if( !CHECK && this_run->VALIDATION ){
6671                                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);            //      for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
6672                                    nseq=0;            for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],this_run->VALIDATION);
6673                            }            nseq=0;
6674            }
6675                                        
6676                    }else if( !this_ONLINE && next_ONLINE) {              // this: DEFAULT + next:ON-LINE        }else if( !this_ONLINE && next_ONLINE) {          // this: DEFAULT + next:ON-LINE
6677                                                        
6678                            CHECK = true;          CHECK = true;
6679    
6680                    }else if( !next_ONLINE ){                                             // this:ANY + next:DEFAULT        }else if( !next_ONLINE ){                                         // this:ANY + next:DEFAULT
6681                                                        
6682                            assignVALIDATION(next_run->ID,true);          assignVALIDATION(next_run->ID,true);
6683                            nseq=0;          nseq=0;
6684                    }        }
6685            }      }
6686    
6687            //----------------------------      //----------------------------
6688            //check run sequence for calib      //check run sequence for calib
6689            //----------------------------      //----------------------------
6690            if( CHECK ){      if( CHECK ){
6691                    // check if calibration exists        // check if calibration exists
6692                    if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);        if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
6693                    Bool_t MISSING = MissingTRK_CALIB(t1,t2);        //      Bool_t MISSING = MissingTRK_CALIB(t1,t2);
6694                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);        UInt_t MISSING = MissingTRK_CALIB(t1,t2);
6695                    nseq=0;        UInt_t val = 0;
6696            };        if ( MISSING == 1 ) val = 0;
6697            //--------------        if ( MISSING == 0 ) val = 1;
6698            //store run info        if ( MISSING == 2 ) val = 2;
6699            //--------------        for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],val);
6700            *next_run   = *this_run;        nseq=0;
6701            next_ONLINE = this_ONLINE;      };
6702            if( !this_BAD ){      //--------------
6703                    if(nseq < nseq_max){      //store run info
6704                            sequence[nseq] = this_run->ID;      //--------------
6705                            nseq++;      *next_run   = *this_run;
6706                    }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);      next_ONLINE = this_ONLINE;
6707            };      if( !this_BAD ){
6708          if(nseq < nseq_max){
6709            sequence[nseq] = this_run->ID;
6710            nseq++;
6711          }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
6712        };
6713                        
6714            if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);      if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);
6715            nrow++;      nrow++;
6716                        
6717    };    };
6718    delete this_run;    delete this_run;
# Line 5561  Int_t PamelaDBOperations::ValidateRuns(T Line 6726  Int_t PamelaDBOperations::ValidateRuns(T
6726   * @param t2 To absolute time   * @param t2 To absolute time
6727   * @return true if there might be a missing calibration   * @return true if there might be a missing calibration
6728   */   */
6729  Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){  //Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
6730    UInt_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
6731                    
6732          GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();    GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
6733                    
6734          // get the closest VALIDATED calibration before the run start (t2)    // get the closest calibration before the run start (t2)
6735          if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);      //>>> missing    //  if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);        //>>> missing
6736      if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(1);       //>>> missing
6737                    
6738          if ( trkcalib->TO_TIME  < t2 ) return(true);                                    //>>> missing    //  if ( trkcalib->TO_TIME  < t2 ) return(true);                                      //>>> missing
6739      if ( trkcalib->TO_TIME  < t2 ) return(1);                                     //>>> missing
6740                    
6741          //==============================================================    //==============================================================
6742          // Check is done first on the basis of time between calibration,    // Check is done first on the basis of time between calibration,
6743          // which should be equal to the time between ascending-nodes.    // which should be equal to the time between ascending-nodes.
6744          //==============================================================    //==============================================================
6745          if ( t2 - trkcalib->FROM_TIME > 5700) {    if ( t2 - trkcalib->FROM_TIME > 5700) {
6746                  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);
6747          //==============================================================      //==============================================================
6748          // there might be a missing calibration, due to:      // there might be a missing calibration, due to:
6749          // - MM full      // - MM full
6750          // - corrupted packets      // - corrupted packets
6751          // - loss of data      // - loss of data
6752          // 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
6753          //==============================================================      //==============================================================
6754                  Bool_t DOWNLOAD = false;      Bool_t DOWNLOAD = false;
6755                  // check if the calib was skipped becouse of download .... DA FARE!!      // check if the calib was skipped becouse of download .... DA FARE!!
6756                  if(DOWNLOAD)return(false);      //    if(DOWNLOAD)return(false);
6757        if(DOWNLOAD)return(0);
6758                                    
6759                  return(true);                                   //>>> missing      //    return(true);                                         //>>> missing
6760        return(1);                                  //>>> missing
6761                                    
6762          };    };
6763                    
6764          //==============================================================    //==============================================================
6765          // 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,
6766          // it is enough to say that there are no missing calibrations    // it is enough to say that there are no missing calibrations
6767          //==============================================================    //==============================================================
6768          // the long time interval bewteen runs might be due to download    // the long time interval bewteen runs might be due to download
6769          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);
6770          return(false);    if ( trkcalib->VALIDATION ) return(0);
6771      if ( IsDebug() )printf("Calibration is not validated... :-/ ==> OK but with VALIDATION=2! \n");
6772      return(2);
6773                    
6774  };  };
6775  /**  /**
# Line 5606  Bool_t PamelaDBOperations::MissingTRK_CA Line 6778  Bool_t PamelaDBOperations::MissingTRK_CA
6778   * @param validation true/false   * @param validation true/false
6779   */   */
6780  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
6781          TSQLResult *result = 0;    return(this->assignVALIDATION(idrun,(UInt_t)validation));
6782          stringstream oss;  }
6783          oss.str("");  
6784          oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, UInt_t validation){
6785          //    TSQLResult *result = 0;
6786  //      if ( IsDebug() )    stringstream oss;
6787  //      printf(" Set VALIDATION = %i for run %i \n",validation,idrun);    oss.str("");
6788          if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());    oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
6789          result = conn->Query(oss.str().c_str());    //
6790          if ( !result ) throw -4;    //    if ( IsDebug() )
6791          return(0);    //    printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
6792      if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
6793      result = conn->Query(oss.str().c_str());
6794      if ( !result ) throw -4;
6795      return(0);
6796  }  }
6797    
6798    
# Line 5802  Int_t PamelaDBOperations::removeFile(TSt Line 6978  Int_t PamelaDBOperations::removeFile(TSt
6978    stringstream myquery;    stringstream myquery;
6979    //      //  
6980    myquery.str("");    myquery.str("");
6981    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() <<"';";
6982    //    //
6983    pResult = conn->Query(myquery.str().c_str());    pResult = conn->Query(myquery.str().c_str());
6984    //    //
# Line 5818  Int_t PamelaDBOperations::removeFile(TSt Line 6994  Int_t PamelaDBOperations::removeFile(TSt
6994    //    //
6995    this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));    this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
6996    this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));    this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
6997      UInt_t idtsy=(UInt_t)atoll(Row->GetField(2));
6998    //    //
6999    this->ValidationOFF();    this->ValidationOFF();
7000    //    //
# Line 5825  Int_t PamelaDBOperations::removeFile(TSt Line 7002  Int_t PamelaDBOperations::removeFile(TSt
7002    //    //
7003    this->RemoveRUNS();    this->RemoveRUNS();
7004    //    //
7005    this->RemoveFILES();    this->RemoveFILES(idtsy);
7006    //    //
7007    this->SetID_ROOT(0);    this->SetID_ROOT(0);
7008    this->SetID_RAW(0);    this->SetID_RAW(0);
# Line 6045  void PamelaDBOperations::RemoveRUNS(){ Line 7222  void PamelaDBOperations::RemoveRUNS(){
7222   * Rearrange calibration tables   * Rearrange calibration tables
7223   *   *
7224   **/   **/
7225  void PamelaDBOperations::RemoveFILES(){  void PamelaDBOperations::RemoveFILES(UInt_t idtsy){
7226    stringstream myquery;    stringstream myquery;
7227    //    //
7228    myquery.str("");    myquery.str("");
# Line 6055  void PamelaDBOperations::RemoveFILES(){ Line 7232  void PamelaDBOperations::RemoveFILES(){
7232    //    //
7233    conn->Query(myquery.str().c_str());    conn->Query(myquery.str().c_str());
7234    //    //
7235      myquery.str("");
7236      myquery << " DELETE FROM GL_ROOT WHERE ID=" <<this->GetID_ROOT() <<";";
7237      //
7238      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
7239      //
7240      conn->Query(myquery.str().c_str());  //
7241      //
7242      if ( !chewbacca ){
7243        myquery.str("");
7244        myquery << " DELETE FROM GL_TIMESYNC WHERE ID=" << idtsy <<";";
7245        //
7246        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
7247        //
7248        conn->Query(myquery.str().c_str());
7249      };
7250      //
7251  };  };
7252    
7253  /**  /**
# Line 6259  void PamelaDBOperations::RemoveCALIBS(){ Line 7452  void PamelaDBOperations::RemoveCALIBS(){
7452   * Rearrange calibration tables   * Rearrange calibration tables
7453   *   *
7454   **/   **/
7455  UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){  UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh , TFile *file){
7456    
7457        if(!caltrk) return 0;
7458    
7459        if ( IsDebug() ) cout << "ValidateTrkCalib:"<<endl;
7460    
7461        UInt_t validate = 1;
7462      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};
7463      UInt_t timeaftercalib=120000; //2000;      UInt_t timeaftercalib=120000; //2000;
7464  //  ----------      TString classname = caltrk->GetName();
7465  //  Check CRCs  
7466  //  ----------  //  ----------------------------------
7467    //  Check CRCs and failed calibrations
7468    //  ----------------------------------
7469      for(Int_t ipkt=0; ipkt<6; ipkt++){      for(Int_t ipkt=0; ipkt<6; ipkt++){
7470          if( caltrk->crc_hcal[ipkt] )return 0; // :-(          if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
7471          for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(              if( caltrk->crc_hcal[ipkt] ){
7472    //              if(IsDebug())cout<<"(CRC Header)";
7473                    validate = 0;
7474                    if(IsDebug())cout <<endl<<" *** CRC *** (header DSPn "<<caltrk->DSPnumber[ipkt]<<")";
7475            
7476                }
7477                for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] ){
7478    //              if(IsDebug())cout<<"(CRC Pkt-"<<ilad<<")";
7479                    if(IsDebug())cout <<endl<<" *** CRC *** (data DSPn "<<caltrk->DSPnumber[ipkt]<<" ladder "<<ilad<<")";
7480                    validate = 0;
7481                }
7482                if( !(caltrk->ncalib_event[ipkt]==0 && caltrk->cal_flag[ipkt]==0) ){
7483                    if(IsDebug())cout <<endl<<" *** FAILURE ***     (data DSPn "<<caltrk->DSPnumber[ipkt]<<")";
7484                    validate = 0;
7485                }
7486            }else{
7487    //          validate=0;
7488                if(IsDebug())cout <<endl<<" *** DSPn *** ("<< caltrk->DSPnumber[ipkt] <<" @pkt "<<ipkt<<")";
7489            }
7490      }      }
7491    
7492  //  -----------------------  //  -----------------------
7493  //  Check missing packets:  //  Check missing packets:
7494  //  -----------------------  //  -----------------------
# Line 6295  UInt_t PamelaDBOperations::ValidateTrkCa Line 7514  UInt_t PamelaDBOperations::ValidateTrkCa
7514  //  Check if it is first or second calibration packet  //  Check if it is first or second calibration packet
7515  //  -------------------------------------------------  //  -------------------------------------------------
7516      UInt_t build=0;      UInt_t build=0;
     TString classname = caltrk->GetName();  
7517      UInt_t base=0;      UInt_t base=0;
7518      UInt_t mask=0;      UInt_t mask=0;
7519      if(classname.Contains("CalibTrk1Event")){      if(classname.Contains("CalibTrk1Event")){
# Line 6306  UInt_t PamelaDBOperations::ValidateTrkCa Line 7524  UInt_t PamelaDBOperations::ValidateTrkCa
7524          base=18;          base=18;
7525          mask=0xFC0000;          mask=0xFC0000;
7526      }      }
7527  //  -------------------------------------------------  //  ----------------------------------------------------
7528  //  Count number of packets and set build variable  //  Count number of valid packets and set build variable
7529  //  -------------------------------------------------  //  ----------------------------------------------------
7530        if(IsDebug())cout <<endl<< " DSP: ";
7531      Int_t  npkts=0;      Int_t  npkts=0;
7532      for(Int_t ipkt=0; ipkt<6; ipkt++){      for(Int_t ipkt=0; ipkt<6; ipkt++){
7533          if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){          if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
7534                if(IsDebug())cout <<" "<<caltrk->DSPnumber[ipkt];
7535              npkts++;              npkts++;
7536              build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );              build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
7537          }  //          cout << caltrk->DSPnumber[ipkt]
7538            };
7539      }      }
7540  //    if( npkts==6 )return 1; // :-)      if(IsDebug())cout << " ==> "<< hex << build << dec;
7541    //  ----------------------------------------------------
7542  //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;  //  If the number of valid packets is 6, ok exit...
7543    //  ----------------------------------------------------
7544  //  -----------------------------------------------      if( npkts==6 ){
7545  //  If missing packets: check the acq configuration          return validate; // exit
7546        }
7547    ////////////////////////////////////////////////////////
7548    //  ...otherwise there might be some missing packets
7549    //
7550    //  In this case check the acq configuration
7551  //  (some DSPs might be excluded from acquisition)  //  (some DSPs might be excluded from acquisition)
7552  //  -----------------------------------------------  ////////////////////////////////////////////////////////
7553    
7554        if(!eh || !file || (file&&file->IsZombie()) ){
7555            if ( IsDebug() )cout << " *** MISSING VIEW *** eh="<<eh<<" file="<<file<<" cannot validate"<<endl;
7556            return (0);
7557        }
7558        
7559  //  -----------------------------------------------  //  -----------------------------------------------
7560  //  retrieve the first run header after calib  //  retrieve the first run header after calib
7561  //  -----------------------------------------------  //  -----------------------------------------------
7562       PacketType *pctp;      
7563       EventCounter *cod;      PacketType *pctp;
7564       cod = eh->GetCounter();      EventCounter *cod;
7565       Int_t irun = cod->Get(pctp->RunHeader);      cod = eh->GetCounter();
7566       TTree *rh=(TTree*)file->Get("RunHeader");      Int_t irun = cod->Get(pctp->RunHeader);
7567       if ( !rh || rh->IsZombie() ) throw -17;      TTree *rh=(TTree*)file->Get("RunHeader");
7568       if( rh->GetEntries() == irun ){      if ( !rh || rh->IsZombie() ) throw -17;
7569            if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;      if( rh->GetEntries() <= irun ){
7570           return 0; // :-(          if ( IsDebug() ) cout << " *** MISSING-PKT *** no runs after calib (1) -- cannot validate :-( "<<endl;
7571       }          return 0; // :-(
7572        }
7573       RunHeaderEvent *run  = 0;      RunHeaderEvent *run  = 0;
7574       EventHeader    *hrun = 0;      EventHeader    *hrun = 0;
7575       rh->SetBranchAddress("RunHeader", &run);      rh->SetBranchAddress("RunHeader", &run);
7576       rh->SetBranchAddress("Header", &hrun);      rh->SetBranchAddress("Header", &hrun);
7577       rh->GetEntry(irun);      rh->GetEntry(irun);    
7578  //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;      if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
7579            if ( IsDebug() ) cout << " *** MISSING-PKT *** no runs after calib (2) -- cannot validate :-( "<<endl;
7580       if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){          return 0; // :-(
7581            if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;      }
7582           return 0; // :-(  
7583       }      UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
7584            if( dtime > timeaftercalib ){
7585       if( !run->RM_ACQ_AFTER_CALIB ){          if ( IsDebug() ) cout << " *** MISSING-PKT *** run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
7586            if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0    -- cannot validate :-( "<<endl;          return 0; // :-(
7587           return 0; // :-(      }
7588       }  
7589        if ( IsDebug() ) cout <<endl<< " ACQ_BUILD_INFO ==> "<<hex<<(run->ACQ_BUILD_INFO & mask)<<dec;
7590       UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());  
7591       if( dtime > timeaftercalib ){      if( (run->ACQ_BUILD_INFO & mask) != build ){
7592           if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;          validate=0; // :-(
7593           return 0; // :-(          cout <<endl<< " *** MISSING-PKT *** packet mismatch: ACQ_BUILD_INFO="<<hex<<(run->ACQ_BUILD_INFO&mask)<<" != "<<build<<dec;
7594       }      };
7595        
7596        return validate;
7597    
      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; // :-)  
7598    
7599  }  }
7600    
# Line 6378  UInt_t PamelaDBOperations::ValidateTrkCa Line 7604  UInt_t PamelaDBOperations::ValidateTrkCa
7604   *   *
7605   **/   **/
7606  UInt_t PamelaDBOperations::Check(){  UInt_t PamelaDBOperations::Check(){
7607          return(this->Check(0,0));    return(this->Check(0,0));
7608  }  }
7609    
7610  UInt_t PamelaDBOperations::Check(UInt_t from, UInt_t to){  UInt_t PamelaDBOperations::Check(UInt_t from, UInt_t to){
7611    //    //
7612          if ( IsDebug() ) printf(" from %u to %u \n",from,to);    if ( IsDebug() ) printf(" from %u to %u \n",from,to);
7613    //    //
7614    UInt_t test = 0;    UInt_t test = 0;
7615    //    //
# Line 6424  UInt_t PamelaDBOperations::Check(UInt_t Line 7650  UInt_t PamelaDBOperations::Check(UInt_t
7650      //      //
7651      if ( from > 0 && nid <= from ) goto ss;      if ( from > 0 && nid <= from ) goto ss;
7652      if ( to > 0 && nid >= to ) goto ss;      if ( to > 0 && nid >= to ) goto ss;
7653  //      //
7654      if ( (UInt_t)atoll(row->GetField(4)) > 1 ){      if ( (UInt_t)atoll(row->GetField(4)) > 1 ){
7655        //        //
7656        //        //
# Line 6455  UInt_t PamelaDBOperations::Check(UInt_t Line 7681  UInt_t PamelaDBOperations::Check(UInt_t
7681              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)));  
7682              row3 = result3->Next();              row3 = result3->Next();
7683            };            };
7684  //      delete result3;            //    delete result3;
7685    
7686          };          };
7687          //          //
# Line 6476  UInt_t PamelaDBOperations::Check(UInt_t Line 7702  UInt_t PamelaDBOperations::Check(UInt_t
7702              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)));
7703              row3 = result3->Next();              row3 = result3->Next();
7704            };            };
7705  //      delete result3;            //    delete result3;
7706          };          };
7707          //          //
7708          oss.str("");          oss.str("");
# Line 6496  UInt_t PamelaDBOperations::Check(UInt_t Line 7722  UInt_t PamelaDBOperations::Check(UInt_t
7722              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)));
7723              row3 = result3->Next();              row3 = result3->Next();
7724            };            };
7725   //     delete result3;            //    delete result3;
7726    
7727          };          };
7728        };        };
# Line 6517  UInt_t PamelaDBOperations::Check(UInt_t Line 7743  UInt_t PamelaDBOperations::Check(UInt_t
7743            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)));
7744            row3 = result3->Next();            row3 = result3->Next();
7745          };          };
7746  //      delete result3;          //      delete result3;
7747        };        };
7748        //        //
7749      };      };
# Line 6543  UInt_t PamelaDBOperations::Check(UInt_t Line 7769  UInt_t PamelaDBOperations::Check(UInt_t
7769        thisf = (TString)row2->GetField(0);        thisf = (TString)row2->GetField(0);
7770        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
7771        test = 1;        test = 1;
7772  //      delete result2;        //        delete result2;
7773      };      };
7774      //      //
7775      if ( (thisrtt < prevrht) && (thisrht != prevrht) ){      if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
# Line 6565  UInt_t PamelaDBOperations::Check(UInt_t Line 7791  UInt_t PamelaDBOperations::Check(UInt_t
7791        thisf = (TString)row2->GetField(0);        thisf = (TString)row2->GetField(0);
7792        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
7793        test = 1;        test = 1;
7794  //      delete result2;        //        delete result2;
7795      };      };
7796      //      //
7797      if ( (thisrht > thisrtt) && (thisrht != prevrht) ){      if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
# Line 6587  UInt_t PamelaDBOperations::Check(UInt_t Line 7813  UInt_t PamelaDBOperations::Check(UInt_t
7813        thisf = (TString)row2->GetField(0);        thisf = (TString)row2->GetField(0);
7814        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());        if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
7815        test = 1;        test = 1;
7816  //      delete result2;        //        delete result2;
7817      };      };
7818  ss:    ss:
7819      //      //
7820      prevrht = thisrht;      prevrht = thisrht;
7821      prevrtt = thisrtt;      prevrtt = thisrtt;
7822      previd = thisid;      previd = thisid;
7823      prevl0id = thisl0id;      prevl0id = thisl0id;
7824      row = result->Next();      row = result->Next();
7825  //    if ( result2 ) delete result2;      //    if ( result2 ) delete result2;
7826  //    if ( result3 ) delete result3;      //    if ( result3 ) delete result3;
7827    };    };
7828    //    //
7829    return(test);    return(test);

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

  ViewVC Help
Powered by ViewVC 1.1.23