/[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.12 by mocchiut, Fri Nov 28 10:57:44 2008 UTC revision 1.39 by mocchiut, Tue Oct 14 12:59:14 2014 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-"<< (float)chlastts <<")<50000 AND ABS(REAL_TIME_INIT-"<< atof(row2->GetField(10)) <<")<50000 group by BOOT_NUMBER order by BOOT_NUMBER asc;"; // even more relaxed conditions... 091214 [9REDtest3: error -29] // 10RED MYSQL operators BUG:
230                  /*
231    
232    mysql> select CAST(1322000646 AS UNSIGNED INTEGER)-1322017203.;
233    +--------------------------------------------------+
234    | CAST(1322000646 AS UNSIGNED INTEGER)-1322017203. |
235    +--------------------------------------------------+
236    |                                           -16557 |
237    +--------------------------------------------------+
238    1 row in set (0.06 sec)
239    
240    mysql> select CAST(1322000646 AS UNSIGNED INTEGER)-1322017203;
241    +-------------------------------------------------+
242    | CAST(1322000646 AS UNSIGNED INTEGER)-1322017203 |
243    +-------------------------------------------------+
244    |                            18446744073709535059 |
245    +-------------------------------------------------+
246    1 row in set (0.02 sec)
247    
248                  */
249                  if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
250                  result3 = conn->Query(qu.str().c_str());
251                  if ( result3 ){
252                    row3 = result3->Next();
253                    if ( row3 && result3->GetRowCount() == 1 ){
254                      chboot = (UInt_t)atoll(row3->GetField(0));
255                      if ( debug ) printf(" Found boot_number = %u \n",chboot);
256                    } else {
257                      if ( debug ) printf(" AGH CANNOT DETERMINE THE BOOT NUMBER... \n");
258                      throw -29;
259                    };
260                  };
261                };
262              };
263              //
264            chpktinit = (UInt_t)atoll(row2->GetField(6));            chpktinit = (UInt_t)atoll(row2->GetField(6));
265            chpktfinal = (UInt_t)atoll(row2->GetField(7));            chpktfinal = (UInt_t)atoll(row2->GetField(7));
266            chobtinit = (UInt_t)atoll(row2->GetField(8));            chobtinit = (UInt_t)atoll(row2->GetField(8));
# Line 189  void PamelaDBOperations::OpenL0File(TStr Line 273  void PamelaDBOperations::OpenL0File(TStr
273          throw -85;          throw -85;
274        };        };
275        filerootname = chpath + chfile;// + ".root";        filerootname = chpath + chfile;// + ".root";
       if ( debug ) printf(" chewbacca: filename is %s \n",filerootname.Data());  
276      };      };
277      this->SetRootName(filerootname);      this->SetRootName(filerootname);
     file = TFile::Open(this->GetRootName().Data());  
   } else {  
     this->SetRootName("");  
278    };    };
279      if ( debug ) printf(" Filename is %s \n",filerootname.Data());
280      file = TFile::Open(this->GetRootName().Data());
281      //  } else {
282      //    this->SetRootName("");
283      //    this->SetRootName(filerootname);
284      //  };
285  }  }
286    
287  //  //
# Line 395  void PamelaDBOperations::SetOrbitNo(UInt Line 481  void PamelaDBOperations::SetOrbitNo(UInt
481      return;      return;
482    };    };
483    //    //
484    TString name = this->GetRootFile();    if ( !chewbacca ){
485    Int_t nlength = name.Length();      TString name = this->GetRootFile();
486    if ( nlength < 5 ) return;      Int_t nlength = name.Length();
487    TString dwo = 0;      if ( nlength < 5 ){
488    for (Int_t i = 0; i<5; i++){        if ( IsDebug() ) printf(" Agh problems determining the orbit number! name = %s \n",name.Data());
489      dwo.Append(name[i],1);        return;
490    };      };
491    if ( dwo.IsDigit() ){      TString dwo = "";
492      dworbit = (UInt_t)dwo.Atoi();      for (Int_t i = 0; i<5; i++){
   } else {  
     dwo="";  
     for (Int_t i = 8; i<13; i++){  
493        dwo.Append(name[i],1);        dwo.Append(name[i],1);
494      };          };
495      if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();      if ( dwo.IsDigit() ){
496          dworbit = (UInt_t)dwo.Atoi();
497        } else {
498          dwo="";
499          for (Int_t i = 8; i<13; i++){
500            dwo.Append(name[i],1);
501          };    
502          if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
503        };
504        if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
505    };    };
   if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());  
