/[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.92 by mocchiut, Thu Dec 15 14:36:05 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              TrkParams::Set(GetRunInfo(), dbc);
2952              if (dbc){
2953                dbc->Close(); // Emiliano
2954                delete dbc;
2955                dbc=0;
2956              }          
2957              if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
2958                cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
2959                     << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
2960                cout
2961                  << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
2962                  << endl;
2963            }            }
2964            /// add an entry            //
2965            if (run_tree_clone)            if (DBG) printf(" found \n");
2966              if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())            //          printf(" found \n");//TOGLITOGLI
2967                run_tree_clone->GetBranch("DeadLiveTime")->Fill();            //
2968            /// reset counters            break;
2969            }
2970          } // loop over run
2971          
2972          // --------------------------------------
2973          // if there was no need to update the run
2974          // ---> exit with FALSE
2975          // --------------------------------------
2976          if (irun == oldrun){
2977            if (DBG) printf(" no new run \n");
2978            //        printf(" no new run \n");//TOGLITOGLI
2979            return (false);
2980          }      
2981          // --------------------------------------
2982          // ... otherwise
2983          // ---> exit with TRUE
2984          // --------------------------------------
2985    
2986          if (SELLI != 2) {
2987            /// decrement counters since this event belongs to a new run
2988            if (GetTrigLevel2()) {
2989              totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time
2990              totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time
2991            }
2992            totdltime[2]--; //event counter
2993            if (DBG) {
2994              cout << endl;
2995              cout << "n.events     : " << totdltime[2] << endl;
2996              cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;
2997              cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;
2998            }
2999            // add an entry
3000            if (run_tree_clone && totdltime[2] > 0)
3001              if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
3002                run_tree_clone->GetBranch("DeadLiveTime")->Fill();
3003            // reset counters
3004            if ( totdltime[2] > 0 ){
3005            if (GetTrigLevel2()) {            if (GetTrigLevel2()) {
3006              totdltime[0] = GetTrigLevel2()->dltime[0];//live-time              totdltime[0] = GetTrigLevel2()->dltime[0];//live-time
3007              totdltime[1] = 0; //dead-time              totdltime[1] = 0; //dead-time
# Line 2991  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 3009  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3009            totdltime[2] = 1; //event counter            totdltime[2] = 1; //event counter
3010          }          }
3011        }        }
       //            }  
3012    
3013        irun++;        if (DBG){
3014        // ------------------------------------          cout << endl << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun
3015        // if the end of run tree is reached...               << endl;        
3016        // ------------------------------------          printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
3017        if (irun == run_tree->GetEntries()) {          printf("2        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
3018          if (!fromfirst) {          printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
3019            // -----------------------------------------------------          printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
3020            // if it happened already once and the run was not found          printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
3021            // ---> 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;  
3022        }        }
3023        //            if(hasfrag &&  fragid != GetRunInfo()->ID){        //        printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
3024        //                cout << "... where is the next fragment ??"<<endl;        //        printf("2        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
3025        //            }        //        printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
3026      };        //        printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
3027          //        printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
3028      // --------------------------------------        //        printf("2        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
3029      // if there was no need to update the run  
3030      // ---> exit with FALSE        return (true);
3031      // --------------------------------------      } // need for run upgrade
3032      if (irun == oldrun)      if (DBG) printf("return false\n");
3033        return (false);      return (false);
3034      }// SELLI = 0 SELLI = 2
3035      // --------------------------------------    
     // ... 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);  
   };  
