/[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.34 by mocchiut, Wed Mar 6 10:12:36 2013 UTC
# Line 60  string getTleDatetime(cTle*); Line 60  string getTleDatetime(cTle*);
60   */   */
61  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename, UInt_t dwinput, Bool_t staticp, Bool_t gpamela, Bool_t keepenv){  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename, UInt_t dwinput, Bool_t staticp, Bool_t gpamela, Bool_t keepenv){
62    //    //
63      RELAXED = false;
64    chewbacca = false;    chewbacca = false;
65    chminentry = 0;    chminentry = 0;
66    chID = 0;    chID = 0;
# Line 87  PamelaDBOperations::PamelaDBOperations(T Line 88  PamelaDBOperations::PamelaDBOperations(T
88    //    //
89    glrun = new GL_RUN();    glrun = new GL_RUN();
90    //    //
91    if ( !chewbacca ){    //   if ( !chewbacca ){
92      if ( !boot ) SetNOBOOT(false);    //     if ( !boot ) SetNOBOOT(false);
93      SetTsync(tsync,gpamela);    //     SetTsync(tsync,gpamela);
94      SetBOOTnumber(boot,gpamela);    //     SetBOOTnumber(boot,gpamela);
95      SetObt0(obt0);    //     SetObt0(obt0);
96    };    //   };
97    //    //
98    SetTLEPath(tlefilename);    SetTLEPath(tlefilename);
99    //    //
# Line 101  PamelaDBOperations::PamelaDBOperations(T Line 102  PamelaDBOperations::PamelaDBOperations(T
102    if (INSERT_RAW) SetRawName(filerawname);    if (INSERT_RAW) SetRawName(filerawname);
103    //    //
104    INSERT_ROOT = !filerootname.IsNull();    INSERT_ROOT = !filerootname.IsNull();
105      if ( INSERT_ROOT ) this->SetRootName(filerootname);
106    this->SetOrbitNo(dwinput);    this->SetOrbitNo(dwinput);
107    //    //
108    this->SetID_RAW(0);    this->SetID_RAW(0);
# Line 124  void PamelaDBOperations::Close(){ Line 126  void PamelaDBOperations::Close(){
126  //  //
127  // SETTERS  // SETTERS
128  //  //
129    void PamelaDBOperations::NotChewbacca(UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t gpamela){
130      if ( !boot ) SetNOBOOT(false);
131      SetTsync(tsync,gpamela);
132      SetBOOTnumber(boot,gpamela);
133      SetObt0(obt0);
134    };
135    
136  void PamelaDBOperations::OpenL0File(TString filerootname){  void PamelaDBOperations::OpenL0File(TString filerootname){
137    if( INSERT_ROOT ){    if( INSERT_ROOT ){
# Line 134  void PamelaDBOperations::OpenL0File(TStr Line 142  void PamelaDBOperations::OpenL0File(TStr
142        stringstream qu;        stringstream qu;
143        TSQLResult *result = 0;        TSQLResult *result = 0;
144        TSQLResult *result2 = 0;        TSQLResult *result2 = 0;
145          TSQLResult *result3 = 0;
146        TSQLRow    *row    = 0;            TSQLRow    *row    = 0;    
147        TSQLRow    *row2    = 0;            TSQLRow    *row2    = 0;    
148          TSQLRow    *row3    = 0;    
149        TString chpath;        TString chpath;
150        TString chfile;        TString chfile;
151        UInt_t ridn = 0;        UInt_t ridn = 0;
152        qu.str("");        qu.str("");
153        qu << "SELECT ROOT_ID_N,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,INSERTED_BY from ROOT_TABLE_MERGING where ID_N=" << chID << ";";        qu << "SELECT ROOT_ID_N,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,INSERTED_BY,REAL_TIME_INIT from ROOT_TABLE_MERGING where ID_N=" << chID << ";";
154        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
155        result = conn->Query(qu.str().c_str());        result = conn->Query(qu.str().c_str());
156        if ( result ){        if ( result ){
# Line 152  void PamelaDBOperations::OpenL0File(TStr Line 162  void PamelaDBOperations::OpenL0File(TStr
162            chobtmin = (UInt_t)atoll(row->GetField(3));            chobtmin = (UInt_t)atoll(row->GetField(3));
163            chobtmax = (UInt_t)atoll(row->GetField(4));            chobtmax = (UInt_t)atoll(row->GetField(4));
164            chiby = (TString)(row->GetField(5));            chiby = (TString)(row->GetField(5));
165              chrtinit = (UInt_t)atoll(row->GetField(6));
166          } else {          } else {
167            throw -84;            throw -84;
168          };          };
# Line 160  void PamelaDBOperations::OpenL0File(TStr Line 171  void PamelaDBOperations::OpenL0File(TStr
171        };        };
172        delete result;        delete result;
173        qu.str("");        qu.str("");
174        qu << "SELECT FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,TIME_OFFSET,BOOT_NUMBER,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL from ROOT_TABLE where ID_N=" << ridn << ";";        qu << "SELECT FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,TIME_OFFSET,BOOT_NUMBER,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,REAL_TIME_INIT from ROOT_TABLE where ID_N=" << ridn << ";";
175        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
176        result2 = conn->Query(qu.str().c_str());        result2 = conn->Query(qu.str().c_str());
177        if ( result2 ){        if ( result2 ){
# Line 177  void PamelaDBOperations::OpenL0File(TStr Line 188  void PamelaDBOperations::OpenL0File(TStr
188            chresursts = (UInt_t)atoll(row2->GetField(4));            chresursts = (UInt_t)atoll(row2->GetField(4));
189            chboot = (UInt_t)atoll(row2->GetField(5));            chboot = (UInt_t)atoll(row2->GetField(5));
190            //            //
191              if ( !chboot ){
192                if ( debug ) printf(" mmm... no BOOT number determined by chewbacca, try to find it looking at DB \n");
193                //
194                // new feature: check in ROOT_TABLE (timewise) the previous and the next BOOT number != 0 , if it is the same we know this one!
195                // if it is different roll back to the old behaviour
196                //
197                qu.str("");
198                qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND REAL_TIME_INIT>"<< row2->GetField(10) <<" order by REAL_TIME_INIT asc limit 1;";
199                if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
200                UInt_t chbootA = 0;
201                UInt_t chbootB = 1;
202                result3 = conn->Query(qu.str().c_str());
203                if ( result3 ){
204                  row3 = result3->Next();
205                  if ( row3 ){
206                    chbootA = (UInt_t)atoll(row3->GetField(0));
207                    if ( debug ) printf(" Found boot_number A = %u \n",chbootA);
208                  };
209                };
210                delete result3;
211                qu.str("");
212                qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND REAL_TIME_INIT<"<< row2->GetField(10) <<" order by REAL_TIME_INIT desc limit 1;";
213                if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
214                result3 = conn->Query(qu.str().c_str());
215                if ( result3 ){
216                  row3 = result3->Next();
217                  if ( row3 ){
218                    chbootB = (UInt_t)atoll(row3->GetField(0));
219                    if ( debug ) printf(" Found boot_number B = %u \n",chbootB);
220                  };
221                };
222                if ( chbootA == chbootB ){
223                  chboot = chbootA;
224                  if ( debug ) printf(" Found boot_number! it is %u \n",chboot);
225                } else {        
226                  qu.str("");
227                  //            qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND ABS(LAST_TIME_SYNC_INFO-"<< chlastts <<")<10000 AND ABS(REAL_TIME_INIT-"<< row2->GetField(10) <<")<7200 group by BOOT_NUMBER order by BOOT_NUMBER asc;";
228                  //            qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND ABS(LAST_TIME_SYNC_INFO-"<< chlastts <<")<40000 AND ABS(REAL_TIME_INIT-"<< row2->GetField(10) <<")<40000 group by BOOT_NUMBER order by BOOT_NUMBER asc;"; // relax conditions... 090112 [8RED: error -29]
229                  qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND ABS(LAST_TIME_SYNC_INFO-"<< chlastts <<")<50000 AND ABS(REAL_TIME_INIT-"<< row2->GetField(10) <<")<50000 group by BOOT_NUMBER order by BOOT_NUMBER asc;"; // even more relaxed conditions... 091214 [9REDtest3: error -29]
230                  if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
231                  result3 = conn->Query(qu.str().c_str());
232                  if ( result3 ){
233                    row3 = result3->Next();
234                    if ( row3 && result3->GetRowCount() == 1 ){
235                      chboot = (UInt_t)atoll(row3->GetField(0));
236                      if ( debug ) printf(" Found boot_number = %u \n",chboot);
237                    } else {
238                      if ( debug ) printf(" AGH CANNOT DETERMINE THE BOOT NUMBER... \n");
239                      throw -29;
240                    };
241                  };
242                };
243              };
244              //
245            chpktinit = (UInt_t)atoll(row2->GetField(6));            chpktinit = (UInt_t)atoll(row2->GetField(6));
246            chpktfinal = (UInt_t)atoll(row2->GetField(7));            chpktfinal = (UInt_t)atoll(row2->GetField(7));
247            chobtinit = (UInt_t)atoll(row2->GetField(8));            chobtinit = (UInt_t)atoll(row2->GetField(8));
# Line 189  void PamelaDBOperations::OpenL0File(TStr Line 254  void PamelaDBOperations::OpenL0File(TStr
254          throw -85;          throw -85;
255        };        };
256        filerootname = chpath + chfile;// + ".root";        filerootname = chpath + chfile;// + ".root";
       if ( debug ) printf(" chewbacca: filename is %s \n",filerootname.Data());  
257      };      };
258      this->SetRootName(filerootname);      this->SetRootName(filerootname);
     file = TFile::Open(this->GetRootName().Data());  
   } else {  
     this->SetRootName("");  
259    };    };
260      if ( debug ) printf(" Filename is %s \n",filerootname.Data());
261      file = TFile::Open(this->GetRootName().Data());
262      //  } else {
263      //    this->SetRootName("");
264      //    this->SetRootName(filerootname);
265      //  };
266  }  }
267    
268  //  //
# Line 395  void PamelaDBOperations::SetOrbitNo(UInt Line 462  void PamelaDBOperations::SetOrbitNo(UInt
462      return;      return;
463    };    };
464    //    //
465    TString name = this->GetRootFile();    if ( !chewbacca ){
466    Int_t nlength = name.Length();      TString name = this->GetRootFile();
467    if ( nlength < 5 ) return;      Int_t nlength = name.Length();
468    TString dwo = 0;      if ( nlength < 5 ){
469    for (Int_t i = 0; i<5; i++){        if ( IsDebug() ) printf(" Agh problems determining the orbit number! name = %s \n",name.Data());
470      dwo.Append(name[i],1);        return;
471    };      };
472    if ( dwo.IsDigit() ){      TString dwo = "";
473      dworbit = (UInt_t)dwo.Atoi();      for (Int_t i = 0; i<5; i++){
   } else {  
     dwo="";  
     for (Int_t i = 8; i<13; i++){  
474        dwo.Append(name[i],1);        dwo.Append(name[i],1);
475      };          };
476      if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();      if ( dwo.IsDigit() ){
477          dworbit = (UInt_t)dwo.Atoi();
478        } else {
479          dwo="";
480          for (Int_t i = 8; i<13; i++){
481            dwo.Append(name[i],1);
482          };    
483          if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
484        };
485        if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
486    };    };
   if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());  