506    return;    return;
507  };  };
508    
# Line 791  Int_t PamelaDBOperations::SetUpperLimits Line 882  Int_t PamelaDBOperations::SetUpperLimits
882      ph = eh->GetPscuHeader();      ph = eh->GetPscuHeader();
883      pktlast = ph->GetCounter();      pktlast = ph->GetCounter();
884      nevent = upperentry - chminentry;      nevent = upperentry - chminentry;
885        if ( ((Int_t)upperentry - (Int_t)chminentry) < 0 ) nevent = 0; // nevent is UInt_t! 090112 [8RED: error -88]
886        if ( IsDebug() ) printf(" Setting nevent to %u - upperentry %i chminentry %i \n",nevent,upperentry,chminentry);
887      //      //
888    } else {    } else {
889      upperpkt = PKT(pktlast);      upperpkt = PKT(pktlast);
# Line 799  Int_t PamelaDBOperations::SetUpperLimits Line 892  Int_t PamelaDBOperations::SetUpperLimits
892    };    };
893    //    //
894    if ( chewbacca && nevent < 1 ) {    if ( chewbacca && nevent < 1 ) {
895        if ( IsDebug() ) printf(" chewbacca and no events in the file \n");
896      pktfirst = chpktmin;      pktfirst = chpktmin;
897      upperpkt = PKT(chpktmax);      upperpkt = PKT(chpktmax);
898      pktlast = chpktmax;      pktlast = chpktmax;
# Line 824  Int_t PamelaDBOperations::SetUpperLimits Line 918  Int_t PamelaDBOperations::SetUpperLimits
918    //  if ( nevent < jump ) jump = int(nevent/10);    //  if ( nevent < jump ) jump = int(nevent/10);
919    //  if ( !jump ) jump = 1;    //  if ( !jump ) jump = 1;
920    //    //
921    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) && nevent > deltapkt ){  
922      if ( ((PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt)) && nevent > deltapkt ){
923      //      //
924      if ( IsDebug() ) printf(" starting jump %i \n",jump);      if ( IsDebug() ) printf(" starting jump %i \n",jump);
925      //    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 1271  void PamelaDBOperations::CheckConnection Line 1366  void PamelaDBOperations::CheckConnection
1366    stringstream oss;    stringstream oss;
1367    //      //  
1368    oss.str("");    oss.str("");
1369    oss << "SET time_zone='+0:00';";    oss << "SET time_zone='+0:00'; SET sql_mode = 'NO_UNSIGNED_SUBTRACTION';";
1370    TSQLResult *result = 0;    TSQLResult *result = 0;
1371    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1372    if ( !result ) throw -10;    if ( !result ) throw -10;
# Line 1471  void PamelaDBOperations::FillClass(Bool_ Line 1566  void PamelaDBOperations::FillClass(Bool_
1566      glrun->SetRUNTRAILER_PKT(lastPkt);      glrun->SetRUNTRAILER_PKT(lastPkt);
1567      //          //    
1568    };    };
1569      //
1570      // ad hoc solution for INF EV_TO [8RED] 090113
1571      //
1572      if ( !firstev && lastev == -1 ){
1573        if ( IsDebug() ) printf(" EE firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1574        firstev = 1;
1575        lastev = 0;
1576      };
1577    glrun->SetEV_FROM((UInt_t)firstev);    glrun->SetEV_FROM((UInt_t)firstev);
1578    glrun->SetEV_TO((UInt_t)lastev);    glrun->SetEV_TO((UInt_t)lastev);
1579    glrun->SetNEVENTS((UInt_t)lastev-(UInt_t)firstev+1);    glrun->SetNEVENTS((UInt_t)lastev-(UInt_t)firstev+1);
# Line 1555  Int_t PamelaDBOperations::insertPamelaRa Line 1658  Int_t PamelaDBOperations::insertPamelaRa
1658  Int_t PamelaDBOperations::insertPamelaGL_TIMESYNC(){  Int_t PamelaDBOperations::insertPamelaGL_TIMESYNC(){
1659    //    //
1660    Int_t signal = 0;    Int_t signal = 0;
1661    UInt_t idresof = 0;    //  UInt_t idresof = 0;
1662      idresof = 0;
1663    stringstream   oss;    stringstream   oss;
1664    TSQLResult *result = 0;    TSQLResult *result = 0;
1665    TSQLRow *row = 0;    TSQLRow *row = 0;
# Line 1577  Int_t PamelaDBOperations::insertPamelaGL Line 1681  Int_t PamelaDBOperations::insertPamelaGL
1681      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
1682      if ( !result ) throw -10;      if ( !result ) throw -10;
1683      row = result->Next();      row = result->Next();
1684        if ( !row ) throw -92;
1685      idresof = (UInt_t)atoll(row->GetField(0));      idresof = (UInt_t)atoll(row->GetField(0));
1686      existsts = true;      existsts = true;
1687      goto eout;      goto eout;
# Line 1598  Int_t PamelaDBOperations::insertPamelaGL Line 1703  Int_t PamelaDBOperations::insertPamelaGL
1703      if ( !row ){      if ( !row ){
1704        oss.str("");        oss.str("");
1705        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< "
1706            << dworbit << " order by FROM_ORBIT desc limit 1;";            << dworbit << " AND SPECIAL_FILE='' order by FROM_ORBIT desc limit 1;";
1707        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1708        result = conn->Query(oss.str().c_str());        result = conn->Query(oss.str().c_str());
1709        if ( !result ) throw -10;        if ( !result ) throw -10;
# Line 1645  Int_t PamelaDBOperations::insertPamelaGL Line 1750  Int_t PamelaDBOperations::insertPamelaGL
1750      TYPE = 0;      TYPE = 0;
1751      TSYNC = 0;      TSYNC = 0;
1752      //      //
1753      Double_t minimum = 0.;      //    Double_t minimum = 0.;
1754      Double_t maximum = 0.;      // Double_t maximum = 0.;
1755      Double_t minimum2 = 0.;      //Double_t minimum2 = 0.;
1756      Double_t maximum2 = 0.;      Double_t maximum2 = 0.;
1757      //      //
1758      //      //
# Line 1655  Int_t PamelaDBOperations::insertPamelaGL Line 1760  Int_t PamelaDBOperations::insertPamelaGL
1760      pamela::McmdRecord *mcrc = 0;      pamela::McmdRecord *mcrc = 0;
1761      TArrayC *mcmddata = 0;      TArrayC *mcmddata = 0;
1762      //      //
1763      minimum = numeric_limits<Double_t>::max();      //    minimum = numeric_limits<Double_t>::max();
1764      maximum = numeric_limits<Double_t>::min();      //    maximum = numeric_limits<Double_t>::min();
1765      minimum2 = numeric_limits<Double_t>::max();      //    minimum2 = numeric_limits<Double_t>::max();
1766      maximum2 = numeric_limits<Double_t>::min();      maximum2 = numeric_limits<Double_t>::min();
1767      //      //
1768      T = (TTree*)file->Get("Mcmd");      T = (TTree*)file->Get("Mcmd");
# Line 1868  Int_t PamelaDBOperations::insertPamelaRo Line 1973  Int_t PamelaDBOperations::insertPamelaRo
1973      //      //
1974      if ( chewbacca ){      if ( chewbacca ){
1975          oss.str("");          oss.str("");
1976          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<<" and ID_RESURS_OFFSET="<< idresof <<" limit 1;"; // 10RED BUG!!!! without 'and ID_RESURS_OFFSET="<< idresof ' this query is not enough to ensure finding the correct line in GL_TIMSYNC...
1977          if ( debug ) printf(" %s \n",oss.str().c_str());          if ( debug ) printf(" %s \n",oss.str().c_str());
1978          result = conn->Query(oss.str().c_str());          result = conn->Query(oss.str().c_str());
1979          //          //
# Line 2077  Int_t PamelaDBOperations::assignBOOT_NUM Line 2182  Int_t PamelaDBOperations::assignBOOT_NUM
2182      UInt_t lowerts = tsync-(obt0/1000)-5;      UInt_t lowerts = tsync-(obt0/1000)-5;
2183      if ( chewbacca ){      if ( chewbacca ){
2184        oss.str("");        oss.str("");
2185        oss << "select BOOT_NUMBER from ROOT_TABLE where LAST_TIME_SYNC_INFO-(OBT_TIME_SYNC)<"        oss << "select BOOT_NUMBER from ROOT_TABLE where CAST((LAST_TIME_SYNC_INFO-(OBT_TIME_SYNC)) AS SIGNED INT)<" // 10RED MYSQL OPERATORS BUG
2186            << upperts            << upperts
2187            << " AND LAST_TIME_SYNC_INFO-(OBT_TIME_SYNC)>"            << " AND CAST((LAST_TIME_SYNC_INFO-(OBT_TIME_SYNC)) AS SIGNED INT)>" // 10RED MYSQL OPERATORS BUG
2188            << lowerts            << lowerts
2189            << " AND BOOT_NUMBER>1;";            << " AND BOOT_NUMBER>1;";
2190      } else {      } else {
2191        oss.str("");        oss.str("");
2192        oss << "select GL_RAW.BOOT_NUMBER from GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW where TIMESYNC-(OBT0/1000)<"        oss << "select GL_RAW.BOOT_NUMBER from GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW where CAST((TIMESYNC-(OBT0/1000)) AS SIGNED INT)<" // 10RED MYSQL OPERATORS BUG
2193            << upperts            << upperts
2194            << " AND TIMESYNC-(OBT0/1000)>"            << " AND CAST((TIMESYNC-(OBT0/1000)) AS SIGNED INT)>" // 10RED MYSQL OPERATORS BUG
2195            << lowerts            << lowerts
2196            << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";            << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
2197      };      };
# Line 2185  Int_t PamelaDBOperations::insertPamelaRU Line 2290  Int_t PamelaDBOperations::insertPamelaRU
2290    //    //
2291    // no runtrailers in the file!    // no runtrailers in the file!
2292    //    //
2293    if ( !rtev ){    if ( IsDebug() ) printf(" Start rtev %i rhev %i nrtev %i nrhev %i \n",rtev,rhev,nrtev,nrhev);
2294      if ( !rhev ){    if ( !rtev || (chewbacca && !nrtev) ){
2295        if ( IsDebug() ) printf(" No runtrailers \n");
2296        if ( !rhev || (chewbacca && !nrhev) ){
2297        if ( IsDebug() ) printf(" No runheaders nor runtrailers!! \n");        if ( IsDebug() ) printf(" No runheaders nor runtrailers!! \n");
2298        if ( !(upperentry-chminentry) ){        if ( !(upperentry-chminentry) ){
2299          if ( IsDebug() ) printf(" No physics events nor runs in the file \n"); // di nuovo potrebbe esserci un runtrailer senza eventi (riempimento MM)          if ( IsDebug() ) printf(" No physics events nor runs in the file \n"); // di nuovo potrebbe esserci un runtrailer senza eventi (riempimento MM)
# Line 2223  Int_t PamelaDBOperations::insertPamelaRU Line 2330  Int_t PamelaDBOperations::insertPamelaRU
2330      //      //
2331    } else {    } else {
2332      //      //
2333        if ( IsDebug() ) printf(" We have runtrailers \n");
2334      Int_t conptt = -1;      Int_t conptt = -1;
2335      for (Int_t ptt=0; ptt<rtev; ptt++){      for (Int_t ptt=0; ptt<rtev; ptt++){
2336        //        //
# Line 2239  Int_t PamelaDBOperations::insertPamelaRU Line 2347  Int_t PamelaDBOperations::insertPamelaRU
2347          ptht = cod->Get(pctp->RunHeader) - 1;          ptht = cod->Get(pctp->RunHeader) - 1;
2348          //      evbeft = cod->Get(pctp->Physics);          //      evbeft = cod->Get(pctp->Physics);
2349          tcod = (UInt_t)cod->Get(pctp->Physics);          tcod = (UInt_t)cod->Get(pctp->Physics);
2350          if ( !tcod ) tcod = 1;          if ( !tcod ) tcod = 1;    // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2351          evbeft = TMath::Min(upperentry,(tcod-1));          evbeft = TMath::Min(upperentry,(tcod-1));
2352          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));
2353          //          //
# Line 2278  Int_t PamelaDBOperations::insertPamelaRU Line 2386  Int_t PamelaDBOperations::insertPamelaRU
2386            obth = phh->GetOrbitalTime();            obth = phh->GetOrbitalTime();
2387            cod = ehh->GetCounter();            cod = ehh->GetCounter();
2388            tcod = (UInt_t)cod->Get(pctp->Physics);            tcod = (UInt_t)cod->Get(pctp->Physics);
2389            if ( !tcod ) tcod = 1;            if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2390            evbefh = TMath::Max(chminentry,(tcod-1));            evbefh = TMath::Max(chminentry,(tcod-1));
2391            //            //
2392            if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){            if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){
# Line 2340  Int_t PamelaDBOperations::insertPamelaRU Line 2448  Int_t PamelaDBOperations::insertPamelaRU
2448                cod = ehh->GetCounter();                cod = ehh->GetCounter();
2449                //            evbeft = cod->Get(pctp->Physics);                //            evbeft = cod->Get(pctp->Physics);
2450                tcod = (UInt_t)cod->Get(pctp->Physics);                tcod = (UInt_t)cod->Get(pctp->Physics);
2451                if ( !tcod ) tcod = 1;                if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2452                evbeft = TMath::Min(upperentry,(tcod-1));                evbeft = TMath::Min(upperentry,(tcod-1));
2453                rh->GetEntry(pth);                rh->GetEntry(pth);
2454                phh = ehh->GetPscuHeader();                phh = ehh->GetPscuHeader();
# Line 2382  Int_t PamelaDBOperations::insertPamelaRU Line 2490  Int_t PamelaDBOperations::insertPamelaRU
2490            //    if ( conptt+1 == (nrtev+nrtbef )){              //    if ( conptt+1 == (nrtev+nrtbef )){  
2491            if ( IsDebug() ) printf(" >>>>>>>>>>> %i %u %i %u \n",ptht,rhev,nrtev,conptt);            if ( IsDebug() ) printf(" >>>>>>>>>>> %i %u %i %u \n",ptht,rhev,nrtev,conptt);
2492            ptht++;            ptht++;
2493            if ( ptht < rhev ){            if ( ptht < rhev ){
2494              rh->GetEntry(ptht);              rh->GetEntry(ptht);
2495              //pth++;              //pth++;
2496              //if ( pth < rhev ){              //if ( pth < rhev ){
# Line 2410  Int_t PamelaDBOperations::insertPamelaRU Line 2518  Int_t PamelaDBOperations::insertPamelaRU
2518              if ( (UInt_t)evbeft < upperentry-1 && upperentry>0 ){              if ( (UInt_t)evbeft < upperentry-1 && upperentry>0 ){
2519                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);
2520                //                //
2521                if ( (ptt-1) < 0 ) throw -15; // should never arrive here!                //              if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
2522                  if ( (rtev-1) < 0 || ptt < 0 ) throw -15; // should never arrive here!
2523                //              rt->GetEntry(ptt-1);                //              rt->GetEntry(ptt-1);
2524                rt->GetEntry(rtev-1);                rt->GetEntry(rtev-1);
2525                cod = eht->GetCounter();                cod = eht->GetCounter();
# Line 2645  void PamelaDBOperations::HandleRunFragme Line 2754  void PamelaDBOperations::HandleRunFragme
2754      //      //
2755      // 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
2756      //      //
2757      if ( rhfirstev != firstev && !mishead ) mishead = true;      if ( firstev != (lastev+1) ){ // could be a problem sometimes (?)
2758      if ( rtlastev != lastev && !mistrail ) mistrail = true;        if ( rhfirstev != firstev && !mishead ) mishead = true;
2759          if ( rtlastev != lastev && !mistrail ) mistrail = true;
2760        };
2761      //      //
2762        if ( IsDebug() ) printf(" bhere firstev is %i lastev is %i \n",firstev,lastev);
2763      this->FillClass(mishead,mistrail,firstev,lastev);      this->FillClass(mishead,mistrail,firstev,lastev);
2764        if ( IsDebug() ) printf(" chere firstev is %i lastev is %i \n",firstev,lastev);
2765      //      //
2766      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");
2767      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 2690  void PamelaDBOperations::HandleRunFragme Line 2803  void PamelaDBOperations::HandleRunFragme
2803        glrun->Fill_GL_RUN_FRAGMENTS(conn);        glrun->Fill_GL_RUN_FRAGMENTS(conn);
2804        //        //
2805      } else {      } else {
2806        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);
2807        if ( PEDANTIC ) throw -69;        if ( !RELAXED ){
2808        return;          if ( PEDANTIC ) throw -69;
2809            return;
2810          } else {
2811            glrun->SetID((UInt_t)atoll(row->GetField(0)));
2812            glrun->SetID_RUN_FRAG(0);
2813          };
2814      };      };
2815      //      //
2816      if ( chewbacca && mishead && mistrail ) goto justcheck;      if ( chewbacca && mishead && mistrail ) goto justcheck;
# Line 2743  void PamelaDBOperations::HandleRunFragme Line 2861  void PamelaDBOperations::HandleRunFragme
2861          //          //
2862          found = false; // default value          found = false; // default value
2863          //          //
2864          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");
2865          //          //
2866          // 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:
2867          //          //
# Line 2759  void PamelaDBOperations::HandleRunFragme Line 2877  void PamelaDBOperations::HandleRunFragme
2877              if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);              if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2878              //              //
2879              found = true;              found = true;
2880                if ( IsDebug() ) printf(" where firstev is %i lastev is %i \n",firstev,lastev);
2881              //              //
2882            } else {            } else {
2883              //              //
# Line 2824  void PamelaDBOperations::HandleRunFragme Line 2943  void PamelaDBOperations::HandleRunFragme
2943          oss << " ID="<<row->GetField(0)<<";";          oss << " ID="<<row->GetField(0)<<";";
2944          //          //
2945          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
2946            if ( IsDebug() ) printf(" there firstev is %i lastev is %i \n",firstev,lastev);
2947          //          //
2948          // merge infos          // merge infos
2949          //          //
# Line 2854  void PamelaDBOperations::HandleRunFragme Line 2974  void PamelaDBOperations::HandleRunFragme
2974          glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());          glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
2975          glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());          glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
2976          //          //
2977            if ( IsDebug() ) printf(" here firstev is %i lastev is %i \n",firstev,lastev);
2978            //
2979          glrun->SetEV_FROM(firstev);          glrun->SetEV_FROM(firstev);
2980            glrun->SetEV_TO(lastev); // EM 10 RED ghost run bug
2981          glrun->SetNEVENTS(lastev-firstev+1);          glrun->SetNEVENTS(lastev-firstev+1);
2982          //          //
2983          glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());          glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
# Line 2946  void PamelaDBOperations::HandleRunFragme Line 3069  void PamelaDBOperations::HandleRunFragme
3069          //          //
3070          found = false; // default value          found = false; // default value
3071          //          //
3072          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");
3073          //          //
3074          // 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:
3075          //          //
# Line 3046  void PamelaDBOperations::HandleRunFragme Line 3169  void PamelaDBOperations::HandleRunFragme
3169            apkt = PKT(ph->GetCounter());            apkt = PKT(ph->GetCounter());
3170            aobt = OBT(ph->GetOrbitalTime());                aobt = OBT(ph->GetOrbitalTime());    
3171            lastev--;            lastev--;
3172            if ( PEDANTIC ) throw -72;            if ( PEDANTIC && !RELAXED) throw -72;
3173          };          };
3174          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);
3175          //          //
3176            glrun->SetEV_FROM(firstev); // EM 10RED ghost run bug
3177          glrun->SetEV_TO(lastev);          glrun->SetEV_TO(lastev);
3178          glrun->SetNEVENTS(lastev-firstev+1);          glrun->SetNEVENTS(lastev-firstev+1);
3179          glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());          glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());
# Line 3114  void PamelaDBOperations::HandleRunFragme Line 3238  void PamelaDBOperations::HandleRunFragme
3238        // 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?
3239        //        //
3240        oss.str("");        oss.str("");
3241        oss << " SELECT ID FROM GL_RUN WHERE "        oss << " SELECT ID,ID_ROOT_L0 FROM GL_RUN WHERE "
3242            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3243            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3244            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
# Line 3137  void PamelaDBOperations::HandleRunFragme Line 3261  void PamelaDBOperations::HandleRunFragme
3261        row = result->Next();        row = result->Next();
3262        //        //
3263        if ( row ){        if ( row ){
3264          if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");          //
3265          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
3266            // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number.
3267            //
3268            // 090112 [8RED (-70): RUN ALREADY INSERTED]
3269            // 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!
3270            //
3271            Bool_t OK = false;
3272            UInt_t IDRL2A = 0;
3273            UInt_t IDRL2B = 0;
3274            if ( chewbacca ){
3275              if ( result->GetRowCount() >= 2 ){
3276                //
3277                if ( IsDebug() ) printf(" RH---| gap |---RT case, number of pieces in the GL_RUN table: %i \n",result->GetRowCount());
3278                OK = true;
3279                IDRL2A = (UInt_t)atoll(row->GetField(0));
3280                //
3281                while ( row ){
3282                  //
3283                  TSQLResult *trresult = 0;
3284                  TSQLRow    *trrow    = 0;
3285                  //
3286                  stringstream tross;
3287                  tross.str("");
3288                  //
3289                  tross << " SELECT RUNHEADER_PKT, RUNTRAILER_PKT FROM GL_RUN_TRASH where ID=" << row->GetField(0) << ";";
3290                  if ( IsDebug() ) printf(" check in the gl_run_trash table for pkt intervals: query is \n %s \n",tross.str().c_str());
3291                  trresult = conn->Query(tross.str().c_str());
3292                  if ( !trresult ) throw -4;
3293                  trrow = trresult->Next();
3294                  if ( !trrow || trresult->GetRowCount() != 1 ){
3295                    OK = false;
3296                    if ( IsDebug() ) printf(" OPS! no such run (or multiple runs!) in GL_RUN_TRASH table, something wrong is going on! \n");
3297                    break;
3298                  };
3299                  //
3300                  UInt_t pktH =  (UInt_t)atoll(row->GetField(0));
3301                  UInt_t pktT =  (UInt_t)atoll(row->GetField(1));
3302                  delete trresult;
3303                  if (
3304                      (PKT(pktH) >= PKT(glrun->GetRUNHEADER_PKT()) && PKT(pktH) <= PKT(glrun->GetRUNTRAILER_PKT())) ||
3305                      (PKT(pktT) >= PKT(glrun->GetRUNHEADER_PKT()) && PKT(pktT) <= PKT(glrun->GetRUNTRAILER_PKT())) ||
3306                      (PKT(pktH) <= PKT(glrun->GetRUNTRAILER_PKT()) && PKT(pktT) >= PKT(glrun->GetRUNTRAILER_PKT())) ){            
3307                    OK = false;
3308                    if ( IsDebug() ) printf(" The run is overlapping with already inserted ones!! \n");
3309                    break;          
3310                  };
3311                  //PKT(glrun->GetRUNHEADER_PKT())  PKT(glrun->GetRUNTRAILER_PKT())
3312                  row = result->Next();
3313                };
3314                
3315                //    if ( result->GetRowCount() == 2 ) {
3316                //      IDRL2A = (UInt_t)atoll(row->GetField(0));
3317                //      UInt_t IDRL0A = (UInt_t)atoll(row->GetField(1));
3318                //      row = result->Next();
3319                //      IDRL2B = (UInt_t)atoll(row->GetField(0));
3320                //      UInt_t IDRL0B = (UInt_t)atoll(row->GetField(1));
3321                //      if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B);
3322                //      //      if ( IDRL0A == IDRL0B ){ //091230 why this condition???? RH could be in a file and RT in another one...
3323                //      if ( true ){
3324                //        TSQLResult *result2 = 0;
3325                //        TSQLRow    *row2    = 0;
3326                //        oss.str("");
3327                //        oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;";
3328                //        if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3329                //        result2 = conn->Query(oss.str().c_str());
3330                //        //
3331                //        if ( !result2 ) throw -4;
3332                //        //
3333                //        row2 = result2->Next();
3334                //        //
3335                //        if ( row2 ){
3336                //          UInt_t PKA = (UInt_t)atoll(row2->GetField(0));
3337                //          delete result2;
3338                //          oss.str("");
3339                //          oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;";
3340                //          if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3341                //          result2 = conn->Query(oss.str().c_str());
3342                //          //
3343                //          if ( !result2 ) throw -4;
3344                //          //
3345                //          row2 = result2->Next();
3346                //          //
3347                //          if ( row2 ){
3348                //            UInt_t PKB = (UInt_t)atoll(row2->GetField(0));
3349                //            //
3350                //            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);
3351                //            if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){
3352                //              if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
3353                //              OK = true;
3354                //            };
3355                //          };
3356                //        };                  
3357                //      };
3358                //    };      
3359              };
3360            };
3361            if ( OK ){
3362              //
3363              // 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  
3364              //
3365              // 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 )
3366              //
3367              if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
3368              //
3369              GL_RUN *glA = new GL_RUN();
3370              glA->Query_GL_RUN(IDRL2A,conn);
3371              //
3372              glrun->SetRUNHEADER_TIME(glA->GetRUNHEADER_TIME());
3373              glrun->SetRUNHEADER_OBT(glA->GetRUNHEADER_OBT());
3374              glrun->SetRUNHEADER_PKT(glA->GetRUNHEADER_PKT());
3375              //
3376              glrun->SetRUNTRAILER_TIME(glA->GetRUNTRAILER_TIME());
3377              glrun->SetRUNTRAILER_OBT(glA->GetRUNTRAILER_OBT());
3378              glrun->SetRUNTRAILER_PKT(glA->GetRUNTRAILER_PKT());
3379              //
3380              if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glA->GetPHYSENDRUN_MASK_S3S2S12());
3381              if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glA->GetPHYSENDRUN_MASK_S11CRC());
3382              //          
3383              if ( glA->GetACQ_BUILD_INFO() != 0 ){
3384                //
3385                // the first piece contains a good runheader we can update all the other runs with correct infos!
3386                //
3387                glrun->SetCOMPILATIONTIMESTAMP(glA->GetCOMPILATIONTIMESTAMP());
3388                glrun->SetFAV_WRK_SCHEDULE(glA->GetFAV_WRK_SCHEDULE());
3389                glrun->SetEFF_WRK_SCHEDULE(glA->GetEFF_WRK_SCHEDULE());
3390                glrun->SetPRH_VAR_TRG_MODE_A(glA->GetPRH_VAR_TRG_MODE_A());
3391                glrun->SetPRH_VAR_TRG_MODE_B(glA->GetPRH_VAR_TRG_MODE_B());
3392                glrun->SetACQ_BUILD_INFO(glA->GetACQ_BUILD_INFO());
3393                glrun->SetACQ_VAR_INFO(glA->GetACQ_VAR_INFO());
3394                glrun->SetRM_ACQ_AFTER_CALIB(glA->GetRM_ACQ_AFTER_CALIB());
3395                glrun->SetRM_ACQ_SETTING_MODE(glA->GetRM_ACQ_SETTING_MODE());
3396                glrun->SetTRK_CALIB_USED(glA->GetTRK_CALIB_USED());
3397                glrun->SetCAL_DSP_MASK(glA->GetCAL_DSP_MASK());
3398                glrun->SetLAST_TIMESYNC(glA->GetLAST_TIMESYNC());
3399                glrun->SetOBT_TIMESYNC(glA->GetOBT_TIMESYNC());
3400                //
3401              };
3402              //
3403              if ( glA->GetPKT_READY_COUNTER() != 0 ){
3404                //
3405                // the first piece contains a good runtrailer we can update all the other runs with correct infos!
3406                //
3407                glrun->SetPKT_COUNTER(glA->GetPKT_COUNTER());
3408                glrun->SetPKT_READY_COUNTER(glA->GetPKT_READY_COUNTER());
3409              };
3410              //
3411              // update runheader ROOT_ID_FRAG
3412              //
3413              oss.str("");
3414              oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
3415              if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3416              conn->Query(oss.str().c_str());
3417              //
3418              oss.str("");
3419              oss << "SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG = " << IDRL2A << ";";
3420              if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3421              TSQLResult *tsresult = 0;
3422              TSQLRow    *tsrow    = 0;
3423              tsresult=conn->Query(oss.str().c_str());
3424              if ( !tsresult ) throw -4;
3425              tsrow = tsresult->Next();
3426              if ( !tsrow || tsresult->GetRowCount() != 1 ) throw -4;
3427              IDRL2B = (UInt_t)atoll(tsrow->GetField(0));
3428              glrun->SetID_RUN_FRAG(IDRL2B);
3429              //
3430              // fill the new run in GL_RUN
3431              //
3432              glrun->Fill_GL_RUN(conn);    
3433              //
3434              glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3435              delete glA;
3436              //
3437            } else {
3438    //        //
3439    //        // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table
3440    //        //
3441    //        // 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 )
3442    //        //
3443    //        GL_RUN *glA = new GL_RUN();
3444    //        glA->Query_GL_RUN(IDRL2A,conn);
3445    //        //
3446    //        if ( glA->GetACQ_BUILD_INFO() != 0 ){
3447    //          //
3448    //          // the first piece contains a good runheader we can update all the other runs with correct infos!
3449    //          //
3450    //          oss.str("");
3451    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3452    //              << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "
3453    //              << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3454    //              << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , "
3455    //              << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , "
3456    //              << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , "
3457    //              << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , "
3458    //              << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , "
3459    //              << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , "
3460    //              << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , "
3461    //              << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , "
3462    //              << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , "  
3463    //              << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , "  
3464    //              << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , "  
3465    //              << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , "  
3466    //              << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , ";
3467    //          //
3468    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3469    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
3470    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3471    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , ";
3472    //          //        
3473    //          oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC();
3474    //          oss << " WHERE ID=" << glrun->GetID() << ";";
3475    //          if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str());
3476    //          conn->Query(oss.str().c_str());    
3477    //          //
3478    //        } else {
3479    //          //
3480    //          // sig no runheader, let set anyway what is possible...
3481    //          //
3482    //          oss.str("");
3483    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3484    //              << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , ";
3485    //          //
3486    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3487    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
3488    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3489    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
3490    //          //        
3491    //          oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3492    //              << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT();
3493    //          oss << " WHERE ID=" << glrun->GetID() << ";";
3494    //          if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str());
3495    //          conn->Query(oss.str().c_str());    
3496    //        };
3497    //        //
3498    //        // update runheader ROOT_ID_FRAG
3499    //        //
3500    //        oss.str("");
3501    //        oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
3502    //        if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3503    //        conn->Query(oss.str().c_str());
3504    //        //
3505    //        // now let's look for runtrailer if any in the last run
3506    //        //
3507    //        glA->Query_GL_RUN(IDRL2B,conn);
3508    //        //
3509    //        if ( glA->GetPKT_READY_COUNTER() != 0 ){
3510    //          //
3511    //          // the first piece contains a good runtrailer we can update all the other runs with correct infos!
3512    //          //
3513    //          oss.str("");
3514    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3515    //              << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3516    //              << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "
3517    //              << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , "
3518    //              << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , ";
3519    //          //
3520    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3521    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3522    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) {
3523    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3524    //          //        
3525    //          oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER()
3526    //              << " WHERE ID=" << glrun->GetID() << ";";
3527    //          if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str());
3528    //          conn->Query(oss.str().c_str());    
3529    //          //
3530    //        } else {
3531    //          //
3532    //          // sig no runtrailer, let set anyway what is possible...
3533    //          //
3534    //          oss.str("");
3535    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3536    //              << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3537    //              << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , ";
3538    //            //
3539    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3540    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3541    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){
3542    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3543    //          //        
3544    //          oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()
3545    //              << " WHERE ID=" << glrun->GetID() << ";";
3546    //          if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str());
3547    //          conn->Query(oss.str().c_str());    
3548    //        };
3549    //        //
3550    //        UInt_t myi = glrun->GetID();
3551    //        oss.str("");
3552    //        oss << " ID= "<< myi;
3553    //        //
3554    //        glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
3555    //        //
3556    //        // fill the new run in GL_RUN
3557    //        //
3558    //        glrun->SetID_RUN_FRAG(IDRL2B);
3559    //        glrun->Fill_GL_RUN(conn);    
3560    //        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3561    //        delete glA;
3562    //        //
3563    //      } else {
3564              //
3565              // is just a repetition
3566              //
3567              if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");        
3568              if ( PEDANTIC ) throw -70;
3569            };
3570            //
3571        } else {        } else {
3572          if ( NoFrag() ){          if ( NoFrag() ){
3573            glrun->SetID_RUN_FRAG(glrun->GetID());            glrun->SetID_RUN_FRAG(glrun->GetID());
# Line 3175  void PamelaDBOperations::HandleMissingHo Line 3604  void PamelaDBOperations::HandleMissingHo
3604        glrun->SetID(this->AssignRunID());        glrun->SetID(this->AssignRunID());
3605        glrun->SetID_RUN_FRAG(0);        glrun->SetID_RUN_FRAG(0);
3606        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...
3607      };        };
3608      //      //
3609    };    };
3610    //    //
# Line 3191  Bool_t PamelaDBOperations::IsRunConsiste Line 3620  Bool_t PamelaDBOperations::IsRunConsiste
3620    //      //  
3621    EventCounter *code=0;    EventCounter *code=0;
3622    //    //
3623    UInt_t nevent = 0;    //  UInt_t nevent = 0;
3624    UInt_t checkfirst = 0;    UInt_t checkfirst = 0;
3625    UInt_t checklast = 0;    UInt_t checklast = 0;
3626    UInt_t firstentry = 0;    UInt_t firstentry = 0;
# Line 3213  Bool_t PamelaDBOperations::IsRunConsiste Line 3642  Bool_t PamelaDBOperations::IsRunConsiste
3642    EventHeader *eh = 0;    EventHeader *eh = 0;
3643    PscuHeader *ph = 0;    PscuHeader *ph = 0;
3644    T->SetBranchAddress("Header", &eh);    T->SetBranchAddress("Header", &eh);
3645    nevent = T->GetEntries();    //  nevent = T->GetEntries();
3646    //    //
3647    //    //
3648    if ( firstev == lastev+1 || lastev == firstev ) { // no events inside the run!    if ( firstev == lastev+1 || lastev == firstev ) { // no events inside the run!
3649      //if ( firstev <= lastev+1 ) { // no events inside the run!      //if ( firstev <= lastev+1 ) { // no events inside the run!
3650      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
3651        firstev = lastev+1; // this is necessary for files with no Physics entries, should have no influence on other files
3652      // return true is correct      // return true is correct
3653      return(true);      return(true);
3654      //      //
# Line 3474  Bool_t PamelaDBOperations::IsRunConsiste Line 3904  Bool_t PamelaDBOperations::IsRunConsiste
3904                  //                  //
3905                  found = false; // default value                  found = false; // default value
3906                  //                  //
3907                  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");
3908                  //                  //
3909                  // 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:
3910                  //                  //
# Line 3586  Bool_t PamelaDBOperations::IsRunConsiste Line 4016  Bool_t PamelaDBOperations::IsRunConsiste
4016                  glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());                  glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
4017                  //                  //
4018                  glrun->SetEV_FROM(firstev);                  glrun->SetEV_FROM(firstev);
4019                    glrun->SetEV_TO(lastev); // 10 RED ghost run bug
4020                  glrun->SetNEVENTS(lastev-firstev+1);                  glrun->SetNEVENTS(lastev-firstev+1);
4021                  //                  //
4022                  glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());                  glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
# Line 3733  void PamelaDBOperations::HandleSuspiciou Line 4164  void PamelaDBOperations::HandleSuspiciou
4164    EventCounter *code=0;    EventCounter *code=0;
4165    UInt_t firstev = 0;    UInt_t firstev = 0;
4166    UInt_t lastev = 0;    UInt_t lastev = 0;
4167    UInt_t nevent = 0;    //  UInt_t nevent = 0;
4168    UInt_t checkfirst = 0;    UInt_t checkfirst = 0;
4169    UInt_t checklast = 0;    UInt_t checklast = 0;
4170    UInt_t firstentry = 0;    UInt_t firstentry = 0;
# Line 3758  void PamelaDBOperations::HandleSuspiciou Line 4189  void PamelaDBOperations::HandleSuspiciou
4189    EventHeader *eh = 0;    EventHeader *eh = 0;
4190    PscuHeader *ph = 0;    PscuHeader *ph = 0;
4191    T->SetBranchAddress("Header", &eh);    T->SetBranchAddress("Header", &eh);
4192    nevent = T->GetEntries();    //  nevent = T->GetEntries();
4193    //    //
4194    codt = eht->GetCounter();    codt = eht->GetCounter();
4195    codh = ehh->GetCounter();    codh = ehh->GetCounter();
# Line 3992  Int_t PamelaDBOperations::insertCALO_CAL Line 4423  Int_t PamelaDBOperations::insertCALO_CAL
4423    UInt_t totime = 0;    UInt_t totime = 0;
4424    UInt_t obt = 0;    UInt_t obt = 0;
4425    UInt_t pkt = 0;    UInt_t pkt = 0;
4426      Float_t totped = 0.;
4427    //    //
4428    tr = (TTree*)file->Get("CalibCalPed");    tr = (TTree*)file->Get("CalibCalPed");
4429    if ( !tr || tr->IsZombie() ) throw -21;    if ( !tr || tr->IsZombie() ) throw -21;
# Line 4009  Int_t PamelaDBOperations::insertCALO_CAL Line 4441  Int_t PamelaDBOperations::insertCALO_CAL
4441        if ( calibCalPed->cstwerr[section] ){        if ( calibCalPed->cstwerr[section] ){
4442          valid = 1;          valid = 1;
4443          if ( calibCalPed->cperror[section] ) valid = 0;          if ( calibCalPed->cperror[section] ) valid = 0;
4444            //
4445            // 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.]
4446            //
4447            totped = 0.;
4448            Int_t ns = 0;
4449            if ( section == 2 ) ns = 3;
4450            if ( section == 3 ) ns = 1;
4451            if ( section == 1 ) ns = 2;
4452            for (UInt_t ss=0; ss<96; ss++){
4453              totped += fabs(calibCalPed->calped[ns][0][ss]);
4454            }
4455            if ( totped < 1. ){
4456              if ( IsDebug() ) printf(" Section %i totped %f -  No calibration data! Calorimeter power problems? \n",section,totped);
4457              valid = 0;
4458            };
4459            //
4460          ph = eh->GetPscuHeader();          ph = eh->GetPscuHeader();
4461          obt = ph->GetOrbitalTime();            obt = ph->GetOrbitalTime();  
4462          pkt = ph->GetCounter();            pkt = ph->GetCounter();  
# Line 4176  Int_t PamelaDBOperations::insertCALOPULS Line 4624  Int_t PamelaDBOperations::insertCALOPULS
4624    tr->SetBranchAddress("Header", &eh);    tr->SetBranchAddress("Header", &eh);
4625    nevents = tr->GetEntries();    nevents = tr->GetEntries();
4626    //    //
4627      Float_t totpul = 0.;
4628      //
4629    if ( nevents > 0 ){    if ( nevents > 0 ){
4630      //      //
4631      for (UInt_t i=0; i < nevents; i++){      for (UInt_t i=0; i < nevents; i++){
# Line 4185  Int_t PamelaDBOperations::insertCALOPULS Line 4635  Int_t PamelaDBOperations::insertCALOPULS
4635          if ( cp1->pstwerr[section] && cp1->unpackError == 0 ){          if ( cp1->pstwerr[section] && cp1->unpackError == 0 ){
4636            valid = 1;            valid = 1;
4637            if ( cp1->pperror[section] ) valid = 0;            if ( cp1->pperror[section] ) valid = 0;
4638              //
4639              // 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.]
4640              //
4641              totpul = 0.;
4642              Int_t ns = 0;
4643              if ( section == 2 ) ns = 3;
4644              if ( section == 3 ) ns = 1;
4645              if ( section == 1 ) ns = 2;
4646              for (UInt_t ss=0; ss<96; ss++){
4647                totpul += cp1->calpuls[ns][0][ss];
4648              }
4649              if ( totpul >= 3145632. ){
4650                if ( IsDebug() ) printf(" PULSE1 Section %i totpul %f -  No calibration data! Calorimeter power problems? \n",section,totpul);
4651                valid = 0;
4652              };
4653              //
4654            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
4655            obt = ph->GetOrbitalTime();              obt = ph->GetOrbitalTime();  
4656            pkt = ph->GetCounter();              pkt = ph->GetCounter();  
# Line 4342  Int_t PamelaDBOperations::insertCALOPULS Line 4808  Int_t PamelaDBOperations::insertCALOPULS
4808          if ( cp2->pstwerr[section] && cp2->unpackError == 0 ){          if ( cp2->pstwerr[section] && cp2->unpackError == 0 ){
4809            valid = 1;            valid = 1;
4810            if ( cp2->pperror[section] ) valid = 0;            if ( cp2->pperror[section] ) valid = 0;
4811              //
4812              // 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.]
4813              //
4814              totpul = 0.;
4815              Int_t ns = 0;
4816              if ( section == 2 ) ns = 3;
4817              if ( section == 3 ) ns = 1;
4818              if ( section == 1 ) ns = 2;
4819              for (UInt_t ss=0; ss<96; ss++){
4820                totpul += cp2->calpuls[ns][0][ss];
4821              }
4822              if ( totpul >= 3145632. ){
4823                if ( IsDebug() ) printf(" PULSE2 Section %i totpul %f -  No calibration data! Calorimeter power problems? \n",section,totpul);
4824                valid = 0;
4825              };
4826              //
4827            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
4828            obt = ph->GetOrbitalTime();              obt = ph->GetOrbitalTime();  
4829            pkt = ph->GetCounter();              pkt = ph->GetCounter();  
# Line 4707  void PamelaDBOperations::HandleTRK_CALIB Line 5189  void PamelaDBOperations::HandleTRK_CALIB
5189      if ( !result ) throw -4;;      if ( !result ) throw -4;;
5190      row = result->Next();      row = result->Next();
5191      if(row)glcal->ID = (UInt_t)atoll(row->GetField(0));      if(row)glcal->ID = (UInt_t)atoll(row->GetField(0));
5192        //
5193        delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5194      //      //
5195  };  };
5196    
# Line 4989  Int_t PamelaDBOperations::insertTRK_CALI Line 5472  Int_t PamelaDBOperations::insertTRK_CALI
5472    
5473          if ( IsDebug() ) cout << endl << ">>>> TRY TO RECOVER TRACKER CALIBRATIONS <<<<"<<endl;          if ( IsDebug() ) cout << endl << ">>>> TRY TO RECOVER TRACKER CALIBRATIONS <<<<"<<endl;
5474    
5475          TSQLResult *result = 0;          TSQLResult *rresult = 0; // 10RED bug, result is re-used inside result looop...
5476            TSQLResult *result = 0; // 10RED bug, result is re-used inside result looop...
5477          TSQLRow    *row    = 0;          TSQLRow    *row    = 0;
5478          //          //
5479          stringstream oss;          stringstream oss;
# Line 5019  Int_t PamelaDBOperations::insertTRK_CALI Line 5503  Int_t PamelaDBOperations::insertTRK_CALI
5503          oss << " ORDER BY ID DESC LIMIT "<<ncalib<<"; ";                  oss << " ORDER BY ID DESC LIMIT "<<ncalib<<"; ";        
5504          if ( IsDebug() ) cout << oss.str().c_str() << endl;          if ( IsDebug() ) cout << oss.str().c_str() << endl;
5505    
5506          result = conn->Query(oss.str().c_str());          rresult = conn->Query(oss.str().c_str());
5507          if ( !result ) throw -4;;          if ( !result ) throw -4;;
5508          if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;          if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5509                    
# Line 5028  Int_t PamelaDBOperations::insertTRK_CALI Line 5512  Int_t PamelaDBOperations::insertTRK_CALI
5512          // -----------------------------------          // -----------------------------------
5513          UInt_t nn=0;          UInt_t nn=0;
5514          do {          do {
5515              row = result->Next();              row = rresult->Next();
5516              if(!row)break;              if(!row)break;
5517                            
5518              UInt_t id       = (UInt_t)atoll(row->GetField(0));              UInt_t id       = (UInt_t)atoll(row->GetField(0));
# Line 5067  Int_t PamelaDBOperations::insertTRK_CALI Line 5551  Int_t PamelaDBOperations::insertTRK_CALI
5551              TString path       = "";              TString path       = "";
5552              TString name       = "";              TString name       = "";
5553              TString raw        = "";              TString raw        = "";
5554              UInt_t obt0        = 0;              //      UInt_t obt0        = 0;
5555              UInt_t timesync    = 0;              //      UInt_t timesync    = 0;
5556              UInt_t boot_number = 0;              //      UInt_t boot_number = 0;
5557              bool   FOUND       = false;              bool   FOUND       = false;
5558    
5559              if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;              if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;
# Line 5142  Int_t PamelaDBOperations::insertTRK_CALI Line 5626  Int_t PamelaDBOperations::insertTRK_CALI
5626                      path       = (TString)row2->GetField(0);                      path       = (TString)row2->GetField(0);
5627                      name       = (TString)row2->GetField(1);                      name       = (TString)row2->GetField(1);
5628                      raw        = (TString)row2->GetField(5);                      raw        = (TString)row2->GetField(5);
5629                      obt0        = (UInt_t)atoll(row2->GetField(2));                      //              obt0        = (UInt_t)atoll(row2->GetField(2));
5630                      timesync    = (UInt_t)atoll(row2->GetField(3));                      //              timesync    = (UInt_t)atoll(row2->GetField(3));
5631                      boot_number = (UInt_t)atoll(row2->GetField(4));                      //              boot_number = (UInt_t)atoll(row2->GetField(4));
5632                                    
5633                      if ( IsDebug() ) cout << "- - - - - - - - - - -" <<endl;                      if ( IsDebug() ) cout << "- - - - - - - - - - -" <<endl;
5634  //                  cout << path <<endl;  //                  cout << path <<endl;
# Line 5208  Int_t PamelaDBOperations::insertTRK_CALI Line 5692  Int_t PamelaDBOperations::insertTRK_CALI
5692                      if(FOUND)break;                      if(FOUND)break;
5693    
5694                  }while(1);//endl loop over root table entries                  }while(1);//endl loop over root table entries
5695                    
5696                    delete result2; // 10RED potential memory leak
5697                  if(FOUND)break;                  if(FOUND)break;
5698    
5699              }//end loop over tables              }//end loop over tables
5700    
5701              if(FOUND){              if(FOUND){
5702    
5703              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  
                 ////////////////////////////////////////////  
5704    
5705                  // ---------------------------------------------------------------------                ////////////////////////////////////////////
5706                  // step 1: insert a new raw file in GL_RAW                // insert a new entry in GL_TRK_CALIB and
5707                  // ---------------------------------------------------------------------                // modify the time-tag of the previous one
5708                  //                ////////////////////////////////////////////
5709                  // check if the raw file already exist  
5710                  //                // ---------------------------------------------------------------------
5711                  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
5712  //              oss.str("");                // ---------------------------------------------------------------------
5713  //              oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";";                //
5714  //              if ( IsDebug() ) cout << oss.str().c_str() << endl;                // check if the raw file already exist
5715                  //
5716                  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
5717                  //                oss.str("");
5718                  //                oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";";
5719                  //                if ( IsDebug() ) cout << oss.str().c_str() << endl;
5720                                    
5721  //              result = conn->Query(oss.str().c_str());                //                result = conn->Query(oss.str().c_str());
5722  //              if ( !result ) throw -4;;                //                if ( !result ) throw -4;;
5723  //              if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;                //                if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5724  //              if( result->GetRowCount() == 0){                //                if( result->GetRowCount() == 0){
5725  //                  if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl;                //                    if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl;
5726  //                  // - - - - - - - - - - -                //                    // - - - - - - - - - - -
5727  //                  // insert new raw file                //                    // insert new raw file
5728  //                  // - - - - - - - - - - -                //                    // - - - - - - - - - - -
5729  //                  GL_RAW glraw = GL_RAW();                    //                    GL_RAW glraw = GL_RAW();    
5730  //                  glraw.PATH        = gSystem->DirName(raw.Data());                //                    glraw.PATH        = gSystem->DirName(raw.Data());
5731  //                  glraw.NAME        = gSystem->BaseName(raw.Data());                //                    glraw.NAME        = gSystem->BaseName(raw.Data());
5732  //                  glraw.BOOT_NUMBER = boot_number;                //                    glraw.BOOT_NUMBER = boot_number;
5733  //                  //                //                    //
5734  //                  insertPamelaRawFile( &glraw );                //                    insertPamelaRawFile( &glraw );
5735  //                  //                              //                    //              
5736  //                  id_raw = glraw.ID;                              //                    id_raw = glraw.ID;              
5737  //              }else{                //                }else{
5738  //                  row = result->Next();                //                    row = result->Next();
5739  //                  id_raw = (UInt_t)atoll(row->GetField(0));                //                    id_raw = (UInt_t)atoll(row->GetField(0));
5740  //              }                //                }
5741  //              if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl;                //                if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl;
5742    
5743                  // ---------------------------------------------------------------------                // ---------------------------------------------------------------------
5744                  // step 1(bis): retrieve the timesync id associated to the file                // step 1(bis): retrieve the timesync id associated to the file
5745                  // (NB, uso lo stesso associato al file iniziale)                // (NB, uso lo stesso associato al file iniziale)
5746                  // ---------------------------------------------------------------------                // ---------------------------------------------------------------------
5747                  UInt_t idtimesync = 0;                UInt_t idtimesync = 0;
5748                  oss.str("");                oss.str("");
5749                  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<<" and ID_RESURS_OFFSET="<< idresof <<" limit 1;"; // 10RED BUG!!!! without 'and ID_RESURS_OFFSET="<< idresof ' this query is not enough to ensure finding the correct line in GL_TIMSYNC...
5750                  if ( debug ) printf(" %s \n",oss.str().c_str());                if ( debug ) printf(" %s \n",oss.str().c_str());
5751                  result = conn->Query(oss.str().c_str());                result = conn->Query(oss.str().c_str());
5752                  if ( !result ) throw -3;                if ( !result ) throw -3;
5753                  row = result->Next();                  row = result->Next();
5754                  if ( !row ) throw -3;                  if ( !row ) throw -3;
5755                  idtimesync = (UInt_t)atoll(row->GetField(0));                  idtimesync = (UInt_t)atoll(row->GetField(0));
5756                  if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl;                  if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl;
5757                                    
5758                    delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5759                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5760                  // step 2: insert a new root file in GL_ROOT                  // step 2: insert a new root file in GL_ROOT
5761                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
# Line 5317  Int_t PamelaDBOperations::insertTRK_CALI Line 5803  Int_t PamelaDBOperations::insertTRK_CALI
5803                  }                  }
5804                  if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl;                  if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl;
5805    
5806                    delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5807                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5808                  // step 3: modify time-tag of corrupted GL_TRK_CALIB entry                  // step 3: modify time-tag of corrupted GL_TRK_CALIB entry
5809                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
# Line 5329  Int_t PamelaDBOperations::insertTRK_CALI Line 5816  Int_t PamelaDBOperations::insertTRK_CALI
5816                  result = conn->Query(oss.str().c_str());                  result = conn->Query(oss.str().c_str());
5817                  if ( !result ) throw -4;;                  if ( !result ) throw -4;;
5818    
5819                    delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5820    
5821                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5822                  // step 4: insert the new calibration:                  // step 4: insert the new calibration:
5823                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
# Line 5358  Int_t PamelaDBOperations::insertTRK_CALI Line 5847  Int_t PamelaDBOperations::insertTRK_CALI
5847          }while(1);//end loop over calibrations          }while(1);//end loop over calibrations
5848                    
5849                    
5850          if( result )delete result;          if( rresult )delete rresult; // 10RED bug
5851          if( row    )delete row;          if( row    )delete row;
5852                    
5853                    
# Line 5659  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6148  Int_t PamelaDBOperations::CleanGL_RUN_FR
6148      memset(myid,0,500*sizeof(UInt_t));      memset(myid,0,500*sizeof(UInt_t));
6149      //      //
6150      oss.str("");      oss.str("");
6151      oss << "SELECT ID,RUNTRAILER_TIME,RUNTRAILER_PKT,BOOT_NUMBER FROM GL_RUN_FRAGMENTS WHERE INSERT_TIME <= '" << clean_time->AsSQLString() << "'order BY RUNHEADER_TIME asc;";      oss << "SELECT ID,RUNTRAILER_TIME,RUNTRAILER_PKT,BOOT_NUMBER FROM GL_RUN_FRAGMENTS WHERE INSERT_TIME <= '" << clean_time->AsSQLString() << "' order BY RUNHEADER_TIME asc;";
6152      if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());      if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
6153      nresult = conn->Query(oss.str().c_str());      nresult = conn->Query(oss.str().c_str());
6154      //      //
# Line 5690  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6179  Int_t PamelaDBOperations::CleanGL_RUN_FR
6179            while ( anr ){            while ( anr ){
6180              //              //
6181              oss.str("");              oss.str("");
6182              oss << "SELECT ID,RUNTRAILER_TIME,RUNTRAILER_PKT,BOOT_NUMBER FROM GL_RUN_FRAGMENTS WHERE BOOT_NUMBER=" << mbo << " AND RUNHEADER_PKT=" << mrhp <<  "+1 AND ABS(RUNHEADER_TIME-"<< mrht <<")<=1 AND INSERT_TIME <= '" << clean_time->AsSQLString() << "' order BY RUNHEADER_TIME asc;";              oss << "SELECT ID,RUNTRAILER_TIME,RUNTRAILER_PKT,BOOT_NUMBER FROM GL_RUN_FRAGMENTS WHERE BOOT_NUMBER=" << mbo << " AND RUNHEADER_PKT=" << mrhp <<  "+1 AND ABS(RUNHEADER_TIME-"<< (float)mrht <<")<=1 AND INSERT_TIME <= '" << clean_time->AsSQLString() << "' order BY RUNHEADER_TIME asc;"; // 10RED MYSQL OPERATOR BUG
6183              if ( IsDebug() ) printf(" In the loop searching for fragmented runs : query is \n %s \n",oss.str().c_str());              if ( IsDebug() ) printf(" In the loop searching for fragmented runs : query is \n %s \n",oss.str().c_str());
6184              //              //
6185              nresult1 = conn->Query(oss.str().c_str());              nresult1 = conn->Query(oss.str().c_str());
# Line 5780  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6269  Int_t PamelaDBOperations::CleanGL_RUN_FR
6269                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6270                //                              //              
6271                oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "                oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "
6272                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< ";";                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT();
6273                  oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6274                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6275              };                  };    
6276            };            };
# Line 5829  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6319  Int_t PamelaDBOperations::CleanGL_RUN_FR
6319                if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){                if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){
6320                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6321                //                              //              
6322                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< ";";                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT();
6323                  oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6324                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6325              };                  };    
6326            };            };
# Line 5880  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6371  Int_t PamelaDBOperations::CleanGL_RUN_FR
6371                row2 = result2->Next();                row2 = result2->Next();
6372                //                //
6373                if ( row2 ){                if ( row2 ){
6374                  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");
6375                  if ( PEDANTIC ) throw -83;                  if ( PEDANTIC ) throw -83;
6376                  row2->Close();                  row2->Close();
6377                };                };
# Line 5957  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6448  Int_t PamelaDBOperations::CleanGL_RUN_FR
6448        glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);          glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
6449        //        //
6450        oss.str("");        oss.str("");
6451        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 "
6452            << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("            << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
6453            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
6454            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
# Line 6015  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6506  Int_t PamelaDBOperations::CleanGL_RUN_FR
6506          moved++;          moved++;
6507          //          //
6508        } else {        } else {
6509          if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");  
6510          if ( PEDANTIC ) throw -83;          // questa parte mah mah mah... da controllare
6511    
6512            //
6513            // 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
6514            // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number.
6515            //
6516            // 090112 [8RED (-70): RUN ALREADY INSERTED]
6517            //
6518            Bool_t OK = false;
6519            UInt_t IDRL2A = 0;
6520            UInt_t IDRL2B = 0;
6521              if ( result2->GetRowCount() == 2 ){
6522                IDRL2A = (UInt_t)atoll(row2->GetField(0));
6523                UInt_t IDRL0A = (UInt_t)atoll(row2->GetField(4));
6524                row2 = result2->Next();
6525                IDRL2B = (UInt_t)atoll(row2->GetField(0));
6526                UInt_t IDRL0B = (UInt_t)atoll(row2->GetField(4));
6527                if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B);
6528                //      if ( IDRL0A == IDRL0B ){
6529    //            TSQLResult *result2a = 0;
6530    //            TSQLRow    *row2a    = 0;
6531    //            oss.str("");
6532    //            oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;";
6533    //            if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
6534    //            result2a = conn->Query(oss.str().c_str());
6535    //            //
6536    //            if ( !result2a ) throw -4;
6537    //            //
6538    //            row2a = result2a->Next();
6539    //            //
6540    //            if ( row2a ){
6541    //              UInt_t PKA = (UInt_t)atoll(row2a->GetField(0));
6542    //              delete result2a;
6543    //              oss.str("");
6544    //              oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;";
6545    //              if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
6546    //              result2a = conn->Query(oss.str().c_str());
6547    //              //
6548    //              if ( !result2a ) throw -4;
6549    //              //
6550    //              row2a = result2a->Next();
6551    //              //
6552    //              if ( row2a ){
6553    //                UInt_t PKB = (UInt_t)atoll(row2a->GetField(0));
6554    //                //
6555    //                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);
6556    //                if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){
6557    //                  if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
6558                        OK = true;
6559                        //            };
6560                        //          };
6561                        //        };                  
6562                        //    };
6563              };      
6564            if ( OK ){
6565              //
6566              // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table
6567              //
6568              // 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 )
6569              //
6570              GL_RUN *glA = new GL_RUN();
6571              glA->Query_GL_RUN(IDRL2A,conn);
6572              //
6573              if ( glA->GetACQ_BUILD_INFO() != 0 ){
6574                //
6575                // the first piece contains a good runheader we can update all the other runs with correct infos!
6576                //
6577                oss.str("");
6578                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6579                    << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "
6580                    << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
6581                    << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , "
6582                    << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , "
6583                    << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , "
6584                    << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , "
6585                    << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , "
6586                    << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , "
6587                    << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , "
6588                    << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , "
6589                    << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , "  
6590                    << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , "  
6591                    << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , "  
6592                    << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , "  
6593                    << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , ";
6594                //
6595                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
6596                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
6597                if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
6598                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , ";
6599                //        
6600                oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC();
6601                oss << " WHERE ID=" << glrun->GetID() << ";";
6602                if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str());
6603                conn->Query(oss.str().c_str());    
6604                //
6605              } else {
6606                //
6607                // sig no runheader, let set anyway what is possible...
6608                //
6609                oss.str("");
6610                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6611                    << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , ";
6612                //
6613                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
6614                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
6615                if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
6616                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6617                //        
6618                oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
6619                    << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT();
6620                oss << " WHERE ID=" << glrun->GetID() << ";";
6621                if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str());
6622                conn->Query(oss.str().c_str());    
6623              };
6624              //
6625              // update runheader ROOT_ID_FRAG
6626              //
6627              oss.str("");
6628              oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
6629              if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
6630              conn->Query(oss.str().c_str());
6631              //
6632              // now let's look for runtrailer if any in the last run
6633              //
6634              glA->Query_GL_RUN(IDRL2B,conn);
6635              //
6636              if ( glA->GetPKT_READY_COUNTER() != 0 ){
6637                //
6638                // the first piece contains a good runtrailer we can update all the other runs with correct infos!
6639                //
6640                oss.str("");
6641                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6642                    << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
6643                    << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "
6644                    << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , "
6645                    << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , ";
6646                //
6647                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
6648                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6649                if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) {
6650                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6651                //        
6652                oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER()
6653                    << " WHERE ID=" << glrun->GetID() << ";";
6654                if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str());
6655                conn->Query(oss.str().c_str());    
6656                //
6657              } else {
6658                //
6659                // sig no runtrailer, let set anyway what is possible...
6660                //
6661                oss.str("");
6662                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6663                    << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
6664                    << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , ";
6665                  //
6666                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
6667                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6668                if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){
6669                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6670                //        
6671                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()
6672                    << " WHERE ID=" << glrun->GetID() << ";";
6673                if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str());
6674                conn->Query(oss.str().c_str());    
6675              };
6676              //
6677              UInt_t myi = glrun->GetID();
6678              oss.str("");
6679              oss << " ID= "<< myi;
6680              //
6681              glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
6682              //
6683              // fill the new run in GL_RUN
6684              //
6685              glrun->SetID_RUN_FRAG(IDRL2B);
6686              glrun->Fill_GL_RUN(conn);    
6687              glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
6688              delete glA;
6689              moved++;
6690              //
6691            } else {
6692              //
6693              // is just a repetition
6694              //
6695              if ( IsDebug() ) printf(" The run %u is already present in the GL_RUN table...\n",glrun->GetID());    
6696              //      printf(" CCCCCCCCICCCCCCCCCCCCIOOOOOOOOOO si muove Ciccio! %u \n",glrun->GetID());
6697              //      if ( PEDANTIC && glrun->GetID() != 61563 ) throw -83;
6698              if ( PEDANTIC ) throw -83;
6699            };
6700            //      if ( IsDebug() ) printf(" The run already exists in the GL_RUN table! ...\n");
6701            //      if ( PEDANTIC ) throw -83;
6702        };        };
6703        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));      
6704        //        //

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

  ViewVC Help
Powered by ViewVC 1.1.23