/[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.6 by mocchiut, Tue Nov 4 10:48:38 2008 UTC revision 1.31 by mocchiut, Wed Dec 30 10:46:35 2009 UTC
# Line 60  string getTleDatetime(cTle*); Line 60  string getTleDatetime(cTle*);
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, Bool_t keepenv){  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 87  PamelaDBOperations::PamelaDBOperations(T Line 88  PamelaDBOperations::PamelaDBOperations(T
88    //    //
89    glrun = new GL_RUN();    glrun = new GL_RUN();
90    //    //
91    if ( !chewbacca ){    //   if ( !chewbacca ){
92      if ( !boot ) SetNOBOOT(false);    //     if ( !boot ) SetNOBOOT(false);
93      SetTsync(tsync,gpamela);    //     SetTsync(tsync,gpamela);
94      SetBOOTnumber(boot,gpamela);    //     SetBOOTnumber(boot,gpamela);
95      SetObt0(obt0);    //     SetObt0(obt0);
96    };    //   };
97    //    //
98    SetTLEPath(tlefilename);    SetTLEPath(tlefilename);
99    //    //
# Line 101  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 124  void PamelaDBOperations::Close(){ Line 126  void PamelaDBOperations::Close(){
126  //  //
127  // SETTERS  // SETTERS
128  //  //
129    void PamelaDBOperations::NotChewbacca(UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t gpamela){
130      if ( !boot ) SetNOBOOT(false);
131      SetTsync(tsync,gpamela);
132      SetBOOTnumber(boot,gpamela);
133      SetObt0(obt0);
134    };
135    
136  void PamelaDBOperations::OpenL0File(TString filerootname){  void PamelaDBOperations::OpenL0File(TString filerootname){
137    if( INSERT_ROOT ){    if( INSERT_ROOT ){
# Line 134  void PamelaDBOperations::OpenL0File(TStr Line 142  void PamelaDBOperations::OpenL0File(TStr
142        stringstream qu;        stringstream qu;
143        TSQLResult *result = 0;        TSQLResult *result = 0;
144        TSQLResult *result2 = 0;        TSQLResult *result2 = 0;
145          TSQLResult *result3 = 0;
146        TSQLRow    *row    = 0;            TSQLRow    *row    = 0;    
147        TSQLRow    *row2    = 0;            TSQLRow    *row2    = 0;    
148          TSQLRow    *row3    = 0;    
149        TString chpath;        TString chpath;
150        TString chfile;        TString chfile;
151        UInt_t ridn = 0;        UInt_t ridn = 0;
152        qu.str("");        qu.str("");
153        qu << "SELECT ROOT_ID_N,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,INSERTED_BY from ROOT_TABLE_MERGING where ID_N=" << chID << ";";        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 << ";";
154        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
155        result = conn->Query(qu.str().c_str());        result = conn->Query(qu.str().c_str());
156        if ( result ){        if ( result ){
# Line 152  void PamelaDBOperations::OpenL0File(TStr Line 162  void PamelaDBOperations::OpenL0File(TStr
162            chobtmin = (UInt_t)atoll(row->GetField(3));            chobtmin = (UInt_t)atoll(row->GetField(3));
163            chobtmax = (UInt_t)atoll(row->GetField(4));            chobtmax = (UInt_t)atoll(row->GetField(4));
164            chiby = (TString)(row->GetField(5));            chiby = (TString)(row->GetField(5));
165              chrtinit = (UInt_t)atoll(row->GetField(6));
166          } else {          } else {
167            throw -84;            throw -84;
168          };          };
# Line 160  void PamelaDBOperations::OpenL0File(TStr Line 171  void PamelaDBOperations::OpenL0File(TStr
171        };        };
172        delete result;        delete result;
173        qu.str("");        qu.str("");
174        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 << ";";
175        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
176        result2 = conn->Query(qu.str().c_str());        result2 = conn->Query(qu.str().c_str());
177        if ( result2 ){        if ( result2 ){
# Line 177  void PamelaDBOperations::OpenL0File(TStr Line 188  void PamelaDBOperations::OpenL0File(TStr
188            chresursts = (UInt_t)atoll(row2->GetField(4));            chresursts = (UInt_t)atoll(row2->GetField(4));
189            chboot = (UInt_t)atoll(row2->GetField(5));            chboot = (UInt_t)atoll(row2->GetField(5));
190            //            //
191              if ( !chboot ){
192                if ( debug ) printf(" mmm... no BOOT number determined by chewbacca, try to find it looking at DB \n");
193                //
194                // new feature: check in ROOT_TABLE (timewise) the previous and the next BOOT number != 0 , if it is the same we know this one!
195                // if it is different roll back to the old behaviour
196                //
197                qu.str("");
198                qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND REAL_TIME_INIT>"<< row2->GetField(10) <<" order by REAL_TIME_INIT asc limit 1;";
199                if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
200                UInt_t chbootA = 0;
201                UInt_t chbootB = 1;
202                result3 = conn->Query(qu.str().c_str());
203                if ( result3 ){
204                  row3 = result3->Next();
205                  if ( row3 ){
206                    chbootA = (UInt_t)atoll(row3->GetField(0));
207                    if ( debug ) printf(" Found boot_number A = %u \n",chbootA);
208                  };
209                };
210                delete result3;
211                qu.str("");
212                qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND REAL_TIME_INIT<"<< row2->GetField(10) <<" order by REAL_TIME_INIT desc limit 1;";
213                if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
214                result3 = conn->Query(qu.str().c_str());
215                if ( result3 ){
216                  row3 = result3->Next();
217                  if ( row3 ){
218                    chbootB = (UInt_t)atoll(row3->GetField(0));
219                    if ( debug ) printf(" Found boot_number B = %u \n",chbootB);
220                  };
221                };
222                if ( chbootA == chbootB ){
223                  chboot = chbootA;
224                  if ( debug ) printf(" Found boot_number! it is %u \n",chboot);
225                } else {        
226                  qu.str("");
227                  //            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;";
228                  //            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]
229                  qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND ABS(LAST_TIME_SYNC_INFO-"<< chlastts <<")<50000 AND ABS(REAL_TIME_INIT-"<< row2->GetField(10) <<")<50000 group by BOOT_NUMBER order by BOOT_NUMBER asc;"; // even more relaxed conditions... 091214 [9REDtest3: error -29]
230                  if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
231                  result3 = conn->Query(qu.str().c_str());
232                  if ( result3 ){
233                    row3 = result3->Next();
234                    if ( row3 && result3->GetRowCount() == 1 ){
235                      chboot = (UInt_t)atoll(row3->GetField(0));
236                      if ( debug ) printf(" Found boot_number = %u \n",chboot);
237                    } else {
238                      if ( debug ) printf(" AGH CANNOT DETERMINE THE BOOT NUMBER... \n");
239                      throw -29;
240                    };
241                  };
242                };
243              };
244              //
245            chpktinit = (UInt_t)atoll(row2->GetField(6));            chpktinit = (UInt_t)atoll(row2->GetField(6));
246            chpktfinal = (UInt_t)atoll(row2->GetField(7));            chpktfinal = (UInt_t)atoll(row2->GetField(7));
247            chobtinit = (UInt_t)atoll(row2->GetField(8));            chobtinit = (UInt_t)atoll(row2->GetField(8));
# Line 189  void PamelaDBOperations::OpenL0File(TStr Line 254  void PamelaDBOperations::OpenL0File(TStr
254          throw -85;          throw -85;
255        };        };
256        filerootname = chpath + chfile;// + ".root";        filerootname = chpath + chfile;// + ".root";
       if ( debug ) printf(" chewbacca: filename is %s \n",filerootname.Data());  
257      };      };
258      this->SetRootName(filerootname);      this->SetRootName(filerootname);
     file = TFile::Open(this->GetRootName().Data());  
   } else {  
     this->SetRootName("");  
259    };    };
260      if ( debug ) printf(" Filename is %s \n",filerootname.Data());
261      file = TFile::Open(this->GetRootName().Data());
262      //  } else {
263      //    this->SetRootName("");
264      //    this->SetRootName(filerootname);
265      //  };
266  }  }
267    
268  //  //
# Line 395  void PamelaDBOperations::SetOrbitNo(UInt Line 462  void PamelaDBOperations::SetOrbitNo(UInt
462      return;      return;
463    };    };
464    //    //
465    TString name = this->GetRootFile();    if ( !chewbacca ){
466    Int_t nlength = name.Length();      TString name = this->GetRootFile();
467    if ( nlength < 5 ) return;      Int_t nlength = name.Length();
468    TString dwo = 0;      if ( nlength < 5 ){
469    for (Int_t i = 0; i<5; i++){        if ( IsDebug() ) printf(" Agh problems determining the orbit number! name = %s \n",name.Data());
470      dwo.Append(name[i],1);        return;
471    };      };
472    if ( dwo.IsDigit() ){      TString dwo = "";
473      dworbit = (UInt_t)dwo.Atoi();      for (Int_t i = 0; i<5; i++){
   } else {  
     dwo="";  
     for (Int_t i = 8; i<13; i++){  
474        dwo.Append(name[i],1);        dwo.Append(name[i],1);
475      };          };
476      if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();      if ( dwo.IsDigit() ){
477          dworbit = (UInt_t)dwo.Atoi();
478        } else {
479          dwo="";
480          for (Int_t i = 8; i<13; i++){
481            dwo.Append(name[i],1);
482          };    
483          if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
484        };
485        if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
486    };    };
   if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());  