487    return;    return;
488  };  };
489    
# Line 791  Int_t PamelaDBOperations::SetUpperLimits Line 863  Int_t PamelaDBOperations::SetUpperLimits
863      ph = eh->GetPscuHeader();      ph = eh->GetPscuHeader();
864      pktlast = ph->GetCounter();      pktlast = ph->GetCounter();
865      nevent = upperentry - chminentry;      nevent = upperentry - chminentry;
866        if ( ((Int_t)upperentry - (Int_t)chminentry) < 0 ) nevent = 0; // nevent is UInt_t! 090112 [8RED: error -88]
867        if ( IsDebug() ) printf(" Setting nevent to %u - upperentry %i chminentry %i \n",nevent,upperentry,chminentry);
868      //      //
869    } else {    } else {
870      upperpkt = PKT(pktlast);      upperpkt = PKT(pktlast);
# Line 799  Int_t PamelaDBOperations::SetUpperLimits Line 873  Int_t PamelaDBOperations::SetUpperLimits
873    };    };
874    //    //
875    if ( chewbacca && nevent < 1 ) {    if ( chewbacca && nevent < 1 ) {
876        if ( IsDebug() ) printf(" chewbacca and no events in the file \n");
877      pktfirst = chpktmin;      pktfirst = chpktmin;
878      upperpkt = PKT(chpktmax);      upperpkt = PKT(chpktmax);
879      pktlast = chpktmax;      pktlast = chpktmax;
# Line 824  Int_t PamelaDBOperations::SetUpperLimits Line 899  Int_t PamelaDBOperations::SetUpperLimits
899    //  if ( nevent < jump ) jump = int(nevent/10);    //  if ( nevent < jump ) jump = int(nevent/10);
900    //  if ( !jump ) jump = 1;    //  if ( !jump ) jump = 1;
901    //    //
902    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) && nevent > deltapkt ){  
903      if ( ((PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt)) && nevent > deltapkt ){
904      //      //
905      if ( IsDebug() ) printf(" starting jump %i \n",jump);      if ( IsDebug() ) printf(" starting jump %i \n",jump);
906      //    if ( IsDebug() ) printf(" (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) %llu < %llu && %llu > %llu \n     OR \n     (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) %llu > %llu && %llu < %llu \n",PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst),PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst));      //    if ( IsDebug() ) printf(" (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) %llu < %llu && %llu > %llu \n     OR \n     (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) %llu > %llu && %llu < %llu \n",PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst),PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst));
# Line 1471  void PamelaDBOperations::FillClass(Bool_ Line 1547  void PamelaDBOperations::FillClass(Bool_
1547      glrun->SetRUNTRAILER_PKT(lastPkt);      glrun->SetRUNTRAILER_PKT(lastPkt);
1548      //          //    
1549    };    };
1550      //
1551      // ad hoc solution for INF EV_TO [8RED] 090113
1552      //
1553      if ( !firstev && lastev == -1 ){
1554        if ( IsDebug() ) printf(" EE firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1555        firstev = 1;
1556        lastev = 0;
1557      };
1558    glrun->SetEV_FROM((UInt_t)firstev);    glrun->SetEV_FROM((UInt_t)firstev);
1559    glrun->SetEV_TO((UInt_t)lastev);    glrun->SetEV_TO((UInt_t)lastev);
1560    glrun->SetNEVENTS((UInt_t)lastev-(UInt_t)firstev+1);    glrun->SetNEVENTS((UInt_t)lastev-(UInt_t)firstev+1);
# Line 1577  Int_t PamelaDBOperations::insertPamelaGL Line 1661  Int_t PamelaDBOperations::insertPamelaGL
1661      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
1662      if ( !result ) throw -10;      if ( !result ) throw -10;
1663      row = result->Next();      row = result->Next();
1664        if ( !row ) throw -92;
1665      idresof = (UInt_t)atoll(row->GetField(0));      idresof = (UInt_t)atoll(row->GetField(0));
1666      existsts = true;      existsts = true;
1667      goto eout;      goto eout;
# Line 1598  Int_t PamelaDBOperations::insertPamelaGL Line 1683  Int_t PamelaDBOperations::insertPamelaGL
1683      if ( !row ){      if ( !row ){
1684        oss.str("");        oss.str("");
1685        oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "        oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
1686            << dworbit << " order by FROM_ORBIT desc limit 1;";            << dworbit << " AND SPECIAL_FILE='' order by FROM_ORBIT desc limit 1;";
1687        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1688        result = conn->Query(oss.str().c_str());        result = conn->Query(oss.str().c_str());
1689        if ( !result ) throw -10;        if ( !result ) throw -10;
# Line 1645  Int_t PamelaDBOperations::insertPamelaGL Line 1730  Int_t PamelaDBOperations::insertPamelaGL
1730      TYPE = 0;      TYPE = 0;
1731      TSYNC = 0;      TSYNC = 0;
1732      //      //
1733      Double_t minimum = 0.;      //    Double_t minimum = 0.;
1734      Double_t maximum = 0.;      // Double_t maximum = 0.;
1735      Double_t minimum2 = 0.;      //Double_t minimum2 = 0.;
1736      Double_t maximum2 = 0.;      Double_t maximum2 = 0.;
1737      //      //
1738      //      //
# Line 1655  Int_t PamelaDBOperations::insertPamelaGL Line 1740  Int_t PamelaDBOperations::insertPamelaGL
1740      pamela::McmdRecord *mcrc = 0;      pamela::McmdRecord *mcrc = 0;
1741      TArrayC *mcmddata = 0;      TArrayC *mcmddata = 0;
1742      //      //
1743      minimum = numeric_limits<Double_t>::max();      //    minimum = numeric_limits<Double_t>::max();
1744      maximum = numeric_limits<Double_t>::min();      //    maximum = numeric_limits<Double_t>::min();
1745      minimum2 = numeric_limits<Double_t>::max();      //    minimum2 = numeric_limits<Double_t>::max();
1746      maximum2 = numeric_limits<Double_t>::min();      maximum2 = numeric_limits<Double_t>::min();
1747      //      //
1748      T = (TTree*)file->Get("Mcmd");      T = (TTree*)file->Get("Mcmd");
# Line 2185  Int_t PamelaDBOperations::insertPamelaRU Line 2270  Int_t PamelaDBOperations::insertPamelaRU
2270    //    //
2271    // no runtrailers in the file!    // no runtrailers in the file!
2272    //    //
2273    if ( !rtev ){    if ( IsDebug() ) printf(" Start rtev %i rhev %i nrtev %i nrhev %i \n",rtev,rhev,nrtev,nrhev);
2274      if ( !rhev ){    if ( !rtev || (chewbacca && !nrtev) ){
2275        if ( IsDebug() ) printf(" No runtrailers \n");
2276        if ( !rhev || (chewbacca && !nrhev) ){
2277        if ( IsDebug() ) printf(" No runheaders nor runtrailers!! \n");        if ( IsDebug() ) printf(" No runheaders nor runtrailers!! \n");
2278        if ( !(upperentry-chminentry) ){        if ( !(upperentry-chminentry) ){
2279          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 2310  Int_t PamelaDBOperations::insertPamelaRU
2310      //      //
2311    } else {    } else {
2312      //      //
2313        if ( IsDebug() ) printf(" We have runtrailers \n");
2314      Int_t conptt = -1;      Int_t conptt = -1;
2315      for (Int_t ptt=0; ptt<rtev; ptt++){      for (Int_t ptt=0; ptt<rtev; ptt++){
2316        //        //
# Line 2239  Int_t PamelaDBOperations::insertPamelaRU Line 2327  Int_t PamelaDBOperations::insertPamelaRU
2327          ptht = cod->Get(pctp->RunHeader) - 1;          ptht = cod->Get(pctp->RunHeader) - 1;
2328          //      evbeft = cod->Get(pctp->Physics);          //      evbeft = cod->Get(pctp->Physics);
2329          tcod = (UInt_t)cod->Get(pctp->Physics);          tcod = (UInt_t)cod->Get(pctp->Physics);
2330          if ( !tcod ) tcod = 1;          if ( !tcod ) tcod = 1;    // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2331          evbeft = TMath::Min(upperentry,(tcod-1));          evbeft = TMath::Min(upperentry,(tcod-1));
2332          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));
2333          //          //
# Line 2278  Int_t PamelaDBOperations::insertPamelaRU Line 2366  Int_t PamelaDBOperations::insertPamelaRU
2366            obth = phh->GetOrbitalTime();            obth = phh->GetOrbitalTime();
2367            cod = ehh->GetCounter();            cod = ehh->GetCounter();
2368            tcod = (UInt_t)cod->Get(pctp->Physics);            tcod = (UInt_t)cod->Get(pctp->Physics);
2369            if ( !tcod ) tcod = 1;            if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2370            evbefh = TMath::Max(chminentry,(tcod-1));            evbefh = TMath::Max(chminentry,(tcod-1));
2371            //            //
2372            if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){            if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){
# Line 2340  Int_t PamelaDBOperations::insertPamelaRU Line 2428  Int_t PamelaDBOperations::insertPamelaRU
2428                cod = ehh->GetCounter();                cod = ehh->GetCounter();
2429                //            evbeft = cod->Get(pctp->Physics);                //            evbeft = cod->Get(pctp->Physics);
2430                tcod = (UInt_t)cod->Get(pctp->Physics);                tcod = (UInt_t)cod->Get(pctp->Physics);
2431                if ( !tcod ) tcod = 1;                if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2432                evbeft = TMath::Min(upperentry,(tcod-1));                evbeft = TMath::Min(upperentry,(tcod-1));
2433                rh->GetEntry(pth);                rh->GetEntry(pth);
2434                phh = ehh->GetPscuHeader();                phh = ehh->GetPscuHeader();
# Line 2382  Int_t PamelaDBOperations::insertPamelaRU Line 2470  Int_t PamelaDBOperations::insertPamelaRU
2470            //    if ( conptt+1 == (nrtev+nrtbef )){              //    if ( conptt+1 == (nrtev+nrtbef )){  
2471            if ( IsDebug() ) printf(" >>>>>>>>>>> %i %u %i %u \n",ptht,rhev,nrtev,conptt);            if ( IsDebug() ) printf(" >>>>>>>>>>> %i %u %i %u \n",ptht,rhev,nrtev,conptt);
2472            ptht++;            ptht++;
2473            if ( ptht < rhev ){            if ( ptht < rhev ){
2474              rh->GetEntry(ptht);              rh->GetEntry(ptht);
2475              //pth++;              //pth++;
2476              //if ( pth < rhev ){              //if ( pth < rhev ){
# Line 2410  Int_t PamelaDBOperations::insertPamelaRU Line 2498  Int_t PamelaDBOperations::insertPamelaRU
2498              if ( (UInt_t)evbeft < upperentry-1 && upperentry>0 ){              if ( (UInt_t)evbeft < upperentry-1 && upperentry>0 ){
2499                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);
2500                //                //
2501                if ( (ptt-1) < 0 ) throw -15; // should never arrive here!                //              if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
2502                  if ( (rtev-1) < 0 || ptt < 0 ) throw -15; // should never arrive here!
2503                //              rt->GetEntry(ptt-1);                //              rt->GetEntry(ptt-1);
2504                rt->GetEntry(rtev-1);                rt->GetEntry(rtev-1);
2505                cod = eht->GetCounter();                cod = eht->GetCounter();
# Line 2645  void PamelaDBOperations::HandleRunFragme Line 2734  void PamelaDBOperations::HandleRunFragme
2734      //      //
2735      // 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
2736      //      //
2737      if ( rhfirstev != firstev && !mishead ) mishead = true;      if ( firstev != (lastev+1) ){ // could be a problem sometimes (?)
2738      if ( rtlastev != lastev && !mistrail ) mistrail = true;        if ( rhfirstev != firstev && !mishead ) mishead = true;
2739          if ( rtlastev != lastev && !mistrail ) mistrail = true;
2740        };
2741      //      //
2742        if ( IsDebug() ) printf(" bhere firstev is %i lastev is %i \n",firstev,lastev);
2743      this->FillClass(mishead,mistrail,firstev,lastev);      this->FillClass(mishead,mistrail,firstev,lastev);
2744        if ( IsDebug() ) printf(" chere firstev is %i lastev is %i \n",firstev,lastev);
2745      //      //
2746      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");
2747      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 2783  void PamelaDBOperations::HandleRunFragme
2783        glrun->Fill_GL_RUN_FRAGMENTS(conn);        glrun->Fill_GL_RUN_FRAGMENTS(conn);
2784        //        //
2785      } else {      } else {
2786        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);
2787        if ( PEDANTIC ) throw -69;        if ( !RELAXED ){
2788        return;          if ( PEDANTIC ) throw -69;
2789            return;
2790          } else {
2791            glrun->SetID((UInt_t)atoll(row->GetField(0)));
2792            glrun->SetID_RUN_FRAG(0);
2793          };
2794      };      };
2795      //      //
2796      if ( chewbacca && mishead && mistrail ) goto justcheck;      if ( chewbacca && mishead && mistrail ) goto justcheck;
# Line 2743  void PamelaDBOperations::HandleRunFragme Line 2841  void PamelaDBOperations::HandleRunFragme
2841          //          //
2842          found = false; // default value          found = false; // default value
2843          //          //
2844          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");
2845          //          //
2846          // 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:
2847          //          //
# Line 2759  void PamelaDBOperations::HandleRunFragme Line 2857  void PamelaDBOperations::HandleRunFragme
2857              if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);              if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2858              //              //
2859              found = true;              found = true;
2860                if ( IsDebug() ) printf(" where firstev is %i lastev is %i \n",firstev,lastev);
2861              //              //
2862            } else {            } else {
2863              //              //
# Line 2824  void PamelaDBOperations::HandleRunFragme Line 2923  void PamelaDBOperations::HandleRunFragme
2923          oss << " ID="<<row->GetField(0)<<";";          oss << " ID="<<row->GetField(0)<<";";
2924          //          //
2925          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
2926            if ( IsDebug() ) printf(" there firstev is %i lastev is %i \n",firstev,lastev);
2927          //          //
2928          // merge infos          // merge infos
2929          //          //
# Line 2854  void PamelaDBOperations::HandleRunFragme Line 2954  void PamelaDBOperations::HandleRunFragme
2954          glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());          glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
2955          glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());          glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
2956          //          //
2957            if ( IsDebug() ) printf(" here firstev is %i lastev is %i \n",firstev,lastev);
2958            //
2959          glrun->SetEV_FROM(firstev);          glrun->SetEV_FROM(firstev);
2960          glrun->SetNEVENTS(lastev-firstev+1);          glrun->SetNEVENTS(lastev-firstev+1);
2961          //          //
# Line 2946  void PamelaDBOperations::HandleRunFragme Line 3048  void PamelaDBOperations::HandleRunFragme
3048          //          //
3049          found = false; // default value          found = false; // default value
3050          //          //
3051          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");
3052          //          //
3053          // 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:
3054          //          //
# Line 3046  void PamelaDBOperations::HandleRunFragme Line 3148  void PamelaDBOperations::HandleRunFragme
3148            apkt = PKT(ph->GetCounter());            apkt = PKT(ph->GetCounter());
3149            aobt = OBT(ph->GetOrbitalTime());                aobt = OBT(ph->GetOrbitalTime());    
3150            lastev--;            lastev--;
3151            if ( PEDANTIC ) throw -72;            if ( PEDANTIC && !RELAXED) throw -72;
3152          };          };
3153          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);
3154          //          //
# Line 3114  void PamelaDBOperations::HandleRunFragme Line 3216  void PamelaDBOperations::HandleRunFragme
3216        // 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?
3217        //        //
3218        oss.str("");        oss.str("");
3219        oss << " SELECT ID FROM GL_RUN WHERE "        oss << " SELECT ID,ID_ROOT_L0 FROM GL_RUN WHERE "
3220            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3221            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3222            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
# Line 3137  void PamelaDBOperations::HandleRunFragme Line 3239  void PamelaDBOperations::HandleRunFragme
3239        row = result->Next();        row = result->Next();
3240        //        //
3241        if ( row ){        if ( row ){
3242          if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");          //
3243          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
3244            // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number.
3245            //
3246            // 090112 [8RED (-70): RUN ALREADY INSERTED]
3247            // 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!
3248            //
3249            Bool_t OK = false;
3250            UInt_t IDRL2A = 0;
3251            UInt_t IDRL2B = 0;
3252            if ( chewbacca ){
3253              if ( result->GetRowCount() >= 2 ){
3254                //
3255                if ( IsDebug() ) printf(" RH---| gap |---RT case, number of pieces in the GL_RUN table: %i \n",result->GetRowCount());
3256                OK = true;
3257                IDRL2A = (UInt_t)atoll(row->GetField(0));
3258                //
3259                while ( row ){
3260                  //
3261                  TSQLResult *trresult = 0;
3262                  TSQLRow    *trrow    = 0;
3263                  //
3264                  stringstream tross;
3265                  tross.str("");
3266                  //
3267                  tross << " SELECT RUNHEADER_PKT, RUNTRAILER_PKT FROM GL_RUN_TRASH where ID=" << row->GetField(0) << ";";
3268                  if ( IsDebug() ) printf(" check in the gl_run_trash table for pkt intervals: query is \n %s \n",tross.str().c_str());
3269                  trresult = conn->Query(tross.str().c_str());
3270                  if ( !trresult ) throw -4;
3271                  trrow = trresult->Next();
3272                  if ( !trrow || trresult->GetRowCount() != 1 ){
3273                    OK = false;
3274                    if ( IsDebug() ) printf(" OPS! no such run (or multiple runs!) in GL_RUN_TRASH table, something wrong is going on! \n");
3275                    break;
3276                  };
3277                  //
3278                  UInt_t pktH =  (UInt_t)atoll(row->GetField(0));
3279                  UInt_t pktT =  (UInt_t)atoll(row->GetField(1));
3280                  delete trresult;
3281                  if (
3282                      (PKT(pktH) >= PKT(glrun->GetRUNHEADER_PKT()) && PKT(pktH) <= PKT(glrun->GetRUNTRAILER_PKT())) ||
3283                      (PKT(pktT) >= PKT(glrun->GetRUNHEADER_PKT()) && PKT(pktT) <= PKT(glrun->GetRUNTRAILER_PKT())) ||
3284                      (PKT(pktH) <= PKT(glrun->GetRUNTRAILER_PKT()) && PKT(pktT) >= PKT(glrun->GetRUNTRAILER_PKT())) ){            
3285                    OK = false;
3286                    if ( IsDebug() ) printf(" The run is overlapping with already inserted ones!! \n");
3287                    break;          
3288                  };
3289                  //PKT(glrun->GetRUNHEADER_PKT())  PKT(glrun->GetRUNTRAILER_PKT())
3290                  row = result->Next();
3291                };
3292                
3293                //    if ( result->GetRowCount() == 2 ) {
3294                //      IDRL2A = (UInt_t)atoll(row->GetField(0));
3295                //      UInt_t IDRL0A = (UInt_t)atoll(row->GetField(1));
3296                //      row = result->Next();
3297                //      IDRL2B = (UInt_t)atoll(row->GetField(0));
3298                //      UInt_t IDRL0B = (UInt_t)atoll(row->GetField(1));
3299                //      if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B);
3300                //      //      if ( IDRL0A == IDRL0B ){ //091230 why this condition???? RH could be in a file and RT in another one...
3301                //      if ( true ){
3302                //        TSQLResult *result2 = 0;
3303                //        TSQLRow    *row2    = 0;
3304                //        oss.str("");
3305                //        oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;";
3306                //        if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3307                //        result2 = conn->Query(oss.str().c_str());
3308                //        //
3309                //        if ( !result2 ) throw -4;
3310                //        //
3311                //        row2 = result2->Next();
3312                //        //
3313                //        if ( row2 ){
3314                //          UInt_t PKA = (UInt_t)atoll(row2->GetField(0));
3315                //          delete result2;
3316                //          oss.str("");
3317                //          oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;";
3318                //          if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3319                //          result2 = conn->Query(oss.str().c_str());
3320                //          //
3321                //          if ( !result2 ) throw -4;
3322                //          //
3323                //          row2 = result2->Next();
3324                //          //
3325                //          if ( row2 ){
3326                //            UInt_t PKB = (UInt_t)atoll(row2->GetField(0));
3327                //            //
3328                //            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);
3329                //            if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){
3330                //              if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
3331                //              OK = true;
3332                //            };
3333                //          };
3334                //        };                  
3335                //      };
3336                //    };      
3337              };
3338            };
3339            if ( OK ){
3340              //
3341              // 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  
3342              //
3343              // 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 )
3344              //
3345              if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
3346              //
3347              GL_RUN *glA = new GL_RUN();
3348              glA->Query_GL_RUN(IDRL2A,conn);
3349              //
3350              glrun->SetRUNHEADER_TIME(glA->GetRUNHEADER_TIME());
3351              glrun->SetRUNHEADER_OBT(glA->GetRUNHEADER_OBT());
3352              glrun->SetRUNHEADER_PKT(glA->GetRUNHEADER_PKT());
3353              //
3354              glrun->SetRUNTRAILER_TIME(glA->GetRUNTRAILER_TIME());
3355              glrun->SetRUNTRAILER_OBT(glA->GetRUNTRAILER_OBT());
3356              glrun->SetRUNTRAILER_PKT(glA->GetRUNTRAILER_PKT());
3357              //
3358              if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glA->GetPHYSENDRUN_MASK_S3S2S12());
3359              if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glA->GetPHYSENDRUN_MASK_S11CRC());
3360              //          
3361              if ( glA->GetACQ_BUILD_INFO() != 0 ){
3362                //
3363                // the first piece contains a good runheader we can update all the other runs with correct infos!
3364                //
3365                glrun->SetCOMPILATIONTIMESTAMP(glA->GetCOMPILATIONTIMESTAMP());
3366                glrun->SetFAV_WRK_SCHEDULE(glA->GetFAV_WRK_SCHEDULE());
3367                glrun->SetEFF_WRK_SCHEDULE(glA->GetEFF_WRK_SCHEDULE());
3368                glrun->SetPRH_VAR_TRG_MODE_A(glA->GetPRH_VAR_TRG_MODE_A());
3369                glrun->SetPRH_VAR_TRG_MODE_B(glA->GetPRH_VAR_TRG_MODE_B());
3370                glrun->SetACQ_BUILD_INFO(glA->GetACQ_BUILD_INFO());
3371                glrun->SetACQ_VAR_INFO(glA->GetACQ_VAR_INFO());
3372                glrun->SetRM_ACQ_AFTER_CALIB(glA->GetRM_ACQ_AFTER_CALIB());
3373                glrun->SetRM_ACQ_SETTING_MODE(glA->GetRM_ACQ_SETTING_MODE());
3374                glrun->SetTRK_CALIB_USED(glA->GetTRK_CALIB_USED());
3375                glrun->SetCAL_DSP_MASK(glA->GetCAL_DSP_MASK());
3376                glrun->SetLAST_TIMESYNC(glA->GetLAST_TIMESYNC());
3377                glrun->SetOBT_TIMESYNC(glA->GetOBT_TIMESYNC());
3378                //
3379              };
3380              //
3381              if ( glA->GetPKT_READY_COUNTER() != 0 ){
3382                //
3383                // the first piece contains a good runtrailer we can update all the other runs with correct infos!
3384                //
3385                glrun->SetPKT_COUNTER(glA->GetPKT_COUNTER());
3386                glrun->SetPKT_READY_COUNTER(glA->GetPKT_READY_COUNTER());
3387              };
3388              //
3389              // update runheader ROOT_ID_FRAG
3390              //
3391              oss.str("");
3392              oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
3393              if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3394              conn->Query(oss.str().c_str());
3395              //
3396              oss.str("");
3397              oss << "SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG = " << IDRL2A << ";";
3398              if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3399              TSQLResult *tsresult = 0;
3400              TSQLRow    *tsrow    = 0;
3401              tsresult=conn->Query(oss.str().c_str());
3402              if ( !tsresult ) throw -4;
3403              tsrow = tsresult->Next();
3404              if ( !tsrow || tsresult->GetRowCount() != 1 ) throw -4;
3405              IDRL2B = (UInt_t)atoll(tsrow->GetField(0));
3406              glrun->SetID_RUN_FRAG(IDRL2B);
3407              //
3408              // fill the new run in GL_RUN
3409              //
3410              glrun->Fill_GL_RUN(conn);    
3411              //
3412              glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3413              delete glA;
3414              //
3415            } else {
3416    //        //
3417    //        // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table
3418    //        //
3419    //        // 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 )
3420    //        //
3421    //        GL_RUN *glA = new GL_RUN();
3422    //        glA->Query_GL_RUN(IDRL2A,conn);
3423    //        //
3424    //        if ( glA->GetACQ_BUILD_INFO() != 0 ){
3425    //          //
3426    //          // the first piece contains a good runheader we can update all the other runs with correct infos!
3427    //          //
3428    //          oss.str("");
3429    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3430    //              << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "
3431    //              << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3432    //              << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , "
3433    //              << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , "
3434    //              << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , "
3435    //              << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , "
3436    //              << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , "
3437    //              << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , "
3438    //              << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , "
3439    //              << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , "
3440    //              << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , "  
3441    //              << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , "  
3442    //              << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , "  
3443    //              << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , "  
3444    //              << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , ";
3445    //          //
3446    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3447    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
3448    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3449    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , ";
3450    //          //        
3451    //          oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC();
3452    //          oss << " WHERE ID=" << glrun->GetID() << ";";
3453    //          if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str());
3454    //          conn->Query(oss.str().c_str());    
3455    //          //
3456    //        } else {
3457    //          //
3458    //          // sig no runheader, let set anyway what is possible...
3459    //          //
3460    //          oss.str("");
3461    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3462    //              << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , ";
3463    //          //
3464    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3465    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
3466    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3467    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
3468    //          //        
3469    //          oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3470    //              << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT();
3471    //          oss << " WHERE ID=" << glrun->GetID() << ";";
3472    //          if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str());
3473    //          conn->Query(oss.str().c_str());    
3474    //        };
3475    //        //
3476    //        // update runheader ROOT_ID_FRAG
3477    //        //
3478    //        oss.str("");
3479    //        oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
3480    //        if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3481    //        conn->Query(oss.str().c_str());
3482    //        //
3483    //        // now let's look for runtrailer if any in the last run
3484    //        //
3485    //        glA->Query_GL_RUN(IDRL2B,conn);
3486    //        //
3487    //        if ( glA->GetPKT_READY_COUNTER() != 0 ){
3488    //          //
3489    //          // the first piece contains a good runtrailer we can update all the other runs with correct infos!
3490    //          //
3491    //          oss.str("");
3492    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3493    //              << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3494    //              << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "
3495    //              << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , "
3496    //              << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , ";
3497    //          //
3498    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3499    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3500    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) {
3501    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3502    //          //        
3503    //          oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER()
3504    //              << " WHERE ID=" << glrun->GetID() << ";";
3505    //          if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str());
3506    //          conn->Query(oss.str().c_str());    
3507    //          //
3508    //        } else {
3509    //          //
3510    //          // sig no runtrailer, let set anyway what is possible...
3511    //          //
3512    //          oss.str("");
3513    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3514    //              << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3515    //              << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , ";
3516    //            //
3517    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3518    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3519    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){
3520    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3521    //          //        
3522    //          oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()
3523    //              << " WHERE ID=" << glrun->GetID() << ";";
3524    //          if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str());
3525    //          conn->Query(oss.str().c_str());    
3526    //        };
3527    //        //
3528    //        UInt_t myi = glrun->GetID();
3529    //        oss.str("");
3530    //        oss << " ID= "<< myi;
3531    //        //
3532    //        glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
3533    //        //
3534    //        // fill the new run in GL_RUN
3535    //        //
3536    //        glrun->SetID_RUN_FRAG(IDRL2B);
3537    //        glrun->Fill_GL_RUN(conn);    
3538    //        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3539    //        delete glA;
3540    //        //
3541    //      } else {
3542              //
3543              // is just a repetition
3544              //
3545              if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");        
3546              if ( PEDANTIC ) throw -70;
3547            };
3548            //
3549        } else {        } else {
3550          if ( NoFrag() ){          if ( NoFrag() ){
3551            glrun->SetID_RUN_FRAG(glrun->GetID());            glrun->SetID_RUN_FRAG(glrun->GetID());
# Line 3175  void PamelaDBOperations::HandleMissingHo Line 3582  void PamelaDBOperations::HandleMissingHo
3582        glrun->SetID(this->AssignRunID());        glrun->SetID(this->AssignRunID());
3583        glrun->SetID_RUN_FRAG(0);        glrun->SetID_RUN_FRAG(0);
3584        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...
3585      };        };
3586      //      //
3587    };    };
3588    //    //
# Line 3191  Bool_t PamelaDBOperations::IsRunConsiste Line 3598  Bool_t PamelaDBOperations::IsRunConsiste
3598    //      //  
3599    EventCounter *code=0;    EventCounter *code=0;
3600    //    //
3601    UInt_t nevent = 0;    //  UInt_t nevent = 0;
3602    UInt_t checkfirst = 0;    UInt_t checkfirst = 0;
3603    UInt_t checklast = 0;    UInt_t checklast = 0;
3604    UInt_t firstentry = 0;    UInt_t firstentry = 0;
# Line 3213  Bool_t PamelaDBOperations::IsRunConsiste Line 3620  Bool_t PamelaDBOperations::IsRunConsiste
3620    EventHeader *eh = 0;    EventHeader *eh = 0;
3621    PscuHeader *ph = 0;    PscuHeader *ph = 0;
3622    T->SetBranchAddress("Header", &eh);    T->SetBranchAddress("Header", &eh);
3623    nevent = T->GetEntries();    //  nevent = T->GetEntries();
3624    //    //
3625    //    //
3626    if ( firstev == lastev+1 || lastev == firstev ) { // no events inside the run!    if ( firstev == lastev+1 || lastev == firstev ) { // no events inside the run!
3627      //if ( firstev <= lastev+1 ) { // no events inside the run!      //if ( firstev <= lastev+1 ) { // no events inside the run!
3628      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
3629        firstev = lastev+1; // this is necessary for files with no Physics entries, should have no influence on other files
3630      // return true is correct      // return true is correct
3631      return(true);      return(true);
3632      //      //
# Line 3474  Bool_t PamelaDBOperations::IsRunConsiste Line 3882  Bool_t PamelaDBOperations::IsRunConsiste
3882                  //                  //
3883                  found = false; // default value                  found = false; // default value
3884                  //                  //
3885                  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");
3886                  //                  //
3887                  // 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:
3888                  //                  //
# Line 3733  void PamelaDBOperations::HandleSuspiciou Line 4141  void PamelaDBOperations::HandleSuspiciou
4141    EventCounter *code=0;    EventCounter *code=0;
4142    UInt_t firstev = 0;    UInt_t firstev = 0;
4143    UInt_t lastev = 0;    UInt_t lastev = 0;
4144    UInt_t nevent = 0;    //  UInt_t nevent = 0;
4145    UInt_t checkfirst = 0;    UInt_t checkfirst = 0;
4146    UInt_t checklast = 0;    UInt_t checklast = 0;
4147    UInt_t firstentry = 0;    UInt_t firstentry = 0;
# Line 3758  void PamelaDBOperations::HandleSuspiciou Line 4166  void PamelaDBOperations::HandleSuspiciou
4166    EventHeader *eh = 0;    EventHeader *eh = 0;
4167    PscuHeader *ph = 0;    PscuHeader *ph = 0;
4168    T->SetBranchAddress("Header", &eh);    T->SetBranchAddress("Header", &eh);
4169    nevent = T->GetEntries();    //  nevent = T->GetEntries();
4170    //    //
4171    codt = eht->GetCounter();    codt = eht->GetCounter();
4172    codh = ehh->GetCounter();    codh = ehh->GetCounter();
# Line 3992  Int_t PamelaDBOperations::insertCALO_CAL Line 4400  Int_t PamelaDBOperations::insertCALO_CAL
4400    UInt_t totime = 0;    UInt_t totime = 0;
4401    UInt_t obt = 0;    UInt_t obt = 0;
4402    UInt_t pkt = 0;    UInt_t pkt = 0;
4403      Float_t totped = 0.;
4404    //    //
4405    tr = (TTree*)file->Get("CalibCalPed");    tr = (TTree*)file->Get("CalibCalPed");
4406    if ( !tr || tr->IsZombie() ) throw -21;    if ( !tr || tr->IsZombie() ) throw -21;
# Line 4009  Int_t PamelaDBOperations::insertCALO_CAL Line 4418  Int_t PamelaDBOperations::insertCALO_CAL
4418        if ( calibCalPed->cstwerr[section] ){        if ( calibCalPed->cstwerr[section] ){
4419          valid = 1;          valid = 1;
4420          if ( calibCalPed->cperror[section] ) valid = 0;          if ( calibCalPed->cperror[section] ) valid = 0;
4421            //
4422            // 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.]
4423            //
4424            totped = 0.;
4425            Int_t ns = 0;
4426            if ( section == 2 ) ns = 3;
4427            if ( section == 3 ) ns = 1;
4428            if ( section == 1 ) ns = 2;
4429            for (UInt_t ss=0; ss<96; ss++){
4430              totped += fabs(calibCalPed->calped[ns][0][ss]);
4431            }
4432            if ( totped < 1. ){
4433              if ( IsDebug() ) printf(" Section %i totped %f -  No calibration data! Calorimeter power problems? \n",section,totped);
4434              valid = 0;
4435            };
4436            //
4437          ph = eh->GetPscuHeader();          ph = eh->GetPscuHeader();
4438          obt = ph->GetOrbitalTime();            obt = ph->GetOrbitalTime();  
4439          pkt = ph->GetCounter();            pkt = ph->GetCounter();  
# Line 4176  Int_t PamelaDBOperations::insertCALOPULS Line 4601  Int_t PamelaDBOperations::insertCALOPULS
4601    tr->SetBranchAddress("Header", &eh);    tr->SetBranchAddress("Header", &eh);
4602    nevents = tr->GetEntries();    nevents = tr->GetEntries();
4603    //    //
4604      Float_t totpul = 0.;
4605      //
4606    if ( nevents > 0 ){    if ( nevents > 0 ){
4607      //      //
4608      for (UInt_t i=0; i < nevents; i++){      for (UInt_t i=0; i < nevents; i++){
# Line 4185  Int_t PamelaDBOperations::insertCALOPULS Line 4612  Int_t PamelaDBOperations::insertCALOPULS
4612          if ( cp1->pstwerr[section] && cp1->unpackError == 0 ){          if ( cp1->pstwerr[section] && cp1->unpackError == 0 ){
4613            valid = 1;            valid = 1;
4614            if ( cp1->pperror[section] ) valid = 0;            if ( cp1->pperror[section] ) valid = 0;
4615              //
4616              // 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.]
4617              //
4618              totpul = 0.;
4619              Int_t ns = 0;
4620              if ( section == 2 ) ns = 3;
4621              if ( section == 3 ) ns = 1;
4622              if ( section == 1 ) ns = 2;
4623              for (UInt_t ss=0; ss<96; ss++){
4624                totpul += cp1->calpuls[ns][0][ss];
4625              }
4626              if ( totpul >= 3145632. ){
4627                if ( IsDebug() ) printf(" PULSE1 Section %i totpul %f -  No calibration data! Calorimeter power problems? \n",section,totpul);
4628                valid = 0;
4629              };
4630              //
4631            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
4632            obt = ph->GetOrbitalTime();              obt = ph->GetOrbitalTime();  
4633            pkt = ph->GetCounter();              pkt = ph->GetCounter();  
# Line 4342  Int_t PamelaDBOperations::insertCALOPULS Line 4785  Int_t PamelaDBOperations::insertCALOPULS
4785          if ( cp2->pstwerr[section] && cp2->unpackError == 0 ){          if ( cp2->pstwerr[section] && cp2->unpackError == 0 ){
4786            valid = 1;            valid = 1;
4787            if ( cp2->pperror[section] ) valid = 0;            if ( cp2->pperror[section] ) valid = 0;
4788              //
4789              // 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.]
4790              //
4791              totpul = 0.;
4792              Int_t ns = 0;
4793              if ( section == 2 ) ns = 3;
4794              if ( section == 3 ) ns = 1;
4795              if ( section == 1 ) ns = 2;
4796              for (UInt_t ss=0; ss<96; ss++){
4797                totpul += cp2->calpuls[ns][0][ss];
4798              }
4799              if ( totpul >= 3145632. ){
4800                if ( IsDebug() ) printf(" PULSE2 Section %i totpul %f -  No calibration data! Calorimeter power problems? \n",section,totpul);
4801                valid = 0;
4802              };
4803              //
4804            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
4805            obt = ph->GetOrbitalTime();              obt = ph->GetOrbitalTime();  
4806            pkt = ph->GetCounter();              pkt = ph->GetCounter();  
# Line 4707  void PamelaDBOperations::HandleTRK_CALIB Line 5166  void PamelaDBOperations::HandleTRK_CALIB
5166      if ( !result ) throw -4;;      if ( !result ) throw -4;;
5167      row = result->Next();      row = result->Next();
5168      if(row)glcal->ID = (UInt_t)atoll(row->GetField(0));      if(row)glcal->ID = (UInt_t)atoll(row->GetField(0));
5169        //
5170        delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5171      //      //
5172  };  };
5173    
# Line 5067  Int_t PamelaDBOperations::insertTRK_CALI Line 5527  Int_t PamelaDBOperations::insertTRK_CALI
5527              TString path       = "";              TString path       = "";
5528              TString name       = "";              TString name       = "";
5529              TString raw        = "";              TString raw        = "";
5530              UInt_t obt0        = 0;              //      UInt_t obt0        = 0;
5531              UInt_t timesync    = 0;              //      UInt_t timesync    = 0;
5532              UInt_t boot_number = 0;              //      UInt_t boot_number = 0;
5533              bool   FOUND       = false;              bool   FOUND       = false;
5534    
5535              if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;              if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;
# Line 5142  Int_t PamelaDBOperations::insertTRK_CALI Line 5602  Int_t PamelaDBOperations::insertTRK_CALI
5602                      path       = (TString)row2->GetField(0);                      path       = (TString)row2->GetField(0);
5603                      name       = (TString)row2->GetField(1);                      name       = (TString)row2->GetField(1);
5604                      raw        = (TString)row2->GetField(5);                      raw        = (TString)row2->GetField(5);
5605                      obt0        = (UInt_t)atoll(row2->GetField(2));                      //              obt0        = (UInt_t)atoll(row2->GetField(2));
5606                      timesync    = (UInt_t)atoll(row2->GetField(3));                      //              timesync    = (UInt_t)atoll(row2->GetField(3));
5607                      boot_number = (UInt_t)atoll(row2->GetField(4));                      //              boot_number = (UInt_t)atoll(row2->GetField(4));
5608                                    
5609                      if ( IsDebug() ) cout << "- - - - - - - - - - -" <<endl;                      if ( IsDebug() ) cout << "- - - - - - - - - - -" <<endl;
5610  //                  cout << path <<endl;  //                  cout << path <<endl;
# Line 5215  Int_t PamelaDBOperations::insertTRK_CALI Line 5675  Int_t PamelaDBOperations::insertTRK_CALI
5675    
5676              if(FOUND){              if(FOUND){
5677    
5678              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  
                 ////////////////////////////////////////////  
5679    
5680                  // ---------------------------------------------------------------------                ////////////////////////////////////////////
5681                  // step 1: insert a new raw file in GL_RAW                // insert a new entry in GL_TRK_CALIB and
5682                  // ---------------------------------------------------------------------                // modify the time-tag of the previous one
5683                  //                ////////////////////////////////////////////
5684                  // check if the raw file already exist  
5685                  //                // ---------------------------------------------------------------------
5686                  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
5687  //              oss.str("");                // ---------------------------------------------------------------------
5688  //              oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";";                //
5689  //              if ( IsDebug() ) cout << oss.str().c_str() << endl;                // check if the raw file already exist
5690                  //
5691                  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
5692                  //                oss.str("");
5693                  //                oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";";
5694                  //                if ( IsDebug() ) cout << oss.str().c_str() << endl;
5695                                    
5696  //              result = conn->Query(oss.str().c_str());                //                result = conn->Query(oss.str().c_str());
5697  //              if ( !result ) throw -4;;                //                if ( !result ) throw -4;;
5698  //              if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;                //                if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5699  //              if( result->GetRowCount() == 0){                //                if( result->GetRowCount() == 0){
5700  //                  if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl;                //                    if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl;
5701  //                  // - - - - - - - - - - -                //                    // - - - - - - - - - - -
5702  //                  // insert new raw file                //                    // insert new raw file
5703  //                  // - - - - - - - - - - -                //                    // - - - - - - - - - - -
5704  //                  GL_RAW glraw = GL_RAW();                    //                    GL_RAW glraw = GL_RAW();    
5705  //                  glraw.PATH        = gSystem->DirName(raw.Data());                //                    glraw.PATH        = gSystem->DirName(raw.Data());
5706  //                  glraw.NAME        = gSystem->BaseName(raw.Data());                //                    glraw.NAME        = gSystem->BaseName(raw.Data());
5707  //                  glraw.BOOT_NUMBER = boot_number;                //                    glraw.BOOT_NUMBER = boot_number;
5708  //                  //                //                    //
5709  //                  insertPamelaRawFile( &glraw );                //                    insertPamelaRawFile( &glraw );
5710  //                  //                              //                    //              
5711  //                  id_raw = glraw.ID;                              //                    id_raw = glraw.ID;              
5712  //              }else{                //                }else{
5713  //                  row = result->Next();                //                    row = result->Next();
5714  //                  id_raw = (UInt_t)atoll(row->GetField(0));                //                    id_raw = (UInt_t)atoll(row->GetField(0));
5715  //              }                //                }
5716  //              if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl;                //                if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl;
5717    
5718                  // ---------------------------------------------------------------------                // ---------------------------------------------------------------------
5719                  // step 1(bis): retrieve the timesync id associated to the file                // step 1(bis): retrieve the timesync id associated to the file
5720                  // (NB, uso lo stesso associato al file iniziale)                // (NB, uso lo stesso associato al file iniziale)
5721                  // ---------------------------------------------------------------------                // ---------------------------------------------------------------------
5722                  UInt_t idtimesync = 0;                UInt_t idtimesync = 0;
5723                  oss.str("");                oss.str("");
5724                  oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";                oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";
5725                  if ( debug ) printf(" %s \n",oss.str().c_str());                if ( debug ) printf(" %s \n",oss.str().c_str());
5726                  result = conn->Query(oss.str().c_str());                result = conn->Query(oss.str().c_str());
5727                  if ( !result ) throw -3;                if ( !result ) throw -3;
5728                  row = result->Next();                  row = result->Next();
5729                  if ( !row ) throw -3;                  if ( !row ) throw -3;
5730                  idtimesync = (UInt_t)atoll(row->GetField(0));                  idtimesync = (UInt_t)atoll(row->GetField(0));
5731                  if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl;                  if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl;
5732                                    
5733                    delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5734                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5735                  // step 2: insert a new root file in GL_ROOT                  // step 2: insert a new root file in GL_ROOT
5736                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
# Line 5317  Int_t PamelaDBOperations::insertTRK_CALI Line 5778  Int_t PamelaDBOperations::insertTRK_CALI
5778                  }                  }
5779                  if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl;                  if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl;
5780    
5781                    delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5782                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5783                  // step 3: modify time-tag of corrupted GL_TRK_CALIB entry                  // step 3: modify time-tag of corrupted GL_TRK_CALIB entry
5784                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
# Line 5329  Int_t PamelaDBOperations::insertTRK_CALI Line 5791  Int_t PamelaDBOperations::insertTRK_CALI
5791                  result = conn->Query(oss.str().c_str());                  result = conn->Query(oss.str().c_str());
5792                  if ( !result ) throw -4;;                  if ( !result ) throw -4;;
5793    
5794                    delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5795    
5796                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5797                  // step 4: insert the new calibration:                  // step 4: insert the new calibration:
5798                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
# Line 5659  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6123  Int_t PamelaDBOperations::CleanGL_RUN_FR
6123      memset(myid,0,500*sizeof(UInt_t));      memset(myid,0,500*sizeof(UInt_t));
6124      //      //
6125      oss.str("");      oss.str("");
6126      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;";
6127      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());
6128      nresult = conn->Query(oss.str().c_str());      nresult = conn->Query(oss.str().c_str());
6129      //      //
# Line 5780  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6244  Int_t PamelaDBOperations::CleanGL_RUN_FR
6244                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6245                //                              //              
6246                oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "                oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "
6247                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< ";";                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT();
6248                  oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6249                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6250              };                  };    
6251            };            };
# Line 5829  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6294  Int_t PamelaDBOperations::CleanGL_RUN_FR
6294                if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){                if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){
6295                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6296                //                              //              
6297                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< ";";                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT();
6298                  oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6299                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6300              };                  };    
6301            };            };
# Line 5880  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6346  Int_t PamelaDBOperations::CleanGL_RUN_FR
6346                row2 = result2->Next();                row2 = result2->Next();
6347                //                //
6348                if ( row2 ){                if ( row2 ){
6349                  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");
6350                  if ( PEDANTIC ) throw -83;                  if ( PEDANTIC ) throw -83;
6351                  row2->Close();                  row2->Close();
6352                };                };
# Line 5957  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6423  Int_t PamelaDBOperations::CleanGL_RUN_FR
6423        glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);          glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
6424        //        //
6425        oss.str("");        oss.str("");
6426        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 "
6427            << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("            << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
6428            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
6429            << " 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 6481  Int_t PamelaDBOperations::CleanGL_RUN_FR
6481          moved++;          moved++;
6482          //          //
6483        } else {        } else {
6484          if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");  
6485          if ( PEDANTIC ) throw -83;          // questa parte mah mah mah... da controllare
6486    
6487            //
6488            // 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
6489            // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number.
6490            //
6491            // 090112 [8RED (-70): RUN ALREADY INSERTED]
6492            //
6493            Bool_t OK = false;
6494            UInt_t IDRL2A = 0;
6495            UInt_t IDRL2B = 0;
6496              if ( result2->GetRowCount() == 2 ){
6497                IDRL2A = (UInt_t)atoll(row2->GetField(0));
6498                UInt_t IDRL0A = (UInt_t)atoll(row2->GetField(4));
6499                row2 = result2->Next();
6500                IDRL2B = (UInt_t)atoll(row2->GetField(0));
6501                UInt_t IDRL0B = (UInt_t)atoll(row2->GetField(4));
6502                if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B);
6503                //      if ( IDRL0A == IDRL0B ){
6504    //            TSQLResult *result2a = 0;
6505    //            TSQLRow    *row2a    = 0;
6506    //            oss.str("");
6507    //            oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;";
6508    //            if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
6509    //            result2a = conn->Query(oss.str().c_str());
6510    //            //
6511    //            if ( !result2a ) throw -4;
6512    //            //
6513    //            row2a = result2a->Next();
6514    //            //
6515    //            if ( row2a ){
6516    //              UInt_t PKA = (UInt_t)atoll(row2a->GetField(0));
6517    //              delete result2a;
6518    //              oss.str("");
6519    //              oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;";
6520    //              if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
6521    //              result2a = conn->Query(oss.str().c_str());
6522    //              //
6523    //              if ( !result2a ) throw -4;
6524    //              //
6525    //              row2a = result2a->Next();
6526    //              //
6527    //              if ( row2a ){
6528    //                UInt_t PKB = (UInt_t)atoll(row2a->GetField(0));
6529    //                //
6530    //                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);
6531    //                if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){
6532    //                  if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
6533                        OK = true;
6534                        //            };
6535                        //          };
6536                        //        };                  
6537                        //    };
6538              };      
6539            if ( OK ){
6540              //
6541              // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table
6542              //
6543              // 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 )
6544              //
6545              GL_RUN *glA = new GL_RUN();
6546              glA->Query_GL_RUN(IDRL2A,conn);
6547              //
6548              if ( glA->GetACQ_BUILD_INFO() != 0 ){
6549                //
6550                // the first piece contains a good runheader we can update all the other runs with correct infos!
6551                //
6552                oss.str("");
6553                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6554                    << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "
6555                    << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
6556                    << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , "
6557                    << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , "
6558                    << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , "
6559                    << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , "
6560                    << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , "
6561                    << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , "
6562                    << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , "
6563                    << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , "
6564                    << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , "  
6565                    << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , "  
6566                    << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , "  
6567                    << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , "  
6568                    << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , ";
6569                //
6570                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
6571                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
6572                if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
6573                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , ";
6574                //        
6575                oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC();
6576                oss << " WHERE ID=" << glrun->GetID() << ";";
6577                if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str());
6578                conn->Query(oss.str().c_str());    
6579                //
6580              } else {
6581                //
6582                // sig no runheader, let set anyway what is possible...
6583                //
6584                oss.str("");
6585                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6586                    << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , ";
6587                //
6588                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
6589                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
6590                if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
6591                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6592                //        
6593                oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
6594                    << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT();
6595                oss << " WHERE ID=" << glrun->GetID() << ";";
6596                if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str());
6597                conn->Query(oss.str().c_str());    
6598              };
6599              //
6600              // update runheader ROOT_ID_FRAG
6601              //
6602              oss.str("");
6603              oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
6604              if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
6605              conn->Query(oss.str().c_str());
6606              //
6607              // now let's look for runtrailer if any in the last run
6608              //
6609              glA->Query_GL_RUN(IDRL2B,conn);
6610              //
6611              if ( glA->GetPKT_READY_COUNTER() != 0 ){
6612                //
6613                // the first piece contains a good runtrailer we can update all the other runs with correct infos!
6614                //
6615                oss.str("");
6616                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6617                    << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
6618                    << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "
6619                    << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , "
6620                    << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , ";
6621                //
6622                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
6623                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6624                if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) {
6625                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6626                //        
6627                oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER()
6628                    << " WHERE ID=" << glrun->GetID() << ";";
6629                if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str());
6630                conn->Query(oss.str().c_str());    
6631                //
6632              } else {
6633                //
6634                // sig no runtrailer, let set anyway what is possible...
6635                //
6636                oss.str("");
6637                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6638                    << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
6639                    << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , ";
6640                  //
6641                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
6642                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6643                if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){
6644                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6645                //        
6646                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()
6647                    << " WHERE ID=" << glrun->GetID() << ";";
6648                if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str());
6649                conn->Query(oss.str().c_str());    
6650              };
6651              //
6652              UInt_t myi = glrun->GetID();
6653              oss.str("");
6654              oss << " ID= "<< myi;
6655              //
6656              glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
6657              //
6658              // fill the new run in GL_RUN
6659              //
6660              glrun->SetID_RUN_FRAG(IDRL2B);
6661              glrun->Fill_GL_RUN(conn);    
6662              glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
6663              delete glA;
6664              moved++;
6665              //
6666            } else {
6667              //
6668              // is just a repetition
6669              //
6670              if ( IsDebug() ) printf(" The run %u is already present in the GL_RUN table...\n",glrun->GetID());    
6671              //      printf(" CCCCCCCCICCCCCCCCCCCCIOOOOOOOOOO si muove Ciccio! %u \n",glrun->GetID());
6672              //      if ( PEDANTIC && glrun->GetID() != 61563 ) throw -83;
6673              if ( PEDANTIC ) throw -83;
6674            };
6675            //      if ( IsDebug() ) printf(" The run already exists in the GL_RUN table! ...\n");
6676            //      if ( PEDANTIC ) throw -83;
6677        };        };
6678        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));      
6679        //        //

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

  ViewVC Help
Powered by ViewVC 1.1.23