/[PAMELA software]/PamelaLevel2/src/PamLevel2.cpp
ViewVC logotype

Diff of /PamelaLevel2/src/PamLevel2.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.82 by mocchiut, Thu Jun 4 15:10:47 2009 UTC revision 1.91 by mocchiut, Thu Dec 15 08:54:12 2011 UTC
# Line 394  void PamTrack::Delete() { Line 394  void PamTrack::Delete() {
394  PamLevel2::PamLevel2() {  PamLevel2::PamLevel2() {
395    Initialize();    Initialize();
396  }  }
397  ;  
398    
399  /**  /**
400   * Constructor   * Constructor
# Line 417  PamLevel2::PamLevel2(TString ddir, TStri Line 417  PamLevel2::PamLevel2(TString ddir, TStri
417      GetPamTree(listf, detlist);      GetPamTree(listf, detlist);
418    if (listf)    if (listf)
419      GetRunTree(listf);      GetRunTree(listf);
420      this->SetMaxShift(-1);
421  }  }
422  ;  
423    
424  PamLevel2::PamLevel2(TString ddir, TList *llist, TString detlist) {  PamLevel2::PamLevel2(TString ddir, TList *llist, TString detlist) {
425    Initialize();    Initialize();
426    GetPamTree(llist, detlist);    GetPamTree(llist, detlist);
427    GetRunTree(llist);    GetRunTree(llist);
428      this->SetMaxShift(-1);
429  }  }
430  ;  
431  /**  /**
432   * Constructor   * Constructor
433   * @param ddir Name of directory where level2 files are stored.   * @param ddir Name of directory where level2 files are stored.
# Line 438  PamLevel2::PamLevel2(TString ddir, TStri Line 440  PamLevel2::PamLevel2(TString ddir, TStri
440    TList* listf = GetListOfLevel2Files(ddir, llist);    TList* listf = GetListOfLevel2Files(ddir, llist);
441    GetPamTree(listf, "");    GetPamTree(listf, "");
442    GetRunTree(listf);    GetRunTree(listf);
443      this->SetMaxShift(-1);
444  }  }
445  ;  
446    
447  void PamLevel2::Initialize() {  void PamLevel2::Initialize() {
448    
# Line 464  void PamLevel2::Initialize() { Line 467  void PamLevel2::Initialize() {
467    soft_obj = 0;// Emiliano    soft_obj = 0;// Emiliano
468    irun = -1LL;    irun = -1LL;
469    irunt = -1LL;    irunt = -1LL;
470      totrunentry = 0LL;
471      totrunentrymax = 0LL;
472      totrunentrymin = 0LL;
473    runfirstentry = 0LL;    runfirstentry = 0LL;
474    runlastentry = 0LL;    runlastentry = 0LL;
475    gltsync = 0; // Emiliano    gltsync = 0; // Emiliano
476      fUpdateRunInfo = true; // Emiliano
477      isSync = false; // by default assume that the level2 file(s) is(are) not sinchronized between L0/DB and L2, that is we miss some packets in L2 due to nested/DV-skipped events
478      il0entry = 0LL;
479      //  hasL0EE = true;
480    
481    l0_file = NULL;    l0_file = NULL;
482    l0_tree = NULL;    l0_tree = NULL;
# Line 474  void PamLevel2::Initialize() { Line 484  void PamLevel2::Initialize() {
484    dbc = 0;    dbc = 0;
485    
486    prevshift = 0;    prevshift = 0;
487    maxshift = 10; //EMILIANO    yprevshift = 0;
488      maxshift = 10; //EMILIANO now overridden by SetMaxShift(-1) called by constructors
489    
490    run_tree = NULL;    run_tree = NULL;
491    run_tree_clone = NULL;    run_tree_clone = NULL;
# Line 633  void PamLevel2::Delete() { Line 644  void PamLevel2::Delete() {
644      //      //
645      TIter next2(contents);      TIter next2(contents);
646      TChain *questa = 0;      TChain *questa = 0;
647      while (questa = (TChain*) next2()) {      while ( (questa = (TChain*)next2()) ) {
648        TString name = questa->GetName();        TString name = questa->GetName();
649        questa->Delete();        questa->Delete();
650        questa = NULL;        questa = NULL;
# Line 646  void PamLevel2::Delete() { Line 657  void PamLevel2::Delete() {
657      run_tree->Delete();;      run_tree->Delete();;
658    if (sel_tree)    if (sel_tree)
659      sel_tree->Delete();;      sel_tree->Delete();;
660    for (Int_t i = 0; i < NCLONES; i++)  
661      // The following lines are commented out since they may generate a double delete error
662      // if the file containing the clone trees is closed. This is because the file owns the
663      // clone trees which are written into it, so it will delete them when it is closed; if
664      // also PamLevel2 will try to delete these trees, a double delete error will be generated
665      // when exiting from analysis program. (Nicola 28/11/2011)
666    
667      /*for (Int_t i = 0; i < NCLONES; i++)
668      if (pam_tree_clone[i])      if (pam_tree_clone[i])
669        pam_tree_clone[i]->Delete();;        pam_tree_clone[i]->Delete();;
670    if (run_tree_clone)    if (run_tree_clone)
671      run_tree_clone->Delete();;      run_tree_clone->Delete();;
672    if (sel_tree_clone)    if (sel_tree_clone)
673      sel_tree_clone->Delete();;      sel_tree_clone->Delete();;*/
674    
675    if (irunoffset)    if (irunoffset)
676      delete[] irunoffset;      delete[] irunoffset;
# Line 747  void PamLevel2::Reset() { Line 765  void PamLevel2::Reset() {
765      //      //
766      TIter next2(contents);      TIter next2(contents);
767      TChain *questa = 0;      TChain *questa = 0;
768      while (questa = (TChain*) next2()) {      while ( (questa = (TChain*) next2()) ) {
769        TString name = questa->GetName();        TString name = questa->GetName();
770        questa->Delete();        questa->Delete();
771        questa = NULL;        questa = NULL;
# Line 792  void PamLevel2::Reset() { Line 810  void PamLevel2::Reset() {
810    soft_obj = 0;// Emiliano    soft_obj = 0;// Emiliano
811    irun = -1;    irun = -1;
812    irunt = -1;    irunt = -1;
813      totrunentry = 0LL;
814      totrunentrymax = 0LL;
815      totrunentrymin = 0LL;
816    runfirstentry = 0ULL;    runfirstentry = 0ULL;
817    runlastentry = 0ULL;    runlastentry = 0ULL;
818      prevabstime = 0ULL;
819      prevpktnum = 0;
820      abstime = 0ULL;
821      pktnum = 0;
822      isFragment = false;
823    //    //
824    totdltime[0] = 0LL;    totdltime[0] = 0LL;
825    totdltime[1] = 0LL;    totdltime[1] = 0LL;
# Line 833  Bool_t PamLevel2::IsGood(Bool_t strict) Line 859  Bool_t PamLevel2::IsGood(Bool_t strict)
859  }  }
860  ;  ;
861    
862    void PamLevel2::SkipRunInfoUpdate(){
863      printf("\n\n ******** WARNING ******** \n Skip DB connections, DO NOT USE PamLevel2::GetRunInfo() method! \n\n");
864      fUpdateRunInfo = false;
865      this->SetSELLI(2);
866      printf(" ===============> W A R N I N G <================ \n");
867      printf(" in case PamLevel2::CreateCloneTrees() will be called \n");
868      printf(" it will be reverted to PadmeAmidala level2 structure , i.e. NO SELECTIONLIST WILL BE CREATED IN THE NEW LEVEL2 FILE! \n\n");
869      if ( run_tree_clone ){
870        printf(" ===============> W A R N I N G <================ \n");
871        printf(" PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() has been called together with PamLevel2::CreateCloneTrees() \n");
872        printf(" TO AVOID CRASHES call PamLevel2::CreateCloneTrees() after PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() \n");    
873      };
874    }
875    
876    void PamLevel2::SetMaxShift(Int_t sh){
877      if ( sh >=  0 ){
878        printf("PamLevel2::SetMaxShift(Int_t) --WARNING-- the default is optimized by checking the level2 file\n it is strongly suggested to let PamLevel2 choose the max shift!\n");
879        maxshift = sh;
880      } else {
881        ULong64_t nev = this->GetEntries();
882        ULong64_t runnev = 0ULL;
883        for (Int_t r=0; r< run_tree->GetEntries();r++){
884          run_tree->GetEntry(r);//update runinfo
885          runnev += GetRunInfo()->NEVENTS;
886        }
887        maxshift = (Int_t)(runnev-nev) + 10; // +10 just to be conservative
888        if ( (runnev-nev) == 0 ) isSync = true;
889        if (DBG) printf("PamLevel2::SetMaxShift(Int_t_) - sh negative %i - nev is %lld runnnev is %lld so maxshift set to %i \n",sh,nev,runnev,maxshift);
890        //    printf("PamLevel2::SetMaxShift(Int_t_) - sh negative %i - nev is %lld runnnev is %lld so maxshift set to %i \n",sh,nev,runnev,maxshift); // TOGLITOGLI
891      }
892    }
893    
894  //--------------------------------------  //--------------------------------------
895  //  //
896  //  //
# Line 2113  TList* PamLevel2::GetListOfLevel2Files(T Line 2171  TList* PamLevel2::GetListOfLevel2Files(T
2171          if (file.EndsWith(".root")) {          if (file.EndsWith(".root")) {
2172            TString filedir;            TString filedir;
2173            cout << ddir << endl;            cout << ddir << endl;
2174            if (ddir != "") {            if ( ddir != "" ) {
2175              filedir = ddir; // take the input dir              filedir = ddir; // take the input dir
2176            }            }
2177            else {            else {
# Line 2121  TList* PamLevel2::GetListOfLevel2Files(T Line 2179  TList* PamLevel2::GetListOfLevel2Files(T
2179              filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir              filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir
2180            };            };
2181            filedir.Append("/");            filedir.Append("/");
2182            char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file));            //          char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file));
2183              char *fullpath = gSystem->ConcatFileName(filedir.Data(), gSystem->BaseName(file));
2184            contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));// add file to the list            contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));// add file to the list
2185            cout << fullpath << endl;            cout << fullpath << endl;
2186            delete fullpath;            delete fullpath;
# Line 2392  TChain *PamLevel2::GetPamTree(TList *fl, Line 2451  TChain *PamLevel2::GetPamTree(TList *fl,
2451      cout << "SelectionList: set branch address RunEntry" << endl;      cout << "SelectionList: set branch address RunEntry" << endl;
2452      L->SetBranchAddress("EventEntry", &irunentry);      L->SetBranchAddress("EventEntry", &irunentry);
2453      cout << "SelectionList: set branch address EventEntry" << endl;      cout << "SelectionList: set branch address EventEntry" << endl;
2454        /*    if ( L->GetBranch("L0EventEntry") ){
2455          hasL0EE = true;
2456          L->SetBranchAddress("L0EventEntry", &il0entry);
2457          cout << "SelectionList: set branch address L0EventEntry" << endl;
2458        } else {
2459          hasL0EE = false; // backward compatibility with old preselected files...
2460          }*/
2461      sel_tree = L;      sel_tree = L;
2462      //          if(!Trout)Trout=O;      //          if(!Trout)Trout=O;
2463      //          else Trout->AddFriend("SelectionList");      //          else Trout->AddFriend("SelectionList");
# Line 2715  TTree *PamLevel2::GetRunTree(TFile *f) { Line 2781  TTree *PamLevel2::GetRunTree(TFile *f) {
2781    return T;    return T;
2782    
2783  }  }
 /**  
  * Update the runinfo informations (to be used to have Run infos event by event basis)  
  * @param run Pointer to the chain/tree which contains run infos  
  * @return true if a new run has been read, false if it is still the same run  
  */  
 Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) {  
   //  
   // check if we have already called once GetEntry, if not call it  
   //  
   cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) --- ATTENZIONE --- NON E` MANTENUTA!!!!!!!.... "  
       << endl;  
   if (!run) {  
     cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing RunInfo tree " << endl;  
     return (false);  
   }  
   if (run->GetEntries() <= 0)  
     return (false);  
   //  
   
   //  Int_t oldrun = irun;  
   Long64_t oldrun = irun;  
   
   // --------------------------------------  
   // if it is a full file (not preselected)  
   // --------------------------------------  
   if (SELLI == 0) {  
   
     //  
     // the absolute time is necessary to relate the event with the run  
     //  
     if (!GetOrbitalInfo() && !ISGP) {  
       cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing OrbitalInfo " << endl;  
       return (false);  
     }  
   
     ULong64_t abstime = 0;  
     if (GetOrbitalInfo())  
       abstime = GetOrbitalInfo()->absTime;  
   
     //  
     // the first time the routine is called, set run search from the beginning  
     //  
     if (irun < 0LL) {  
       irun = 0LL;  
       run->GetEntry(irun);  
       runfirstentry = 0LL;  
       runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);  
       if ((Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL)  
         runlastentry -= 1LL;  
   
       if (ISGP && run->GetEntries() != 1) {  
         cout << "** WARNING ** simulated files are assumed to have 1 single run, not " << run->GetEntries() << endl;  
         cout << "** WARNING ** run will not be updated" << endl;  
       }  
   
     };  
     //  
     if (ISGP)  
       abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO  
     //  
     if (irun == run->GetEntries() - 1LL && !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime  
         <= GetRunInfo()->RUNTRAILER_TIME)) {  
       irun = -1LL;  
       irunt = -1LL;  
       runfirstentry = 0LL;  
       runlastentry = -1LL;  
     };  
     // modificato il controllo sull'aggiornamento del run, per evitare problemi  
     // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)  
     //  
     bool fromfirst = true;  
     //  
     while (!(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME) && irun  
         < run->GetEntries() - 1LL) {  
       //      while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){  
       irun++;  
       run->GetEntry(irun);  
       runfirstentry = runlastentry;  
       if ((Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL)  
         runfirstentry += 1LL;  
       runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);  
       //          cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;  
       //          cout << "runfirstentry "<<runfirstentry<<endl;  
       //          printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));  
       //          printf(" abstime %u trailertime %u \n",abstime,GetRunInfo()->RUNTRAILER_TIME);  
       //          printf(" IDRUN %u \n",GetRunInfo()->ID);  
       //  
       //          prevshift = 0;  
       //  
       if (irun == (Long64_t)(run->GetEntries() - 1LL) && fromfirst && !(abstime >= GetRunInfo()->RUNHEADER_TIME  
           && abstime <= GetRunInfo()->RUNTRAILER_TIME)) {  
         printf(" resetting irun  (it should NOT happen!!!)\n");  
         fromfirst = false;  
         irun = 0;  
         run->GetEntry(irun);  
         runfirstentry = 0ULL;  
         runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);  
         if ((Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL)  
           runlastentry -= 1LL;  
       };  
       //  
     };  
     //  
     if (!(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME)) {  
       printf(" Something very wrong here: cannot find RUN containing absolute time %llu \n", abstime);  
       return false;  
     }  
     //  
     if (irun == oldrun || irun >= run->GetEntries())  
       return (false);  
     //  
     //  printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);  
     //  
     prevshift = 0;  
     cout << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun << endl;  
     //      cout << "runfirstentry "<<runfirstentry<<endl;  
     return (true);  
   };  
   // ----------------------------------------------------  
   // if it is a preselected file (there is SelectionList)  
   // NBNB - the event tree MUST be read first  
   // ----------------------------------------------------  
   if (SELLI == 1) {  
     sel_tree->GetEntry(iev);  
     //      cout << irun << " "<< irunentry << endl;  
     if (irun != oldrun) {  
       run->GetEntry(irun);  
       cout << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun << endl;  
       prevshift = 0;  
       return true;  
     }  
     return false;  
   }  
   
   return false;  
   //  
 }  
 ;  
2784    
2785  Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) {  Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) {
2786    
2787      if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - inside\n");
2788    
2789    if (!run_tree) {    if (!run_tree) {
2790      cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded" << endl;      cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded" << endl;
2791      return false;      return false;
# Line 2865  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 2795  Bool_t PamLevel2::UpdateRunInfo(Long64_t
2795      return (false);      return (false);
2796    }    }
2797    
2798    
2799    Int_t oldrun = irun; // store current run index    Int_t oldrun = irun; // store current run index
2800    
2801    // -----------------------------------------------------------------------    // -----------------------------------------------------------------------
# Line 2874  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 2805  Bool_t PamLevel2::UpdateRunInfo(Long64_t
2805    if (irun < 0) {    if (irun < 0) {
2806      irun = 0LL;      irun = 0LL;
2807      irunt = 0LL;      irunt = 0LL;
2808      irunentry = 0;      totrunentry = 0LL;
2809        totrunentrymin = 0LL;
2810        totrunentrymax = 0LL;
2811        irunentry = 0LL;
2812        il0entry = 0LL;
2813      prevshift = 0;      prevshift = 0;
2814        yprevshift = 0;
2815        prevabstime = 0;
2816        prevpktnum = 0;
2817        abstime = 0ULL;
2818        pktnum = 0;
2819        isFragment = false;
2820      run_tree->GetEntry(irun);      run_tree->GetEntry(irun);
2821      if (!GetOrbitalInfo())      if (!GetOrbitalInfo())
2822        cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated " << endl;        cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated " << endl;
# Line 2886  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 2827  Bool_t PamLevel2::UpdateRunInfo(Long64_t
2827      gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); //Emiliano // the "false" means not to use level0 file (not necessary here)      gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); //Emiliano // the "false" means not to use level0 file (not necessary here)
2828      if (dbc){      if (dbc){
2829        dbc->Close();// Emiliano        dbc->Close();// Emiliano
2830          delete dbc;
2831        dbc=0;        dbc=0;
2832      };      };
2833    };    };
2834    // ---------------------------------------------------------------    // ---------------------------------------------------------------
2835    // retrieve OBT and absolute time of the event    // retrieve OBT and absolute time of the event
2836    // ---------------------------------------------------------------    // ---------------------------------------------------------------
   ULong64_t abstime = 0LL;  
   //    ULong64_t obt     = 0LL; // Emiliano  
2837    Long64_t obt = 0LL; // Emiliano, Long64_t GL_TIMESYNC::DBobt(UInt_t obt) since depending on the situation OBT is lowered or boosted    Long64_t obt = 0LL; // Emiliano, Long64_t GL_TIMESYNC::DBobt(UInt_t obt) since depending on the situation OBT is lowered or boosted
2838      prevabstime = abstime;
2839      prevpktnum = pktnum;
2840    if (GetOrbitalInfo()) {    if (GetOrbitalInfo()) {
2841      abstime = GetOrbitalInfo()->absTime;      abstime = GetOrbitalInfo()->absTime;
2842      obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano      obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano
2843        pktnum = GetOrbitalInfo()->pkt_num; // Emiliano
2844    }    }
2845    else {    else {
2846      abstime = GetRunInfo()->RUNHEADER_TIME;      abstime = GetRunInfo()->RUNHEADER_TIME;
2847      obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano      obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano
2848        pktnum = GetRunInfo()->RUNHEADER_PKT; // Emiliano
2849      }
2850    
2851      if (DBG){
2852        printf("0abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
2853        printf("0        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
2854        printf("0        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
2855        printf("0        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
2856        printf("0        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
2857        printf("0        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
2858    }    }
2859    
2860    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# Line 2922  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 2875  Bool_t PamLevel2::UpdateRunInfo(Long64_t
2875      }      }
2876    
2877      //      //
2878      bool fromfirst = true; // first loop over runs      if ( iev < totrunentrymin || iev > totrunentrymax // entry is outside run limits
2879             || iev == 0 // or it is the first entry
2880             || (!isSync && (
2881                             (abstime <= GetRunInfo()->RUNHEADER_TIME && obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) // or it is outside obt limits (and abstime limits for security reasons)
2882                             || (abstime >= GetRunInfo()->RUNTRAILER_TIME && obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) ))// or it is outside obt limits (and abstime limits for security reasons)
2883             ){ // check on abstime and obt needed to handle nested+DV_skipped packets
2884          
2885          // check for a new run (ma prima il primo!)
2886          if (DBG){
2887            printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
2888            printf("1        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
2889            printf("1        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
2890            printf("1        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
2891            printf("1        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
2892            printf("1        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
2893          }
2894          //        printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
2895          //        printf("1        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
2896          //        printf("1        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
2897          //        printf("1        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
2898          //        printf("1        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
2899          //        printf("1        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
2900    
2901      // ------------------------------------------------------        totrunentry = 0LL;
2902      // loop over runs to find the one that contains the event        runfirstentry = 0LL;
2903      // ------------------------------------------------------        for (Int_t r=0; r< run_tree->GetEntries();r++){
2904      while ((          // -------------------------------------------------------------------
2905      //          (          // save the index of the first entry of the run, relative to pam_tree,
2906      //              !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)          // and read a new run
2907      //                abstime <= GetRunInfo()->RUNTRAILER_TIME) &&          // -------------------------------------------------------------------
2908      //              !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)          run_tree->GetEntry(r);//update runinfo
2909      //                obt <= GetRunInfo()->RUNTRAILER_OBT)          if ( r > 0 ){
2910      //              )            totrunentrymin = totrunentrymax+1;
2911            } else {
2912      !(abstime >= GetRunInfo()->RUNHEADER_TIME && // check on absolute time (s)            totrunentrymin = 0LL;
2913          abstime <= GetRunInfo()->RUNTRAILER_TIME) || !(obt >= gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) && // additional check on OBT (ms) // Emiliano          }
2914          obt <= gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) // Emiliano          totrunentry += GetRunInfo()->NEVENTS;
2915          || GetRunInfo()->NEVENTS == 0          totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
2916      //          || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift) // ERRORE!!! fa saltare i run con 1 evento          irun = r;        
2917          || !(irunentry <= GetRunInfo()->NEVENTS - 1 - prevshift)) && irun < run_tree->GetEntries()) {  
2918            if ( (iev >= totrunentrymin && iev <= totrunentrymax) || // entry is inside run limits
2919        //            if( !(abstime >= GetRunInfo()->RUNHEADER_TIME &&abstime <= GetRunInfo()->RUNTRAILER_TIME)  )cout << "ABS TIME "<<abstime << " " <<GetRunInfo()->RUNTRAILER_TIME <<endl;               ( !isSync &&
2920        //            if( !(obt >= GetRunInfo()->RUNHEADER_OBT && obt <= GetRunInfo()->RUNTRAILER_OBT) )cout << "OBT TIME "<< obt <<" "<< GetRunInfo()->RUNTRAILER_OBT << endl;                 ( abstime >= GetRunInfo()->RUNHEADER_TIME  && obt >= gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) // or it is inside obt limits (and abstime limits for security reasons)
2921        //            if( GetRunInfo()->NEVENTS==0  )cout <<"GetRunInfo()->NEVENTS==0 "<<endl;                   && abstime <= GetRunInfo()->RUNTRAILER_TIME && obt <= gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)))  // or it is inside obt limits (and abstime limits for security reasons)
2922        //            if( !(irunentry <= GetRunInfo()->NEVENTS-1-prevshift) ) cout << "irunentry > "<<GetRunInfo()->NEVENTS-1-prevshift << endl;               ){ // check on abstime and obt needed to handle nested+DV_skipped packets
2923        // - - - - - - - - - - - - -            if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
2924        // irunentry = position of current entry, relative to the run              if ( !isSync ){
2925        // prevshift = shift needed to synchronize l0 and l2 data (nested events)                if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
2926        // - - - - - - - - - - - - -                if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
2927                  //              printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
2928        // -----------------------------------------                //              printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
2929        // store dead and live-time of previous run                prevshift += (totrunentrymin-iev); // add the new shift to total shift
2930        // -----------------------------------------                totrunentrymin -= (totrunentrymin-iev); // shift run position min
2931        //            if(SELLI==0){                totrunentrymax -= (totrunentrymin-iev); // shift run position max
2932        if (SELLI != 2) {                if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
2933          if (fromfirst) {                //              printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
2934            if (oldrun == irun) {              } else {
2935              /// decrement counters                printf(" PamLevel2::UpdateRunInfo(Long64_t) ERROR! sync file but unconsistent totrunetrymin %lld and iev %lld!!! \n",totrunentrymin,iev);
2936              if (GetTrigLevel2()) {                cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
2937                totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time                cout << "\nFor bug reporting instructions, please see for example:\n";
2938                totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time                cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
2939              }                cout << "  " << endl;
             totdltime[2]--; //event counter  
             if (DBG) {  
               cout << endl;  
               cout << "n.events     : " << totdltime[2] << endl;  
               cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;  
               cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;  
2940              }              }
2941            }            }
2942            else {            runfirstentry = totrunentrymin; // first entry of the run in the level2
2943              totdltime[0] = 0;//live-time            
2944              totdltime[1] = 0;//dead-time  
2945              totdltime[2] = 0; //event counter            //
2946              if (DBG)            if (gltsync)
2947                cout << " *** JUMP RUN *** irun " << irun << endl;              delete gltsync; // Emiliano
2948              if (!dbc || (dbc && !dbc->IsConnected()))
2949                SetDBConnection(); //Emiliano
2950              gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
2951              if (dbc){
2952                dbc->Close(); // Emiliano
2953                delete dbc;
2954                dbc=0;
2955              }          
2956              if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
2957                cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
2958                     << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
2959                cout
2960                  << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
2961                  << endl;
2962            }            }
2963            /// add an entry            //
2964            if (run_tree_clone)            if (DBG) printf(" found \n");
2965              if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())            //          printf(" found \n");//TOGLITOGLI
2966                run_tree_clone->GetBranch("DeadLiveTime")->Fill();            //
2967            /// reset counters            break;
2968            }
2969          } // loop over run
2970          
2971          // --------------------------------------
2972          // if there was no need to update the run
2973          // ---> exit with FALSE
2974          // --------------------------------------
2975          if (irun == oldrun){
2976            if (DBG) printf(" no new run \n");
2977            //        printf(" no new run \n");//TOGLITOGLI
2978            return (false);
2979          }      
2980          // --------------------------------------
2981          // ... otherwise
2982          // ---> exit with TRUE
2983          // --------------------------------------
2984    
2985          if (SELLI != 2) {
2986            /// decrement counters since this event belongs to a new run
2987            if (GetTrigLevel2()) {
2988              totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time
2989              totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time
2990            }
2991            totdltime[2]--; //event counter
2992            if (DBG) {
2993              cout << endl;
2994              cout << "n.events     : " << totdltime[2] << endl;
2995              cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;
2996              cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;
2997            }
2998            // add an entry
2999            if (run_tree_clone && totdltime[2] > 0)
3000              if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
3001                run_tree_clone->GetBranch("DeadLiveTime")->Fill();
3002            // reset counters
3003            if ( totdltime[2] > 0 ){
3004            if (GetTrigLevel2()) {            if (GetTrigLevel2()) {
3005              totdltime[0] = GetTrigLevel2()->dltime[0];//live-time              totdltime[0] = GetTrigLevel2()->dltime[0];//live-time
3006              totdltime[1] = 0; //dead-time              totdltime[1] = 0; //dead-time
# Line 2991  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 3008  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3008            totdltime[2] = 1; //event counter            totdltime[2] = 1; //event counter
3009          }          }
3010        }        }
       //            }  
3011    
3012        irun++;        if (DBG){
3013        // ------------------------------------          cout << endl << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun
3014        // if the end of run tree is reached...               << endl;        
3015        // ------------------------------------          printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
3016        if (irun == run_tree->GetEntries()) {          printf("2        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
3017          if (!fromfirst) {          printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
3018            // -----------------------------------------------------          printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
3019            // if it happened already once and the run was not found          printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
3020            // ---> exit with error          printf("2        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
           // -----------------------------------------------------  
           cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- event entry #" << iev  
               << " does not belong to any run (should not happen)" << endl;  
           return false;  
         }  
         // -----------------------------------------  
         // ...otherwise repeat search from beginning  
         // -----------------------------------------  
         cout  
             << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- reached end of run tree. searchin again from beginning "  
             << endl;  
         fromfirst = false;  
         irun = 0LL;  
         runfirstentry = 0LL;  
       }  
       // -------------------------------------------------------------------  
       // save the index of the first entry of the run, relative to pam_tree,  
       // and read a new run  
       // -------------------------------------------------------------------  
       if (irun > 0)  
         runfirstentry += (GetRunInfo()->NEVENTS) - prevshift;  
       irunentry = 0;  
       prevshift = 0;  
       run_tree->GetEntry(irun);//update runinfo  
       irunt = irun - irunoffset[run_tree->GetTreeNumber()];  
       if (gltsync)  
         delete gltsync; // Emiliano  
       if (!dbc || (dbc && !dbc->IsConnected()))  
         SetDBConnection(); //Emiliano  
       gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano  
       if (dbc){  
         dbc->Close(); // Emiliano  
         dbc=0;  
       };  
       if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano  
         cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun  
             << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;  
         cout  
             << "                                                            (NB!! in this case some events are assigned to a wrong run)"  
             << endl;  
3021        }        }
3022        //            if(hasfrag &&  fragid != GetRunInfo()->ID){        //        printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
3023        //                cout << "... where is the next fragment ??"<<endl;        //        printf("2        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
3024        //            }        //        printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
3025      };        //        printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
3026          //        printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
3027      // --------------------------------------        //        printf("2        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
3028      // if there was no need to update the run  
3029      // ---> exit with FALSE        return (true);
3030      // --------------------------------------      } // need for run upgrade
3031      if (irun == oldrun)      if (DBG) printf("return false\n");
3032        return (false);      return (false);
3033      }// SELLI = 0 SELLI = 2
3034      // --------------------------------------    
     // ... otherwise  
     // --------------------------------------  
   
   
     // --------------------------------------  
     // ---> exit with TRUE  
     // --------------------------------------  
     if (DBG)  
       cout << endl << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun  
           << endl;  
     // ----------------------------------------------------  
     // update the tracker parameters  
     // (non ho trovato nessun altro modo sicuro di farlo...)  
     // ----------------------------------------------------  
     if (!dbc || (dbc && !dbc->IsConnected()))  
       SetDBConnection();  
     TrkParams::Set(GetRunInfo(), dbc);  
     if (dbc){  
       dbc->Close();  
       dbc=0;  
     };  
   
     // ----------------------------------------------------  
     // then check if the run has a fragment  
     // in this case we have to switch to the next fragment  
     // when the end of the first fragment is reached  
     // ----------------------------------------------------  
     if (GetRunInfo()->ID_RUN_FRAG != 0 &&  
     //      GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID &&  
         DBG && true) {  
       cout << "* fragment *" << endl;  
     }  
   
     return (true);  
   };  
3035    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
3036    // if it is a preselected file (there is SelectionList)    // if it is a preselected file (there is SelectionList)
3037    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# Line 3170  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 3111  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3111        TrkParams::Set(GetRunInfo(), dbc);        TrkParams::Set(GetRunInfo(), dbc);
3112        if (dbc){        if (dbc){
3113          dbc->Close();          dbc->Close();
3114            delete dbc;
3115          dbc=0;          dbc=0;
3116        };        };
3117        //            cout << endl;        //            cout << endl;
3118        prevshift = 0;        prevshift = 0;
3119          yprevshift = 0;
3120        return true;        return true;
3121      }      }
3122      return false;      return false;
# Line 3182  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 3125  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3125    return false;    return false;
3126    //    //
3127  }  }
3128  ;  
3129    /**
3130     * Update the runinfo informations (to be used to have Run infos event by event basis)
3131     * @param run Pointer to the chain/tree which contains run infos
3132     * @return true if a new run has been read, false if it is still the same run
3133     */
3134    Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) {
3135      return (UpdateRunInfo(iev));
3136    }
3137    
3138  /**  /**
3139   * Update the runinfo informations (to be used to have Run infos event by event basis)   * Update the runinfo informations (to be used to have Run infos event by event basis)
3140   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
# Line 3191  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 3143  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3143  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {
3144    return (UpdateRunInfo((TChain*) run, iev));    return (UpdateRunInfo((TChain*) run, iev));
3145  }  }
3146  ;  
3147    
3148  //--------------------------------------  //--------------------------------------
3149  //  //
# Line 4022  void PamLevel2::CreateCloneTrees(TFile * Line 3974  void PamLevel2::CreateCloneTrees(TFile *
3974      //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");      //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
3975      sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW      sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW
3976      sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");      sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");
3977        //    if ( hasL0EE ) sel_tree_clone->Branch("L0EventEntry", &il0entry, "l0eventry/L");
3978    };    };
3979    
3980    Int_t i = 0;    Int_t i = 0;
# Line 4104  void PamLevel2::CreateCloneTrees(TFile * Line 4057  void PamLevel2::CreateCloneTrees(TFile *
4057    if (GP) {    if (GP) {
4058      pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");      pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");
4059      pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split      pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split
4060      cout << "OrbitalInfo  : branch OrbitalInfo" << endl;      cout << "GPamela  : branch GPamela" << endl;
4061      i++;      i++;
4062    };    };
4063    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
# Line 4177  void PamLevel2::WriteCloneTrees() { Line 4130  void PamLevel2::WriteCloneTrees() {
4130  /**  /**
4131   * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.   * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.
4132   */   */
 //Int_t PamLevel2::GetEntry(Int_t iee){  
4133  Int_t PamLevel2::GetEntry(Long64_t iee) {  Int_t PamLevel2::GetEntry(Long64_t iee) {
4134    
   //     cout << "-------------------------------------"<<endl;  
   //     cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;  
   
4135    if (!pam_tree) {    if (!pam_tree) {
4136      cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded" << endl;      cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded" << endl;
4137      return 0;      return 0;
# Line 4196  Int_t PamLevel2::GetEntry(Long64_t iee) Line 4145  Int_t PamLevel2::GetEntry(Long64_t iee)
4145    //    return 0;    //    return 0;
4146    //    }    //    }
4147    
   Long64_t ii = 0;  
4148    //-------------------------------    //-------------------------------
4149    ii = iee;    if (!pam_tree->GetEntry(iee)) {
   if (!pam_tree->GetEntry(ii)) {  
4150      cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree" << endl;      cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree" << endl;
4151      return 0;      return 0;
4152    }    }
# Line 4219  Int_t PamLevel2::GetEntry(Long64_t iee) Line 4166  Int_t PamLevel2::GetEntry(Long64_t iee)
4166    }    }
4167    
4168    //-------------------------------    //-------------------------------
4169    ii = iee;    //
4170    //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);    if ( fUpdateRunInfo ) UpdateRunInfo(iee); // Emiliano
4171    //    Bool_t UPDATED = UpdateRunInfo(ii);    if (SELLI == 0 || SELLI == 2) irunentry = iee - runfirstentry;
   UpdateRunInfo(ii);  
   if (SELLI == 0 || SELLI == 2)  
     irunentry = iee - runfirstentry;  
   //    if(UPDATED && run_tree_clone)run_tree_clone->Fill();  
   
   //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;  
   
   //     cout << "irunentry     "<<irunentry << endl;  
   //     cout << "runfirstentry "<<runfirstentry << endl;  
   //     cout << "nevents       "<<GetRunInfo()->NEVENTS<< endl;  
   
   //     if( TRK0 || CAL0 || TOF0 ){  
   //    if( !GetYodaEntry( ) ){  
   //        cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading level0 tree"<<endl;  
   //        return 0;  
   //    }  
   //     }  
   
4172    
4173    return 1;    return 1;
4174    
# Line 4280  TTree* PamLevel2::GetYodaTree() { Line 4209  TTree* PamLevel2::GetYodaTree() {
4209    //===================================    //===================================
4210    if (irun < 0) {    if (irun < 0) {
4211      cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;      cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;
4212      //  cout << "In order to use this method you have to first load the RunInfo tree "<<endl;      if (DBG) cout << "In order to use this method you have to first load the RunInfo tree "<<endl;
4213      return NULL;      return NULL;
4214    }    }
4215    Int_t irootnew = run_obj->ID_ROOT_L0;    Int_t irootnew = GetRunInfo()->ID_ROOT_L0;
4216    //      cout << "iroot    "<<iroot<<endl;    if (DBG){
4217    //      cout << "irootnew "<<irootnew<<endl;      cout << "iroot    "<<iroot<<endl;
4218        cout << "irootnew "<<irootnew<<endl;
4219      }
4220    
4221    //===================================    //===================================
4222    // load the level0 file    // load the level0 file
# Line 4323  TTree* PamLevel2::GetYodaTree() { Line 4254  TTree* PamLevel2::GetYodaTree() {
4254      if (!h0_obj)      if (!h0_obj)
4255        h0_obj = new EventHeader();        h0_obj = new EventHeader();
4256      l0_tree->SetBranchAddress("Header", &h0_obj);      l0_tree->SetBranchAddress("Header", &h0_obj);
4257      prevshift = 0;      yprevshift = 0; // yes, yprevshift is the shift in the level0, prevshift is the shift in the level2
4258      //---------------------------------------------------      //---------------------------------------------------
4259      // TRACKER:      // TRACKER:
4260      if (TRK0) {      if (TRK0) {
# Line 4341  TTree* PamLevel2::GetYodaTree() { Line 4272  TTree* PamLevel2::GetYodaTree() {
4272          calo0_obj->Set();          calo0_obj->Set();
4273        };        };
4274        l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());        l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());
       //            cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;  
4275      }      }
4276      //---------------------------------------------------      //---------------------------------------------------
4277      // TOF:      // TOF:
# Line 4350  TTree* PamLevel2::GetYodaTree() { Line 4280  TTree* PamLevel2::GetYodaTree() {
4280      }      }
4281    
4282      dbc->Close(); // EMILIANO, do not leave open connections, open only when needed      dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
4283        delete dbc;
4284      dbc=0;      dbc=0;
4285    
4286    };    };
4287    
   //     if(!dbc || (dbc && !dbc->IsConnected())){  
   //    cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;  
   //     }  
   
4288    if (TRK0) {    if (TRK0) {
4289      //  TrkParams::Load(6);      if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection();
     //  if( !TrkParams::IsLoaded(6) ){  
     //      cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- VK-mask not loaded"<<endl;  
     //  };  
     if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection(); // PERCHE` ERA COMMENTATA QUESTA RIGA?  
