/[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.21 by mocchiut, Mon Jan 12 18:23:37 2009 UTC
# Line 58  string getTleDatetime(cTle*); Line 58  string getTleDatetime(cTle*);
58   * @param debug        debug flag.   * @param debug        debug flag.
59   * @param tlefilename  ascii file with TLE 3 line elements.   * @param tlefilename  ascii file with TLE 3 line elements.
60   */   */
61  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename, UInt_t dwinput, Bool_t staticp, Bool_t gpamela){  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename, UInt_t dwinput, Bool_t staticp, Bool_t gpamela, Bool_t keepenv){
62    //    //
63      RELAXED = false;
64    chewbacca = false;    chewbacca = false;
65    chminentry = 0;    chminentry = 0;
66    chID = 0;    chID = 0;
# Line 76  PamelaDBOperations::PamelaDBOperations(T Line 77  PamelaDBOperations::PamelaDBOperations(T
77      //      //
78    };    };
79    //    //
80      KEEPENV = false;
81      if ( keepenv ) KEEPENV=true;
82    STATIC=false;    STATIC=false;
83    if ( staticp ) STATIC=true;    if ( staticp ) STATIC=true;
84    //    //
# Line 99  PamelaDBOperations::PamelaDBOperations(T Line 102  PamelaDBOperations::PamelaDBOperations(T
102    if (INSERT_RAW) SetRawName(filerawname);    if (INSERT_RAW) SetRawName(filerawname);
103    //    //
104    INSERT_ROOT = !filerootname.IsNull();    INSERT_ROOT = !filerootname.IsNull();
105      if ( INSERT_ROOT ) this->SetRootName(filerootname);
106    this->SetOrbitNo(dwinput);    this->SetOrbitNo(dwinput);
107    //    //
108    this->SetID_RAW(0);    this->SetID_RAW(0);
# Line 132  void PamelaDBOperations::OpenL0File(TStr Line 136  void PamelaDBOperations::OpenL0File(TStr
136        stringstream qu;        stringstream qu;
137        TSQLResult *result = 0;        TSQLResult *result = 0;
138        TSQLResult *result2 = 0;        TSQLResult *result2 = 0;
139          TSQLResult *result3 = 0;
140        TSQLRow    *row    = 0;            TSQLRow    *row    = 0;    
141        TSQLRow    *row2    = 0;            TSQLRow    *row2    = 0;    
142          TSQLRow    *row3    = 0;    
143        TString chpath;        TString chpath;
144        TString chfile;        TString chfile;
145        UInt_t ridn = 0;        UInt_t ridn = 0;
146        qu.str("");        qu.str("");
147        qu << "SELECT ROOT_ID_N,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL from ROOT_TABLE_MERGING where ID_N=" << chID << ";";        qu << "SELECT ROOT_ID_N,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,INSERTED_BY,REAL_TIME_INIT from ROOT_TABLE_MERGING where ID_N=" << chID << ";";
148        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
149        result = conn->Query(qu.str().c_str());        result = conn->Query(qu.str().c_str());
150        if ( result ){        if ( result ){
# Line 149  void PamelaDBOperations::OpenL0File(TStr Line 155  void PamelaDBOperations::OpenL0File(TStr
155            chpktmax = (UInt_t)atoll(row->GetField(2));            chpktmax = (UInt_t)atoll(row->GetField(2));
156            chobtmin = (UInt_t)atoll(row->GetField(3));            chobtmin = (UInt_t)atoll(row->GetField(3));
157            chobtmax = (UInt_t)atoll(row->GetField(4));            chobtmax = (UInt_t)atoll(row->GetField(4));
158              chiby = (TString)(row->GetField(5));
159              chrtinit = (UInt_t)atoll(row->GetField(6));
160          } else {          } else {
161            throw -84;            throw -84;
162          };          };
# Line 157  void PamelaDBOperations::OpenL0File(TStr Line 165  void PamelaDBOperations::OpenL0File(TStr
165        };        };
166        delete result;        delete result;
167        qu.str("");        qu.str("");
168        qu << "SELECT FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,TIME_OFFSET,BOOT_NUMBER,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL from ROOT_TABLE where ID_N=" << ridn << ";";        qu << "SELECT FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,TIME_OFFSET,BOOT_NUMBER,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,REAL_TIME_INIT from ROOT_TABLE where ID_N=" << ridn << ";";
169        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());        if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
170        result2 = conn->Query(qu.str().c_str());        result2 = conn->Query(qu.str().c_str());
171        if ( result2 ){        if ( result2 ){
172          row2 = result2->Next();          row2 = result2->Next();
173          if ( row2 ){          if ( row2 ){
174            chpath = (TString)gSystem->ExpandPathName(row2->GetField(0))+'/';            if ( KEEPENV ){
175                chpath = (TString)(row2->GetField(0))+'/';
176              } else {
177                chpath = (TString)gSystem->ExpandPathName(row2->GetField(0))+'/';
178              };
179            chfile = (TString)(row2->GetField(1));            chfile = (TString)(row2->GetField(1));
180            chobtts = (UInt_t)atoll(row2->GetField(2));            chobtts = (UInt_t)atoll(row2->GetField(2));
181            chlastts = (UInt_t)atoll(row2->GetField(3));            chlastts = (UInt_t)atoll(row2->GetField(3));
182            chresursts = (UInt_t)atoll(row2->GetField(4));            chresursts = (UInt_t)atoll(row2->GetField(4));
183            chboot = (UInt_t)atoll(row2->GetField(5));            chboot = (UInt_t)atoll(row2->GetField(5));
184            //            //
185              if ( !chboot ){
186                qu.str("");
187                //      qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND ABS(LAST_TIME_SYNC_INFO-"<< chlastts <<")<10000 AND ABS(REAL_TIME_INIT-"<< row2->GetField(10) <<")<7200 group by BOOT_NUMBER order by BOOT_NUMBER asc;";
188                qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND ABS(LAST_TIME_SYNC_INFO-"<< chlastts <<")<20000 AND ABS(REAL_TIME_INIT-"<< row2->GetField(10) <<")<40000 group by BOOT_NUMBER order by BOOT_NUMBER asc;"; // relax conditions... 090112 [8RED: error -29]
189                if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
190                result3 = conn->Query(qu.str().c_str());
191                if ( result3 ){
192                  row3 = result3->Next();
193                  if ( row3 && result3->GetRowCount() == 1 ){
194                    chboot = (UInt_t)atoll(row3->GetField(0));
195                    if ( debug ) printf(" Found boot_number = %u \n",chboot);
196                  } else {
197                    if ( debug ) printf(" AGH CANNOT DETERMINE THE BOOT NUMBER... \n");
198                    throw -29;
199                  };
200                };
201              };
202              //
203            chpktinit = (UInt_t)atoll(row2->GetField(6));            chpktinit = (UInt_t)atoll(row2->GetField(6));
204            chpktfinal = (UInt_t)atoll(row2->GetField(7));            chpktfinal = (UInt_t)atoll(row2->GetField(7));
205            chobtinit = (UInt_t)atoll(row2->GetField(8));            chobtinit = (UInt_t)atoll(row2->GetField(8));
# Line 182  void PamelaDBOperations::OpenL0File(TStr Line 212  void PamelaDBOperations::OpenL0File(TStr
212          throw -85;          throw -85;
213        };        };
214        filerootname = chpath + chfile;// + ".root";        filerootname = chpath + chfile;// + ".root";
       if ( debug ) printf(" chewbacca: filename is %s \n",filerootname.Data());  
215      };      };
216      this->SetRootName(filerootname);      this->SetRootName(filerootname);
     file = TFile::Open(this->GetRootName().Data());  
   } else {  
     this->SetRootName("");  
217    };    };
218      if ( debug ) printf(" Filename is %s \n",filerootname.Data());
219      file = TFile::Open(this->GetRootName().Data());
220      //  } else {
221      //    this->SetRootName("");
222      //    this->SetRootName(filerootname);
223      //  };
224  }  }
225    
226  //  //
# Line 388  void PamelaDBOperations::SetOrbitNo(UInt Line 420  void PamelaDBOperations::SetOrbitNo(UInt
420      return;      return;
421    };    };
422    //    //
423    TString name = this->GetRootFile();    if ( !chewbacca ){
424    Int_t nlength = name.Length();      TString name = this->GetRootFile();
425    if ( nlength < 5 ) return;      Int_t nlength = name.Length();
426    TString dwo = 0;      if ( nlength < 5 ){
427    for (Int_t i = 0; i<5; i++){        if ( IsDebug() ) printf(" Agh problems determining the orbit number! name = %s \n",name.Data());
428      dwo.Append(name[i],1);        return;
429    };      };
430    if ( dwo.IsDigit() ){      TString dwo = 0;
431      dworbit = (UInt_t)dwo.Atoi();      for (Int_t i = 0; i<5; i++){
   } else {  
     dwo="";  
     for (Int_t i = 8; i<13; i++){  
432        dwo.Append(name[i],1);        dwo.Append(name[i],1);
433      };          };
434      if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();      if ( dwo.IsDigit() ){
435          dworbit = (UInt_t)dwo.Atoi();
436        } else {
437          dwo="";
438          for (Int_t i = 8; i<13; i++){
439            dwo.Append(name[i],1);
440          };    
441          if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
442        };
443        if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
444    };    };
   if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());  
