/[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.94 by mocchiut, Wed Mar 12 16:37:29 2014 UTC
# Line 30  void GPamela::Clear() { Line 30  void GPamela::Clear() {
30  void GPamela::Delete() {  void GPamela::Delete() {
31    
32    Clear();    Clear();
33      /* EM all these are in the stack not in the heap, so no need to delete by hand...
34    delete[] Ipltof;    delete[] Ipltof;
35    delete[] Ipaddle;    delete[] Ipaddle;
36    delete[] Ipartof;    delete[] Ipartof;
# Line 131  void GPamela::Delete() { Line 131  void GPamela::Delete() {
131    delete[] Timecard;    delete[] Timecard;
132    delete[] Pathcard;    delete[] Pathcard;
133    delete[] P0card;    delete[] P0card;
134      */
135  }  }
136  ;  
137    
138  void GPamela::SetBranchAddress(TChain* fhBookTree) {  void GPamela::SetBranchAddress(TChain* fhBookTree) {
139    
# 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      fUseDBinRunInfo = true; // Emiliano
478      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
479      il0entry = 0LL;
480      //  hasL0EE = true;
481    
482    l0_file = NULL;    l0_file = NULL;
483    l0_tree = NULL;    l0_tree = NULL;
# Line 474  void PamLevel2::Initialize() { Line 485  void PamLevel2::Initialize() {
485    dbc = 0;    dbc = 0;
486    
487    prevshift = 0;    prevshift = 0;
488    maxshift = 10; //EMILIANO    yprevshift = 0;
489      maxshift = 10; //EMILIANO now overridden by SetMaxShift(-1) called by constructors
490    
491    run_tree = NULL;    run_tree = NULL;
492    run_tree_clone = NULL;    run_tree_clone = NULL;
# Line 633  void PamLevel2::Delete() { Line 645  void PamLevel2::Delete() {
645      //      //
646      TIter next2(contents);      TIter next2(contents);
647      TChain *questa = 0;      TChain *questa = 0;
648      while (questa = (TChain*) next2()) {      while ( (questa = (TChain*)next2()) ) {
649        TString name = questa->GetName();        TString name = questa->GetName();
650        questa->Delete();        questa->Delete();
651        questa = NULL;        questa = NULL;
# Line 646  void PamLevel2::Delete() { Line 658  void PamLevel2::Delete() {
658      run_tree->Delete();;      run_tree->Delete();;
659    if (sel_tree)    if (sel_tree)
660      sel_tree->Delete();;      sel_tree->Delete();;
661    for (Int_t i = 0; i < NCLONES; i++)  
662      // The following lines are commented out since they may generate a double delete error
663      // if the file containing the clone trees is closed. This is because the file owns the
664      // clone trees which are written into it, so it will delete them when it is closed; if
665      // also PamLevel2 will try to delete these trees, a double delete error will be generated
666      // when exiting from analysis program. (Nicola 28/11/2011)
667    
668      /*for (Int_t i = 0; i < NCLONES; i++)
669      if (pam_tree_clone[i])      if (pam_tree_clone[i])
670        pam_tree_clone[i]->Delete();;        pam_tree_clone[i]->Delete();;
671    if (run_tree_clone)    if (run_tree_clone)
672      run_tree_clone->Delete();;      run_tree_clone->Delete();;
673    if (sel_tree_clone)    if (sel_tree_clone)
674      sel_tree_clone->Delete();;      sel_tree_clone->Delete();;*/
675    
676    if (irunoffset)    if (irunoffset)
677      delete[] irunoffset;      delete[] irunoffset;
# Line 747  void PamLevel2::Reset() { Line 766  void PamLevel2::Reset() {
766      //      //
767      TIter next2(contents);      TIter next2(contents);
768      TChain *questa = 0;      TChain *questa = 0;
769      while (questa = (TChain*) next2()) {      while ( (questa = (TChain*) next2()) ) {
770        TString name = questa->GetName();        TString name = questa->GetName();
771        questa->Delete();        questa->Delete();
772        questa = NULL;        questa = NULL;
# Line 792  void PamLevel2::Reset() { Line 811  void PamLevel2::Reset() {
811    soft_obj = 0;// Emiliano    soft_obj = 0;// Emiliano
812    irun = -1;    irun = -1;
813    irunt = -1;    irunt = -1;
814      totrunentry = 0LL;
815      totrunentrymax = 0LL;
816      totrunentrymin = 0LL;
817    runfirstentry = 0ULL;    runfirstentry = 0ULL;
818    runlastentry = 0ULL;    runlastentry = 0ULL;
819      prevabstime = 0ULL;
820      prevpktnum = 0;
821      abstime = 0ULL;
822      pktnum = 0;
823      isFragment = false;
824    //    //
825    totdltime[0] = 0LL;    totdltime[0] = 0LL;
826    totdltime[1] = 0LL;    totdltime[1] = 0LL;
# Line 833  Bool_t PamLevel2::IsGood(Bool_t strict) Line 860  Bool_t PamLevel2::IsGood(Bool_t strict)
860  }  }
861  ;  ;
862    
863    void PamLevel2::SkipRunInfoUpdate(){
864      printf("\n\n ******** WARNING ******** \n Skip DB connections, DO NOT USE PamLevel2::GetRunInfo() method! \n\n");
865      fUpdateRunInfo = false;
866      this->SetSELLI(2);
867      printf(" ===============> W A R N I N G <================ \n");
868      printf(" in case PamLevel2::CreateCloneTrees() will be called \n");
869      printf(" it will be reverted to PadmeAmidala level2 structure , i.e. NO SELECTIONLIST WILL BE CREATED IN THE NEW LEVEL2 FILE! \n\n");
870      if ( run_tree_clone ){
871        printf(" ===============> W A R N I N G <================ \n");
872        printf(" PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() has been called together with PamLevel2::CreateCloneTrees() \n");
873        printf(" TO AVOID CRASHES call PamLevel2::CreateCloneTrees() after PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() \n");    
874      };
875    }
876    
877    void PamLevel2::SetMaxShift(Int_t sh){
878      if ( sh >=  0 ){
879        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");
880        maxshift = sh;
881      } else {
882        ULong64_t nev = this->GetEntries();
883        ULong64_t runnev = 0ULL;
884        for (Int_t r=0; r< run_tree->GetEntries();r++){
885          run_tree->GetEntry(r);//update runinfo
886          runnev += GetRunInfo()->NEVENTS;
887        }
888        maxshift = (Int_t)(runnev-nev) + 10; // +10 just to be conservative
889        if ( (runnev-nev) == 0 ) isSync = true;
890        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);
891        //    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
892      }
893    }
894    
895  //--------------------------------------  //--------------------------------------
896  //  //
897  //  //
# Line 2113  TList* PamLevel2::GetListOfLevel2Files(T Line 2172  TList* PamLevel2::GetListOfLevel2Files(T
2172          if (file.EndsWith(".root")) {          if (file.EndsWith(".root")) {
2173            TString filedir;            TString filedir;
2174            cout << ddir << endl;            cout << ddir << endl;
2175            if (ddir != "") {            if ( ddir != "" ) {
2176              filedir = ddir; // take the input dir              filedir = ddir; // take the input dir
2177            }            }
2178            else {            else {
# Line 2121  TList* PamLevel2::GetListOfLevel2Files(T Line 2180  TList* PamLevel2::GetListOfLevel2Files(T
2180              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
2181            };            };
2182            filedir.Append("/");            filedir.Append("/");
2183            char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file));            //          char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file));
2184              char *fullpath = gSystem->ConcatFileName(filedir.Data(), gSystem->BaseName(file));
2185            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
2186            cout << fullpath << endl;            cout << fullpath << endl;
2187            delete fullpath;            delete fullpath;
# Line 2392  TChain *PamLevel2::GetPamTree(TList *fl, Line 2452  TChain *PamLevel2::GetPamTree(TList *fl,
2452      cout << "SelectionList: set branch address RunEntry" << endl;      cout << "SelectionList: set branch address RunEntry" << endl;
2453      L->SetBranchAddress("EventEntry", &irunentry);      L->SetBranchAddress("EventEntry", &irunentry);
2454      cout << "SelectionList: set branch address EventEntry" << endl;      cout << "SelectionList: set branch address EventEntry" << endl;
2455        /*    if ( L->GetBranch("L0EventEntry") ){
2456          hasL0EE = true;
2457          L->SetBranchAddress("L0EventEntry", &il0entry);
2458          cout << "SelectionList: set branch address L0EventEntry" << endl;
2459        } else {
2460          hasL0EE = false; // backward compatibility with old preselected files...
2461          }*/
2462      sel_tree = L;      sel_tree = L;
2463      //          if(!Trout)Trout=O;      //          if(!Trout)Trout=O;
2464      //          else Trout->AddFriend("SelectionList");      //          else Trout->AddFriend("SelectionList");
# Line 2715  TTree *PamLevel2::GetRunTree(TFile *f) { Line 2782  TTree *PamLevel2::GetRunTree(TFile *f) {
2782    return T;    return T;
2783    
2784  }  }
 /**  
  * 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;  
   //  
 }  
 ;  
2785    
2786  Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) {  Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) {
2787    
2788      if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - inside\n");
2789    
2790    if (!run_tree) {    if (!run_tree) {
2791      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;
2792      return false;      return false;
# Line 2865  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 2796  Bool_t PamLevel2::UpdateRunInfo(Long64_t
2796      return (false);      return (false);
2797    }    }
2798    
2799    
2800    Int_t oldrun = irun; // store current run index    Int_t oldrun = irun; // store current run index
2801    
2802    // -----------------------------------------------------------------------    // -----------------------------------------------------------------------
# Line 2874  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 2806  Bool_t PamLevel2::UpdateRunInfo(Long64_t
2806    if (irun < 0) {    if (irun < 0) {
2807      irun = 0LL;      irun = 0LL;
2808      irunt = 0LL;      irunt = 0LL;
2809      irunentry = 0;      totrunentry = 0LL;
2810        totrunentrymin = 0LL;
2811        totrunentrymax = 0LL;
2812        irunentry = 0LL;
2813        il0entry = 0LL;
2814      prevshift = 0;      prevshift = 0;
2815        yprevshift = 0;
2816        prevabstime = 0;
2817        prevpktnum = 0;
2818        abstime = 0ULL;
2819        pktnum = 0;
2820        isFragment = false;
2821      run_tree->GetEntry(irun);      run_tree->GetEntry(irun);
2822      if (!GetOrbitalInfo())      if (!GetOrbitalInfo())
2823        cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated " << endl;        cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated " << endl;
2824      if (gltsync)      if ( fUseDBinRunInfo ){
2825        delete gltsync; //Emiliano        if (gltsync)
2826      if (!dbc || (dbc && !dbc->IsConnected()))          delete gltsync; //Emiliano
2827        SetDBConnection(); //Emiliano        if (!dbc || (dbc && !dbc->IsConnected()))
2828      gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); //Emiliano // the "false" means not to use level0 file (not necessary here)          SetDBConnection(); //Emiliano
2829      if (dbc){        gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); //Emiliano // the "false" means not to use level0 file (not necessary here)
2830        dbc->Close();// Emiliano        if (dbc){
2831        dbc=0;          dbc->Close();// Emiliano
2832      };          delete dbc;
2833    };          dbc=0;
2834          }
2835        }
2836      }
2837    // ---------------------------------------------------------------    // ---------------------------------------------------------------
2838    // retrieve OBT and absolute time of the event    // retrieve OBT and absolute time of the event
2839    // ---------------------------------------------------------------    // ---------------------------------------------------------------
   ULong64_t abstime = 0LL;  
   //    ULong64_t obt     = 0LL; // Emiliano  
2840    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
2841      prevabstime = abstime;
2842      prevpktnum = pktnum;
2843    if (GetOrbitalInfo()) {    if (GetOrbitalInfo()) {
2844      abstime = GetOrbitalInfo()->absTime;      abstime = GetOrbitalInfo()->absTime;
2845      obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano      if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano
2846        pktnum = GetOrbitalInfo()->pkt_num; // Emiliano
2847    }    }
2848    else {    else {
2849      abstime = GetRunInfo()->RUNHEADER_TIME;      abstime = GetRunInfo()->RUNHEADER_TIME;
2850      obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano      if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano
2851        pktnum = GetRunInfo()->RUNHEADER_PKT; // Emiliano
2852      }
2853    
2854      if (DBG){
2855        printf("0abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
2856        printf("0        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
2857        printf("0        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
2858        if ( fUseDBinRunInfo ) printf("0        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
2859        printf("0        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
2860        printf("0        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
2861    }    }
2862    
2863    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# Line 2922  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 2878  Bool_t PamLevel2::UpdateRunInfo(Long64_t
2878      }      }
2879    
2880      //      //
2881      bool fromfirst = true; // first loop over runs      bool a = true;
2882        bool b = true;
2883        if ( fUseDBinRunInfo ){
2884          a = false;    
2885          b = false;
2886          if ( obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) a = true;
2887          if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true;
2888        }
2889        if ( iev < totrunentrymin || iev > totrunentrymax // entry is outside run limits
2890             || iev == 0 // or it is the first entry
2891             || (!isSync && (
2892                             (abstime <= GetRunInfo()->RUNHEADER_TIME && a ) // or it is outside obt limits (and abstime limits for security reasons)
2893                             || (abstime >= GetRunInfo()->RUNTRAILER_TIME && b ) ))// or it is outside obt limits (and abstime limits for security reasons)
2894             ){ // check on abstime and obt needed to handle nested+DV_skipped packets
2895          
2896          // check for a new run (ma prima il primo!)
2897          if (DBG){
2898            printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
2899            printf("1        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
2900            printf("1        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
2901            if ( fUseDBinRunInfo ) printf("1        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
2902            printf("1        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
2903            printf("1        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
2904          }
2905          //        printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
2906          //        printf("1        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
2907          //        printf("1        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
2908          //        printf("1        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
2909          //        printf("1        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
2910          //        printf("1        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
2911    
2912      // ------------------------------------------------------        totrunentry = 0LL;
2913      // loop over runs to find the one that contains the event        runfirstentry = 0LL;
2914      // ------------------------------------------------------        for (Int_t r=0; r< run_tree->GetEntries();r++){
2915      while ((          // -------------------------------------------------------------------
2916      //          (          // save the index of the first entry of the run, relative to pam_tree,
2917      //              !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)          // and read a new run
2918      //                abstime <= GetRunInfo()->RUNTRAILER_TIME) &&          // -------------------------------------------------------------------
2919      //              !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)          run_tree->GetEntry(r);//update runinfo
2920      //                obt <= GetRunInfo()->RUNTRAILER_OBT)          if ( r > 0 ){
2921      //              )            totrunentrymin = totrunentrymax+1;
2922            } else {
2923      !(abstime >= GetRunInfo()->RUNHEADER_TIME && // check on absolute time (s)            totrunentrymin = 0LL;
2924          abstime <= GetRunInfo()->RUNTRAILER_TIME) || !(obt >= gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) && // additional check on OBT (ms) // Emiliano          }
2925          obt <= gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) // Emiliano          totrunentry += GetRunInfo()->NEVENTS;
2926          || GetRunInfo()->NEVENTS == 0          totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
2927      //          || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift) // ERRORE!!! fa saltare i run con 1 evento          irun = r;        
2928          || !(irunentry <= GetRunInfo()->NEVENTS - 1 - prevshift)) && irun < run_tree->GetEntries()) {          if ( fUseDBinRunInfo ){
2929              a = false;    
2930        //            if( !(abstime >= GetRunInfo()->RUNHEADER_TIME &&abstime <= GetRunInfo()->RUNTRAILER_TIME)  )cout << "ABS TIME "<<abstime << " " <<GetRunInfo()->RUNTRAILER_TIME <<endl;            b = false;
2931        //            if( !(obt >= GetRunInfo()->RUNHEADER_OBT && obt <= GetRunInfo()->RUNTRAILER_OBT) )cout << "OBT TIME "<< obt <<" "<< GetRunInfo()->RUNTRAILER_OBT << endl;            if ( obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) a = true;
2932        //            if( GetRunInfo()->NEVENTS==0  )cout <<"GetRunInfo()->NEVENTS==0 "<<endl;            if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true;
2933        //            if( !(irunentry <= GetRunInfo()->NEVENTS-1-prevshift) ) cout << "irunentry > "<<GetRunInfo()->NEVENTS-1-prevshift << endl;          }
2934        // - - - - - - - - - - - - -          if ( (iev >= totrunentrymin && iev <= totrunentrymax) || // entry is inside run limits
2935        // irunentry = position of current entry, relative to the run               ( !isSync &&
2936        // prevshift = shift needed to synchronize l0 and l2 data (nested events)                 ( abstime >= GetRunInfo()->RUNHEADER_TIME  && a // or it is inside obt limits (and abstime limits for security reasons)
2937        // - - - - - - - - - - - - -                   && abstime <= GetRunInfo()->RUNTRAILER_TIME && b))  // or it is inside obt limits (and abstime limits for security reasons)
2938                 ){ // check on abstime and obt needed to handle nested+DV_skipped packets
2939        // -----------------------------------------            if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
2940        // store dead and live-time of previous run              if ( !isSync ){
2941        // -----------------------------------------                if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
2942        //            if(SELLI==0){                if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
2943        if (SELLI != 2) {                //              printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
2944          if (fromfirst) {                //              printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
2945            if (oldrun == irun) {                prevshift += (totrunentrymin-iev); // add the new shift to total shift
2946              /// decrement counters                totrunentrymin -= (totrunentrymin-iev); // shift run position min
2947              if (GetTrigLevel2()) {                totrunentrymax -= (totrunentrymin-iev); // shift run position max
2948                totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time                if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
2949                totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time                //              printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
2950              }              } else {
2951              totdltime[2]--; //event counter                printf(" PamLevel2::UpdateRunInfo(Long64_t) ERROR! sync file but unconsistent totrunetrymin %lld and iev %lld!!! \n",totrunentrymin,iev);
2952              if (DBG) {                cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
2953                cout << endl;                cout << "\nFor bug reporting instructions, please see for example:\n";
2954                cout << "n.events     : " << totdltime[2] << endl;                cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
2955                cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;                cout << "  " << endl;
               cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;  
2956              }              }
2957            }            }
2958            else {            runfirstentry = totrunentrymin; // first entry of the run in the level2
2959              totdltime[0] = 0;//live-time            
2960              totdltime[1] = 0;//dead-time  
2961              totdltime[2] = 0; //event counter            //
2962              if (DBG)            if ( fUseDBinRunInfo ){
2963                cout << " *** JUMP RUN *** irun " << irun << endl;              if (gltsync)
2964                  delete gltsync; // Emiliano
2965                if (!dbc || (dbc && !dbc->IsConnected()))
2966                  SetDBConnection(); //Emiliano
2967                gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
2968                TrkParams::Set(GetRunInfo(), dbc);
2969                if (dbc){
2970                  dbc->Close(); // Emiliano
2971                  delete dbc;
2972                  dbc=0;
2973                }          
2974                if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
2975                  cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
2976                       << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
2977                  cout
2978                    << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
2979                    << endl;
2980                }
2981            }            }
2982            /// add an entry            //
2983            if (run_tree_clone)            if (DBG) printf(" found \n");
2984              if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())            //          printf(" found \n");//TOGLITOGLI
2985                run_tree_clone->GetBranch("DeadLiveTime")->Fill();            //
2986            /// reset counters            break;
2987            }
2988          } // loop over run
2989          
2990          // --------------------------------------
2991          // if there was no need to update the run
2992          // ---> exit with FALSE
2993          // --------------------------------------
2994          if (irun == oldrun){
2995            if (DBG) printf(" no new run \n");
2996            //        printf(" no new run \n");//TOGLITOGLI
2997            return (false);
2998          }      
2999          // --------------------------------------
3000          // ... otherwise
3001          // ---> exit with TRUE
3002          // --------------------------------------
3003    
3004          if (SELLI != 2) {
3005            /// decrement counters since this event belongs to a new run
3006            if (GetTrigLevel2()) {
3007              totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time
3008              totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time
3009            }
3010            totdltime[2]--; //event counter
3011            if (DBG) {
3012              cout << endl;
3013              cout << "n.events     : " << totdltime[2] << endl;
3014              cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;
3015              cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;
3016            }
3017            // add an entry
3018            if (run_tree_clone && totdltime[2] > 0)
3019              if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
3020                run_tree_clone->GetBranch("DeadLiveTime")->Fill();
3021            // reset counters
3022            if ( totdltime[2] > 0 ){
3023            if (GetTrigLevel2()) {            if (GetTrigLevel2()) {
3024              totdltime[0] = GetTrigLevel2()->dltime[0];//live-time              totdltime[0] = GetTrigLevel2()->dltime[0];//live-time
3025              totdltime[1] = 0; //dead-time              totdltime[1] = 0; //dead-time
# Line 2991  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 3027  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3027            totdltime[2] = 1; //event counter            totdltime[2] = 1; //event counter
3028          }          }
3029        }        }
       //            }  
3030    
3031        irun++;        if (DBG){
3032        // ------------------------------------          cout << endl << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun
3033        // if the end of run tree is reached...               << endl;        
3034        // ------------------------------------          printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
3035        if (irun == run_tree->GetEntries()) {          printf("2        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
3036          if (!fromfirst) {          printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
3037            // -----------------------------------------------------          if ( fUseDBinRunInfo ) printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
3038            // if it happened already once and the run was not found          printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
3039            // ---> 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;  
3040        }        }
3041        //            if(hasfrag &&  fragid != GetRunInfo()->ID){        //        printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
3042        //                cout << "... where is the next fragment ??"<<endl;        //        printf("2        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
3043        //            }        //        printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
3044      };        //        printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
3045          //        printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
3046      // --------------------------------------        //        printf("2        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
3047      // if there was no need to update the run  
3048      // ---> exit with FALSE        return (true);
3049      // --------------------------------------      } // need for run upgrade
3050      if (irun == oldrun)      if (DBG) printf("return false\n");
3051        return (false);      return (false);
3052      }// SELLI = 0 SELLI = 2
3053      // --------------------------------------    
     // ... 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);  
   };  
3054    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
3055    // if it is a preselected file (there is SelectionList)    // if it is a preselected file (there is SelectionList)
3056    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# Line 3165  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 3125  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3125        // update the tracker parameters        // update the tracker parameters
3126        // (non ho trovato nessun altro modo sicuro di farlo...)        // (non ho trovato nessun altro modo sicuro di farlo...)
3127        // ----------------------------------------------------        // ----------------------------------------------------
3128        if (!dbc || (dbc && !dbc->IsConnected()))        if ( fUseDBinRunInfo ){
3129          SetDBConnection();          if (!dbc || (dbc && !dbc->IsConnected()))
3130        TrkParams::Set(GetRunInfo(), dbc);            SetDBConnection();
3131        if (dbc){          TrkParams::Set(GetRunInfo(), dbc);
3132          dbc->Close();          if (dbc){
3133          dbc=0;            dbc->Close();
3134        };            delete dbc;
3135              dbc=0;
3136            }
3137          }
3138        //            cout << endl;        //            cout << endl;
3139        prevshift = 0;        prevshift = 0;
3140          yprevshift = 0;
3141        return true;        return true;
3142      }      }
3143      return false;      return false;
# Line 3182  Bool_t PamLevel2::UpdateRunInfo(Long64_t Line 3146  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3146    return false;    return false;
3147    //    //
3148  }  }
3149  ;  
3150    /**
3151     * Update the runinfo informations (to be used to have Run infos event by event basis)
3152     * @param run Pointer to the chain/tree which contains run infos
3153     * @return true if a new run has been read, false if it is still the same run
3154     */
3155    Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) {
3156      return (UpdateRunInfo(iev));
3157    }
3158    
3159  /**  /**
3160   * 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)
3161   * @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 3164  Bool_t PamLevel2::UpdateRunInfo(Long64_t
3164  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {
3165    return (UpdateRunInfo((TChain*) run, iev));    return (UpdateRunInfo((TChain*) run, iev));
3166  }  }
3167  ;  
3168    
3169  //--------------------------------------  //--------------------------------------
3170  //  //
# Line 4022  void PamLevel2::CreateCloneTrees(TFile * Line 3995  void PamLevel2::CreateCloneTrees(TFile *
3995      //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");      //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
3996      sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW      sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW
3997      sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");      sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");
3998        //    if ( hasL0EE ) sel_tree_clone->Branch("L0EventEntry", &il0entry, "l0eventry/L");
3999    };    };
4000    
4001    Int_t i = 0;    Int_t i = 0;
# Line 4104  void PamLevel2::CreateCloneTrees(TFile * Line 4078  void PamLevel2::CreateCloneTrees(TFile *
4078    if (GP) {    if (GP) {
4079      pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");      pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");
4080      pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split      pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split
4081      cout << "OrbitalInfo  : branch OrbitalInfo" << endl;      cout << "GPamela  : branch GPamela" << endl;
4082      i++;      i++;
4083    };    };
4084    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
# Line 4177  void PamLevel2::WriteCloneTrees() { Line 4151  void PamLevel2::WriteCloneTrees() {
4151  /**  /**
4152   * 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.
4153   */   */
 //Int_t PamLevel2::GetEntry(Int_t iee){  
4154  Int_t PamLevel2::GetEntry(Long64_t iee) {  Int_t PamLevel2::GetEntry(Long64_t iee) {
4155    
   //     cout << "-------------------------------------"<<endl;  
   //     cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;  
   
4156    if (!pam_tree) {    if (!pam_tree) {
4157      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;
4158      return 0;      return 0;
# Line 4196  Int_t PamLevel2::GetEntry(Long64_t iee) Line 4166  Int_t PamLevel2::GetEntry(Long64_t iee)
4166    //    return 0;    //    return 0;
4167    //    }    //    }
4168    
   Long64_t ii = 0;  
4169    //-------------------------------    //-------------------------------
4170    ii = iee;    if (!pam_tree->GetEntry(iee)) {
   if (!pam_tree->GetEntry(ii)) {  
4171      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;
4172      return 0;      return 0;
4173    }    }
# Line 4219  Int_t PamLevel2::GetEntry(Long64_t iee) Line 4187  Int_t PamLevel2::GetEntry(Long64_t iee)
4187    }    }
4188    
4189    //-------------------------------    //-------------------------------
4190    ii = iee;    //
4191    //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);    if ( fUpdateRunInfo ) UpdateRunInfo(iee); // Emiliano
4192    //    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;  
   //    }  
   //     }  
   
4193    
4194    return 1;    return 1;
4195    
# Line 4280  TTree* PamLevel2::GetYodaTree() { Line 4230  TTree* PamLevel2::GetYodaTree() {
4230    //===================================    //===================================
4231    if (irun < 0) {    if (irun < 0) {
4232      cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;      cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;
4233      //  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;
4234      return NULL;      return NULL;
4235    }    }
4236    Int_t irootnew = run_obj->ID_ROOT_L0;    Int_t irootnew = GetRunInfo()->ID_ROOT_L0;
4237    //      cout << "iroot    "<<iroot<<endl;    if (DBG){
4238    //      cout << "irootnew "<<irootnew<<endl;      cout << "iroot    "<<iroot<<endl;
4239        cout << "irootnew "<<irootnew<<endl;
4240      }
4241    
4242    //===================================    //===================================
4243    // load the level0 file    // load the level0 file
# Line 4323  TTree* PamLevel2::GetYodaTree() { Line 4275  TTree* PamLevel2::GetYodaTree() {
4275      if (!h0_obj)      if (!h0_obj)
4276        h0_obj = new EventHeader();        h0_obj = new EventHeader();
4277      l0_tree->SetBranchAddress("Header", &h0_obj);      l0_tree->SetBranchAddress("Header", &h0_obj);
4278      prevshift = 0;      yprevshift = 0; // yes, yprevshift is the shift in the level0, prevshift is the shift in the level2
4279      //---------------------------------------------------      //---------------------------------------------------
4280      // TRACKER:      // TRACKER:
4281      if (TRK0) {      if (TRK0) {
# Line 4341  TTree* PamLevel2::GetYodaTree() { Line 4293  TTree* PamLevel2::GetYodaTree() {
4293          calo0_obj->Set();          calo0_obj->Set();
4294        };        };
4295        l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());        l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());
       //            cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;  