4290      TrkParams::SetCalib(run_obj, dbc);      TrkParams::SetCalib(run_obj, dbc);
4291      TrkParams::LoadCalib();      TrkParams::LoadCalib();
4292      if (!TrkParams::CalibIsLoaded()) {      if (!TrkParams::CalibIsLoaded()) {
# Line 4371  TTree* PamLevel2::GetYodaTree() { Line 4294  TTree* PamLevel2::GetYodaTree() {
4294      };      };
4295      if(dbc){      if(dbc){
4296        dbc->Close(); // EMILIANO, do not leave open connections, open only when needed        dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
4297          delete dbc;
4298        dbc=0;        dbc=0;
4299      };      };
4300    }    }
   
   //    cout << l0_tree << endl;  
4301    return l0_tree;    return l0_tree;
   
4302  }  }
4303    
4304  /**  /**
# Line 4385  TTree* PamLevel2::GetYodaTree() { Line 4306  TTree* PamLevel2::GetYodaTree() {
4306   */   */
4307  Int_t PamLevel2::GetYodaEntry() {  Int_t PamLevel2::GetYodaEntry() {
4308    
4309    //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;    Long64_t iev = this->GetReadEntry();
   if (!GetYodaTree())  
     return 0;  
4310    
4311    // patch    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4312    if (irunentry < 0) {    // if it is a full file (not preselected)
4313      //  cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4314      irunentry = 0LL;    //  if (SELLI == 0 || SELLI == 2 || !hasL0EE) {
   }  
   //  ---------------------------------  
   //  if file is NOT a preselected file  
   //  ---------------------------------  
   Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM);  
   //     cout << " irunentry "<<irunentry<<endl;  
   //     cout << " EV_FROM "<<run_obj->EV_FROM<<endl;  
   //     cout << " quellagiusta = irunentry + EV_FROM "<< quellagiusta << endl;  
   
   //     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;  
   //     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;  
   //     cout << " time "<< abstime << endl;  
   //     cout << " trk_calib_used "<<run_obj->TRK_CALIB_USED<< endl;  
