/[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.4 by pam-fi, Fri Oct 31 11:21:44 2008 UTC revision 1.40 by mocchiut, Tue Oct 14 13:19:32 2014 UTC
# Line 58  string getTleDatetime(cTle*); Line 58  string getTleDatetime(cTle*);
58   * @param debug        debug flag.   * @param debug        debug flag.
59   * @param tlefilename  ascii file with TLE 3 line elements.   * @param tlefilename  ascii file with TLE 3 line elements.
60   */   */
61  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename, UInt_t dwinput, Bool_t staticp, Bool_t gpamela){  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename, UInt_t dwinput, Bool_t staticp, Bool_t gpamela, Bool_t keepenv){
62    //    //
63      RELAXED = false;
64    chewbacca = false;    chewbacca = false;
65    chminentry = 0;    chminentry = 0;
66    chID = 0;    chID = 0;
# Line 76  PamelaDBOperations::PamelaDBOperations(T Line 77  PamelaDBOperations::PamelaDBOperations(T
77      //      //
78    };    };
79    //    //
80      KEEPENV = false;
81      if ( keepenv ) KEEPENV=true;
82    STATIC=false;    STATIC=false;
83    if ( staticp ) STATIC=true;    if ( staticp ) STATIC=true;
84    //    //
# Line 85  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 99  PamelaDBOperations::PamelaDBOperations(T Line 102  PamelaDBOperations::PamelaDBOperations(T
102    if (INSERT_RAW) SetRawName(filerawname);    if (INSERT_RAW) SetRawName(filerawname);
103    //    //
104    INSERT_ROOT = !filerootname.IsNull();    INSERT_ROOT = !filerootname.IsNull();
105      if ( INSERT_ROOT ) this->SetRootName(filerootname);
106    this->SetOrbitNo(dwinput);    this->SetOrbitNo(dwinput);
107    //    //
108    this->SetID_RAW(0);    this->SetID_RAW(0);
# Line 122  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 132  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 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 149  void PamelaDBOperations::OpenL0File(TStr Line 161  void PamelaDBOperations::OpenL0File(TStr
161            chpktmax = (UInt_t)atoll(row->GetField(2));            chpktmax = (UInt_t)atoll(row->GetField(2));
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));
165              chrtinit = (UInt_t)atoll(row->GetField(6));
166          } else {          } else {
167            throw -84;            throw -84;
168          };          };
# Line 157  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 ){
178          row2 = result2->Next();          row2 = result2->Next();
179          if ( row2 ){          if ( row2 ){
180            chpath = (TString)gSystem->ExpandPathName(row2->GetField(0))+'/';            if ( KEEPENV ){
181                chpath = (TString)(row2->GetField(0))+'/';
182              } else {
183                chpath = (TString)gSystem->ExpandPathName(row2->GetField(0))+'/';
184              };
185            chfile = (TString)(row2->GetField(1));            chfile = (TString)(row2->GetField(1));
186            chobtts = (UInt_t)atoll(row2->GetField(2));            chobtts = (UInt_t)atoll(row2->GetField(2));
187            chlastts = (UInt_t)atoll(row2->GetField(3));            chlastts = (UInt_t)atoll(row2->GetField(3));
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 182  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 388  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 435  TString PamelaDBOperations::GetRawPath() Line 533  TString PamelaDBOperations::GetRawPath()
533    
534  TString PamelaDBOperations::GetRootPath(){  TString PamelaDBOperations::GetRootPath(){
535    if ( STATIC ){    if ( STATIC ){
536      return((TString)gSystem->DirName(filerootname.Data())+'/');      return((TString)gSystem->DirName(this->GetRootName().Data())+'/');
537    } else {    } else {    
538      return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');      if ( KEEPENV ){
539          return((TString)gSystem->ExpandPathName(gSystem->DirName(filerootname.Data()))+'/');
540        } else {
541          return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');
542        };
543    };    };
544  };  };
545    
# Line 649  Int_t PamelaDBOperations::SetUpperLimits Line 751  Int_t PamelaDBOperations::SetUpperLimits
751      UInt_t tjump = 50000;      UInt_t tjump = 50000;
752      //UInt_t tjump = 100;      //UInt_t tjump = 100;
753      while ( tjump > 0 ){      while ( tjump > 0 ){
754        pktlast = numeric_limits<UInt_t>::max();        //      pktlast = numeric_limits<UInt_t>::max();
755        while ( pktlast > chpktmax && (Int_t)(nevent-1-it) >= 0 ){        pktlast = chpktmax + 1;
756          while ( PKT(pktlast) > PKT(chpktmax) && (Int_t)(nevent-1-it) >= 0 ){
757          if ( (Int_t)(nevent-1-it) >= 0 ){          if ( (Int_t)(nevent-1-it) >= 0 ){
758            T->GetEntry(nevent-1-it);            T->GetEntry(nevent-1-it);
759            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
# Line 658  Int_t PamelaDBOperations::SetUpperLimits Line 761  Int_t PamelaDBOperations::SetUpperLimits
761          } else {          } else {
762            pktlast = chpktmax + 1;            pktlast = chpktmax + 1;
763          };          };
764          if ( (!(it%1000) || abs((int)pktlast - (int)chpktmax)<1000 ) && debug ) printf(" look for up %i %i %i nevent %u (nevent-1-it) %i \n",it,pktlast,chpktmax,nevent,(Int_t)(nevent-1-it));          if ( (!(it%1000) || abs((int)(PKT(pktlast) - PKT(chpktmax)))<1000 ) && debug ) printf(" look for up %i %i %i nevent %u (nevent-1-it) %i \n",it,pktlast,chpktmax,nevent,(Int_t)(nevent-1-it));
765          it += tjump;          it += tjump;
766        };        };
767        if ( tjump > 1 ) it -= 2*tjump;        if ( tjump > 1 ) it -= 2*tjump;
# Line 682  Int_t PamelaDBOperations::SetUpperLimits Line 785  Int_t PamelaDBOperations::SetUpperLimits
785      tjump = 50000;      tjump = 50000;
786      //tjump = 100;      //tjump = 100;
787      while ( tjump > 0 ){      while ( tjump > 0 ){
788        pktlast = 0;        //      pktlast = 0;
789        while ( pktlast < chpktmin && it < (Int_t)nevent ){        pktlast = chpktmin - 1;
790          if ( debug ) printf("LLlook for down %i %i %llu %llu \n",it,pktlast,PKT(pktlast),PKT(chpktmin));
791          while ( PKT(pktlast) < PKT(chpktmin) && it < (Int_t)nevent ){
792          if ( it < (Int_t)nevent ){          if ( it < (Int_t)nevent ){
793            T->GetEntry(it);            T->GetEntry(it);
794            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
# Line 777  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 785  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 810  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 1125  UInt_t PamelaDBOperations::AssignRunID() Line 1234  UInt_t PamelaDBOperations::AssignRunID()
1234    stringstream   oss;    stringstream   oss;
1235    //      //  
1236    oss.str("");    oss.str("");
1237    oss << "INSERT INTO _RUNID_GEN VALUES (NULL);";    if ( chewbacca ){// if chewbacca and tag=none then use chewbacca tag (chiby = chewbacca inserted by), if chewbacca and tag!=none then use tag, if not chewbacca use tag.
1238        if ( !strcmp(tag.Data(),"NONE") ){
1239          oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< chiby.Data() <<"');";
1240        } else {
1241          oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');";
1242        };
1243      } else {
1244        oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');";
1245      };
1246    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1247    if ( !result ) throw -10;    if ( !result ) throw -10;
1248    oss.str("");    oss.str("");
# Line 1256  void PamelaDBOperations::CheckConnection Line 1373  void PamelaDBOperations::CheckConnection
1373    oss.str("");    oss.str("");
1374    oss << "SET wait_timeout=173000;";    oss << "SET wait_timeout=173000;";
1375    conn->Query(oss.str().c_str());    conn->Query(oss.str().c_str());
1376      delete conn->Query("SET sql_mode = 'NO_UNSIGNED_SUBTRACTION';");
1377    //    //
1378  }  }
1379    
# Line 1330  Long64_t PamelaDBOperations::OBT(UInt_t Line 1448  Long64_t PamelaDBOperations::OBT(UInt_t
1448    //    //
1449    //  if ( IsDebug() ) printf(" obt conversion: obt is %u obtfirst is %u  (numeric_limits<UInt_t>::max()/2) is %u  \n",obt,pobtfirst,(UInt_t)(numeric_limits<UInt_t>::max()/2));    //  if ( IsDebug() ) printf(" obt conversion: obt is %u obtfirst is %u  (numeric_limits<UInt_t>::max()/2) is %u  \n",obt,pobtfirst,(UInt_t)(numeric_limits<UInt_t>::max()/2));
1450    //    //
1451    if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){    if ( obt < ((Long64_t)pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){
1452      //    if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());      //    if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());
1453      return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());      return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());
1454    };    };
# Line 1449  void PamelaDBOperations::FillClass(Bool_ Line 1567  void PamelaDBOperations::FillClass(Bool_
1567      glrun->SetRUNTRAILER_PKT(lastPkt);      glrun->SetRUNTRAILER_PKT(lastPkt);
1568      //          //    
1569    };    };
1570      //
1571      // ad hoc solution for INF EV_TO [8RED] 090113
1572      //
1573      if ( !firstev && lastev == -1 ){
1574        if ( IsDebug() ) printf(" EE firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1575        firstev = 1;
1576        lastev = 0;
1577      };
1578    glrun->SetEV_FROM((UInt_t)firstev);    glrun->SetEV_FROM((UInt_t)firstev);
1579    glrun->SetEV_TO((UInt_t)lastev);    glrun->SetEV_TO((UInt_t)lastev);
1580    glrun->SetNEVENTS((UInt_t)lastev-(UInt_t)firstev+1);    glrun->SetNEVENTS((UInt_t)lastev-(UInt_t)firstev+1);
# Line 1464  void PamelaDBOperations::FillClass(Bool_ Line 1590  void PamelaDBOperations::FillClass(Bool_
1590  // PUBLIC FUNCTIONS  // PUBLIC FUNCTIONS
1591  //  //
1592    
 // /**  
 //  * Insert a new row into GL_RAW table.  
 //  */  
 // Int_t PamelaDBOperations::insertPamelaRawFile(){  
 //   //  
 //   stringstream oss;  
 //   //  
 //   Bool_t idr =  this->SetID_RAW();  
 //   if ( idr ) return(1);  
 //   //  
 //   oss.str("");  
 //   if ( STATIC ){  
 //     oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"  
 //      << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";  
 //   } else {  
 //     oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')";  
 //   };  
 //   if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;  
 //   //  
 //   idr =  this->SetID_RAW();  
 //   if ( !idr ) throw -11;  
 //   //  
 //   return(0);  
 // }  
1593  /**  /**
1594   * Insert a new row into GL_RAW table.   * Insert a new row into GL_RAW table.
1595   */   */
# Line 1501  Int_t PamelaDBOperations::insertPamelaRa Line 1603  Int_t PamelaDBOperations::insertPamelaRa
1603            
1604      glraw.PATH = GetRawPath();      glraw.PATH = GetRawPath();
1605      glraw.NAME = GetRawFile();      glraw.NAME = GetRawFile();
1606      glraw.BOOT_NUMBER = 0;//???      //    glraw.BOOT_NUMBER = 0;//???
1607        glraw.BOOT_NUMBER = this->GetBOOTnumber();
1608            
1609      if( insertPamelaRawFile(&glraw) )return(1);      if( insertPamelaRawFile(&glraw) )return(1);
1610      //      //
# Line 1515  Int_t PamelaDBOperations::insertPamelaRa Line 1618  Int_t PamelaDBOperations::insertPamelaRa
1618   */   */
1619  Int_t PamelaDBOperations::insertPamelaRawFile(GL_RAW *glraw){  Int_t PamelaDBOperations::insertPamelaRawFile(GL_RAW *glraw){
1620    //    //
1621      if(!glraw)return(1);//??      if(!glraw)return(1);//?? ok I think
1622      //      //
1623      stringstream oss;      stringstream oss;
1624      //      //
# Line 1556  Int_t PamelaDBOperations::insertPamelaRa Line 1659  Int_t PamelaDBOperations::insertPamelaRa
1659  Int_t PamelaDBOperations::insertPamelaGL_TIMESYNC(){  Int_t PamelaDBOperations::insertPamelaGL_TIMESYNC(){
1660    //    //
1661    Int_t signal = 0;    Int_t signal = 0;
1662    UInt_t idresof = 0;    //  UInt_t idresof = 0;
1663      idresof = 0;
1664    stringstream   oss;    stringstream   oss;
1665    TSQLResult *result = 0;    TSQLResult *result = 0;
1666    TSQLRow *row = 0;    TSQLRow *row = 0;
# Line 1578  Int_t PamelaDBOperations::insertPamelaGL Line 1682  Int_t PamelaDBOperations::insertPamelaGL
1682      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
1683      if ( !result ) throw -10;      if ( !result ) throw -10;
1684      row = result->Next();      row = result->Next();
1685        if ( !row ) throw -92;
1686      idresof = (UInt_t)atoll(row->GetField(0));      idresof = (UInt_t)atoll(row->GetField(0));
1687      existsts = true;      existsts = true;
1688      goto eout;      goto eout;
# Line 1599  Int_t PamelaDBOperations::insertPamelaGL Line 1704  Int_t PamelaDBOperations::insertPamelaGL
1704      if ( !row ){      if ( !row ){
1705        oss.str("");        oss.str("");
1706        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< "
1707            << dworbit << " order by FROM_ORBIT desc limit 1;";            << dworbit << " AND SPECIAL_FILE='' order by FROM_ORBIT desc limit 1;";
1708        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1709        result = conn->Query(oss.str().c_str());        result = conn->Query(oss.str().c_str());
1710        if ( !result ) throw -10;        if ( !result ) throw -10;
# Line 1646  Int_t PamelaDBOperations::insertPamelaGL Line 1751  Int_t PamelaDBOperations::insertPamelaGL
1751      TYPE = 0;      TYPE = 0;
1752      TSYNC = 0;      TSYNC = 0;
1753      //      //
1754      Double_t minimum = 0.;      //    Double_t minimum = 0.;
1755      Double_t maximum = 0.;      // Double_t maximum = 0.;
1756      Double_t minimum2 = 0.;      //Double_t minimum2 = 0.;
1757      Double_t maximum2 = 0.;      Double_t maximum2 = 0.;
1758      //      //
1759      //      //
# Line 1656  Int_t PamelaDBOperations::insertPamelaGL Line 1761  Int_t PamelaDBOperations::insertPamelaGL
1761      pamela::McmdRecord *mcrc = 0;      pamela::McmdRecord *mcrc = 0;
1762      TArrayC *mcmddata = 0;      TArrayC *mcmddata = 0;
1763      //      //
1764      minimum = numeric_limits<Double_t>::max();      //    minimum = numeric_limits<Double_t>::max();
1765      maximum = numeric_limits<Double_t>::min();      //    maximum = numeric_limits<Double_t>::min();
1766      minimum2 = numeric_limits<Double_t>::max();      //    minimum2 = numeric_limits<Double_t>::max();
1767      maximum2 = numeric_limits<Double_t>::min();      maximum2 = numeric_limits<Double_t>::min();
1768      //      //
1769      T = (TTree*)file->Get("Mcmd");      T = (TTree*)file->Get("Mcmd");
# Line 1852  Int_t PamelaDBOperations::insertPamelaGL Line 1957  Int_t PamelaDBOperations::insertPamelaGL
1957    return(signal);    return(signal);
1958  }  }
1959    
 // /**  
 //  * Insert all the new rows into GL_ROOT.  
 //  * The raw file indicates in the parameters should be already been stored in the database.  
 //  */  
 // Int_t PamelaDBOperations::insertPamelaRootFile(){  
 //   stringstream oss;  
 //   TSQLResult *result = 0;  
 //   TSQLRow    *row    = 0;  
 //   UInt_t idtimesync = 0;  
 //   //  
 //   //  
 //   if ( chewbacca ){  
 //     oss.str("");  
 //     oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";  
 //     if ( debug ) printf(" query is %s \n",oss.str().c_str());  
 //     result = conn->Query(oss.str().c_str());  
 //     //  
 //     if ( !result ) throw -3;  
 //     //  
 //     row = result->Next();  
 //     //  
 //     if ( !row ) throw -3;  
 //     idtimesync = (UInt_t)atoll(row->GetField(0));  
 //   } else {  
 //     oss.str("");  
 //     if ( STATIC ){  
 //       oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "  
 //        << " LEFT JOIN GL_ROOT "  
 //        << " ON GL_RAW.ID = GL_ROOT.ID_RAW "  
 //        << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "  
 //        << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";  
 //     } else {  
 //       oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "  
 //        << " LEFT JOIN GL_ROOT "  
 //        << " ON GL_RAW.ID = GL_ROOT.ID_RAW "  
 //        << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "  
 //        << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";  
 //     };  
 //     result = conn->Query(oss.str().c_str());  
 //     //  
 //     if ( !result ) throw -12;  
 //     //  
 //     row = result->Next();  
 //     //  
 //     if ( !row ) throw -10;  
 //     if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){  
 //       idroot = (UInt_t)atoll(row->GetField(2));  
 //       return(1);  
 //     };  
 //     //  
 //     // determine which timesync has to be used  
 //     //  
 //     oss.str("");  
 //     oss << "SELECT GL_TIMESYNC.ID FROM GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW ORDER BY GL_TIMESYNC.ID DESC LIMIT 1;";  
 //     result = conn->Query(oss.str().c_str());  
 //     //  
 //     if ( !result ) throw -3;  
 //     //  
 //     row = result->Next();  
 //     //  
 //     if ( !row ) throw -3;  
 //     idtimesync = (UInt_t)atoll(row->GetField(0));  
 //   };  
 //   //  
 //   oss.str("");  
 //   if ( STATIC ){  
 //     oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"  
 //      << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";  
 //   } else {  
 //     oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"  
 //      << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')";  
 //   };  
 //   //  
 //   if ( debug ) printf(" query is %s \n",oss.str().c_str());  
 //   if (conn->Query(oss.str().c_str()) == 0) throw -4;  
 //   //  
 //   delete result;  
 //   //  
 //   oss.str("");  
 //   //  oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";  
 //   if ( STATIC ){  
 //     oss << "SELECT ID FROM GL_ROOT WHERE PATH='" << this->GetRootPath().Data() << "' and NAME='"<< this->GetRootFile().Data() <<"';";  
 //   } else {  
 //     oss << "SELECT ID FROM GL_ROOT WHERE PATH='$PAM_L0' and NAME='"<< this->GetRootFile().Data() <<"';";  
 //   };  
 //   //  
 //   if ( debug ) printf(" query is %s \n",oss.str().c_str());  
 //   result = conn->Query(oss.str().c_str());  
 //   if ( !result ) throw -12;  
 //   row = result->Next();  
 //   if ( !row ) throw -3;  
 //   this->SetID_ROOT((UInt_t)atoll(row->GetField(0)));  
 //   if ( debug ) printf(" The ID of the ROOT file is %u \n",this->GetID_ROOT());  
 //   //  
 //   delete result;  
 //   //  
 //   return(0);  
 // }  
1960  /**  /**
1961   * Insert all the new rows into GL_ROOT.   * Insert all the new rows into GL_ROOT.
1962   * The raw file indicates in the parameters should be already been stored in the database.   * The raw file indicates in the parameters should be already been stored in the database.
# Line 1959  Int_t PamelaDBOperations::insertPamelaRo Line 1966  Int_t PamelaDBOperations::insertPamelaRo
1966      stringstream oss;      stringstream oss;
1967      TSQLResult *result = 0;      TSQLResult *result = 0;
1968      TSQLRow    *row    = 0;      TSQLRow    *row    = 0;
1969    //      //
1970      // ----------------------      // ----------------------
1971      // determine the timesync      // determine the timesync
1972      // ----------------------      // ----------------------
# Line 1967  Int_t PamelaDBOperations::insertPamelaRo Line 1974  Int_t PamelaDBOperations::insertPamelaRo
1974      //      //
1975      if ( chewbacca ){      if ( chewbacca ){
1976          oss.str("");          oss.str("");
1977          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...
1978          if ( debug ) printf(" %s \n",oss.str().c_str());          if ( debug ) printf(" %s \n",oss.str().c_str());
1979          result = conn->Query(oss.str().c_str());          result = conn->Query(oss.str().c_str());
1980          //          //
# Line 2031  Int_t PamelaDBOperations::insertPamelaRo Line 2038  Int_t PamelaDBOperations::insertPamelaRo
2038            
2039      glroot.ID_RAW = GetID_RAW();      glroot.ID_RAW = GetID_RAW();
2040      glroot.ID_TIMESYNC = idtimesync;      glroot.ID_TIMESYNC = idtimesync;
2041      glroot.PATH = GetRootPath();      if ( STATIC ){
2042          glroot.PATH = GetRootPath();
2043        } else {
2044          if ( KEEPENV ){
2045            glroot.PATH = gSystem->DirName(filerootname.Data());
2046          } else {
2047            glroot.PATH = "$PAM_L0";
2048          };
2049        };
2050      glroot.NAME = GetRootFile();      glroot.NAME = GetRootFile();
2051    
2052      if ( insertPamelaRootFile(&glroot) )return 1;      if ( insertPamelaRootFile(&glroot) )return 1;
# Line 2046  Int_t PamelaDBOperations::insertPamelaRo Line 2061  Int_t PamelaDBOperations::insertPamelaRo
2061   * The raw file indicates in the parameters should be already been stored in the database.   * The raw file indicates in the parameters should be already been stored in the database.
2062   */   */
2063  Int_t PamelaDBOperations::insertPamelaRootFile(GL_ROOT *glroot){  Int_t PamelaDBOperations::insertPamelaRootFile(GL_ROOT *glroot){
   
   
   
2064    stringstream oss;    stringstream oss;
2065    TSQLResult *result = 0;    TSQLResult *result = 0;
2066    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
2067    //    //
2068    //    //
2069    oss.str("");    oss.str("");
2070    if ( STATIC ){    oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
2071      oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"        << glroot->ID_RAW << "', '" << glroot->ID_TIMESYNC << "', '" << glroot->PATH << "', '" << glroot->NAME << "')";
         << glroot->ID_RAW << "', '" << glroot->ID_TIMESYNC << "', '" << glroot->PATH << "', '" << glroot->NAME << "')";  
   } else {  
     oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"  
         << glroot->ID_RAW << "', '" << glroot->ID_TIMESYNC << "', '$PAM_L0', '" << glroot->NAME << "')";  
   };  
2072    //    //
2073    if ( debug ) printf("%s \n",oss.str().c_str());    if ( debug ) printf("%s \n",oss.str().c_str());
2074    if (conn->Query(oss.str().c_str()) == 0) throw -4;    if (conn->Query(oss.str().c_str()) == 0) throw -4;
# Line 2070  Int_t PamelaDBOperations::insertPamelaRo Line 2077  Int_t PamelaDBOperations::insertPamelaRo
2077    //    //
2078    oss.str("");    oss.str("");
2079    //  oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";    //  oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";
2080    if ( STATIC ){    oss << "SELECT ID FROM GL_ROOT WHERE PATH='" << glroot->PATH << "' and NAME='"<< glroot->NAME <<"';";
     oss << "SELECT ID FROM GL_ROOT WHERE PATH='" << glroot->PATH << "' and NAME='"<< glroot->NAME <<"';";  
   } else {  
     oss << "SELECT ID FROM GL_ROOT WHERE PATH='$PAM_L0' and NAME='"<< glroot->NAME <<"';";  
   };  
2081    //    //
2082    if ( debug ) printf("%s \n",oss.str().c_str());    if ( debug ) printf("%s \n",oss.str().c_str());
2083    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
# Line 2180  Int_t PamelaDBOperations::assignBOOT_NUM Line 2183  Int_t PamelaDBOperations::assignBOOT_NUM
2183      UInt_t lowerts = tsync-(obt0/1000)-5;      UInt_t lowerts = tsync-(obt0/1000)-5;
2184      if ( chewbacca ){      if ( chewbacca ){
2185        oss.str("");        oss.str("");
2186        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
2187            << upperts            << upperts
2188            << " AND LAST_TIME_SYNC_INFO-(OBT_TIME_SYNC)>"            << " AND CAST((LAST_TIME_SYNC_INFO-(OBT_TIME_SYNC)) AS SIGNED INT)>" // 10RED MYSQL OPERATORS BUG
2189            << lowerts            << lowerts
2190            << " AND BOOT_NUMBER>1;";            << " AND BOOT_NUMBER>1;";
2191      } else {      } else {
2192        oss.str("");        oss.str("");
2193        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
2194            << upperts            << upperts
2195            << " AND TIMESYNC-(OBT0/1000)>"            << " AND CAST((TIMESYNC-(OBT0/1000)) AS SIGNED INT)>" // 10RED MYSQL OPERATORS BUG
2196            << lowerts            << lowerts
2197            << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";            << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
2198      };      };
# Line 2288  Int_t PamelaDBOperations::insertPamelaRU Line 2291  Int_t PamelaDBOperations::insertPamelaRU
2291    //    //
2292    // no runtrailers in the file!    // no runtrailers in the file!
2293    //    //
2294    if ( !rtev ){    if ( IsDebug() ) printf(" Start rtev %i rhev %i nrtev %i nrhev %i \n",rtev,rhev,nrtev,nrhev);
2295      if ( !rhev ){    if ( !rtev || (chewbacca && !nrtev) ){
2296        if ( IsDebug() ) printf(" No runtrailers \n");
2297        if ( !rhev || (chewbacca && !nrhev) ){
2298        if ( IsDebug() ) printf(" No runheaders nor runtrailers!! \n");        if ( IsDebug() ) printf(" No runheaders nor runtrailers!! \n");
2299        if ( !(upperentry-chminentry) ){        if ( !(upperentry-chminentry) ){
2300          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 2326  Int_t PamelaDBOperations::insertPamelaRU Line 2331  Int_t PamelaDBOperations::insertPamelaRU
2331      //      //
2332    } else {    } else {
2333      //      //
2334        if ( IsDebug() ) printf(" We have runtrailers \n");
2335      Int_t conptt = -1;      Int_t conptt = -1;
2336      for (Int_t ptt=0; ptt<rtev; ptt++){      for (Int_t ptt=0; ptt<rtev; ptt++){
2337        //        //
# Line 2342  Int_t PamelaDBOperations::insertPamelaRU Line 2348  Int_t PamelaDBOperations::insertPamelaRU
2348          ptht = cod->Get(pctp->RunHeader) - 1;          ptht = cod->Get(pctp->RunHeader) - 1;
2349          //      evbeft = cod->Get(pctp->Physics);          //      evbeft = cod->Get(pctp->Physics);
2350          tcod = (UInt_t)cod->Get(pctp->Physics);          tcod = (UInt_t)cod->Get(pctp->Physics);
2351          if ( !tcod ) tcod = 1;          if ( !tcod ) tcod = 1;    // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2352          evbeft = TMath::Min(upperentry,(tcod-1));          evbeft = TMath::Min(upperentry,(tcod-1));
2353          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));
2354          //          //
# Line 2381  Int_t PamelaDBOperations::insertPamelaRU Line 2387  Int_t PamelaDBOperations::insertPamelaRU
2387            obth = phh->GetOrbitalTime();            obth = phh->GetOrbitalTime();
2388            cod = ehh->GetCounter();            cod = ehh->GetCounter();
2389            tcod = (UInt_t)cod->Get(pctp->Physics);            tcod = (UInt_t)cod->Get(pctp->Physics);
2390            if ( !tcod ) tcod = 1;            if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2391            evbefh = TMath::Max(chminentry,(tcod-1));            evbefh = TMath::Max(chminentry,(tcod-1));
2392            //            //
2393            if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){            if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){
# Line 2420  Int_t PamelaDBOperations::insertPamelaRU Line 2426  Int_t PamelaDBOperations::insertPamelaRU
2426                //                //
2427                this->HandleRunFragments(true,true,chminentry,(evbefh));                this->HandleRunFragments(true,true,chminentry,(evbefh));
2428                //                //
2429                  // what about pth=ptht-1
2430              };              };
2431            };            };
2432            //            //
# Line 2442  Int_t PamelaDBOperations::insertPamelaRU Line 2449  Int_t PamelaDBOperations::insertPamelaRU
2449                cod = ehh->GetCounter();                cod = ehh->GetCounter();
2450                //            evbeft = cod->Get(pctp->Physics);                //            evbeft = cod->Get(pctp->Physics);
2451                tcod = (UInt_t)cod->Get(pctp->Physics);                tcod = (UInt_t)cod->Get(pctp->Physics);
2452                if ( !tcod ) tcod = 1;                if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2453                evbeft = TMath::Min(upperentry,(tcod-1));                evbeft = TMath::Min(upperentry,(tcod-1));
2454                rh->GetEntry(pth);                rh->GetEntry(pth);
2455                phh = ehh->GetPscuHeader();                phh = ehh->GetPscuHeader();
# Line 2482  Int_t PamelaDBOperations::insertPamelaRU Line 2489  Int_t PamelaDBOperations::insertPamelaRU
2489          //      if ( ptt+1 == rtev){            //      if ( ptt+1 == rtev){  
2490          if ( conptt+1 == nrtev ){            if ( conptt+1 == nrtev ){  
2491            //    if ( conptt+1 == (nrtev+nrtbef )){              //    if ( conptt+1 == (nrtev+nrtbef )){  
2492              if ( IsDebug() ) printf(" >>>>>>>>>>> %i %u %i %u \n",ptht,rhev,nrtev,conptt);
2493            ptht++;            ptht++;
2494            if ( ptht < rhev ){            if ( ptht < rhev ){
2495              rh->GetEntry(ptht);              rh->GetEntry(ptht);
2496                //pth++;
2497                //if ( pth < rhev ){
2498                //rh->GetEntry(pth);
2499              phh = ehh->GetPscuHeader();              phh = ehh->GetPscuHeader();
2500              pkth = phh->GetCounter();              pkth = phh->GetCounter();
2501              obth = phh->GetOrbitalTime();              obth = phh->GetOrbitalTime();
# Line 2508  Int_t PamelaDBOperations::insertPamelaRU Line 2519  Int_t PamelaDBOperations::insertPamelaRU
2519              if ( (UInt_t)evbeft < upperentry-1 && upperentry>0 ){              if ( (UInt_t)evbeft < upperentry-1 && upperentry>0 ){
2520                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);
2521                //                //
2522                if ( (ptt-1) < 0 ) throw -15; // should never arrive here!                //              if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
2523                rt->GetEntry(ptt-1);                if ( (rtev-1) < 0 || ptt < 0 ) throw -15; // should never arrive here!
2524                  //              rt->GetEntry(ptt-1);
2525                  rt->GetEntry(rtev-1);
2526                cod = eht->GetCounter();                cod = eht->GetCounter();
2527                tcod = (UInt_t)cod->Get(pctp->Physics);                tcod = (UInt_t)cod->Get(pctp->Physics)+1;
2528                evbefh = TMath::Max(chminentry,tcod);                evbefh = TMath::Max(chminentry,tcod);
2529                //            evbefh = cod->Get(pctp->Physics);                //            evbefh = cod->Get(pctp->Physics);
2530                rt->GetEntry(ptt);                rt->GetEntry(ptt);
# Line 2742  void PamelaDBOperations::HandleRunFragme Line 2755  void PamelaDBOperations::HandleRunFragme
2755      //      //
2756      // 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
2757      //      //
2758      if ( rhfirstev != firstev && !mishead ) mishead = true;      if ( firstev != (lastev+1) ){ // could be a problem sometimes (?)
2759      if ( rtlastev != lastev && !mistrail ) mistrail = true;        if ( rhfirstev != firstev && !mishead ) mishead = true;
2760          if ( rtlastev != lastev && !mistrail ) mistrail = true;
2761        };
2762      //      //
2763        if ( IsDebug() ) printf(" bhere firstev is %i lastev is %i \n",firstev,lastev);
2764      this->FillClass(mishead,mistrail,firstev,lastev);      this->FillClass(mishead,mistrail,firstev,lastev);
2765        if ( IsDebug() ) printf(" chere firstev is %i lastev is %i \n",firstev,lastev);
2766      //      //
2767      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");
2768      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 2787  void PamelaDBOperations::HandleRunFragme Line 2804  void PamelaDBOperations::HandleRunFragme
2804        glrun->Fill_GL_RUN_FRAGMENTS(conn);        glrun->Fill_GL_RUN_FRAGMENTS(conn);
2805        //        //
2806      } else {      } else {
2807        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);
2808        if ( PEDANTIC ) throw -69;        if ( !RELAXED ){
2809        return;          if ( PEDANTIC ) throw -69;
2810            return;
2811          } else {
2812            glrun->SetID((UInt_t)atoll(row->GetField(0)));
2813            glrun->SetID_RUN_FRAG(0);
2814          };
2815      };      };
2816      //      //
2817      if ( chewbacca && mishead && mistrail ) goto justcheck;      if ( chewbacca && mishead && mistrail ) goto justcheck;
# Line 2840  void PamelaDBOperations::HandleRunFragme Line 2862  void PamelaDBOperations::HandleRunFragme
2862          //          //
2863          found = false; // default value          found = false; // default value
2864          //          //
2865          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");
2866          //          //
2867          // 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:
2868          //          //
# Line 2856  void PamelaDBOperations::HandleRunFragme Line 2878  void PamelaDBOperations::HandleRunFragme
2878              if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);              if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2879              //              //
2880              found = true;              found = true;
2881                if ( IsDebug() ) printf(" where firstev is %i lastev is %i \n",firstev,lastev);
2882              //              //
2883            } else {            } else {
2884              //              //
# Line 2921  void PamelaDBOperations::HandleRunFragme Line 2944  void PamelaDBOperations::HandleRunFragme
2944          oss << " ID="<<row->GetField(0)<<";";          oss << " ID="<<row->GetField(0)<<";";
2945          //          //
2946          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
2947            if ( IsDebug() ) printf(" there firstev is %i lastev is %i \n",firstev,lastev);
2948          //          //
2949          // merge infos          // merge infos
2950          //          //
# Line 2951  void PamelaDBOperations::HandleRunFragme Line 2975  void PamelaDBOperations::HandleRunFragme
2975          glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());          glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
2976          glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());          glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
2977          //          //
2978            if ( IsDebug() ) printf(" here firstev is %i lastev is %i \n",firstev,lastev);
2979            //
2980          glrun->SetEV_FROM(firstev);          glrun->SetEV_FROM(firstev);
2981            glrun->SetEV_TO(lastev); // EM 10 RED ghost run bug
2982          glrun->SetNEVENTS(lastev-firstev+1);          glrun->SetNEVENTS(lastev-firstev+1);
2983          //          //
2984          glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());          glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
# Line 3043  void PamelaDBOperations::HandleRunFragme Line 3070  void PamelaDBOperations::HandleRunFragme
3070          //          //
3071          found = false; // default value          found = false; // default value
3072          //          //
3073          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");
3074          //          //
3075          // 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:
3076          //          //
# Line 3143  void PamelaDBOperations::HandleRunFragme Line 3170  void PamelaDBOperations::HandleRunFragme
3170            apkt = PKT(ph->GetCounter());            apkt = PKT(ph->GetCounter());
3171            aobt = OBT(ph->GetOrbitalTime());                aobt = OBT(ph->GetOrbitalTime());    
3172            lastev--;            lastev--;
3173            if ( PEDANTIC ) throw -72;            if ( PEDANTIC && !RELAXED) throw -72;
3174          };          };
3175          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);
3176          //          //
3177            glrun->SetEV_FROM(firstev); // EM 10RED ghost run bug
3178          glrun->SetEV_TO(lastev);          glrun->SetEV_TO(lastev);
3179          glrun->SetNEVENTS(lastev-firstev+1);          glrun->SetNEVENTS(lastev-firstev+1);
3180          glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());          glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());
# Line 3211  void PamelaDBOperations::HandleRunFragme Line 3239  void PamelaDBOperations::HandleRunFragme
3239        // 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?
3240        //        //
3241        oss.str("");        oss.str("");
3242        oss << " SELECT ID FROM GL_RUN WHERE "        oss << " SELECT ID,ID_ROOT_L0 FROM GL_RUN WHERE "
3243            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3244            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3245            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
# Line 3234  void PamelaDBOperations::HandleRunFragme Line 3262  void PamelaDBOperations::HandleRunFragme
3262        row = result->Next();        row = result->Next();
3263        //        //
3264        if ( row ){        if ( row ){
3265          if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");          //
3266          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
3267            // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number.
3268            //
3269            // 090112 [8RED (-70): RUN ALREADY INSERTED]
3270            // 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!
3271            //
3272            Bool_t OK = false;
3273            UInt_t IDRL2A = 0;
3274            UInt_t IDRL2B = 0;
3275            if ( chewbacca ){
3276              if ( result->GetRowCount() >= 2 ){
3277                //
3278                if ( IsDebug() ) printf(" RH---| gap |---RT case, number of pieces in the GL_RUN table: %i \n",result->GetRowCount());
3279                OK = true;
3280                IDRL2A = (UInt_t)atoll(row->GetField(0));
3281                //
3282                while ( row ){
3283                  //
3284                  TSQLResult *trresult = 0;
3285                  TSQLRow    *trrow    = 0;
3286                  //
3287                  stringstream tross;
3288                  tross.str("");
3289                  //
3290                  tross << " SELECT RUNHEADER_PKT, RUNTRAILER_PKT FROM GL_RUN_TRASH where ID=" << row->GetField(0) << ";";
3291                  if ( IsDebug() ) printf(" check in the gl_run_trash table for pkt intervals: query is \n %s \n",tross.str().c_str());
3292                  trresult = conn->Query(tross.str().c_str());
3293                  if ( !trresult ) throw -4;
3294                  trrow = trresult->Next();
3295                  if ( !trrow || trresult->GetRowCount() != 1 ){
3296                    OK = false;
3297                    if ( IsDebug() ) printf(" OPS! no such run (or multiple runs!) in GL_RUN_TRASH table, something wrong is going on! \n");
3298                    break;
3299                  };
3300                  //
3301                  UInt_t pktH =  (UInt_t)atoll(row->GetField(0));
3302                  UInt_t pktT =  (UInt_t)atoll(row->GetField(1));
3303                  delete trresult;
3304                  if (
3305                      (PKT(pktH) >= PKT(glrun->GetRUNHEADER_PKT()) && PKT(pktH) <= PKT(glrun->GetRUNTRAILER_PKT())) ||
3306                      (PKT(pktT) >= PKT(glrun->GetRUNHEADER_PKT()) && PKT(pktT) <= PKT(glrun->GetRUNTRAILER_PKT())) ||
3307                      (PKT(pktH) <= PKT(glrun->GetRUNTRAILER_PKT()) && PKT(pktT) >= PKT(glrun->GetRUNTRAILER_PKT())) ){            
3308                    OK = false;
3309                    if ( IsDebug() ) printf(" The run is overlapping with already inserted ones!! \n");
3310                    break;          
3311                  };
3312                  //PKT(glrun->GetRUNHEADER_PKT())  PKT(glrun->GetRUNTRAILER_PKT())
3313                  row = result->Next();
3314                };
3315                
3316                //    if ( result->GetRowCount() == 2 ) {
3317                //      IDRL2A = (UInt_t)atoll(row->GetField(0));
3318                //      UInt_t IDRL0A = (UInt_t)atoll(row->GetField(1));
3319                //      row = result->Next();
3320                //      IDRL2B = (UInt_t)atoll(row->GetField(0));
3321                //      UInt_t IDRL0B = (UInt_t)atoll(row->GetField(1));
3322                //      if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B);
3323                //      //      if ( IDRL0A == IDRL0B ){ //091230 why this condition???? RH could be in a file and RT in another one...
3324                //      if ( true ){
3325                //        TSQLResult *result2 = 0;
3326                //        TSQLRow    *row2    = 0;
3327                //        oss.str("");
3328                //        oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;";
3329                //        if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3330                //        result2 = conn->Query(oss.str().c_str());
3331                //        //
3332                //        if ( !result2 ) throw -4;
3333                //        //
3334                //        row2 = result2->Next();
3335                //        //
3336                //        if ( row2 ){
3337                //          UInt_t PKA = (UInt_t)atoll(row2->GetField(0));
3338                //          delete result2;
3339                //          oss.str("");
3340                //          oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;";
3341                //          if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3342                //          result2 = conn->Query(oss.str().c_str());
3343                //          //
3344                //          if ( !result2 ) throw -4;
3345                //          //
3346                //          row2 = result2->Next();
3347                //          //
3348                //          if ( row2 ){
3349                //            UInt_t PKB = (UInt_t)atoll(row2->GetField(0));
3350                //            //
3351                //            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);
3352                //            if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){
3353                //              if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
3354                //              OK = true;
3355                //            };
3356                //          };
3357                //        };                  
3358                //      };
3359                //    };      
3360              };
3361            };
3362            if ( OK ){
3363              //
3364              // 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  
3365              //
3366              // 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 )
3367              //
3368              if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
3369              //
3370              GL_RUN *glA = new GL_RUN();
3371              glA->Query_GL_RUN(IDRL2A,conn);
3372              //
3373              glrun->SetRUNHEADER_TIME(glA->GetRUNHEADER_TIME());
3374              glrun->SetRUNHEADER_OBT(glA->GetRUNHEADER_OBT());
3375              glrun->SetRUNHEADER_PKT(glA->GetRUNHEADER_PKT());
3376              //
3377              glrun->SetRUNTRAILER_TIME(glA->GetRUNTRAILER_TIME());
3378              glrun->SetRUNTRAILER_OBT(glA->GetRUNTRAILER_OBT());
3379              glrun->SetRUNTRAILER_PKT(glA->GetRUNTRAILER_PKT());
3380              //
3381              if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glA->GetPHYSENDRUN_MASK_S3S2S12());
3382              if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glA->GetPHYSENDRUN_MASK_S11CRC());
3383              //          
3384              if ( glA->GetACQ_BUILD_INFO() != 0 ){
3385                //
3386                // the first piece contains a good runheader we can update all the other runs with correct infos!
3387                //
3388                glrun->SetCOMPILATIONTIMESTAMP(glA->GetCOMPILATIONTIMESTAMP());
3389                glrun->SetFAV_WRK_SCHEDULE(glA->GetFAV_WRK_SCHEDULE());
3390                glrun->SetEFF_WRK_SCHEDULE(glA->GetEFF_WRK_SCHEDULE());
3391                glrun->SetPRH_VAR_TRG_MODE_A(glA->GetPRH_VAR_TRG_MODE_A());
3392                glrun->SetPRH_VAR_TRG_MODE_B(glA->GetPRH_VAR_TRG_MODE_B());
3393                glrun->SetACQ_BUILD_INFO(glA->GetACQ_BUILD_INFO());
3394                glrun->SetACQ_VAR_INFO(glA->GetACQ_VAR_INFO());
3395                glrun->SetRM_ACQ_AFTER_CALIB(glA->GetRM_ACQ_AFTER_CALIB());
3396                glrun->SetRM_ACQ_SETTING_MODE(glA->GetRM_ACQ_SETTING_MODE());
3397                glrun->SetTRK_CALIB_USED(glA->GetTRK_CALIB_USED());
3398                glrun->SetCAL_DSP_MASK(glA->GetCAL_DSP_MASK());
3399                glrun->SetLAST_TIMESYNC(glA->GetLAST_TIMESYNC());
3400                glrun->SetOBT_TIMESYNC(glA->GetOBT_TIMESYNC());
3401                //
3402              };
3403              //
3404              if ( glA->GetPKT_READY_COUNTER() != 0 ){
3405                //
3406                // the first piece contains a good runtrailer we can update all the other runs with correct infos!
3407                //
3408                glrun->SetPKT_COUNTER(glA->GetPKT_COUNTER());
3409                glrun->SetPKT_READY_COUNTER(glA->GetPKT_READY_COUNTER());
3410              };
3411              //
3412              // update runheader ROOT_ID_FRAG
3413              //
3414              oss.str("");
3415              oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
3416              if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3417              conn->Query(oss.str().c_str());
3418              //
3419              oss.str("");
3420              oss << "SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG = " << IDRL2A << ";";
3421              if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3422              TSQLResult *tsresult = 0;
3423              TSQLRow    *tsrow    = 0;
3424              tsresult=conn->Query(oss.str().c_str());
3425              if ( !tsresult ) throw -4;
3426              tsrow = tsresult->Next();
3427              if ( !tsrow || tsresult->GetRowCount() != 1 ) throw -4;
3428              IDRL2B = (UInt_t)atoll(tsrow->GetField(0));
3429              glrun->SetID_RUN_FRAG(IDRL2B);
3430              //
3431              // fill the new run in GL_RUN
3432              //
3433              glrun->Fill_GL_RUN(conn);    
3434              //
3435              glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3436              delete glA;
3437              //
3438            } else {
3439    //        //
3440    //        // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table
3441    //        //
3442    //        // 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 )
3443    //        //
3444    //        GL_RUN *glA = new GL_RUN();
3445    //        glA->Query_GL_RUN(IDRL2A,conn);
3446    //        //
3447    //        if ( glA->GetACQ_BUILD_INFO() != 0 ){
3448    //          //
3449    //          // the first piece contains a good runheader we can update all the other runs with correct infos!
3450    //          //
3451    //          oss.str("");
3452    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3453    //              << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "
3454    //              << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3455    //              << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , "
3456    //              << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , "
3457    //              << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , "
3458    //              << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , "
3459    //              << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , "
3460    //              << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , "
3461    //              << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , "
3462    //              << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , "
3463    //              << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , "  
3464    //              << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , "  
3465    //              << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , "  
3466    //              << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , "  
3467    //              << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , ";
3468    //          //
3469    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3470    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
3471    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3472    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , ";
3473    //          //        
3474    //          oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC();
3475    //          oss << " WHERE ID=" << glrun->GetID() << ";";
3476    //          if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str());
3477    //          conn->Query(oss.str().c_str());    
3478    //          //
3479    //        } else {
3480    //          //
3481    //          // sig no runheader, let set anyway what is possible...
3482    //          //
3483    //          oss.str("");
3484    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3485    //              << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , ";
3486    //          //
3487    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3488    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
3489    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3490    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
3491    //          //        
3492    //          oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3493    //              << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT();
3494    //          oss << " WHERE ID=" << glrun->GetID() << ";";
3495    //          if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str());
3496    //          conn->Query(oss.str().c_str());    
3497    //        };
3498    //        //
3499    //        // update runheader ROOT_ID_FRAG
3500    //        //
3501    //        oss.str("");
3502    //        oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
3503    //        if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3504    //        conn->Query(oss.str().c_str());
3505    //        //
3506    //        // now let's look for runtrailer if any in the last run
3507    //        //
3508    //        glA->Query_GL_RUN(IDRL2B,conn);
3509    //        //
3510    //        if ( glA->GetPKT_READY_COUNTER() != 0 ){
3511    //          //
3512    //          // the first piece contains a good runtrailer we can update all the other runs with correct infos!
3513    //          //
3514    //          oss.str("");
3515    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3516    //              << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3517    //              << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "
3518    //              << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , "
3519    //              << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , ";
3520    //          //
3521    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3522    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3523    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) {
3524    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3525    //          //        
3526    //          oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER()
3527    //              << " WHERE ID=" << glrun->GetID() << ";";
3528    //          if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str());
3529    //          conn->Query(oss.str().c_str());    
3530    //          //
3531    //        } else {
3532    //          //
3533    //          // sig no runtrailer, let set anyway what is possible...
3534    //          //
3535    //          oss.str("");
3536    //          oss << "UPDATE GL_RUN_FRAGMENTS SET "
3537    //              << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3538    //              << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , ";
3539    //            //
3540    //          if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3541    //            oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3542    //          if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){
3543    //            oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3544    //          //        
3545    //          oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()
3546    //              << " WHERE ID=" << glrun->GetID() << ";";
3547    //          if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str());
3548    //          conn->Query(oss.str().c_str());    
3549    //        };
3550    //        //
3551    //        UInt_t myi = glrun->GetID();
3552    //        oss.str("");
3553    //        oss << " ID= "<< myi;
3554    //        //
3555    //        glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
3556    //        //
3557    //        // fill the new run in GL_RUN
3558    //        //
3559    //        glrun->SetID_RUN_FRAG(IDRL2B);
3560    //        glrun->Fill_GL_RUN(conn);    
3561    //        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3562    //        delete glA;
3563    //        //
3564    //      } else {
3565              //
3566              // is just a repetition
3567              //
3568              if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");        
3569              if ( PEDANTIC ) throw -70;
3570            };
3571            //
3572        } else {        } else {
3573          if ( NoFrag() ){          if ( NoFrag() ){
3574            glrun->SetID_RUN_FRAG(glrun->GetID());            glrun->SetID_RUN_FRAG(glrun->GetID());
# Line 3272  void PamelaDBOperations::HandleMissingHo Line 3605  void PamelaDBOperations::HandleMissingHo
3605        glrun->SetID(this->AssignRunID());        glrun->SetID(this->AssignRunID());
3606        glrun->SetID_RUN_FRAG(0);        glrun->SetID_RUN_FRAG(0);
3607        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...
3608      };        };
3609      //      //
3610    };    };
3611    //    //
# Line 3288  Bool_t PamelaDBOperations::IsRunConsiste Line 3621  Bool_t PamelaDBOperations::IsRunConsiste
3621    //      //  
3622    EventCounter *code=0;    EventCounter *code=0;
3623    //    //
3624    UInt_t nevent = 0;    //  UInt_t nevent = 0;
3625    UInt_t checkfirst = 0;    UInt_t checkfirst = 0;
3626    UInt_t checklast = 0;    UInt_t checklast = 0;
3627    UInt_t firstentry = 0;    UInt_t firstentry = 0;
# Line 3310  Bool_t PamelaDBOperations::IsRunConsiste Line 3643  Bool_t PamelaDBOperations::IsRunConsiste
3643    EventHeader *eh = 0;    EventHeader *eh = 0;
3644    PscuHeader *ph = 0;    PscuHeader *ph = 0;
3645    T->SetBranchAddress("Header", &eh);    T->SetBranchAddress("Header", &eh);
3646    nevent = T->GetEntries();    //  nevent = T->GetEntries();
3647    //    //
3648    //    //
3649    if ( firstev == lastev+1 || lastev == firstev ) { // no events inside the run!    if ( firstev == lastev+1 || lastev == firstev ) { // no events inside the run!
3650      //if ( firstev <= lastev+1 ) { // no events inside the run!      //if ( firstev <= lastev+1 ) { // no events inside the run!
3651      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
3652        firstev = lastev+1; // this is necessary for files with no Physics entries, should have no influence on other files
3653      // return true is correct      // return true is correct
3654      return(true);      return(true);
3655      //      //
# Line 3345  Bool_t PamelaDBOperations::IsRunConsiste Line 3679  Bool_t PamelaDBOperations::IsRunConsiste
3679        //        //
3680      } else {      } else {
3681        //        //
3682        if ( IsDebug() ) printf(" There are no-phyics packets inside the run!\n");        if ( IsDebug() ) printf(" There are no-physics packets inside the run!\n");
3683        //        //
3684        // HERE WE MUST HANDLE THAT RUNS AND GO BACK        // HERE WE MUST HANDLE THAT RUNS AND GO BACK
3685        //        //
# Line 3355  Bool_t PamelaDBOperations::IsRunConsiste Line 3689  Bool_t PamelaDBOperations::IsRunConsiste
3689        UInt_t check = 0;        UInt_t check = 0;
3690        UInt_t lastevtemp = lastev;        UInt_t lastevtemp = lastev;
3691        UInt_t firstevno = firstev;        UInt_t firstevno = firstev;
3692          UInt_t rhchko=0;
3693          UInt_t rhchk=0;
3694        //        //
3695        for (UInt_t i=firstev; i<=lastev; i++){        for (UInt_t i=firstev; i<=lastev; i++){
3696          //          //
# Line 3363  Bool_t PamelaDBOperations::IsRunConsiste Line 3699  Bool_t PamelaDBOperations::IsRunConsiste
3699          //          //
3700          check = 0;          check = 0;
3701          //          //
3702    
3703            // if we have a runheader set lastev then exit
3704            //
3705          for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){          for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3706            if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));            if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
3707          };          };
3708            // check here if we have a runheader
3709            rhchko = rhchk;  
3710            rhchk = code->Get(GetPacketType("RunHeader"));    
3711          //          //
3712          if ( checkfirst < check || i == lastev ){          if ( checkfirst < check || i == lastev ){
3713            //            //
# Line 3449  Bool_t PamelaDBOperations::IsRunConsiste Line 3791  Bool_t PamelaDBOperations::IsRunConsiste
3791              //      this->HandleRunFragments(true,mistrail,firstentry,lastentry); // cannot call it here since it enters a loop which will destroy the already stored variables if we arrive here from HandleRunFragments              //      this->HandleRunFragments(true,mistrail,firstentry,lastentry); // cannot call it here since it enters a loop which will destroy the already stored variables if we arrive here from HandleRunFragments
3792              //              //
3793    
   
   
   
   
   
   
   
   
   
   
   
   
3794              mishead = true;              mishead = true;
3795    
3796    
# Line 3575  Bool_t PamelaDBOperations::IsRunConsiste Line 3905  Bool_t PamelaDBOperations::IsRunConsiste
3905                  //                  //
3906                  found = false; // default value                  found = false; // default value
3907                  //                  //
3908                  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");
3909                  //                  //
3910                  // 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:
3911                  //                  //
# Line 3687  Bool_t PamelaDBOperations::IsRunConsiste Line 4017  Bool_t PamelaDBOperations::IsRunConsiste
4017                  glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());                  glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
4018                  //                  //
4019                  glrun->SetEV_FROM(firstev);                  glrun->SetEV_FROM(firstev);
4020                    glrun->SetEV_TO(lastev); // 10 RED ghost run bug
4021                  glrun->SetNEVENTS(lastev-firstev+1);                  glrun->SetNEVENTS(lastev-firstev+1);
4022                  //                  //
4023                  glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());                  glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
# Line 3780  Bool_t PamelaDBOperations::IsRunConsiste Line 4111  Bool_t PamelaDBOperations::IsRunConsiste
4111              }; // EEE              }; // EEE
4112    
4113    
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
4114            } else {            } else {
4115              if ( !IsRunAlreadyInserted() ){              if ( !IsRunAlreadyInserted() ){
4116                glrun->SetID(this->AssignRunID());                glrun->SetID(this->AssignRunID());
# Line 3825  Bool_t PamelaDBOperations::IsRunConsiste Line 4125  Bool_t PamelaDBOperations::IsRunConsiste
4125            //            //
4126          };          };
4127          //          //
4128            if ( i > firstev ){
4129              if ( rhchko != rhchk ){
4130                if ( IsDebug() ) printf("oh oh... we have a runheader! stop here and handle later the remaining piece\n");
4131                lastev = i;
4132                return(false);
4133              };
4134            };
4135            //
4136          if ( check == checklast && i != lastev ){          if ( check == checklast && i != lastev ){
4137            lastevtemp = i - 1;            lastevtemp = i - 1;
4138            i = lastev - 1;            i = lastev - 1;
# Line 3857  void PamelaDBOperations::HandleSuspiciou Line 4165  void PamelaDBOperations::HandleSuspiciou
4165    EventCounter *code=0;    EventCounter *code=0;
4166    UInt_t firstev = 0;    UInt_t firstev = 0;
4167    UInt_t lastev = 0;    UInt_t lastev = 0;
4168    UInt_t nevent = 0;    //  UInt_t nevent = 0;
4169    UInt_t checkfirst = 0;    UInt_t checkfirst = 0;
4170    UInt_t checklast = 0;    UInt_t checklast = 0;
4171    UInt_t firstentry = 0;    UInt_t firstentry = 0;
# Line 3882  void PamelaDBOperations::HandleSuspiciou Line 4190  void PamelaDBOperations::HandleSuspiciou
4190    EventHeader *eh = 0;    EventHeader *eh = 0;
4191    PscuHeader *ph = 0;    PscuHeader *ph = 0;
4192    T->SetBranchAddress("Header", &eh);    T->SetBranchAddress("Header", &eh);
4193    nevent = T->GetEntries();    //  nevent = T->GetEntries();
4194    //    //
4195    codt = eht->GetCounter();    codt = eht->GetCounter();
4196    codh = ehh->GetCounter();    codh = ehh->GetCounter();
# Line 4116  Int_t PamelaDBOperations::insertCALO_CAL Line 4424  Int_t PamelaDBOperations::insertCALO_CAL
4424    UInt_t totime = 0;    UInt_t totime = 0;
4425    UInt_t obt = 0;    UInt_t obt = 0;
4426    UInt_t pkt = 0;    UInt_t pkt = 0;
4427      Float_t totped = 0.;
4428    //    //
4429    tr = (TTree*)file->Get("CalibCalPed");    tr = (TTree*)file->Get("CalibCalPed");
4430    if ( !tr || tr->IsZombie() ) throw -21;    if ( !tr || tr->IsZombie() ) throw -21;
# Line 4133  Int_t PamelaDBOperations::insertCALO_CAL Line 4442  Int_t PamelaDBOperations::insertCALO_CAL
4442        if ( calibCalPed->cstwerr[section] ){        if ( calibCalPed->cstwerr[section] ){
4443          valid = 1;          valid = 1;
4444          if ( calibCalPed->cperror[section] ) valid = 0;          if ( calibCalPed->cperror[section] ) valid = 0;
4445            //
4446            // 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.]
4447            //
4448            totped = 0.;
4449            Int_t ns = 0;
4450            if ( section == 2 ) ns = 3;
4451            if ( section == 3 ) ns = 1;
4452            if ( section == 1 ) ns = 2;
4453            for (UInt_t ss=0; ss<96; ss++){
4454              totped += fabs(calibCalPed->calped[ns][0][ss]);
4455            }
4456            if ( totped < 1. ){
4457              if ( IsDebug() ) printf(" Section %i totped %f -  No calibration data! Calorimeter power problems? \n",section,totped);
4458              valid = 0;
4459            };
4460            //
4461          ph = eh->GetPscuHeader();          ph = eh->GetPscuHeader();
4462          obt = ph->GetOrbitalTime();            obt = ph->GetOrbitalTime();  
4463          pkt = ph->GetCounter();            pkt = ph->GetCounter();  
# Line 4300  Int_t PamelaDBOperations::insertCALOPULS Line 4625  Int_t PamelaDBOperations::insertCALOPULS
4625    tr->SetBranchAddress("Header", &eh);    tr->SetBranchAddress("Header", &eh);
4626    nevents = tr->GetEntries();    nevents = tr->GetEntries();
4627    //    //
4628      Float_t totpul = 0.;
4629      //
4630    if ( nevents > 0 ){    if ( nevents > 0 ){
4631      //      //
4632      for (UInt_t i=0; i < nevents; i++){      for (UInt_t i=0; i < nevents; i++){
# Line 4309  Int_t PamelaDBOperations::insertCALOPULS Line 4636  Int_t PamelaDBOperations::insertCALOPULS
4636          if ( cp1->pstwerr[section] && cp1->unpackError == 0 ){          if ( cp1->pstwerr[section] && cp1->unpackError == 0 ){
4637            valid = 1;            valid = 1;
4638            if ( cp1->pperror[section] ) valid = 0;            if ( cp1->pperror[section] ) valid = 0;
4639              //
4640              // 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.]
4641              //
4642              totpul = 0.;
4643              Int_t ns = 0;
4644              if ( section == 2 ) ns = 3;
4645              if ( section == 3 ) ns = 1;
4646              if ( section == 1 ) ns = 2;
4647              for (UInt_t ss=0; ss<96; ss++){
4648                totpul += cp1->calpuls[ns][0][ss];
4649              }
4650              if ( totpul >= 3145632. ){
4651                if ( IsDebug() ) printf(" PULSE1 Section %i totpul %f -  No calibration data! Calorimeter power problems? \n",section,totpul);
4652                valid = 0;
4653              };
4654              //
4655            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
4656            obt = ph->GetOrbitalTime();              obt = ph->GetOrbitalTime();  
4657            pkt = ph->GetCounter();              pkt = ph->GetCounter();  
# Line 4466  Int_t PamelaDBOperations::insertCALOPULS Line 4809  Int_t PamelaDBOperations::insertCALOPULS
4809          if ( cp2->pstwerr[section] && cp2->unpackError == 0 ){          if ( cp2->pstwerr[section] && cp2->unpackError == 0 ){
4810            valid = 1;            valid = 1;
4811            if ( cp2->pperror[section] ) valid = 0;            if ( cp2->pperror[section] ) valid = 0;
4812              //
4813              // 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.]
4814              //
4815              totpul = 0.;
4816              Int_t ns = 0;
4817              if ( section == 2 ) ns = 3;
4818              if ( section == 3 ) ns = 1;
4819              if ( section == 1 ) ns = 2;
4820              for (UInt_t ss=0; ss<96; ss++){
4821                totpul += cp2->calpuls[ns][0][ss];
4822              }
4823              if ( totpul >= 3145632. ){
4824                if ( IsDebug() ) printf(" PULSE2 Section %i totpul %f -  No calibration data! Calorimeter power problems? \n",section,totpul);
4825                valid = 0;
4826              };
4827              //
4828            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
4829            obt = ph->GetOrbitalTime();              obt = ph->GetOrbitalTime();  
4830            pkt = ph->GetCounter();              pkt = ph->GetCounter();  
# Line 4641  Int_t PamelaDBOperations::insertCALOPULS Line 5000  Int_t PamelaDBOperations::insertCALOPULS
5000    return(0);    return(0);
5001  };  };
5002    
 // /**  
 //  * Fill the GL_TRK_CALIB table  
 //  */  
 // void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){  
 //   //  
 //   TSQLResult *result = 0;  
 //   TSQLRow    *row    = 0;  
 //   //  
 //   stringstream oss;  
 //   oss.str("");  
 //   //  
 //   UInt_t totime = 0;  
 //   //  
 //   if ( !pk1 && !pk2 ){  
 //     if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n");  
 //     return;  
 //   };  
 //   //  
 //   // check if the calibration has already been inserted  
 //   //  
 //   oss.str("");  
 //   oss << " SELECT ID FROM GL_TRK_CALIB WHERE "  
 //       << " BOOT_NUMBER = "<< this->GetBOOTnumber(); //  
 //   oss << " AND ( ( ";    
 //   if ( pk1 ){  
 //     oss << " OBT1 = "<< obt1 << " AND "  
 //      << " PKT1 = "<< pkt1  
 //      << " ) OR ( ";  
 //   } else {  
 //     oss << " PKT1 = "<< pkt2-1  
 //      << " ) OR ( ";    
 //   };        
 //   if ( pk2 ){  
 //     oss << " OBT2 = "<< obt2 << " AND "  
 //      << " PKT2 = "<< pkt2;  
 //   } else {  
 //     oss << " PKT2 = "<< pkt1+1;  
 //   };        
 //   oss << " ) );";    
 //   //  
 //   if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str());  
 //   result = conn->Query(oss.str().c_str());  
 //   //  
 //   if ( !result ) throw -4;  
 //   //  
 //   row = result->Next();  
 //   //  
 //   if ( row ){  
 //     //  
 //     if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");  
 //     if ( PEDANTIC ) throw -80;  
 //     //  
 //   } else {  
 //     //  
 //     // we have to insert a new calibration, check where to place it  
 //     //  
 //     oss.str("");  
 //     oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "  
 //      << " FROM_TIME < "<< fromtime << " AND "  
 //      << " TO_TIME > "<< fromtime << ";";  
 //     //  
 //     if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());  
 //     result = conn->Query(oss.str().c_str());  
 //     //  
 //     if ( !result ) throw -4;  
 //     //  
 //     row = result->Next();  
 //     //  
 //     if ( !row ){  
 //       //  
 //       // no calibrations in the db contain our calibration  
 //       //  
 //       if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");  
 //       if ( fromtime < 1150871000 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode  
 //       //  
 //       oss.str("");  
 //       oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "  
 //        << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";  
 //       //  
 //       if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());  
 //       result = conn->Query(oss.str().c_str());  
 //       //  
 //       if ( !result ) throw -4;  
 //       //  
 //       row = result->Next();  
 //       if ( !row ){  
 //      totime = numeric_limits<UInt_t>::max();  
 //       } else {  
 //      totime = (UInt_t)atoll(row->GetField(0));  
 //       };  
 //       //  
 //     } else {  
 //       //  
 //       // determine upper and lower limits and make space for the new calibration  
 //       //  
 //       totime = (UInt_t)atoll(row->GetField(1));  
 //       //  
 //       oss.str("");  
 //       oss << " UPDATE GL_TRK_CALIB SET "    
 //        << " TO_TIME = "<< fromtime << " WHERE "  // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[  
 //        << " ID = "<< row->GetField(0) << ";";  
 //       //  
 //       if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str());  
 //       result = conn->Query(oss.str().c_str());  
 //       //  
 //       if ( !result ) throw -4;  
 //       //  
 //     };  
 //     //  
 //     oss.str("");  
 //     oss << " INSERT INTO GL_TRK_CALIB (ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION) "  
 //      << " VALUES (NULL,' "  
 //      << idroot << "',";  
 //     //  
 //     if ( !pk1 ){  
 //       oss << "NULL,";  
 //     } else {  
 //       oss << "'"  
 //        << t1 << "',";  
 //     };  
 //     //  
 //     if ( !pk2 ){  
 //       oss << "NULL,'";  
 //     } else {  
 //       oss << "'"  
 //        << t2 << "','";  
 //     };  
 //     //  
 //     oss << fromtime << "','"  
 //      << totime << "','"  
 //      << obt1 << "','"  
 //      << pkt1 << "','"  
 //      << obt2 << "','"  
 //      << pkt2 << "','"  
 //      << this->GetBOOTnumber() << "','"  
 //      << valid << "');";  
 //     //  
 //     if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());  
 //     //  
 //     result = conn->Query(oss.str().c_str());  
 //     //  
 //     if ( !result ) throw -4;  
 //     //  
 //   };  
 //   //  
 // };  
5003  /**  /**
5004   * Fill the GL_TRK_CALIB table   * Fill the GL_TRK_CALIB table
5005   */   */
# Line 4977  void PamelaDBOperations::HandleTRK_CALIB Line 5190  void PamelaDBOperations::HandleTRK_CALIB
5190      if ( !result ) throw -4;;      if ( !result ) throw -4;;
5191      row = result->Next();      row = result->Next();
5192      if(row)glcal->ID = (UInt_t)atoll(row->GetField(0));      if(row)glcal->ID = (UInt_t)atoll(row->GetField(0));
5193        //
5194        delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5195      //      //
5196  };  };
5197    
# Line 5187  Int_t PamelaDBOperations::insertTRK_CALI Line 5401  Int_t PamelaDBOperations::insertTRK_CALI
5401                  //                  //
5402              };              };
5403              //              //
5404    
5405                if( !(MISSING_pkt1&MISSING_pkt2) ){
5406                  this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5407                  ncalib++;
5408                  if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;
5409                }
5410    
5411    
5412          } else {          } else {
5413              //            //
5414              if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u OUTSIDE the considered time interval \n",fromtime,obt1,pkt1);              if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u OUTSIDE the considered time interval \n",fromtime,obt1,pkt1);
5415              //      if ( PEDANTIC ) throw -79;              //      if ( PEDANTIC ) throw -79;
5416              //              //
5417          };          };
5418          //              //    
5419    
         if( !(MISSING_pkt1&MISSING_pkt2) ){  
             this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);  
             ncalib++;  
             if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;  
         }  
   
5420      }; //end loop on pkt1      }; //end loop on pkt1
5421    
5422    
# Line 5257  Int_t PamelaDBOperations::insertTRK_CALI Line 5473  Int_t PamelaDBOperations::insertTRK_CALI
5473    
5474          if ( IsDebug() ) cout << endl << ">>>> TRY TO RECOVER TRACKER CALIBRATIONS <<<<"<<endl;          if ( IsDebug() ) cout << endl << ">>>> TRY TO RECOVER TRACKER CALIBRATIONS <<<<"<<endl;
5475    
5476          TSQLResult *result = 0;          TSQLResult *rresult = 0; // 10RED bug, result is re-used inside result looop...
5477            TSQLResult *result = 0; // 10RED bug, result is re-used inside result looop...
5478          TSQLRow    *row    = 0;          TSQLRow    *row    = 0;
5479          //          //
5480          stringstream oss;          stringstream oss;
# Line 5287  Int_t PamelaDBOperations::insertTRK_CALI Line 5504  Int_t PamelaDBOperations::insertTRK_CALI
5504          oss << " ORDER BY ID DESC LIMIT "<<ncalib<<"; ";                  oss << " ORDER BY ID DESC LIMIT "<<ncalib<<"; ";        
5505          if ( IsDebug() ) cout << oss.str().c_str() << endl;          if ( IsDebug() ) cout << oss.str().c_str() << endl;
5506    
5507          result = conn->Query(oss.str().c_str());          rresult = conn->Query(oss.str().c_str());
5508          if ( !result ) throw -4;;          if ( !result ) throw -4;;
5509          if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;          if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5510                    
# Line 5296  Int_t PamelaDBOperations::insertTRK_CALI Line 5513  Int_t PamelaDBOperations::insertTRK_CALI
5513          // -----------------------------------          // -----------------------------------
5514          UInt_t nn=0;          UInt_t nn=0;
5515          do {          do {
5516              row = result->Next();              row = rresult->Next();
5517              if(!row)break;              if(!row)break;
5518                            
5519              UInt_t id       = (UInt_t)atoll(row->GetField(0));              UInt_t id       = (UInt_t)atoll(row->GetField(0));
# Line 5335  Int_t PamelaDBOperations::insertTRK_CALI Line 5552  Int_t PamelaDBOperations::insertTRK_CALI
5552              TString path       = "";              TString path       = "";
5553              TString name       = "";              TString name       = "";
5554              TString raw        = "";              TString raw        = "";
5555              UInt_t obt0        = 0;              //      UInt_t obt0        = 0;
5556              UInt_t timesync    = 0;              //      UInt_t timesync    = 0;
5557              UInt_t boot_number = 0;              //      UInt_t boot_number = 0;
5558              bool   FOUND       = false;              bool   FOUND       = false;
5559    
5560              if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;              if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;
# Line 5410  Int_t PamelaDBOperations::insertTRK_CALI Line 5627  Int_t PamelaDBOperations::insertTRK_CALI
5627                      path       = (TString)row2->GetField(0);                      path       = (TString)row2->GetField(0);
5628                      name       = (TString)row2->GetField(1);                      name       = (TString)row2->GetField(1);
5629                      raw        = (TString)row2->GetField(5);                      raw        = (TString)row2->GetField(5);
5630                      obt0        = (UInt_t)atoll(row2->GetField(2));                      //              obt0        = (UInt_t)atoll(row2->GetField(2));
5631                      timesync    = (UInt_t)atoll(row2->GetField(3));                      //              timesync    = (UInt_t)atoll(row2->GetField(3));
5632                      boot_number = (UInt_t)atoll(row2->GetField(4));                      //              boot_number = (UInt_t)atoll(row2->GetField(4));
5633                                    
5634                      if ( IsDebug() ) cout << "- - - - - - - - - - -" <<endl;                      if ( IsDebug() ) cout << "- - - - - - - - - - -" <<endl;
5635  //                  cout << path <<endl;  //                  cout << path <<endl;
5636                      cout << "File    : " <<name <<endl;  //                  cout << "File    : " <<name <<endl;
5637  //                  cout << obt0 <<endl;  //                  cout << obt0 <<endl;
5638  //                  cout << timesync <<endl;  //                  cout << timesync <<endl;
5639                      cout << "boot n. : "<<boot_number <<endl;  //                  cout << "boot n. : "<<boot_number <<endl;
5640  //                  cout << raw <<endl;  //                  cout << raw <<endl;
5641    
5642                      // ------------------------------------------------------                      // ------------------------------------------------------
5643                      // ... retrieve the calibration packets.                      // ... retrieve the calibration packets.
5644                      // ------------------------------------------------------                      // ------------------------------------------------------
5645                      TFile *file = new TFile(path+"/"+name);                      if ( IsDebug() ) printf(" file is %s/%s \n",((TString)gSystem->ExpandPathName(path.Data())).Data(),name.Data());
5646                        TFile *file = new TFile(((TString)gSystem->ExpandPathName(path.Data()))+"/"+name); // EM, path could be symbolic and we must expand it
5647                      if(!file)throw -100;                      if(!file)throw -100;
5648                      if(file->IsZombie())throw -100;                      if(file->IsZombie())throw -100;
5649                      //                      //
# Line 5475  Int_t PamelaDBOperations::insertTRK_CALI Line 5693  Int_t PamelaDBOperations::insertTRK_CALI
5693                      if(FOUND)break;                      if(FOUND)break;
5694    
5695                  }while(1);//endl loop over root table entries                  }while(1);//endl loop over root table entries
5696                    
5697                    delete result2; // 10RED potential memory leak
5698                  if(FOUND)break;                  if(FOUND)break;
5699    
5700              }//end loop over tables              }//end loop over tables
5701    
5702              if(FOUND){              if(FOUND){
5703    
5704              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  
                 ////////////////////////////////////////////  
5705    
5706                  // ---------------------------------------------------------------------                ////////////////////////////////////////////
5707                  // step 1: insert a new raw file in GL_RAW                // insert a new entry in GL_TRK_CALIB and
5708                  // ---------------------------------------------------------------------                // modify the time-tag of the previous one
5709                  //                ////////////////////////////////////////////
5710                  // check if the raw file already exist  
5711                  //                // ---------------------------------------------------------------------
5712                  UInt_t id_raw = 0;                // step 1: insert a new raw file in GL_RAW
5713                  oss.str("");                // ---------------------------------------------------------------------
5714                  oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";";                //
5715                  if ( IsDebug() ) cout << oss.str().c_str() << endl;                // check if the raw file already exist
5716                  //
5717                  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
5718                  //                oss.str("");
5719                  //                oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";";
5720                  //                if ( IsDebug() ) cout << oss.str().c_str() << endl;
5721                                    
5722                  result = conn->Query(oss.str().c_str());                //                result = conn->Query(oss.str().c_str());
5723                  if ( !result ) throw -4;;                //                if ( !result ) throw -4;;
5724                  if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;                //                if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5725                  if( result->GetRowCount() == 0){                //                if( result->GetRowCount() == 0){
5726                      if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl;                //                    if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl;
5727                      // - - - - - - - - - - -                //                    // - - - - - - - - - - -
5728                      // insert new raw file                //                    // insert new raw file
5729                      // - - - - - - - - - - -                //                    // - - - - - - - - - - -
5730                      GL_RAW glraw = GL_RAW();                    //                    GL_RAW glraw = GL_RAW();    
5731                      glraw.PATH        = gSystem->DirName(raw.Data());                //                    glraw.PATH        = gSystem->DirName(raw.Data());
5732                      glraw.NAME        = gSystem->BaseName(raw.Data());                //                    glraw.NAME        = gSystem->BaseName(raw.Data());
5733                      glraw.BOOT_NUMBER = boot_number;                //                    glraw.BOOT_NUMBER = boot_number;
5734                      //                //                    //
5735                      insertPamelaRawFile( &glraw );                //                    insertPamelaRawFile( &glraw );
5736                      //                              //                    //              
5737                      id_raw = glraw.ID;                              //                    id_raw = glraw.ID;              
5738                  }else{                //                }else{
5739                      row = result->Next();                //                    row = result->Next();
5740                      id_raw = (UInt_t)atoll(row->GetField(0));                //                    id_raw = (UInt_t)atoll(row->GetField(0));
5741                  }                //                }
5742                  if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl;                //                if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl;
5743    
5744                  // ---------------------------------------------------------------------                // ---------------------------------------------------------------------
5745                  // step 1(bis): retrieve the timesync id associated to the file                // step 1(bis): retrieve the timesync id associated to the file
5746                  // (NB, uso lo stesso associato al file iniziale)                // (NB, uso lo stesso associato al file iniziale)
5747                  // ---------------------------------------------------------------------                // ---------------------------------------------------------------------
5748                  UInt_t idtimesync = 0;                UInt_t idtimesync = 0;
5749                  oss.str("");                oss.str("");
5750                  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...
5751                  if ( debug ) printf(" %s \n",oss.str().c_str());                if ( debug ) printf(" %s \n",oss.str().c_str());
5752                  result = conn->Query(oss.str().c_str());                result = conn->Query(oss.str().c_str());
5753                  if ( !result ) throw -3;                if ( !result ) throw -3;
5754                  row = result->Next();                  row = result->Next();
5755                  if ( !row ) throw -3;                  if ( !row ) throw -3;
5756                  idtimesync = (UInt_t)atoll(row->GetField(0));                  idtimesync = (UInt_t)atoll(row->GetField(0));
5757                  if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl;                  if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl;
5758                                    
5759                    delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5760                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5761                  // step 2: insert a new root file in GL_ROOT                  // step 2: insert a new root file in GL_ROOT
5762                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5763                  //                  //
5764                  // check if the raw file already exist                  // check if the root file already exist
5765                  //                  //
5766                  UInt_t id_root = 0;                  UInt_t id_root = 0;
5767                  oss.str("");                  oss.str("");
# Line 5559  Int_t PamelaDBOperations::insertTRK_CALI Line 5779  Int_t PamelaDBOperations::insertTRK_CALI
5779                      GL_ROOT glroot = GL_ROOT();                                  GL_ROOT glroot = GL_ROOT();            
5780                      glroot.ID_RAW      = id_raw;                      glroot.ID_RAW      = id_raw;
5781                      glroot.ID_TIMESYNC = idtimesync;                      glroot.ID_TIMESYNC = idtimesync;
5782                      glroot.PATH        = path;                      //
5783                        // EM STATIC = the full expanded path must be put in the DB, KEEPENV = the path given as input (or found in ROOT_TABLE) must be used,
5784                        //             NOT STATIC NOT KEEPENV = $PAM_L0 must be used in the DB
5785                        //
5786                        if ( STATIC ){
5787                          glroot.PATH        = (TString)gSystem->ExpandPathName(path);
5788                        } else {
5789                          if ( KEEPENV ){
5790                            glroot.PATH      = path;
5791                          } else {
5792                            glroot.PATH      = "$PAM_L0";
5793                          };
5794                        };
5795                        //              glroot.PATH        = path;
5796                      glroot.NAME        = name;                      glroot.NAME        = name;
5797                      //                      //
5798                      insertPamelaRootFile( &glroot );                      insertPamelaRootFile( &glroot );
# Line 5571  Int_t PamelaDBOperations::insertTRK_CALI Line 5804  Int_t PamelaDBOperations::insertTRK_CALI
5804                  }                  }
5805                  if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl;                  if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl;
5806    
5807                    delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5808                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5809                  // step 3: modify time-tag of corrupted GL_TRK_CALIB entry                  // step 3: modify time-tag of corrupted GL_TRK_CALIB entry
5810                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
# Line 5583  Int_t PamelaDBOperations::insertTRK_CALI Line 5817  Int_t PamelaDBOperations::insertTRK_CALI
5817                  result = conn->Query(oss.str().c_str());                  result = conn->Query(oss.str().c_str());
5818                  if ( !result ) throw -4;;                  if ( !result ) throw -4;;
5819    
5820                    delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5821    
5822                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5823                  // step 4: insert the new calibration:                  // step 4: insert the new calibration:
5824                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
# Line 5612  Int_t PamelaDBOperations::insertTRK_CALI Line 5848  Int_t PamelaDBOperations::insertTRK_CALI
5848          }while(1);//end loop over calibrations          }while(1);//end loop over calibrations
5849                    
5850                    
5851          if( result )delete result;          if( rresult )delete rresult; // 10RED bug
5852          if( row    )delete row;          if( row    )delete row;
5853                    
5854                    
# Line 5913  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6149  Int_t PamelaDBOperations::CleanGL_RUN_FR
6149      memset(myid,0,500*sizeof(UInt_t));      memset(myid,0,500*sizeof(UInt_t));
6150      //      //
6151      oss.str("");      oss.str("");
6152      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;";
6153      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());
6154      nresult = conn->Query(oss.str().c_str());      nresult = conn->Query(oss.str().c_str());
6155      //      //
# Line 5944  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6180  Int_t PamelaDBOperations::CleanGL_RUN_FR
6180            while ( anr ){            while ( anr ){
6181              //              //
6182              oss.str("");              oss.str("");
6183              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
6184              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());
6185              //              //
6186              nresult1 = conn->Query(oss.str().c_str());              nresult1 = conn->Query(oss.str().c_str());
# Line 6034  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6270  Int_t PamelaDBOperations::CleanGL_RUN_FR
6270                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6271                //                              //              
6272                oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "                oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "
6273                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< ";";                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT();
6274                  oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6275                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6276              };                  };    
6277            };            };
# Line 6083  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6320  Int_t PamelaDBOperations::CleanGL_RUN_FR
6320                if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){                if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){
6321                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6322                //                              //              
6323                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< ";";                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT();
6324                  oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6325                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6326              };                  };    
6327            };            };
# Line 6134  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6372  Int_t PamelaDBOperations::CleanGL_RUN_FR
6372                row2 = result2->Next();                row2 = result2->Next();
6373                //                //
6374                if ( row2 ){                if ( row2 ){
6375                  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");
6376                  if ( PEDANTIC ) throw -83;                  if ( PEDANTIC ) throw -83;
6377                  row2->Close();                  row2->Close();
6378                };                };
# Line 6211  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6449  Int_t PamelaDBOperations::CleanGL_RUN_FR
6449        glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);          glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
6450        //        //
6451        oss.str("");        oss.str("");
6452        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 "
6453            << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("            << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
6454            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
6455            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
# Line 6269  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6507  Int_t PamelaDBOperations::CleanGL_RUN_FR
6507          moved++;          moved++;
6508          //          //
6509        } else {        } else {
6510          if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");  
6511          if ( PEDANTIC ) throw -83;          // questa parte mah mah mah... da controllare
6512    
6513            //
6514            // 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
6515            // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number.
6516            //
6517            // 090112 [8RED (-70): RUN ALREADY INSERTED]
6518            //
6519            Bool_t OK = false;
6520            UInt_t IDRL2A = 0;
6521            UInt_t IDRL2B = 0;
6522              if ( result2->GetRowCount() == 2 ){
6523                IDRL2A = (UInt_t)atoll(row2->GetField(0));
6524                UInt_t IDRL0A = (UInt_t)atoll(row2->GetField(4));
6525                row2 = result2->Next();
6526                IDRL2B = (UInt_t)atoll(row2->GetField(0));
6527                UInt_t IDRL0B = (UInt_t)atoll(row2->GetField(4));
6528                if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B);
6529                //      if ( IDRL0A == IDRL0B ){
6530    //            TSQLResult *result2a = 0;
6531    //            TSQLRow    *row2a    = 0;
6532    //            oss.str("");
6533    //            oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;";
6534    //            if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
6535    //            result2a = conn->Query(oss.str().c_str());
6536    //            //
6537    //            if ( !result2a ) throw -4;
6538    //            //
6539    //            row2a = result2a->Next();
6540    //            //
6541    //            if ( row2a ){
6542    //              UInt_t PKA = (UInt_t)atoll(row2a->GetField(0));
6543    //              delete result2a;
6544    //              oss.str("");
6545    //              oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;";
6546    //              if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
6547    //              result2a = conn->Query(oss.str().c_str());
6548    //              //
6549    //              if ( !result2a ) throw -4;
6550    //              //
6551    //              row2a = result2a->Next();
6552    //              //
6553    //              if ( row2a ){
6554    //                UInt_t PKB = (UInt_t)atoll(row2a->GetField(0));
6555    //                //
6556    //                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);
6557    //                if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){
6558    //                  if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
6559                        OK = true;
6560                        //            };
6561                        //          };
6562                        //        };                  
6563                        //    };
6564              };      
6565            if ( OK ){
6566              //
6567              // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table
6568              //
6569              // 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 )
6570              //
6571              GL_RUN *glA = new GL_RUN();
6572              glA->Query_GL_RUN(IDRL2A,conn);
6573              //
6574              if ( glA->GetACQ_BUILD_INFO() != 0 ){
6575                //
6576                // the first piece contains a good runheader we can update all the other runs with correct infos!
6577                //
6578                oss.str("");
6579                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6580                    << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "
6581                    << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
6582                    << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , "
6583                    << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , "
6584                    << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , "
6585                    << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , "
6586                    << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , "
6587                    << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , "
6588                    << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , "
6589                    << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , "
6590                    << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , "  
6591                    << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , "  
6592                    << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , "  
6593                    << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , "  
6594                    << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , ";
6595                //
6596                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
6597                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
6598                if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
6599                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , ";
6600                //        
6601                oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC();
6602                oss << " WHERE ID=" << glrun->GetID() << ";";
6603                if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str());
6604                conn->Query(oss.str().c_str());    
6605                //
6606              } else {
6607                //
6608                // sig no runheader, let set anyway what is possible...
6609                //
6610                oss.str("");
6611                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6612                    << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , ";
6613                //
6614                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
6615                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
6616                if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
6617                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6618                //        
6619                oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
6620                    << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT();
6621                oss << " WHERE ID=" << glrun->GetID() << ";";
6622                if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str());
6623                conn->Query(oss.str().c_str());    
6624              };
6625              //
6626              // update runheader ROOT_ID_FRAG
6627              //
6628              oss.str("");
6629              oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
6630              if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
6631              conn->Query(oss.str().c_str());
6632              //
6633              // now let's look for runtrailer if any in the last run
6634              //
6635              glA->Query_GL_RUN(IDRL2B,conn);
6636              //
6637              if ( glA->GetPKT_READY_COUNTER() != 0 ){
6638                //
6639                // the first piece contains a good runtrailer we can update all the other runs with correct infos!
6640                //
6641                oss.str("");
6642                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6643                    << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
6644                    << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "
6645                    << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , "
6646                    << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , ";
6647                //
6648                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
6649                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6650                if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) {
6651                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6652                //        
6653                oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER()
6654                    << " WHERE ID=" << glrun->GetID() << ";";
6655                if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str());
6656                conn->Query(oss.str().c_str());    
6657                //
6658              } else {
6659                //
6660                // sig no runtrailer, let set anyway what is possible...
6661                //
6662                oss.str("");
6663                oss << "UPDATE GL_RUN_FRAGMENTS SET "
6664                    << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
6665                    << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , ";
6666                  //
6667                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
6668                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6669                if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){
6670                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6671                //        
6672                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()
6673                    << " WHERE ID=" << glrun->GetID() << ";";
6674                if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str());
6675                conn->Query(oss.str().c_str());    
6676              };
6677              //
6678              UInt_t myi = glrun->GetID();
6679              oss.str("");
6680              oss << " ID= "<< myi;
6681              //
6682              glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
6683              //
6684              // fill the new run in GL_RUN
6685              //
6686              glrun->SetID_RUN_FRAG(IDRL2B);
6687              glrun->Fill_GL_RUN(conn);    
6688              glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
6689              delete glA;
6690              moved++;
6691              //
6692            } else {
6693              //
6694              // is just a repetition
6695              //
6696              if ( IsDebug() ) printf(" The run %u is already present in the GL_RUN table...\n",glrun->GetID());    
6697              //      printf(" CCCCCCCCICCCCCCCCCCCCIOOOOOOOOOO si muove Ciccio! %u \n",glrun->GetID());
6698              //      if ( PEDANTIC && glrun->GetID() != 61563 ) throw -83;
6699              if ( PEDANTIC ) throw -83;
6700            };
6701            //      if ( IsDebug() ) printf(" The run already exists in the GL_RUN table! ...\n");
6702            //      if ( PEDANTIC ) throw -83;
6703        };        };
6704        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));      
6705        //        //
# Line 6336  Int_t PamelaDBOperations::ValidateRuns(T Line 6765  Int_t PamelaDBOperations::ValidateRuns(T
6765      // 2) get the OBT of the last validated run      // 2) get the OBT of the last validated run
6766      // --------------------------------------------------------------      // --------------------------------------------------------------
6767      oss.str("");      oss.str("");
6768      oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start      //    oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
6769        oss << " SELECT * FROM GL_RUN  WHERE VALIDATION>0 AND RUNHEADER_TIME<="<< t_start
6770          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
6771      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
6772      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
# Line 6479  Int_t PamelaDBOperations::ValidateRuns(T Line 6909  Int_t PamelaDBOperations::ValidateRuns(T
6909          if( interval >= calibtime )CHECK = true;                        //more than calibtime s => there might be a calibration          if( interval >= calibtime )CHECK = true;                        //more than calibtime s => there might be a calibration
6910                                                        
6911          if( !CHECK && this_run->VALIDATION ){          if( !CHECK && this_run->VALIDATION ){
6912            for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);            //      for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
6913              for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],this_run->VALIDATION);
6914            nseq=0;            nseq=0;
6915          }          }
6916                                        
# Line 6500  Int_t PamelaDBOperations::ValidateRuns(T Line 6931  Int_t PamelaDBOperations::ValidateRuns(T
6931      if( CHECK ){      if( CHECK ){
6932        // check if calibration exists        // check if calibration exists
6933        if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);        if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
6934        Bool_t MISSING = MissingTRK_CALIB(t1,t2);        //      Bool_t MISSING = MissingTRK_CALIB(t1,t2);
6935        for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);        UInt_t MISSING = MissingTRK_CALIB(t1,t2);
6936          UInt_t val = 0;
6937          if ( MISSING == 1 ) val = 0;
6938          if ( MISSING == 0 ) val = 1;
6939          if ( MISSING == 2 ) val = 2;
6940          for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],val);
6941        nseq=0;        nseq=0;
6942      };      };
6943      //--------------      //--------------
# Line 6531  Int_t PamelaDBOperations::ValidateRuns(T Line 6967  Int_t PamelaDBOperations::ValidateRuns(T
6967   * @param t2 To absolute time   * @param t2 To absolute time
6968   * @return true if there might be a missing calibration   * @return true if there might be a missing calibration
6969   */   */
6970  Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){  //Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
6971    UInt_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
6972                    
6973    GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();    GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
6974                    
6975    // get the closest calibration before the run start (t2)    // get the closest calibration before the run start (t2)
6976    if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);    //>>> missing    //  if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);        //>>> missing
6977      if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(1);       //>>> missing
6978                    
6979    if ( trkcalib->TO_TIME  < t2 ) return(true);                                  //>>> missing    //  if ( trkcalib->TO_TIME  < t2 ) return(true);                                      //>>> missing
6980      if ( trkcalib->TO_TIME  < t2 ) return(1);                                     //>>> missing
6981                    
6982    //==============================================================    //==============================================================
6983    // Check is done first on the basis of time between calibration,    // Check is done first on the basis of time between calibration,
# Line 6555  Bool_t PamelaDBOperations::MissingTRK_CA Line 6994  Bool_t PamelaDBOperations::MissingTRK_CA
6994      //==============================================================      //==============================================================
6995      Bool_t DOWNLOAD = false;      Bool_t DOWNLOAD = false;
6996      // check if the calib was skipped becouse of download .... DA FARE!!      // check if the calib was skipped becouse of download .... DA FARE!!
6997      if(DOWNLOAD)return(false);      //    if(DOWNLOAD)return(false);
6998        if(DOWNLOAD)return(0);
6999                                    
7000      return(true);                                       //>>> missing      //    return(true);                                         //>>> missing
7001        return(1);                                  //>>> missing
7002                                    
7003    };    };
7004                    
# Line 6567  Bool_t PamelaDBOperations::MissingTRK_CA Line 7008  Bool_t PamelaDBOperations::MissingTRK_CA
7008    //==============================================================    //==============================================================
7009    // the long time interval bewteen runs might be due to download    // the long time interval bewteen runs might be due to download
7010    if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);    if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
7011    return(false);    if ( trkcalib->VALIDATION ) return(0);
7012      if ( IsDebug() )printf("Calibration is not validated... :-/ ==> OK but with VALIDATION=2! \n");
7013      return(2);
7014                    
7015  };  };
7016  /**  /**
# Line 6576  Bool_t PamelaDBOperations::MissingTRK_CA Line 7019  Bool_t PamelaDBOperations::MissingTRK_CA
7019   * @param validation true/false   * @param validation true/false
7020   */   */
7021  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
7022      return(this->assignVALIDATION(idrun,(UInt_t)validation));
7023    }
7024    
7025    Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, UInt_t validation){
7026    TSQLResult *result = 0;    TSQLResult *result = 0;
7027    stringstream oss;    stringstream oss;
7028    oss.str("");    oss.str("");
# Line 6772  Int_t PamelaDBOperations::removeFile(TSt Line 7219  Int_t PamelaDBOperations::removeFile(TSt
7219    stringstream myquery;    stringstream myquery;
7220    //      //  
7221    myquery.str("");    myquery.str("");
7222    myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";    myquery << " SELECT ID,ID_RAW,ID_TIMESYNC FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
7223    //    //
7224    pResult = conn->Query(myquery.str().c_str());    pResult = conn->Query(myquery.str().c_str());
7225    //    //
# Line 6788  Int_t PamelaDBOperations::removeFile(TSt Line 7235  Int_t PamelaDBOperations::removeFile(TSt
7235    //    //
7236    this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));    this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
7237    this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));    this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
7238      UInt_t idtsy=(UInt_t)atoll(Row->GetField(2));
7239    //    //
7240    this->ValidationOFF();    this->ValidationOFF();
7241    //    //
# Line 6795  Int_t PamelaDBOperations::removeFile(TSt Line 7243  Int_t PamelaDBOperations::removeFile(TSt
7243    //    //
7244    this->RemoveRUNS();    this->RemoveRUNS();
7245    //    //
7246    this->RemoveFILES();    this->RemoveFILES(idtsy);
7247    //    //
7248    this->SetID_ROOT(0);    this->SetID_ROOT(0);
7249    this->SetID_RAW(0);    this->SetID_RAW(0);
# Line 7015  void PamelaDBOperations::RemoveRUNS(){ Line 7463  void PamelaDBOperations::RemoveRUNS(){
7463   * Rearrange calibration tables   * Rearrange calibration tables
7464   *   *
7465   **/   **/
7466  void PamelaDBOperations::RemoveFILES(){  void PamelaDBOperations::RemoveFILES(UInt_t idtsy){
7467    stringstream myquery;    stringstream myquery;
7468    //    //
7469    myquery.str("");    myquery.str("");
# Line 7025  void PamelaDBOperations::RemoveFILES(){ Line 7473  void PamelaDBOperations::RemoveFILES(){
7473    //    //
7474    conn->Query(myquery.str().c_str());    conn->Query(myquery.str().c_str());
7475    //    //
7476      myquery.str("");
7477      myquery << " DELETE FROM GL_ROOT WHERE ID=" <<this->GetID_ROOT() <<";";
7478      //
7479      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
7480      //
7481      conn->Query(myquery.str().c_str());  //
7482      //
7483      if ( !chewbacca ){
7484        myquery.str("");
7485        myquery << " DELETE FROM GL_TIMESYNC WHERE ID=" << idtsy <<";";
7486        //
7487        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
7488        //
7489        conn->Query(myquery.str().c_str());
7490      };
7491      //
7492  };  };
7493    
7494  /**  /**
# Line 7374  UInt_t PamelaDBOperations::ValidateTrkCa Line 7838  UInt_t PamelaDBOperations::ValidateTrkCa
7838    
7839    
7840  }  }
 // UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){  
   
 //     if(!caltrk || !eh) return 0;  
   
 //   Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};  
 //   UInt_t timeaftercalib=120000; //2000;  
 //   //  ----------  
 //   //  Check CRCs  
 //   //  ----------  
 //   for(Int_t ipkt=0; ipkt<6; ipkt++){  
 //     if( caltrk->crc_hcal[ipkt] )return 0; // :-(  
 //     for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(  
 //   }  
 //   //  -----------------------  
 //   //  Check missing packets:  
 //   //  -----------------------  
 //   //    Readout order:  
 //   //    ------------------  
 //   //    DSP   packet board  
 //   //    ------------------  
 //   //    12    0      1  
 //   //    10    1      1  
 //   //     8    2      1  
 //   //     4    3      1  
 //   //     6    4      1  
 //   //     2    5      1  
 //   //    ------------------  
 //   //    11    0      2  
 //   //     9    1      2  
 //   //     7    2      2  
 //   //     3    3      2  
 //   //     5    4      2  
 //   //     1    5      2  
 //   //    ------------------  
 //   //  -------------------------------------------------  
 //   //  Check if it is first or second calibration packet  
 //   //  -------------------------------------------------  
 //   UInt_t build=0;  
 //   TString classname = caltrk->GetName();  
 //   UInt_t base=0;  
 //   UInt_t mask=0;  
 //   if(classname.Contains("CalibTrk1Event")){  
 //     base=12;  
 //     mask=0x03F000;  
 //   }  
 //   if(classname.Contains("CalibTrk2Event")){  
 //     base=18;  
 //     mask=0xFC0000;  
 //   }  
 //   //  -------------------------------------------------  
 //   //  Count number of packets and set build variable  
 //   //  -------------------------------------------------  
 //   Int_t  npkts=0;  
 //   for(Int_t ipkt=0; ipkt<6; ipkt++){  
 //     if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){  
 //       npkts++;  
 //       build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );  
 //     }  
 //   }  
 //   //    if( npkts==6 )return 1; // :-)  
   
 //   //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;  
   
 //   //  -----------------------------------------------  
 //   //  If missing packets: check the acq configuration  
 //   //  (some DSPs might be excluded from acquisition)  
 //   //  -----------------------------------------------  
   
 //   //  -----------------------------------------------  
 //   //  retrieve the first run header after calib  
 //   //  -----------------------------------------------  
 //   PacketType *pctp;  
 //   EventCounter *cod;  
 //   cod = eh->GetCounter();  
 //   Int_t irun = cod->Get(pctp->RunHeader);  
 //   TTree *rh=(TTree*)file->Get("RunHeader");  
 //   if ( !rh || rh->IsZombie() ) throw -17;  
 //   if( rh->GetEntries() == irun ){  
 //     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;  
 //     return 0; // :-(  
 //   }  
   
 //   RunHeaderEvent *run  = 0;  
 //   EventHeader    *hrun = 0;  
 //   rh->SetBranchAddress("RunHeader", &run);  
 //   rh->SetBranchAddress("Header", &hrun);  
 //   rh->GetEntry(irun);  
 //   //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;  
   
 //   if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){  
 //     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;  
 //     return 0; // :-(  
 //   }  
       
 //   if( !run->RM_ACQ_AFTER_CALIB ){  
 //     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0    -- cannot validate :-( "<<endl;  
 //     return 0; // :-(  
 //   }  
   
 //   UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());  
 //   if( dtime > timeaftercalib ){  
 //     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;  
 //     return 0; // :-(  
 //   }  
       
   
   
 //   if( (run->ACQ_BUILD_INFO & mask) != build ){  
 //     if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;  
 //     return 0; // :-(  
 //   }  
 //   return 1; // :-)  
   
 // }  
7841    
7842  /**  /**
7843   *   *

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.40

  ViewVC Help
Powered by ViewVC 1.1.23