/[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.25 by mocchiut, Thu Mar 15 13:42:36 2007 UTC revision 1.51 by mocchiut, Mon Oct 29 09:13:06 2007 UTC
# Line 11  PamTrack::PamTrack(){ Line 11  PamTrack::PamTrack(){
11      calo_track = 0;      calo_track = 0;
12      tof_track  = 0;      tof_track  = 0;
13      candeleteobj = 0;      candeleteobj = 0;
14        pscore = 0;
15        iscore = 0;
16  };  };
17  //--------------------------------------  //--------------------------------------
18  //  //
# Line 60  void PamTrack::Clear(){ Line 62  void PamTrack::Clear(){
62          calo_track = 0;          calo_track = 0;
63          tof_track  = 0;          tof_track  = 0;
64      }      }
65        pscore = 0;
66        iscore = 0;
67    
68  }  }
69  void PamTrack::Delete(){  void PamTrack::Delete(){
70  //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;  //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;
# Line 85  void PamTrack::Delete(){ Line 90  void PamTrack::Delete(){
90  //  //
91  //--------------------------------------  //--------------------------------------
92  /**  /**
93   * Constructor   * Default Constructor
94   */   */
95  PamLevel2::PamLevel2(){  PamLevel2::PamLevel2(){
96      Initialize();
97    };
98    
99    /**
100     * Constructor with given dir, list and detectors
101     */
102    PamLevel2::PamLevel2(TString ddir,TString list,TString detlist){
103      Initialize();
104      GetPamTree(GetListOfLevel2Files(ddir,list),detlist);
105      GetRunTree(GetListOfLevel2Files(ddir,list));
106    };
107    
108    /**
109     * Constructor with given dir and list
110     */
111    PamLevel2::PamLevel2(TString ddir,TString list){
112      Initialize();
113      GetPamTree(GetListOfLevel2Files(ddir,list),"");
114      GetRunTree(GetListOfLevel2Files(ddir,list));
115    };
116    
117    
118    void PamLevel2::Initialize(){
119                    
 //     trk2_obj  = TrkLevel2::GetTrkLevel2();  
 //     trk1_obj  = TrkLevel1::GetTrkLevel1();  
 //     trkh_obj  = TrkHough::GetTrkHough();  
 //     calo1_obj = CaloLevel1::GetCaloLevel1();  
 //     calo2_obj = CaloLevel2::GetCaloLevel2();  
 //     tof_obj   = ToFLevel2::GetToFLevel2();  
 //     trig_obj  = TrigLevel2::GetTrigLevel2();  
 //     s4_obj    = S4Level2::GetS4Level2();  
 //     nd_obj    = NDLevel2::GetNDLevel2();  
 //     ac_obj    = AcLevel2::GetAcLevel2();  
 //     orb_obj   = OrbitalInfo::GetOrbitalInfo();  
       
120      h0_obj    = 0;      h0_obj    = 0;
121      trk0_obj  = 0;      trk0_obj  = 0;
122        calo0_obj  = 0;
123    
124      trk2_obj  = 0;      trk2_obj  = 0;
125      trk1_obj  = 0;      trk1_obj  = 0;
# Line 118  PamLevel2::PamLevel2(){ Line 135  PamLevel2::PamLevel2(){
135    
136      run_obj   = 0;//new GL_RUN();      run_obj   = 0;//new GL_RUN();
137      soft_obj   = 0;// Emiliano      soft_obj   = 0;// Emiliano
138      irun = -1;      irun = -1LL;
139      runfirstentry = 0ULL;      runfirstentry = 0LL;
140      runlastentry = 0ULL;      runlastentry = 0LL;
141    
142      l0_file = NULL;      l0_file = NULL;
143      l0_tree = NULL;      l0_tree = NULL;
144      iroot   = -1;      iroot   = -1;
145      dbc     = 0;      dbc     = 0;
146    
147        prevshift = 0;
148        maxshift = 10; //EMILIANO
149            
     irun = -1;  
150      run_tree = NULL;      run_tree = NULL;
151      run_tree_clone = NULL;      run_tree_clone = NULL;
152      sel_tree = NULL;      sel_tree = NULL;
153      sel_tree_clone = NULL;      sel_tree_clone = NULL;
154            
155      irunentry = -1;      irunentry = -1LL;
156      pam_tree = NULL;      pam_tree = NULL;
157      for(Int_t i=0; i<NCLONES; i++ )pam_tree_clone[i]=NULL;      for(Int_t i=0; i<NCLONES; i++ )pam_tree_clone[i]=NULL;
158    
159        totdltime[0] = 0LL;
160        totdltime[1] = 0LL;
161    
162      host = "mysql://localhost/pamelaprod";      host = "mysql://localhost/pamelaprod";
163      user = "anonymous";      user = "anonymous";
164      psw = "";      psw = "";
# Line 173  PamLevel2::PamLevel2(){ Line 195  PamLevel2::PamLevel2(){
195    
196      SELLI = -1;      SELLI = -1;
197    
198        ISGP = false;
199    
200      tsorted=0;      tsorted=0;
201      timage=0;      timage=0;
202        
203        howtosort = "+CAL+TOF";
204        //howtosort = "+TOF";
205        sortthr = 100.;
206    
207  };  };
208  /**  /**
# Line 188  void PamLevel2::Delete(){ Line 216  void PamLevel2::Delete(){
216  //    cout << "void PamLevel2::Clear()"<<endl;  //    cout << "void PamLevel2::Clear()"<<endl;
217      if(h0_obj)   delete h0_obj;      if(h0_obj)   delete h0_obj;
218      if(trk0_obj) delete trk0_obj;            if(trk0_obj) delete trk0_obj;      
219        if(calo0_obj) delete calo0_obj;    
220      if(trk1_obj) delete trk1_obj;            if(trk1_obj) delete trk1_obj;      
221      if(trk2_obj) delete trk2_obj;      if(trk2_obj) delete trk2_obj;
222      if(trkh_obj) delete trkh_obj;      if(trkh_obj) delete trkh_obj;
# Line 215  void PamLevel2::Delete(){ Line 244  void PamLevel2::Delete(){
244      }      }
245            
246      if(l0_file)l0_file->Close();      if(l0_file)l0_file->Close();
247      if(pam_tree)pam_tree->Delete();;      //    if(pam_tree)pam_tree->Delete();;
248    
249      if ( pam_tree ){
250        //
251        // we have first to find which chains we have to delete, then delete the main chain and only after delete the friend chains. Any other order causes a segfault...
252        //
253        TList *temp = pam_tree->GetListOfFriends();
254        TList *contents  = new TList; // create chain friend list
255        contents->SetOwner();
256        TIter next(temp);
257        TChain *questo = 0;
258        while ( (questo = (TChain*) next()) ){
259          TString name =  questo->GetName();
260          contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
261        };
262        //
263        // deleting the main chain
264        //
265        pam_tree->Delete();
266        //
267        // deleting the friends...
268        //
269        TIter next2(contents);
270        TChain *questa = 0;
271        while ( questa = (TChain*)next2() ){
272          TString name =  questa->GetName();
273          questa->Delete();      
274          questa=NULL;
275        };
276        //
277      };
278      pam_tree = NULL;
279    
280      if(run_tree)run_tree->Delete();;      if(run_tree)run_tree->Delete();;
281      if(sel_tree)sel_tree->Delete();;      if(sel_tree)sel_tree->Delete();;
282      for(Int_t i=0; i<NCLONES; i++ )if(pam_tree_clone[i])pam_tree_clone[i]->Delete();;      for(Int_t i=0; i<NCLONES; i++ )if(pam_tree_clone[i])pam_tree_clone[i]->Delete();;
# Line 241  void PamLevel2::Clear(){ Line 302  void PamLevel2::Clear(){
302  //    if(soft_obj) soft_obj->Clear();  //    if(soft_obj) soft_obj->Clear();
303    
304      if(h0_obj)   h0_obj->Clear();            if(h0_obj)   h0_obj->Clear();      
305      if(trk0_obj) trk0_obj->Clear();      //    if(trk0_obj) trk0_obj->Clear();  
306      if(trk1_obj) trk1_obj->Clear();          if(trk1_obj) trk1_obj->Clear();    
307      if(trk2_obj) trk2_obj->Clear();      if(trk2_obj) trk2_obj->Clear();
308      if(trkh_obj) trkh_obj->Clear();      if(trkh_obj) trkh_obj->Clear();
309        if(calo0_obj) calo0_obj->Clear();  
310      if(calo1_obj)calo1_obj->Clear();      if(calo1_obj)calo1_obj->Clear();
311      if(calo2_obj)calo2_obj->Clear();      if(calo2_obj)calo2_obj->Clear();
312      if(tof_obj)  tof_obj->Clear();      if(tof_obj)  tof_obj->Clear();
# Line 263  void PamLevel2::Clear(){ Line 325  void PamLevel2::Clear(){
325      if(timage){      if(timage){
326          timage->Delete();          timage->Delete();
327      }      }
       
328  };  };
329    
330    void PamLevel2::Reset(){
331      //
332      // First of all clear everything
333      //
334      Clear();
335      //
336      // close and reset chains and pointers
337      //    
338      if ( pam_tree ){
339        //
340        // we have first to find which chains we have to delete, then delete the main chain and only after delete the friend chains. Any other order causes a segfault...
341        //
342        TList *temp = pam_tree->GetListOfFriends();
343        TList *contents  = new TList; // create chain friend list
344        contents->SetOwner();
345        TIter next(temp);
346        TChain *questo = 0;
347        while ( (questo = (TChain*) next()) ){
348          TString name =  questo->GetName();
349          contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
350        };
351        //
352        // deleting the main chain
353        //
354        pam_tree->Delete();
355        //
356        // deleting the friends...
357        //
358        TIter next2(contents);
359        TChain *questa = 0;
360        while ( questa = (TChain*)next2() ){
361          TString name =  questa->GetName();
362          questa->Delete();      
363          questa=NULL;
364        };
365        //
366      };
367      pam_tree = NULL;
368      //
369      if(run_tree)run_tree->Delete();;
370      run_tree = NULL;
371      if(sel_tree)sel_tree->Delete();;
372      sel_tree = NULL;
373      //
374      // Close file
375      //
376      if(l0_file)l0_file->Close("R");
377      l0_file = NULL;
378      //
379      h0_obj    = 0;
380      trk0_obj  = 0;
381      calo0_obj  = 0;
382      //
383      trk2_obj  = 0;
384      trk1_obj  = 0;
385      trkh_obj  = 0;
386      calo1_obj = 0;
387      calo2_obj = 0;
388      tof_obj   = 0;
389      trig_obj  = 0;
390      s4_obj    = 0;
391      nd_obj    = 0;
392      ac_obj    = 0;
393      orb_obj   = 0;
394      //
395      // Reset run pointers
396      //
397      run_obj   = 0;//new GL_RUN();
398      soft_obj   = 0;// Emiliano
399      irun = -1;
400      runfirstentry = 0ULL;
401      runlastentry = 0ULL;    
402      //
403      totdltime[0] = 0LL;
404      totdltime[1] = 0LL;
405      //
406    };
407    
408    Bool_t PamLevel2::IsGood(){
409      Bool_t goodev=true;
410      //  if(trk2_obj && trk2_obj->UnpackError() != 0 ) goodev = false;
411      if(calo2_obj && calo2_obj->good != 1) goodev = false;
412      if(tof_obj && tof_obj->unpackError != 0) goodev = false;  
413      if(trig_obj && trig_obj->unpackError != 0) goodev = false;
414      if(s4_obj && s4_obj->unpackError != 0) goodev = false;  
415      if(nd_obj && nd_obj->unpackError != 0) goodev = false;  
416      if(ac_obj && ac_obj->unpackError != 255) goodev = false;  
417        //  if(orb_obj)  
418      return goodev;
419    };
420    
421  //--------------------------------------  //--------------------------------------
422  //  //
# Line 460  PamTrack* PamLevel2::GetStoredTrack(Int_ Line 611  PamTrack* PamLevel2::GetStoredTrack(Int_
611  }  }
612  //--------------------------------------  //--------------------------------------
613  //  //
614    
615    /**
616     * Sort physical (tracker) tracks and stores them in the TRefArray (of TrkTrack objects) which pointer is  PamLevel2::sorted_tracks. Left here as backward compatibility method.
617     **/
618    void PamLevel2::SortTracks(TString how){
619      printf(" WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",how.Data());
620      howtosort = how;  
621      SortTracks();
622    };
623    
624  //  //
625  //--------------------------------------  //--------------------------------------
626  /**  /**
# Line 473  PamTrack* PamLevel2::GetStoredTrack(Int_ Line 634  PamTrack* PamLevel2::GetStoredTrack(Int_
634   *   *
635   * The total number of physical tracks is always given by GetNTracks() and the it-th physical track can be retrieved by means of the methods GetTrack(int it) and GetTrack(int it, TString how).   * The total number of physical tracks is always given by GetNTracks() and the it-th physical track can be retrieved by means of the methods GetTrack(int it) and GetTrack(int it, TString how).
636   */   */
637  void PamLevel2::SortTracks(TString how){  void PamLevel2::SortTracks(){
638      TString how = howtosort;
639    
640  //    cout <<" PamLevel2::SortTracks(TString how) "<<endl;    //  cout <<" PamLevel2::SortTracks(TString how) "<<endl;
641      if( !trk2_obj ){    if( !trk2_obj ){
642          cout << "void PamLevel2::SortTracks(TString how):  TrkLevel2 not loaded !!!";      cout << "void PamLevel2::SortTracks():  TrkLevel2 not loaded !!!";
643          return;      return;
644      };    };
645  //    cout << "call SortTracs() "<<endl;    //    cout << "call SortTracs() "<<endl;
646    //Save current Object count    //Save current Object count
647      Int_t ObjectNumber = TProcessID::GetObjectCount();    Int_t ObjectNumber = TProcessID::GetObjectCount();
648    
649  //    cout << "ObjectNumber  "<<ObjectNumber <<endl;    //    cout << "ObjectNumber  "<<ObjectNumber <<endl;
650    
651  //     if(!sorted_tracks)sorted_tracks = new TRefArray();    //     if(!sorted_tracks)sorted_tracks = new TRefArray();
652  //     sorted_tracks->Clear();    //     sorted_tracks->Clear();
653  //    sorted_tracks.Clear();    //    sorted_tracks.Clear();
654    
655      if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
656      tsorted->Delete();
657      TClonesArray &ttsorted = *tsorted;
658      if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
659      timage->Delete();
660      TClonesArray &ttimage = *timage;
661    
     if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());  
     tsorted->Delete();  
     TClonesArray &ttsorted = *tsorted;  
     if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());  
     timage->Delete();  
     TClonesArray &ttimage = *timage;  
662    
663    
664      // loop over the tracks sorted by the tracker    //--------------------------------------------------
665      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);    // retrieve sorting method
666      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);    //--------------------------------------------------
667      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);    Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
668          Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
669      if( !CAL2 &&  use_CAL) use_CAL = false;    Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
670      if( !TOF &&  use_TOF) use_TOF = false;    Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase);
671          Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase);
672      if( !TRK2 ){    Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase);
673  //      cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;    
674          return;    if ( use_TOF ){
675      };      use_S1 = true;
676        use_S2 = true;
677        use_S3 = true;
678      };
679      if( !CAL2 &&  use_CAL) use_CAL = false;
680      if( !TOF ){
681        use_TOF = false;
682        use_S1  = false;
683        use_S2  = false;
684        use_S3  = false;
685      }
686    
687      //   cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;    if( !TRK2 ){
688            //  cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
689      for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){      return;
690              };
691          TrkTrack   *ts = 0;  
692          CaloTrkVar *cs = 0;    //   cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;
693          ToFTrkVar  *os = 0;    
694            
695          // get tracker tracks    //--------------------------------------------------
696          TrkTrack   *tp = trk2_obj->TrkLevel2::GetTrack(i);                    //tracker    // loop over "physical" tracks sorted by the tracker
697          CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());    //--------------------------------------------------
698          ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());    for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){
699            
700          TrkTrack   *ti = 0;              //tracker (image)      TrkTrack   *ts = 0;
701          CaloTrkVar *ci = 0;      CaloTrkVar *cs = 0;
702          ToFTrkVar  *oi = 0;      ToFTrkVar  *os = 0;
703  //      cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;          
704          // if track has an image, check image selection      // get tracker tracks
705          if(tp->HasImage()){      TrkTrack   *tp = trk2_obj->TrkLevel2::GetTrack(i); //tracker
706        CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());
707        ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());
708    
709        TrkTrack   *ti = 0; //tracker (image)
710        CaloTrkVar *ci = 0;
711        ToFTrkVar  *oi = 0;
712        //  cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
713        // if track has an image, check image selection
714    
715        Int_t tp_score = 0;  //main track sorted by the tracker
716        Int_t ti_score = 0;  //image track
717        Int_t totp_score = 0;  //main track sorted by the tracker
718        Int_t toti_score = 0;  //image track
719    
720        if(tp->HasImage()){
721                            
722              ti = trk2_obj->TrkLevel2::GetTrackImage(i);              //tracker (image)        ti = trk2_obj->TrkLevel2::GetTrackImage(i);              //tracker (image)
723              ci = GetCaloStoredTrack(ti->GetSeqNo());        ci = GetCaloStoredTrack(ti->GetSeqNo());
724              oi = GetToFStoredTrack(ti->GetSeqNo());        oi = GetToFStoredTrack(ti->GetSeqNo());
725                            
726  //          cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;        //            cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
727    
728              //assign starting scores        //assign starting scores
729              Int_t tp_score = 0;  //main track sorted by the tracker        tp_score = 0;  //main track sorted by the tracker
730              Int_t ti_score = 0;  //image track        ti_score = 0;  //image track
731                            
732              // ------------------------        // -----------------------------------------------------------------------------------------
733              // calorimeter check        // calorimeter check
734              // ------------------------        // -----------------------------------------------------------------------------------------
735              // check the Y spatial residual on the first calorimeter plane        // check the Y spatial residual on the first calorimeter plane
736              // (cut on calorimeter variables from Emiliano)        // (cut on calorimeter variables from Emiliano)
737              if( use_CAL && !calo2_obj ){        if( use_CAL && !calo2_obj ){
738                  cout << "void PamLevel2::SortTracks(TString how): how= "<<how<<" but CaloLevel2 not loaded !!!";          cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but CaloLevel2 not loaded !!!";
739                  return;          return;
740              };        };
741              if(        if( use_CAL && !cp &&  ci ){
742                  use_CAL            &&          ti_score++;
743                  calo2_obj->npcfit[1] > 15     &&   //no. of fit planes on Y view          toti_score++;
744                  calo2_obj->varcfit[1] < 1000. &&  //fit variance on Y view        };
745                  true){        if( use_CAL &&  cp && !ci ){
746            tp_score++;
747            totp_score++;
748          };
749          if(
750             use_CAL            &&
751    //       calo2_obj->npcfit[1] > 5     &&   //no. of fit planes on Y view
752    //       calo2_obj->varcfit[1] < 1000. &&  //fit variance on Y view
753             cp && ci &&
754             true){
755    
756                                    
757                  Float_t resy_p = cp->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_p < 0)resy_p= - resy_p;  //      Float_t resy_p = cp->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_p < 0)resy_p= - resy_p;
758                  Float_t resy_i = ci->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_i < 0)resy_i= - resy_i;  //      Float_t resy_i = ci->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_i < 0)resy_i= - resy_i;
759                                    
760                  if(resy_p <= resy_i) tp_score++;  //      if(resy_p <= resy_i) tp_score++;
761                  else                 ti_score++;  //      else                 ti_score++;
762            
763    
764            if( cp->npresh > ci->npresh){
765              tp_score++;
766              totp_score++;
767            };
768            if( cp->npresh < ci->npresh){
769              ti_score++;
770              toti_score++;
771            };
772    
773            //      cout << "CALO "<<tp_score<<ti_score<<endl;
774    
775  //              cout << "CALO "<<tp_score<<ti_score<<endl;        };
776          // -----------------------------------------------------------------------------------------
777          // TOF check
778          // -----------------------------------------------------------------------------------------
779          // check the number of hit pmts along the track
780          // on S12 S21 and S32, where paddles are parallel to Y axis
781          if( (use_TOF || use_S1 || use_S2 || use_S3 ) && !tof_obj ){
782            cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but ToFLevel2 not loaded !!!";
783            return;
784          };
785          //
786          if( (use_TOF || use_S1 || use_S2 || use_S3 ) && !op && oi ){
787            ti_score++;
788            toti_score++;
789          };
790          if( (use_TOF || use_S1 || use_S2 || use_S3 ) && op && !oi ){
791            tp_score++;
792            totp_score++;
793          };
794          if( (use_TOF || use_S1 || use_S2 || use_S3 ) && op && oi ){
795            //
796            Float_t sen = 0.;
797            for (Int_t ih=0; ih < op->npmtadc; ih++){
798              Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
799              if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (op->dedx).At(ih);
800            };
801            for (Int_t ih=0; ih < oi->npmtadc; ih++){
802              Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
803              if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (oi->dedx).At(ih);
804            };
805            //
806            if (  sen >= sortthr && false){ // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!)
807              //printf(" IS A NUCLEUS! en = %f \n",sen);
808              //
809              // is a nucleus use a different algorithm
810              //
811              Int_t nz = 6; Float_t zin[6];                                          // << define TOF z-coordinates
812              for(Int_t ip=0; ip<nz; ip++)
813                zin[ip] = tof_obj->ToFLevel2::GetZTOF(tof_obj->ToFLevel2::GetToFPlaneID(ip));     // << read ToF plane z-coordinates
814              Trajectory *tr = new Trajectory(nz,zin);
815              //
816              Int_t nphit_p =0;
817              Int_t nphit_i =0;
818              Float_t enhit_p = 0.;
819              Float_t enhit_i = 0.;
820              //
821              for (Int_t ih=0; ih < op->npmtadc; ih++){
822                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
823                if(pl == 1 || pl == 2 || pl == 5){
824                  nphit_p++;
825                  enhit_p += (op->dedx).At(ih);
826                };
827              };
828              //
829              tp->DoTrack2(tr);
830              //
831              if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
832                for (Int_t ih=0; ih < op->npmtadc; ih++){
833                  Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
834                  if(pl == 0){
835                    nphit_p++;
836                    enhit_p += (op->dedx).At(ih);
837                  };
838                };
839              };
840              if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
841                for (Int_t ih=0; ih < op->npmtadc; ih++){
842                  Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
843                  if(pl == 3){
844                    nphit_p++;
845                    enhit_p += (op->dedx).At(ih);
846                  };
847                };
848              };
849              if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
850                for (Int_t ih=0; ih < op->npmtadc; ih++){
851                  Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
852                  if(pl == 4){
853                    nphit_p++;
854                    enhit_p += (op->dedx).At(ih);
855                  };
856                };
857              };
858    
859              for (Int_t ih=0; ih < oi->npmtadc; ih++){
860                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
861                if(pl == 1 || pl == 2 || pl == 5){
862                  nphit_i++;        
863                  enhit_i += (op->dedx).At(ih);
864                };
865              };
866              //
867              ti->DoTrack2(tr);
868              //
869              if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
870                for (Int_t ih=0; ih < oi->npmtadc; ih++){
871                  Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
872                  if(pl == 0){
873                    nphit_i++;
874                    enhit_i += (op->dedx).At(ih);
875                  };
876              };              };
877              // ------------------------            };
878              // TOF check            if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
879              // ------------------------                  for (Int_t ih=0; ih < oi->npmtadc; ih++){
880              // check the number of hit pmts along the track                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
881              // on S12 S21 and S32, where paddles are parallel to Y axis                if(pl == 3){
882              if( use_TOF && !tof_obj ){                  nphit_i++;
883                  cout << "void PamLevel2::SortTracks(TString how): how= "<<how<<" but ToFLevel2 not loaded !!!";                  enhit_i += (op->dedx).At(ih);
884                  return;                };
885              };              };
886              if( use_TOF ){            };
887              if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
888                for (Int_t ih=0; ih < oi->npmtadc; ih++){
889                  Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
890                  if(pl == 4){
891                    nphit_i++;
892                    enhit_i += (op->dedx).At(ih);
893                  };
894                };
895              };            
896    
897                    
898              if(
899                 (use_TOF || use_S1 || use_S2 || use_S3)            &&
900                 (nphit_p+nphit_i) !=0 &&  
901                 true){
902                        
903                //      printf(" seqno %i nphit_p %i nphit_i %i enhit_p %f enhit_i %f \n",trk2_obj->TrkLevel2::GetSeqNo(i),nphit_p,nphit_i,enhit_p,enhit_i);
904                //      printf(" score p %i score i %i \n",tp_score,ti_score);
905                //            if( enhit_p > enhit_i ) tp_score++;
906                //            if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
907                if ( nphit_p > nphit_i ) tp_score++;
908                if ( nphit_p < nphit_i ) ti_score++;
909                if ( nphit_p == nphit_i ){
910                  if ( enhit_p > enhit_i ) tp_score++;
911                  else ti_score++;
912                };
913                //      printf(" dopo score p %i score i %i \n",tp_score,ti_score);
914              };
915              delete tr;
916              //
917            } else {
918              //
919              // NOT a NUCLEUS
920              //
921              //printf(" NOT a NUCLEUS! en = %f \n",sen);
922    
923              Int_t nphit_p =0;
924              Int_t nphit_i =0;
925                                    
                 Int_t nphit_p =0;  
                 Int_t nphit_i =0;  
926                                    
927              /*                            cout << "track: npmtadc "<< op->npmtadc << endl;
928                cout << "track: npmttdc "<< op->npmttdc << endl;
929                cout << "image: npmtadc "<< oi->npmtadc << endl;
930                cout << "image: npmttdc "<< oi->npmttdc << endl;*/
931                                    
932  /*                              cout << "track: npmtadc "<< op->npmtadc << endl;  //        for (Int_t ih=0; ih < op->npmtadc; ih++){
933                                  cout << "track: npmttdc "<< op->npmttdc << endl;  //          Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
934                                  cout << "image: npmtadc "<< oi->npmtadc << endl;  //          if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
935                                  cout << "image: npmttdc "<< oi->npmttdc << endl;*/  //        };
936                                    
937                  for (Int_t ih=0; ih < op->npmtadc; ih++){  //        for (Int_t ih=0; ih < oi->npmtadc; ih++){
938                      Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );  //          Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
939                      if(pl == 1 || pl == 2 || pl == 5)nphit_p++;  //          if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
940                  };  //        };
941              // --- modified to count tdc signals (more efficient?)
942              // --- and to implement check on tdcflag
943              for (Int_t ih=0; ih < op->npmttdc; ih++){
944                Int_t pl = tof_obj->GetPlaneIndex( (op->pmttdc).At(ih) );
945    //          if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
946                if ( (use_S1 && ( pl == 0 || pl == 1 )) || (use_S2 && ( pl == 2 || pl == 3 )) || (use_S3 && ( pl == 4 || pl == 5 )) ){
947                  if( (op->tdcflag).At(ih)==0 )nphit_p++;
948                };
949              };
950                                    
951                  for (Int_t ih=0; ih < oi->npmtadc; ih++){            for (Int_t ih=0; ih < oi->npmttdc; ih++){
952                      Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );              Int_t pl = tof_obj->GetPlaneIndex( (oi->pmttdc).At(ih) );
953                      if(pl == 1 || pl == 2 || pl == 5)nphit_i++;  //          if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;  
954                  };              if ( (use_S1 && ( pl == 0 || pl == 1 )) || (use_S2 && ( pl == 2 || pl == 3 )) || (use_S3 && ( pl == 4 || pl == 5 )) ){
955                  if( (oi->tdcflag).At(ih)==0 )nphit_i++;  
956                };
957              };
958                                    
959                  if(            if(
960                      use_TOF            &&               (nphit_p+nphit_i) !=0 &&  
961                      (nphit_p+nphit_i) !=0 &&                   true){
                     true){  
962                                            
963                      if( nphit_p >= nphit_i) tp_score++;              if ( nphit_p != nphit_i ){
964                      else ti_score++;                totp_score += nphit_p;
965                  };                toti_score += nphit_i;
966  //              cout << "TOF "<<tp_score<<ti_score<<endl;                tp_score+=nphit_p;
967              };                ti_score+=nphit_i;
             if(tp_score == ti_score) use_TRK = true;  
             // ------------------------  
             // tracker check  
             // ------------------------  
             // chi**2 difference is not always large enough to distinguish among  
             // the real track and its image.  
             // Tracker check will be applied always when calorimeter and tof information is ambiguous.  
             if(use_TRK){  
                 if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;  
                 else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;  
 //              cout << "TRK "<<tp_score<<ti_score<<endl;  
968              };              };
969                //      if     ( nphit_p > nphit_i) tp_score+=nphit_p;
970                //      else if( nphit_p < nphit_i) ti_score+=nphit_i;
971                //      else ;//niente
972              };
973            };
974            //      cout << "TOF "<<tp_score<<ti_score<<endl;
975          };
976    
977    
978    //      if(tp_score == ti_score) use_TRK = true;
979    
980    
981          // -----------------------------------------------------------------------------------------
982          // tracker check
983          // -----------------------------------------------------------------------------------------
984          // chi**2 difference is not always large enough to distinguish among
985          // the real track and its image.
986          // Tracker check will be applied always when calorimeter and tof information is ambiguous.
987          // *** MODIFIED ON AUGUST 2007 ***
988          if(use_TRK){
989    //      if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
990    //      else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
991    
992              // CHECK 1 : number of points along X  
993            if ( tp->GetNX() >= ti->GetNX() ){
994              tp_score++ ;
995              totp_score++ ;
996            };
997            if ( tp->GetNX() <= ti->GetNX() ){
998              ti_score++ ;
999              toti_score++ ;
1000            };
1001              // CHECK 2 : number of points along Y  
1002            if ( tp->GetNY() >= ti->GetNY() ){
1003              tp_score++ ;
1004              totp_score++ ;
1005            };
1006            if ( tp->GetNY() <= ti->GetNY() ){
1007              ti_score++ ;
1008              toti_score++ ;
1009            };
1010              // CHECK 3 : chi**2 along X  
1011    //        if(      tp->GetChi2X() > 0 && (tp->GetChi2X() < ti->GetChi2X() || ti->GetChi2X() <=0) ) tp_score++ ;
1012    //        if(      ti->GetChi2X() > 0 && (ti->GetChi2X() < tp->GetChi2X() || tp->GetChi2X() <=0) ) ti_score++ ;
1013              // CHECK 4 : chi**2 along Y  
1014    //        if(      tp->GetChi2Y() > 0 && (tp->GetChi2Y() < ti->GetChi2Y() || ti->GetChi2Y() <=0) ) tp_score++ ;
1015    //        if(      ti->GetChi2Y() > 0 && (ti->GetChi2Y() < tp->GetChi2Y() || tp->GetChi2Y() <=0) ) ti_score++ ;
1016              // CHECK 5 : total chi**2  
1017    //        if(      tp->chi2 > 0 && (tp->chi2 < ti->chi2 || ti->chi2 <=0) ) tp_score++ ;
1018    //        if(      ti->chi2 > 0 && (ti->chi2 < tp->chi2 || tp->chi2 <=0) ) ti_score++ ;
1019    
1020            //      cout << "TRK "<<tp_score<<ti_score<<endl;
1021          };
1022                            
1023              // ------------------------  
1024              // the winner is....  
1025              // ------------------------            // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1026              if      (tp_score > ti_score) {        // the winner is....
1027  //              ts = tp;//the track sorted by the tracker!!        // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1028  //              cs = cp;        if      (tp_score > ti_score) {
1029  //              os = op;          
1030              }else if (tp_score < ti_score) {  
1031                  ts = ti;//its image!!        }else if (tp_score < ti_score) {
1032                  cs = ci;  
1033                  os = oi;  
1034            ts = ti;//its image!!
1035                  ti = tp;//its image!!          cs = ci;
1036                  ci = cp;          os = oi;
1037                  oi = op;          Int_t totis = toti_score;
1038    
1039                  tp = ts;//its image!!          ti = tp;//its image!!
1040                  cp = cs;          ci = cp;
1041                  op = os;          oi = op;
1042    
1043            tp = ts;//its image!!
1044            cp = cs;
1045            op = os;
1046    
1047            toti_score = totp_score;
1048            totp_score = totis;
1049    
1050                                    
1051              }else {        }else {
1052  //              ts = tp;  
1053  //              cs = cp;  //      cout << "Warning - track image ambiguity not solved" << endl;
1054  //              os = op;  
1055  //                              cout << "Warning - track image ambiguity not solved" << endl;        };
 //                              cout << ts->GetNtot() << " "<< ts->chi2 << " " << npcfit[1] << " "<< nphit_p << endl;  
             };  