3036    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
3037    // if it is a preselected file (there is SelectionList)    // if it is a preselected file (there is SelectionList)
3038    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# Line 3170  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 3112  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3112        TrkParams::Set(GetRunInfo(), dbc);        TrkParams::Set(GetRunInfo(), dbc);
3113        if (dbc){        if (dbc){
3114          dbc->Close();          dbc->Close();
3115            delete dbc;
3116          dbc=0;          dbc=0;
3117        };        };
3118        //            cout << endl;        //            cout << endl;
3119        prevshift = 0;        prevshift = 0;
3120          yprevshift = 0;
3121        return true;        return true;
3122      }      }
3123      return false;      return false;
# Line 3182  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 3126  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3126    return false;    return false;
3127    //    //
3128  }  }
3129  ;  
3130    /**
3131     * Update the runinfo informations (to be used to have Run infos event by event basis)
3132     * @param run Pointer to the chain/tree which contains run infos
3133     * @return true if a new run has been read, false if it is still the same run
3134     */
3135    Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) {
3136      return (UpdateRunInfo(iev));
3137    }
3138    
3139  /**  /**
3140   * 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)
3141   * @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 3144  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3144  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {
3145    return (UpdateRunInfo((TChain*) run, iev));    return (UpdateRunInfo((TChain*) run, iev));
3146  }  }
3147  ;  
3148    
3149  //--------------------------------------  //--------------------------------------
3150  //  //
# Line 4022  void PamLevel2::CreateCloneTrees(TFile * Line 3975  void PamLevel2::CreateCloneTrees(TFile *
3975      //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");      //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
3976      sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW      sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW
3977      sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");      sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");
3978        //    if ( hasL0EE ) sel_tree_clone->Branch("L0EventEntry", &il0entry, "l0eventry/L");
3979    };    };
3980    
3981    Int_t i = 0;    Int_t i = 0;
# Line 4104  void PamLevel2::CreateCloneTrees(TFile * Line 4058  void PamLevel2::CreateCloneTrees(TFile *
4058    if (GP) {    if (GP) {
4059      pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");      pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");
4060      pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split      pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split
4061      cout << "OrbitalInfo  : branch OrbitalInfo" << endl;      cout << "GPamela  : branch GPamela" << endl;
4062      i++;      i++;
4063    };    };
4064    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
# Line 4177  void PamLevel2::WriteCloneTrees() { Line 4131  void PamLevel2::WriteCloneTrees() {
4131  /**  /**
4132   * 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.
4133   */   */
 //Int_t PamLevel2::GetEntry(Int_t iee){  
4134  Int_t PamLevel2::GetEntry(Long64_t iee) {  Int_t PamLevel2::GetEntry(Long64_t iee) {
4135    
   //     cout << "-------------------------------------"<<endl;  
   //     cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;  
   
4136    if (!pam_tree) {    if (!pam_tree) {
4137      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;
4138      return 0;      return 0;
# Line 4196  Int_t PamLevel2::GetEntry(Long64_t iee) Line 4146  Int_t PamLevel2::GetEntry(Long64_t iee)
4146    //    return 0;    //    return 0;
4147    //    }    //    }
4148    
   Long64_t ii = 0;  
4149    //-------------------------------    //-------------------------------
4150    ii = iee;    if (!pam_tree->GetEntry(iee)) {
   if (!pam_tree->GetEntry(ii)) {  
4151      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;
4152      return 0;      return 0;
4153    }    }
# Line 4219  Int_t PamLevel2::GetEntry(Long64_t iee) Line 4167  Int_t PamLevel2::GetEntry(Long64_t iee)
4167    }    }
4168    
4169    //-------------------------------    //-------------------------------
4170    ii = iee;    //
4171    //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);    if ( fUpdateRunInfo ) UpdateRunInfo(iee); // Emiliano
4172    //    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;  
   //    }  
   //     }  
   
4173    
4174    return 1;    return 1;
4175    
# Line 4280  TTree* PamLevel2::GetYodaTree() { Line 4210  TTree* PamLevel2::GetYodaTree() {
4210    //===================================    //===================================
4211    if (irun < 0) {    if (irun < 0) {
4212      cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;      cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;
4213      //  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;
4214      return NULL;      return NULL;
4215    }    }
4216    Int_t irootnew = run_obj->ID_ROOT_L0;    Int_t irootnew = GetRunInfo()->ID_ROOT_L0;
4217    //      cout << "iroot    "<<iroot<<endl;    if (DBG){
4218    //      cout << "irootnew "<<irootnew<<endl;      cout << "iroot    "<<iroot<<endl;
4219        cout << "irootnew "<<irootnew<<endl;
4220      }
4221    
4222    //===================================    //===================================
4223    // load the level0 file    // load the level0 file
# Line 4323  TTree* PamLevel2::GetYodaTree() { Line 4255  TTree* PamLevel2::GetYodaTree() {
4255      if (!h0_obj)      if (!h0_obj)
4256        h0_obj = new EventHeader();        h0_obj = new EventHeader();
4257      l0_tree->SetBranchAddress("Header", &h0_obj);      l0_tree->SetBranchAddress("Header", &h0_obj);
4258      prevshift = 0;      yprevshift = 0; // yes, yprevshift is the shift in the level0, prevshift is the shift in the level2
4259      //---------------------------------------------------      //---------------------------------------------------
4260      // TRACKER:      // TRACKER:
4261      if (TRK0) {      if (TRK0) {
# Line 4341  TTree* PamLevel2::GetYodaTree() { Line 4273  TTree* PamLevel2::GetYodaTree() {
4273          calo0_obj->Set();          calo0_obj->Set();
4274        };        };
4275        l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());        l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());
       //            cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;  