4296      }      }
4297      //---------------------------------------------------      //---------------------------------------------------
4298      // TOF:      // TOF:
# Line 4350  TTree* PamLevel2::GetYodaTree() { Line 4301  TTree* PamLevel2::GetYodaTree() {
4301      }      }
4302    
4303      dbc->Close(); // EMILIANO, do not leave open connections, open only when needed      dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
4304        delete dbc;
4305      dbc=0;      dbc=0;
4306    
4307    };    };
4308    
   //     if(!dbc || (dbc && !dbc->IsConnected())){  
   //    cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;  
   //     }  
   
4309    if (TRK0) {    if (TRK0) {
4310      //  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?  
4311      TrkParams::SetCalib(run_obj, dbc);      TrkParams::SetCalib(run_obj, dbc);
4312      TrkParams::LoadCalib();      TrkParams::LoadCalib();
4313      if (!TrkParams::CalibIsLoaded()) {      if (!TrkParams::CalibIsLoaded()) {
# Line 4371  TTree* PamLevel2::GetYodaTree() { Line 4315  TTree* PamLevel2::GetYodaTree() {
4315      };      };
4316      if(dbc){      if(dbc){
4317        dbc->Close(); // EMILIANO, do not leave open connections, open only when needed        dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
4318          delete dbc;
4319        dbc=0;        dbc=0;
4320      };      };
4321    }    }
   
   //    cout << l0_tree << endl;  