1056                            
1057          }else{      }else{
1058  //          ts = tp;        totp_score = 1;
1059  //          cs = cp;        toti_score = 0;
1060  //          os = op;        
1061          };        //            ts = tp;
1062                  //            cs = cp;
1063  //      cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;        //            os = op;
1064  //      sorted_tracks->Add(ts);//save the track in the sorted array      };
1065  //      sorted_tracks.Add(ts);//save the track in the sorted array          
1066  //      sorted_tracks.Add(tp);//save the track in the sorted array      //  cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
1067  //      cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;      //  sorted_tracks->Add(ts);//save the track in the sorted array
1068  //      cout<<"o "<<tp<<endl;      //  sorted_tracks.Add(ts);//save the track in the sorted array
1069  //      cout<<"o "<<cp<<endl;      //  sorted_tracks.Add(tp);//save the track in the sorted array
1070  //      cout<<"o "<<op<<endl;      //  cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
1071          new(ttsorted[i]) PamTrack(tp,cp,op);      //  cout<<"o "<<tp<<endl;
1072          new(ttimage[i])  PamTrack(ti,ci,oi);      //  cout<<"o "<<cp<<endl;
1073      };      //  cout<<"o "<<op<<endl;
1074    
1075      if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){      new(ttsorted[i]) PamTrack(tp,cp,op);
1076          cout << "void PamLevel2::SortTracks(TString how): tsorted->GetEntries() "<<tsorted->GetEntries()<<" != trk2_obj->GetNTracks() = "<<trk2_obj->GetNTracks() <<endl;      new(ttimage[i])  PamTrack(ti,ci,oi);
1077          tsorted->Delete(); tsorted=0;  
1078          timage->Delete(); timage=0;          ((PamTrack*)(ttsorted[i]))->SetPScore(totp_score);
1079      }      ((PamTrack*)(ttsorted[i]))->SetIScore(toti_score);
1080        ((PamTrack*)(ttimage[i]))->SetPScore(totp_score);
1081      //Restore Object count      ((PamTrack*)(ttimage[i]))->SetIScore(toti_score);
1082      //To save space in the table keeping track of all referenced objects    };
1083      //We reset the object count to what it was at the beginning of the event.  
1084      TProcessID::SetObjectCount(ObjectNumber);    if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){
1085        cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() "<<tsorted->GetEntries()<<" != trk2_obj->GetNTracks() = "<<trk2_obj->GetNTracks() <<endl;
1086        tsorted->Delete(); tsorted=0;
1087        timage->Delete(); timage=0;
1088      }
1089    
1090      //Restore Object count
1091      //To save space in the table keeping track of all referenced objects
1092      //We reset the object count to what it was at the beginning of the event.
1093      TProcessID::SetObjectCount(ObjectNumber);
1094            
1095  };  };
1096  //--------------------------------------  //--------------------------------------
# Line 699  void PamLevel2::SortTracks(TString how){ Line 1114  void PamLevel2::SortTracks(TString how){
1114  TClonesArray *PamLevel2::GetTracks(){  TClonesArray *PamLevel2::GetTracks(){
1115    
1116  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1117      SortTracks("+CAL+TOF");      SortTracks();
1118  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1119    
1120      return tsorted;      return tsorted;
# Line 718  PamTrack *PamLevel2::GetTrack(int it){ Line 1133  PamTrack *PamLevel2::GetTrack(int it){
1133    
1134  //    if(!trk2_obj) return 0;  //    if(!trk2_obj) return 0;
1135                    
1136  // //  *-*-*-*-*-*-*-*-*-*-*-*-*  // //  *-*-*-*+-*-*-*-*-*-*-*-*-*
1137  //     SortTracks("+CAL+TOF");  //     SortTracks("+CAL+TOF");
1138  // //  *-*-*-*-*-*-*-*-*-*-*-*-*  // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1139  // //    if(!sorted_tracks)return 0;  // //    if(!sorted_tracks)return 0;
# Line 737  PamTrack *PamLevel2::GetTrack(int it){ Line 1152  PamTrack *PamLevel2::GetTrack(int it){
1152    
1153  //    cout << "PamLevel2::GetTrack(int it) "<<endl;  //    cout << "PamLevel2::GetTrack(int it) "<<endl;
1154  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1155      SortTracks("+CAL+TOF");      SortTracks();
1156  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1157      if(!tsorted)return 0;      if(!tsorted)return 0;
1158      if(!tsorted->GetEntries())return 0;      if(!tsorted->GetEntries())return 0;
# Line 794  PamTrack *PamLevel2::GetTrackImage(int i Line 1209  PamTrack *PamLevel2::GetTrackImage(int i
1209    
1210    
1211  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1212      SortTracks("+CAL+TOF");      SortTracks();
1213  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1214      if(!timage)return 0;      if(!timage)return 0;
1215      if(!timage->GetEntries())return 0;      if(!timage->GetEntries())return 0;
# Line 831  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1246  TTree *PamLevel2::GetPamTree(TFile *f, T
1246    
1247  //     if( !detlist.IsNull() ) SetWhichTrees(detlist);  //     if( !detlist.IsNull() ) SetWhichTrees(detlist);
1248  //     else                    GetWhichTrees(f);  //     else                    GetWhichTrees(f);
1249          if ( pam_tree ){
1250        printf("WARNING: TTree *PamLevel2::GetPamTree(TFile *fl, TString detlist) -- pam_tree already exists!\n ");
1251        return pam_tree;
1252      };
1253      //
1254    
1255      cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;      cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;
1256    
1257      SetWhichTrees(detlist);      SetWhichTrees(detlist);
# Line 907  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1327  TTree *PamLevel2::GetPamTree(TFile *f, T
1327      if(R && TRG) {      if(R && TRG) {
1328          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1329          cout << "Trigger      : set branch address TrigLevel2"<<endl;          cout << "Trigger      : set branch address TrigLevel2"<<endl;
1330          if(!Trout)Trout=O;          if(!Trout)Trout=R;
1331          else Trout->AddFriend(R);          else Trout->AddFriend(R);
1332      }else{      }else{
1333          cout << "Trigger      : missing tree"<<endl;          cout << "Trigger      : missing tree"<<endl;
# Line 917  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1337  TTree *PamLevel2::GetPamTree(TFile *f, T
1337      if(S && S4) {      if(S && S4) {
1338          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1339          cout << "S4           : set branch address S4Level2"<<endl;          cout << "S4           : set branch address S4Level2"<<endl;
1340          if(!Trout)Trout=O;          if(!Trout)Trout=S;
1341          else Trout->AddFriend(S);          else Trout->AddFriend(S);
1342      }else{      }else{
1343          cout << "S4           : missing tree"<<endl;          cout << "S4           : missing tree"<<endl;
# Line 927  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1347  TTree *PamLevel2::GetPamTree(TFile *f, T
1347      if(N && ND) {      if(N && ND) {
1348          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1349          cout << "NeutronD     : set branch address NDLevel2"<<endl;          cout << "NeutronD     : set branch address NDLevel2"<<endl;
1350          if(!Trout)Trout=O;          if(!Trout)Trout=N;
1351          else Trout->AddFriend(N);          else Trout->AddFriend(N);
1352      }else{      }else{
1353          cout << "NeutronD     : missing tree"<<endl;          cout << "NeutronD     : missing tree"<<endl;
# Line 937  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1357  TTree *PamLevel2::GetPamTree(TFile *f, T
1357      if(A && AC) {      if(A && AC) {
1358          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1359          cout << "Anticounter  : set branch address AcLevel2"<<endl;          cout << "Anticounter  : set branch address AcLevel2"<<endl;
1360          if(!Trout)Trout=O;          if(!Trout)Trout=A;
1361          else Trout->AddFriend(A);          else Trout->AddFriend(A);
1362      }else{      }else{
1363          cout << "Anticounter  : missing tree"<<endl;          cout << "Anticounter  : missing tree"<<endl;
# Line 947  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1367  TTree *PamLevel2::GetPamTree(TFile *f, T
1367      if(B && ORB) {      if(B && ORB) {
1368          B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1369          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
1370          if(!Trout)Trout=O;          if(!Trout)Trout=B;
1371          else Trout->AddFriend(B);          else Trout->AddFriend(B);
1372      }else{      }else{
1373          cout << "OrbitalInfo  : missing tree"<<endl;          cout << "OrbitalInfo  : missing tree"<<endl;
# Line 968  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1388  TTree *PamLevel2::GetPamTree(TFile *f, T
1388      };      };
1389            
1390      cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;      cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;
       
     pam_tree = (TChain*)Trout;  
1391    
1392        pam_tree = (TChain*)Trout;
1393        
1394      return Trout;      return Trout;
1395            
1396  }  }
# Line 989  TList*  PamLevel2::GetListOfLevel2Files( Line 1409  TList*  PamLevel2::GetListOfLevel2Files(
1409                    
1410      TString wdir = gSystem->WorkingDirectory();      TString wdir = gSystem->WorkingDirectory();
1411            
1412      if(ddir=="")ddir = wdir;      //    if(ddir=="")ddir = wdir;
1413  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1414      cout << "Level2 data directory : "<<  ddir << endl;      if ( ddir != ""){
1415              cout << "Level2 data directory : "<<  ddir << endl;
1416        } else {
1417          cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory " << endl;
1418        };
1419      TList *contents  = new TList; // create output list      TList *contents  = new TList; // create output list
1420      contents->SetOwner();      contents->SetOwner();
1421            
1422  //    char *fullpath;  //    char *fullpath;
1423  //    const char *fullpath;  //    const char *fullpath;
1424            
1425      // if input file list is given:          // if no input file list is given:  
1426      if ( flisttxt != "" ){      if ( flisttxt != "" ){
1427                    
1428  //      if( !gSystem->IsFileInIncludePath(flisttxt,&fullpath) ){          //      if( !gSystem->IsFileInIncludePath(flisttxt,&fullpath) ){        
# Line 1008  TList*  PamLevel2::GetListOfLevel2Files( Line 1431  TList*  PamLevel2::GetListOfLevel2Files(
1431  //          return 0;  //          return 0;
1432  //      }        //      }      
1433  //      flisttxt = fullpath;  //      flisttxt = fullpath;
   
1434        if ( !flisttxt.EndsWith(".root") ){        if ( !flisttxt.EndsWith(".root") ){
1435    
1436          flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));          flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1437    
1438          if( !gSystem->ChangeDirectory(ddir) ){          if( !gSystem->ChangeDirectory(ddir) ){
# Line 1041  TList*  PamLevel2::GetListOfLevel2Files( Line 1464  TList*  PamLevel2::GetListOfLevel2Files(
1464  //          if( gSystem->IsFileInIncludePath(file,&fullpath) ){  //          if( gSystem->IsFileInIncludePath(file,&fullpath) ){
1465  //          if( (fullpath = gSystem->FindFile(ddir,file)) ){  //          if( (fullpath = gSystem->FindFile(ddir,file)) ){
1466              if( file.EndsWith(".root") ){              if( file.EndsWith(".root") ){
1467                  char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir),gSystem->BaseName(file));                TString filedir;
1468                  if (ddir != ""){
1469                    filedir = ddir; // take the input dir
1470                  } else {
1471                    gSystem->ChangeDirectory(wdir); // back to the working directory
1472                    filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir
1473                  };
1474                    char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir),gSystem->BaseName(file));
1475                  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
1476                  delete fullpath;                  delete fullpath;
1477              }              }
# Line 1051  TList*  PamLevel2::GetListOfLevel2Files( Line 1481  TList*  PamLevel2::GetListOfLevel2Files(
1481          };          };
1482          in.close();          in.close();
1483        } else {        } else {
1484              if(flisttxt.Contains("#"))flisttxt = flisttxt(0,flisttxt.First("#"));
1485            char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));            char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1486            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
1487            delete fullpath;            delete fullpath;
1488        };              };        
1489      }else{      }else{
1490                    
1491          cout << "No input file list given."<<endl;          cout << "No input file list given."<<endl;
1492          cout << "Check for existing root files."<<endl;          cout << "Check for existing root files."<<endl;
1493  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;
1494                    if ( ddir == "" ){
1495              ddir = wdir;
1496              cout << "Level2 data directory : "<<  ddir << endl;
1497            };
1498    
1499          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1500          TList *temp = datadir->GetListOfFiles();          TList *temp = datadir->GetListOfFiles();
1501  //              temp->Print();  //              temp->Print();
# Line 1104  TList*  PamLevel2::GetListOfLevel2Files( Line 1539  TList*  PamLevel2::GetListOfLevel2Files(
1539   * @return Pointer to a TChain   * @return Pointer to a TChain
1540   */   */
1541  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){
1542      //
1543      //
1544      //
1545      if ( pam_tree ){
1546        printf("WARNING: TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) -- pam_tree already exists!\n ");
1547        return pam_tree;
1548      };
1549      //
1550            
1551      TChain *Trout =0;      TChain *Trout =0;
1552    
# Line 1124  TChain *PamLevel2::GetPamTree(TList *fl, Line 1567  TChain *PamLevel2::GetPamTree(TList *fl,
1567      if(ORB)cout<<"ORB ";      if(ORB)cout<<"ORB ";
1568      cout << endl;      cout << endl;
1569    
1570      TChain *T = 0;             TChain *T = 0;    
1571      TChain *C = 0;       TChain *C = 0;
1572      TChain *O = 0;       TChain *O = 0;
1573      TChain *R = 0;       TChain *R = 0;
1574      TChain *S = 0;       TChain *S = 0;
1575      TChain *N = 0;       TChain *N = 0;
1576      TChain *A = 0;       TChain *A = 0;
1577      TChain *B = 0;       TChain *B = 0;
1578    
1579      TChain *L = 0;       TChain *L = 0;
1580            
1581      if(TRK2||TRK1||TRKh) T = new TChain("Tracker");          if(TRK2||TRK1||TRKh) T = new TChain("Tracker");    
1582      if(CAL2||CAL1)       C = new TChain("Calorimeter");      if(CAL2||CAL1)       C = new TChain("Calorimeter");
# Line 1143  TChain *PamLevel2::GetPamTree(TList *fl, Line 1586  TChain *PamLevel2::GetPamTree(TList *fl,
1586      if(ND)               N = new TChain("NeutronD");      if(ND)               N = new TChain("NeutronD");
1587      if(AC)               A = new TChain("Anticounter");      if(AC)               A = new TChain("Anticounter");
1588      if(ORB)              B = new TChain("OrbitalInfo");      if(ORB)              B = new TChain("OrbitalInfo");
   
1589       L = new TChain("SelectionList");       L = new TChain("SelectionList");
1590            
1591      // loop over files and create chains              // loop over files and create chains        
# Line 1163  TChain *PamLevel2::GetPamTree(TList *fl, Line 1605  TChain *PamLevel2::GetPamTree(TList *fl,
1605              if(ORB)              B->Add(name);              if(ORB)              B->Add(name);
1606              if(SELLI==1)         L->Add(name);              if(SELLI==1)         L->Add(name);
1607          };          };
1608      }      };
1609            
1610      cout << "done chain \n";      cout << "done chain \n";
1611        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
1612    
1613  //    UInt_t *found=0;  //    UInt_t *found=0;
1614  // Tracker  // Tracker
# Line 1180  TChain *PamLevel2::GetPamTree(TList *fl, Line 1623  TChain *PamLevel2::GetPamTree(TList *fl,
1623  //      else    T->SetBranchStatus("TrkHough",0,found);  //      else    T->SetBranchStatus("TrkHough",0,found);
1624          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;
1625          if(!Trout)Trout=T;          if(!Trout)Trout=T;
1626          else Trout->AddFriend("Tracker");          else Trout->AddFriend("Tracker");
1627      }else{      }else{
1628          cout << "Tracker      : missing tree"<<endl;          cout << "Tracker      : missing tree"<<endl;
1629      };      };
# Line 1251  TChain *PamLevel2::GetPamTree(TList *fl, Line 1694  TChain *PamLevel2::GetPamTree(TList *fl,
1694      }else{      }else{
1695          cout << "OrbitalInfo  : missing tree"<<endl;          cout << "OrbitalInfo  : missing tree"<<endl;
1696      };      };
1697        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
1698    
1699      // Selection List      // Selection List
1700      if(L && SELLI==1) {      if(L && SELLI==1) {
# Line 1262  TChain *PamLevel2::GetPamTree(TList *fl, Line 1706  TChain *PamLevel2::GetPamTree(TList *fl,
1706          sel_tree = L;          sel_tree = L;
1707  //      if(!Trout)Trout=O;  //      if(!Trout)Trout=O;
1708  //      else Trout->AddFriend("SelectionList");  //      else Trout->AddFriend("SelectionList");
1709            cout << "----------------------------------------------------" <<endl;
1710      }else{      }else{
1711  //      cout << "SelectionList  : missing tree"<<endl;  //      cout << "SelectionList  : missing tree"<<endl;
1712          if(L)L->Delete();          if(L)L->Delete();
# Line 1453  void PamLevel2::SetBranchAddress(TChain Line 1898  void PamLevel2::SetBranchAddress(TChain
1898   * @return Pointer to a TChain   * @return Pointer to a TChain
1899   */   */
1900  TChain *PamLevel2::GetRunTree(TList *fl){  TChain *PamLevel2::GetRunTree(TList *fl){
1901              //
1902      TChain *R = new TChain("Run");    //
1903      //
1904      if ( run_tree ){
1905        printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");
1906        return run_tree;
1907      };    
1908      //
1909      TChain *R = new TChain("Run");
1910            
1911      // loop over files and create chains              // loop over files and create chains        
1912      TIter next(fl);      TIter next(fl);
# Line 1493  TChain *PamLevel2::GetRunTree(TList *fl) Line 1945  TChain *PamLevel2::GetRunTree(TList *fl)
1945   * @return Pointer to a TTree   * @return Pointer to a TTree
1946   */   */
1947  TTree *PamLevel2::GetRunTree(TFile *f){  TTree *PamLevel2::GetRunTree(TFile *f){
1948      if ( run_tree ){
1949        printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");
1950        return run_tree;
1951      };
1952            
1953    
1954      cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;      cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;
1955    
1956      TTree *R = (TTree*)f->Get("Run");      TTree *T = (TTree*)f->Get("Run");
1957            
1958      if(R){      if(T){
1959          R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));          T->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
1960          cout << "Run         : set branch address RunInfo"<<endl;          cout << "Run         : set branch address RunInfo"<<endl;
1961          R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano          T->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
1962          cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano          cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano
1963      }      }
1964    
1965      run_tree = (TChain*)R;      run_tree = (TChain*)T;
1966    
1967      return R;      return T;
1968            
1969  }  }
1970  /**  /**
# Line 1515  TTree *PamLevel2::GetRunTree(TFile *f){ Line 1972  TTree *PamLevel2::GetRunTree(TFile *f){
1972   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
1973   * @return true if a new run has been read, false if it is still the same run   * @return true if a new run has been read, false if it is still the same run
1974   */   */
1975  Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev){  Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev){
1976    //    //
1977    // check if we have already called once GetEntry, if not call it    // check if we have already called once GetEntry, if not call it
1978    //    //
1979    if ( run->GetEntries() <= 0 ) return(false);      cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) --- ATTENZIONE --- NON E` MANTENUTA!!!!!!!.... "<<endl;
1980        if(!run){
1981              cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing RunInfo tree "<<endl;
1982              return(false);        
1983        }
1984        if ( run->GetEntries() <= 0 ) return(false);
1985    //    //
1986        
1987    Int_t oldrun = irun;    //  Int_t oldrun = irun;
1988      Long64_t oldrun = irun;
1989    
1990    // --------------------------------------    // --------------------------------------
1991    // if it is a full file (not preselected)    // if it is a full file (not preselected)
1992    // --------------------------------------    // --------------------------------------
1993    if(SELLI==0){    if(SELLI==0){
       if ( irun < 0 ){  
           irun = 0;  
           run->GetEntry(0);  
           runfirstentry = 0ULL;  
           runlastentry += (ULong64_t)(this->GetRunInfo()->NEVENTS) - 1ULL;  
       };        
1994    
1995        if( !GetOrbitalInfo() ){        //
1996          // the absolute time is necessary to relate the event with the run
1997          //
1998          if( !GetOrbitalInfo() && !ISGP){
1999            cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;            cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
2000            return(false);            return(false);
2001        }        }
2002    
2003          ULong64_t abstime = 0;
2004          if( GetOrbitalInfo() )abstime=GetOrbitalInfo()->absTime;
2005    
2006    
2007          //
2008          // the first time the routine is called, set run search from the beginning
2009          //
2010          if ( irun < 0LL ){
2011              irun = 0LL;
2012              run->GetEntry(irun);
2013              runfirstentry = 0LL;
2014              runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2015              if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
2016              
2017              if( ISGP && run->GetEntries()!=1 ){
2018                  cout << "** WARNING ** simulated files are assumed to have 1 single run, not "<< run->GetEntries() << endl;
2019                  cout << "** WARNING ** run will not be updated"<<endl;
2020              }
2021    
2022          };      
2023          //
2024          if(ISGP)abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO
2025          //
2026          if ( irun == run->GetEntries()-1LL &&
2027               !(abstime >= GetRunInfo()->RUNHEADER_TIME &&
2028                 abstime <= GetRunInfo()->RUNTRAILER_TIME)
2029                ){
2030             irun = -1LL;
2031             runfirstentry = 0LL;
2032             runlastentry = -1LL;
2033           };
2034        // modificato il controllo sull'aggiornamento del run, per evitare problemi        // modificato il controllo sull'aggiornamento del run, per evitare problemi
2035        // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)        // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)
2036        while ( GetOrbitalInfo()->absTime > GetRunInfo()->RUNTRAILER_TIME && irun < run->GetEntries() ){        //
2037          bool fromfirst = true;
2038          //
2039          while ( !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME) && irun < run->GetEntries()-1LL ){
2040  //      while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){  //      while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){
           //    printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));  
2041            irun++;            irun++;
2042            run->GetEntry(irun);            run->GetEntry(irun);
2043            runfirstentry = runlastentry+1ULL;            runfirstentry = runlastentry;
2044            runlastentry += (ULong64_t)(this->GetRunInfo()->NEVENTS);            if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runfirstentry += 1LL;
2045              runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2046    //        cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2047    //        cout << "runfirstentry "<<runfirstentry<<endl;
2048              //      printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));
2049              //      printf(" abstime %u trailertime %u \n",abstime,GetRunInfo()->RUNTRAILER_TIME);
2050              //      printf(" IDRUN %u \n",GetRunInfo()->ID);
2051              //
2052    //        prevshift = 0;
2053              //
2054              if ( irun == (Long64_t)(run->GetEntries()-1LL) && fromfirst && !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME)){
2055                  printf(" resetting irun  (it should NOT happen!!!)\n");
2056                  fromfirst = false;
2057                  irun = 0;
2058                  run->GetEntry(irun);
2059                  runfirstentry = 0ULL;
2060                  runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2061                  if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
2062              };
2063              //
2064        };        };
2065                //
2066          if (
2067               !(abstime >= GetRunInfo()->RUNHEADER_TIME &&
2068                 abstime <= GetRunInfo()->RUNTRAILER_TIME)
2069              ) {
2070            printf(" Something very wrong here: cannot find RUN containing absolute time %llu \n",abstime);
2071              return false;
2072          }
2073        //        //
2074        if ( irun == oldrun || irun >= run->GetEntries() ) return(false);        if ( irun == oldrun || irun >= run->GetEntries() ) return(false);
2075        //        //
2076        //  printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);        //  printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);
2077        //        //
2078          prevshift = 0;
2079          cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2080    //      cout << "runfirstentry "<<runfirstentry<<endl;
2081        return(true);            return(true);    
2082    };    };
2083    // ----------------------------------------------------    // ----------------------------------------------------
# Line 1562  Bool_t PamLevel2::UpdateRunInfo(TChain * Line 2086  Bool_t PamLevel2::UpdateRunInfo(TChain *
2086    // ----------------------------------------------------    // ----------------------------------------------------
2087    if(SELLI==1){          if(SELLI==1){      
2088        sel_tree->GetEntry(iev);        sel_tree->GetEntry(iev);
2089    //      cout << irun << " "<< irunentry << endl;
2090        if(irun != oldrun){        if(irun != oldrun){
2091            run->GetEntry(irun);            run->GetEntry(irun);
2092              cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2093              prevshift = 0;
2094            return true;            return true;
2095        }        }
2096        return false;        return false;
# Line 1572  Bool_t PamLevel2::UpdateRunInfo(TChain * Line 2099  Bool_t PamLevel2::UpdateRunInfo(TChain *
2099    return false;    return false;
2100    //    //
2101  };  };
2102    
2103    Bool_t PamLevel2::UpdateRunInfo(Long64_t iev){
2104    
2105        if(!run_tree){
2106            cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded"<<endl;
2107            return false;
2108        }
2109        if ( run_tree->GetEntries() <= 0 ) {
2110            cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty"<<endl;
2111            return(false);
2112        }
2113      
2114        Int_t oldrun = irun; // store current run index
2115    
2116        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2117        // if it is a full file (not preselected)
2118        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2119        if(SELLI==0){
2120    
2121            // ---------------------------------------------------------------
2122            // increment dead and live-time counters
2123            // ---------------------------------------------------------------
2124            if( GetTrigLevel2() ){
2125                totdltime[0]+=GetTrigLevel2()->dltime[0];
2126                totdltime[1]+=GetTrigLevel2()->dltime[1];
2127            }
2128            totdltime[2]++;
2129    
2130    //      cout << setw(10)<<totdltime[0]<<setw(10)<<totdltime[1]<<setw(10)<<totdltime[2]<<endl;
2131    
2132            // ---------------------------------------------------------------
2133            // retrieve OBT and absolute time of the event
2134            // ---------------------------------------------------------------
2135            ULong64_t abstime = 0;
2136            ULong64_t obt     = 0;
2137            if( GetOrbitalInfo() ){
2138                abstime = GetOrbitalInfo()->absTime;
2139                obt     = GetOrbitalInfo()->OBT;
2140            }
2141            // ---------------------------------------------------------------
2142            // the absolute time is necessary to relate the event with the run
2143            // ---------------------------------------------------------------
2144            if( !GetOrbitalInfo() && !ISGP ){
2145                cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
2146                return(false);
2147            }
2148          
2149            // -----------------------------------------------------------------------
2150            // the first time the routine is called, set run search from the beginning
2151            // -----------------------------------------------------------------------
2152            if ( irun < 0 ){
2153                irun = 0LL;
2154                irunentry = 0;
2155                prevshift = 0;
2156                run_tree->GetEntry(irun);
2157    
2158                if( ISGP && run_tree->GetEntries()!=1 ){
2159                    cout << "** WARNING ** simulated files are assumed to have 1 single run, not "<< run_tree->GetEntries() << endl;
2160                    cout << "** WARNING ** run will not be updated"<<endl;
2161                }
2162            };      
2163    
2164            // -----------------------------------------------------------------------
2165            // if it is simulation, assign abstime by hand (temporaneo!!!)
2166            // -----------------------------------------------------------------------
2167            if(ISGP){
2168                abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO
2169                obt     = GetRunInfo()->RUNHEADER_OBT; // BARBATRUCCO
2170            }
2171            //
2172            bool fromfirst = true; // first loop over runs
2173    
2174    //      Bool_t hasfrag = false;
2175    //      if( GetRunInfo()->ID_RUN_FRAG!=0 && GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID)hasfrag=true;
2176    //      ULong64_t fragid = GetRunInfo()->ID_RUN_FRAG;
2177    
2178            // ------------------------------------------------------
2179            // loop over runs to find the one that contains the event
2180            // ------------------------------------------------------
2181            while (
2182                (
2183                    (
2184                        !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)
2185                          abstime <= GetRunInfo()->RUNTRAILER_TIME) &&
2186                        !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)
2187                          obt <= GetRunInfo()->RUNTRAILER_OBT)
2188                        )
2189                    || GetRunInfo()->NEVENTS==0
2190    //              || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift) // ERRORE!!! fa saltare i run con 1 evento
2191                    || !(irunentry <= GetRunInfo()->NEVENTS-1-prevshift)
2192                    )
2193                    && irun < run_tree->GetEntries() ){
2194    
2195                // - - - - - - - - - - - - -
2196                // irunentry = position of current entry, relative to the run
2197                // prevshift = shift needed to synchronize l0 and l2 data (nested events)
2198                // - - - - - - - - - - - - -
2199    
2200                // -----------------------------------------
2201                // store dead and live-time of previous run
2202                // -----------------------------------------
2203                if(fromfirst){
2204                    if(oldrun==irun){
2205                        /// decrement counters
2206                        if( GetTrigLevel2()){
2207                            totdltime[0]-=GetTrigLevel2()->dltime[0];//live-time
2208                            totdltime[1]-=GetTrigLevel2()->dltime[1];//dead-time
2209                        }
2210                        totdltime[2]--;                              //event counter
2211                        cout << endl;
2212                        cout << "n.events     : "<<totdltime[2]<<endl;
2213                        cout << "RUN LIVE-TIME: "<<totdltime[0]*0.16<<" ms"<<endl;
2214                        cout << "RUN DEAD-TIME: "<<totdltime[1]*0.01<<" ms"<<endl;      
2215                    }else{
2216                        totdltime[0]=0;//live-time
2217                        totdltime[1]=0;//dead-time
2218                        totdltime[2]=0;                             //event counter
2219                        cout << " *** JUMP RUN *** irun "<<irun<<endl;
2220                    }
2221                    /// add an entry
2222                    if(run_tree_clone)
2223                        if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
2224                            run_tree_clone->GetBranch("DeadLiveTime")->Fill();
2225                    /// reset counters
2226                    if( GetTrigLevel2() ){
2227                        totdltime[0]=GetTrigLevel2()->dltime[0];//live-time
2228                        totdltime[1]=0;                         //dead-time
2229                    }
2230                    totdltime[2]=1;                             //event counter
2231                }
2232    
2233    
2234                irun++;
2235                // ------------------------------------
2236                // if the end of run tree is reached...
2237                // ------------------------------------
2238                if( irun == run_tree->GetEntries() ){
2239                    if(!fromfirst){
2240                        // -----------------------------------------------------
2241                        // if it happened already once and the run was not found
2242                        // ---> exit with error
2243                        // -----------------------------------------------------
2244                        cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- event entry #"<<iev<<" does not belong to any run (should not happen)" <<endl;
2245                        return false;
2246                    }
2247                    // -----------------------------------------
2248                    // ...otherwise repeat search from beginning
2249                    // -----------------------------------------
2250                    cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- reached end of run tree. searchin again from beginning " <<endl;
2251                    fromfirst = false;
2252                    irun = 0LL;
2253                    runfirstentry = 0LL;
2254                }
2255                // -------------------------------------------------------------------
2256                // save the index of the first entry of the run, relative to pam_tree,
2257                // and read a new run
2258                // -------------------------------------------------------------------
2259                if(irun>0)runfirstentry += (GetRunInfo()->NEVENTS)-prevshift;
2260                irunentry = 0;
2261                prevshift = 0;          
2262                run_tree->GetEntry(irun);      
2263                if(GetRunInfo()->RUNHEADER_OBT>GetRunInfo()->RUNTRAILER_OBT ){
2264                    cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun "<<irun<<"  has RUNHEADER_OBT>=RUNTRAILER_OBT " <<endl;
2265                    cout << "                                                            (NB!! in this case some events are assigned to a wrong run)"<<endl;
2266                }
2267    //          if(hasfrag &&  fragid != GetRunInfo()->ID){
2268    //              cout << "... where is the next fragment ??"<<endl;
2269    //          }
2270            };
2271    
2272    
2273            // --------------------------------------
2274            // if there was no need to update the run
2275            // ---> exit with FALSE
2276            // --------------------------------------
2277            if ( irun == oldrun ) return(false);
2278    
2279            // --------------------------------------
2280            // ... otherwise
2281            // --------------------------------------
2282    
2283    
2284    
2285    //      // -----------------------------------------
2286    //      // store dead and live-time of previous run
2287    //      // -----------------------------------------
2288    //      // --------------------------------------------------------------
2289    //      // if the run is empty, fill the dead-live time branch with zeros
2290    //      // --------------------------------------------------------------
2291    //      /// if some runs have been jumped, fill with zeros
2292    //      if(irun-oldrun>1){
2293    //          ULong64_t  temp[3];
2294    //          temp[0]=totdltime[0];
2295    //          temp[1]=totdltime[1];
2296    //          temp[2]=totdltime[2];
2297    //          for(int i=oldrun+1; i<irun; irun++){
2298    //              totdltime[0]=0;
2299    //              totdltime[1]=0;
2300    //              totdltime[2]=0;
2301    //              cout << " ** JUMPED RUN **"<<endl;
2302    //              if(run_tree_clone)
2303    //                  if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
2304    //                      run_tree_clone->GetBranch("DeadLiveTime")->Fill();
2305    //          }
2306    //          totdltime[0]=temp[0];
2307    //          totdltime[1]=temp[1];
2308    //          totdltime[2]=temp[2];
2309    //      }
2310    //      /// decrement counters
2311    //      if( GetTrigLevel2() ){
2312    //          totdltime[0]-=GetTrigLevel2()->dltime[0];//live-time
2313    //          totdltime[1]-=GetTrigLevel2()->dltime[1];//dead-time
2314    //      }
2315    //      totdltime[2]--;                              //event counter
2316    //      /// add an entry
2317    //      if(irun>0 ){
2318    //          cout << endl;
2319    //          cout << "n.events     : "<<totdltime[2]<<endl;
2320    //          cout << "RUN LIVE-TIME: "<<totdltime[0]*0.16<<" ms"<<endl;
2321    //          cout << "RUN DEAD-TIME: "<<totdltime[1]*0.01<<" ms"<<endl;      
2322    //          if(run_tree_clone)
2323    //              if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
2324    //                  run_tree_clone->GetBranch("DeadLiveTime")->Fill();
2325    //      }
2326    //      /// reset counters
2327    //      if( GetTrigLevel2() ){
2328    //          totdltime[0]=GetTrigLevel2()->dltime[0];//live-time
2329    //          totdltime[1]=0;                         //dead-time
2330    //      }
2331    //      totdltime[2]=1;                             //event counter
2332            
2333    
2334            // --------------------------------------
2335            // ---> exit with TRUE
2336            // --------------------------------------
2337            cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2338            // ----------------------------------------------------
2339            // then check if the run has a fragment
2340            // in this case we have to switch to the next fragment
2341            // when the end of the first fragment is reached
2342            // ----------------------------------------------------
2343            if(
2344                GetRunInfo()->ID_RUN_FRAG != 0 &&
2345    //          GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID &&
2346                true ){
2347                cout << "* fragment *"<<endl;              
2348            }
2349    
2350            return(true);    
2351        };
2352        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2353        // if it is a preselected file (there is SelectionList)
2354        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2355        if(SELLI==1){      
2356            sel_tree->GetEntry(iev);
2357            if(irun != oldrun){
2358                run_tree->GetEntry(irun);
2359                cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2360                prevshift = 0;
2361                return true;
2362            }
2363            return false;
2364        }
2365    
2366        return false;
2367        //
2368    };
2369  /**  /**
2370   * 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)
2371   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
2372   * @return true if a new run has been read, false if it is still the same run   * @return true if a new run has been read, false if it is still the same run
2373   */   */
2374  Bool_t PamLevel2::UpdateRunInfo(TTree *run, ULong64_t iev){  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev){
2375    return(UpdateRunInfo((TChain*)run,iev));    return(UpdateRunInfo((TChain*)run,iev));
2376  };  };
2377    
# Line 2201  void PamLevel2::CreateCloneTrees0( TChai Line 2995  void PamLevel2::CreateCloneTrees0( TChai
2995   */   */
2996  void PamLevel2::CreateCloneTrees(TFile *ofile){  void PamLevel2::CreateCloneTrees(TFile *ofile){
2997    
2998    
2999    //  if the pointer is null, create a default file
3000    
3001        if(!ofile){
3002            cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root "<<endl;
3003            ofile = new TFile("clone-tree.root","recreate");
3004        }
3005    
3006      ofile->cd();      ofile->cd();
3007    
3008      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
# Line 2212  void PamLevel2::CreateCloneTrees(TFile * Line 3014  void PamLevel2::CreateCloneTrees(TFile *
3014      cout << "Run          : branch RunInfo"<<endl;      cout << "Run          : branch RunInfo"<<endl;
3015      run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));      run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));
3016      cout << "Run          : branch SoftInfo"<<endl;      cout << "Run          : branch SoftInfo"<<endl;
3017                    // ------------------
3018        // replicate run tree
3019        // ------------------
3020        cout << "----------------------------------------------------"<<endl;
3021        cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
3022        for (Int_t i=0; i<run_tree->GetEntries(); i++){
3023            run_tree->GetEntry(i);
3024            cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
3025            run_tree_clone->Fill();
3026        }
3027        cout << "----------------------------------------------------"<<endl;
3028    
3029        // ------------------------------------
3030        // add branch with dead and live times
3031        // ------------------------------------
3032        run_tree_clone->Branch("DeadLiveTime",totdltime,"dltime[3]/l");
3033        cout << "Run          : branch DeadLiveTime"<<endl;
3034    
3035    
3036      sel_tree_clone = new TTree("SelectionList","List of selected events ");      sel_tree_clone = new TTree("SelectionList","List of selected events ");
3037      sel_tree_clone->Branch("RunEntry",&irun,"runentry/I");      sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
3038      sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/I");      sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/L");
3039            
3040    
3041      Int_t i=0;      Int_t i=0;
# Line 2227  void PamLevel2::CreateCloneTrees(TFile * Line 3045  void PamLevel2::CreateCloneTrees(TFile *
3045              pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));              pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));
3046              pam_tree_clone[i]->BranchRef();              pam_tree_clone[i]->BranchRef();
3047              cout << "Tracker      : branch TrkLevel1"<<endl;              cout << "Tracker      : branch TrkLevel1"<<endl;
3048              cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;  //          cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;
3049          };          };
3050          if(TRK2) {          if(TRK2) {
3051              pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));              pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));
# Line 2340  TTree* PamLevel2::GetCloneTree(TString n Line 3158  TTree* PamLevel2::GetCloneTree(TString n
3158  void PamLevel2::WriteCloneTrees(){  void PamLevel2::WriteCloneTrees(){
3159      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
3160      cout << "Write clones of PAMELA trees "<<endl;      cout << "Write clones of PAMELA trees "<<endl;
3161      cout << run_tree_clone->GetName()<<endl;          cout << run_tree_clone->GetName()<<endl;  
3162        if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
3163            run_tree_clone->GetBranch("DeadLiveTime")->Fill();
3164      run_tree_clone->Write();      run_tree_clone->Write();
3165      cout << sel_tree_clone->GetName()<<endl;          cout << sel_tree_clone->GetName()<<endl;    
3166      sel_tree_clone->Write();      sel_tree_clone->Write();
# Line 2357  void PamLevel2::WriteCloneTrees(){ Line 3177  void PamLevel2::WriteCloneTrees(){
3177  /**  /**
3178   * 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.
3179   */   */
3180  Int_t PamLevel2::GetEntry(Int_t iee){  //Int_t PamLevel2::GetEntry(Int_t iee){
3181    Int_t PamLevel2::GetEntry(Long64_t iee){
3182            
3183    //     cout << "-------------------------------------"<<endl;
3184    //     cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;
3185    
3186      if(!pam_tree){      if(!pam_tree){
3187          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;
3188          return 0;          return 0;
3189      }      }
3190    
3191      Int_t ii=0;  
3192        //
3193        // This is a sort of bug: if you don't have the run tree you don't want to exit here you want to have loaded the event anyway...
3194        //
3195        //    if(!run_tree ){
3196        //  cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loeaded"<<endl;
3197        //  return 0;
3198        //    }
3199    
3200        Long64_t ii=0;
3201      //-------------------------------      //-------------------------------
3202      ii = iee;      ii = iee;
3203      if( !pam_tree->GetEntry(ii) ) return 0;      if( !pam_tree->GetEntry(ii) ){      
3204      //-------------------------------          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree"<<endl;
   
     if(!run_tree ){  
         cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded"<<endl;  
3205          return 0;          return 0;
3206      }      }
3207        //
3208        // ... that's way I put it here. Notice that nothing change in the code (is backward compatible) since in any case you return with 0.
3209        // in theory one would like to return 1 if run is not loaded but now I don't have the will to add that 2 lines of code and it is not
3210        // a problem if you don't check the return code of getentry.
3211        //
3212        if(!run_tree ){
3213            if ( TRK0 || CAL0 || TOF0 || RUN ) { //forse cosi` va bene per tornare 1?
3214                cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded"<<endl;
3215                return 0;
3216            }  else {
3217                return 1; //cosi` se non c'e` run esce qua...
3218            }
3219        }
3220    
3221        //-------------------------------
3222      ii = iee;      ii = iee;
3223      Bool_t UPDATED = UpdateRunInfo(run_tree,ii);  //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);
3224    //    Bool_t UPDATED = UpdateRunInfo(ii);
3225        UpdateRunInfo(ii);
3226      if(SELLI==0)irunentry = iee-runfirstentry;      if(SELLI==0)irunentry = iee-runfirstentry;
3227      if(UPDATED && run_tree_clone)run_tree_clone->Fill();  //    if(UPDATED && run_tree_clone)run_tree_clone->Fill();
3228      
3229  //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;  //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;
3230    
3231      if( TRK0 || CAL0 || TOF0 )GetYodaEntry( );  //     cout << "irunentry     "<<irunentry << endl;
3232    //     cout << "runfirstentry "<<runfirstentry << endl;
3233    //     cout << "nevents       "<<GetRunInfo()->NEVENTS<< endl;
3234    
3235    //     if( TRK0 || CAL0 || TOF0 ){
3236    //      if( !GetYodaEntry( ) ){
3237    //          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading level0 tree"<<endl;
3238    //          return 0;
3239    //      }
3240    //     }
3241    
3242    
3243      return 1;      return 1;
3244    
3245  }  }
3246    
3247    TrkLevel0    *PamLevel2::GetTrkLevel0(){
3248        if( !TRK0 )return NULL;
3249        if( !GetYodaEntry( ) ){
3250            cout << " Int_t PamLevel2::GetTrkLevel0() -- ERROR -- error reading level0 tree"<<endl;
3251            return 0;
3252        }
3253        return trk0_obj;
3254    };
3255    CaloLevel0    *PamLevel2::GetCaloLevel0(){
3256        if( !CAL0 )return NULL;
3257        if( !GetYodaEntry( ) ){
3258            cout << " Int_t PamLevel2::GetCaloLevel0() -- ERROR -- error reading level0 tree"<<endl;
3259            return 0;
3260        }
3261        return calo0_obj;
3262    };
3263    
3264    
3265  /**  /**
3266   * Method to retrieve the level0 tree (YODA tree) that contains the current event.   * Method to retrieve the level0 tree (YODA tree) that contains the current event.
3267   * Given the run ID (...), if needed it query the DB and load the proper file.   * Given the run ID (...), if needed it query the DB and load the proper file.
# Line 2402  TTree* PamLevel2::GetYodaTree( ){ Line 3276  TTree* PamLevel2::GetYodaTree( ){
3276      // check if iroot has changed      // check if iroot has changed
3277      //===================================      //===================================
3278      if( irun<0 ){      if( irun<0 ){
3279          cout << "PamLevel2::GetYodaTree() -- ERROR "<<endl;          cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = "<<irun<<endl;
3280          cout << "In order to use this method you have to load the RunInfo tree "<<endl;  //      cout << "In order to use this method you have to first load the RunInfo tree "<<endl;
         cout << "with the method TChain* PamLevel2::GetRunTree(TString, TString)"<<endl;  
 //      cout << " - read the event with PamLevel2::GetEntry(Int_t)"<<endl;  
3281          return NULL;          return NULL;
3282      }          }    
3283      Int_t irootnew = run_obj->ID_ROOT_L0;      Int_t irootnew = run_obj->ID_ROOT_L0;
3284  //     cout << "iroot    "<<iroot<<endl;  //      cout << "iroot    "<<iroot<<endl;
3285  //     cout << "irootnew "<<irootnew<<endl;  //      cout << "irootnew "<<irootnew<<endl;
3286    
3287      //===================================      //===================================
3288      // load the level0 file      // load the level0 file
# Line 2424  TTree* PamLevel2::GetYodaTree( ){ Line 3296  TTree* PamLevel2::GetYodaTree( ){
3296          //===================================          //===================================
3297          if(!dbc || (dbc && !dbc->IsConnected())){          if(!dbc || (dbc && !dbc->IsConnected())){
3298              cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;              cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3299              cout<<"Connecting to DB"<<endl;              cout<<"Re-connecting to DB"<<endl;
3300              cout<<"HOST "<<host<<endl;              cout<<"HOST "<<host<<endl;
3301              cout<<"USER "<<user<<endl;              cout<<"USER "<<user<<endl;
3302              cout<<"PSW  "<<psw<<endl;              cout<<"PSW  "<<psw<<endl;
# Line 2457  TTree* PamLevel2::GetYodaTree( ){ Line 3329  TTree* PamLevel2::GetYodaTree( ){
3329          l0_tree = (TTree*)l0_file->Get("Physics");          l0_tree = (TTree*)l0_file->Get("Physics");
3330          if(!h0_obj)h0_obj = new EventHeader();          if(!h0_obj)h0_obj = new EventHeader();
3331          l0_tree->SetBranchAddress("Header" ,&h0_obj);          l0_tree->SetBranchAddress("Header" ,&h0_obj);
3332            prevshift = 0;
3333          //---------------------------------------------------          //---------------------------------------------------
3334          // TRACKER:          // TRACKER:
3335          if(TRK0){          if(TRK0){
# Line 2465  TTree* PamLevel2::GetYodaTree( ){ Line 3338  TTree* PamLevel2::GetYodaTree( ){
3338                  trk0_obj->Set();                  trk0_obj->Set();
3339              };              };
3340              l0_tree->SetBranchAddress("Tracker" ,trk0_obj->GetPointerToTrackerEvent());              l0_tree->SetBranchAddress("Tracker" ,trk0_obj->GetPointerToTrackerEvent());
             TrkParams::SetCalib(run_obj,dbc);  
3341          }          }
3342          //---------------------------------------------------          //--------------------------------------------------
3343          // CALORIMETER:          // CALORIMETER:
3344          if(CAL0){          if(CAL0){
3345              cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;              if(!calo0_obj){
3346                    calo0_obj = new CaloLevel0();
3347                    calo0_obj->Set();
3348                };
3349                l0_tree->SetBranchAddress("Calorimeter" ,calo0_obj->GetPointerToCalorimeterEvent());
3350                //      cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;
3351          }          }
3352          //---------------------------------------------------          //---------------------------------------------------
3353          // TOF:          // TOF:
# Line 2478  TTree* PamLevel2::GetYodaTree( ){ Line 3355  TTree* PamLevel2::GetYodaTree( ){
3355              cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented "<<endl;              cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented "<<endl;
3356          }          }
3357    
3358            dbc->Close();
3359    
3360    
3361      };      };
3362    
3363    //     if(!dbc || (dbc && !dbc->IsConnected())){
3364    //      cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;
3365    //     }
3366    
3367        if ( TRK0 ){
3368            TrkParams::Load(6);
3369            if( !TrkParams::IsLoaded(6) ){
3370                cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- VK-mask not loaded"<<endl;
3371            };
3372            TrkParams::SetCalib(run_obj,dbc);
3373            TrkParams::LoadCalib( );
3374            if( !TrkParams::CalibIsLoaded() ){
3375                cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded"<<endl;
3376            };
3377        }
3378    
3379    
3380  //    cout << l0_tree << endl;  //    cout << l0_tree << endl;
       
3381      return l0_tree;      return l0_tree;
3382    
3383  }  }
# Line 2494  Int_t PamLevel2::GetYodaEntry(){ Line 3390  Int_t PamLevel2::GetYodaEntry(){
3390  //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;  //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;
3391      if(!GetYodaTree())return 0;      if(!GetYodaTree())return 0;
3392            
3393        // patch
3394        if( irunentry < 0){
3395    //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
3396            irunentry=0LL;
3397        }
3398      //  ---------------------------------      //  ---------------------------------
3399      //  if file is NOT a preselected file      //  if file is NOT a preselected file
3400      //  ---------------------------------      //  ---------------------------------
3401      Int_t quellagiusta = irunentry + run_obj->EV_FROM;      Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM);
3402    //     cout << " irunentry "<<irunentry<<endl;
3403    //     cout << " EV_FROM "<<run_obj->EV_FROM<<endl;
3404    //     cout << " quellagiusta = irunentry + EV_FROM "<< quellagiusta << endl;
3405    
3406    //     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
3407    //     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
3408    //     cout << " time "<< abstime << endl;
3409    //     cout << " trk_calib_used "<<run_obj->TRK_CALIB_USED<< endl;
3410        
3411        ULong64_t obt     = 0;
3412        ULong64_t pktn    = 0;
3413        if( GetOrbitalInfo() ){
3414            obt     = GetOrbitalInfo()->OBT;
3415            pktn    = GetOrbitalInfo()->pkt_num;
3416        }
3417    
3418      if( !GetOrbitalInfo() ){      if( !GetOrbitalInfo() && !ISGP){
3419          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo "<<endl;          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo "<<endl;
3420          return 0;          return 0;
3421      }      }
3422        if( obt==0 && pktn==0 && !ISGP ){
3423            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? "<<endl;
3424            return 0;
3425        }
3426    
3427      // ---------------------------------------------------------------------      // ---------------------------------------------------------------------
3428      // ATTENTION!!!      // ATTENTION!!!
# Line 2517  Int_t PamLevel2::GetYodaEntry(){ Line 3437  Int_t PamLevel2::GetYodaEntry(){
3437      // ---------------------------------------------------------------------      // ---------------------------------------------------------------------
3438      Int_t answer = 0;      Int_t answer = 0;
3439      Int_t shift =0;      Int_t shift =0;
3440        //    printf(" siamo qui %i %i \n",shift,prevshift);
3441        //    Int_t maxshift = 50; // EMILIANO
3442      do{      do{
3443  //      if(shift>0){          if(shift>0){    
3444  //          cout << " level0 <--> level2 mismatch ( irun "<<irun<<" irunentry "<<irunentry<<" shift "<<shift<<" )"<<endl;              cout << " PKTNUM  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
3445  //      }              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;
3446          answer = GetYodaTree()->GetEntry(quellagiusta+shift);              cout << "         L2 <--> L0 mismatch ( irun "<<irun<<" irunentry "<<irunentry<<" shift "<<shift<<" prevshift "<<prevshift<<" )"<<endl;
3447            }
3448            answer = l0_tree->GetEntry(quellagiusta+(Long64_t)shift+(Long64_t)prevshift);
3449          shift++;          shift++;
3450          if( !GetEventHeader() ){          if( !GetEventHeader() ){
3451              cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader "<<endl;              cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader "<<endl;
3452              return 0;              return 0;
3453          }          }
 //      if( (quellagiusta+shift) == GetYodaTree()->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;  
     }while( ( (UInt_t)(GetOrbitalInfo()->OBT) != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || GetOrbitalInfo()->pkt_num != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta+shift) < (UInt_t)(GetYodaTree()->GetEntries()) );  
3454    
3455            if(ISGP){
3456                obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
3457                pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
3458            }
3459    
3460    //      cout << "PKTNUM "<<shift<<" ==  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
3461    //      cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;
3462    //      if( (quellagiusta+shift) == l0_tree->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;
3463    //      cout << " obt "<< obt << endl;
3464    //      cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;
3465    //      cout << " pktn "<< pktn << endl;
3466    //      cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
3467    //      printf(" IDRUN %u \n",GetRunInfo()->ID);
3468    //
3469            if ( prevshift != 0 && (quellagiusta+(Long64_t)shift) == GetYodaTree()->GetEntries() ){
3470              prevshift = 0;
3471              shift = -1;
3472            };
3473    
3474        }while( ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) ||
3475                  pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())       )
3476                && (quellagiusta+(Long64_t)shift) < GetYodaTree()->GetEntries() && shift < maxshift);
3477    
3478        if ( (quellagiusta+(Long64_t)shift+(Long64_t)prevshift) > GetYodaTree()->GetEntries() || shift == maxshift ) {
3479            cout << " Big trouble here, no such event in Level0 data! " <<endl;
3480            return 0;
3481        }
3482  //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;  //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
3483  //    return GetYodaTree()->GetEntry(quellagiusta);  //    return GetYodaTree()->GetEntry(quellagiusta);
3484        if ( shift > 1 ) prevshift += (shift-1);
3485            
3486      return answer;      return answer;
3487            
3488  }  }
3489    /**
3490     * \Brief Set DB connection
3491     */
3492    Bool_t PamLevel2::SetDBConnection(){
3493    
3494        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3495        cout<<"Connecting to DB"<<endl;
3496        cout<<"HOST "<<host<<endl;
3497        cout<<"USER "<<user<<endl;
3498        cout<<"PSW  "<<psw<<endl;
3499        dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
3500        if( !dbc )return false;
3501        if( !dbc->IsConnected() )return false;    
3502        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3503        return true;
3504    
3505    }
3506    
3507    /**
3508     * \Brief Add a friend to the pamela chain.
3509     * @param cname name of the chain to be added
3510     */
3511    
3512    TChain* PamLevel2::AddFriend(TString cname){
3513    
3514        if(!GetPamTree()){
3515            cout << " TChain* PamLevel2::AddFriend(TString cname) --- a pamela tree must be created first"<<endl;
3516            return NULL;
3517        }
3518    
3519    
3520        TChain *c = new TChain( cname.Data() );
3521    
3522        TIter next( GetPamTree()->GetListOfFiles() );
3523        Int_t nf = 0;
3524        TChainElement* element = 0;    
3525        while ((element = (TChainElement*) next())) {      
3526            c->Add(element->GetTitle());
3527            nf++;
3528        }
3529    
3530        GetPamTree()->AddFriend(cname.Data());
3531    
3532        cout << "external chain created and added to pamela friends :"<<cname<<endl;
3533        cout << "n.files "<<nf<<endl;
3534        
3535    
3536        return c;    
3537    
3538    }

Legend:
Removed from v.1.25  
changed lines
  Added in v.1.51

  ViewVC Help
Powered by ViewVC 1.1.23