4276      }      }
4277      //---------------------------------------------------      //---------------------------------------------------
4278      // TOF:      // TOF:
# Line 4350  TTree* PamLevel2::GetYodaTree() { Line 4281  TTree* PamLevel2::GetYodaTree() {
4281      }      }
4282    
4283      dbc->Close(); // EMILIANO, do not leave open connections, open only when needed      dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
4284        delete dbc;
4285      dbc=0;      dbc=0;
4286    
4287    };    };
4288    
   //     if(!dbc || (dbc && !dbc->IsConnected())){  
   //    cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;  
   //     }  
   
4289    if (TRK0) {    if (TRK0) {
4290      //  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?  
4291      TrkParams::SetCalib(run_obj, dbc);      TrkParams::SetCalib(run_obj, dbc);
4292      TrkParams::LoadCalib();      TrkParams::LoadCalib();
4293      if (!TrkParams::CalibIsLoaded()) {      if (!TrkParams::CalibIsLoaded()) {
# Line 4371  TTree* PamLevel2::GetYodaTree() { Line 4295  TTree* PamLevel2::GetYodaTree() {
4295      };      };
4296      if(dbc){      if(dbc){
4297        dbc->Close(); // EMILIANO, do not leave open connections, open only when needed        dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
4298          delete dbc;
4299        dbc=0;        dbc=0;
4300      };      };
4301    }    }
   
   //    cout << l0_tree << endl;  
4302    return l0_tree;    return l0_tree;
   