487    return;    return;
488  };  };
489    
# Line 660  Int_t PamelaDBOperations::SetUpperLimits Line 732  Int_t PamelaDBOperations::SetUpperLimits
732      UInt_t tjump = 50000;      UInt_t tjump = 50000;
733      //UInt_t tjump = 100;      //UInt_t tjump = 100;
734      while ( tjump > 0 ){      while ( tjump > 0 ){
735        pktlast = numeric_limits<UInt_t>::max();        //      pktlast = numeric_limits<UInt_t>::max();
736        while ( pktlast > chpktmax && (Int_t)(nevent-1-it) >= 0 ){        pktlast = chpktmax + 1;
737          while ( PKT(pktlast) > PKT(chpktmax) && (Int_t)(nevent-1-it) >= 0 ){
738          if ( (Int_t)(nevent-1-it) >= 0 ){          if ( (Int_t)(nevent-1-it) >= 0 ){
739            T->GetEntry(nevent-1-it);            T->GetEntry(nevent-1-it);
740            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
# Line 669  Int_t PamelaDBOperations::SetUpperLimits Line 742  Int_t PamelaDBOperations::SetUpperLimits
742          } else {          } else {
743            pktlast = chpktmax + 1;            pktlast = chpktmax + 1;
744          };          };
745          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));
746          it += tjump;          it += tjump;
747        };        };
748        if ( tjump > 1 ) it -= 2*tjump;        if ( tjump > 1 ) it -= 2*tjump;
# Line 693  Int_t PamelaDBOperations::SetUpperLimits Line 766  Int_t PamelaDBOperations::SetUpperLimits
766      tjump = 50000;      tjump = 50000;
767      //tjump = 100;      //tjump = 100;
768      while ( tjump > 0 ){      while ( tjump > 0 ){
769        pktlast = 0;        //      pktlast = 0;
770        while ( pktlast < chpktmin && it < (Int_t)nevent ){        pktlast = chpktmin - 1;
771          if ( debug ) printf("LLlook for down %i %i %llu %llu \n",it,pktlast,PKT(pktlast),PKT(chpktmin));
772          while ( PKT(pktlast) < PKT(chpktmin) && it < (Int_t)nevent ){
773          if ( it < (Int_t)nevent ){          if ( it < (Int_t)nevent ){
774            T->GetEntry(it);            T->GetEntry(it);
775            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
# Line 788  Int_t PamelaDBOperations::SetUpperLimits Line 863  Int_t PamelaDBOperations::SetUpperLimits
863      ph = eh->GetPscuHeader();      ph = eh->GetPscuHeader();
864      pktlast = ph->GetCounter();      pktlast = ph->GetCounter();
865      nevent = upperentry - chminentry;      nevent = upperentry - chminentry;
866        if ( ((Int_t)upperentry - (Int_t)chminentry) < 0 ) nevent = 0; // nevent is UInt_t! 090112 [8RED: error -88]
867        if ( IsDebug() ) printf(" Setting nevent to %u - upperentry %i chminentry %i \n",nevent,upperentry,chminentry);
868      //      //
869    } else {    } else {
870      upperpkt = PKT(pktlast);      upperpkt = PKT(pktlast);
# Line 796  Int_t PamelaDBOperations::SetUpperLimits Line 873  Int_t PamelaDBOperations::SetUpperLimits
873    };    };
874    //    //
875    if ( chewbacca && nevent < 1 ) {    if ( chewbacca && nevent < 1 ) {
876        if ( IsDebug() ) printf(" chewbacca and no events in the file \n");
877      pktfirst = chpktmin;      pktfirst = chpktmin;
878      upperpkt = PKT(chpktmax);      upperpkt = PKT(chpktmax);
879      pktlast = chpktmax;      pktlast = chpktmax;
# Line 821  Int_t PamelaDBOperations::SetUpperLimits Line 899  Int_t PamelaDBOperations::SetUpperLimits
899    //  if ( nevent < jump ) jump = int(nevent/10);    //  if ( nevent < jump ) jump = int(nevent/10);
900    //  if ( !jump ) jump = 1;    //  if ( !jump ) jump = 1;
901    //    //
902    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) && nevent > deltapkt ){  
903      if ( ((PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt)) && nevent > deltapkt ){
904      //      //
905      if ( IsDebug() ) printf(" starting jump %i \n",jump);      if ( IsDebug() ) printf(" starting jump %i \n",jump);
906      //    if ( IsDebug() ) printf(" (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) %llu < %llu && %llu > %llu \n     OR \n     (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) %llu > %llu && %llu < %llu \n",PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst),PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst));      //    if ( IsDebug() ) printf(" (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) %llu < %llu && %llu > %llu \n     OR \n     (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) %llu > %llu && %llu < %llu \n",PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst),PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst));
# Line 1349  Long64_t PamelaDBOperations::OBT(UInt_t Line 1428  Long64_t PamelaDBOperations::OBT(UInt_t
1428    //    //
1429    //  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));
1430    //    //
1431    if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){    if ( obt < ((Long64_t)pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){
1432      //    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());
1433      return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());      return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());
1434    };    };
# Line 1468  void PamelaDBOperations::FillClass(Bool_ Line 1547  void PamelaDBOperations::FillClass(Bool_
1547      glrun->SetRUNTRAILER_PKT(lastPkt);      glrun->SetRUNTRAILER_PKT(lastPkt);
1548      //          //    
1549    };    };
1550      //
1551      // ad hoc solution for INF EV_TO [8RED] 090113
1552      //
1553      if ( !firstev && lastev == -1 ){
1554        if ( IsDebug() ) printf(" EE firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1555        firstev = 1;
1556        lastev = 0;
1557      };
1558    glrun->SetEV_FROM((UInt_t)firstev);    glrun->SetEV_FROM((UInt_t)firstev);
1559    glrun->SetEV_TO((UInt_t)lastev);    glrun->SetEV_TO((UInt_t)lastev);
1560    glrun->SetNEVENTS((UInt_t)lastev-(UInt_t)firstev+1);    glrun->SetNEVENTS((UInt_t)lastev-(UInt_t)firstev+1);
# Line 1574  Int_t PamelaDBOperations::insertPamelaGL Line 1661  Int_t PamelaDBOperations::insertPamelaGL
1661      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
1662      if ( !result ) throw -10;      if ( !result ) throw -10;
1663      row = result->Next();      row = result->Next();
1664        if ( !row ) throw -92;
1665      idresof = (UInt_t)atoll(row->GetField(0));      idresof = (UInt_t)atoll(row->GetField(0));
1666      existsts = true;      existsts = true;
1667      goto eout;      goto eout;
# Line 1595  Int_t PamelaDBOperations::insertPamelaGL Line 1683  Int_t PamelaDBOperations::insertPamelaGL
1683      if ( !row ){      if ( !row ){
1684        oss.str("");        oss.str("");
1685        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< "
1686            << dworbit << " order by FROM_ORBIT desc limit 1;";            << dworbit << " AND SPECIAL_FILE='' order by FROM_ORBIT desc limit 1;";
1687        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1688        result = conn->Query(oss.str().c_str());        result = conn->Query(oss.str().c_str());
1689        if ( !result ) throw -10;        if ( !result ) throw -10;
# Line 2236  Int_t PamelaDBOperations::insertPamelaRU Line 2324  Int_t PamelaDBOperations::insertPamelaRU
2324          ptht = cod->Get(pctp->RunHeader) - 1;          ptht = cod->Get(pctp->RunHeader) - 1;
2325          //      evbeft = cod->Get(pctp->Physics);          //      evbeft = cod->Get(pctp->Physics);
2326          tcod = (UInt_t)cod->Get(pctp->Physics);          tcod = (UInt_t)cod->Get(pctp->Physics);
2327          if ( !tcod ) tcod = 1;          if ( !tcod ) tcod = 1;    // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2328          evbeft = TMath::Min(upperentry,(tcod-1));          evbeft = TMath::Min(upperentry,(tcod-1));
2329          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));
2330          //          //
# Line 2275  Int_t PamelaDBOperations::insertPamelaRU Line 2363  Int_t PamelaDBOperations::insertPamelaRU
2363            obth = phh->GetOrbitalTime();            obth = phh->GetOrbitalTime();
2364            cod = ehh->GetCounter();            cod = ehh->GetCounter();
2365            tcod = (UInt_t)cod->Get(pctp->Physics);            tcod = (UInt_t)cod->Get(pctp->Physics);
2366            if ( !tcod ) tcod = 1;            if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2367            evbefh = TMath::Max(chminentry,(tcod-1));            evbefh = TMath::Max(chminentry,(tcod-1));
2368            //            //
2369            if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){            if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){
# Line 2314  Int_t PamelaDBOperations::insertPamelaRU Line 2402  Int_t PamelaDBOperations::insertPamelaRU
2402                //                //
2403                this->HandleRunFragments(true,true,chminentry,(evbefh));                this->HandleRunFragments(true,true,chminentry,(evbefh));
2404                //                //
2405                  // what about pth=ptht-1
2406              };              };
2407            };            };
2408            //            //
# Line 2336  Int_t PamelaDBOperations::insertPamelaRU Line 2425  Int_t PamelaDBOperations::insertPamelaRU
2425                cod = ehh->GetCounter();                cod = ehh->GetCounter();
2426                //            evbeft = cod->Get(pctp->Physics);                //            evbeft = cod->Get(pctp->Physics);
2427                tcod = (UInt_t)cod->Get(pctp->Physics);                tcod = (UInt_t)cod->Get(pctp->Physics);
2428                if ( !tcod ) tcod = 1;                if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2429                evbeft = TMath::Min(upperentry,(tcod-1));                evbeft = TMath::Min(upperentry,(tcod-1));
2430                rh->GetEntry(pth);                rh->GetEntry(pth);
2431                phh = ehh->GetPscuHeader();                phh = ehh->GetPscuHeader();
# Line 2376  Int_t PamelaDBOperations::insertPamelaRU Line 2465  Int_t PamelaDBOperations::insertPamelaRU
2465          //      if ( ptt+1 == rtev){            //      if ( ptt+1 == rtev){  
2466          if ( conptt+1 == nrtev ){            if ( conptt+1 == nrtev ){  
2467            //    if ( conptt+1 == (nrtev+nrtbef )){              //    if ( conptt+1 == (nrtev+nrtbef )){  
2468              if ( IsDebug() ) printf(" >>>>>>>>>>> %i %u %i %u \n",ptht,rhev,nrtev,conptt);
2469            ptht++;            ptht++;
2470            if ( ptht < rhev ){            if ( ptht < rhev ){
2471              rh->GetEntry(ptht);              rh->GetEntry(ptht);
2472                //pth++;
2473                //if ( pth < rhev ){
2474                //rh->GetEntry(pth);
2475              phh = ehh->GetPscuHeader();              phh = ehh->GetPscuHeader();
2476              pkth = phh->GetCounter();              pkth = phh->GetCounter();
2477              obth = phh->GetOrbitalTime();              obth = phh->GetOrbitalTime();
# Line 2402  Int_t PamelaDBOperations::insertPamelaRU Line 2495  Int_t PamelaDBOperations::insertPamelaRU
2495              if ( (UInt_t)evbeft < upperentry-1 && upperentry>0 ){              if ( (UInt_t)evbeft < upperentry-1 && upperentry>0 ){
2496                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);
2497                //                //
2498                if ( (ptt-1) < 0 ) throw -15; // should never arrive here!                //              if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
2499                rt->GetEntry(ptt-1);                if ( (rtev-1) < 0 || ptt < 0 ) throw -15; // should never arrive here!
2500                  //              rt->GetEntry(ptt-1);
2501                  rt->GetEntry(rtev-1);
2502                cod = eht->GetCounter();                cod = eht->GetCounter();
2503                tcod = (UInt_t)cod->Get(pctp->Physics);                tcod = (UInt_t)cod->Get(pctp->Physics)+1;
2504                evbefh = TMath::Max(chminentry,tcod);                evbefh = TMath::Max(chminentry,tcod);
2505                //            evbefh = cod->Get(pctp->Physics);                //            evbefh = cod->Get(pctp->Physics);
2506                rt->GetEntry(ptt);                rt->GetEntry(ptt);
# Line 2636  void PamelaDBOperations::HandleRunFragme Line 2731  void PamelaDBOperations::HandleRunFragme
2731      //      //
2732      // we have now the good first piece of a run, fill the glrun object      // we have now the good first piece of a run, fill the glrun object
2733      //      //
2734      if ( rhfirstev != firstev && !mishead ) mishead = true;      if ( firstev != (lastev+1) ){ // could be a problem sometimes (?)
2735      if ( rtlastev != lastev && !mistrail ) mistrail = true;        if ( rhfirstev != firstev && !mishead ) mishead = true;
2736          if ( rtlastev != lastev && !mistrail ) mistrail = true;
2737        };
2738      //      //
2739        if ( IsDebug() ) printf(" bhere firstev is %i lastev is %i \n",firstev,lastev);
2740      this->FillClass(mishead,mistrail,firstev,lastev);      this->FillClass(mishead,mistrail,firstev,lastev);
2741        if ( IsDebug() ) printf(" chere firstev is %i lastev is %i \n",firstev,lastev);
2742      //      //
2743      if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");      if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
2744      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());      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());
# Line 2681  void PamelaDBOperations::HandleRunFragme Line 2780  void PamelaDBOperations::HandleRunFragme
2780        glrun->Fill_GL_RUN_FRAGMENTS(conn);        glrun->Fill_GL_RUN_FRAGMENTS(conn);
2781        //        //
2782      } else {      } else {
2783        if ( IsDebug() ) printf(" The run is already present in the fragment table \n");        if ( IsDebug() ) printf(" The run is already present in the fragment table, relaxed %u \n",RELAXED);
2784        if ( PEDANTIC ) throw -69;        if ( !RELAXED ){
2785        return;          if ( PEDANTIC ) throw -69;
2786            return;
2787          } else {
2788            glrun->SetID((UInt_t)atoll(row->GetField(0)));
2789            glrun->SetID_RUN_FRAG(0);
2790          };
2791      };      };
2792      //      //
2793      if ( chewbacca && mishead && mistrail ) goto justcheck;      if ( chewbacca && mishead && mistrail ) goto justcheck;
# Line 2734  void PamelaDBOperations::HandleRunFragme Line 2838  void PamelaDBOperations::HandleRunFragme
2838          //          //
2839          found = false; // default value          found = false; // default value
2840          //          //
2841          if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");          if ( IsDebug() ) printf(" A Found a possible candidate, checking if it is the good one... \n");
2842          //          //
2843          // if we have both runheader and runtrailer we can check with pkt_counter:          // if we have both runheader and runtrailer we can check with pkt_counter:
2844          //          //
# Line 2750  void PamelaDBOperations::HandleRunFragme Line 2854  void PamelaDBOperations::HandleRunFragme
2854              if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);              if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2855              //              //
2856              found = true;              found = true;
2857                if ( IsDebug() ) printf(" where firstev is %i lastev is %i \n",firstev,lastev);
2858              //              //
2859            } else {            } else {
2860              //              //
# Line 2815  void PamelaDBOperations::HandleRunFragme Line 2920  void PamelaDBOperations::HandleRunFragme
2920          oss << " ID="<<row->GetField(0)<<";";          oss << " ID="<<row->GetField(0)<<";";
2921          //          //
2922          glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos          glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
2923            if ( IsDebug() ) printf(" there firstev is %i lastev is %i \n",firstev,lastev);
2924          //          //
2925          // merge infos          // merge infos
2926          //          //
# Line 2845  void PamelaDBOperations::HandleRunFragme Line 2951  void PamelaDBOperations::HandleRunFragme
2951          glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());          glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
2952          glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());          glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
2953          //          //
2954            if ( IsDebug() ) printf(" here firstev is %i lastev is %i \n",firstev,lastev);
2955            //
2956          glrun->SetEV_FROM(firstev);          glrun->SetEV_FROM(firstev);
2957          glrun->SetNEVENTS(lastev-firstev+1);          glrun->SetNEVENTS(lastev-firstev+1);
2958          //          //
# Line 2937  void PamelaDBOperations::HandleRunFragme Line 3045  void PamelaDBOperations::HandleRunFragme
3045          //          //
3046          found = false; // default value          found = false; // default value
3047          //          //
3048          if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");          if ( IsDebug() ) printf(" B Found a possible candidate, checking if it is the good one... \n");
3049          //          //
3050          // if we have both runheader and runtrailer we can check with pkt_counter:          // if we have both runheader and runtrailer we can check with pkt_counter:
3051          //          //
# Line 3037  void PamelaDBOperations::HandleRunFragme Line 3145  void PamelaDBOperations::HandleRunFragme
3145            apkt = PKT(ph->GetCounter());            apkt = PKT(ph->GetCounter());
3146            aobt = OBT(ph->GetOrbitalTime());                aobt = OBT(ph->GetOrbitalTime());    
3147            lastev--;            lastev--;
3148            if ( PEDANTIC ) throw -72;            if ( PEDANTIC && !RELAXED) throw -72;
3149          };          };
3150          if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);          if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
3151          //          //
# Line 3105  void PamelaDBOperations::HandleRunFragme Line 3213  void PamelaDBOperations::HandleRunFragme
3213        // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?        // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
3214        //        //
3215        oss.str("");        oss.str("");
3216        oss << " SELECT ID FROM GL_RUN WHERE "        oss << " SELECT ID,ID_ROOT_L0 FROM GL_RUN WHERE "
3217            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3218            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3219            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
# Line 3128  void PamelaDBOperations::HandleRunFragme Line 3236  void PamelaDBOperations::HandleRunFragme
3236        row = result->Next();        row = result->Next();
3237        //        //
3238        if ( row ){        if ( row ){
3239          if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");          //
3240          if ( PEDANTIC ) throw -70;          // 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
3241            // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number.
3242            //
3243            // 090112 [8RED (-70): RUN ALREADY INSERTED]
3244            // 093012 [8RED (-70): RUN ALREADY INSERTED] try to be more general... the run has not been instered yet if the runheader-trailer PKT interval is not covered by already inserted runs, NOTICE: we must look in the GL_RUN_TRASH table!
3245            //
3246            Bool_t OK = false;
3247            UInt_t IDRL2A = 0;
3248            UInt_t IDRL2B = 0;
3249            if ( chewbacca ){
3250              if ( result->GetRowCount() >= 2 ){
3251                //
3252                if ( IsDebug() ) printf(" RH---| gap |---RT case, number of pieces in the GL_RUN table: %i \n",result->GetRowCount());
3253                OK = true;
3254                IDRL2A = (UInt_t)atoll(row->GetField(0));
3255                //
3256                while ( row ){
3257                  //
3258                  TSQLResult *trresult = 0;
3259                  TSQLRow    *trrow    = 0;
3260                  //
3261                  stringstream tross;
3262                  tross.str("");
3263                  //
3264                  tross << " SELECT RUNHEADER_PKT, RUNTRAILER_PKT FROM GL_RUN_TRASH where ID=" << row->GetField(0) << ";";
3265                  if ( IsDebug() ) printf(" check in the gl_run_trash table for pkt intervals: query is \n %s \n",tross.str().c_str());
3266                  trresult = conn->Query(tross.str().c_str());
3267                  if ( !trresult ) throw -4;
3268                  trrow = trresult->Next();
3269                  if ( !trrow || trresult->GetRowCount() != 1 ){
3270                    OK = false;
3271                    if ( IsDebug() ) printf(" OPS! no such run (or multiple runs!) in GL_RUN_TRASH table, something wrong is going on! \n");
3272                    break;
3273                  };
3274                  //
3275                  UInt_t pktH =  (UInt_t)atoll(row->GetField(0));
3276                  UInt_t pktT =  (UInt_t)atoll(row->GetField(1));
3277                  delete trresult;
3278                  if (
3279                      (PKT(pktH) >= PKT(glrun->GetRUNHEADER_PKT()) && PKT(pktH) <= PKT(glrun->GetRUNTRAILER_PKT())) ||
3280                      (PKT(pktT) >= PKT(glrun->GetRUNHEADER_PKT()) && PKT(pktT) <= PKT(glrun->GetRUNTRAILER_PKT())) ||
3281                      (PKT(pktH) <= PKT(glrun->GetRUNTRAILER_PKT()) && PKT(pktT) >= PKT(glrun->GetRUNTRAILER_PKT())) ){            
3282                    OK = false;
3283                    if ( IsDebug() ) printf(" The run is overlapping with already inserted ones!! \n");
3284                    break;          
3285                  };
3286                  //PKT(glrun->GetRUNHEADER_PKT())  PKT(glrun->GetRUNTRAILER_PKT())
3287                  row = result->Next();
3288                };
3289                
3290                //    if ( result->GetRowCount() == 2 ) {
3291                //      IDRL2A = (UInt_t)atoll(row->GetField(0));
3292                //      UInt_t IDRL0A = (UInt_t)atoll(row->GetField(1));
3293                //      row = result->Next();
3294                //      IDRL2B = (UInt_t)atoll(row->GetField(0));
3295                //      UInt_t IDRL0B = (UInt_t)atoll(row->GetField(1));
3296                //      if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B);
3297                //      //      if ( IDRL0A == IDRL0B ){ //091230 why this condition???? RH could be in a file and RT in another one...
3298                //      if ( true ){
3299                //        TSQLResult *result2 = 0;
3300                //        TSQLRow    *row2    = 0;
3301                //        oss.str("");
3302                //        oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;";
3303                //        if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3304                //        result2 = conn->Query(oss.str().c_str());
3305                //        //
3306                //        if ( !result2 ) throw -4;
3307                //        //
3308                //        row2 = result2->Next();
3309                //        //
3310                //        if ( row2 ){
3311                //          UInt_t PKA = (UInt_t)atoll(row2->GetField(0));
3312                //          delete result2;
3313                //          oss.str("");
3314                //          oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;";
3315                //          if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3316                //          result2 = conn->Query(oss.str().c_str());
3317                //          //
3318                //          if ( !result2 ) throw -4;
3319                //          //
3320                //          row2 = result2->Next();
3321                //          //
3322                //          if ( row2 ){
3323                //            UInt_t PKB = (UInt_t)atoll(row2->GetField(0));
3324                //            //
3325                //            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);
3326                //            if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){
3327                //              if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
3328                //              OK = true;
3329                //            };
3330                //          };
3331                //        };                  
3332                //      };
3333                //    };      
3334              };
3335            };
3336            if ( OK ){
3337              //
3338              // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table // we arrive here ONLY with internal pieces of runs, never runheader or runtrailer by definition  
3339              //
3340              // 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 )
3341              //
3342              if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
3343              //
3344              GL_RUN *glA = new GL_RUN();
3345              glA->Query_GL_RUN(IDRL2A,conn);
3346              //
3347              glrun->SetRUNHEADER_TIME(glA->GetRUNHEADER_TIME());
3348              glrun->SetRUNHEADER_OBT(glA->GetRUNHEADER_OBT());
3349              glrun->SetRUNHEADER_PKT(glA->GetRUNHEADER_PKT());
3350              //
3351              glrun->SetRUNTRAILER_TIME(glA->GetRUNTRAILER_TIME());
3352              glrun->SetRUNTRAILER_OBT(glA->GetRUNTRAILER_OBT());
3353              glrun->SetRUNTRAILER_PKT(glA->GetRUNTRAILER_PKT());
3354              //
3355              if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glA->GetPHYSENDRUN_MASK_S3S2S12());
3356              if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glA->GetPHYSENDRUN_MASK_S11CRC());
3357              //          
3358              if ( glA->GetACQ_BUILD_INFO() != 0 ){
3359                //
3360                // the first piece contains a good runheader we can update all the other runs with correct infos!
3361                //
3362                glrun->SetCOMPILATIONTIMESTAMP(glA->GetCOMPILATIONTIMESTAMP());
3363                glrun->SetFAV_WRK_SCHEDULE(glA->GetFAV_WRK_SCHEDULE());
3364                glrun->SetEFF_WRK_SCHEDULE(glA->GetEFF_WRK_SCHEDULE());
3365                glrun->SetPRH_VAR_TRG_MODE_A(glA->GetPRH_VAR_TRG_MODE_A());
3366                glrun->SetPRH_VAR_TRG_MODE_B(glA->GetPRH_VAR_TRG_MODE_B());
3367                glrun->SetACQ_BUILD_INFO(glA->GetACQ_BUILD_INFO());
3368                glrun->SetACQ_VAR_INFO(glA->GetACQ_VAR_INFO());
3369                glrun->SetRM_ACQ_AFTER_CALIB(glA->GetRM_ACQ_AFTER_CALIB());
3370                glrun->SetRM_ACQ_SETTING_MODE(glA->GetRM_ACQ_SETTING_MODE());
3371                glrun->SetTRK_CALIB_USED(glA->GetTRK_CALIB_USED());
3372                glrun->SetCAL_DSP_MASK(glA->GetCAL_DSP_MASK());
3373                glrun->SetLAST_TIMESYNC(glA->GetLAST_TIMESYNC());
3374                glrun->SetOBT_TIMESYNC(glA->GetOBT_TIMESYNC());
3375                //
3376              };
3377              //
3378              if ( glA->GetPKT_READY_COUNTER() != 0 ){
3379                //
3380                // the first piece contains a good runtrailer we can update all the other runs with correct infos!
3381                //
3382                glrun->SetPKT_COUNTER(glA->GetPKT_COUNTER());
3383                glrun->SetPKT_READY_COUNTER(glA->GetPKT_READY_COUNTER());
3384              };
3385              //
3386              // update runheader ROOT_ID_FRAG
3387              //
3388              oss.str("");
3389              oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
3390              if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3391              conn->Query(oss.str().c_str());
3392              //
3393              oss.str("");
3394              oss << "SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG = " << IDRL2A << ";";
3395              if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3396              TSQLResult *tsresult = 0;
3397              TSQLRow    *tsrow    = 0;
3398              tsresult=conn->Query(oss.str().c_str());
3399              if ( !tsresult ) throw -4;
3400              tsrow = tsresult->Next();
3401              if ( !tsrow || tsresult->GetRowCount() != 1 ) throw -4;
3402              IDRL2B = (UInt_t)atoll(tsrow->GetField(0));
3403              glrun->SetID_RUN_FRAG(IDRL2B);
3404              //
3405              // fill the new run in GL_RUN
3406              //
3407              glrun->Fill_GL_RUN(conn);    
3408              //
3409              glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3410              delete glA;
3411              //
3412            } else {
3413    //        //
3414    //        // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table
3415    //        //
3416    //        // 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 )
3417    //        //
3418    //        GL_RUN *glA = new GL_RUN();
3419    //        glA->Query_GL_RUN(IDRL2A,conn);
3420    //        //
3421    //        if ( glA->GetACQ_BUILD_INFO() != 0 ){
3422    //          //
3423    //          // the first piece contains a good runheader we can update all the other runs with correct infos!
3424    //          //
3425    //          oss.str("");
3426    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3427    //              << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "
3428    //              << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3429    //              << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , "
3430    //              << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , "
3431    //              << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , "
3432    //              << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , "
3433    //              << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , "
3434    //              << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , "
3435    //              << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , "
3436    //              << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , "
3437    //              << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , "  
3438    //              << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , "  
3439    //              << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , "  
3440    //              << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , "  
3441    //              << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , ";
3442    //          //
3443    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3444    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
3445    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3446    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , ";
3447    //          //        
3448    //          oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC();
3449    //          oss << " WHERE ID=" << glrun->GetID() << ";";
3450    //          if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str());
3451    //          conn->Query(oss.str().c_str());    
3452    //          //
3453    //        } else {
3454    //          //
3455    //          // sig no runheader, let set anyway what is possible...
3456    //          //
3457    //          oss.str("");
3458    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3459    //              << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , ";
3460    //          //
3461    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3462    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
3463    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3464    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
3465    //          //        
3466    //          oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3467    //              << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT();
3468    //          oss << " WHERE ID=" << glrun->GetID() << ";";
3469    //          if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str());
3470    //          conn->Query(oss.str().c_str());    
3471    //        };
3472    //        //
3473    //        // update runheader ROOT_ID_FRAG
3474    //        //
3475    //        oss.str("");
3476    //        oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
3477    //        if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3478    //        conn->Query(oss.str().c_str());
3479    //        //
3480    //        // now let's look for runtrailer if any in the last run
3481    //        //
3482    //        glA->Query_GL_RUN(IDRL2B,conn);
3483    //        //
3484    //        if ( glA->GetPKT_READY_COUNTER() != 0 ){
3485    //          //
3486    //          // the first piece contains a good runtrailer we can update all the other runs with correct infos!
3487    //          //
3488    //          oss.str("");
3489    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3490    //              << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3491    //              << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "
3492    //              << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , "
3493    //              << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , ";
3494    //          //
3495    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3496    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3497    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) {
3498    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3499    //          //        
3500    //          oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER()
3501    //              << " WHERE ID=" << glrun->GetID() << ";";
3502    //          if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str());
3503    //          conn->Query(oss.str().c_str());    
3504    //          //
3505    //        } else {
3506    //          //
3507    //          // sig no runtrailer, let set anyway what is possible...
3508    //          //
3509    //          oss.str("");
3510    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3511    //              << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3512    //              << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , ";
3513    //            //
3514    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3515    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3516    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){
3517    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3518    //          //        
3519    //          oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()
3520    //              << " WHERE ID=" << glrun->GetID() << ";";
3521    //          if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str());
3522    //          conn->Query(oss.str().c_str());    
3523    //        };
3524    //        //
3525    //        UInt_t myi = glrun->GetID();
3526    //        oss.str("");
3527    //        oss << " ID= "<< myi;
3528    //        //
3529    //        glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
3530    //        //
3531    //        // fill the new run in GL_RUN
3532    //        //
3533    //        glrun->SetID_RUN_FRAG(IDRL2B);
3534    //        glrun->Fill_GL_RUN(conn);    
3535    //        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3536    //        delete glA;
3537    //        //
3538    //      } else {
3539              //
3540              // is just a repetition
3541              //
3542              if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");        
3543              if ( PEDANTIC ) throw -70;
3544            };
3545            //
3546        } else {        } else {
3547          if ( NoFrag() ){          if ( NoFrag() ){
3548            glrun->SetID_RUN_FRAG(glrun->GetID());            glrun->SetID_RUN_FRAG(glrun->GetID());
# Line 3166  void PamelaDBOperations::HandleMissingHo Line 3579  void PamelaDBOperations::HandleMissingHo
3579        glrun->SetID(this->AssignRunID());        glrun->SetID(this->AssignRunID());
3580        glrun->SetID_RUN_FRAG(0);        glrun->SetID_RUN_FRAG(0);
3581        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...
3582      };        };
3583      //      //
3584    };    };
3585    //    //
# Line 3210  Bool_t PamelaDBOperations::IsRunConsiste Line 3623  Bool_t PamelaDBOperations::IsRunConsiste
3623    if ( firstev == lastev+1 || lastev == firstev ) { // no events inside the run!    if ( firstev == lastev+1 || lastev == firstev ) { // no events inside the run!
3624      //if ( firstev <= lastev+1 ) { // no events inside the run!      //if ( firstev <= lastev+1 ) { // no events inside the run!
3625      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
3626        firstev = lastev+1; // this is necessary for files with no Physics entries, should have no influence on other files
3627      // return true is correct      // return true is correct
3628      return(true);      return(true);
3629      //      //
# Line 3239  Bool_t PamelaDBOperations::IsRunConsiste Line 3653  Bool_t PamelaDBOperations::IsRunConsiste
3653        //        //
3654      } else {      } else {
3655        //        //
3656        if ( IsDebug() ) printf(" There are no-phyics packets inside the run!\n");        if ( IsDebug() ) printf(" There are no-physics packets inside the run!\n");
3657        //        //
3658        // HERE WE MUST HANDLE THAT RUNS AND GO BACK        // HERE WE MUST HANDLE THAT RUNS AND GO BACK
3659        //        //
# Line 3249  Bool_t PamelaDBOperations::IsRunConsiste Line 3663  Bool_t PamelaDBOperations::IsRunConsiste
3663        UInt_t check = 0;        UInt_t check = 0;
3664        UInt_t lastevtemp = lastev;        UInt_t lastevtemp = lastev;
3665        UInt_t firstevno = firstev;        UInt_t firstevno = firstev;
3666          UInt_t rhchko=0;
3667          UInt_t rhchk=0;
3668        //        //
3669        for (UInt_t i=firstev; i<=lastev; i++){        for (UInt_t i=firstev; i<=lastev; i++){
3670          //          //
# Line 3257  Bool_t PamelaDBOperations::IsRunConsiste Line 3673  Bool_t PamelaDBOperations::IsRunConsiste
3673          //          //
3674          check = 0;          check = 0;
3675          //          //
3676    
3677            // if we have a runheader set lastev then exit
3678            //
3679          for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){          for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3680            if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));            if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
3681          };          };
3682            // check here if we have a runheader
3683            rhchko = rhchk;  
3684            rhchk = code->Get(GetPacketType("RunHeader"));    
3685          //          //
3686          if ( checkfirst < check || i == lastev ){          if ( checkfirst < check || i == lastev ){
3687            //            //
# Line 3457  Bool_t PamelaDBOperations::IsRunConsiste Line 3879  Bool_t PamelaDBOperations::IsRunConsiste
3879                  //                  //
3880                  found = false; // default value                  found = false; // default value
3881                  //                  //
3882                  if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");                  if ( IsDebug() ) printf(" C Found a possible candidate, checking if it is the good one... \n");
3883                  //                  //
3884                  // if we have both runheader and runtrailer we can check with pkt_counter:                  // if we have both runheader and runtrailer we can check with pkt_counter:
3885                  //                  //
# Line 3676  Bool_t PamelaDBOperations::IsRunConsiste Line 4098  Bool_t PamelaDBOperations::IsRunConsiste
4098            //            //
4099          };          };
4100          //          //
4101            if ( i > firstev ){
4102              if ( rhchko != rhchk ){
4103                if ( IsDebug() ) printf("oh oh... we have a runheader! stop here and handle later the remaining piece\n");
4104                lastev = i;
4105                return(false);
4106              };
4107            };
4108            //
4109          if ( check == checklast && i != lastev ){          if ( check == checklast && i != lastev ){
4110            lastevtemp = i - 1;            lastevtemp = i - 1;
4111            i = lastev - 1;            i = lastev - 1;
# Line 3967  Int_t PamelaDBOperations::insertCALO_CAL Line 4397  Int_t PamelaDBOperations::insertCALO_CAL
4397    UInt_t totime = 0;    UInt_t totime = 0;
4398    UInt_t obt = 0;    UInt_t obt = 0;
4399    UInt_t pkt = 0;    UInt_t pkt = 0;
4400      Float_t totped = 0.;
4401    //    //
4402    tr = (TTree*)file->Get("CalibCalPed");    tr = (TTree*)file->Get("CalibCalPed");
4403    if ( !tr || tr->IsZombie() ) throw -21;    if ( !tr || tr->IsZombie() ) throw -21;
# Line 3984  Int_t PamelaDBOperations::insertCALO_CAL Line 4415  Int_t PamelaDBOperations::insertCALO_CAL
4415        if ( calibCalPed->cstwerr[section] ){        if ( calibCalPed->cstwerr[section] ){
4416          valid = 1;          valid = 1;
4417          if ( calibCalPed->cperror[section] ) valid = 0;          if ( calibCalPed->cperror[section] ) valid = 0;
4418            //
4419            // check pedestal values for one plane, if all zeros calibration is not valid (calorimeter power problems) [8th data reduction bug, fixed on 25/11/2009 by E.M.]
4420            //
4421            totped = 0.;
4422            Int_t ns = 0;
4423            if ( section == 2 ) ns = 3;
4424            if ( section == 3 ) ns = 1;
4425            if ( section == 1 ) ns = 2;
4426            for (UInt_t ss=0; ss<96; ss++){
4427              totped += fabs(calibCalPed->calped[ns][0][ss]);
4428            }
4429            if ( totped < 1. ){
4430              if ( IsDebug() ) printf(" Section %i totped %f -  No calibration data! Calorimeter power problems? \n",section,totped);
4431              valid = 0;
4432            };
4433            //
4434          ph = eh->GetPscuHeader();          ph = eh->GetPscuHeader();
4435          obt = ph->GetOrbitalTime();            obt = ph->GetOrbitalTime();  
4436          pkt = ph->GetCounter();            pkt = ph->GetCounter();  
# Line 4151  Int_t PamelaDBOperations::insertCALOPULS Line 4598  Int_t PamelaDBOperations::insertCALOPULS
4598    tr->SetBranchAddress("Header", &eh);    tr->SetBranchAddress("Header", &eh);
4599    nevents = tr->GetEntries();    nevents = tr->GetEntries();
4600    //    //
4601      Float_t totpul = 0.;
4602      //
4603    if ( nevents > 0 ){    if ( nevents > 0 ){
4604      //      //
4605      for (UInt_t i=0; i < nevents; i++){      for (UInt_t i=0; i < nevents; i++){
# Line 4160  Int_t PamelaDBOperations::insertCALOPULS Line 4609  Int_t PamelaDBOperations::insertCALOPULS
4609          if ( cp1->pstwerr[section] && cp1->unpackError == 0 ){          if ( cp1->pstwerr[section] && cp1->unpackError == 0 ){
4610            valid = 1;            valid = 1;
4611            if ( cp1->pperror[section] ) valid = 0;            if ( cp1->pperror[section] ) valid = 0;
4612              //
4613              // check pulse values for one plane, if all zeros calibration is not valid (calorimeter power problems) [8th data reduction bug, fixed on 25/11/2009 by E.M.]
4614              //
4615              totpul = 0.;
4616              Int_t ns = 0;
4617              if ( section == 2 ) ns = 3;
4618              if ( section == 3 ) ns = 1;
4619              if ( section == 1 ) ns = 2;
4620              for (UInt_t ss=0; ss<96; ss++){
4621                totpul += cp1->calpuls[ns][0][ss];
4622              }
4623              if ( totpul >= 3145632. ){
4624                if ( IsDebug() ) printf(" PULSE1 Section %i totpul %f -  No calibration data! Calorimeter power problems? \n",section,totpul);
4625                valid = 0;
4626              };
4627              //
4628            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
4629            obt = ph->GetOrbitalTime();              obt = ph->GetOrbitalTime();  
4630            pkt = ph->GetCounter();              pkt = ph->GetCounter();  
# Line 4317  Int_t PamelaDBOperations::insertCALOPULS Line 4782  Int_t PamelaDBOperations::insertCALOPULS
4782          if ( cp2->pstwerr[section] && cp2->unpackError == 0 ){          if ( cp2->pstwerr[section] && cp2->unpackError == 0 ){
4783            valid = 1;            valid = 1;
4784            if ( cp2->pperror[section] ) valid = 0;            if ( cp2->pperror[section] ) valid = 0;
4785              //
4786              // check pulse values for one plane, if all zeros calibration is not valid (calorimeter power problems) [8th data reduction bug, fixed on 25/11/2009 by E.M.]
4787              //
4788              totpul = 0.;
4789              Int_t ns = 0;
4790              if ( section == 2 ) ns = 3;
4791              if ( section == 3 ) ns = 1;
4792              if ( section == 1 ) ns = 2;
4793              for (UInt_t ss=0; ss<96; ss++){
4794                totpul += cp2->calpuls[ns][0][ss];
4795              }
4796              if ( totpul >= 3145632. ){
4797                if ( IsDebug() ) printf(" PULSE2 Section %i totpul %f -  No calibration data! Calorimeter power problems? \n",section,totpul);
4798                valid = 0;
4799              };
4800              //
4801            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
4802            obt = ph->GetOrbitalTime();              obt = ph->GetOrbitalTime();  
4803            pkt = ph->GetCounter();              pkt = ph->GetCounter();  
# Line 4682  void PamelaDBOperations::HandleTRK_CALIB Line 5163  void PamelaDBOperations::HandleTRK_CALIB
5163      if ( !result ) throw -4;;      if ( !result ) throw -4;;
5164      row = result->Next();      row = result->Next();
5165      if(row)glcal->ID = (UInt_t)atoll(row->GetField(0));      if(row)glcal->ID = (UInt_t)atoll(row->GetField(0));
5166        //
5167        delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5168      //      //
5169  };  };
5170    
# Line 4892  Int_t PamelaDBOperations::insertTRK_CALI Line 5374  Int_t PamelaDBOperations::insertTRK_CALI
5374                  //                  //
5375              };              };
5376              //              //
5377    
5378                if( !(MISSING_pkt1&MISSING_pkt2) ){
5379                  this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5380                  ncalib++;
5381                  if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;
5382                }
5383    
5384    
5385          } else {          } else {
5386              //            //
5387              if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u OUTSIDE the considered time interval \n",fromtime,obt1,pkt1);              if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u OUTSIDE the considered time interval \n",fromtime,obt1,pkt1);
5388              //      if ( PEDANTIC ) throw -79;              //      if ( PEDANTIC ) throw -79;
5389              //              //
5390          };          };
5391          //              //    
5392    
         if( !(MISSING_pkt1&MISSING_pkt2) ){  
             this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);  
             ncalib++;  
             if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;  
         }  
   
5393      }; //end loop on pkt1      }; //end loop on pkt1
5394    
5395    
# Line 5130  Int_t PamelaDBOperations::insertTRK_CALI Line 5614  Int_t PamelaDBOperations::insertTRK_CALI
5614                      // ------------------------------------------------------                      // ------------------------------------------------------
5615                      // ... retrieve the calibration packets.                      // ... retrieve the calibration packets.
5616                      // ------------------------------------------------------                      // ------------------------------------------------------
5617                      TFile *file = new TFile((TString)gSystem->ExpandPathName(path)+"/"+name); // EM, path could be symbolic and we must expand it                      if ( IsDebug() ) printf(" file is %s/%s \n",((TString)gSystem->ExpandPathName(path.Data())).Data(),name.Data());
5618                        TFile *file = new TFile(((TString)gSystem->ExpandPathName(path.Data()))+"/"+name); // EM, path could be symbolic and we must expand it
5619                      if(!file)throw -100;                      if(!file)throw -100;
5620                      if(file->IsZombie())throw -100;                      if(file->IsZombie())throw -100;
5621                      //                      //
# Line 5187  Int_t PamelaDBOperations::insertTRK_CALI Line 5672  Int_t PamelaDBOperations::insertTRK_CALI
5672    
5673              if(FOUND){              if(FOUND){
5674    
5675              if ( IsDebug() ) cout << " >>> REPETITION FOUND :-) <<<" <<endl;                if ( IsDebug() ) cout << " >>> REPETITION FOUND :-) <<<" <<endl;
   
                 ////////////////////////////////////////////  
                 // insert a new entry in GL_TRK_CALIB and  
                 // modify the time-tag of the previous one  
                 ////////////////////////////////////////////  
5676    
5677                  // ---------------------------------------------------------------------                ////////////////////////////////////////////
5678                  // step 1: insert a new raw file in GL_RAW                // insert a new entry in GL_TRK_CALIB and
5679                  // ---------------------------------------------------------------------                // modify the time-tag of the previous one
5680                  //                ////////////////////////////////////////////
5681                  // check if the raw file already exist  
5682                  //                // ---------------------------------------------------------------------
5683                  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                // step 1: insert a new raw file in GL_RAW
5684  //              oss.str("");                // ---------------------------------------------------------------------
5685  //              oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";";                //
5686  //              if ( IsDebug() ) cout << oss.str().c_str() << endl;                // check if the raw file already exist
5687                  //
5688                  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
5689                  //                oss.str("");
5690                  //                oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";";
5691                  //                if ( IsDebug() ) cout << oss.str().c_str() << endl;
5692                                    
5693  //              result = conn->Query(oss.str().c_str());                //                result = conn->Query(oss.str().c_str());
5694  //              if ( !result ) throw -4;;                //                if ( !result ) throw -4;;
5695  //              if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;                //                if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5696  //              if( result->GetRowCount() == 0){                //                if( result->GetRowCount() == 0){
5697  //                  if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl;                //                    if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl;
5698  //                  // - - - - - - - - - - -                //                    // - - - - - - - - - - -
5699  //                  // insert new raw file                //                    // insert new raw file
5700  //                  // - - - - - - - - - - -                //                    // - - - - - - - - - - -
5701  //                  GL_RAW glraw = GL_RAW();                    //                    GL_RAW glraw = GL_RAW();    
5702  //                  glraw.PATH        = gSystem->DirName(raw.Data());                //                    glraw.PATH        = gSystem->DirName(raw.Data());
5703  //                  glraw.NAME        = gSystem->BaseName(raw.Data());                //                    glraw.NAME        = gSystem->BaseName(raw.Data());
5704  //                  glraw.BOOT_NUMBER = boot_number;                //                    glraw.BOOT_NUMBER = boot_number;
5705  //                  //                //                    //
5706  //                  insertPamelaRawFile( &glraw );                //                    insertPamelaRawFile( &glraw );
5707  //                  //                              //                    //              
5708  //                  id_raw = glraw.ID;                              //                    id_raw = glraw.ID;              
5709  //              }else{                //                }else{
5710  //                  row = result->Next();                //                    row = result->Next();
5711  //                  id_raw = (UInt_t)atoll(row->GetField(0));                //                    id_raw = (UInt_t)atoll(row->GetField(0));
5712  //              }                //                }
5713  //              if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl;                //                if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl;
5714    
5715                  // ---------------------------------------------------------------------                // ---------------------------------------------------------------------
5716                  // step 1(bis): retrieve the timesync id associated to the file                // step 1(bis): retrieve the timesync id associated to the file
5717                  // (NB, uso lo stesso associato al file iniziale)                // (NB, uso lo stesso associato al file iniziale)
5718                  // ---------------------------------------------------------------------                // ---------------------------------------------------------------------
5719                  UInt_t idtimesync = 0;                UInt_t idtimesync = 0;
5720                  oss.str("");                oss.str("");
5721                  oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";                oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";
5722                  if ( debug ) printf(" %s \n",oss.str().c_str());                if ( debug ) printf(" %s \n",oss.str().c_str());
5723                  result = conn->Query(oss.str().c_str());                result = conn->Query(oss.str().c_str());
5724                  if ( !result ) throw -3;                if ( !result ) throw -3;
5725                  row = result->Next();                  row = result->Next();
5726                  if ( !row ) throw -3;                  if ( !row ) throw -3;
5727                  idtimesync = (UInt_t)atoll(row->GetField(0));                  idtimesync = (UInt_t)atoll(row->GetField(0));
5728                  if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl;                  if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl;
5729                                    
5730                    delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5731                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5732                  // step 2: insert a new root file in GL_ROOT                  // step 2: insert a new root file in GL_ROOT
5733                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
# Line 5289  Int_t PamelaDBOperations::insertTRK_CALI Line 5775  Int_t PamelaDBOperations::insertTRK_CALI
5775                  }                  }
5776                  if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl;                  if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl;
5777    
5778                    delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5779                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5780                  // step 3: modify time-tag of corrupted GL_TRK_CALIB entry                  // step 3: modify time-tag of corrupted GL_TRK_CALIB entry
5781                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
# Line 5301  Int_t PamelaDBOperations::insertTRK_CALI Line 5788  Int_t PamelaDBOperations::insertTRK_CALI
5788                  result = conn->Query(oss.str().c_str());                  result = conn->Query(oss.str().c_str());
5789                  if ( !result ) throw -4;;                  if ( !result ) throw -4;;
5790    
5791                    delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5792    
5793                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5794                  // step 4: insert the new calibration:                  // step 4: insert the new calibration:
5795                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
# Line 5752  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6241  Int_t PamelaDBOperations::CleanGL_RUN_FR
6241                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6242                //                              //              
6243                oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "                oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "
6244                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< ";";                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT();
6245                  oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6246                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6247              };                  };    
6248            };            };
# Line 5801  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6291  Int_t PamelaDBOperations::CleanGL_RUN_FR
6291                if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){                if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){
6292                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6293                //                              //              
6294                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< ";";                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT();
6295                  oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6296                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6297              };                  };    
6298            };            };
# Line 5852  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6343  Int_t PamelaDBOperations::CleanGL_RUN_FR
6343                row2 = result2->Next();                row2 = result2->Next();
6344                //                //
6345                if ( row2 ){                if ( row2 ){
6346                  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");
6347                  if ( PEDANTIC ) throw -83;                  if ( PEDANTIC ) throw -83;
6348                  row2->Close();                  row2->Close();
6349                };                };
# Line 5929  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6420  Int_t PamelaDBOperations::CleanGL_RUN_FR
6420        glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);          glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
6421        //        //
6422        oss.str("");        oss.str("");
6423        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 "
6424            << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("            << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
6425            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
6426            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
# Line 5987  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6478  Int_t PamelaDBOperations::CleanGL_RUN_FR
6478          moved++;          moved++;
6479          //          //
6480        } else {        } else {
6481          if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");  
6482          if ( PEDANTIC ) throw -83;          // questa parte mah mah mah... da controllare
6483    
6484            //
6485            // 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
6486            // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number.
6487            //
6488            // 090112 [8RED (-70): RUN ALREADY INSERTED]
6489            //
6490            Bool_t OK = false;
6491            UInt_t IDRL2A = 0;
6492            UInt_t IDRL2B = 0;
6493              if ( result2->GetRowCount() == 2 ){
6494                IDRL2A = (UInt_t)atoll(row2->GetField(0));
6495                UInt_t IDRL0A = (UInt_t)atoll(row2->GetField(4));
6496                row2 = result2->Next();
6497                IDRL2B = (UInt_t)atoll(row2->GetField(0));
6498                UInt_t IDRL0B = (UInt_t)atoll(row2->GetField(4));
6499                if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B);
6500                //      if ( IDRL0A == IDRL0B ){
6501    //            TSQLResult *result2a = 0;
6502    //            TSQLRow    *row2a    = 0;
6503    //            oss.str("");
6504    //            oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;";
6505    //            if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
6506    //            result2a = conn->Query(oss.str().c_str());
6507    //            //
6508    //            if ( !result2a ) throw -4;
6509    //            //
6510    //            row2a = result2a->Next();
6511    //            //
6512    //            if ( row2a ){
6513    //              UInt_t PKA = (UInt_t)atoll(row2a->GetField(0));
6514    //              delete result2a;
6515    //              oss.str("");
6516    //              oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;";
6517    //              if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
6518    //              result2a = conn->Query(oss.str().c_str());
6519    //              //
6520    //              if ( !result2a ) throw -4;
6521    //              //
6522    //              row2a = result2a->Next();
6523    //              //
6524    //              if ( row2a ){
6525    //                UInt_t PKB = (UInt_t)atoll(row2a->GetField(0));
6526    //                //
6527    //                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);
6528    //                if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){
6529    //                  if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
6530                        OK = true;
6531                        //            };
6532                        //          };
6533                        //        };                  
6534                        //    };
6535              };      
6536            if ( OK ){
6537              //
6538              // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table
6539              //
6540              // 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 )
6541              //
6542              GL_RUN *glA = new GL_RUN();
6543              glA->Query_GL_RUN(IDRL2A,conn);
6544              //
6545              if ( glA->GetACQ_BUILD_INFO() != 0 ){
6546                //
6547                // the first piece contains a good runheader we can update all the other runs with correct infos!
6548                //
6549                oss.str("");
6550                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6551                    << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "
6552                    << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
6553                    << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , "
6554                    << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , "
6555                    << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , "
6556                    << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , "
6557                    << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , "
6558                    << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , "
6559                    << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , "
6560                    << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , "
6561                    << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , "  
6562                    << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , "  
6563                    << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , "  
6564                    << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , "  
6565                    << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , ";
6566                //
6567                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
6568                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
6569                if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
6570                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , ";
6571                //        
6572                oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC();
6573                oss << " WHERE ID=" << glrun->GetID() << ";";
6574                if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str());
6575                conn->Query(oss.str().c_str());    
6576                //
6577              } else {
6578                //
6579                // sig no runheader, let set anyway what is possible...
6580                //
6581                oss.str("");
6582                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6583                    << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , ";
6584                //
6585                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
6586                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
6587                if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
6588                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6589                //        
6590                oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
6591                    << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT();
6592                oss << " WHERE ID=" << glrun->GetID() << ";";
6593                if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str());
6594                conn->Query(oss.str().c_str());    
6595              };
6596              //
6597              // update runheader ROOT_ID_FRAG
6598              //
6599              oss.str("");
6600              oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
6601              if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
6602              conn->Query(oss.str().c_str());
6603              //
6604              // now let's look for runtrailer if any in the last run
6605              //
6606              glA->Query_GL_RUN(IDRL2B,conn);
6607              //
6608              if ( glA->GetPKT_READY_COUNTER() != 0 ){
6609                //
6610                // the first piece contains a good runtrailer we can update all the other runs with correct infos!
6611                //
6612                oss.str("");
6613                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6614                    << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
6615                    << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "
6616                    << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , "
6617                    << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , ";
6618                //
6619                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
6620                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6621                if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) {
6622                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6623                //        
6624                oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER()
6625                    << " WHERE ID=" << glrun->GetID() << ";";
6626                if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str());
6627                conn->Query(oss.str().c_str());    
6628                //
6629              } else {
6630                //
6631                // sig no runtrailer, let set anyway what is possible...
6632                //
6633                oss.str("");
6634                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6635                    << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
6636                    << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , ";
6637                  //
6638                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
6639                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6640                if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){
6641                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6642                //        
6643                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()
6644                    << " WHERE ID=" << glrun->GetID() << ";";
6645                if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str());
6646                conn->Query(oss.str().c_str());    
6647              };
6648              //
6649              UInt_t myi = glrun->GetID();
6650              oss.str("");
6651              oss << " ID= "<< myi;
6652              //
6653              glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
6654              //
6655              // fill the new run in GL_RUN
6656              //
6657              glrun->SetID_RUN_FRAG(IDRL2B);
6658              glrun->Fill_GL_RUN(conn);    
6659              glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
6660              delete glA;
6661              moved++;
6662              //
6663            } else {
6664              //
6665              // is just a repetition
6666              //
6667              if ( IsDebug() ) printf(" The run %u is already present in the GL_RUN table...\n",glrun->GetID());    
6668              //      printf(" CCCCCCCCICCCCCCCCCCCCIOOOOOOOOOO si muove Ciccio! %u \n",glrun->GetID());
6669              //      if ( PEDANTIC && glrun->GetID() != 61563 ) throw -83;
6670              if ( PEDANTIC ) throw -83;
6671            };
6672            //      if ( IsDebug() ) printf(" The run already exists in the GL_RUN table! ...\n");
6673            //      if ( PEDANTIC ) throw -83;
6674        };        };
6675        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));      
6676        //        //
# Line 6054  Int_t PamelaDBOperations::ValidateRuns(T Line 6736  Int_t PamelaDBOperations::ValidateRuns(T
6736      // 2) get the OBT of the last validated run      // 2) get the OBT of the last validated run
6737      // --------------------------------------------------------------      // --------------------------------------------------------------
6738      oss.str("");      oss.str("");
6739      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
6740        oss << " SELECT * FROM GL_RUN  WHERE VALIDATION>0 AND RUNHEADER_TIME<="<< t_start
6741          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
6742      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());
6743      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
# Line 6197  Int_t PamelaDBOperations::ValidateRuns(T Line 6880  Int_t PamelaDBOperations::ValidateRuns(T
6880          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
6881                                                        
6882          if( !CHECK && this_run->VALIDATION ){          if( !CHECK && this_run->VALIDATION ){
6883            for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);            //      for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
6884              for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],this_run->VALIDATION);
6885            nseq=0;            nseq=0;
6886          }          }
6887                                        
# Line 6218  Int_t PamelaDBOperations::ValidateRuns(T Line 6902  Int_t PamelaDBOperations::ValidateRuns(T
6902      if( CHECK ){      if( CHECK ){
6903        // check if calibration exists        // check if calibration exists
6904        if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);        if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
6905        Bool_t MISSING = MissingTRK_CALIB(t1,t2);        //      Bool_t MISSING = MissingTRK_CALIB(t1,t2);
6906        for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);        UInt_t MISSING = MissingTRK_CALIB(t1,t2);
6907          UInt_t val = 0;
6908          if ( MISSING == 1 ) val = 0;
6909          if ( MISSING == 0 ) val = 1;
6910          if ( MISSING == 2 ) val = 2;
6911          for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],val);
6912        nseq=0;        nseq=0;
6913      };      };
6914      //--------------      //--------------
# Line 6249  Int_t PamelaDBOperations::ValidateRuns(T Line 6938  Int_t PamelaDBOperations::ValidateRuns(T
6938   * @param t2 To absolute time   * @param t2 To absolute time
6939   * @return true if there might be a missing calibration   * @return true if there might be a missing calibration
6940   */   */
6941  Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){  //Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
6942    UInt_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
6943                    
6944    GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();    GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
6945                    
6946    // get the closest calibration before the run start (t2)    // get the closest calibration before the run start (t2)
6947    if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);    //>>> missing    //  if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);        //>>> missing
6948      if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(1);       //>>> missing
6949                    
6950    if ( trkcalib->TO_TIME  < t2 ) return(true);                                  //>>> missing    //  if ( trkcalib->TO_TIME  < t2 ) return(true);                                      //>>> missing
6951      if ( trkcalib->TO_TIME  < t2 ) return(1);                                     //>>> missing
6952                    
6953    //==============================================================    //==============================================================
6954    // Check is done first on the basis of time between calibration,    // Check is done first on the basis of time between calibration,
# Line 6273  Bool_t PamelaDBOperations::MissingTRK_CA Line 6965  Bool_t PamelaDBOperations::MissingTRK_CA
6965      //==============================================================      //==============================================================
6966      Bool_t DOWNLOAD = false;      Bool_t DOWNLOAD = false;
6967      // check if the calib was skipped becouse of download .... DA FARE!!      // check if the calib was skipped becouse of download .... DA FARE!!
6968      if(DOWNLOAD)return(false);      //    if(DOWNLOAD)return(false);
6969        if(DOWNLOAD)return(0);
6970                                    
6971      return(true);                                       //>>> missing      //    return(true);                                         //>>> missing
6972        return(1);                                  //>>> missing
6973                                    
6974    };    };
6975                    
# Line 6285  Bool_t PamelaDBOperations::MissingTRK_CA Line 6979  Bool_t PamelaDBOperations::MissingTRK_CA
6979    //==============================================================    //==============================================================
6980    // the long time interval bewteen runs might be due to download    // the long time interval bewteen runs might be due to download
6981    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);
6982    return(false);    if ( trkcalib->VALIDATION ) return(0);
6983      if ( IsDebug() )printf("Calibration is not validated... :-/ ==> OK but with VALIDATION=2! \n");
6984      return(2);
6985                    
6986  };  };
6987  /**  /**
# Line 6294  Bool_t PamelaDBOperations::MissingTRK_CA Line 6990  Bool_t PamelaDBOperations::MissingTRK_CA
6990   * @param validation true/false   * @param validation true/false
6991   */   */
6992  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
6993      return(this->assignVALIDATION(idrun,(UInt_t)validation));
6994    }
6995    
6996    Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, UInt_t validation){
6997    TSQLResult *result = 0;    TSQLResult *result = 0;
6998    stringstream oss;    stringstream oss;
6999    oss.str("");    oss.str("");
# Line 6490  Int_t PamelaDBOperations::removeFile(TSt Line 7190  Int_t PamelaDBOperations::removeFile(TSt
7190    stringstream myquery;    stringstream myquery;
7191    //      //  
7192    myquery.str("");    myquery.str("");
7193    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() <<"';";
7194    //    //
7195    pResult = conn->Query(myquery.str().c_str());    pResult = conn->Query(myquery.str().c_str());
7196    //    //
# Line 6506  Int_t PamelaDBOperations::removeFile(TSt Line 7206  Int_t PamelaDBOperations::removeFile(TSt
7206    //    //
7207    this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));    this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
7208    this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));    this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
7209      UInt_t idtsy=(UInt_t)atoll(Row->GetField(2));
7210    //    //
7211    this->ValidationOFF();    this->ValidationOFF();
7212    //    //
# Line 6513  Int_t PamelaDBOperations::removeFile(TSt Line 7214  Int_t PamelaDBOperations::removeFile(TSt
7214    //    //
7215    this->RemoveRUNS();    this->RemoveRUNS();
7216    //    //
7217    this->RemoveFILES();    this->RemoveFILES(idtsy);
7218    //    //
7219    this->SetID_ROOT(0);    this->SetID_ROOT(0);
7220    this->SetID_RAW(0);    this->SetID_RAW(0);
# Line 6733  void PamelaDBOperations::RemoveRUNS(){ Line 7434  void PamelaDBOperations::RemoveRUNS(){
7434   * Rearrange calibration tables   * Rearrange calibration tables
7435   *   *
7436   **/   **/
7437  void PamelaDBOperations::RemoveFILES(){  void PamelaDBOperations::RemoveFILES(UInt_t idtsy){
7438    stringstream myquery;    stringstream myquery;
7439    //    //
7440    myquery.str("");    myquery.str("");
# Line 6743  void PamelaDBOperations::RemoveFILES(){ Line 7444  void PamelaDBOperations::RemoveFILES(){
7444    //    //
7445    conn->Query(myquery.str().c_str());    conn->Query(myquery.str().c_str());
7446    //    //
7447      myquery.str("");
7448      myquery << " DELETE FROM GL_ROOT WHERE ID=" <<this->GetID_ROOT() <<";";
7449      //
7450      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
7451      //
7452      conn->Query(myquery.str().c_str());  //
7453      //
7454      if ( !chewbacca ){
7455        myquery.str("");
7456        myquery << " DELETE FROM GL_TIMESYNC WHERE ID=" << idtsy <<";";
7457        //
7458        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
7459        //
7460        conn->Query(myquery.str().c_str());
7461      };
7462      //
7463  };  };
7464    
7465  /**  /**

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.31

  ViewVC Help
Powered by ViewVC 1.1.23