4322    return l0_tree;    return l0_tree;
   
4323  }  }
4324    
4325  /**  /**
# Line 4385  TTree* PamLevel2::GetYodaTree() { Line 4327  TTree* PamLevel2::GetYodaTree() {
4327   */   */
4328  Int_t PamLevel2::GetYodaEntry() {  Int_t PamLevel2::GetYodaEntry() {
4329    
4330    //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;    Long64_t iev = this->GetReadEntry();
   if (!GetYodaTree())  
     return 0;  
4331    
4332    // patch    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4333    if (irunentry < 0) {    // if it is a full file (not preselected)
4334      //  cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4335      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;  
4336    
4337    ULong64_t obt = 0;      if (!GetYodaTree()){
4338    ULong64_t pktn = 0;        printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
4339    if (GetOrbitalInfo()) {        return 0;
4340      obt = GetOrbitalInfo()->OBT;      }
     pktn = GetOrbitalInfo()->pkt_num;  
   }  
4341    
4342    if (!GetOrbitalInfo() && !ISGP) {      if (irunentry < 0) {
4343      cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;        if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
4344      return 0;        //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
4345    }        irunentry = 0LL;
4346    if (obt == 0 && pktn == 0 && !ISGP) {      }
4347      cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;      //  ---------------------------------
4348      return 0;      //  if file is NOT a preselected file
4349    }      //  ---------------------------------
4350        Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
4351    
4352        if (DBG){
4353          cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
4354          cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
4355          cout << " time "<< abstime << endl;
4356        }
4357    
4358        ULong64_t obt = 0;
4359        ULong64_t pktn = 0;
4360        if (GetOrbitalInfo()) {
4361          obt = GetOrbitalInfo()->OBT;
4362          pktn = GetOrbitalInfo()->pkt_num;
4363        }
4364    
4365    // ---------------------------------------------------------------------      if (!GetOrbitalInfo() && !ISGP) {
4366    // ATTENTION!!!        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;
4367    // 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;  
4368      }      }
4369      answer = l0_tree->GetEntry(quellagiusta + (Long64_t) shift + (Long64_t) prevshift);      if (obt == 0 && pktn == 0 && !ISGP) {
4370      shift++;        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;
     if (!GetEventHeader()) {  
       cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;  
4371        return 0;        return 0;
4372      }      }
4373    
4374        // ---------------------------------------------------------------------
4375        // ATTENTION!!!
4376        // If data are not pre-processed with cleaner, the level0 tree may contain
4377        // spurious nested physics packets.
4378        // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
4379        // while level2 tree DOES NOT!!
4380        // This means that "quellagiusta" in these cases is not correct.
4381        // In order to retrieve the correct level0 event, I implemented a check
4382        // of the OBT and pkt-number. In case of mismatch, the level0 entry number
4383        // is shift forward until when the packets match.
4384        // ---------------------------------------------------------------------
4385        Long64_t shift = 0LL;
4386        Long64_t answer = quellagiusta + shift + yprevshift;
4387        Int_t readl0 = 0;
4388        readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
4389    
4390        if (DBG){
4391          printf(" siamo qui shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4392        }
4393    
4394    
4395      if (ISGP) {      if (ISGP) {
4396        obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO        obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
4397        pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO        pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
4398      }      }
4399    
4400      //  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 ){
4401      //  cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;        if ( isSync && shift == 0LL ){
4402      //  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");
4403      //  cout << " obt "<< obt << endl;          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4404      //  cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;          cout << "\nFor bug reporting instructions, please see for example:\n";
4405      //  cout << " pktn "<< pktn << endl;          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4406      //  cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;          cout << "  " << endl;
4407      //  printf(" IDRUN %u \n",GetRunInfo()->ID);        }
4408      //        if (shift > 0) {
4409      if (prevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {          if (DBG) cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
4410        prevshift = 0;          if (DBG)
4411        shift = -1;            cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
4412      };                 << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
4413            if (DBG)
4414              cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
4415                   << " prevshift " << prevshift << " )" << endl;
4416          }
4417          answer = quellagiusta +  shift+ yprevshift;
4418          readl0 = l0_tree->GetEntry(answer);
4419          //      printf(" inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4420    
4421    } while ((obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(        if (!GetEventHeader()) {
4422        GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift)          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
4423        < GetYodaTree()->GetEntries() && shift < maxshift);          return 0;
4424          }
4425    
4426    if ((quellagiusta + (Long64_t) shift + (Long64_t) prevshift) > GetYodaTree()->GetEntries() || shift == maxshift) {        //
4427      cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;        if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
4428      return 0;          if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4429    }          //        printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4430    //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;          yprevshift = 0LL;
4431    //    return GetYodaTree()->GetEntry(quellagiusta);          shift = -1LL;
4432    if (shift > 1)        };
4433      prevshift += (shift - 1);      
4434          shift++;
4435        }
4436                                      
4437    
4438        if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
4439          if ( isSync ){
4440            printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found AT ALL!!! \n");
4441            cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4442            cout << "\nFor bug reporting instructions, please see for example:\n";
4443            cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4444            cout << "  " << endl;
4445          }
4446          cout << "Int_t PamLevel2::GetYodaEntry() -- WARNING -- " << endl;
4447          cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;    
4448          cout << " Nested and/or DarthVader skipped packets in fragmented run? checking and trying to fix " <<endl;
4449          // query the DB for runs containing the event, loop over LEVEL0 files which could contain the level0 event and try to find it
4450          // 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
4451          // connect to db
4452          if (!dbc || (dbc && !dbc->IsConnected())) SetDBConnection(); //Emiliano
4453          //
4454          if (GetOrbitalInfo()){
4455            abstime = GetOrbitalInfo()->absTime;
4456          } else {
4457            printf(" PamLevel2::GetYodaEntry() ERROR! no OrbitalInfo, cannot get the absolute time for event \n");
4458            return 0;
4459          }
4460          // query DB looking for runs containing the processed event
4461          TSQLResult *pResult;
4462          TSQLRow *Row = NULL;
4463          TString myquery = Form("select ID,NEVENTS from GL_RUN where RUNHEADER_TIME<=%lld and RUNTRAILER_TIME>=%lld;",abstime,abstime);
4464          if ( DBG ) printf(" query is %s \n",myquery.Data());
4465          //      printf(" query is %s \n",myquery.Data());// TOGLITOGLI
4466          pResult = dbc->Query(myquery.Data());    
4467          if (!pResult->GetRowCount()){
4468            printf(" PamLevel2::GetYodaEntry() ERROR! event is not included in any run!!! \n");
4469            cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4470            cout << "\nFor bug reporting instructions, please see for example:\n";
4471            cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4472            cout << "  " << endl;
4473            return 0;
4474          }
4475          if ( pResult->GetRowCount() == 1 ){
4476            if (DBG) printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");
4477            //        printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");//TOGLITOGLI
4478          }
4479          for( Int_t ru=0; ru < pResult->GetRowCount(); ru++){ // loop over runs containing the event
4480            if (Row) delete Row;
4481            Row = pResult->Next();  
4482            if( Row == NULL ) break;
4483            UInt_t idrun = (UInt_t)atoll(Row->GetField(0));
4484            UInt_t nev = (UInt_t)atoll(Row->GetField(1));
4485            if (DBG) printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);
4486            //        printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);//TOGLITOGLI
4487    
4488            // now look for this run in the level2 file, it must be present! code is taken from updateruninfo of course
4489            Bool_t rfound = false;
4490            totrunentry = 0LL;
4491            runfirstentry = 0LL;
4492            for (Int_t r=0; r< run_tree->GetEntries();r++){
4493              run_tree->GetEntry(r);//update runinfo
4494              if ( r > 0 ){
4495                totrunentrymin = totrunentrymax+1;
4496              } else {
4497                totrunentrymin = 0LL;
4498              }
4499              totrunentry += GetRunInfo()->NEVENTS;
4500              totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
4501              irun = r;        
4502    
4503              if (idrun == GetRunInfo()->ID){
4504                if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
4505                  if (DBG) printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
4506                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4507                  //              printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
4508                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4509                  prevshift += (totrunentrymin-iev); // add the new shift to total shift
4510                  totrunentrymin -= (totrunentrymin-iev); // shift run position min
4511                  totrunentrymax -= (totrunentrymin-iev); // shift run position max
4512                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4513                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4514                }
4515                runfirstentry = totrunentrymin; // first entry of the run in the level2
4516              
4517    
4518    return answer;              //
4519                if (gltsync)
4520                  delete gltsync; // Emiliano
4521                if (!dbc || (dbc && !dbc->IsConnected()))
4522                  SetDBConnection(); //Emiliano
4523                gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
4524                if (dbc){
4525                  dbc->Close(); // Emiliano
4526                  delete dbc;
4527                  dbc=0;
4528                }          
4529                if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
4530                  cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
4531                       << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
4532                  cout
4533                    << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
4534                    << endl;
4535                }
4536                //
4537                if (DBG) printf(" found \n");
4538                //            printf(" found \n");//TOGLITOGLI
4539                rfound = true;
4540                //
4541                break;
4542              }
4543            } // loop over run
4544            if ( !rfound ){
4545              printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level2 file!!! \n");
4546              cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4547              cout << "\nFor bug reporting instructions, please see for example:\n";
4548              cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4549              cout << "  " << endl;        
4550              return 0;
4551            }
4552    
4553            // here we got the first run and we can check if it contains the level0 event
4554            if (!GetYodaTree()){
4555              printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
4556              return 0;
4557            }      
4558    
4559            // get the current run entry
4560            irunentry = iev - runfirstentry;
4561            if (irunentry < 0) {
4562              if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
4563              //          cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
4564              irunentry = 0LL;
4565            }
4566            //  ---------------------------------
4567            //  if file is NOT a preselected file
4568            //  ---------------------------------
4569            quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
4570          
4571            if (DBG){
4572              cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
4573              cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
4574              cout << " time "<< abstime << endl;
4575            }
4576            //        cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
4577            //        cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
4578            //        cout << " time "<< abstime << endl; // TOGLITOGLI
4579          
4580            shift = 0;
4581            answer = quellagiusta + shift + yprevshift; // prevshift already included in irunentry
4582            readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
4583    
4584            if (DBG){
4585              printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4586            }
4587            //        printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4588          
4589            while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){
4590              if (shift > 0) {
4591                if (DBG) cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
4592                if (DBG)
4593                  cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
4594                       << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
4595                if (DBG)
4596                  cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
4597                       << " prevshift " << prevshift << " )" << endl;
4598              }
4599              answer = quellagiusta +  shift+ yprevshift;
4600              readl0 = l0_tree->GetEntry(answer);
4601              //          printf(" inside inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4602            
4603              if (!GetEventHeader()) {
4604                cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
4605                return 0;
4606              }
4607              //
4608              if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
4609                if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
4610                //            printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
4611                yprevshift = 0;
4612                shift = -1;
4613              };
4614            
4615              shift++;
4616            }
4617          
4618            if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
4619              //still not the good run... continue with the nex one!
4620              printf("still not the good run... continue with the nex one!\n");
4621            } else {
4622              if (DBG) cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
4623              //          cout << "LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;//TOGLITOGLI
4624              if (shift > 1) yprevshift = (shift - 1);
4625              if (Row) delete Row;
4626              delete pResult;  
4627              if (dbc){
4628                dbc->Close(); // Emiliano
4629                delete dbc;
4630                dbc=0;
4631              }    
4632              il0entry = answer;
4633              return readl0;
4634            }
4635            // perhaps it is all
4636          }// loop over runs containing the event
4637          if (Row) delete Row;
4638          delete pResult;  
4639          if (dbc){
4640            dbc->Close(); // Emiliano
4641            delete dbc;
4642            dbc=0;
4643          }          
4644          // arriving here it means no run found, cannot be! error!
4645          printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level0 files!!! \n");
4646          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4647          cout << "\nFor bug reporting instructions, please see for example:\n";
4648          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4649          cout << "  " << endl;        
4650          return 0;
4651        } else {
4652          if (DBG) cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
4653          //    cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
4654          //    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()) );
4655          if (shift > 1) yprevshift = (shift - 1);
4656          il0entry = answer;
4657          return readl0;
4658        }
4659    
4660        /*  } // if selli 0 || 2
4661      if ( SELLI == 1 && hasL0EE ){
4662        sel_tree->GetEntry(iev);  
4663        Long64_t answer = il0entry;
4664        Int_t readl0 = 0;
4665        readl0 = l0_tree->GetEntry(answer);
4666        return readl0;
4667        }*/
4668      printf(" PamLevel2::GetYodaEntry() ERROR! \n");
4669      cout << " Entry not found! OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4670      cout << "\nFor bug reporting instructions, please see for example:\n";
4671      cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4672      cout << "  " << endl;
4673      return 0;
4674  }  }
4675    
4676  /**  /**
4677   * \Brief Set DB connection   * \Brief Set DB connection
4678   */   */

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

  ViewVC Help
Powered by ViewVC 1.1.23