4303  }  }
4304    
4305  /**  /**
# Line 4385  TTree* PamLevel2::GetYodaTree() { Line 4307  TTree* PamLevel2::GetYodaTree() {
4307   */   */
4308  Int_t PamLevel2::GetYodaEntry() {  Int_t PamLevel2::GetYodaEntry() {
4309    
4310    //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;    Long64_t iev = this->GetReadEntry();
   if (!GetYodaTree())  
     return 0;  
4311    
4312    // patch    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4313    if (irunentry < 0) {    // if it is a full file (not preselected)
4314      //  cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4315      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;  
4316    
4317    ULong64_t obt = 0;      if (!GetYodaTree()){
4318    ULong64_t pktn = 0;        printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
4319    if (GetOrbitalInfo()) {        return 0;
4320      obt = GetOrbitalInfo()->OBT;      }
     pktn = GetOrbitalInfo()->pkt_num;  
   }  
4321    
4322    if (!GetOrbitalInfo() && !ISGP) {      if (irunentry < 0) {
4323      cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;        if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
4324      return 0;        //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
4325    }        irunentry = 0LL;
4326    if (obt == 0 && pktn == 0 && !ISGP) {      }
4327      cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;      //  ---------------------------------
4328      return 0;      //  if file is NOT a preselected file
4329    }      //  ---------------------------------
4330        Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
4331    
4332        if (DBG){
4333          cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
4334          cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
4335          cout << " time "<< abstime << endl;
4336        }
4337    
4338        ULong64_t obt = 0;
4339        ULong64_t pktn = 0;
4340        if (GetOrbitalInfo()) {
4341          obt = GetOrbitalInfo()->OBT;
4342          pktn = GetOrbitalInfo()->pkt_num;
4343        }
4344    
4345    // ---------------------------------------------------------------------      if (!GetOrbitalInfo() && !ISGP) {
4346    // ATTENTION!!!        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;
4347    // 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;  
4348      }      }
4349      answer = l0_tree->GetEntry(quellagiusta + (Long64_t) shift + (Long64_t) prevshift);      if (obt == 0 && pktn == 0 && !ISGP) {
4350      shift++;        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;
     if (!GetEventHeader()) {  
       cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;  
4351        return 0;        return 0;
4352      }      }
4353    
4354        // ---------------------------------------------------------------------
4355        // ATTENTION!!!
4356        // If data are not pre-processed with cleaner, the level0 tree may contain
4357        // spurious nested physics packets.
4358        // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
4359        // while level2 tree DOES NOT!!
4360        // This means that "quellagiusta" in these cases is not correct.
4361        // In order to retrieve the correct level0 event, I implemented a check
4362        // of the OBT and pkt-number. In case of mismatch, the level0 entry number
4363        // is shift forward until when the packets match.
4364        // ---------------------------------------------------------------------
4365        Long64_t shift = 0LL;
4366        Long64_t answer = quellagiusta + shift + yprevshift;
4367        Int_t readl0 = 0;
4368        readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
4369    
4370        if (DBG){
4371          printf(" siamo qui shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4372        }
4373    
4374    
4375      if (ISGP) {      if (ISGP) {
4376        obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO        obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
4377        pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO        pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
4378      }      }
4379    
4380      //  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 ){
4381      //  cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;        if ( isSync && shift == 0LL ){
4382      //  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");
4383      //  cout << " obt "<< obt << endl;          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4384      //  cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;          cout << "\nFor bug reporting instructions, please see for example:\n";
4385      //  cout << " pktn "<< pktn << endl;          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4386      //  cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;          cout << "  " << endl;
4387      //  printf(" IDRUN %u \n",GetRunInfo()->ID);        }
4388      //        if (shift > 0) {
4389      if (prevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {          cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
4390        prevshift = 0;          if (DBG)
4391        shift = -1;            cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
4392      };                 << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
4393            if (DBG)
4394              cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
4395                   << " prevshift " << prevshift << " )" << endl;
4396          }
4397          answer = quellagiusta +  shift+ yprevshift;
4398          readl0 = l0_tree->GetEntry(answer);
4399          //      printf(" inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4400    
4401          if (!GetEventHeader()) {
4402            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
4403            return 0;
4404          }
4405    
4406    } while ((obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(        //
4407        GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift)        if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
4408        < GetYodaTree()->GetEntries() && shift < maxshift);          if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4409            //        printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4410            yprevshift = 0LL;
4411            shift = -1LL;
4412          };
4413        
4414          shift++;
4415        }
4416                                      
4417    
4418    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()) ){
4419      cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;        if ( isSync ){
4420      return 0;          printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found AT ALL!!! \n");
4421    }          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4422    //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;          cout << "\nFor bug reporting instructions, please see for example:\n";
4423    //    return GetYodaTree()->GetEntry(quellagiusta);          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4424    if (shift > 1)          cout << "  " << endl;
4425      prevshift += (shift - 1);        }
4426          cout << "Int_t PamLevel2::GetYodaEntry() -- WARNING -- " << endl;
4427          cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;    
4428          cout << " Nested and/or DarthVader skipped packets in fragmented run? checking and trying to fix " <<endl;
4429          // query the DB for runs containing the event, loop over LEVEL0 files which could contain the level0 event and try to find it
4430          // 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
4431          // connect to db
4432          if (!dbc || (dbc && !dbc->IsConnected())) SetDBConnection(); //Emiliano
4433          //
4434          if (GetOrbitalInfo()){
4435            abstime = GetOrbitalInfo()->absTime;
4436          } else {
4437            printf(" PamLevel2::GetYodaEntry() ERROR! no OrbitalInfo, cannot get the absolute time for event \n");
4438            return 0;
4439          }
4440          // query DB looking for runs containing the processed event
4441          TSQLResult *pResult;
4442          TSQLRow *Row = NULL;
4443          TString myquery = Form("select ID,NEVENTS from GL_RUN where RUNHEADER_TIME<=%lld and RUNTRAILER_TIME>=%lld;",abstime,abstime);
4444          if ( DBG ) printf(" query is %s \n",myquery.Data());
4445          //      printf(" query is %s \n",myquery.Data());// TOGLITOGLI
4446          pResult = dbc->Query(myquery.Data());    
4447          if (!pResult->GetRowCount()){
4448            printf(" PamLevel2::GetYodaEntry() ERROR! event is not included in any run!!! \n");
4449            cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4450            cout << "\nFor bug reporting instructions, please see for example:\n";
4451            cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4452            cout << "  " << endl;
4453            return 0;
4454          }
4455          if ( pResult->GetRowCount() == 1 ){
4456            if (DBG) printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");
4457            //        printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");//TOGLITOGLI
4458          }
4459          for( Int_t ru=0; ru < pResult->GetRowCount(); ru++){ // loop over runs containing the event
4460            if (Row) delete Row;
4461            Row = pResult->Next();  
4462            if( Row == NULL ) break;
4463            UInt_t idrun = (UInt_t)atoll(Row->GetField(0));
4464            UInt_t nev = (UInt_t)atoll(Row->GetField(1));
4465            if (DBG) printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);
4466            //        printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);//TOGLITOGLI
4467    
4468            // now look for this run in the level2 file, it must be present! code is taken from updateruninfo of course
4469            Bool_t rfound = false;
4470            totrunentry = 0LL;
4471            runfirstentry = 0LL;
4472            for (Int_t r=0; r< run_tree->GetEntries();r++){
4473              run_tree->GetEntry(r);//update runinfo
4474              if ( r > 0 ){
4475                totrunentrymin = totrunentrymax+1;
4476              } else {
4477                totrunentrymin = 0LL;
4478              }
4479              totrunentry += GetRunInfo()->NEVENTS;
4480              totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
4481              irun = r;        
4482    
4483              if (idrun == GetRunInfo()->ID){
4484                if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
4485                  if (DBG) printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
4486                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4487                  //              printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
4488                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4489                  prevshift += (totrunentrymin-iev); // add the new shift to total shift
4490                  totrunentrymin -= (totrunentrymin-iev); // shift run position min
4491                  totrunentrymax -= (totrunentrymin-iev); // shift run position max
4492                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4493                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4494                }
4495                runfirstentry = totrunentrymin; // first entry of the run in the level2
4496              
4497    
4498    return answer;              //
4499                if (gltsync)
4500                  delete gltsync; // Emiliano
4501                if (!dbc || (dbc && !dbc->IsConnected()))
4502                  SetDBConnection(); //Emiliano
4503                gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
4504                if (dbc){
4505                  dbc->Close(); // Emiliano
4506                  delete dbc;
4507                  dbc=0;
4508                }          
4509                if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
4510                  cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
4511                       << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
4512                  cout
4513                    << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
4514                    << endl;
4515                }
4516                //
4517                if (DBG) printf(" found \n");
4518                //            printf(" found \n");//TOGLITOGLI
4519                rfound = true;
4520                //
4521                break;
4522              }
4523            } // loop over run
4524            if ( !rfound ){
4525              printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level2 file!!! \n");
4526              cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4527              cout << "\nFor bug reporting instructions, please see for example:\n";
4528              cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4529              cout << "  " << endl;        
4530              return 0;
4531            }
4532    
4533            // here we got the first run and we can check if it contains the level0 event
4534            if (!GetYodaTree()){
4535              printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
4536              return 0;
4537            }      
4538    
4539            // get the current run entry
4540            irunentry = iev - runfirstentry;
4541            if (irunentry < 0) {
4542              if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
4543              //          cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
4544              irunentry = 0LL;
4545            }
4546            //  ---------------------------------
4547            //  if file is NOT a preselected file
4548            //  ---------------------------------
4549            quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
4550          
4551            if (DBG){
4552              cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
4553              cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
4554              cout << " time "<< abstime << endl;
4555            }
4556            //        cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
4557            //        cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
4558            //        cout << " time "<< abstime << endl; // TOGLITOGLI
4559          
4560            shift = 0;
4561            answer = quellagiusta + shift + yprevshift; // prevshift already included in irunentry
4562            readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
4563    
4564            if (DBG){
4565              printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4566            }
4567            //        printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4568          
4569            while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){
4570              if (shift > 0) {
4571                cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
4572                if (DBG)
4573                  cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
4574                       << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
4575                if (DBG)
4576                  cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
4577                       << " prevshift " << prevshift << " )" << endl;
4578              }
4579              answer = quellagiusta +  shift+ yprevshift;
4580              readl0 = l0_tree->GetEntry(answer);
4581              //          printf(" inside inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4582            
4583              if (!GetEventHeader()) {
4584                cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
4585                return 0;
4586              }
4587              //
4588              if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
4589                if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4590                //            printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4591                yprevshift = 0;
4592                shift = -1;
4593              };
4594            
4595              shift++;
4596            }
4597          
4598            if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
4599              //still not the good run... continue with the nex one!
4600              printf("still not the good run... continue with the nex one!\n");
4601            } else {
4602              if (DBG) cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
4603              //          cout << "LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;//TOGLITOGLI
4604              if (shift > 1) yprevshift = (shift - 1);
4605              if (Row) delete Row;
4606              delete pResult;  
4607              if (dbc){
4608                dbc->Close(); // Emiliano
4609                delete dbc;
4610                dbc=0;
4611              }    
4612              il0entry = answer;
4613              return readl0;
4614            }
4615            // perhaps it is all
4616          }// loop over runs containing the event
4617          if (Row) delete Row;
4618          delete pResult;  
4619          if (dbc){
4620            dbc->Close(); // Emiliano
4621            delete dbc;
4622            dbc=0;
4623          }          
4624          // arriving here it means no run found, cannot be! error!
4625          printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level0 files!!! \n");
4626          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4627          cout << "\nFor bug reporting instructions, please see for example:\n";
4628          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4629          cout << "  " << endl;        
4630          return 0;
4631        } else {
4632          if (DBG) cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
4633          //    cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
4634          //    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()) );
4635          if (shift > 1) yprevshift = (shift - 1);
4636          il0entry = answer;
4637          return readl0;
4638        }
4639    
4640        /*  } // if selli 0 || 2
4641      if ( SELLI == 1 && hasL0EE ){
4642        sel_tree->GetEntry(iev);  
4643        Long64_t answer = il0entry;
4644        Int_t readl0 = 0;
4645        readl0 = l0_tree->GetEntry(answer);
4646        return readl0;
4647        }*/
4648      printf(" PamLevel2::GetYodaEntry() ERROR! \n");
4649      cout << " Entry not found! OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4650      cout << "\nFor bug reporting instructions, please see for example:\n";
4651      cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4652      cout << "  " << endl;
4653      return 0;
4654  }  }
4655    
4656  /**  /**
4657   * \Brief Set DB connection   * \Brief Set DB connection
4658   */   */

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

  ViewVC Help
Powered by ViewVC 1.1.23