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

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

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

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

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

  ViewVC Help
Powered by ViewVC 1.1.23