445    return;    return;
446  };  };
447    
# Line 435  TString PamelaDBOperations::GetRawPath() Line 472  TString PamelaDBOperations::GetRawPath()
472    
473  TString PamelaDBOperations::GetRootPath(){  TString PamelaDBOperations::GetRootPath(){
474    if ( STATIC ){    if ( STATIC ){
475      return((TString)gSystem->DirName(filerootname.Data())+'/');      return((TString)gSystem->DirName(this->GetRootName().Data())+'/');
476    } else {    } else {    
477      return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');      if ( KEEPENV ){
478          return((TString)gSystem->ExpandPathName(gSystem->DirName(filerootname.Data()))+'/');
479        } else {
480          return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');
481        };
482    };    };
483  };  };
484    
# Line 649  Int_t PamelaDBOperations::SetUpperLimits Line 690  Int_t PamelaDBOperations::SetUpperLimits
690      UInt_t tjump = 50000;      UInt_t tjump = 50000;
691      //UInt_t tjump = 100;      //UInt_t tjump = 100;
692      while ( tjump > 0 ){      while ( tjump > 0 ){
693        pktlast = numeric_limits<UInt_t>::max();        //      pktlast = numeric_limits<UInt_t>::max();
694        while ( pktlast > chpktmax && (Int_t)(nevent-1-it) >= 0 ){        pktlast = chpktmax + 1;
695          while ( PKT(pktlast) > PKT(chpktmax) && (Int_t)(nevent-1-it) >= 0 ){
696          if ( (Int_t)(nevent-1-it) >= 0 ){          if ( (Int_t)(nevent-1-it) >= 0 ){
697            T->GetEntry(nevent-1-it);            T->GetEntry(nevent-1-it);
698            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
# Line 658  Int_t PamelaDBOperations::SetUpperLimits Line 700  Int_t PamelaDBOperations::SetUpperLimits
700          } else {          } else {
701            pktlast = chpktmax + 1;            pktlast = chpktmax + 1;
702          };          };
703          if ( (!(it%1000) || abs((int)pktlast - (int)chpktmax)<1000 ) && debug ) printf(" look for up %i %i %i nevent %u (nevent-1-it) %i \n",it,pktlast,chpktmax,nevent,(Int_t)(nevent-1-it));          if ( (!(it%1000) || abs((int)(PKT(pktlast) - PKT(chpktmax)))<1000 ) && debug ) printf(" look for up %i %i %i nevent %u (nevent-1-it) %i \n",it,pktlast,chpktmax,nevent,(Int_t)(nevent-1-it));
704          it += tjump;          it += tjump;
705        };        };
706        if ( tjump > 1 ) it -= 2*tjump;        if ( tjump > 1 ) it -= 2*tjump;
# Line 682  Int_t PamelaDBOperations::SetUpperLimits Line 724  Int_t PamelaDBOperations::SetUpperLimits
724      tjump = 50000;      tjump = 50000;
725      //tjump = 100;      //tjump = 100;
726      while ( tjump > 0 ){      while ( tjump > 0 ){
727        pktlast = 0;        //      pktlast = 0;
728        while ( pktlast < chpktmin && it < (Int_t)nevent ){        pktlast = chpktmin - 1;
729          if ( debug ) printf("LLlook for down %i %i %llu %llu \n",it,pktlast,PKT(pktlast),PKT(chpktmin));
730          while ( PKT(pktlast) < PKT(chpktmin) && it < (Int_t)nevent ){
731          if ( it < (Int_t)nevent ){          if ( it < (Int_t)nevent ){
732            T->GetEntry(it);            T->GetEntry(it);
733            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
# Line 777  Int_t PamelaDBOperations::SetUpperLimits Line 821  Int_t PamelaDBOperations::SetUpperLimits
821      ph = eh->GetPscuHeader();      ph = eh->GetPscuHeader();
822      pktlast = ph->GetCounter();      pktlast = ph->GetCounter();
823      nevent = upperentry - chminentry;      nevent = upperentry - chminentry;
824        if ( ((Int_t)upperentry - (Int_t)chminentry) < 0 ) nevent = 0; // nevent is UInt_t! 090112 [8RED: error -88]
825        if ( IsDebug() ) printf(" Setting nevent to %u - upperentry %i chminentry %i \n",nevent,upperentry,chminentry);
826      //      //
827    } else {    } else {
828      upperpkt = PKT(pktlast);      upperpkt = PKT(pktlast);
# Line 785  Int_t PamelaDBOperations::SetUpperLimits Line 831  Int_t PamelaDBOperations::SetUpperLimits
831    };    };
832    //    //
833    if ( chewbacca && nevent < 1 ) {    if ( chewbacca && nevent < 1 ) {
834        if ( IsDebug() ) printf(" chewbacca and no events in the file \n");
835      pktfirst = chpktmin;      pktfirst = chpktmin;
836      upperpkt = PKT(chpktmax);      upperpkt = PKT(chpktmax);
837      pktlast = chpktmax;      pktlast = chpktmax;
# Line 1125  UInt_t PamelaDBOperations::AssignRunID() Line 1172  UInt_t PamelaDBOperations::AssignRunID()
1172    stringstream   oss;    stringstream   oss;
1173    //      //  
1174    oss.str("");    oss.str("");
1175    oss << "INSERT INTO _RUNID_GEN VALUES (NULL);";    if ( chewbacca ){// if chewbacca and tag=none then use chewbacca tag (chiby = chewbacca inserted by), if chewbacca and tag!=none then use tag, if not chewbacca use tag.
1176        if ( !strcmp(tag.Data(),"NONE") ){
1177          oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< chiby.Data() <<"');";
1178        } else {
1179          oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');";
1180        };
1181      } else {
1182        oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');";
1183      };
1184    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1185    if ( !result ) throw -10;    if ( !result ) throw -10;
1186    oss.str("");    oss.str("");
# Line 1330  Long64_t PamelaDBOperations::OBT(UInt_t Line 1385  Long64_t PamelaDBOperations::OBT(UInt_t
1385    //    //
1386    //  if ( IsDebug() ) printf(" obt conversion: obt is %u obtfirst is %u  (numeric_limits<UInt_t>::max()/2) is %u  \n",obt,pobtfirst,(UInt_t)(numeric_limits<UInt_t>::max()/2));    //  if ( IsDebug() ) printf(" obt conversion: obt is %u obtfirst is %u  (numeric_limits<UInt_t>::max()/2) is %u  \n",obt,pobtfirst,(UInt_t)(numeric_limits<UInt_t>::max()/2));
1387    //    //
1388    if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){    if ( obt < ((Long64_t)pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){
1389      //    if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());      //    if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());
1390      return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());      return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());
1391    };    };
# Line 1464  void PamelaDBOperations::FillClass(Bool_ Line 1519  void PamelaDBOperations::FillClass(Bool_
1519  // PUBLIC FUNCTIONS  // PUBLIC FUNCTIONS
1520  //  //
1521    
 // /**  
 //  * 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);  
 // }  
1522  /**  /**
1523   * Insert a new row into GL_RAW table.   * Insert a new row into GL_RAW table.
1524   */   */
# Line 1501  Int_t PamelaDBOperations::insertPamelaRa Line 1532  Int_t PamelaDBOperations::insertPamelaRa
1532            
1533      glraw.PATH = GetRawPath();      glraw.PATH = GetRawPath();
1534      glraw.NAME = GetRawFile();      glraw.NAME = GetRawFile();
1535      glraw.BOOT_NUMBER = 0;//???      //    glraw.BOOT_NUMBER = 0;//???
1536        glraw.BOOT_NUMBER = this->GetBOOTnumber();
1537            
1538      if( insertPamelaRawFile(&glraw) )return(1);      if( insertPamelaRawFile(&glraw) )return(1);
1539      //      //
# Line 1515  Int_t PamelaDBOperations::insertPamelaRa Line 1547  Int_t PamelaDBOperations::insertPamelaRa
1547   */   */
1548  Int_t PamelaDBOperations::insertPamelaRawFile(GL_RAW *glraw){  Int_t PamelaDBOperations::insertPamelaRawFile(GL_RAW *glraw){
1549    //    //
1550      if(!glraw)return(1);//??      if(!glraw)return(1);//?? ok I think
1551      //      //
1552      stringstream oss;      stringstream oss;
1553      //      //
# Line 1578  Int_t PamelaDBOperations::insertPamelaGL Line 1610  Int_t PamelaDBOperations::insertPamelaGL
1610      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
1611      if ( !result ) throw -10;      if ( !result ) throw -10;
1612      row = result->Next();      row = result->Next();
1613        if ( !row ) throw -92;
1614      idresof = (UInt_t)atoll(row->GetField(0));      idresof = (UInt_t)atoll(row->GetField(0));
1615      existsts = true;      existsts = true;
1616      goto eout;      goto eout;
# Line 1599  Int_t PamelaDBOperations::insertPamelaGL Line 1632  Int_t PamelaDBOperations::insertPamelaGL
1632      if ( !row ){      if ( !row ){
1633        oss.str("");        oss.str("");
1634        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< "
1635            << dworbit << " order by FROM_ORBIT desc limit 1;";            << dworbit << " AND SPECIAL_FILE='' order by FROM_ORBIT desc limit 1;";
1636        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1637        result = conn->Query(oss.str().c_str());        result = conn->Query(oss.str().c_str());
1638        if ( !result ) throw -10;        if ( !result ) throw -10;
# Line 1852  Int_t PamelaDBOperations::insertPamelaGL Line 1885  Int_t PamelaDBOperations::insertPamelaGL
1885    return(signal);    return(signal);
1886  }  }
1887    
 // /**  
 //  * 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);  
 // }  
1888  /**  /**
1889   * Insert all the new rows into GL_ROOT.   * Insert all the new rows into GL_ROOT.
1890   * 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 1894  Int_t PamelaDBOperations::insertPamelaRo
1894      stringstream oss;      stringstream oss;
1895      TSQLResult *result = 0;      TSQLResult *result = 0;
1896      TSQLRow    *row    = 0;      TSQLRow    *row    = 0;
1897    //      //
1898      // ----------------------      // ----------------------
1899      // determine the timesync      // determine the timesync
1900      // ----------------------      // ----------------------
# Line 2031  Int_t PamelaDBOperations::insertPamelaRo Line 1966  Int_t PamelaDBOperations::insertPamelaRo
1966            
1967      glroot.ID_RAW = GetID_RAW();      glroot.ID_RAW = GetID_RAW();
1968      glroot.ID_TIMESYNC = idtimesync;      glroot.ID_TIMESYNC = idtimesync;
1969      glroot.PATH = GetRootPath();      if ( STATIC ){
1970          glroot.PATH = GetRootPath();
1971        } else {
1972          if ( KEEPENV ){
1973            glroot.PATH = gSystem->DirName(filerootname.Data());
1974          } else {
1975            glroot.PATH = "$PAM_L0";
1976          };
1977        };
1978      glroot.NAME = GetRootFile();      glroot.NAME = GetRootFile();
1979    
1980      if ( insertPamelaRootFile(&glroot) )return 1;      if ( insertPamelaRootFile(&glroot) )return 1;
# Line 2046  Int_t PamelaDBOperations::insertPamelaRo Line 1989  Int_t PamelaDBOperations::insertPamelaRo
1989   * 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.
1990   */   */
1991  Int_t PamelaDBOperations::insertPamelaRootFile(GL_ROOT *glroot){  Int_t PamelaDBOperations::insertPamelaRootFile(GL_ROOT *glroot){
   
   
   
1992    stringstream oss;    stringstream oss;
1993    TSQLResult *result = 0;    TSQLResult *result = 0;
1994    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
1995    //    //
1996    //    //
1997    oss.str("");    oss.str("");
1998    if ( STATIC ){    oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1999      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 << "')";  
   };  
2000    //    //
2001    if ( debug ) printf("%s \n",oss.str().c_str());    if ( debug ) printf("%s \n",oss.str().c_str());
2002    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 2005  Int_t PamelaDBOperations::insertPamelaRo
2005    //    //
2006    oss.str("");    oss.str("");
2007    //  oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";    //  oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";
2008    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 <<"';";  
   };  
2009    //    //
2010    if ( debug ) printf("%s \n",oss.str().c_str());    if ( debug ) printf("%s \n",oss.str().c_str());
2011    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
# Line 2342  Int_t PamelaDBOperations::insertPamelaRU Line 2273  Int_t PamelaDBOperations::insertPamelaRU
2273          ptht = cod->Get(pctp->RunHeader) - 1;          ptht = cod->Get(pctp->RunHeader) - 1;
2274          //      evbeft = cod->Get(pctp->Physics);          //      evbeft = cod->Get(pctp->Physics);
2275          tcod = (UInt_t)cod->Get(pctp->Physics);          tcod = (UInt_t)cod->Get(pctp->Physics);
2276          if ( !tcod ) tcod = 1;          if ( !tcod ) tcod = 1;    // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2277          evbeft = TMath::Min(upperentry,(tcod-1));          evbeft = TMath::Min(upperentry,(tcod-1));
2278          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));
2279          //          //
# Line 2381  Int_t PamelaDBOperations::insertPamelaRU Line 2312  Int_t PamelaDBOperations::insertPamelaRU
2312            obth = phh->GetOrbitalTime();            obth = phh->GetOrbitalTime();
2313            cod = ehh->GetCounter();            cod = ehh->GetCounter();
2314            tcod = (UInt_t)cod->Get(pctp->Physics);            tcod = (UInt_t)cod->Get(pctp->Physics);
2315            if ( !tcod ) tcod = 1;            if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2316            evbefh = TMath::Max(chminentry,(tcod-1));            evbefh = TMath::Max(chminentry,(tcod-1));
2317            //            //
2318            if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){            if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){
# Line 2420  Int_t PamelaDBOperations::insertPamelaRU Line 2351  Int_t PamelaDBOperations::insertPamelaRU
2351                //                //
2352                this->HandleRunFragments(true,true,chminentry,(evbefh));                this->HandleRunFragments(true,true,chminentry,(evbefh));
2353                //                //
2354                  // what about pth=ptht-1
2355              };              };
2356            };            };
2357            //            //
# Line 2442  Int_t PamelaDBOperations::insertPamelaRU Line 2374  Int_t PamelaDBOperations::insertPamelaRU
2374                cod = ehh->GetCounter();                cod = ehh->GetCounter();
2375                //            evbeft = cod->Get(pctp->Physics);                //            evbeft = cod->Get(pctp->Physics);
2376                tcod = (UInt_t)cod->Get(pctp->Physics);                tcod = (UInt_t)cod->Get(pctp->Physics);
2377                if ( !tcod ) tcod = 1;                if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2378                evbeft = TMath::Min(upperentry,(tcod-1));                evbeft = TMath::Min(upperentry,(tcod-1));
2379                rh->GetEntry(pth);                rh->GetEntry(pth);
2380                phh = ehh->GetPscuHeader();                phh = ehh->GetPscuHeader();
# Line 2482  Int_t PamelaDBOperations::insertPamelaRU Line 2414  Int_t PamelaDBOperations::insertPamelaRU
2414          //      if ( ptt+1 == rtev){            //      if ( ptt+1 == rtev){  
2415          if ( conptt+1 == nrtev ){            if ( conptt+1 == nrtev ){  
2416            //    if ( conptt+1 == (nrtev+nrtbef )){              //    if ( conptt+1 == (nrtev+nrtbef )){  
2417              if ( IsDebug() ) printf(" >>>>>>>>>>> %i %u %i %u \n",ptht,rhev,nrtev,conptt);
2418            ptht++;            ptht++;
2419            if ( ptht < rhev ){            if ( ptht < rhev ){
2420              rh->GetEntry(ptht);              rh->GetEntry(ptht);
2421                //pth++;
2422                //if ( pth < rhev ){
2423                //rh->GetEntry(pth);
2424              phh = ehh->GetPscuHeader();              phh = ehh->GetPscuHeader();
2425              pkth = phh->GetCounter();              pkth = phh->GetCounter();
2426              obth = phh->GetOrbitalTime();              obth = phh->GetOrbitalTime();
# Line 2508  Int_t PamelaDBOperations::insertPamelaRU Line 2444  Int_t PamelaDBOperations::insertPamelaRU
2444              if ( (UInt_t)evbeft < upperentry-1 && upperentry>0 ){              if ( (UInt_t)evbeft < upperentry-1 && upperentry>0 ){
2445                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);
2446                //                //
2447                if ( (ptt-1) < 0 ) throw -15; // should never arrive here!                //              if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
2448                rt->GetEntry(ptt-1);                if ( (rtev-1) < 0 || ptt < 0 ) throw -15; // should never arrive here!
2449                  //              rt->GetEntry(ptt-1);
2450                  rt->GetEntry(rtev-1);
2451                cod = eht->GetCounter();                cod = eht->GetCounter();
2452                tcod = (UInt_t)cod->Get(pctp->Physics);                tcod = (UInt_t)cod->Get(pctp->Physics)+1;
2453                evbefh = TMath::Max(chminentry,tcod);                evbefh = TMath::Max(chminentry,tcod);
2454                //            evbefh = cod->Get(pctp->Physics);                //            evbefh = cod->Get(pctp->Physics);
2455                rt->GetEntry(ptt);                rt->GetEntry(ptt);
# Line 2742  void PamelaDBOperations::HandleRunFragme Line 2680  void PamelaDBOperations::HandleRunFragme
2680      //      //
2681      // 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
2682      //      //
2683      if ( rhfirstev != firstev && !mishead ) mishead = true;      if ( firstev != (lastev+1) ){ // could be a problem sometimes (?)
2684      if ( rtlastev != lastev && !mistrail ) mistrail = true;        if ( rhfirstev != firstev && !mishead ) mishead = true;
2685          if ( rtlastev != lastev && !mistrail ) mistrail = true;
2686        };
2687      //      //
2688        if ( IsDebug() ) printf(" bhere firstev is %i lastev is %i \n",firstev,lastev);
2689      this->FillClass(mishead,mistrail,firstev,lastev);      this->FillClass(mishead,mistrail,firstev,lastev);
2690        if ( IsDebug() ) printf(" chere firstev is %i lastev is %i \n",firstev,lastev);
2691      //      //
2692      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");
2693      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 2729  void PamelaDBOperations::HandleRunFragme
2729        glrun->Fill_GL_RUN_FRAGMENTS(conn);        glrun->Fill_GL_RUN_FRAGMENTS(conn);
2730        //        //
2731      } else {      } else {
2732        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);
2733        if ( PEDANTIC ) throw -69;        if ( !RELAXED ){
2734        return;          if ( PEDANTIC ) throw -69;
2735            return;
2736          } else {
2737            glrun->SetID((UInt_t)atoll(row->GetField(0)));
2738            glrun->SetID_RUN_FRAG(0);
2739          };
2740      };      };
2741      //      //
2742      if ( chewbacca && mishead && mistrail ) goto justcheck;      if ( chewbacca && mishead && mistrail ) goto justcheck;
# Line 2840  void PamelaDBOperations::HandleRunFragme Line 2787  void PamelaDBOperations::HandleRunFragme
2787          //          //
2788          found = false; // default value          found = false; // default value
2789          //          //
2790          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");
2791          //          //
2792          // 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:
2793          //          //
# Line 2856  void PamelaDBOperations::HandleRunFragme Line 2803  void PamelaDBOperations::HandleRunFragme
2803              if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);              if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2804              //              //
2805              found = true;              found = true;
2806                if ( IsDebug() ) printf(" where firstev is %i lastev is %i \n",firstev,lastev);
2807              //              //
2808            } else {            } else {
2809              //              //
# Line 2921  void PamelaDBOperations::HandleRunFragme Line 2869  void PamelaDBOperations::HandleRunFragme
2869          oss << " ID="<<row->GetField(0)<<";";          oss << " ID="<<row->GetField(0)<<";";
2870          //          //
2871          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
2872            if ( IsDebug() ) printf(" there firstev is %i lastev is %i \n",firstev,lastev);
2873          //          //
2874          // merge infos          // merge infos
2875          //          //
# Line 2951  void PamelaDBOperations::HandleRunFragme Line 2900  void PamelaDBOperations::HandleRunFragme
2900          glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());          glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
2901          glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());          glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
2902          //          //
2903            if ( IsDebug() ) printf(" here firstev is %i lastev is %i \n",firstev,lastev);
2904            //
2905          glrun->SetEV_FROM(firstev);          glrun->SetEV_FROM(firstev);
2906          glrun->SetNEVENTS(lastev-firstev+1);          glrun->SetNEVENTS(lastev-firstev+1);
2907          //          //
# Line 3043  void PamelaDBOperations::HandleRunFragme Line 2994  void PamelaDBOperations::HandleRunFragme
2994          //          //
2995          found = false; // default value          found = false; // default value
2996          //          //
2997          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");
2998          //          //
2999          // 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:
3000          //          //
# Line 3143  void PamelaDBOperations::HandleRunFragme Line 3094  void PamelaDBOperations::HandleRunFragme
3094            apkt = PKT(ph->GetCounter());            apkt = PKT(ph->GetCounter());
3095            aobt = OBT(ph->GetOrbitalTime());                aobt = OBT(ph->GetOrbitalTime());    
3096            lastev--;            lastev--;
3097            if ( PEDANTIC ) throw -72;            if ( PEDANTIC && !RELAXED) throw -72;
3098          };          };
3099          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);
3100          //          //
# Line 3211  void PamelaDBOperations::HandleRunFragme Line 3162  void PamelaDBOperations::HandleRunFragme
3162        // 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?
3163        //        //
3164        oss.str("");        oss.str("");
3165        oss << " SELECT ID FROM GL_RUN WHERE "        oss << " SELECT ID,ID_ROOT_L0 FROM GL_RUN WHERE "
3166            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3167            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "            << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3168            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("            << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
# Line 3234  void PamelaDBOperations::HandleRunFragme Line 3185  void PamelaDBOperations::HandleRunFragme
3185        row = result->Next();        row = result->Next();
3186        //        //
3187        if ( row ){        if ( row ){
3188          if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");          //
3189          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
3190            // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number.
3191            //
3192            // 090112 [8RED (-70): RUN ALREADY INSERTED]
3193            //
3194            Bool_t OK = false;
3195            UInt_t IDRL2A = 0;
3196            UInt_t IDRL2B = 0;
3197            if ( chewbacca ){
3198              if ( result->GetRowCount() == 2 ){
3199                IDRL2A = (UInt_t)atoll(row->GetField(0));
3200                UInt_t IDRL0A = (UInt_t)atoll(row->GetField(1));
3201                row = result->Next();
3202                IDRL2B = (UInt_t)atoll(row->GetField(0));
3203                UInt_t IDRL0B = (UInt_t)atoll(row->GetField(1));
3204                if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B);
3205                if ( IDRL0A == IDRL0B ){
3206                  TSQLResult *result2 = 0;
3207                  TSQLRow    *row2    = 0;
3208                  oss.str("");
3209                  oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;";
3210                  if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3211                  result2 = conn->Query(oss.str().c_str());
3212                  //
3213                  if ( !result2 ) throw -4;
3214                  //
3215                  row2 = result2->Next();
3216                  //
3217                  if ( row2 ){
3218                    UInt_t PKA = (UInt_t)atoll(row2->GetField(0));
3219                    delete result2;
3220                    oss.str("");
3221                    oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;";
3222                    if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3223                    result2 = conn->Query(oss.str().c_str());
3224                    //
3225                    if ( !result2 ) throw -4;
3226                    //
3227                    row2 = result2->Next();
3228                    //
3229                    if ( row2 ){
3230                      UInt_t PKB = (UInt_t)atoll(row2->GetField(0));
3231                      //
3232                      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);
3233                      if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){
3234                        if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
3235                        OK = true;
3236                      };
3237                    };
3238                  };                  
3239                };
3240              };      
3241            };
3242            if ( OK ){
3243              //
3244              // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table
3245              //
3246              // 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 )
3247              //
3248              GL_RUN *glA = new GL_RUN();
3249              glA->Query_GL_RUN(IDRL2A,conn);
3250              //
3251              if ( glA->GetACQ_BUILD_INFO() != 0 ){
3252                //
3253                // the first piece contains a good runheader we can update all the other runs with correct infos!
3254                //
3255                oss.str("");
3256                oss << "UPDATE GL_RUN_FRAGMENTS SET "
3257                    << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "
3258                    << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3259                    << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , "
3260                    << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , "
3261                    << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , "
3262                    << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , "
3263                    << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , "
3264                    << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , "
3265                    << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , "
3266                    << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , "
3267                    << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , "  
3268                    << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , "  
3269                    << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , "  
3270                    << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , "  
3271                    << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , ";
3272                //
3273                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3274                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
3275                if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3276                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , ";
3277                //        
3278                oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC();
3279                oss << " WHERE ID=" << glrun->GetID() << ";";
3280                if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str());
3281                conn->Query(oss.str().c_str());    
3282                //
3283              } else {
3284                //
3285                // sig no runheader, let set anyway what is possible...
3286                //
3287                oss.str("");
3288                oss << "UPDATE GL_RUN_FRAGMENTS SET "
3289                    << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , ";
3290                //
3291                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3292                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
3293                if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3294                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
3295                //        
3296                oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3297                    << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT();
3298                oss << " WHERE ID=" << glrun->GetID() << ";";
3299                if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str());
3300                conn->Query(oss.str().c_str());    
3301              };
3302              //
3303              // update runheader ROOT_ID_FRAG
3304              //
3305              oss.str("");
3306              oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
3307              if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3308              conn->Query(oss.str().c_str());
3309              //
3310              // now let's look for runtrailer if any in the last run
3311              //
3312              glA->Query_GL_RUN(IDRL2B,conn);
3313              //
3314              if ( glA->GetPKT_READY_COUNTER() != 0 ){
3315                //
3316                // the first piece contains a good runtrailer we can update all the other runs with correct infos!
3317                //
3318                oss.str("");
3319                oss << "UPDATE GL_RUN_FRAGMENTS SET "
3320                    << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3321                    << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "
3322                    << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , "
3323                    << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , ";
3324                //
3325                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3326                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3327                if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) {
3328                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3329                //        
3330                oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER()
3331                    << " WHERE ID=" << glrun->GetID() << ";";
3332                if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str());
3333                conn->Query(oss.str().c_str());    
3334                //
3335              } else {
3336                //
3337                // sig no runtrailer, let set anyway what is possible...
3338                //
3339                oss.str("");
3340                oss << "UPDATE GL_RUN_FRAGMENTS SET "
3341                    << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3342                    << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , ";
3343                  //
3344                if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3345                  oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3346                if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){
3347                  oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3348                //        
3349                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()
3350                    << " WHERE ID=" << glrun->GetID() << ";";
3351                if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str());
3352                conn->Query(oss.str().c_str());    
3353              };
3354              //
3355              UInt_t myi = glrun->GetID();
3356              oss.str("");
3357              oss << " ID= "<< myi;
3358              //
3359              glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
3360              //
3361              // fill the new run in GL_RUN
3362              //
3363              glrun->SetID_RUN_FRAG(IDRL2B);
3364              glrun->Fill_GL_RUN(conn);    
3365              glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3366              //
3367            } else {
3368              //
3369              // is just a repetition
3370              //
3371              if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");        
3372              if ( PEDANTIC ) throw -70;
3373            };
3374            //
3375        } else {        } else {
3376          if ( NoFrag() ){          if ( NoFrag() ){
3377            glrun->SetID_RUN_FRAG(glrun->GetID());            glrun->SetID_RUN_FRAG(glrun->GetID());
# Line 3272  void PamelaDBOperations::HandleMissingHo Line 3408  void PamelaDBOperations::HandleMissingHo
3408        glrun->SetID(this->AssignRunID());        glrun->SetID(this->AssignRunID());
3409        glrun->SetID_RUN_FRAG(0);        glrun->SetID_RUN_FRAG(0);
3410        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...
3411      };        };
3412      //      //
3413    };    };
3414    //    //
# Line 3316  Bool_t PamelaDBOperations::IsRunConsiste Line 3452  Bool_t PamelaDBOperations::IsRunConsiste
3452    if ( firstev == lastev+1 || lastev == firstev ) { // no events inside the run!    if ( firstev == lastev+1 || lastev == firstev ) { // no events inside the run!
3453      //if ( firstev <= lastev+1 ) { // no events inside the run!      //if ( firstev <= lastev+1 ) { // no events inside the run!
3454      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
3455        firstev = lastev+1; // this is necessary for files with no Physics entries, should have no influence on other files
3456      // return true is correct      // return true is correct
3457      return(true);      return(true);
3458      //      //
# Line 3345  Bool_t PamelaDBOperations::IsRunConsiste Line 3482  Bool_t PamelaDBOperations::IsRunConsiste
3482        //        //
3483      } else {      } else {
3484        //        //
3485        if ( IsDebug() ) printf(" There are no-phyics packets inside the run!\n");        if ( IsDebug() ) printf(" There are no-physics packets inside the run!\n");
3486        //        //
3487        // HERE WE MUST HANDLE THAT RUNS AND GO BACK        // HERE WE MUST HANDLE THAT RUNS AND GO BACK
3488        //        //
# Line 3355  Bool_t PamelaDBOperations::IsRunConsiste Line 3492  Bool_t PamelaDBOperations::IsRunConsiste
3492        UInt_t check = 0;        UInt_t check = 0;
3493        UInt_t lastevtemp = lastev;        UInt_t lastevtemp = lastev;
3494        UInt_t firstevno = firstev;        UInt_t firstevno = firstev;
3495          UInt_t rhchko=0;
3496          UInt_t rhchk=0;
3497        //        //
3498        for (UInt_t i=firstev; i<=lastev; i++){        for (UInt_t i=firstev; i<=lastev; i++){
3499          //          //
# Line 3363  Bool_t PamelaDBOperations::IsRunConsiste Line 3502  Bool_t PamelaDBOperations::IsRunConsiste
3502          //          //
3503          check = 0;          check = 0;
3504          //          //
3505    
3506            // if we have a runheader set lastev then exit
3507            //
3508          for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){          for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3509            if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));            if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
3510          };          };
3511            // check here if we have a runheader
3512            rhchko = rhchk;  
3513            rhchk = code->Get(GetPacketType("RunHeader"));    
3514          //          //
3515          if ( checkfirst < check || i == lastev ){          if ( checkfirst < check || i == lastev ){
3516            //            //
# Line 3449  Bool_t PamelaDBOperations::IsRunConsiste Line 3594  Bool_t PamelaDBOperations::IsRunConsiste
3594              //      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
3595              //              //
3596    
   
   
   
   
   
   
   
   
   
   
   
   
3597              mishead = true;              mishead = true;
3598    
3599    
# Line 3575  Bool_t PamelaDBOperations::IsRunConsiste Line 3708  Bool_t PamelaDBOperations::IsRunConsiste
3708                  //                  //
3709                  found = false; // default value                  found = false; // default value
3710                  //                  //
3711                  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");
3712                  //                  //
3713                  // 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:
3714                  //                  //
# Line 3780  Bool_t PamelaDBOperations::IsRunConsiste Line 3913  Bool_t PamelaDBOperations::IsRunConsiste
3913              }; // EEE              }; // EEE
3914    
3915    
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
3916            } else {            } else {
3917              if ( !IsRunAlreadyInserted() ){              if ( !IsRunAlreadyInserted() ){
3918                glrun->SetID(this->AssignRunID());                glrun->SetID(this->AssignRunID());
# Line 3825  Bool_t PamelaDBOperations::IsRunConsiste Line 3927  Bool_t PamelaDBOperations::IsRunConsiste
3927            //            //
3928          };          };
3929          //          //
3930            if ( i > firstev ){
3931              if ( rhchko != rhchk ){
3932                if ( IsDebug() ) printf("oh oh... we have a runheader! stop here and handle later the remaining piece\n");
3933                lastev = i;
3934                return(false);
3935              };
3936            };
3937            //
3938          if ( check == checklast && i != lastev ){          if ( check == checklast && i != lastev ){
3939            lastevtemp = i - 1;            lastevtemp = i - 1;
3940            i = lastev - 1;            i = lastev - 1;
# Line 4641  Int_t PamelaDBOperations::insertCALOPULS Line 4751  Int_t PamelaDBOperations::insertCALOPULS
4751    return(0);    return(0);
4752  };  };
4753    
 // /**  
 //  * 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;  
 //     //  
 //   };  
 //   //  
 // };  
4754  /**  /**
4755   * Fill the GL_TRK_CALIB table   * Fill the GL_TRK_CALIB table
4756   */   */
# Line 4977  void PamelaDBOperations::HandleTRK_CALIB Line 4941  void PamelaDBOperations::HandleTRK_CALIB
4941      if ( !result ) throw -4;;      if ( !result ) throw -4;;
4942      row = result->Next();      row = result->Next();
4943      if(row)glcal->ID = (UInt_t)atoll(row->GetField(0));      if(row)glcal->ID = (UInt_t)atoll(row->GetField(0));
4944        //
4945        delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
4946      //      //
4947  };  };
4948    
# Line 5187  Int_t PamelaDBOperations::insertTRK_CALI Line 5152  Int_t PamelaDBOperations::insertTRK_CALI
5152                  //                  //
5153              };              };
5154              //              //
5155    
5156                if( !(MISSING_pkt1&MISSING_pkt2) ){
5157                  this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5158                  ncalib++;
5159                  if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;
5160                }
5161    
5162    
5163          } else {          } else {
5164              //            //
5165              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);
5166              //      if ( PEDANTIC ) throw -79;              //      if ( PEDANTIC ) throw -79;
5167              //              //
5168          };          };
5169          //              //    
5170    
         if( !(MISSING_pkt1&MISSING_pkt2) ){  
             this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);  
             ncalib++;  
             if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;  
         }  
   
