/[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.24 by pam-fi, Thu Mar 15 12:18:51 2007 UTC revision 1.40 by pam-fi, Fri May 25 13:38:51 2007 UTC
# Line 85  void PamTrack::Delete(){ Line 85  void PamTrack::Delete(){
85  //  //
86  //--------------------------------------  //--------------------------------------
87  /**  /**
88   * Constructor   * Default Constructor
89   */   */
90  PamLevel2::PamLevel2(){  PamLevel2::PamLevel2(){
91      Initialize();
92    };
93    
94    /**
95     * Constructor with given dir, list and detectors
96     */
97    PamLevel2::PamLevel2(TString ddir,TString list,TString detlist){
98      Initialize();
99      GetPamTree(GetListOfLevel2Files(ddir,list),detlist);
100      GetRunTree(GetListOfLevel2Files(ddir,list));
101    };
102    
103    /**
104     * Constructor with given dir and list
105     */
106    PamLevel2::PamLevel2(TString ddir,TString list){
107      Initialize();
108      GetPamTree(GetListOfLevel2Files(ddir,list),"");
109      GetRunTree(GetListOfLevel2Files(ddir,list));
110    };
111    
112    
113    void PamLevel2::Initialize(){
114                    
115  //     trk2_obj  = TrkLevel2::GetTrkLevel2();  //     trk2_obj  = TrkLevel2::GetTrkLevel2();
116  //     trk1_obj  = TrkLevel1::GetTrkLevel1();  //     trk1_obj  = TrkLevel1::GetTrkLevel1();
# Line 103  PamLevel2::PamLevel2(){ Line 126  PamLevel2::PamLevel2(){
126            
127      h0_obj    = 0;      h0_obj    = 0;
128      trk0_obj  = 0;      trk0_obj  = 0;
129        calo0_obj  = 0;
130    
131      trk2_obj  = 0;      trk2_obj  = 0;
132      trk1_obj  = 0;      trk1_obj  = 0;
# Line 118  PamLevel2::PamLevel2(){ Line 142  PamLevel2::PamLevel2(){
142    
143      run_obj   = 0;//new GL_RUN();      run_obj   = 0;//new GL_RUN();
144      soft_obj   = 0;// Emiliano      soft_obj   = 0;// Emiliano
145      irun = -1;      irun = -1LL;
146      runfirstentry = 0ULL;      runfirstentry = 0LL;
147      runlastentry = 0ULL;      runlastentry = 0LL;
148    
149      l0_file = NULL;      l0_file = NULL;
150      l0_tree = NULL;      l0_tree = NULL;
151      iroot   = -1;      iroot   = -1;
152      dbc     = 0;      dbc     = 0;
153    
154        prevshift = 0;
155            
     irun = -1;  
156      run_tree = NULL;      run_tree = NULL;
157      run_tree_clone = NULL;      run_tree_clone = NULL;
158      sel_tree = NULL;      sel_tree = NULL;
159      sel_tree_clone = NULL;      sel_tree_clone = NULL;
160            
161      irunentry = -1;      irunentry = -1LL;
162      pam_tree = NULL;      pam_tree = NULL;
163      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;
164    
# Line 175  PamLevel2::PamLevel2(){ Line 200  PamLevel2::PamLevel2(){
200    
201      tsorted=0;      tsorted=0;
202      timage=0;      timage=0;
203        
204        howtosort = "+CAL+TOF";
205        //howtosort = "+TOF";
206        sortthr = 100.;
207    
208  };  };
209  /**  /**
# Line 188  void PamLevel2::Delete(){ Line 217  void PamLevel2::Delete(){
217  //    cout << "void PamLevel2::Clear()"<<endl;  //    cout << "void PamLevel2::Clear()"<<endl;
218      if(h0_obj)   delete h0_obj;      if(h0_obj)   delete h0_obj;
219      if(trk0_obj) delete trk0_obj;            if(trk0_obj) delete trk0_obj;      
220        if(calo0_obj) delete calo0_obj;    
221      if(trk1_obj) delete trk1_obj;            if(trk1_obj) delete trk1_obj;      
222      if(trk2_obj) delete trk2_obj;      if(trk2_obj) delete trk2_obj;
223      if(trkh_obj) delete trkh_obj;      if(trkh_obj) delete trkh_obj;
# Line 215  void PamLevel2::Delete(){ Line 245  void PamLevel2::Delete(){
245      }      }
246            
247      if(l0_file)l0_file->Close();      if(l0_file)l0_file->Close();
248      if(pam_tree)pam_tree->Delete();;      //    if(pam_tree)pam_tree->Delete();;
249    
250      if ( pam_tree ){
251        //
252        // 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...
253        //
254        TList *temp = pam_tree->GetListOfFriends();
255        TList *contents  = new TList; // create chain friend list
256        contents->SetOwner();
257        TIter next(temp);
258        TChain *questo = 0;
259        while ( (questo = (TChain*) next()) ){
260          TString name =  questo->GetName();
261          contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
262        };
263        //
264        // deleting the main chain
265        //
266        pam_tree->Delete();
267        //
268        // deleting the friends...
269        //
270        TIter next2(contents);
271        TChain *questa = 0;
272        while ( questa = (TChain*)next2() ){
273          TString name =  questa->GetName();
274          questa->Delete();      
275          questa=NULL;
276        };
277        //
278      };
279      pam_tree = NULL;
280    
281      if(run_tree)run_tree->Delete();;      if(run_tree)run_tree->Delete();;
282      if(sel_tree)sel_tree->Delete();;      if(sel_tree)sel_tree->Delete();;
283      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 303  void PamLevel2::Clear(){
303  //    if(soft_obj) soft_obj->Clear();  //    if(soft_obj) soft_obj->Clear();
304    
305      if(h0_obj)   h0_obj->Clear();            if(h0_obj)   h0_obj->Clear();      
306      if(trk0_obj) trk0_obj->Clear();      //    if(trk0_obj) trk0_obj->Clear();  
307      if(trk1_obj) trk1_obj->Clear();          if(trk1_obj) trk1_obj->Clear();    
308      if(trk2_obj) trk2_obj->Clear();      if(trk2_obj) trk2_obj->Clear();
309      if(trkh_obj) trkh_obj->Clear();      if(trkh_obj) trkh_obj->Clear();
310        if(calo0_obj) calo0_obj->Clear();  
311      if(calo1_obj)calo1_obj->Clear();      if(calo1_obj)calo1_obj->Clear();
312      if(calo2_obj)calo2_obj->Clear();      if(calo2_obj)calo2_obj->Clear();
313      if(tof_obj)  tof_obj->Clear();      if(tof_obj)  tof_obj->Clear();
# Line 263  void PamLevel2::Clear(){ Line 326  void PamLevel2::Clear(){
326      if(timage){      if(timage){
327          timage->Delete();          timage->Delete();
328      }      }
       
329  };  };
330    
331    void PamLevel2::Reset(){
332      //
333      // First of all clear everything
334      //
335      Clear();
336      //
337      // close and reset chains and pointers
338      //    
339      if ( pam_tree ){
340        //
341        // 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...
342        //
343        TList *temp = pam_tree->GetListOfFriends();
344        TList *contents  = new TList; // create chain friend list
345        contents->SetOwner();
346        TIter next(temp);
347        TChain *questo = 0;
348        while ( (questo = (TChain*) next()) ){
349          TString name =  questo->GetName();
350          contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
351        };
352        //
353        // deleting the main chain
354        //
355        pam_tree->Delete();
356        //
357        // deleting the friends...
358        //
359        TIter next2(contents);
360        TChain *questa = 0;
361        while ( questa = (TChain*)next2() ){
362          TString name =  questa->GetName();
363          questa->Delete();      
364          questa=NULL;
365        };
366        //
367      };
368      pam_tree = NULL;
369      //
370      if(run_tree)run_tree->Delete();;
371      run_tree = NULL;
372      if(sel_tree)sel_tree->Delete();;
373      sel_tree = NULL;
374      //
375      // Close file
376      //
377      if(l0_file)l0_file->Close("R");
378      l0_file = NULL;
379      //
380      h0_obj    = 0;
381      trk0_obj  = 0;
382      calo0_obj  = 0;
383      //
384      trk2_obj  = 0;
385      trk1_obj  = 0;
386      trkh_obj  = 0;
387      calo1_obj = 0;
388      calo2_obj = 0;
389      tof_obj   = 0;
390      trig_obj  = 0;
391      s4_obj    = 0;
392      nd_obj    = 0;
393      ac_obj    = 0;
394      orb_obj   = 0;
395      //
396      // Reset run pointers
397      //
398      run_obj   = 0;//new GL_RUN();
399      soft_obj   = 0;// Emiliano
400      irun = -1;
401      runfirstentry = 0ULL;
402      runlastentry = 0ULL;    
403      //
404    };
405    
406    Bool_t PamLevel2::IsGood(){
407      Bool_t goodev=true;
408      //  if(trk2_obj && trk2_obj->UnpackError() != 0 ) goodev = false;
409      if(calo2_obj && calo2_obj->good != 1) goodev = false;
410      if(tof_obj && tof_obj->unpackError != 0) goodev = false;  
411      if(trig_obj && trig_obj->unpackError != 0) goodev = false;
412      if(s4_obj && s4_obj->unpackError != 0) goodev = false;  
413      if(nd_obj && nd_obj->unpackError != 0) goodev = false;  
414      if(ac_obj && ac_obj->unpackError != 255) goodev = false;  
415        //  if(orb_obj)  
416      return goodev;
417    };
418    
419  //--------------------------------------  //--------------------------------------
420  //  //
# Line 460  PamTrack* PamLevel2::GetStoredTrack(Int_ Line 609  PamTrack* PamLevel2::GetStoredTrack(Int_
609  }  }
610  //--------------------------------------  //--------------------------------------
611  //  //
612    
613    /**
614     * 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.
615     **/
616    void PamLevel2::SortTracks(TString how){
617      printf(" WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",how.Data());
618      howtosort = how;  
619      SortTracks();
620    };
621    
622  //  //
623  //--------------------------------------  //--------------------------------------
624  /**  /**
# Line 473  PamTrack* PamLevel2::GetStoredTrack(Int_ Line 632  PamTrack* PamLevel2::GetStoredTrack(Int_
632   *   *
633   * 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).
634   */   */
635  void PamLevel2::SortTracks(TString how){  void PamLevel2::SortTracks(){
636      TString how = howtosort;
637    
638  //    cout <<" PamLevel2::SortTracks(TString how) "<<endl;    //    cout <<" PamLevel2::SortTracks(TString how) "<<endl;
639      if( !trk2_obj ){    if( !trk2_obj ){
640          cout << "void PamLevel2::SortTracks(TString how):  TrkLevel2 not loaded !!!";      cout << "void PamLevel2::SortTracks():  TrkLevel2 not loaded !!!";
641          return;      return;
642      };    };
643  //    cout << "call SortTracs() "<<endl;    //    cout << "call SortTracs() "<<endl;
644    //Save current Object count    //Save current Object count
645      Int_t ObjectNumber = TProcessID::GetObjectCount();    Int_t ObjectNumber = TProcessID::GetObjectCount();
646    
647  //    cout << "ObjectNumber  "<<ObjectNumber <<endl;    //    cout << "ObjectNumber  "<<ObjectNumber <<endl;
648    
649  //     if(!sorted_tracks)sorted_tracks = new TRefArray();    //     if(!sorted_tracks)sorted_tracks = new TRefArray();
650  //     sorted_tracks->Clear();    //     sorted_tracks->Clear();
651  //    sorted_tracks.Clear();    //    sorted_tracks.Clear();
652    
653      if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());    if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
654      tsorted->Delete();    tsorted->Delete();
655      TClonesArray &ttsorted = *tsorted;    TClonesArray &ttsorted = *tsorted;
656      if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());    if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
657      timage->Delete();    timage->Delete();
658      TClonesArray &ttimage = *timage;    TClonesArray &ttimage = *timage;
659    
660    
661      // loop over the tracks sorted by the tracker    // loop over the tracks sorted by the tracker
662      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);    Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
663      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);    Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
664      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);    Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
665            
666      if( !CAL2 &&  use_CAL) use_CAL = false;    if( !CAL2 &&  use_CAL) use_CAL = false;
667      if( !TOF &&  use_TOF) use_TOF = false;    if( !TOF &&  use_TOF) use_TOF = false;
668            
669      if( !TRK2 ){    if( !TRK2 ){
670  //      cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;      //  cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
671          return;      return;
672      };    };
673    
674      //   cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;    //   cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;
675            
676      for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){    for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){
677                    
678          TrkTrack   *ts = 0;      TrkTrack   *ts = 0;
679          CaloTrkVar *cs = 0;      CaloTrkVar *cs = 0;
680          ToFTrkVar  *os = 0;      ToFTrkVar  *os = 0;
681                    
682          // get tracker tracks      // get tracker tracks
683          TrkTrack   *tp = trk2_obj->TrkLevel2::GetTrack(i);                    //tracker      TrkTrack   *tp = trk2_obj->TrkLevel2::GetTrack(i);                    //tracker
684          CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());      CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());
685          ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());      ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());
686    
687          TrkTrack   *ti = 0;              //tracker (image)      TrkTrack   *ti = 0;              //tracker (image)
688          CaloTrkVar *ci = 0;      CaloTrkVar *ci = 0;
689          ToFTrkVar  *oi = 0;      ToFTrkVar  *oi = 0;
690  //      cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;      //  cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
691          // if track has an image, check image selection      // if track has an image, check image selection
692          if(tp->HasImage()){      if(tp->HasImage()){
693                            
694              ti = trk2_obj->TrkLevel2::GetTrackImage(i);              //tracker (image)        ti = trk2_obj->TrkLevel2::GetTrackImage(i);              //tracker (image)
695              ci = GetCaloStoredTrack(ti->GetSeqNo());        ci = GetCaloStoredTrack(ti->GetSeqNo());
696              oi = GetToFStoredTrack(ti->GetSeqNo());        oi = GetToFStoredTrack(ti->GetSeqNo());
697                            
698  //          cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;        //            cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
699    
700              //assign starting scores        //assign starting scores
701              Int_t tp_score = 0;  //main track sorted by the tracker        Int_t tp_score = 0;  //main track sorted by the tracker
702              Int_t ti_score = 0;  //image track        Int_t ti_score = 0;  //image track
703                            
704              // ------------------------        // ------------------------
705              // calorimeter check        // calorimeter check
706              // ------------------------        // ------------------------
707              // check the Y spatial residual on the first calorimeter plane        // check the Y spatial residual on the first calorimeter plane
708              // (cut on calorimeter variables from Emiliano)        // (cut on calorimeter variables from Emiliano)
709              if( use_CAL && !calo2_obj ){        if( use_CAL && !calo2_obj ){
710                  cout << "void PamLevel2::SortTracks(TString how): how= "<<how<<" but CaloLevel2 not loaded !!!";          cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but CaloLevel2 not loaded !!!";
711                  return;          return;
712              };        };
713              if(        if(
714                  use_CAL            &&           use_CAL            &&
715                  calo2_obj->npcfit[1] > 15     &&   //no. of fit planes on Y view           calo2_obj->npcfit[1] > 5     &&   //no. of fit planes on Y view
716                  calo2_obj->varcfit[1] < 1000. &&  //fit variance on Y view           calo2_obj->varcfit[1] < 1000. &&  //fit variance on Y view
717                  true){           cp && ci &&
718             true){
719    
720                                    
721                  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;
722                  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;
723                                    
724                  if(resy_p <= resy_i) tp_score++;          if(resy_p <= resy_i) tp_score++;
725                  else                 ti_score++;          else                 ti_score++;
726            
727            //      cout << "CALO "<<tp_score<<ti_score<<endl;
728    
729  //              cout << "CALO "<<tp_score<<ti_score<<endl;        };
730          // ------------------------
731          // TOF check
732          // ------------------------          
733          // check the number of hit pmts along the track
734          // on S12 S21 and S32, where paddles are parallel to Y axis
735          if( use_TOF && !tof_obj ){
736            cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but ToFLevel2 not loaded !!!";
737            return;
738          };
739          if( use_TOF && op && oi ){
740                    
741            //
742            Float_t sen = 0.;
743            for (Int_t ih=0; ih < op->npmtadc; ih++){
744              Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
745              if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (op->dedx).At(ih);
746            };
747            for (Int_t ih=0; ih < oi->npmtadc; ih++){
748              Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
749              if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (oi->dedx).At(ih);
750            };
751            //
752            if (  sen >= sortthr ){
753              //printf(" IS A NUCLEUS! en = %f \n",sen);
754              //
755              // is a nucleus use a different algorithm
756              //
757              Int_t nz = 6; Float_t zin[6];                                          // << define TOF z-coordinates
758              for(Int_t ip=0; ip<nz; ip++)
759                zin[ip] = tof_obj->ToFLevel2::GetZTOF(tof_obj->ToFLevel2::GetToFPlaneID(ip));     // << read ToF plane z-coordinates
760              Trajectory *tr = new Trajectory(nz,zin);
761              //
762              Int_t nphit_p =0;
763              Int_t nphit_i =0;
764              Float_t enhit_p = 0.;
765              Float_t enhit_i = 0.;
766              //
767              for (Int_t ih=0; ih < op->npmtadc; ih++){
768                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
769                if(pl == 1 || pl == 2 || pl == 5){
770                  nphit_p++;
771                  enhit_p += (op->dedx).At(ih);
772                };
773              };
774              //
775              tp->DoTrack2(tr);
776              //
777              if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
778                for (Int_t ih=0; ih < op->npmtadc; ih++){
779                  Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
780                  if(pl == 0){
781                    nphit_p++;
782                    enhit_p += (op->dedx).At(ih);
783                  };
784                };
785              };
786              if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
787                for (Int_t ih=0; ih < op->npmtadc; ih++){
788                  Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
789                  if(pl == 3){
790                    nphit_p++;
791                    enhit_p += (op->dedx).At(ih);
792                  };
793                };
794              };
795              if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
796                for (Int_t ih=0; ih < op->npmtadc; ih++){
797                  Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
798                  if(pl == 4){
799                    nphit_p++;
800                    enhit_p += (op->dedx).At(ih);
801                  };
802                };
803              };
804    
805              for (Int_t ih=0; ih < oi->npmtadc; ih++){
806                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
807                if(pl == 1 || pl == 2 || pl == 5){
808                  nphit_i++;        
809                  enhit_i += (op->dedx).At(ih);
810                };
811              };
812              //
813              ti->DoTrack2(tr);
814              //
815              if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
816                for (Int_t ih=0; ih < oi->npmtadc; ih++){
817                  Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
818                  if(pl == 0){
819                    nphit_i++;
820                    enhit_i += (op->dedx).At(ih);
821                  };
822              };              };
823              // ------------------------            };
824              // TOF check            if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
825              // ------------------------                  for (Int_t ih=0; ih < oi->npmtadc; ih++){
826              // check the number of hit pmts along the track                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
827              // on S12 S21 and S32, where paddles are parallel to Y axis                if(pl == 3){
828              if( use_TOF && !tof_obj ){                  nphit_i++;
829                  cout << "void PamLevel2::SortTracks(TString how): how= "<<how<<" but ToFLevel2 not loaded !!!";                  enhit_i += (op->dedx).At(ih);
830                  return;                };
831              };              };
832              if( use_TOF ){            };
833              if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
834                for (Int_t ih=0; ih < oi->npmtadc; ih++){
835                  Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
836                  if(pl == 4){
837                    nphit_i++;
838                    enhit_i += (op->dedx).At(ih);
839                  };
840                };
841              };            
842    
843                                    
844                  Int_t nphit_p =0;            if(
845                  Int_t nphit_i =0;               use_TOF            &&
846                 (nphit_p+nphit_i) !=0 &&  
847                 true){
848                        
849                //      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);
850                //      printf(" score p %i score i %i \n",tp_score,ti_score);
851                //            if( enhit_p > enhit_i ) tp_score++;
852                //            if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
853                if ( nphit_p > nphit_i ) tp_score++;
854                if ( nphit_p < nphit_i ) ti_score++;
855                if ( nphit_p == nphit_i ){
856                  if ( enhit_p > enhit_i ) tp_score++;
857                  else ti_score++;
858                };
859                //      printf(" dopo score p %i score i %i \n",tp_score,ti_score);
860              };
861              delete tr;
862              //
863            } else {
864              //
865              // NOT a NUCLEUS
866              //
867              //printf(" NOT a NUCLEUS! en = %f \n",sen);
868    
869              Int_t nphit_p =0;
870              Int_t nphit_i =0;
871                                    
872                                    
873  /*                              cout << "track: npmtadc "<< op->npmtadc << endl;            /*                            cout << "track: npmtadc "<< op->npmtadc << endl;
874                                  cout << "track: npmttdc "<< op->npmttdc << endl;              cout << "track: npmttdc "<< op->npmttdc << endl;
875                                  cout << "image: npmtadc "<< oi->npmtadc << endl;              cout << "image: npmtadc "<< oi->npmtadc << endl;
876                                  cout << "image: npmttdc "<< oi->npmttdc << endl;*/              cout << "image: npmttdc "<< oi->npmttdc << endl;*/
877                                    
878                  for (Int_t ih=0; ih < op->npmtadc; ih++){            for (Int_t ih=0; ih < op->npmtadc; ih++){
879                      Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );              Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
880                      if(pl == 1 || pl == 2 || pl == 5)nphit_p++;              if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
881                  };            };
882                                    
883                  for (Int_t ih=0; ih < oi->npmtadc; ih++){            for (Int_t ih=0; ih < oi->npmtadc; ih++){
884                      Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );              Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
885                      if(pl == 1 || pl == 2 || pl == 5)nphit_i++;              if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
886                  };            };
887                                    
888                  if(            if(
889                      use_TOF            &&               use_TOF            &&
890                      (nphit_p+nphit_i) !=0 &&                   (nphit_p+nphit_i) !=0 &&  
891                      true){               true){
892                                            
893                      if( nphit_p >= nphit_i) tp_score++;              if( nphit_p >= nphit_i) tp_score++;
894                      else ti_score++;              else ti_score++;
895                  };            };
896  //              cout << "TOF "<<tp_score<<ti_score<<endl;          };
897              };          //      cout << "TOF "<<tp_score<<ti_score<<endl;
898              if(tp_score == ti_score) use_TRK = true;        };
899              // ------------------------        if(tp_score == ti_score) use_TRK = true;
900              // tracker check        // ------------------------
901              // ------------------------        // tracker check
902              // chi**2 difference is not always large enough to distinguish among        // ------------------------
903              // the real track and its image.        // chi**2 difference is not always large enough to distinguish among
904              // Tracker check will be applied always when calorimeter and tof information is ambiguous.        // the real track and its image.
905              if(use_TRK){        // Tracker check will be applied always when calorimeter and tof information is ambiguous.
906                  if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;        if(use_TRK){
907                  else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;          if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
908  //              cout << "TRK "<<tp_score<<ti_score<<endl;          else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
909              };          //      cout << "TRK "<<tp_score<<ti_score<<endl;
910          };
911                            
912              // ------------------------        // ------------------------
913              // the winner is....        // the winner is....
914              // ------------------------            // ------------------------          
915              if      (tp_score > ti_score) {        if      (tp_score > ti_score) {
916  //              ts = tp;//the track sorted by the tracker!!          //              ts = tp;//the track sorted by the tracker!!
917  //              cs = cp;          //              cs = cp;
918  //              os = op;          //              os = op;
919              }else if (tp_score < ti_score) {          //      cout << "It's primary!" << endl;
920                  ts = ti;//its image!!  
921                  cs = ci;        }else if (tp_score < ti_score) {
922                  os = oi;  
923            //      cout << "It's image!" << endl;
924                  ti = tp;//its image!!  
925                  ci = cp;          ts = ti;//its image!!
926                  oi = op;          cs = ci;
927            os = oi;
928                  tp = ts;//its image!!  
929                  cp = cs;          ti = tp;//its image!!
930                  op = os;          ci = cp;
931            oi = op;
932    
933            tp = ts;//its image!!
934            cp = cs;
935            op = os;
936    
937                                    
938              }else {        }else {
939  //              ts = tp;          //              ts = tp;
940  //              cs = cp;          //              cs = cp;
941  //              os = op;          //              os = op;
942  //                              cout << "Warning - track image ambiguity not solved" << endl;  //      cout << "Warning - track image ambiguity not solved" << endl;
943  //                              cout << ts->GetNtot() << " "<< ts->chi2 << " " << npcfit[1] << " "<< nphit_p << endl;          //                              cout << ts->GetNtot() << " "<< ts->chi2 << " " << npcfit[1] << " "<< nphit_p << endl;
944              };        };
945                            
946          }else{      }else{
947  //          ts = tp;        //            ts = tp;
948  //          cs = cp;        //            cs = cp;
949  //          os = op;        //            os = op;
950          };      };
951                    
952  //      cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;      //  cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
953  //      sorted_tracks->Add(ts);//save the track in the sorted array      //  sorted_tracks->Add(ts);//save the track in the sorted array
954  //      sorted_tracks.Add(ts);//save the track in the sorted array      //  sorted_tracks.Add(ts);//save the track in the sorted array
955  //      sorted_tracks.Add(tp);//save the track in the sorted array      //  sorted_tracks.Add(tp);//save the track in the sorted array
956  //      cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;      //  cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
957  //      cout<<"o "<<tp<<endl;      //  cout<<"o "<<tp<<endl;
958  //      cout<<"o "<<cp<<endl;      //  cout<<"o "<<cp<<endl;
959  //      cout<<"o "<<op<<endl;      //  cout<<"o "<<op<<endl;
960          new(ttsorted[i]) PamTrack(tp,cp,op);      new(ttsorted[i]) PamTrack(tp,cp,op);
961          new(ttimage[i])  PamTrack(ti,ci,oi);      new(ttimage[i])  PamTrack(ti,ci,oi);
962      };    };
963    
964      if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){    if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){
965          cout << "void PamLevel2::SortTracks(TString how): tsorted->GetEntries() "<<tsorted->GetEntries()<<" != trk2_obj->GetNTracks() = "<<trk2_obj->GetNTracks() <<endl;      cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() "<<tsorted->GetEntries()<<" != trk2_obj->GetNTracks() = "<<trk2_obj->GetNTracks() <<endl;
966          tsorted->Delete(); tsorted=0;      tsorted->Delete(); tsorted=0;
967          timage->Delete(); timage=0;          timage->Delete(); timage=0;
968      }    }
969    
970      //Restore Object count    //Restore Object count
971      //To save space in the table keeping track of all referenced objects    //To save space in the table keeping track of all referenced objects
972      //We reset the object count to what it was at the beginning of the event.    //We reset the object count to what it was at the beginning of the event.
973      TProcessID::SetObjectCount(ObjectNumber);    TProcessID::SetObjectCount(ObjectNumber);
974            
975  };  };
976  //--------------------------------------  //--------------------------------------
# Line 699  void PamLevel2::SortTracks(TString how){ Line 994  void PamLevel2::SortTracks(TString how){
994  TClonesArray *PamLevel2::GetTracks(){  TClonesArray *PamLevel2::GetTracks(){
995    
996  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
997      SortTracks("+CAL+TOF");      SortTracks();
998  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
999    
1000      return tsorted;      return tsorted;
# Line 718  PamTrack *PamLevel2::GetTrack(int it){ Line 1013  PamTrack *PamLevel2::GetTrack(int it){
1013    
1014  //    if(!trk2_obj) return 0;  //    if(!trk2_obj) return 0;
1015                    
1016  // //  *-*-*-*-*-*-*-*-*-*-*-*-*  // //  *-*-*-*+-*-*-*-*-*-*-*-*-*
1017  //     SortTracks("+CAL+TOF");  //     SortTracks("+CAL+TOF");
1018  // //  *-*-*-*-*-*-*-*-*-*-*-*-*  // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1019  // //    if(!sorted_tracks)return 0;  // //    if(!sorted_tracks)return 0;
# Line 737  PamTrack *PamLevel2::GetTrack(int it){ Line 1032  PamTrack *PamLevel2::GetTrack(int it){
1032    
1033  //    cout << "PamLevel2::GetTrack(int it) "<<endl;  //    cout << "PamLevel2::GetTrack(int it) "<<endl;
1034  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1035      SortTracks("+CAL+TOF");      SortTracks();
1036  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1037      if(!tsorted)return 0;      if(!tsorted)return 0;
1038      if(!tsorted->GetEntries())return 0;      if(!tsorted->GetEntries())return 0;
# Line 794  PamTrack *PamLevel2::GetTrackImage(int i Line 1089  PamTrack *PamLevel2::GetTrackImage(int i
1089    
1090    
1091  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1092      SortTracks("+CAL+TOF");      SortTracks();
1093  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1094      if(!timage)return 0;      if(!timage)return 0;
1095      if(!timage->GetEntries())return 0;      if(!timage->GetEntries())return 0;
# Line 831  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1126  TTree *PamLevel2::GetPamTree(TFile *f, T
1126    
1127  //     if( !detlist.IsNull() ) SetWhichTrees(detlist);  //     if( !detlist.IsNull() ) SetWhichTrees(detlist);
1128  //     else                    GetWhichTrees(f);  //     else                    GetWhichTrees(f);
1129          if ( pam_tree ){
1130        printf("WARNING: TTree *PamLevel2::GetPamTree(TFile *fl, TString detlist) -- pam_tree already exists!\n ");
1131        return pam_tree;
1132      };
1133      //
1134    
1135      cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;      cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;
1136    
1137      SetWhichTrees(detlist);      SetWhichTrees(detlist);
# Line 907  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1207  TTree *PamLevel2::GetPamTree(TFile *f, T
1207      if(R && TRG) {      if(R && TRG) {
1208          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1209          cout << "Trigger      : set branch address TrigLevel2"<<endl;          cout << "Trigger      : set branch address TrigLevel2"<<endl;
1210          if(!Trout)Trout=O;          if(!Trout)Trout=R;
1211          else Trout->AddFriend(R);          else Trout->AddFriend(R);
1212      }else{      }else{
1213          cout << "Trigger      : missing tree"<<endl;          cout << "Trigger      : missing tree"<<endl;
# Line 917  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1217  TTree *PamLevel2::GetPamTree(TFile *f, T
1217      if(S && S4) {      if(S && S4) {
1218          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1219          cout << "S4           : set branch address S4Level2"<<endl;          cout << "S4           : set branch address S4Level2"<<endl;
1220          if(!Trout)Trout=O;          if(!Trout)Trout=S;
1221          else Trout->AddFriend(S);          else Trout->AddFriend(S);
1222      }else{      }else{
1223          cout << "S4           : missing tree"<<endl;          cout << "S4           : missing tree"<<endl;
# Line 927  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1227  TTree *PamLevel2::GetPamTree(TFile *f, T
1227      if(N && ND) {      if(N && ND) {
1228          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1229          cout << "NeutronD     : set branch address NDLevel2"<<endl;          cout << "NeutronD     : set branch address NDLevel2"<<endl;
1230          if(!Trout)Trout=O;          if(!Trout)Trout=N;
1231          else Trout->AddFriend(N);          else Trout->AddFriend(N);
1232      }else{      }else{
1233          cout << "NeutronD     : missing tree"<<endl;          cout << "NeutronD     : missing tree"<<endl;
# Line 937  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1237  TTree *PamLevel2::GetPamTree(TFile *f, T
1237      if(A && AC) {      if(A && AC) {
1238          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1239          cout << "Anticounter  : set branch address AcLevel2"<<endl;          cout << "Anticounter  : set branch address AcLevel2"<<endl;
1240          if(!Trout)Trout=O;          if(!Trout)Trout=A;
1241          else Trout->AddFriend(A);          else Trout->AddFriend(A);
1242      }else{      }else{
1243          cout << "Anticounter  : missing tree"<<endl;          cout << "Anticounter  : missing tree"<<endl;
# Line 947  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1247  TTree *PamLevel2::GetPamTree(TFile *f, T
1247      if(B && ORB) {      if(B && ORB) {
1248          B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1249          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
1250          if(!Trout)Trout=O;          if(!Trout)Trout=B;
1251          else Trout->AddFriend(B);          else Trout->AddFriend(B);
1252      }else{      }else{
1253          cout << "OrbitalInfo  : missing tree"<<endl;          cout << "OrbitalInfo  : missing tree"<<endl;
# Line 968  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1268  TTree *PamLevel2::GetPamTree(TFile *f, T
1268      };      };
1269            
1270      cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;      cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;
       
     pam_tree = (TChain*)Trout;  
1271    
1272        pam_tree = (TChain*)Trout;
1273        
1274      return Trout;      return Trout;
1275            
1276  }  }
# Line 989  TList*  PamLevel2::GetListOfLevel2Files( Line 1289  TList*  PamLevel2::GetListOfLevel2Files(
1289                    
1290      TString wdir = gSystem->WorkingDirectory();      TString wdir = gSystem->WorkingDirectory();
1291            
1292      if(ddir=="")ddir = wdir;      //    if(ddir=="")ddir = wdir;
1293  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1294      cout << "Level2 data directory : "<<  ddir << endl;      if ( ddir != ""){
1295              cout << "Level2 data directory : "<<  ddir << endl;
1296        } else {
1297          cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory " << endl;
1298        };
1299      TList *contents  = new TList; // create output list      TList *contents  = new TList; // create output list
1300      contents->SetOwner();      contents->SetOwner();
1301            
# Line 1008  TList*  PamLevel2::GetListOfLevel2Files( Line 1311  TList*  PamLevel2::GetListOfLevel2Files(
1311  //          return 0;  //          return 0;
1312  //      }        //      }      
1313  //      flisttxt = fullpath;  //      flisttxt = fullpath;
1314          if ( !flisttxt.EndsWith(".root") ){
1315    
1316          flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));          flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1317    
# Line 1040  TList*  PamLevel2::GetListOfLevel2Files( Line 1344  TList*  PamLevel2::GetListOfLevel2Files(
1344  //          if( gSystem->IsFileInIncludePath(file,&fullpath) ){  //          if( gSystem->IsFileInIncludePath(file,&fullpath) ){
1345  //          if( (fullpath = gSystem->FindFile(ddir,file)) ){  //          if( (fullpath = gSystem->FindFile(ddir,file)) ){
1346              if( file.EndsWith(".root") ){              if( file.EndsWith(".root") ){
1347                  char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir),gSystem->BaseName(file));                TString filedir;
1348                  if (ddir != ""){
1349                    filedir = ddir; // take the input dir
1350                  } else {
1351                    gSystem->ChangeDirectory(wdir); // back to the working directory
1352                    filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir
1353                  };
1354                    char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir),gSystem->BaseName(file));
1355                  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
1356                  delete fullpath;                  delete fullpath;
1357              }              }
# Line 1049  TList*  PamLevel2::GetListOfLevel2Files( Line 1360  TList*  PamLevel2::GetListOfLevel2Files(
1360  //          };  //          };
1361          };          };
1362          in.close();          in.close();
1363                  } else {
1364              if(flisttxt.Contains("#"))flisttxt = flisttxt(0,flisttxt.First("#"));
1365              char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1366              contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list
1367              delete fullpath;
1368          };        
1369      }else{      }else{
1370                    
1371          cout << "No input file list given."<<endl;          cout << "No input file list given."<<endl;
1372          cout << "Check for existing root files."<<endl;          cout << "Check for existing root files."<<endl;
1373  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;
1374                    if ( ddir == "" ){
1375              ddir = wdir;
1376              cout << "Level2 data directory : "<<  ddir << endl;
1377            };
1378    
1379          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1380          TList *temp = datadir->GetListOfFiles();          TList *temp = datadir->GetListOfFiles();
1381  //              temp->Print();  //              temp->Print();
# Line 1099  TList*  PamLevel2::GetListOfLevel2Files( Line 1419  TList*  PamLevel2::GetListOfLevel2Files(
1419   * @return Pointer to a TChain   * @return Pointer to a TChain
1420   */   */
1421  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){
1422      //
1423      //
1424      //
1425      if ( pam_tree ){
1426        printf("WARNING: TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) -- pam_tree already exists!\n ");
1427        return pam_tree;
1428      };
1429      //
1430            
1431      TChain *Trout =0;      TChain *Trout =0;
1432    
# Line 1119  TChain *PamLevel2::GetPamTree(TList *fl, Line 1447  TChain *PamLevel2::GetPamTree(TList *fl,
1447      if(ORB)cout<<"ORB ";      if(ORB)cout<<"ORB ";
1448      cout << endl;      cout << endl;
1449    
1450      TChain *T = 0;             TChain *T = 0;    
1451      TChain *C = 0;       TChain *C = 0;
1452      TChain *O = 0;       TChain *O = 0;
1453      TChain *R = 0;       TChain *R = 0;
1454      TChain *S = 0;       TChain *S = 0;
1455      TChain *N = 0;       TChain *N = 0;
1456      TChain *A = 0;       TChain *A = 0;
1457      TChain *B = 0;       TChain *B = 0;
1458    
1459      TChain *L = 0;       TChain *L = 0;
1460            
1461      if(TRK2||TRK1||TRKh) T = new TChain("Tracker");          if(TRK2||TRK1||TRKh) T = new TChain("Tracker");    
1462      if(CAL2||CAL1)       C = new TChain("Calorimeter");      if(CAL2||CAL1)       C = new TChain("Calorimeter");
# Line 1138  TChain *PamLevel2::GetPamTree(TList *fl, Line 1466  TChain *PamLevel2::GetPamTree(TList *fl,
1466      if(ND)               N = new TChain("NeutronD");      if(ND)               N = new TChain("NeutronD");
1467      if(AC)               A = new TChain("Anticounter");      if(AC)               A = new TChain("Anticounter");
1468      if(ORB)              B = new TChain("OrbitalInfo");      if(ORB)              B = new TChain("OrbitalInfo");
   
1469       L = new TChain("SelectionList");       L = new TChain("SelectionList");
1470            
1471      // loop over files and create chains              // loop over files and create chains        
# Line 1158  TChain *PamLevel2::GetPamTree(TList *fl, Line 1485  TChain *PamLevel2::GetPamTree(TList *fl,
1485              if(ORB)              B->Add(name);              if(ORB)              B->Add(name);
1486              if(SELLI==1)         L->Add(name);              if(SELLI==1)         L->Add(name);
1487          };          };
1488      }      };
1489            
1490      cout << "done chain \n";      cout << "done chain \n";
1491        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
1492    
1493  //    UInt_t *found=0;  //    UInt_t *found=0;
1494  // Tracker  // Tracker
# Line 1175  TChain *PamLevel2::GetPamTree(TList *fl, Line 1503  TChain *PamLevel2::GetPamTree(TList *fl,
1503  //      else    T->SetBranchStatus("TrkHough",0,found);  //      else    T->SetBranchStatus("TrkHough",0,found);
1504          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;
1505          if(!Trout)Trout=T;          if(!Trout)Trout=T;
1506          else Trout->AddFriend("Tracker");          else Trout->AddFriend("Tracker");
1507      }else{      }else{
1508          cout << "Tracker      : missing tree"<<endl;          cout << "Tracker      : missing tree"<<endl;
1509      };      };
# Line 1246  TChain *PamLevel2::GetPamTree(TList *fl, Line 1574  TChain *PamLevel2::GetPamTree(TList *fl,
1574      }else{      }else{
1575          cout << "OrbitalInfo  : missing tree"<<endl;          cout << "OrbitalInfo  : missing tree"<<endl;
1576      };      };
1577        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
1578    
1579      // Selection List      // Selection List
1580      if(L && SELLI==1) {      if(L && SELLI==1) {
# Line 1257  TChain *PamLevel2::GetPamTree(TList *fl, Line 1586  TChain *PamLevel2::GetPamTree(TList *fl,
1586          sel_tree = L;          sel_tree = L;
1587  //      if(!Trout)Trout=O;  //      if(!Trout)Trout=O;
1588  //      else Trout->AddFriend("SelectionList");  //      else Trout->AddFriend("SelectionList");
1589            cout << "----------------------------------------------------" <<endl;
1590      }else{      }else{
1591  //      cout << "SelectionList  : missing tree"<<endl;  //      cout << "SelectionList  : missing tree"<<endl;
1592          if(L)L->Delete();          if(L)L->Delete();
# Line 1448  void PamLevel2::SetBranchAddress(TChain Line 1778  void PamLevel2::SetBranchAddress(TChain
1778   * @return Pointer to a TChain   * @return Pointer to a TChain
1779   */   */
1780  TChain *PamLevel2::GetRunTree(TList *fl){  TChain *PamLevel2::GetRunTree(TList *fl){
1781              //
1782      TChain *R = new TChain("Run");    //
1783      //
1784      if ( run_tree ){
1785        printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");
1786        return run_tree;
1787      };    
1788      //
1789      TChain *R = new TChain("Run");
1790            
1791      // loop over files and create chains              // loop over files and create chains        
1792      TIter next(fl);      TIter next(fl);
# Line 1488  TChain *PamLevel2::GetRunTree(TList *fl) Line 1825  TChain *PamLevel2::GetRunTree(TList *fl)
1825   * @return Pointer to a TTree   * @return Pointer to a TTree
1826   */   */
1827  TTree *PamLevel2::GetRunTree(TFile *f){  TTree *PamLevel2::GetRunTree(TFile *f){
1828      if ( run_tree ){
1829        printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");
1830        return run_tree;
1831      };
1832            
1833    
1834      cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;      cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;
1835    
1836      TTree *R = (TTree*)f->Get("Run");      TTree *T = (TTree*)f->Get("Run");
1837            
1838      if(R){      if(T){
1839          R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));          T->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
1840          cout << "Run         : set branch address RunInfo"<<endl;          cout << "Run         : set branch address RunInfo"<<endl;
1841          R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano          T->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
1842          cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano          cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano
1843      }      }
1844    
1845      run_tree = (TChain*)R;      run_tree = (TChain*)T;
1846    
1847      return R;      return T;
1848            
1849  }  }
1850  /**  /**
# Line 1510  TTree *PamLevel2::GetRunTree(TFile *f){ Line 1852  TTree *PamLevel2::GetRunTree(TFile *f){
1852   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
1853   * @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
1854   */   */
1855  Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev){  Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev){
1856    //    //
1857    // check if we have already called once GetEntry, if not call it    // check if we have already called once GetEntry, if not call it
1858    //    //
1859    if ( run->GetEntries() <= 0 ) return(false);      cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) --- NON FUNZIONA BENISSIMO.... "<<endl;
1860        if(!run){
1861              cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing RunInfo tree "<<endl;
1862              return(false);        
1863        }
1864        if ( run->GetEntries() <= 0 ) return(false);
1865    //    //
1866        
1867    Int_t oldrun = irun;    //  Int_t oldrun = irun;
1868      Long64_t oldrun = irun;
1869    
1870    // --------------------------------------    // --------------------------------------
1871    // if it is a full file (not preselected)    // if it is a full file (not preselected)
1872    // --------------------------------------    // --------------------------------------
1873    if(SELLI==0){    if(SELLI==0){
       if ( irun < 0 ){  
           irun = 0;  
           run->GetEntry(0);  
           runfirstentry = 0ULL;  
           runlastentry += (ULong64_t)(this->GetRunInfo()->NEVENTS) - 1ULL;  
       };        
1874    
1875          //
1876          // the absolute time is necessary to relate the event with the run
1877          //
1878        if( !GetOrbitalInfo() ){        if( !GetOrbitalInfo() ){
1879            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;
1880            return(false);            return(false);
1881        }        }
1882    
1883          //
1884          // the first time the routine is called, set run search from the beginning
1885          //
1886          if ( irun < 0LL ){
1887              irun = 0LL;
1888              run->GetEntry(irun);
1889              runfirstentry = 0LL;
1890              runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
1891              if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
1892          };      
1893          //
1894           if ( irun == run->GetEntries()-1LL &&
1895               !(GetOrbitalInfo()->absTime >= GetRunInfo()->RUNHEADER_TIME &&
1896                 GetOrbitalInfo()->absTime <= GetRunInfo()->RUNTRAILER_TIME)
1897                ){
1898             irun = -1LL;
1899             runfirstentry = 0LL;
1900             runlastentry = -1LL;
1901           };
1902        // modificato il controllo sull'aggiornamento del run, per evitare problemi        // modificato il controllo sull'aggiornamento del run, per evitare problemi
1903        // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)        // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)
1904        while ( GetOrbitalInfo()->absTime > GetRunInfo()->RUNTRAILER_TIME && irun < run->GetEntries() ){        //
1905          bool fromfirst = true;
1906          //
1907          while ( !(GetOrbitalInfo()->absTime >= GetRunInfo()->RUNHEADER_TIME && GetOrbitalInfo()->absTime <= GetRunInfo()->RUNTRAILER_TIME) && irun < run->GetEntries()-1LL ){
1908  //      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)));  
1909            irun++;            irun++;
1910            run->GetEntry(irun);            run->GetEntry(irun);
1911            runfirstentry = runlastentry+1ULL;            runfirstentry = runlastentry;
1912            runlastentry += (ULong64_t)(this->GetRunInfo()->NEVENTS);            if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runfirstentry += 1LL;
1913              runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
1914    //        cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
1915    //        cout << "runfirstentry "<<runfirstentry<<endl;
1916              //      printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));
1917              //      printf(" abstime %u trailertime %u \n",GetOrbitalInfo()->absTime,GetRunInfo()->RUNTRAILER_TIME);
1918              //      printf(" IDRUN %u \n",GetRunInfo()->ID);
1919              //
1920    //        prevshift = 0;
1921              //
1922              if ( irun == (Long64_t)(run->GetEntries()-1LL) && fromfirst && !(GetOrbitalInfo()->absTime >= GetRunInfo()->RUNHEADER_TIME && GetOrbitalInfo()->absTime <= GetRunInfo()->RUNTRAILER_TIME)){
1923                  printf(" resetting irun  (it should NOT happen!!!)\n");
1924                  fromfirst = false;
1925                  irun = 0;
1926                  run->GetEntry(irun);
1927                  runfirstentry = 0ULL;
1928                  runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
1929                  if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
1930              };
1931              //
1932        };        };
1933                //
1934          if (
1935               !(GetOrbitalInfo()->absTime >= GetRunInfo()->RUNHEADER_TIME &&
1936                 GetOrbitalInfo()->absTime <= GetRunInfo()->RUNTRAILER_TIME)
1937              ) {
1938              printf(" Something very wrong here: cannot find RUN containing absolute time %u \n",GetOrbitalInfo()->absTime);
1939              return false;
1940          }
1941        //        //
1942        if ( irun == oldrun || irun >= run->GetEntries() ) return(false);        if ( irun == oldrun || irun >= run->GetEntries() ) return(false);
1943        //        //
1944        //  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);
1945        //        //
1946          prevshift = 0;
1947          cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
1948    //      cout << "runfirstentry "<<runfirstentry<<endl;
1949        return(true);            return(true);    
1950    };    };
1951    // ----------------------------------------------------    // ----------------------------------------------------
# Line 1557  Bool_t PamLevel2::UpdateRunInfo(TChain * Line 1954  Bool_t PamLevel2::UpdateRunInfo(TChain *
1954    // ----------------------------------------------------    // ----------------------------------------------------
1955    if(SELLI==1){          if(SELLI==1){      
1956        sel_tree->GetEntry(iev);        sel_tree->GetEntry(iev);
1957    //      cout << irun << " "<< irunentry << endl;
1958        if(irun != oldrun){        if(irun != oldrun){
1959            run->GetEntry(irun);            run->GetEntry(irun);
1960              cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
1961              prevshift = 0;
1962            return true;            return true;
1963        }        }
1964        return false;        return false;
# Line 1567  Bool_t PamLevel2::UpdateRunInfo(TChain * Line 1967  Bool_t PamLevel2::UpdateRunInfo(TChain *
1967    return false;    return false;
1968    //    //
1969  };  };
1970    
1971    Bool_t PamLevel2::UpdateRunInfo(Long64_t iev){
1972    
1973        if(!run_tree){
1974            cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded"<<endl;
1975            return false;
1976        }
1977        if ( run_tree->GetEntries() <= 0 ) {
1978            cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty"<<endl;
1979            return(false);
1980        }
1981      
1982        Int_t oldrun = irun; // store current run index
1983    
1984        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
1985        // if it is a full file (not preselected)
1986        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
1987        if(SELLI==0){
1988    
1989            // ---------------------------------------------------------------
1990            // the absolute time is necessary to relate the event with the run
1991            // ---------------------------------------------------------------
1992            if( !GetOrbitalInfo() ){
1993                cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
1994                return(false);
1995            }
1996          
1997            // -----------------------------------------------------------------------
1998            // the first time the routine is called, set run search from the beginning
1999            // -----------------------------------------------------------------------
2000            if ( irun < 0 ){
2001                irun = 0LL;
2002                irunentry = 0;
2003                prevshift = 0;
2004                run_tree->GetEntry(irun);
2005            };      
2006            //
2007            bool fromfirst = true; // first loop over runs
2008    
2009    //      Bool_t hasfrag = false;
2010    //      if( GetRunInfo()->ID_RUN_FRAG!=0 && GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID)hasfrag=true;
2011    //      ULong64_t fragid = GetRunInfo()->ID_RUN_FRAG;
2012    
2013            // ------------------------------------------------------
2014            // loop over runs to find the one that contains the event
2015            // ------------------------------------------------------
2016            while (
2017                (
2018                    (
2019                        !(GetOrbitalInfo()->absTime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)
2020                          GetOrbitalInfo()->absTime <= GetRunInfo()->RUNTRAILER_TIME) &&
2021                        !(GetOrbitalInfo()->OBT >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)
2022                          GetOrbitalInfo()->OBT <= GetRunInfo()->RUNTRAILER_OBT)
2023                        )
2024                    || GetRunInfo()->NEVENTS==0
2025                    || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift)
2026                    )
2027                    && irun < run_tree->GetEntries() ){
2028    
2029                irun++;
2030                // ------------------------------------
2031                // if the end of run tree is reached...
2032                // ------------------------------------
2033                if( irun == run_tree->GetEntries() ){
2034                    if(!fromfirst){
2035                        // -----------------------------------------------------
2036                        // if it happened already once and the run was not found
2037                        // ---> exit with error
2038                        // -----------------------------------------------------
2039                        cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- event entry #"<<iev<<" does not belong to any run (should not happen)" <<endl;
2040                        return false;
2041                    }
2042                    // -----------------------------------------
2043                    // ...otherwise repeat search from beginning
2044                    // -----------------------------------------
2045                    cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- reached end of run tree. searchin again from beginning " <<endl;
2046                    fromfirst = false;
2047                    irun = 0LL;
2048                    runfirstentry = 0LL;
2049                }
2050                // -------------------------------------------------------------------
2051                // save the index of the first entry of the run, relative to pam_tree,
2052                // and read a new run
2053                // -------------------------------------------------------------------
2054                if(irun>0)runfirstentry += (GetRunInfo()->NEVENTS)-prevshift;
2055                irunentry = 0;
2056                prevshift = 0;
2057                run_tree->GetEntry(irun);      
2058                if(GetRunInfo()->RUNHEADER_OBT>GetRunInfo()->RUNTRAILER_OBT ){
2059                    cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun "<<irun<<"  has RUNHEADER_OBT>=RUNTRAILER_OBT " <<endl;
2060                    cout << "                                                            (NB!! in this case some events are assigned to a wrong run)"<<endl;
2061                }
2062    //          if(hasfrag &&  fragid != GetRunInfo()->ID){
2063    //              cout << "... where is the next fragment ??"<<endl;
2064    //          }
2065            };
2066    
2067    
2068            // --------------------------------------
2069            // if there was no need to update the run
2070            // ---> exit with FALSE
2071            // --------------------------------------
2072            if ( irun == oldrun ) return(false);
2073    
2074            // --------------------------------------
2075            // ... otherwise
2076            // ---> exit with TRUE
2077            // --------------------------------------
2078            cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2079            // ----------------------------------------------------
2080            // then check if the run has a fragment
2081            // in this case we have to switch to the next fragment
2082            // when the end of the first fragment is reached
2083            // ----------------------------------------------------
2084            if(
2085                GetRunInfo()->ID_RUN_FRAG != 0 &&
2086    //          GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID &&
2087                true ){
2088                cout << "* fragment *"<<endl;              
2089            }
2090            
2091            return(true);    
2092        };
2093        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2094        // if it is a preselected file (there is SelectionList)
2095        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2096        if(SELLI==1){      
2097            sel_tree->GetEntry(iev);
2098            if(irun != oldrun){
2099                run_tree->GetEntry(irun);
2100                cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2101                prevshift = 0;
2102                return true;
2103            }
2104            return false;
2105        }
2106    
2107        return false;
2108        //
2109    };
2110  /**  /**
2111   * 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)
2112   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
2113   * @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
2114   */   */
2115  Bool_t PamLevel2::UpdateRunInfo(TTree *run, ULong64_t iev){  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev){
2116    return(UpdateRunInfo((TChain*)run,iev));    return(UpdateRunInfo((TChain*)run,iev));
2117  };  };
2118    
# Line 2196  void PamLevel2::CreateCloneTrees0( TChai Line 2736  void PamLevel2::CreateCloneTrees0( TChai
2736   */   */
2737  void PamLevel2::CreateCloneTrees(TFile *ofile){  void PamLevel2::CreateCloneTrees(TFile *ofile){
2738    
2739    
2740    //  if the pointer is null, create a default file
2741    
2742        if(!ofile){
2743            cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root "<<endl;
2744            ofile = new TFile("clone-tree.root","recreate");
2745        }
2746    
2747      ofile->cd();      ofile->cd();
2748    
2749      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
# Line 2207  void PamLevel2::CreateCloneTrees(TFile * Line 2755  void PamLevel2::CreateCloneTrees(TFile *
2755      cout << "Run          : branch RunInfo"<<endl;      cout << "Run          : branch RunInfo"<<endl;
2756      run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));      run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));
2757      cout << "Run          : branch SoftInfo"<<endl;      cout << "Run          : branch SoftInfo"<<endl;
2758                    // ------------------
2759        // replicate run tree
2760        // ------------------
2761        cout << "----------------------------------------------------"<<endl;
2762        cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
2763        for (Int_t i=0; i<run_tree->GetEntries(); i++){
2764            run_tree->GetEntry(i);
2765            cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
2766            run_tree_clone->Fill();
2767        }
2768        cout << "----------------------------------------------------"<<endl;
2769    
2770    
2771      sel_tree_clone = new TTree("SelectionList","List of selected events ");      sel_tree_clone = new TTree("SelectionList","List of selected events ");
2772      sel_tree_clone->Branch("RunEntry",&irun,"runentry/I");      sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
2773      sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/I");      sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/L");
2774            
2775    
2776      Int_t i=0;      Int_t i=0;
# Line 2222  void PamLevel2::CreateCloneTrees(TFile * Line 2780  void PamLevel2::CreateCloneTrees(TFile *
2780              pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));              pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));
2781              pam_tree_clone[i]->BranchRef();              pam_tree_clone[i]->BranchRef();
2782              cout << "Tracker      : branch TrkLevel1"<<endl;              cout << "Tracker      : branch TrkLevel1"<<endl;
2783              cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;  //          cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;
2784          };          };
2785          if(TRK2) {          if(TRK2) {
2786              pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));              pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));
# Line 2352  void PamLevel2::WriteCloneTrees(){ Line 2910  void PamLevel2::WriteCloneTrees(){
2910  /**  /**
2911   * 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.
2912   */   */
2913  Int_t PamLevel2::GetEntry(Int_t iee){  //Int_t PamLevel2::GetEntry(Int_t iee){
2914    Int_t PamLevel2::GetEntry(Long64_t iee){
2915            
2916    //     cout << "-------------------------------------"<<endl;
2917    //     cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;
2918    
2919      if(!pam_tree){      if(!pam_tree){
2920          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;
2921          return 0;          return 0;
2922      }      }
2923    
2924      Int_t ii=0;  
2925        //
2926        // 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...
2927        //
2928        //    if(!run_tree ){
2929        //  cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loeaded"<<endl;
2930        //  return 0;
2931        //    }
2932    
2933        Long64_t ii=0;
2934      //-------------------------------      //-------------------------------
2935      ii = iee;      ii = iee;
2936      if( !pam_tree->GetEntry(ii) ) return 0;      if( !pam_tree->GetEntry(ii) ){      
2937      //-------------------------------          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;  
2938          return 0;          return 0;
2939      }      }
2940        //
2941        // ... 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.
2942        // 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
2943        // a problem if you don't check the return code of getentry.
2944        //
2945        if(!run_tree ){
2946            if ( TRK0 || CAL0 || TOF0 || RUN ) { //forse cosi` va bene per tornare 1?
2947                cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded"<<endl;
2948                return 0;
2949            }  else {
2950                return 1; //cosi` se non c'e` run esce qua...
2951            }
2952        }
2953    
2954        //-------------------------------
2955      ii = iee;      ii = iee;
2956      Bool_t UPDATED = UpdateRunInfo(run_tree,ii);  //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);
2957    //    Bool_t UPDATED = UpdateRunInfo(ii);
2958        UpdateRunInfo(ii);
2959      if(SELLI==0)irunentry = iee-runfirstentry;      if(SELLI==0)irunentry = iee-runfirstentry;
2960      if(UPDATED && run_tree_clone)run_tree_clone->Fill();  //    if(UPDATED && run_tree_clone)run_tree_clone->Fill();
2961      
2962  //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;  //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;
2963    
2964      if( TRK0 || CAL0 || TOF0 )GetYodaEntry( );  //     cout << "irunentry     "<<irunentry << endl;
2965    //     cout << "runfirstentry "<<runfirstentry << endl;
2966    //     cout << "nevents       "<<GetRunInfo()->NEVENTS<< endl;
2967    
2968    //     if( TRK0 || CAL0 || TOF0 ){
2969    //      if( !GetYodaEntry( ) ){
2970    //          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading level0 tree"<<endl;
2971    //          return 0;
2972    //      }
2973    //     }
2974    
2975    
2976      return 1;      return 1;
2977    
2978  }  }
2979    
2980    TrkLevel0    *PamLevel2::GetTrkLevel0(){
2981        if( !TRK0 )return NULL;
2982        if( !GetYodaEntry( ) ){
2983            cout << " Int_t PamLevel2::GetTrkLevel0() -- ERROR -- error reading level0 tree"<<endl;
2984            return 0;
2985        }
2986        return trk0_obj;
2987    };
2988    CaloLevel0    *PamLevel2::GetCaloLevel0(){
2989        if( !CAL0 )return NULL;
2990        if( !GetYodaEntry( ) ){
2991            cout << " Int_t PamLevel2::GetCaloLevel0() -- ERROR -- error reading level0 tree"<<endl;
2992            return 0;
2993        }
2994        return calo0_obj;
2995    };
2996    
2997    
2998  /**  /**
2999   * 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.
3000   * 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 2397  TTree* PamLevel2::GetYodaTree( ){ Line 3009  TTree* PamLevel2::GetYodaTree( ){
3009      // check if iroot has changed      // check if iroot has changed
3010      //===================================      //===================================
3011      if( irun<0 ){      if( irun<0 ){
3012          cout << "PamLevel2::GetYodaTree() -- ERROR "<<endl;          cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = "<<irun<<endl;
3013          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;  
3014          return NULL;          return NULL;
3015      }          }    
3016      Int_t irootnew = run_obj->ID_ROOT_L0;      Int_t irootnew = run_obj->ID_ROOT_L0;
3017  //     cout << "iroot    "<<iroot<<endl;  //      cout << "iroot    "<<iroot<<endl;
3018  //     cout << "irootnew "<<irootnew<<endl;  //      cout << "irootnew "<<irootnew<<endl;
3019    
3020      //===================================      //===================================
3021      // load the level0 file      // load the level0 file
# Line 2452  TTree* PamLevel2::GetYodaTree( ){ Line 3062  TTree* PamLevel2::GetYodaTree( ){
3062          l0_tree = (TTree*)l0_file->Get("Physics");          l0_tree = (TTree*)l0_file->Get("Physics");
3063          if(!h0_obj)h0_obj = new EventHeader();          if(!h0_obj)h0_obj = new EventHeader();
3064          l0_tree->SetBranchAddress("Header" ,&h0_obj);          l0_tree->SetBranchAddress("Header" ,&h0_obj);
3065            prevshift = 0;
3066          //---------------------------------------------------          //---------------------------------------------------
3067          // TRACKER:          // TRACKER:
3068          if(TRK0){          if(TRK0){
# Line 2460  TTree* PamLevel2::GetYodaTree( ){ Line 3071  TTree* PamLevel2::GetYodaTree( ){
3071                  trk0_obj->Set();                  trk0_obj->Set();
3072              };              };
3073              l0_tree->SetBranchAddress("Tracker" ,trk0_obj->GetPointerToTrackerEvent());              l0_tree->SetBranchAddress("Tracker" ,trk0_obj->GetPointerToTrackerEvent());
             TrkParams::SetCalib(run_obj,dbc);  
3074          }          }
3075          //---------------------------------------------------          //--------------------------------------------------
3076          // CALORIMETER:          // CALORIMETER:
3077          if(CAL0){          if(CAL0){
3078              cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;              if(!calo0_obj){
3079                    calo0_obj = new CaloLevel0();
3080                    calo0_obj->Set();
3081                };
3082                l0_tree->SetBranchAddress("Calorimeter" ,calo0_obj->GetPointerToCalorimeterEvent());
3083                //      cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;
3084          }          }
3085          //---------------------------------------------------          //---------------------------------------------------
3086          // TOF:          // TOF:
# Line 2475  TTree* PamLevel2::GetYodaTree( ){ Line 3090  TTree* PamLevel2::GetYodaTree( ){
3090    
3091      };      };
3092    
3093        if(!dbc || (dbc && !dbc->IsConnected())){
3094            cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;
3095        }
3096    
3097        if ( TRK0 ){
3098            TrkParams::Load(6);
3099            if( !TrkParams::IsLoaded(6) ){
3100                cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- VK-mask not loaded"<<endl;
3101            };
3102            TrkParams::SetCalib(run_obj,dbc);
3103            TrkParams::LoadCalib( );
3104            if( !TrkParams::CalibIsLoaded() ){
3105                cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded"<<endl;
3106            };
3107        }
3108    
3109  //    cout << l0_tree << endl;  //    cout << l0_tree << endl;
3110            
3111      return l0_tree;      return l0_tree;
# Line 2489  Int_t PamLevel2::GetYodaEntry(){ Line 3120  Int_t PamLevel2::GetYodaEntry(){
3120  //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;  //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;
3121      if(!GetYodaTree())return 0;      if(!GetYodaTree())return 0;
3122            
3123        // patch
3124        if( irunentry < 0){
3125    //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
3126            irunentry=0LL;
3127        }
3128      //  ---------------------------------      //  ---------------------------------
3129      //  if file is NOT a preselected file      //  if file is NOT a preselected file
3130      //  ---------------------------------      //  ---------------------------------
3131      Int_t quellagiusta = irunentry + run_obj->EV_FROM;      Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM);
3132    //     cout << " irunentry "<<irunentry<<endl;
3133    //     cout << " EV_FROM "<<run_obj->EV_FROM<<endl;
3134    //     cout << " quellagiusta = irunentry + EV_FROM "<< quellagiusta << endl;
3135    
3136    //     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
3137    //     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
3138    //     cout << " time "<< GetOrbitalInfo()->absTime << endl;
3139    //     cout << " trk_calib_used "<<run_obj->TRK_CALIB_USED<< endl;
3140        
3141      if( !GetOrbitalInfo() ){      if( !GetOrbitalInfo() ){
3142          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo "<<endl;          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo "<<endl;
3143          return 0;          return 0;
3144      }      }
3145        if( GetOrbitalInfo()->OBT==0 && GetOrbitalInfo()->pkt_num==0 ){
3146            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? "<<endl;
3147            return 0;
3148        }
3149    
3150      // ---------------------------------------------------------------------      // ---------------------------------------------------------------------
3151      // ATTENTION!!!      // ATTENTION!!!
# Line 2512  Int_t PamLevel2::GetYodaEntry(){ Line 3160  Int_t PamLevel2::GetYodaEntry(){
3160      // ---------------------------------------------------------------------      // ---------------------------------------------------------------------
3161      Int_t answer = 0;      Int_t answer = 0;
3162      Int_t shift =0;      Int_t shift =0;
3163        //    printf(" siamo qui %i %i \n",shift,prevshift);
3164        Int_t maxshift = 10;
3165      do{      do{
3166  //      if(shift>0){          if(shift>0){    
3167  //          cout << " level0 <--> level2 mismatch ( irun "<<irun<<" irunentry "<<irunentry<<" shift "<<shift<<" )"<<endl;              cout << " PKTNUM  L2 --- "<< GetOrbitalInfo()->pkt_num << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
3168  //      }              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;
3169          answer = GetYodaTree()->GetEntry(quellagiusta+shift);              cout << "         L2 <--> L0 mismatch ( irun "<<irun<<" irunentry "<<irunentry<<" shift "<<shift<<" prevshift "<<prevshift<<" )"<<endl;
3170            }
3171            answer = l0_tree->GetEntry(quellagiusta+(Long64_t)shift+(Long64_t)prevshift);
3172          shift++;          shift++;
3173          if( !GetEventHeader() ){          if( !GetEventHeader() ){
3174              cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader "<<endl;              cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader "<<endl;
3175              return 0;              return 0;
3176          }          }
3177  //      if( (quellagiusta+shift) == GetYodaTree()->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;  //      cout << "PKTNUM "<<shift<<" ==  L2 --- "<< GetOrbitalInfo()->pkt_num << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
3178      }while( ( (UInt_t)(GetOrbitalInfo()->OBT) != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || GetOrbitalInfo()->pkt_num != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta+shift) < (UInt_t)(GetYodaTree()->GetEntries()) );  //      cout << " L2 --- "<< GetOrbitalInfo()->OBT << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;
3179    //      if( (quellagiusta+shift) == l0_tree->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;
3180    //      cout << " GetOrbitalInfo()->OBT "<< GetOrbitalInfo()->OBT << endl;
3181    //      cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;
3182    //      cout << " GetOrbitalInfo()->pkt_num "<< GetOrbitalInfo()->pkt_num << endl;
3183    //      cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
3184    //      printf(" IDRUN %u \n",GetRunInfo()->ID);
3185    //
3186            if ( prevshift != 0 && (quellagiusta+(Long64_t)shift) == GetYodaTree()->GetEntries() ){
3187              prevshift = 0;
3188              shift = -1;
3189            };
3190    
3191        }while( ( GetOrbitalInfo()->OBT != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || GetOrbitalInfo()->pkt_num != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta+(Long64_t)shift) < GetYodaTree()->GetEntries() && shift < maxshift);
3192    
3193        if ( (quellagiusta+(Long64_t)shift+(Long64_t)prevshift) > GetYodaTree()->GetEntries() || shift == maxshift ) {
3194            cout << " Big trouble here, no such event in Level0 data! " <<endl;
3195            return 0;
3196        }
3197  //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;  //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
3198  //    return GetYodaTree()->GetEntry(quellagiusta);  //    return GetYodaTree()->GetEntry(quellagiusta);
3199        if ( shift > 1 ) prevshift += (shift-1);
3200            
3201      return answer;      return answer;
3202            
3203  }  }
3204    /**
3205     * \Brief Set DB connection
3206     */
3207    Bool_t PamLevel2::SetDBConnection(){
3208    
3209        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3210        cout<<"Connecting to DB"<<endl;
3211        cout<<"HOST "<<host<<endl;
3212        cout<<"USER "<<user<<endl;
3213        cout<<"PSW  "<<psw<<endl;
3214        dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
3215        if( !dbc )return false;
3216        if( !dbc->IsConnected() )return false;    
3217        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3218        return true;
3219    
3220    }

Legend:
Removed from v.1.24  
changed lines
  Added in v.1.40

  ViewVC Help
Powered by ViewVC 1.1.23