4315    
4316    ULong64_t obt = 0;      if (!GetYodaTree()){
4317    ULong64_t pktn = 0;        printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
4318    if (GetOrbitalInfo()) {        return 0;
4319      obt = GetOrbitalInfo()->OBT;      }
     pktn = GetOrbitalInfo()->pkt_num;  
   }  
4320    
4321    if (!GetOrbitalInfo() && !ISGP) {      if (irunentry < 0) {
4322      cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;        if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
4323      return 0;        //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
4324    }        irunentry = 0LL;
4325    if (obt == 0 && pktn == 0 && !ISGP) {      }
4326      cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;      //  ---------------------------------
4327      return 0;      //  if file is NOT a preselected file
4328    }      //  ---------------------------------
4329        Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
4330    
4331        if (DBG){
4332          cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
4333          cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
4334          cout << " time "<< abstime << endl;
4335        }
4336    
4337        ULong64_t obt = 0;
4338        ULong64_t pktn = 0;
4339        if (GetOrbitalInfo()) {
4340          obt = GetOrbitalInfo()->OBT;
4341          pktn = GetOrbitalInfo()->pkt_num;
4342        }
4343    
4344    // ---------------------------------------------------------------------      if (!GetOrbitalInfo() && !ISGP) {
4345    // ATTENTION!!!        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;
4346    // If data are not pre-processed with cleaner, the level0 tree may contain        return 0;
   // spurious nested physics packets.  
   // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries  
   // while level2 tree DOES NOT!!  
   // This means that "quellagiusta" in these cases is not correct.  
   // In order to retrieve the correct level0 event, I implemented a check  
   // of the OBT and pkt-number. In case of mismatch, the level0 entry number  
   // is shift forward until when the packets match.  
   // ---------------------------------------------------------------------  
   Int_t answer = 0;  
   Int_t shift = 0;  
   //    printf(" siamo qui %i %i \n",shift,prevshift);  
   //    Int_t maxshift = 50; // EMILIANO  
   do {  
     if (shift > 0) {  
       cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;  
       if (DBG)  
         cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "  
             << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;  
       if (DBG)  
         cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift  
             << " prevshift " << prevshift << " )" << endl;  
4347      }      }
4348      answer = l0_tree->GetEntry(quellagiusta + (Long64_t) shift + (Long64_t) prevshift);      if (obt == 0 && pktn == 0 && !ISGP) {
4349      shift++;        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;
     if (!GetEventHeader()) {  
       cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;  
4350        return 0;        return 0;
4351      }      }
4352    
4353        // ---------------------------------------------------------------------
4354        // ATTENTION!!!
4355        // If data are not pre-processed with cleaner, the level0 tree may contain
4356        // spurious nested physics packets.
4357        // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
4358        // while level2 tree DOES NOT!!
4359        // This means that "quellagiusta" in these cases is not correct.
4360        // In order to retrieve the correct level0 event, I implemented a check
4361        // of the OBT and pkt-number. In case of mismatch, the level0 entry number
4362        // is shift forward until when the packets match.
4363        // ---------------------------------------------------------------------
4364        Long64_t shift = 0LL;
4365        Long64_t answer = quellagiusta + shift + yprevshift;
4366        Int_t readl0 = 0;
4367        readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
4368    
4369        if (DBG){
4370          printf(" siamo qui shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4371        }
4372    
4373    
4374      if (ISGP) {      if (ISGP) {
4375        obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO        obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
4376        pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO        pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
4377      }      }
4378    
4379      //  cout << "PKTNUM "<<shift<<" ==  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;      while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){
4380      //  cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;        if ( isSync && shift == 0LL ){
4381      //  if( (quellagiusta+shift) == l0_tree->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;          printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found in place!!! \n");
4382      //  cout << " obt "<< obt << endl;          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4383      //  cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;          cout << "\nFor bug reporting instructions, please see for example:\n";
4384      //  cout << " pktn "<< pktn << endl;          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4385      //  cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;          cout << "  " << endl;
4386      //  printf(" IDRUN %u \n",GetRunInfo()->ID);        }
4387      //        if (shift > 0) {
4388      if (prevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {          cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
4389        prevshift = 0;          if (DBG)
4390        shift = -1;            cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
4391      };                 << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
4392            if (DBG)
4393              cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
4394                   << " prevshift " << prevshift << " )" << endl;
4395          }
4396          answer = quellagiusta +  shift+ yprevshift;
4397          readl0 = l0_tree->GetEntry(answer);
4398          //      printf(" inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4399    
4400          if (!GetEventHeader()) {
4401            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
4402            return 0;
4403          }
4404    
4405    } while ((obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(        //
4406        GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift)        if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
4407        < GetYodaTree()->GetEntries() && shift < maxshift);          if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4408            //        printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4409            yprevshift = 0LL;
4410            shift = -1LL;
4411          };
4412        
4413          shift++;
4414        }
4415                                      
4416    
4417    if ((quellagiusta + (Long64_t) shift + (Long64_t) prevshift) > GetYodaTree()->GetEntries() || shift == maxshift) {      if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
4418      cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;        if ( isSync ){
4419      return 0;          printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found AT ALL!!! \n");
4420    }          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4421    //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;          cout << "\nFor bug reporting instructions, please see for example:\n";
4422    //    return GetYodaTree()->GetEntry(quellagiusta);          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4423    if (shift > 1)          cout << "  " << endl;
4424      prevshift += (shift - 1);        }
4425          cout << "Int_t PamLevel2::GetYodaEntry() -- WARNING -- " << endl;
4426          cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;    
4427          cout << " Nested and/or DarthVader skipped packets in fragmented run? checking and trying to fix " <<endl;
4428          // query the DB for runs containing the event, loop over LEVEL0 files which could contain the level0 event and try to find it
4429          // ma nel mezzo del cammin per ogni run che pesco devo vedere la posizione relativa di iev rispetto al runheader nel livello2 per andare a cercare nel posto giusto
4430          // connect to db
4431          if (!dbc || (dbc && !dbc->IsConnected())) SetDBConnection(); //Emiliano
4432          //
4433          if (GetOrbitalInfo()){
4434            abstime = GetOrbitalInfo()->absTime;
4435          } else {
4436            printf(" PamLevel2::GetYodaEntry() ERROR! no OrbitalInfo, cannot get the absolute time for event \n");
4437            return 0;
4438          }
4439          // query DB looking for runs containing the processed event
4440          TSQLResult *pResult;
4441          TSQLRow *Row = NULL;
4442          TString myquery = Form("select ID,NEVENTS from GL_RUN where RUNHEADER_TIME<=%lld and RUNTRAILER_TIME>=%lld;",abstime,abstime);
4443          if ( DBG ) printf(" query is %s \n",myquery.Data());
4444          //      printf(" query is %s \n",myquery.Data());// TOGLITOGLI
4445          pResult = dbc->Query(myquery.Data());    
4446          if (!pResult->GetRowCount()){
4447            printf(" PamLevel2::GetYodaEntry() ERROR! event is not included in any run!!! \n");
4448            cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4449            cout << "\nFor bug reporting instructions, please see for example:\n";
4450            cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4451            cout << "  " << endl;
4452            return 0;
4453          }
4454          if ( pResult->GetRowCount() == 1 ){
4455            if (DBG) printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");
4456            //        printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");//TOGLITOGLI
4457          }
4458          for( Int_t ru=0; ru < pResult->GetRowCount(); ru++){ // loop over runs containing the event
4459            if (Row) delete Row;
4460            Row = pResult->Next();  
4461            if( Row == NULL ) break;
4462            UInt_t idrun = (UInt_t)atoll(Row->GetField(0));
4463            UInt_t nev = (UInt_t)atoll(Row->GetField(1));
4464            if (DBG) printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);
4465            //        printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);//TOGLITOGLI
4466    
4467            // now look for this run in the level2 file, it must be present! code is taken from updateruninfo of course
4468            Bool_t rfound = false;
4469            totrunentry = 0LL;
4470            runfirstentry = 0LL;
4471            for (Int_t r=0; r< run_tree->GetEntries();r++){
4472              run_tree->GetEntry(r);//update runinfo
4473              if ( r > 0 ){
4474                totrunentrymin = totrunentrymax+1;
4475              } else {
4476                totrunentrymin = 0LL;
4477              }
4478              totrunentry += GetRunInfo()->NEVENTS;
4479              totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
4480              irun = r;        
4481    
4482              if (idrun == GetRunInfo()->ID){
4483                if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
4484                  if (DBG) printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
4485                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4486                  //              printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
4487                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4488                  prevshift += (totrunentrymin-iev); // add the new shift to total shift
4489                  totrunentrymin -= (totrunentrymin-iev); // shift run position min
4490                  totrunentrymax -= (totrunentrymin-iev); // shift run position max
4491                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4492                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4493                }
4494                runfirstentry = totrunentrymin; // first entry of the run in the level2
4495              
4496    
4497    return answer;              //
4498                if (gltsync)
4499                  delete gltsync; // Emiliano
4500                if (!dbc || (dbc && !dbc->IsConnected()))
4501                  SetDBConnection(); //Emiliano
4502                gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
4503                if (dbc){
4504                  dbc->Close(); // Emiliano
4505                  delete dbc;
4506                  dbc=0;
4507                }          
4508                if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
4509                  cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
4510                       << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
4511                  cout
4512                    << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
4513                    << endl;
4514                }
4515                //
4516                if (DBG) printf(" found \n");
4517                //            printf(" found \n");//TOGLITOGLI
4518                rfound = true;
4519                //
4520                break;
4521              }
4522            } // loop over run
4523            if ( !rfound ){
4524              printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level2 file!!! \n");
4525              cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4526              cout << "\nFor bug reporting instructions, please see for example:\n";
4527              cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4528              cout << "  " << endl;        
4529              return 0;
4530            }
4531    
4532            // here we got the first run and we can check if it contains the level0 event
4533            if (!GetYodaTree()){
4534              printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
4535              return 0;
4536            }      
4537    
4538            // get the current run entry
4539            irunentry = iev - runfirstentry;
4540            if (irunentry < 0) {
4541              if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
4542              //          cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
4543              irunentry = 0LL;
4544            }
4545            //  ---------------------------------
4546            //  if file is NOT a preselected file
4547            //  ---------------------------------
4548            quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
4549          
4550            if (DBG){
4551              cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
4552              cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
4553              cout << " time "<< abstime << endl;
4554            }
4555            //        cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
4556            //        cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
4557            //        cout << " time "<< abstime << endl; // TOGLITOGLI
4558          
4559            shift = 0;
4560            answer = quellagiusta + shift + yprevshift; // prevshift already included in irunentry
4561            readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
4562    
4563            if (DBG){
4564              printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4565            }
4566            //        printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4567          
4568            while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){
4569              if (shift > 0) {
4570                cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
4571                if (DBG)
4572                  cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
4573                       << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
4574                if (DBG)
4575                  cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
4576                       << " prevshift " << prevshift << " )" << endl;
4577              }
4578              answer = quellagiusta +  shift+ yprevshift;
4579              readl0 = l0_tree->GetEntry(answer);
4580              //          printf(" inside inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4581            
4582              if (!GetEventHeader()) {
4583                cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
4584                return 0;
4585              }
4586              //
4587              if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
4588                if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4589                //            printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4590                yprevshift = 0;
4591                shift = -1;
4592              };
4593            
4594              shift++;
4595            }
4596          
4597            if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
4598              //still not the good run... continue with the nex one!
4599              printf("still not the good run... continue with the nex one!\n");
4600            } else {
4601              if (DBG) cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
4602              //          cout << "LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;//TOGLITOGLI
4603              if (shift > 1) yprevshift = (shift - 1);
4604              if (Row) delete Row;
4605              delete pResult;  
4606              if (dbc){
4607                dbc->Close(); // Emiliano
4608                delete dbc;
4609                dbc=0;
4610              }    
4611              il0entry = answer;
4612              return readl0;
4613            }
4614            // perhaps it is all
4615          }// loop over runs containing the event
4616          if (Row) delete Row;
4617          delete pResult;  
4618          if (dbc){
4619            dbc->Close(); // Emiliano
4620            delete dbc;
4621            dbc=0;
4622          }          
4623          // arriving here it means no run found, cannot be! error!
4624          printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level0 files!!! \n");
4625          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4626          cout << "\nFor bug reporting instructions, please see for example:\n";
4627          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4628          cout << "  " << endl;        
4629          return 0;
4630        } else {
4631          if (DBG) cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
4632          //    cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
4633          //    printf("obt %lld (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) %i  pktn %lld (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter() %i \n",obt,(UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()), pktn, (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) );
4634          if (shift > 1) yprevshift = (shift - 1);
4635          il0entry = answer;
4636          return readl0;
4637        }
4638    
4639        /*  } // if selli 0 || 2
4640      if ( SELLI == 1 && hasL0EE ){
4641        sel_tree->GetEntry(iev);  
4642        Long64_t answer = il0entry;
4643        Int_t readl0 = 0;
4644        readl0 = l0_tree->GetEntry(answer);
4645        return readl0;
4646        }*/
4647      printf(" PamLevel2::GetYodaEntry() ERROR! \n");
4648      cout << " Entry not found! OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4649      cout << "\nFor bug reporting instructions, please see for example:\n";
4650      cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4651      cout << "  " << endl;
4652      return 0;
4653  }  }
4654    
4655  /**  /**
4656   * \Brief Set DB connection   * \Brief Set DB connection
4657   */   */

Legend:
Removed from v.1.82  
changed lines
  Added in v.1.91

  ViewVC Help
Powered by ViewVC 1.1.23