5171      }; //end loop on pkt1      }; //end loop on pkt1
5172    
5173    
# Line 5416  Int_t PamelaDBOperations::insertTRK_CALI Line 5383  Int_t PamelaDBOperations::insertTRK_CALI
5383                                    
5384                      if ( IsDebug() ) cout << "- - - - - - - - - - -" <<endl;                      if ( IsDebug() ) cout << "- - - - - - - - - - -" <<endl;
5385  //                  cout << path <<endl;  //                  cout << path <<endl;
5386                      cout << "File    : " <<name <<endl;  //                  cout << "File    : " <<name <<endl;
5387  //                  cout << obt0 <<endl;  //                  cout << obt0 <<endl;
5388  //                  cout << timesync <<endl;  //                  cout << timesync <<endl;
5389                      cout << "boot n. : "<<boot_number <<endl;  //                  cout << "boot n. : "<<boot_number <<endl;
5390  //                  cout << raw <<endl;  //                  cout << raw <<endl;
5391    
5392                      // ------------------------------------------------------                      // ------------------------------------------------------
5393                      // ... retrieve the calibration packets.                      // ... retrieve the calibration packets.
5394                      // ------------------------------------------------------                      // ------------------------------------------------------
5395                      TFile *file = new TFile(path+"/"+name);                      if ( IsDebug() ) printf(" file is %s/%s \n",((TString)gSystem->ExpandPathName(path.Data())).Data(),name.Data());
5396                        TFile *file = new TFile(((TString)gSystem->ExpandPathName(path.Data()))+"/"+name); // EM, path could be symbolic and we must expand it
5397                      if(!file)throw -100;                      if(!file)throw -100;
5398                      if(file->IsZombie())throw -100;                      if(file->IsZombie())throw -100;
5399                      //                      //
# Line 5482  Int_t PamelaDBOperations::insertTRK_CALI Line 5450  Int_t PamelaDBOperations::insertTRK_CALI
5450    
5451              if(FOUND){              if(FOUND){
5452    
5453              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  
                 ////////////////////////////////////////////  
5454    
5455                  // ---------------------------------------------------------------------                ////////////////////////////////////////////
5456                  // step 1: insert a new raw file in GL_RAW                // insert a new entry in GL_TRK_CALIB and
5457                  // ---------------------------------------------------------------------                // modify the time-tag of the previous one
5458                  //                ////////////////////////////////////////////
5459                  // check if the raw file already exist  
5460                  //                // ---------------------------------------------------------------------
5461                  UInt_t id_raw = 0;                // step 1: insert a new raw file in GL_RAW
5462                  oss.str("");                // ---------------------------------------------------------------------
5463                  oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";";                //
5464                  if ( IsDebug() ) cout << oss.str().c_str() << endl;                // check if the raw file already exist
5465                  //
5466                  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
5467                  //                oss.str("");
5468                  //                oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";";
5469                  //                if ( IsDebug() ) cout << oss.str().c_str() << endl;
5470                                    
5471                  result = conn->Query(oss.str().c_str());                //                result = conn->Query(oss.str().c_str());
5472                  if ( !result ) throw -4;;                //                if ( !result ) throw -4;;
5473                  if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;                //                if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5474                  if( result->GetRowCount() == 0){                //                if( result->GetRowCount() == 0){
5475                      if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl;                //                    if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl;
5476                      // - - - - - - - - - - -                //                    // - - - - - - - - - - -
5477                      // insert new raw file                //                    // insert new raw file
5478                      // - - - - - - - - - - -                //                    // - - - - - - - - - - -
5479                      GL_RAW glraw = GL_RAW();                    //                    GL_RAW glraw = GL_RAW();    
5480                      glraw.PATH        = gSystem->DirName(raw.Data());                //                    glraw.PATH        = gSystem->DirName(raw.Data());
5481                      glraw.NAME        = gSystem->BaseName(raw.Data());                //                    glraw.NAME        = gSystem->BaseName(raw.Data());
5482                      glraw.BOOT_NUMBER = boot_number;                //                    glraw.BOOT_NUMBER = boot_number;
5483                      //                //                    //
5484                      insertPamelaRawFile( &glraw );                //                    insertPamelaRawFile( &glraw );
5485                      //                              //                    //              
5486                      id_raw = glraw.ID;                              //                    id_raw = glraw.ID;              
5487                  }else{                //                }else{
5488                      row = result->Next();                //                    row = result->Next();
5489                      id_raw = (UInt_t)atoll(row->GetField(0));                //                    id_raw = (UInt_t)atoll(row->GetField(0));
5490                  }                //                }
5491                  if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl;                //                if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl;
5492    
5493                  // ---------------------------------------------------------------------                // ---------------------------------------------------------------------
5494                  // step 1(bis): retrieve the timesync id associated to the file                // step 1(bis): retrieve the timesync id associated to the file
5495                  // (NB, uso lo stesso associato al file iniziale)                // (NB, uso lo stesso associato al file iniziale)
5496                  // ---------------------------------------------------------------------                // ---------------------------------------------------------------------
5497                  UInt_t idtimesync = 0;                UInt_t idtimesync = 0;
5498                  oss.str("");                oss.str("");
5499                  oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";                oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";
5500                  if ( debug ) printf(" %s \n",oss.str().c_str());                if ( debug ) printf(" %s \n",oss.str().c_str());
5501                  result = conn->Query(oss.str().c_str());                result = conn->Query(oss.str().c_str());
5502                  if ( !result ) throw -3;                if ( !result ) throw -3;
5503                  row = result->Next();                  row = result->Next();
5504                  if ( !row ) throw -3;                  if ( !row ) throw -3;
5505                  idtimesync = (UInt_t)atoll(row->GetField(0));                  idtimesync = (UInt_t)atoll(row->GetField(0));
5506                  if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl;                  if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl;
5507                                    
5508                    delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5509                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5510                  // step 2: insert a new root file in GL_ROOT                  // step 2: insert a new root file in GL_ROOT
5511                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5512                  //                  //
5513                  // check if the raw file already exist                  // check if the root file already exist
5514                  //                  //
5515                  UInt_t id_root = 0;                  UInt_t id_root = 0;
5516                  oss.str("");                  oss.str("");
# Line 5559  Int_t PamelaDBOperations::insertTRK_CALI Line 5528  Int_t PamelaDBOperations::insertTRK_CALI
5528                      GL_ROOT glroot = GL_ROOT();                                  GL_ROOT glroot = GL_ROOT();            
5529                      glroot.ID_RAW      = id_raw;                      glroot.ID_RAW      = id_raw;
5530                      glroot.ID_TIMESYNC = idtimesync;                      glroot.ID_TIMESYNC = idtimesync;
5531                      glroot.PATH        = path;                      //
5532                        // 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,
5533                        //             NOT STATIC NOT KEEPENV = $PAM_L0 must be used in the DB
5534                        //
5535                        if ( STATIC ){
5536                          glroot.PATH        = (TString)gSystem->ExpandPathName(path);
5537                        } else {
5538                          if ( KEEPENV ){
5539                            glroot.PATH      = path;
5540                          } else {
5541                            glroot.PATH      = "$PAM_L0";
5542                          };
5543                        };
5544                        //              glroot.PATH        = path;
5545                      glroot.NAME        = name;                      glroot.NAME        = name;
5546                      //                      //
5547                      insertPamelaRootFile( &glroot );                      insertPamelaRootFile( &glroot );
# Line 5571  Int_t PamelaDBOperations::insertTRK_CALI Line 5553  Int_t PamelaDBOperations::insertTRK_CALI
5553                  }                  }
5554                  if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl;                  if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl;
5555    
5556                    delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5557                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5558                  // step 3: modify time-tag of corrupted GL_TRK_CALIB entry                  // step 3: modify time-tag of corrupted GL_TRK_CALIB entry
5559                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
# Line 5583  Int_t PamelaDBOperations::insertTRK_CALI Line 5566  Int_t PamelaDBOperations::insertTRK_CALI
5566                  result = conn->Query(oss.str().c_str());                  result = conn->Query(oss.str().c_str());
5567                  if ( !result ) throw -4;;                  if ( !result ) throw -4;;
5568    
5569                    delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5570    
5571                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
5572                  // step 4: insert the new calibration:                  // step 4: insert the new calibration:
5573                  // ---------------------------------------------------------------------                  // ---------------------------------------------------------------------
# Line 6034  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6019  Int_t PamelaDBOperations::CleanGL_RUN_FR
6019                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6020                //                              //              
6021                oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "                oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "
6022                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< ";";                    << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT();
6023                  oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6024                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6025              };                  };    
6026            };            };
# Line 6083  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 6069  Int_t PamelaDBOperations::CleanGL_RUN_FR
6069                if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){                if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){
6070                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };                  oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6071                //                              //              
6072                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< ";";                oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT();
6073                  oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6074                conn->Query(oss.str().c_str());                  conn->Query(oss.str().c_str());  
6075              };                  };    
6076            };            };
# Line 6336  Int_t PamelaDBOperations::ValidateRuns(T Line 6323  Int_t PamelaDBOperations::ValidateRuns(T
6323      // 2) get the OBT of the last validated run      // 2) get the OBT of the last validated run
6324      // --------------------------------------------------------------      // --------------------------------------------------------------
6325      oss.str("");      oss.str("");
6326      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
6327        oss << " SELECT * FROM GL_RUN  WHERE VALIDATION>0 AND RUNHEADER_TIME<="<< t_start
6328          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
6329      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());
6330      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
# Line 6479  Int_t PamelaDBOperations::ValidateRuns(T Line 6467  Int_t PamelaDBOperations::ValidateRuns(T
6467          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
6468                                                        
6469          if( !CHECK && this_run->VALIDATION ){          if( !CHECK && this_run->VALIDATION ){
6470            for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);            //      for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
6471              for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],this_run->VALIDATION);
6472            nseq=0;            nseq=0;
6473          }          }
6474                                        
# Line 6500  Int_t PamelaDBOperations::ValidateRuns(T Line 6489  Int_t PamelaDBOperations::ValidateRuns(T
6489      if( CHECK ){      if( CHECK ){
6490        // check if calibration exists        // check if calibration exists
6491        if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);        if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
6492        Bool_t MISSING = MissingTRK_CALIB(t1,t2);        //      Bool_t MISSING = MissingTRK_CALIB(t1,t2);
6493        for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);        UInt_t MISSING = MissingTRK_CALIB(t1,t2);
6494          UInt_t val = 0;
6495          if ( MISSING == 1 ) val = 0;
6496          if ( MISSING == 0 ) val = 1;
6497          if ( MISSING == 2 ) val = 2;
6498          for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],val);
6499        nseq=0;        nseq=0;
6500      };      };
6501      //--------------      //--------------
# Line 6531  Int_t PamelaDBOperations::ValidateRuns(T Line 6525  Int_t PamelaDBOperations::ValidateRuns(T
6525   * @param t2 To absolute time   * @param t2 To absolute time
6526   * @return true if there might be a missing calibration   * @return true if there might be a missing calibration
6527   */   */
6528  Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){  //Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
6529    UInt_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
6530                    
6531    GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();    GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
6532                    
6533    // get the closest calibration before the run start (t2)    // get the closest calibration before the run start (t2)
6534    if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);    //>>> missing    //  if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);        //>>> missing
6535      if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(1);       //>>> missing
6536                    
6537    if ( trkcalib->TO_TIME  < t2 ) return(true);                                  //>>> missing    //  if ( trkcalib->TO_TIME  < t2 ) return(true);                                      //>>> missing
6538      if ( trkcalib->TO_TIME  < t2 ) return(1);                                     //>>> missing
6539                    
6540    //==============================================================    //==============================================================
6541    // 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 6552  Bool_t PamelaDBOperations::MissingTRK_CA
6552      //==============================================================      //==============================================================
6553      Bool_t DOWNLOAD = false;      Bool_t DOWNLOAD = false;
6554      // check if the calib was skipped becouse of download .... DA FARE!!      // check if the calib was skipped becouse of download .... DA FARE!!
6555      if(DOWNLOAD)return(false);      //    if(DOWNLOAD)return(false);
6556        if(DOWNLOAD)return(0);
6557                                    
6558      return(true);                                       //>>> missing      //    return(true);                                         //>>> missing
6559        return(1);                                  //>>> missing
6560                                    
6561    };    };
6562                    
# Line 6567  Bool_t PamelaDBOperations::MissingTRK_CA Line 6566  Bool_t PamelaDBOperations::MissingTRK_CA
6566    //==============================================================    //==============================================================
6567    // the long time interval bewteen runs might be due to download    // the long time interval bewteen runs might be due to download
6568    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);
6569    return(false);    if ( trkcalib->VALIDATION ) return(0);
6570      if ( IsDebug() )printf("Calibration is not validated... :-/ ==> OK but with VALIDATION=2! \n");
6571      return(2);
6572                    
6573  };  };
6574  /**  /**
# Line 6576  Bool_t PamelaDBOperations::MissingTRK_CA Line 6577  Bool_t PamelaDBOperations::MissingTRK_CA
6577   * @param validation true/false   * @param validation true/false
6578   */   */
6579  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){  Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
6580      return(this->assignVALIDATION(idrun,(UInt_t)validation));
6581    }
6582    
6583    Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, UInt_t validation){
6584    TSQLResult *result = 0;    TSQLResult *result = 0;
6585    stringstream oss;    stringstream oss;
6586    oss.str("");    oss.str("");
# Line 6772  Int_t PamelaDBOperations::removeFile(TSt Line 6777  Int_t PamelaDBOperations::removeFile(TSt
6777    stringstream myquery;    stringstream myquery;
6778    //      //  
6779    myquery.str("");    myquery.str("");
6780    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() <<"';";
6781    //    //
6782    pResult = conn->Query(myquery.str().c_str());    pResult = conn->Query(myquery.str().c_str());
6783    //    //
# Line 6788  Int_t PamelaDBOperations::removeFile(TSt Line 6793  Int_t PamelaDBOperations::removeFile(TSt
6793    //    //
6794    this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));    this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
6795    this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));    this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
6796      UInt_t idtsy=(UInt_t)atoll(Row->GetField(2));
6797    //    //
6798    this->ValidationOFF();    this->ValidationOFF();
6799    //    //
# Line 6795  Int_t PamelaDBOperations::removeFile(TSt Line 6801  Int_t PamelaDBOperations::removeFile(TSt
6801    //    //
6802    this->RemoveRUNS();    this->RemoveRUNS();
6803    //    //
6804    this->RemoveFILES();    this->RemoveFILES(idtsy);
6805    //    //
6806    this->SetID_ROOT(0);    this->SetID_ROOT(0);
6807    this->SetID_RAW(0);    this->SetID_RAW(0);
# Line 7015  void PamelaDBOperations::RemoveRUNS(){ Line 7021  void PamelaDBOperations::RemoveRUNS(){
7021   * Rearrange calibration tables   * Rearrange calibration tables
7022   *   *
7023   **/   **/
7024  void PamelaDBOperations::RemoveFILES(){  void PamelaDBOperations::RemoveFILES(UInt_t idtsy){
7025    stringstream myquery;    stringstream myquery;
7026    //    //
7027    myquery.str("");    myquery.str("");
# Line 7025  void PamelaDBOperations::RemoveFILES(){ Line 7031  void PamelaDBOperations::RemoveFILES(){
7031    //    //
7032    conn->Query(myquery.str().c_str());    conn->Query(myquery.str().c_str());
7033    //    //
7034      myquery.str("");
7035      myquery << " DELETE FROM GL_ROOT WHERE ID=" <<this->GetID_ROOT() <<";";
7036      //
7037      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
7038      //
7039      conn->Query(myquery.str().c_str());  //
7040      //
7041      if ( !chewbacca ){
7042        myquery.str("");
7043        myquery << " DELETE FROM GL_TIMESYNC WHERE ID=" << idtsy <<";";
7044        //
7045        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
7046        //
7047        conn->Query(myquery.str().c_str());
7048      };
7049      //
7050  };  };
7051    
7052  /**  /**
# Line 7374  UInt_t PamelaDBOperations::ValidateTrkCa Line 7396  UInt_t PamelaDBOperations::ValidateTrkCa
7396    
7397    
7398  }  }
 // 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; // :-)  
   
 // }  
7399    
7400  /**  /**
7401   *   *

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

  ViewVC Help
Powered by ViewVC 1.1.23