/[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.9 by pam-fi, Tue Dec 5 12:13:39 2006 UTC revision 1.47 by mocchiut, Wed Sep 12 09:39:32 2007 UTC
# Line 7  Line 7 
7   * Default constructor   * Default constructor
8   */   */
9  PamTrack::PamTrack(){  PamTrack::PamTrack(){
10      trk_track  = this->TrkTrack::GetTrkTrack();      trk_track  = 0;
11      calo_track = this->CaloTrkVar::GetCaloTrkVar();      calo_track = 0;
12      tof_track  = this->ToFTrkVar::GetToFTrkVar();      tof_track  = 0;
13        candeleteobj = 0;
14        pscore = 0;
15        iscore = 0;
16  };  };
17  //--------------------------------------  //--------------------------------------
18  //  //
# Line 19  PamTrack::PamTrack(){ Line 22  PamTrack::PamTrack(){
22   * Constructor   * Constructor
23   */   */
24  PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o){  PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o){
25      trk_track  = this->TrkTrack::GetTrkTrack();  
26      calo_track = this->CaloTrkVar::GetCaloTrkVar();      trk_track  = 0;
27      tof_track  = this->ToFTrkVar::GetToFTrkVar();      calo_track = 0;
28      if(t) *trk_track  = *t;      tof_track  = 0;
29      if(c) *calo_track = *c;  //     if(t)trk_track  = new TrkTrack(*t);
30      if(o) *tof_track  = *o;      //     if(c)calo_track = new CaloTrkVar(*c);
31    //     if(o)tof_track  = new ToFTrkVar(*o);
32        if(t)trk_track  = t;
33        if(c)calo_track = c;
34        if(o)tof_track  = o;
35        candeleteobj = 0;
36    
37  };  };
38    PamTrack::PamTrack(const PamTrack& track){
39    
40        TrkTrack   *t = track.trk_track;
41        CaloTrkVar *c = track.calo_track;
42        ToFTrkVar  *o = track.tof_track;
43    
44        trk_track  = 0;
45        calo_track = 0;
46        tof_track  = 0;
47        if(t)trk_track  = new TrkTrack(*t);
48        if(c)calo_track = new CaloTrkVar(*c);
49        if(o)tof_track  = new ToFTrkVar(*o);
50        candeleteobj = 1;
51        
52    }
53    void PamTrack::Clear(){
54    
55    //    cout << "PamTrack::Clear() "<<candeleteobj<<endl;
56        if(candeleteobj){
57            if(trk_track)  trk_track->TrkTrack::Clear();
58            if(calo_track) calo_track->CaloTrkVar::Clear();//???
59            if(tof_track)  tof_track->ToFTrkVar::Clear();//???
60        }else{
61            trk_track  = 0;
62            calo_track = 0;
63            tof_track  = 0;
64        }
65        pscore = 0;
66        iscore = 0;
67    
68    }
69    void PamTrack::Delete(){
70    //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;
71        if(candeleteobj){
72            if(trk_track)  {
73                trk_track->TrkTrack::Clear();
74                delete trk_track;
75            }
76            if(calo_track){
77                calo_track->CaloTrkVar::Clear();//???
78                delete calo_track;
79            }
80            if(tof_track){
81                tof_track->ToFTrkVar::Clear();//???
82                delete tof_track;
83            }
84        }else{
85            Clear();
86        }
87    }
88  //--------------------------------------  //--------------------------------------
89  //  //
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                    
120      trk2_obj  = TrkLevel2::GetTrkLevel2();      h0_obj    = 0;
121      trk1_obj  = TrkLevel1::GetTrkLevel1();      trk0_obj  = 0;
122      trkh_obj  = TrkHough::GetTrkHough();      calo0_obj  = 0;
123      calo1_obj = CaloLevel1::GetCaloLevel1();  
124      calo2_obj = CaloLevel2::GetCaloLevel2();      trk2_obj  = 0;
125      tof_obj   = ToFLevel2::GetToFLevel2();      trk1_obj  = 0;
126      trig_obj  = TrigLevel2::GetTrigLevel2();      trkh_obj  = 0;
127      s4_obj    = S4Level2::GetS4Level2();      calo1_obj = 0;
128      nd_obj    = NDLevel2::GetNDLevel2();      calo2_obj = 0;
129      ac_obj    = AcLevel2::GetAcLevel2();      tof_obj   = 0;
130      orb_obj   = OrbitalInfo::GetOrbitalInfo();      trig_obj  = 0;
131        s4_obj    = 0;
132      run_obj   = new GL_RUN();      nd_obj    = 0;
133            ac_obj    = 0;
134  //     trk1_clone  = 0;      orb_obj   = 0;
135  //     trk2_clone  = 0;  
136  //     trkh_clone  = 0;      run_obj   = 0;//new GL_RUN();
137  //     calo1_clone = 0;      soft_obj   = 0;// Emiliano
138  //     calo2_clone = 0;      irun = -1LL;
139  //     tof_clone   = 0;      runfirstentry = 0LL;
140  //     trig_clone  = 0;      runlastentry = 0LL;
141  //     s4_clone    = 0;  
142  //     nd_clone    = 0;      l0_file = NULL;
143  //     ac_clone    = 0;      l0_tree = NULL;
144  //     orb_clone   = 0;      iroot   = -1;
145        dbc     = 0;
146  //     T_clone  = 0;  
147  //     C_clone  = 0;      prevshift = 0;
148  //     O_clone  = 0;      
149  //     R_clone  = 0;      run_tree = NULL;
150  //     S_clone  = 0;      run_tree_clone = NULL;
151  //     N_clone  = 0;      sel_tree = NULL;
152  //     A_clone  = 0;      sel_tree_clone = NULL;
153  //     B_clone  = 0;      
154        irunentry = -1LL;
155        pam_tree = NULL;
156        for(Int_t i=0; i<NCLONES; i++ )pam_tree_clone[i]=NULL;
157    
158        totdltime[0] = 0LL;
159        totdltime[1] = 0LL;
160    
161        host = "mysql://localhost/pamelaprod";
162        user = "anonymous";
163        psw = "";
164        const char *pamdbhost=gSystem->Getenv("PAM_DBHOST");
165        const char *pamdbuser=gSystem->Getenv("PAM_DBUSER");
166        const char *pamdbpsw=gSystem->Getenv("PAM_DBPSW");
167        if ( !pamdbhost ) pamdbhost = "";
168        if ( !pamdbuser ) pamdbuser = "";
169        if ( !pamdbpsw ) pamdbpsw = "";
170        if ( strcmp(pamdbhost,"") ) host = pamdbhost;
171        if ( strcmp(pamdbuser,"") ) user = pamdbuser;
172        if ( strcmp(pamdbpsw,"") ) psw = pamdbpsw;
173    
174      for(Int_t i=0; i<8; i++ )tree_clone[i]=NULL;      
175    
176      sorted_tracks = new TRefArray();  //    sorted_tracks = 0;//new TRefArray();
177            
178        CAL0 = false;
179      CAL1 = true;      CAL1 = true;
180      CAL2 = true;      CAL2 = true;
181      TRK2 = true;      TRK2 = true;
182      TRK1 = false;      TRK1 = false;
183      TRKh = false;      TRKh = false;
184      TRG = true;      TRKh = false;
185      TOF = true;      TRG  = true;
186      S4  = true;      TOF  = true;
187      ND  = true;      TOF0 = false;
188      AC  = true;      S4   = true;
189      ORB = true;      ND   = true;
190            AC   = true;
191        ORB  = true;
192        
193        RUN  = true;
194    
195        SELLI = -1;
196    
197        ISGP = false;
198    
199        tsorted=0;
200        timage=0;
201        
202        howtosort = "+CAL+TOF";
203        //howtosort = "+TOF";
204        sortthr = 100.;
205    
206  };  };
207  /**  /**
208   * Destructor   * Delete the event (act as Dtor)
209   */   */
210  PamLevel2::~PamLevel2(){                  void PamLevel2::Delete(){
211      delete run_obj;          
212      Clear();      if(run_obj)delete run_obj;
213        if(soft_obj)delete soft_obj; //Emiliano
214    
215    //    cout << "void PamLevel2::Clear()"<<endl;
216        if(h0_obj)   delete h0_obj;
217        if(trk0_obj) delete trk0_obj;      
218        if(calo0_obj) delete calo0_obj;    
219        if(trk1_obj) delete trk1_obj;      
220        if(trk2_obj) delete trk2_obj;
221        if(trkh_obj) delete trkh_obj;
222        if(calo1_obj)delete calo1_obj;
223        if(calo2_obj)delete calo2_obj;
224        if(tof_obj)  delete tof_obj;
225        if(trig_obj) delete trig_obj;
226        if(s4_obj)   delete s4_obj;
227        if(nd_obj)   delete nd_obj;
228        if(ac_obj)   delete ac_obj;
229        if(orb_obj)  delete orb_obj;
230        
231        if(tsorted){
232            tsorted->Delete();
233            delete tsorted;
234        }
235        if(timage){
236            timage->Delete();
237            delete timage;
238        }
239    
240        if(dbc){
241            dbc->Close();
242            delete dbc;
243        }
244        
245        if(l0_file)l0_file->Close();
246        //    if(pam_tree)pam_tree->Delete();;
247    
248      if ( pam_tree ){
249        //
250        // 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...
251        //
252        TList *temp = pam_tree->GetListOfFriends();
253        TList *contents  = new TList; // create chain friend list
254        contents->SetOwner();
255        TIter next(temp);
256        TChain *questo = 0;
257        while ( (questo = (TChain*) next()) ){
258          TString name =  questo->GetName();
259          contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
260        };
261        //
262        // deleting the main chain
263        //
264        pam_tree->Delete();
265        //
266        // deleting the friends...
267        //
268        TIter next2(contents);
269        TChain *questa = 0;
270        while ( questa = (TChain*)next2() ){
271          TString name =  questa->GetName();
272          questa->Delete();      
273          questa=NULL;
274        };
275        //
276      };
277      pam_tree = NULL;
278    
279        if(run_tree)run_tree->Delete();;
280        if(sel_tree)sel_tree->Delete();;
281        for(Int_t i=0; i<NCLONES; i++ )if(pam_tree_clone[i])pam_tree_clone[i]->Delete();;
282        if(run_tree_clone)run_tree_clone->Delete();;
283        if(sel_tree_clone)sel_tree_clone->Delete();;
284        
285    
286  };  };
287    
288  /**  /**
289   * Clear the event   * Clear the event (NB! does not deallocate objects)
290   */   */
291  void PamLevel2::Clear(){  void PamLevel2::Clear(){
292                    
293      TrkLevel1::Clear();  //    cout << "void PamLevel2::Clear()"<<endl;
294            
295      TrkLevel2::Clear();  //
296      CaloLevel2::Clear();  // This method is called once for every entry but RunInfo and SoftInfo do not change until the next run so we cannot clear them here unless we don't
297      ToFLevel2::Clear();  // want to load them for each event even if they are the same...
298      TrigLevel2::Clear();  //
299      S4Level2::Clear();  //    if(run_obj)delete run_obj;
300      NDLevel2::Clear();  //    if(run_obj) run_obj->Clear();  // Emiliano: Do not deallocate run_obj here, it will give segmentation fault! call clear instead
301      AcLevel2::Clear();  //    if(soft_obj) soft_obj->Clear();
302      OrbitalInfo::Clear();  
303        if(h0_obj)   h0_obj->Clear();      
304  //     if(trk1_clone)trk1_clone->Clear();  //    if(trk0_obj) trk0_obj->Clear();  
305  //     if(trk2_clone)trk2_clone->Clear();      if(trk1_obj) trk1_obj->Clear();    
306  //     if(trkh_clone)trkh_clone->Clear();      if(trk2_obj) trk2_obj->Clear();
307  //     if(calo1_clone)calo1_clone->Clear();      if(trkh_obj) trkh_obj->Clear();
308  //     if(calo2_clone)calo2_clone->Clear();      if(calo0_obj) calo0_obj->Clear();  
309  //     if(tof_clone)tof_clone->Clear();      if(calo1_obj)calo1_obj->Clear();
310  //     if(trig_clone)trig_clone->Clear();      if(calo2_obj)calo2_obj->Clear();
311  //     if(s4_clone)s4_clone->Clear();      if(tof_obj)  tof_obj->Clear();
312  //     if(nd_clone)nd_clone->Clear();      if(trig_obj) trig_obj->Clear();
313  //     if(ac_clone)ac_clone->Clear();      if(s4_obj)   s4_obj->Clear();
314  //     if(orb_clone)orb_clone->Clear();      if(nd_obj)   nd_obj->Clear();
315            if(ac_obj)   ac_obj->Clear();
316      sorted_tracks->Delete(); // clean the reference array      if(orb_obj)  orb_obj->Clear();
317            
318    //    if(sorted_tracks)sorted_tracks->Clear();
319    //    sorted_tracks.Clear();
320    
321        if(tsorted){
322            tsorted->Delete();
323        }
324        if(timage){
325            timage->Delete();
326        }
327    };
328    
329    void PamLevel2::Reset(){
330      //
331      // First of all clear everything
332      //
333      Clear();
334      //
335      // close and reset chains and pointers
336      //    
337      if ( pam_tree ){
338        //
339        // 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...
340        //
341        TList *temp = pam_tree->GetListOfFriends();
342        TList *contents  = new TList; // create chain friend list
343        contents->SetOwner();
344        TIter next(temp);
345        TChain *questo = 0;
346        while ( (questo = (TChain*) next()) ){
347          TString name =  questo->GetName();
348          contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
349        };
350        //
351        // deleting the main chain
352        //
353        pam_tree->Delete();
354        //
355        // deleting the friends...
356        //
357        TIter next2(contents);
358        TChain *questa = 0;
359        while ( questa = (TChain*)next2() ){
360          TString name =  questa->GetName();
361          questa->Delete();      
362          questa=NULL;
363        };
364        //
365      };
366      pam_tree = NULL;
367      //
368      if(run_tree)run_tree->Delete();;
369      run_tree = NULL;
370      if(sel_tree)sel_tree->Delete();;
371      sel_tree = NULL;
372      //
373      // Close file
374      //
375      if(l0_file)l0_file->Close("R");
376      l0_file = NULL;
377      //
378      h0_obj    = 0;
379      trk0_obj  = 0;
380      calo0_obj  = 0;
381      //
382      trk2_obj  = 0;
383      trk1_obj  = 0;
384      trkh_obj  = 0;
385      calo1_obj = 0;
386      calo2_obj = 0;
387      tof_obj   = 0;
388      trig_obj  = 0;
389      s4_obj    = 0;
390      nd_obj    = 0;
391      ac_obj    = 0;
392      orb_obj   = 0;
393      //
394      // Reset run pointers
395      //
396      run_obj   = 0;//new GL_RUN();
397      soft_obj   = 0;// Emiliano
398      irun = -1;
399      runfirstentry = 0ULL;
400      runlastentry = 0ULL;    
401      //
402      totdltime[0] = 0LL;
403      totdltime[1] = 0LL;
404      //
405  };  };
406    
407    Bool_t PamLevel2::IsGood(){
408      Bool_t goodev=true;
409      //  if(trk2_obj && trk2_obj->UnpackError() != 0 ) goodev = false;
410      if(calo2_obj && calo2_obj->good != 1) goodev = false;
411      if(tof_obj && tof_obj->unpackError != 0) goodev = false;  
412      if(trig_obj && trig_obj->unpackError != 0) goodev = false;
413      if(s4_obj && s4_obj->unpackError != 0) goodev = false;  
414      if(nd_obj && nd_obj->unpackError != 0) goodev = false;  
415      if(ac_obj && ac_obj->unpackError != 255) goodev = false;  
416        //  if(orb_obj)  
417      return goodev;
418    };
419    
420  //--------------------------------------  //--------------------------------------
421  //  //
# Line 136  void PamLevel2::Clear(){ Line 424  void PamLevel2::Clear(){
424  void *PamLevel2::GetPointerTo(const char* c ){  void *PamLevel2::GetPointerTo(const char* c ){
425    
426      TString objname = c;      TString objname = c;
427      if(!objname.CompareTo("TrkLevel1"))return &trk1_obj;  
428      if(!objname.CompareTo("TrkLevel2"))return &trk2_obj;      if(!objname.CompareTo("TrkLevel1"))  {
429      if(!objname.CompareTo("TrkHough"))return &trkh_obj;          if(!trk1_obj){
430      if(!objname.CompareTo("CaloLevel1"))return &calo1_obj;              trk1_obj  = new TrkLevel1();
431      if(!objname.CompareTo("CaloLevel2"))return &calo2_obj;              trk1_obj->Set();
432      if(!objname.CompareTo("ToFLevel2"))return &tof_obj;          }
433      if(!objname.CompareTo("TrigLevel2"))return &trig_obj;          return &trk1_obj;
434      if(!objname.CompareTo("S4Level2"))return &s4_obj;      };
435      if(!objname.CompareTo("NDLevel2"))return &nd_obj;      if(!objname.CompareTo("TrkLevel2"))  {
436      if(!objname.CompareTo("AcLevel2"))return &ac_obj;          if(!trk2_obj){  
437      if(!objname.CompareTo("OrbitalInfo"))return &orb_obj;              trk2_obj  = new TrkLevel2();
438                trk2_obj->Set();
439            }
440            return &trk2_obj;
441        };
442        if(!objname.CompareTo("TrkHough"))   {
443            if(!trkh_obj)  trkh_obj  = new TrkHough();
444            return &trkh_obj;
445        };
446        if(!objname.CompareTo("CaloLevel1")) {
447            if(!calo1_obj) calo1_obj = new CaloLevel1();
448            return &calo1_obj;
449        };
450        if(!objname.CompareTo("CaloLevel2")) {
451            if(!calo2_obj){
452                calo2_obj = new CaloLevel2();
453                calo2_obj->Set();
454            };
455            return &calo2_obj;
456        };
457        if(!objname.CompareTo("ToFLevel2"))  {
458            if(!tof_obj){
459                tof_obj   = new ToFLevel2();
460                tof_obj->Set();
461            }
462            return &tof_obj;
463        };
464        if(!objname.CompareTo("TrigLevel2")) {
465            if(!trig_obj)  trig_obj  = new TrigLevel2();
466            return &trig_obj;
467        };
468        if(!objname.CompareTo("S4Level2"))   {
469            if(!s4_obj)    s4_obj    = new S4Level2();
470            return &s4_obj;
471        };
472        if(!objname.CompareTo("NDLevel2"))   {
473            if(!nd_obj)    nd_obj    = new NDLevel2();
474            return &nd_obj;
475        };
476        if(!objname.CompareTo("AcLevel2"))   {
477            if(!ac_obj)    ac_obj    = new AcLevel2();
478            return &ac_obj;
479        };
480        if(!objname.CompareTo("OrbitalInfo")){
481            if(!orb_obj)   orb_obj   = new OrbitalInfo();
482            return &orb_obj;
483        };
484        
485      if(!objname.CompareTo("RunInfo"))return &run_obj;      if(!objname.CompareTo("RunInfo"))return &run_obj;
486    
487        if(!objname.CompareTo("SoftInfo"))return &soft_obj; // Emiliano
488    
489      return NULL;      return NULL;
490  };  };
491  //--------------------------------------  //--------------------------------------
# Line 159  void *PamLevel2::GetPointerTo(const char Line 497  void *PamLevel2::GetPointerTo(const char
497   * (If seqno = -1 retrieves the self-trigger calorimeter track)   * (If seqno = -1 retrieves the self-trigger calorimeter track)
498   */   */
499   CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno){   CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno){
500                
501       if( CaloLevel2::ntrk()==0 ){       if( !calo2_obj )return 0;
502    
503         if( calo2_obj->CaloLevel2::ntrk()==0 ){
504           cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no Calorimeter tracks are stored"<<endl;           cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no Calorimeter tracks are stored"<<endl;
505           return NULL;           return NULL;
506       };       };
# Line 169  void *PamLevel2::GetPointerTo(const char Line 509  void *PamLevel2::GetPointerTo(const char
509       Int_t it_calo=0;       Int_t it_calo=0;
510            
511       do{       do{
512           c = CaloLevel2::GetCaloTrkVar(it_calo);           c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);
513           it_calo++;           it_calo++;
514       } while( c && seqno != c->trkseqno && it_calo < CaloLevel2::ntrk());             } while( c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());    
515            
516       if(!c || seqno != c->trkseqno){       if(!c || seqno != c->trkseqno){
517           c = 0;           c = 0;
# Line 190  void *PamLevel2::GetPointerTo(const char Line 530  void *PamLevel2::GetPointerTo(const char
530   */   */
531   ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno){   ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno){
532                    
533       if( ToFLevel2::ntrk()==0 ){       if( !tof_obj )return 0;
534    
535         if( tof_obj->ToFLevel2::ntrk()==0 ){
536           cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no ToF tracks are stored"<<endl;           cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no ToF tracks are stored"<<endl;
537           return NULL;           return NULL;
538       };       };
# Line 199  void *PamLevel2::GetPointerTo(const char Line 541  void *PamLevel2::GetPointerTo(const char
541       Int_t it_tof=0;       Int_t it_tof=0;
542            
543       do{       do{
544           c = ToFLevel2::GetToFTrkVar(it_tof);           c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof);
545           it_tof++;           it_tof++;
546       } while( c && seqno != c->trkseqno && it_tof < ToFLevel2::ntrk());       } while( c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk());        
547            
548       if(!c || seqno != c->trkseqno){       if(!c || seqno != c->trkseqno){
549           c = 0;           c = 0;
# Line 219  void *PamLevel2::GetPointerTo(const char Line 561  void *PamLevel2::GetPointerTo(const char
561   * Give the pamela track associated to a tracker track, retrieving related calorimeter and tof track information.   * Give the pamela track associated to a tracker track, retrieving related calorimeter and tof track information.
562   */   */
563   PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t){   PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t){
564                
565         cout <<"PamLevel2::GetPamTrackAlong(TrkTrack* t) **obsolete** "<<endl;
566         cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;
567    
568       CaloTrkVar *c = 0;       CaloTrkVar *c = 0;
569       ToFTrkVar  *o = 0;       ToFTrkVar  *o = 0;
570            
# Line 244  void *PamLevel2::GetPointerTo(const char Line 589  void *PamLevel2::GetPointerTo(const char
589    
590  PamTrack* PamLevel2::GetStoredTrack(Int_t itrk){  PamTrack* PamLevel2::GetStoredTrack(Int_t itrk){
591            
592        cout <<"PamLevel2::GetStoredTrack(Int_t itrk) **to-be-updated** "<<endl;
593        cout <<"for the moment, better use separately the methods: TrkLevel2::GetStoredTrack(seqno) CaloLevel2::GetCaloTrkVar(Int_t notrack) ToFLevel2::GetToFTrkVar(Int_t notrack)"<<endl;
594        cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;
595      PamTrack *track = 0;      PamTrack *track = 0;
596            
597      if( itrk >=0 && itrk < TrkLevel2::ntrk() ){      if( itrk >=0 && itrk < trk2_obj->TrkLevel2::ntrk() ){
598                    
599          TrkTrack *t = TrkLevel2::GetStoredTrack(itrk);          TrkTrack *t = trk2_obj->TrkLevel2::GetStoredTrack(itrk);
600          track = GetPamTrackAlong(t);          track = GetPamTrackAlong(t);
601                    
602            
603    
604      }else{      }else{
605          cout << "PamLevel2::GetStoredTrack(int) : tracker track SeqNo "<< itrk <<" does not exist (GetNTracks() = "<<TrkLevel2::GetNTracks()<<")"<<endl;          cout << "PamLevel2::GetStoredTrack(int) : tracker track SeqNo "<< itrk <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
606      };      };
607            
608      return track;      return track;
# Line 260  PamTrack* PamLevel2::GetStoredTrack(Int_ Line 610  PamTrack* PamLevel2::GetStoredTrack(Int_
610  }  }
611  //--------------------------------------  //--------------------------------------
612  //  //
613    
614    /**
615     * 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.
616     **/
617    void PamLevel2::SortTracks(TString how){
618      printf(" WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",how.Data());
619      howtosort = how;  
620      SortTracks();
621    };
622    
623  //  //
624  //--------------------------------------  //--------------------------------------
625  /**  /**
# Line 273  PamTrack* PamLevel2::GetStoredTrack(Int_ Line 633  PamTrack* PamLevel2::GetStoredTrack(Int_
633   *   *
634   * 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).
635   */   */
636  void PamLevel2::SortTracks(TString how){  void PamLevel2::SortTracks(){
637      TString how = howtosort;
638    
639  //    cout << "call SortTracs() "<<endl;    //  cout <<" PamLevel2::SortTracks(TString how) "<<endl;
640      if( !trk2_obj ){
641        cout << "void PamLevel2::SortTracks():  TrkLevel2 not loaded !!!";
642        return;
643      };
644      //    cout << "call SortTracs() "<<endl;
645    //Save current Object count    //Save current Object count
646      Int_t ObjectNumber = TProcessID::GetObjectCount();    Int_t ObjectNumber = TProcessID::GetObjectCount();
647    
648  //    cout << "ObjectNumber  "<<ObjectNumber <<endl;    //    cout << "ObjectNumber  "<<ObjectNumber <<endl;
       
     sorted_tracks->Delete(); //temporaneo???  
           
     // loop over the tracks sorted by the tracker  
     Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);  
     Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);  
     Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);  
       
     if( !CAL2 &&  use_CAL) use_CAL = false;  
     if( !TOF &&  use_TOF) use_TOF = false;  
       
     if( !TRK2 ){  
 //      cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;  
         return;  
     };  
649    
650      //   cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;    //     if(!sorted_tracks)sorted_tracks = new TRefArray();
651          //     sorted_tracks->Clear();
652      for(Int_t i=0; i < TrkLevel2::GetNTracks(); i++){    //    sorted_tracks.Clear();
653            
654          TrkTrack *ts = 0;    if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
655              tsorted->Delete();
656          // get tracker tracks    TClonesArray &ttsorted = *tsorted;
657          TrkTrack   *tp = TrkLevel2::GetTrack(i);                    //tracker    if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
658          CaloTrkVar *cp = 0;    timage->Delete();
659          ToFTrkVar  *op = 0;    TClonesArray &ttimage = *timage;
660    
661    
662    
663      //--------------------------------------------------
664      // retrieve sorting method
665      //--------------------------------------------------
666      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
667      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
668      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
669      Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase);
670      Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase);
671      Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase);
672      
673      if ( use_TOF ){
674        use_S1 = true;
675        use_S2 = true;
676        use_S3 = true;
677      };
678      if( !CAL2 &&  use_CAL) use_CAL = false;
679      if( !TOF ){
680        use_TOF = false;
681        use_S1  = false;
682        use_S2  = false;
683        use_S3  = false;
684      }
685    
686      if( !TRK2 ){
687        //  cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
688        return;
689      };
690    
691      //   cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;
692      
693    
694      //--------------------------------------------------
695      // loop over "physical" tracks sorted by the tracker
696      //--------------------------------------------------
697      for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){
698            
699        TrkTrack   *ts = 0;
700        CaloTrkVar *cs = 0;
701        ToFTrkVar  *os = 0;
702            
703        // get tracker tracks
704        TrkTrack   *tp = trk2_obj->TrkLevel2::GetTrack(i); //tracker
705        CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());
706        ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());
707    
708        TrkTrack   *ti = 0; //tracker (image)
709        CaloTrkVar *ci = 0;
710        ToFTrkVar  *oi = 0;
711        //  cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
712        // if track has an image, check image selection
713    
714        Int_t tp_score = 0;  //main track sorted by the tracker
715        Int_t ti_score = 0;  //image track
716        Int_t totp_score = 0;  //main track sorted by the tracker
717        Int_t toti_score = 0;  //image track
718    
719  //      cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;      if(tp->HasImage()){
         // if track has an image, check image selection  
         if(tp->HasImage()){  
720                            
721              TrkTrack   *ti = TrkLevel2::GetTrackImage(i);              //tracker (image)        ti = trk2_obj->TrkLevel2::GetTrackImage(i);              //tracker (image)
722              CaloTrkVar *ci = 0;        ci = GetCaloStoredTrack(ti->GetSeqNo());
723              ToFTrkVar  *oi = 0;        oi = GetToFStoredTrack(ti->GetSeqNo());
724                            
725  //          cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;        //            cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
726    
727              //assign starting scores        //assign starting scores
728              Int_t tp_score = 0;  //main track sorted by the tracker        tp_score = 0;  //main track sorted by the tracker
729              Int_t ti_score = 0;  //image track        ti_score = 0;  //image track
730                            
731              // ------------------------        // -----------------------------------------------------------------------------------------
732              // calorimeter check        // calorimeter check
733              // ------------------------        // -----------------------------------------------------------------------------------------
734              // check the Y spatial residual on the first calorimeter plane        // check the Y spatial residual on the first calorimeter plane
735              // (cut on calorimeter variables from Emiliano)        // (cut on calorimeter variables from Emiliano)
736              if(        if( use_CAL && !calo2_obj ){
737                  use_CAL            &&          cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but CaloLevel2 not loaded !!!";
738                  npcfit[1] > 15     &&   //no. of fit planes on Y view          return;
739                  varcfit[1] < 1000. &&  //fit variance on Y view        };
740                  true){        if( use_CAL && !cp &&  ci ){
741            ti_score++;
742            toti_score++;
743          };
744          if( use_CAL &&  cp && !ci ){
745            tp_score++;
746            totp_score++;
747          };
748          if(
749             use_CAL            &&
750    //       calo2_obj->npcfit[1] > 5     &&   //no. of fit planes on Y view
751    //       calo2_obj->varcfit[1] < 1000. &&  //fit variance on Y view
752             cp && ci &&
753             true){
754    
                 cp = GetCaloStoredTrack(tp->GetSeqNo());  
                 ci = GetCaloStoredTrack(ti->GetSeqNo());  
755                                    
756                  Float_t resy_p = cp->tbar[0][1] - 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;
757                  Float_t resy_i = ci->tbar[0][1] - 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;
758                                    
759                  if(resy_p <= resy_i) tp_score++;  //      if(resy_p <= resy_i) tp_score++;
760                  else                 ti_score++;  //      else                 ti_score++;
761            
762    
763            if( cp->npresh > ci->npresh){
764              tp_score++;
765              totp_score++;
766            };
767            if( cp->npresh < ci->npresh){
768              ti_score++;
769              toti_score++;
770            };
771    
772  //              cout << "CALO "<<tp_score<<ti_score<<endl;          //      cout << "CALO "<<tp_score<<ti_score<<endl;
773    
774          };
775          // -----------------------------------------------------------------------------------------
776          // TOF check
777          // -----------------------------------------------------------------------------------------
778          // check the number of hit pmts along the track
779          // on S12 S21 and S32, where paddles are parallel to Y axis
780          if( (use_TOF || use_S1 || use_S2 || use_S3 ) && !tof_obj ){
781            cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but ToFLevel2 not loaded !!!";
782            return;
783          };
784          //
785          if( (use_TOF || use_S1 || use_S2 || use_S3 ) && !op && oi ){
786            ti_score++;
787            toti_score++;
788          };
789          if( (use_TOF || use_S1 || use_S2 || use_S3 ) && op && !oi ){
790            tp_score++;
791            totp_score++;
792          };
793          if( (use_TOF || use_S1 || use_S2 || use_S3 ) && op && oi ){
794            //
795            Float_t sen = 0.;
796            for (Int_t ih=0; ih < op->npmtadc; ih++){
797              Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
798              if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (op->dedx).At(ih);
799            };
800            for (Int_t ih=0; ih < oi->npmtadc; ih++){
801              Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
802              if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (oi->dedx).At(ih);
803            };
804            //
805            if (  sen >= sortthr && false){ // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!)
806              //printf(" IS A NUCLEUS! en = %f \n",sen);
807              //
808              // is a nucleus use a different algorithm
809              //
810              Int_t nz = 6; Float_t zin[6];                                          // << define TOF z-coordinates
811              for(Int_t ip=0; ip<nz; ip++)
812                zin[ip] = tof_obj->ToFLevel2::GetZTOF(tof_obj->ToFLevel2::GetToFPlaneID(ip));     // << read ToF plane z-coordinates
813              Trajectory *tr = new Trajectory(nz,zin);
814              //
815              Int_t nphit_p =0;
816              Int_t nphit_i =0;
817              Float_t enhit_p = 0.;
818              Float_t enhit_i = 0.;
819              //
820              for (Int_t ih=0; ih < op->npmtadc; ih++){
821                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
822                if(pl == 1 || pl == 2 || pl == 5){
823                  nphit_p++;
824                  enhit_p += (op->dedx).At(ih);
825                };
826              };
827              //
828              tp->DoTrack2(tr);
829              //
830              if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
831                for (Int_t ih=0; ih < op->npmtadc; ih++){
832                  Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
833                  if(pl == 0){
834                    nphit_p++;
835                    enhit_p += (op->dedx).At(ih);
836                  };
837                };
838              };
839              if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
840                for (Int_t ih=0; ih < op->npmtadc; ih++){
841                  Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
842                  if(pl == 3){
843                    nphit_p++;
844                    enhit_p += (op->dedx).At(ih);
845                  };
846                };
847              };
848              if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
849                for (Int_t ih=0; ih < op->npmtadc; ih++){
850                  Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
851                  if(pl == 4){
852                    nphit_p++;
853                    enhit_p += (op->dedx).At(ih);
854                  };
855                };
856              };
857    
858              for (Int_t ih=0; ih < oi->npmtadc; ih++){
859                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
860                if(pl == 1 || pl == 2 || pl == 5){
861                  nphit_i++;        
862                  enhit_i += (op->dedx).At(ih);
863              };              };
864              // ------------------------            };
865              // TOF check            //
866              // ------------------------                ti->DoTrack2(tr);
867              // check the number of hit pmts along the track            //
868              // on S12 S21 and S32, where paddles are parallel to Y axis            if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
869              if( use_TOF ){              for (Int_t ih=0; ih < oi->npmtadc; ih++){
870                  Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
871                  if(pl == 0){
872                    nphit_i++;
873                    enhit_i += (op->dedx).At(ih);
874                  };
875                };
876              };
877              if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
878                for (Int_t ih=0; ih < oi->npmtadc; ih++){
879                  Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
880                  if(pl == 3){
881                    nphit_i++;
882                    enhit_i += (op->dedx).At(ih);
883                  };
884                };
885              };
886              if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
887                for (Int_t ih=0; ih < oi->npmtadc; ih++){
888                  Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
889                  if(pl == 4){
890                    nphit_i++;
891                    enhit_i += (op->dedx).At(ih);
892                  };
893                };
894              };            
895    
896                    
897              if(
898                 (use_TOF || use_S1 || use_S2 || use_S3)            &&
899                 (nphit_p+nphit_i) !=0 &&  
900                 true){
901                        
902                //      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);
903                //      printf(" score p %i score i %i \n",tp_score,ti_score);
904                //            if( enhit_p > enhit_i ) tp_score++;
905                //            if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
906                if ( nphit_p > nphit_i ) tp_score++;
907                if ( nphit_p < nphit_i ) ti_score++;
908                if ( nphit_p == nphit_i ){
909                  if ( enhit_p > enhit_i ) tp_score++;
910                  else ti_score++;
911                };
912                //      printf(" dopo score p %i score i %i \n",tp_score,ti_score);
913              };
914              delete tr;
915              //
916            } else {
917              //
918              // NOT a NUCLEUS
919              //
920              //printf(" NOT a NUCLEUS! en = %f \n",sen);
921    
922              Int_t nphit_p =0;
923              Int_t nphit_i =0;
924                                    
                 Int_t nphit_p =0;  
                 Int_t nphit_i =0;  
925                                    
926                  op = GetToFStoredTrack(tp->GetSeqNo());            /*                            cout << "track: npmtadc "<< op->npmtadc << endl;
927                  oi = GetToFStoredTrack(ti->GetSeqNo());              cout << "track: npmttdc "<< op->npmttdc << endl;
928                cout << "image: npmtadc "<< oi->npmtadc << endl;
929                cout << "image: npmttdc "<< oi->npmttdc << endl;*/
930                                    
931  /*                              cout << "track: npmtadc "<< op->npmtadc << endl;  //        for (Int_t ih=0; ih < op->npmtadc; ih++){
932                                  cout << "track: npmttdc "<< op->npmttdc << endl;  //          Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
933                                  cout << "image: npmtadc "<< oi->npmtadc << endl;  //          if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
934                                  cout << "image: npmttdc "<< oi->npmttdc << endl;*/  //        };
935                                    
936                  for (Int_t ih=0; ih < op->npmtadc; ih++){  //        for (Int_t ih=0; ih < oi->npmtadc; ih++){
937                      Int_t pl = GetPlaneIndex( (op->pmtadc).At(ih) );  //          Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
938                      if(pl == 1 || pl == 2 || pl == 5)nphit_p++;  //          if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
939                  };  //        };
940              // --- modified to count tdc signals (more efficient?)
941              // --- and to implement check on tdcflag
942              for (Int_t ih=0; ih < op->npmttdc; ih++){
943                Int_t pl = tof_obj->GetPlaneIndex( (op->pmttdc).At(ih) );
944    //          if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
945                if ( (use_S1 && ( pl == 0 || pl == 1 )) || (use_S2 && ( pl == 2 || pl == 3 )) || (use_S3 && ( pl == 4 || pl == 5 )) ){
946                  if( (op->tdcflag).At(ih)==0 )nphit_p++;
947                };
948              };
949                                    
950                  for (Int_t ih=0; ih < oi->npmtadc; ih++){            for (Int_t ih=0; ih < oi->npmttdc; ih++){
951                      Int_t pl = GetPlaneIndex( (oi->pmtadc).At(ih) );              Int_t pl = tof_obj->GetPlaneIndex( (oi->pmttdc).At(ih) );
952                      if(pl == 1 || pl == 2 || pl == 5)nphit_i++;  //          if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;  
953                  };              if ( (use_S1 && ( pl == 0 || pl == 1 )) || (use_S2 && ( pl == 2 || pl == 3 )) || (use_S3 && ( pl == 4 || pl == 5 )) ){
954                  if( (oi->tdcflag).At(ih)==0 )nphit_i++;  
955                };
956              };
957                                    
958                  if(            if(
959                      use_TOF            &&               (nphit_p+nphit_i) !=0 &&  
960                      (nphit_p+nphit_i) !=0 &&                   true){
                     true){  
961                                            
962                      if( nphit_p >= nphit_i) tp_score++;              if ( nphit_p != nphit_i ){
963                      else ti_score++;                totp_score += nphit_p;
964                  };                toti_score += nphit_i;
965  //              cout << "TOF "<<tp_score<<ti_score<<endl;                tp_score+=nphit_p;
966              };                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;  
967              };              };
968                //      if     ( nphit_p > nphit_i) tp_score+=nphit_p;
969                //      else if( nphit_p < nphit_i) ti_score+=nphit_i;
970                //      else ;//niente
971              };
972            };
973            //      cout << "TOF "<<tp_score<<ti_score<<endl;
974          };
975    
976    
977    //      if(tp_score == ti_score) use_TRK = true;
978    
979    
980          // -----------------------------------------------------------------------------------------
981          // tracker check
982          // -----------------------------------------------------------------------------------------
983          // chi**2 difference is not always large enough to distinguish among
984          // the real track and its image.
985          // Tracker check will be applied always when calorimeter and tof information is ambiguous.
986          // *** MODIFIED ON AUGUST 2007 ***
987          if(use_TRK){
988    //      if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
989    //      else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
990    
991              // CHECK 1 : number of points along X  
992            if ( tp->GetNX() >= ti->GetNX() ){
993              tp_score++ ;
994              totp_score++ ;
995            };
996            if ( tp->GetNX() <= ti->GetNX() ){
997              ti_score++ ;
998              toti_score++ ;
999            };
1000              // CHECK 2 : number of points along Y  
1001            if ( tp->GetNY() >= ti->GetNY() ){
1002              tp_score++ ;
1003              totp_score++ ;
1004            };
1005            if ( tp->GetNY() <= ti->GetNY() ){
1006              ti_score++ ;
1007              toti_score++ ;
1008            };
1009              // CHECK 3 : chi**2 along X  
1010    //        if(      tp->GetChi2X() > 0 && (tp->GetChi2X() < ti->GetChi2X() || ti->GetChi2X() <=0) ) tp_score++ ;
1011    //        if(      ti->GetChi2X() > 0 && (ti->GetChi2X() < tp->GetChi2X() || tp->GetChi2X() <=0) ) ti_score++ ;
1012              // CHECK 4 : chi**2 along Y  
1013    //        if(      tp->GetChi2Y() > 0 && (tp->GetChi2Y() < ti->GetChi2Y() || ti->GetChi2Y() <=0) ) tp_score++ ;
1014    //        if(      ti->GetChi2Y() > 0 && (ti->GetChi2Y() < tp->GetChi2Y() || tp->GetChi2Y() <=0) ) ti_score++ ;
1015              // CHECK 5 : total chi**2  
1016    //        if(      tp->chi2 > 0 && (tp->chi2 < ti->chi2 || ti->chi2 <=0) ) tp_score++ ;
1017    //        if(      ti->chi2 > 0 && (ti->chi2 < tp->chi2 || tp->chi2 <=0) ) ti_score++ ;
1018    
1019            //      cout << "TRK "<<tp_score<<ti_score<<endl;
1020          };
1021                            
1022              // ------------------------  
1023              // the winner is....  
1024              // ------------------------            // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1025              if      (tp_score > ti_score) ts = tp;//the track sorted by the tracker!!        // the winner is....
1026              else if (tp_score < ti_score) ts = ti;//its image!!        // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1027              else {        if      (tp_score > ti_score) {
1028                  ts = tp;          
1029  //                              cout << "Warning - track image ambiguity not solved" << endl;  
1030  //                              cout << ts->GetNtot() << " "<< ts->chi2 << " " << npcfit[1] << " "<< nphit_p << endl;        }else if (tp_score < ti_score) {
1031              };  
1032    
1033            ts = ti;//its image!!
1034            cs = ci;
1035            os = oi;
1036    
1037            ti = tp;//its image!!
1038            ci = cp;
1039            oi = op;
1040    
1041            tp = ts;//its image!!
1042            cp = cs;
1043            op = os;
1044    
1045                    
1046          }else {
1047    
1048    //      cout << "Warning - track image ambiguity not solved" << endl;
1049    
1050          };
1051                            
1052          }else{      }else{
1053              ts = tp;        totp_score = 1;
1054          };        toti_score = 0;
1055                  
1056  //      cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;        //            ts = tp;
1057          sorted_tracks->Add(ts);//save the track in the sorted array        //            cs = cp;
1058  //      cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;        //            os = op;
1059                };
1060      };          
1061      //Restore Object count      //  cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
1062      //To save space in the table keeping track of all referenced objects      //  sorted_tracks->Add(ts);//save the track in the sorted array
1063      //we assume that our events do not address each other. We reset the      //  sorted_tracks.Add(ts);//save the track in the sorted array
1064      //object count to what it was at the beginning of the event.      //  sorted_tracks.Add(tp);//save the track in the sorted array
1065      TProcessID::SetObjectCount(ObjectNumber);      //  cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
1066        //  cout<<"o "<<tp<<endl;
1067        //  cout<<"o "<<cp<<endl;
1068        //  cout<<"o "<<op<<endl;
1069    
1070        new(ttsorted[i]) PamTrack(tp,cp,op);
1071        new(ttimage[i])  PamTrack(ti,ci,oi);
1072    
1073        ((PamTrack*)(ttsorted[i]))->SetPScore(totp_score);
1074        ((PamTrack*)(ttimage[i]))->SetIScore(toti_score);
1075      };
1076    
1077      if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){
1078        cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() "<<tsorted->GetEntries()<<" != trk2_obj->GetNTracks() = "<<trk2_obj->GetNTracks() <<endl;
1079        tsorted->Delete(); tsorted=0;
1080        timage->Delete(); timage=0;
1081      }
1082    
1083      //Restore Object count
1084      //To save space in the table keeping track of all referenced objects
1085      //We reset the object count to what it was at the beginning of the event.
1086      TProcessID::SetObjectCount(ObjectNumber);
1087            
1088  };  };
1089  //--------------------------------------  //--------------------------------------
# Line 430  void PamLevel2::SortTracks(TString how){ Line 1094  void PamLevel2::SortTracks(TString how){
1094   * This method overrides TrkLevel2::GetTracks(), where sorting is done by decreasing number of fit points and increasing chi^2.   * This method overrides TrkLevel2::GetTracks(), where sorting is done by decreasing number of fit points and increasing chi^2.
1095   * PamLevel2::GetTracks() keeps the same track order given by TrkLevel2::GetTracks(), but checks image selection by using calorimeter and ToF tracking information.   * PamLevel2::GetTracks() keeps the same track order given by TrkLevel2::GetTracks(), but checks image selection by using calorimeter and ToF tracking information.
1096   */   */
1097  TRefArray *PamLevel2::GetTracks(){  // TRefArray *PamLevel2::GetTracks(){
1098    
1099    // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1100    //     SortTracks("+CAL+TOF");
1101    // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1102    
1103    // //   return  sorted_tracks;
1104    //     return &sorted_tracks;
1105        
1106    //  };
1107    TClonesArray *PamLevel2::GetTracks(){
1108    
1109  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1110      SortTracks("+CAL+TOF");      SortTracks();
1111  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1112    
1113      return sorted_tracks;      return tsorted;
1114        
1115   };   };
1116  //--------------------------------------  //--------------------------------------
1117   //   //
# Line 448  TRefArray *PamLevel2::GetTracks(){ Line 1123  TRefArray *PamLevel2::GetTracks(){
1123   * @param it Track number, ranging from 0 to GetNTracks().   * @param it Track number, ranging from 0 to GetNTracks().
1124   */   */
1125  PamTrack *PamLevel2::GetTrack(int it){  PamTrack *PamLevel2::GetTrack(int it){
1126    
1127    //    if(!trk2_obj) return 0;
1128                    
1129    // //  *-*-*-*+-*-*-*-*-*-*-*-*-*
1130    //     SortTracks("+CAL+TOF");
1131    // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1132    // //    if(!sorted_tracks)return 0;
1133    //     if(sorted_tracks.IsEmpty())return 0;
1134    
1135    //     PamTrack *track = 0;
1136        
1137    //     if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() && it<sorted_tracks.GetEntriesFast() ){    
1138    //      TrkTrack   *t = (TrkTrack*)sorted_tracks.At(it);
1139    //      track = GetPamTrackAlong(t);
1140    //     }else{
1141    //      cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1142    //     };
1143        
1144    //     return track;
1145    
1146    //    cout << "PamLevel2::GetTrack(int it) "<<endl;
1147  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1148      SortTracks("+CAL+TOF");      SortTracks();
1149  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1150            if(!tsorted)return 0;
1151        if(!tsorted->GetEntries())return 0;
1152    
1153      PamTrack *track = 0;      PamTrack *track = 0;
1154            
1155      if( it >=0 && it < TrkLevel2::GetNTracks() && it<sorted_tracks->GetEntriesFast() ){      if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks()){
1156          TrkTrack   *t = (TrkTrack*)sorted_tracks->At(it);  //      TrkTrack   *t = (TrkTrack*)sorted_tracks.At(it);
1157          track = GetPamTrackAlong(t);  //      track = GetPamTrackAlong(t);
1158            TClonesArray &t = *(tsorted);
1159            track = (PamTrack*)t[it];
1160            
1161      }else{      }else{
1162          cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<TrkLevel2::GetNTracks()<<")"<<endl;          cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1163      };      };
1164            
1165      return track;      return track;
# Line 476  PamTrack *PamLevel2::GetTrack(int it){ Line 1176  PamTrack *PamLevel2::GetTrack(int it){
1176   */   */
1177  PamTrack *PamLevel2::GetTrackImage(int it){  PamTrack *PamLevel2::GetTrackImage(int it){
1178    
1179    //     if(!trk2_obj) return 0;
1180    
1181    // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1182    //     SortTracks("+CAL+TOF");
1183    // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1184    // //    if(!sorted_tracks)return 0;
1185    //     if(sorted_tracks.IsEmpty())return 0;
1186        
1187    //     PamTrack *image = 0;
1188        
1189    //     if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() ){
1190    //      TrkTrack *temp = (TrkTrack*)sorted_tracks.At(it);
1191    //      if( temp->HasImage() ){
1192    //          TrkTrack   *t = trk2_obj->TrkLevel2::GetStoredTrack(temp->GetImageSeqNo());
1193    //          image = GetPamTrackAlong(t);
1194    //      }else{
1195    //          cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;
1196    //      };
1197    //     }else{
1198    //      cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1199    //     };
1200        
1201    //     return image;
1202    
1203    
1204  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1205      SortTracks("+CAL+TOF");      SortTracks();
1206  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1207        if(!timage)return 0;
1208        if(!timage->GetEntries())return 0;
1209            
1210      PamTrack *image = 0;      PamTrack *image = 0;
1211            
1212      if( it >=0 && it < TrkLevel2::GetNTracks() ){      if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() ){
1213          TrkTrack *temp = (TrkTrack*)sorted_tracks->At(it);          TClonesArray &t = *(tsorted);
1214          if( temp->HasImage() ){          PamTrack *temp = (PamTrack*)t[it];
1215              TrkTrack   *t = TrkLevel2::GetStoredTrack(temp->GetImageSeqNo());          if( temp->GetTrkTrack()->HasImage() ){
1216              image = GetPamTrackAlong(t);              TClonesArray &t = *(timage);
1217                image = (PamTrack*)t[it];
1218          }else{          }else{
1219              cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;  //          cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;
1220          };          };
1221      }else{      }else{
1222          cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<TrkLevel2::GetNTracks()<<")"<<endl;          cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1223      };      };
1224            
1225      return image;      return image;
# Line 507  PamTrack *PamLevel2::GetTrackImage(int i Line 1235  PamTrack *PamLevel2::GetTrackImage(int i
1235   * @param detlist String to select trees to be included   * @param detlist String to select trees to be included
1236   * @return Pointer to a TTree   * @return Pointer to a TTree
1237   */   */
1238  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist="+ALL"){  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ){
1239    
1240  //      cout << "WARNING!!! -- obsolete method -- \n";  //     if( !detlist.IsNull() ) SetWhichTrees(detlist);
1241  //      cout << "better use TChain *PamLevel2::GetPamTree(TList*, TString) \n";  //     else                    GetWhichTrees(f);
1242      if ( pam_tree ){
1243        printf("WARNING: TTree *PamLevel2::GetPamTree(TFile *fl, TString detlist) -- pam_tree already exists!\n ");
1244        return pam_tree;
1245      };
1246      //
1247    
1248        cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;
1249    
1250      SetWhichTrees(detlist);      SetWhichTrees(detlist);
     TTree *Trout =0;  
1251    
1252        TTree *Trout =0;
1253    
1254      TString fname = f->GetName();      TString fname = f->GetName();
1255      if(!CheckLevel2File(fname))return NULL;      if(!CheckLevel2File(fname))return NULL;
1256    
1257        //    UInt_t *found=0;    
1258    
1259        cout<< "GetPamTree(TFile*,TString): detector list --> ";
1260        if(TRK1)cout<<"TRK1 ";
1261        if(TRK2)cout<<"TRK2 ";
1262        if(TRKh)cout<<"TRKH ";
1263        if(CAL1)cout<<"CAL1 ";
1264        if(CAL2)cout<<"CAL2 ";
1265        if(TOF)cout<<"TOF ";
1266        if(TRG)cout<<"TRG ";
1267        if(AC)cout<<"AC ";
1268        if(ND)cout<<"ND ";
1269        if(S4)cout<<"S4 ";
1270        if(ORB)cout<<"ORB ";
1271        cout << endl;
1272        if(SELLI)cout<<">>> Found selection-list <<<"<<endl;
1273    
1274        f->cd();
1275    
1276  // Tracker  // Tracker
1277      TTree *T = (TTree*)f->Get("Tracker");      TTree *T = (TTree*)f->Get("Tracker");
1278      if(T && (TRK2||TRK1||TRKh)) {      if(T && (TRK2||TRK1||TRKh)) {
1279          if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));          if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
1280    //      else    T->SetBranchStatus("TrkLevel2",0,found);
1281          if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;          if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;
1282          if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));          if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
1283    //      else    T->SetBranchStatus("TrkLevel1",0,found);
1284          if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;          if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;
1285          if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));          if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1286    //      else    T->SetBranchStatus("TrkHough",0,found);
1287          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;
1288          if(!Trout)Trout=T;          if(!Trout)Trout=T;
1289          else Trout->AddFriend(T);          else Trout->AddFriend(T);
# Line 538  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1294  TTree *PamLevel2::GetPamTree(TFile *f, T
1294      TTree *C = (TTree*)f->Get("Calorimeter");      TTree *C = (TTree*)f->Get("Calorimeter");
1295      if(C && (CAL2||CAL1)) {      if(C && (CAL2||CAL1)) {
1296          if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));          if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1297    //      else    C->SetBranchStatus("CaloLevel2",0,found);
1298          if(CAL2)cout << "Calorimeter  : set branch address CaloLevel2"<<endl;          if(CAL2)cout << "Calorimeter  : set branch address CaloLevel2"<<endl;
1299          if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));          if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1300    //      else    C->SetBranchStatus("CaloLevel1",0,found);
1301          if(CAL1)cout << "Calorimeter  : set branch address CaloLevel1"<<endl;          if(CAL1)cout << "Calorimeter  : set branch address CaloLevel1"<<endl;
1302          if(!Trout)Trout=C;          if(!Trout)Trout=C;
1303          else Trout->AddFriend(C);          else Trout->AddFriend(C);
1304      }else{      }else{
1305          cout << "Calorimeter  : missing tree"<<endl;          cout << "Calorimeter  : missing tree"<<endl;
1306      };      };
1307    
1308      // ToF          // ToF    
1309      TTree *O = (TTree*)f->Get("ToF");      TTree *O = (TTree*)f->Get("ToF");
1310      if(O && TOF) {      if(O && TOF) {
# Line 561  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1320  TTree *PamLevel2::GetPamTree(TFile *f, T
1320      if(R && TRG) {      if(R && TRG) {
1321          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1322          cout << "Trigger      : set branch address TrigLevel2"<<endl;          cout << "Trigger      : set branch address TrigLevel2"<<endl;
1323          if(!Trout)Trout=O;          if(!Trout)Trout=R;
1324          else Trout->AddFriend(R);          else Trout->AddFriend(R);
1325      }else{      }else{
1326          cout << "Trigger      : missing tree"<<endl;          cout << "Trigger      : missing tree"<<endl;
# Line 571  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1330  TTree *PamLevel2::GetPamTree(TFile *f, T
1330      if(S && S4) {      if(S && S4) {
1331          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1332          cout << "S4           : set branch address S4Level2"<<endl;          cout << "S4           : set branch address S4Level2"<<endl;
1333          if(!Trout)Trout=O;          if(!Trout)Trout=S;
1334          else Trout->AddFriend(S);          else Trout->AddFriend(S);
1335      }else{      }else{
1336          cout << "S4           : missing tree"<<endl;          cout << "S4           : missing tree"<<endl;
# Line 581  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1340  TTree *PamLevel2::GetPamTree(TFile *f, T
1340      if(N && ND) {      if(N && ND) {
1341          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1342          cout << "NeutronD     : set branch address NDLevel2"<<endl;          cout << "NeutronD     : set branch address NDLevel2"<<endl;
1343          if(!Trout)Trout=O;          if(!Trout)Trout=N;
1344          else Trout->AddFriend(N);          else Trout->AddFriend(N);
1345      }else{      }else{
1346          cout << "NeutronD     : missing tree"<<endl;          cout << "NeutronD     : missing tree"<<endl;
# Line 591  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1350  TTree *PamLevel2::GetPamTree(TFile *f, T
1350      if(A && AC) {      if(A && AC) {
1351          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1352          cout << "Anticounter  : set branch address AcLevel2"<<endl;          cout << "Anticounter  : set branch address AcLevel2"<<endl;
1353          if(!Trout)Trout=O;          if(!Trout)Trout=A;
1354          else Trout->AddFriend(A);          else Trout->AddFriend(A);
1355      }else{      }else{
1356          cout << "Anticounter  : missing tree"<<endl;          cout << "Anticounter  : missing tree"<<endl;
1357      };      };
1358      // Orbital Info      // Orbital Info
1359          TTree *B = (TTree*)f->Get("OrbitalInfo");      TTree *B = (TTree*)f->Get("OrbitalInfo");
1360          if(B && ORB) {      if(B && ORB) {
1361                  B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1362                  cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
1363                  if(!Trout)Trout=O;          if(!Trout)Trout=B;
1364                  else Trout->AddFriend(B);          else Trout->AddFriend(B);
1365          }else{      }else{
1366                  cout << "OrbitalInfo  : missing tree"<<endl;          cout << "OrbitalInfo  : missing tree"<<endl;
1367          };      };
1368    
1369        TTree *L = (TTree*)f->Get("SelectionList");
1370        if(L && SELLI==1) {
1371    //      L->SetBranchAddress("RunEntry",&irun);
1372    //      cout << "SelectionList: set branch address RunEntry"<<endl;
1373    //      L->SetBranchAddress("EventEntry",&irunentry);
1374    //      cout << "SelectionList: set branch address EventEntry"<<endl;
1375    //      if(!Trout)Trout=O;
1376    //      else Trout->AddFriend("SelectionList");
1377            cout << " TTree *PamLevel2::GetPamTree(TFile, TString) >>> SelectionList not implemente!!!!"<<endl;
1378            sel_tree = 0;
1379        }else{
1380            cout << "SelectionList  : missing tree"<<endl;
1381        };
1382            
1383          cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;      cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;
1384                    
1385          return Trout;      pam_tree = (TChain*)Trout;
1386        
1387        return Trout;
1388            
1389  }  }
1390  //--------------------------------------  //--------------------------------------
# Line 627  TList*  PamLevel2::GetListOfLevel2Files( Line 1402  TList*  PamLevel2::GetListOfLevel2Files(
1402                    
1403      TString wdir = gSystem->WorkingDirectory();      TString wdir = gSystem->WorkingDirectory();
1404            
1405      if(ddir=="")ddir = wdir;      //    if(ddir=="")ddir = wdir;
1406  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1407      cout << "Level2 data directory : "<<  ddir << endl;      if ( ddir != ""){
1408              cout << "Level2 data directory : "<<  ddir << endl;
1409        } else {
1410          cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory " << endl;
1411        };
1412      TList *contents  = new TList; // create output list      TList *contents  = new TList; // create output list
1413      contents->SetOwner();      contents->SetOwner();
1414            
1415      char *fullpath;  //    char *fullpath;
1416    //    const char *fullpath;
1417            
1418      // if no input file list is given:        // if no input file list is given:  
1419      if ( flisttxt != "" ){      if ( flisttxt != "" ){
1420                    
1421          if( !gSystem->IsFileInIncludePath(flisttxt,&fullpath) )return 0;  //      if( !gSystem->IsFileInIncludePath(flisttxt,&fullpath) ){        
1422            //      if( !(fullpath = gSystem->FindFile("./",flisttxt)) ){
1423  //              flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));  //          cout <<"File "<<flisttxt<<" not found"<<endl;
1424          flisttxt = fullpath;  //          return 0;
1425            //      }      
1426          if( !gSystem->ChangeDirectory(ddir) )return 0;  //      flisttxt = fullpath;
1427          if ( !flisttxt.EndsWith(".root") ){
1428    
1429            flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1430    
1431            if( !gSystem->ChangeDirectory(ddir) ){
1432                cout << "Cannot change directory : "<<ddir<<endl;
1433                return 0;
1434            }
1435                    
1436          cout <<"Input file list : " << flisttxt <<endl;          cout <<"Input file list : " << flisttxt <<endl;
1437          ifstream in;          ifstream in;
1438          in.open(flisttxt, ios::in); //open input file list          in.open(flisttxt, ios::in); //open input file list
1439            if(!in.good()){
1440                cout <<" ERROR opening the file "<<endl;
1441                gSystem->ChangeDirectory(wdir); // back to the working directory
1442                return 0;
1443            }      
1444          int line=0;          int line=0;
1445          while (1) {          while (1) {
1446              TString file;              TString file;
# Line 656  TList*  PamLevel2::GetListOfLevel2Files( Line 1448  TList*  PamLevel2::GetListOfLevel2Files(
1448              if (!in.good()) break;              if (!in.good()) break;
1449              line++;              line++;
1450  //          cout <<"(1) " << file << endl;  //          cout <<"(1) " << file << endl;
             if(file.Contains("#"))file = file(0,file.First("#"));  
 //          cout <<"(2) " << file << endl;  
1451              if(file.IsNull()){              if(file.IsNull()){
1452                  cout << "-- list interrupted at line "<<line <<endl;                  cout << "-- list interrupted at line "<<line <<endl;
1453                  break;                  break;
1454              }              }
1455              if( gSystem->IsFileInIncludePath(file,&fullpath) ){              if(file.Contains("#"))file = file(0,file.First("#"));
1456    //          cout <<"(2) " << file << endl;
1457    //          if( gSystem->IsFileInIncludePath(file,&fullpath) ){
1458    //          if( (fullpath = gSystem->FindFile(ddir,file)) ){
1459                if( file.EndsWith(".root") ){
1460                  TString filedir;
1461                  if (ddir != ""){
1462                    filedir = ddir; // take the input dir
1463                  } else {
1464                    gSystem->ChangeDirectory(wdir); // back to the working directory
1465                    filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir
1466                  };
1467                    char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir),gSystem->BaseName(file));
1468                  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
1469                    delete fullpath;
1470              }else{              }
1471                  if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;  //          }else{
1472              };  //              if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;
1473    //          };
1474          };          };
1475          in.close();          in.close();
1476                  } else {
1477              if(flisttxt.Contains("#"))flisttxt = flisttxt(0,flisttxt.First("#"));
1478              char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1479              contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list
1480              delete fullpath;
1481          };        
1482      }else{      }else{
1483                    
1484          cout << "No input file list given."<<endl;          cout << "No input file list given."<<endl;
1485          cout << "Check for existing root files."<<endl;          cout << "Check for existing root files."<<endl;
1486  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;
1487                    if ( ddir == "" ){
1488              ddir = wdir;
1489              cout << "Level2 data directory : "<<  ddir << endl;
1490            };
1491    
1492          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1493          TList *temp = datadir->GetListOfFiles();          TList *temp = datadir->GetListOfFiles();
1494  //              temp->Print();  //              temp->Print();
# Line 690  TList*  PamLevel2::GetListOfLevel2Files( Line 1501  TList*  PamLevel2::GetListOfLevel2Files(
1501          while ( (questo = (TSystemFile*) next()) ) {          while ( (questo = (TSystemFile*) next()) ) {
1502              TString name =  questo-> GetName();              TString name =  questo-> GetName();
1503              if( name.EndsWith(".root") ){              if( name.EndsWith(".root") ){
1504                  char *fullpath;  //              const char *fullpath = gSystem->FindFile(ddir,name);
1505                  gSystem->IsFileInIncludePath(name,&fullpath);  //              char *fullpath;
1506    //              gSystem->IsFileInIncludePath(name,&fullpath);
1507                    char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir),gSystem->BaseName(name));
1508                  contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));                  contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));
1509                    delete fullpath;
1510              };              };
1511          }          }
1512          delete temp;          delete temp;
# Line 702  TList*  PamLevel2::GetListOfLevel2Files( Line 1516  TList*  PamLevel2::GetListOfLevel2Files(
1516      gSystem->ChangeDirectory(wdir); // back to the working directory      gSystem->ChangeDirectory(wdir); // back to the working directory
1517  //      cout << endl << "Selected files:" << endl;  //      cout << endl << "Selected files:" << endl;
1518  //      contents->Print();  //      contents->Print();
1519      cout << contents->GetEntries()<<" files selected\n";      cout << contents->GetEntries()<<" files \n";
1520  //      cout << endl;  //      cout << endl;
1521  //      cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;  //      cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;
1522      return contents;      return contents;
# Line 717  TList*  PamLevel2::GetListOfLevel2Files( Line 1531  TList*  PamLevel2::GetListOfLevel2Files(
1531   * @param detlist String to select trees to be included   * @param detlist String to select trees to be included
1532   * @return Pointer to a TChain   * @return Pointer to a TChain
1533   */   */
1534  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist="+ALL"){  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){
1535      //
1536  //      TChain *Tout=0;    //
1537  //    if(Tout)Tout->Delete();    //
1538  //    Tout = NULL;    if ( pam_tree ){
1539        printf("WARNING: TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) -- pam_tree already exists!\n ");
1540        return pam_tree;
1541      };
1542      //
1543            
1544      TChain *Tout =0;      TChain *Trout =0;
1545    
1546    //    if( !detlist.IsNull() )SetWhichTrees(detlist);
1547      SetWhichTrees(detlist);      SetWhichTrees(detlist);
1548            
1549      TChain *T = 0;            cout<< "GetPamTree(TList*,TString): input detector list --> ";
1550      TChain *C = 0;      if(TRK1)cout<<"TRK1 ";
1551      TChain *O = 0;      if(TRK2)cout<<"TRK2 ";
1552      TChain *R = 0;      if(TRKh)cout<<"TRKH ";
1553      TChain *S = 0;      if(CAL1)cout<<"CAL1 ";
1554      TChain *N = 0;      if(CAL2)cout<<"CAL2 ";
1555      TChain *A = 0;      if(TOF)cout<<"TOF ";
1556      TChain *B = 0;      if(TRG)cout<<"TRG ";
1557        if(AC)cout<<"AC ";
1558        if(ND)cout<<"ND ";
1559        if(S4)cout<<"S4 ";
1560        if(ORB)cout<<"ORB ";
1561        cout << endl;
1562    
1563         TChain *T = 0;    
1564         TChain *C = 0;
1565         TChain *O = 0;
1566         TChain *R = 0;
1567         TChain *S = 0;
1568         TChain *N = 0;
1569         TChain *A = 0;
1570         TChain *B = 0;
1571    
1572         TChain *L = 0;
1573            
1574      if(TRK2||TRK1||TRKh) T = new TChain("Tracker");          if(TRK2||TRK1||TRKh) T = new TChain("Tracker");    
1575      if(CAL2||CAL1) C = new TChain("Calorimeter");      if(CAL2||CAL1)       C = new TChain("Calorimeter");
1576      if(TOF) O = new TChain("ToF");      if(TOF)              O = new TChain("ToF");
1577      if(TRG) R = new TChain("Trigger");      if(TRG)              R = new TChain("Trigger");
1578      if(S4)  S = new TChain("S4");      if(S4)               S = new TChain("S4");
1579      if(ND)  N = new TChain("NeutronD");      if(ND)               N = new TChain("NeutronD");
1580      if(AC)  A = new TChain("Anticounter");      if(AC)               A = new TChain("Anticounter");
1581      if(ORB) B = new TChain("OrbitalInfo");      if(ORB)              B = new TChain("OrbitalInfo");
1582         L = new TChain("SelectionList");
1583            
1584      // loop over files and create chains              // loop over files and create chains        
1585      TIter next(fl);      TIter next(fl);
1586      TSystemFile *questo = 0;      TSystemFile *questo = 0;
1587      while ( (questo = (TSystemFile*) next()) ) {      while ( (questo = (TSystemFile*) next()) ) {
1588          TString name =  questo->GetName();          TString name =  questo->GetName();
1589  //              cout << "File: "<< name << endl;          cout << "File: "<< name << endl;
1590          if( CheckLevel2File(name) ){          if( CheckLevel2File(name) ){
1591              if(TRK2||TRK1||TRKh) T->Add(name);              if(TRK2||TRK1||TRKh) T->Add(name);
1592              if(CAL1||CAL2) C->Add(name);              if(CAL1||CAL2)       C->Add(name);
1593              if(TOF) O->Add(name);              if(TOF)              O->Add(name);
1594              if(TRG) R->Add(name);              if(TRG)              R->Add(name);
1595              if(S4)  S->Add(name);              if(S4)               S->Add(name);
1596              if(ND)  N->Add(name);              if(ND)               N->Add(name);
1597              if(AC)  A->Add(name);              if(AC)               A->Add(name);
1598              if(ORB) B->Add(name);              if(ORB)              B->Add(name);
1599                if(SELLI==1)         L->Add(name);
1600          };          };
1601      }      };
1602            
1603      cout << "done chain \n";      cout << "done chain \n";
1604            cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
1605    
1606    //    UInt_t *found=0;
1607    // Tracker
1608        if(T && (TRK2||TRK1||TRKh)) {
1609            if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
1610    //      else    T->SetBranchStatus("TrkLevel2",0,found);
1611            if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;
1612            if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
1613    //      else    T->SetBranchStatus("TrkLevel1",0,found);
1614            if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;
1615            if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1616    //      else    T->SetBranchStatus("TrkHough",0,found);
1617            if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;
1618            if(!Trout)Trout=T;
1619            else Trout->AddFriend("Tracker");
1620        }else{
1621            cout << "Tracker      : missing tree"<<endl;
1622        };
1623      // Calorimeter      // Calorimeter
1624      if(CAL2||CAL1) {                  if(C && (CAL2||CAL1)) {
1625          if(!Tout)Tout=C;          if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1626          else Tout->AddFriend("Calorimeter");  //      else    C->SetBranchStatus("CaloLevel2",0,found);
1627            if(CAL2)cout << "Calorimeter  : set branch address CaloLevel2"<<endl;
1628            if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1629    //      else    C->SetBranchStatus("CaloLevel1",0,found);
1630            if(CAL1)cout << "Calorimeter  : set branch address CaloLevel1"<<endl;
1631            if(!Trout)Trout=C;
1632            else Trout->AddFriend("Calorimeter");
1633        }else{
1634            cout << "Calorimeter  : missing tree"<<endl;
1635      };      };
1636      // ToF          // ToF    
1637      if(TOF) {      if(O && TOF) {
1638          if(!Tout)Tout=O;          O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
1639          else Tout->AddFriend("ToF");          cout << "ToF          : set branch address ToFLevel2"<<endl;
1640            if(!Trout)Trout=O;
1641            else Trout->AddFriend("ToF");
1642        }else{
1643            cout << "ToF         : missing tree"<<endl;
1644      };      };
     // Tracker  
     if(TRK2||TRK1||TRKh){  
         if(!Tout)Tout=T;  
         else Tout->AddFriend("Tracker");  
     }  
1645      // Trigger      // Trigger
1646      if(TRG) {      if(R && TRG) {
1647          if(!Tout)Tout=R;          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1648          else Tout->AddFriend("Trigger");          cout << "Trigger      : set branch address TrigLevel2"<<endl;
1649            if(!Trout)Trout=O;
1650            else Trout->AddFriend("Trigger");
1651        }else{
1652            cout << "Trigger      : missing tree"<<endl;
1653      };      };
1654      // S4      // S4
1655      if(S4) {      if(S && S4) {
1656          if(!Tout)Tout=S;          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1657          else Tout->AddFriend("S4");          cout << "S4           : set branch address S4Level2"<<endl;
1658            if(!Trout)Trout=O;
1659            else Trout->AddFriend("S4");
1660        }else{
1661            cout << "S4           : missing tree"<<endl;
1662      };      };
1663      // Neutron Detector      // Neutron Detector
1664      if(ND) {      if(N && ND) {
1665          if(!Tout)Tout=N;          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1666          else Tout->AddFriend("NeutronD");          cout << "NeutronD     : set branch address NDLevel2"<<endl;
1667      };          if(!Trout)Trout=O;
1668      // Anticounters          else Trout->AddFriend("NeutronD");
1669      if(AC) {      }else{
1670          if(!Tout)Tout=A;          cout << "NeutronD     : missing tree"<<endl;
         else Tout->AddFriend("Anticounter");  
1671      };      };
1672      // OrbitalInfo      // Anticounters
1673      if(ORB) {      if(A && AC) {
1674          if(!Tout)Tout=B;          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1675          else Tout->AddFriend("OrbitalInfo");          cout << "Anticounter  : set branch address AcLevel2"<<endl;
1676            if(!Trout)Trout=O;
1677            else Trout->AddFriend("Anticounter");
1678        }else{
1679            cout << "Anticounter  : missing tree"<<endl;
1680        };
1681        // Orbital Info
1682        if(B && ORB) {
1683            B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1684            cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
1685            if(!Trout)Trout=O;
1686            else Trout->AddFriend("OrbitalInfo");
1687        }else{
1688            cout << "OrbitalInfo  : missing tree"<<endl;
1689        };
1690        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
1691    
1692        // Selection List
1693        if(L && SELLI==1) {
1694            cout<<">>> Found selection-list <<<"<<endl;
1695            L->SetBranchAddress("RunEntry",&irun);
1696            cout << "SelectionList: set branch address RunEntry"<<endl;
1697            L->SetBranchAddress("EventEntry",&irunentry);
1698            cout << "SelectionList: set branch address EventEntry"<<endl;
1699            sel_tree = L;
1700    //      if(!Trout)Trout=O;
1701    //      else Trout->AddFriend("SelectionList");
1702            cout << "----------------------------------------------------" <<endl;
1703        }else{
1704    //      cout << "SelectionList  : missing tree"<<endl;
1705            if(L)L->Delete();
1706      };      };
1707            
1708  //    cout<<endl<<" Number of entries: "<<Tout->GetEntries()<<endl<<endl;  //    cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;
1709        
1710  //    if( Tout->GetEntries() )PamLevel2::SetBranchAddress();      pam_tree = Trout;
1711      if( Tout->GetEntries() )PamLevel2::SetBranchAddress( Tout );  
1712            return Trout;
     return Tout;  
1713  }  }
1714    
1715    
# Line 892  void PamLevel2::SetBranchAddress(TTree * Line 1790  void PamLevel2::SetBranchAddress(TTree *
1790          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1791          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
1792      };      };
1793        // SelectionList
1794        if(SELLI==1) {
1795            t->SetBranchAddress("RunEntry", &irun);
1796            cout << "SelectionList: set branch address RunEntry"<<endl;
1797            t->SetBranchAddress("EventEntry", &irunentry);
1798            cout << "SelectionList: set branch address EventEntry"<<endl;
1799        };
1800            
1801  }  }
1802  /**  /**
# Line 913  void PamLevel2::SetBranchAddress(TChain Line 1818  void PamLevel2::SetBranchAddress(TChain
1818    
1819      // Tracker      // Tracker
1820       if(TRK2) {       if(TRK2) {
1821          t->SetBranchAddress("TrkLevel2", this->GetPointerTo("TrkLevel2"));          t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
1822          cout << "Tracker      : set branch address TrkLevel2"<<endl;          cout << "Tracker      : set branch address TrkLevel2"<<endl;
1823      };      };
1824      if(TRK1) {      if(TRK1) {
# Line 965  void PamLevel2::SetBranchAddress(TChain Line 1870  void PamLevel2::SetBranchAddress(TChain
1870          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1871          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
1872      };      };
1873        // SelectionList
1874        if(SELLI==1) {
1875            t->SetBranchAddress("RunEntry", &irun);
1876            cout << "SelectionList: set branch address RunEntry"<<endl;
1877            t->SetBranchAddress("EventEntry", &irunentry);
1878            cout << "SelectionList: set branch address EventEntry"<<endl;
1879        };
1880            
1881  }  }
1882    
 // void* PamLevel2::GetPointerToTrk()  {return &trk2_obj;};  
 // void* PamLevel2::GetPointerToTrk(int s){  
 //     switch (s) {  
 //     case 1:  return &trk1_obj;  
 //     case 2:  return &trk2_obj;  
 //     case 3:  return &trkh_obj;  
 //     default: return 0;  
 //     };  
 // };  
 // void* PamLevel2::GetPointerToCalo() {return &calo2_obj; };  
 // void*       GetPointerToCalo(int s){  
 //     switch (s) {  
 //     case 1:  return &calo1_obj;  
 //     case 2:  return &calo2_obj;  
 //     default: return 0;  
 //     };  
 // };  
 // void*       GetPointerToToF()  {return &tof_obj;  };  
 // void*       GetPointerTo("TrigLevel2") {return &trig_obj; };  
 // void*       GetPointerTo("S4Level2")   {return &s4_obj;   };  
 // void*       GetPointerTo("NDLevel2")   {return &nd_obj;   };  
 // void*       GetPointerTo("AcLevel2")   {return &ac_obj;   };  
 // void*       GetPointerTo("OrbitalInfo")  {return &orb_obj;  };  
 // void*       GetPointerTo("RunInfo")  {return &run_obj;  };  
1883    
1884  //--------------------------------------  //--------------------------------------
1885  //  //
# Line 1003  void PamLevel2::SetBranchAddress(TChain Line 1891  void PamLevel2::SetBranchAddress(TChain
1891   * @return Pointer to a TChain   * @return Pointer to a TChain
1892   */   */
1893  TChain *PamLevel2::GetRunTree(TList *fl){  TChain *PamLevel2::GetRunTree(TList *fl){
1894              //
1895      TChain *R = new TChain("Run");    //
1896      //
1897      if ( run_tree ){
1898        printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");
1899        return run_tree;
1900      };    
1901      //
1902      TChain *R = new TChain("Run");
1903            
1904      // loop over files and create chains              // loop over files and create chains        
1905      TIter next(fl);      TIter next(fl);
# Line 1016  TChain *PamLevel2::GetRunTree(TList *fl) Line 1911  TChain *PamLevel2::GetRunTree(TList *fl)
1911              R->Add(name);              R->Add(name);
1912          };          };
1913      }      }
1914    
1915        if(R->GetNtrees()){
1916            
1917      R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));          R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
1918      cout << "Run         : set branch address RunInfo"<<endl;          cout << "Run         : set branch address RunInfo"<<endl;
1919                R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
1920            cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano
1921        }else{
1922            delete R;
1923            R=0;
1924        }
1925    
1926        run_tree = R;
1927    
1928      return R;      return R;
1929            
1930  }  }
# Line 1033  TChain *PamLevel2::GetRunTree(TList *fl) Line 1938  TChain *PamLevel2::GetRunTree(TList *fl)
1938   * @return Pointer to a TTree   * @return Pointer to a TTree
1939   */   */
1940  TTree *PamLevel2::GetRunTree(TFile *f){  TTree *PamLevel2::GetRunTree(TFile *f){
1941      if ( run_tree ){
1942        printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");
1943        return run_tree;
1944      };
1945            
1946    
1947        cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;
1948    
1949      TTree *R = (TTree*)f->Get("Run");      TTree *T = (TTree*)f->Get("Run");
       
     R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));  
     cout << "Run         : set branch address RunInfo"<<endl;  
1950            
1951      return R;      if(T){
1952            T->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
1953            cout << "Run         : set branch address RunInfo"<<endl;
1954            T->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
1955            cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano
1956        }
1957    
1958        run_tree = (TChain*)T;
1959    
1960        return T;
1961            
1962  }  }
1963    /**
1964     * Update the runinfo informations (to be used to have Run infos event by event basis)
1965     * @param run Pointer to the chain/tree which contains run infos
1966     * @return true if a new run has been read, false if it is still the same run
1967     */
1968    Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev){
1969      //
1970      // check if we have already called once GetEntry, if not call it
1971      //
1972        cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) --- NON FUNZIONA BENISSIMO.... "<<endl;
1973        if(!run){
1974              cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing RunInfo tree "<<endl;
1975              return(false);        
1976        }
1977        if ( run->GetEntries() <= 0 ) return(false);
1978      //
1979      
1980      //  Int_t oldrun = irun;
1981      Long64_t oldrun = irun;
1982    
1983      // --------------------------------------
1984      // if it is a full file (not preselected)
1985      // --------------------------------------
1986      if(SELLI==0){
1987    
1988          //
1989          // the absolute time is necessary to relate the event with the run
1990          //
1991          if( !GetOrbitalInfo() && !ISGP){
1992              cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
1993              return(false);
1994          }
1995    
1996          ULong64_t abstime = 0;
1997          if( GetOrbitalInfo() )abstime=GetOrbitalInfo()->absTime;
1998    
1999    
2000          //
2001          // the first time the routine is called, set run search from the beginning
2002          //
2003          if ( irun < 0LL ){
2004              irun = 0LL;
2005              run->GetEntry(irun);
2006              runfirstentry = 0LL;
2007              runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2008              if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
2009              
2010              if( ISGP && run->GetEntries()!=1 ){
2011                  cout << "** WARNING ** simulated files are assumed to have 1 single run, not "<< run->GetEntries() << endl;
2012                  cout << "** WARNING ** run will not be updated"<<endl;
2013              }
2014    
2015          };      
2016          //
2017          if(ISGP)abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO
2018          //
2019          if ( irun == run->GetEntries()-1LL &&
2020               !(abstime >= GetRunInfo()->RUNHEADER_TIME &&
2021                 abstime <= GetRunInfo()->RUNTRAILER_TIME)
2022                ){
2023             irun = -1LL;
2024             runfirstentry = 0LL;
2025             runlastentry = -1LL;
2026           };
2027          // modificato il controllo sull'aggiornamento del run, per evitare problemi
2028          // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)
2029          //
2030          bool fromfirst = true;
2031          //
2032          while ( !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME) && irun < run->GetEntries()-1LL ){
2033    //      while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){
2034              irun++;
2035              run->GetEntry(irun);
2036              runfirstentry = runlastentry;
2037              if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runfirstentry += 1LL;
2038              runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2039    //        cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2040    //        cout << "runfirstentry "<<runfirstentry<<endl;
2041              //      printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));
2042              //      printf(" abstime %u trailertime %u \n",abstime,GetRunInfo()->RUNTRAILER_TIME);
2043              //      printf(" IDRUN %u \n",GetRunInfo()->ID);
2044              //
2045    //        prevshift = 0;
2046              //
2047              if ( irun == (Long64_t)(run->GetEntries()-1LL) && fromfirst && !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME)){
2048                  printf(" resetting irun  (it should NOT happen!!!)\n");
2049                  fromfirst = false;
2050                  irun = 0;
2051                  run->GetEntry(irun);
2052                  runfirstentry = 0ULL;
2053                  runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2054                  if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
2055              };
2056              //
2057          };
2058          //
2059          if (
2060               !(abstime >= GetRunInfo()->RUNHEADER_TIME &&
2061                 abstime <= GetRunInfo()->RUNTRAILER_TIME)
2062              ) {
2063            printf(" Something very wrong here: cannot find RUN containing absolute time %llu \n",abstime);
2064              return false;
2065          }
2066          //
2067          if ( irun == oldrun || irun >= run->GetEntries() ) return(false);
2068          //
2069          //  printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);
2070          //
2071          prevshift = 0;
2072          cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2073    //      cout << "runfirstentry "<<runfirstentry<<endl;
2074          return(true);    
2075      };
2076      // ----------------------------------------------------
2077      // if it is a preselected file (there is SelectionList)
2078      // NBNB - the event tree MUST be read first
2079      // ----------------------------------------------------
2080      if(SELLI==1){      
2081          sel_tree->GetEntry(iev);
2082    //      cout << irun << " "<< irunentry << endl;
2083          if(irun != oldrun){
2084              run->GetEntry(irun);
2085              cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2086              prevshift = 0;
2087              return true;
2088          }
2089          return false;
2090      }
2091    
2092      return false;
2093      //
2094    };
2095    
2096    Bool_t PamLevel2::UpdateRunInfo(Long64_t iev){
2097    
2098        if(!run_tree){
2099            cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded"<<endl;
2100            return false;
2101        }
2102        if ( run_tree->GetEntries() <= 0 ) {
2103            cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty"<<endl;
2104            return(false);
2105        }
2106      
2107        Int_t oldrun = irun; // store current run index
2108    
2109        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2110        // if it is a full file (not preselected)
2111        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2112        if(SELLI==0){
2113    
2114            // ---------------------------------------------------------------
2115            // increment dead and live-time counters
2116            // ---------------------------------------------------------------
2117            if( GetTrigLevel2() ){
2118                totdltime[0]+=GetTrigLevel2()->dltime[0];
2119                totdltime[1]+=GetTrigLevel2()->dltime[1];
2120            }
2121            totdltime[2]++;
2122    
2123    //      cout << setw(10)<<totdltime[0]<<setw(10)<<totdltime[1]<<setw(10)<<totdltime[2]<<endl;
2124    
2125            // ---------------------------------------------------------------
2126            // retrieve OBT and absolute time of the event
2127            // ---------------------------------------------------------------
2128            ULong64_t abstime = 0;
2129            ULong64_t obt     = 0;
2130            if( GetOrbitalInfo() ){
2131                abstime = GetOrbitalInfo()->absTime;
2132                obt     = GetOrbitalInfo()->OBT;
2133            }
2134            // ---------------------------------------------------------------
2135            // the absolute time is necessary to relate the event with the run
2136            // ---------------------------------------------------------------
2137            if( !GetOrbitalInfo() && !ISGP ){
2138                cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
2139                return(false);
2140            }
2141          
2142            // -----------------------------------------------------------------------
2143            // the first time the routine is called, set run search from the beginning
2144            // -----------------------------------------------------------------------
2145            if ( irun < 0 ){
2146                irun = 0LL;
2147                irunentry = 0;
2148                prevshift = 0;
2149                run_tree->GetEntry(irun);
2150    
2151                if( ISGP && run_tree->GetEntries()!=1 ){
2152                    cout << "** WARNING ** simulated files are assumed to have 1 single run, not "<< run_tree->GetEntries() << endl;
2153                    cout << "** WARNING ** run will not be updated"<<endl;
2154                }
2155            };      
2156    
2157            // -----------------------------------------------------------------------
2158            // if it is simulation, assigh abstime by hand (temporaneo!!!)
2159            // -----------------------------------------------------------------------
2160            if(ISGP){
2161                abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO
2162                obt     = GetRunInfo()->RUNHEADER_OBT; // BARBATRUCCO
2163            }
2164            //
2165            bool fromfirst = true; // first loop over runs
2166    
2167    //      Bool_t hasfrag = false;
2168    //      if( GetRunInfo()->ID_RUN_FRAG!=0 && GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID)hasfrag=true;
2169    //      ULong64_t fragid = GetRunInfo()->ID_RUN_FRAG;
2170    
2171            // ------------------------------------------------------
2172            // loop over runs to find the one that contains the event
2173            // ------------------------------------------------------
2174            while (
2175                (
2176                    (
2177                        !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)
2178                          abstime <= GetRunInfo()->RUNTRAILER_TIME) &&
2179                        !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)
2180                          obt <= GetRunInfo()->RUNTRAILER_OBT)
2181                        )
2182                    || GetRunInfo()->NEVENTS==0
2183                    || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift)
2184                    )
2185                    && irun < run_tree->GetEntries() ){
2186    
2187    
2188    
2189    
2190                // -----------------------------------------
2191                // store dead and live-time of previous run
2192                // -----------------------------------------
2193                if(fromfirst){
2194                    if(oldrun==irun){
2195                        /// decrement counters
2196                        if( GetTrigLevel2()){
2197                            totdltime[0]-=GetTrigLevel2()->dltime[0];//live-time
2198                            totdltime[1]-=GetTrigLevel2()->dltime[1];//dead-time
2199                        }
2200                        totdltime[2]--;                              //event counter
2201                        cout << endl;
2202                        cout << "n.events     : "<<totdltime[2]<<endl;
2203                        cout << "RUN LIVE-TIME: "<<totdltime[0]*0.16<<" ms"<<endl;
2204                        cout << "RUN DEAD-TIME: "<<totdltime[1]*0.01<<" ms"<<endl;      
2205                    }else{
2206                        totdltime[0]=0;//live-time
2207                        totdltime[1]=0;//dead-time
2208                        totdltime[2]=0;                             //event counter
2209                        cout << " *** JUMP RUN *** irun "<<irun<<endl;
2210                    }
2211                    /// add an entry
2212                    if(run_tree_clone)
2213                        if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
2214                            run_tree_clone->GetBranch("DeadLiveTime")->Fill();
2215                    /// reset counters
2216                    if( GetTrigLevel2() ){
2217                        totdltime[0]=GetTrigLevel2()->dltime[0];//live-time
2218                        totdltime[1]=0;                         //dead-time
2219                    }
2220                    totdltime[2]=1;                             //event counter
2221                }
2222    
2223    
2224                irun++;
2225                // ------------------------------------
2226                // if the end of run tree is reached...
2227                // ------------------------------------
2228                if( irun == run_tree->GetEntries() ){
2229                    if(!fromfirst){
2230                        // -----------------------------------------------------
2231                        // if it happened already once and the run was not found
2232                        // ---> exit with error
2233                        // -----------------------------------------------------
2234                        cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- event entry #"<<iev<<" does not belong to any run (should not happen)" <<endl;
2235                        return false;
2236                    }
2237                    // -----------------------------------------
2238                    // ...otherwise repeat search from beginning
2239                    // -----------------------------------------
2240                    cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- reached end of run tree. searchin again from beginning " <<endl;
2241                    fromfirst = false;
2242                    irun = 0LL;
2243                    runfirstentry = 0LL;
2244                }
2245                // -------------------------------------------------------------------
2246                // save the index of the first entry of the run, relative to pam_tree,
2247                // and read a new run
2248                // -------------------------------------------------------------------
2249                if(irun>0)runfirstentry += (GetRunInfo()->NEVENTS)-prevshift;
2250                irunentry = 0;
2251                prevshift = 0;          
2252                run_tree->GetEntry(irun);      
2253                if(GetRunInfo()->RUNHEADER_OBT>GetRunInfo()->RUNTRAILER_OBT ){
2254                    cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun "<<irun<<"  has RUNHEADER_OBT>=RUNTRAILER_OBT " <<endl;
2255                    cout << "                                                            (NB!! in this case some events are assigned to a wrong run)"<<endl;
2256                }
2257    //          if(hasfrag &&  fragid != GetRunInfo()->ID){
2258    //              cout << "... where is the next fragment ??"<<endl;
2259    //          }
2260            };
2261    
2262    
2263            // --------------------------------------
2264            // if there was no need to update the run
2265            // ---> exit with FALSE
2266            // --------------------------------------
2267            if ( irun == oldrun ) return(false);
2268    
2269            // --------------------------------------
2270            // ... otherwise
2271            // --------------------------------------
2272    
2273    
2274    
2275    //      // -----------------------------------------
2276    //      // store dead and live-time of previous run
2277    //      // -----------------------------------------
2278    //      // --------------------------------------------------------------
2279    //      // if the run is empty, fill the dead-live time branch with zeros
2280    //      // --------------------------------------------------------------
2281    //      /// if some runs have been jumped, fill with zeros
2282    //      if(irun-oldrun>1){
2283    //          ULong64_t  temp[3];
2284    //          temp[0]=totdltime[0];
2285    //          temp[1]=totdltime[1];
2286    //          temp[2]=totdltime[2];
2287    //          for(int i=oldrun+1; i<irun; irun++){
2288    //              totdltime[0]=0;
2289    //              totdltime[1]=0;
2290    //              totdltime[2]=0;
2291    //              cout << " ** JUMPED RUN **"<<endl;
2292    //              if(run_tree_clone)
2293    //                  if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
2294    //                      run_tree_clone->GetBranch("DeadLiveTime")->Fill();
2295    //          }
2296    //          totdltime[0]=temp[0];
2297    //          totdltime[1]=temp[1];
2298    //          totdltime[2]=temp[2];
2299    //      }
2300    //      /// decrement counters
2301    //      if( GetTrigLevel2() ){
2302    //          totdltime[0]-=GetTrigLevel2()->dltime[0];//live-time
2303    //          totdltime[1]-=GetTrigLevel2()->dltime[1];//dead-time
2304    //      }
2305    //      totdltime[2]--;                              //event counter
2306    //      /// add an entry
2307    //      if(irun>0 ){
2308    //          cout << endl;
2309    //          cout << "n.events     : "<<totdltime[2]<<endl;
2310    //          cout << "RUN LIVE-TIME: "<<totdltime[0]*0.16<<" ms"<<endl;
2311    //          cout << "RUN DEAD-TIME: "<<totdltime[1]*0.01<<" ms"<<endl;      
2312    //          if(run_tree_clone)
2313    //              if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
2314    //                  run_tree_clone->GetBranch("DeadLiveTime")->Fill();
2315    //      }
2316    //      /// reset counters
2317    //      if( GetTrigLevel2() ){
2318    //          totdltime[0]=GetTrigLevel2()->dltime[0];//live-time
2319    //          totdltime[1]=0;                         //dead-time
2320    //      }
2321    //      totdltime[2]=1;                             //event counter
2322            
2323    
2324            // --------------------------------------
2325            // ---> exit with TRUE
2326            // --------------------------------------
2327            cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2328            // ----------------------------------------------------
2329            // then check if the run has a fragment
2330            // in this case we have to switch to the next fragment
2331            // when the end of the first fragment is reached
2332            // ----------------------------------------------------
2333            if(
2334                GetRunInfo()->ID_RUN_FRAG != 0 &&
2335    //          GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID &&
2336                true ){
2337                cout << "* fragment *"<<endl;              
2338            }
2339    
2340            return(true);    
2341        };
2342        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2343        // if it is a preselected file (there is SelectionList)
2344        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2345        if(SELLI==1){      
2346            sel_tree->GetEntry(iev);
2347            if(irun != oldrun){
2348                run_tree->GetEntry(irun);
2349                cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2350                prevshift = 0;
2351                return true;
2352            }
2353            return false;
2354        }
2355    
2356        return false;
2357        //
2358    };
2359    /**
2360     * Update the runinfo informations (to be used to have Run infos event by event basis)
2361     * @param run Pointer to the chain/tree which contains run infos
2362     * @return true if a new run has been read, false if it is still the same run
2363     */
2364    Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev){
2365      return(UpdateRunInfo((TChain*)run,iev));
2366    };
2367    
2368  //--------------------------------------  //--------------------------------------
2369  //  //
2370  //  //
# Line 1053  TTree *PamLevel2::GetRunTree(TFile *f){ Line 2375  TTree *PamLevel2::GetRunTree(TFile *f){
2375  */  */
2376  void PamLevel2::SetWhichTrees(TString detlist){  void PamLevel2::SetWhichTrees(TString detlist){
2377                    
2378      if(detlist.Contains("+ALL", TString::kIgnoreCase)){      if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){
2379            CAL0 = false;
2380          CAL1 = true;          CAL1 = true;
2381          CAL2 = true;          CAL2 = true;
2382          TRK2 = true;          TRK2 = true;
2383          TRK1 = false;          TRK1 = false;
2384          TRKh = false;          TRKh = false;
2385            TRK0 = false;
2386          TRG = true;          TRG = true;
2387          TOF = true;          TOF = true;
2388            TOF0 = false;
2389          S4  = true;          S4  = true;
2390          ND  = true;          ND  = true;
2391          AC  = true;          AC  = true;
2392          ORB = true;          ORB = true;
2393      }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){      }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){
2394            CAL0 = false;
2395          CAL1 = false;          CAL1 = false;
2396          CAL2 = false;          CAL2 = false;
2397          TRK2 = false;          TRK2 = false;
2398          TRK1 = false;          TRK1 = false;
2399          TRKh = false;          TRKh = false;
2400            TRK0 = false;
2401          TRG = false;          TRG = false;
2402          TOF = false;          TOF = false;
2403            TOF0 = false;
2404          S4  = false;          S4  = false;
2405          ND  = false;          ND  = false;
2406          AC  = false;          AC  = false;
# Line 1084  void PamLevel2::SetWhichTrees(TString de Line 2412  void PamLevel2::SetWhichTrees(TString de
2412          if ( detlist.Contains("-CAL1", TString::kIgnoreCase) )CAL1=false;          if ( detlist.Contains("-CAL1", TString::kIgnoreCase) )CAL1=false;
2413          if ( detlist.Contains("+CAL1", TString::kIgnoreCase) )CAL1=true;          if ( detlist.Contains("+CAL1", TString::kIgnoreCase) )CAL1=true;
2414      };      };
2415    
2416        if( detlist.Contains("CAL0", TString::kIgnoreCase) ){
2417            if ( detlist.Contains("-CAL0", TString::kIgnoreCase) )CAL0=false;
2418            if ( detlist.Contains("+CAL0", TString::kIgnoreCase) )CAL0=true;
2419        };
2420                    
2421      if( detlist.Contains("CAL2", TString::kIgnoreCase)){      if( detlist.Contains("CAL2", TString::kIgnoreCase)){
2422          if ( detlist.Contains("-CAL2", TString::kIgnoreCase) )CAL2=false;          if ( detlist.Contains("-CAL2", TString::kIgnoreCase) )CAL2=false;
# Line 1096  void PamLevel2::SetWhichTrees(TString de Line 2429  void PamLevel2::SetWhichTrees(TString de
2429          CAL1=false;          CAL1=false;
2430      }      }
2431  //  -------------------------------------------------------------------------  //  -------------------------------------------------------------------------
2432        if( detlist.Contains("TRK0", TString::kIgnoreCase) ){
2433            if ( detlist.Contains("-TRK0", TString::kIgnoreCase) )TRK0=false;
2434            if ( detlist.Contains("+TRK0", TString::kIgnoreCase) )TRK0=true;
2435        };
2436    
2437      if( detlist.Contains("TRK1", TString::kIgnoreCase) ){      if( detlist.Contains("TRK1", TString::kIgnoreCase) ){
2438          if ( detlist.Contains("-TRK1", TString::kIgnoreCase) )TRK1=false;          if ( detlist.Contains("-TRK1", TString::kIgnoreCase) )TRK1=false;
2439          if ( detlist.Contains("+TRK1", TString::kIgnoreCase) )TRK1=true;          if ( detlist.Contains("+TRK1", TString::kIgnoreCase) )TRK1=true;
# Line 1118  void PamLevel2::SetWhichTrees(TString de Line 2456  void PamLevel2::SetWhichTrees(TString de
2456          TRKh=false;          TRKh=false;
2457      }      }
2458  //  -------------------------------------------------------------------------  //  -------------------------------------------------------------------------
   
   
       
 //     if( detlist.Contains("-TRK", TString::kIgnoreCase) )TRK2 = false;  
 //     else if( detlist.Contains("+TRK", TString::kIgnoreCase) )TRK2 = true;  
       
 //     if( detlist.Contains("-TRK1", TString::kIgnoreCase) )TRK1 = false;  
 //     else if( detlist.Contains("+TRK1", TString::kIgnoreCase) )TRK1 = true;  
   
 //     if( detlist.Contains("-TRKh", TString::kIgnoreCase) )TRKh = false;  
 //     else if( detlist.Contains("+TRKh", TString::kIgnoreCase) )TRKh = true;  
2459            
2460      if( detlist.Contains("-TRG", TString::kIgnoreCase) )TRG = false;      if( detlist.Contains("-TRG", TString::kIgnoreCase) )TRG = false;
2461      else if( detlist.Contains("+TRG", TString::kIgnoreCase) )TRG = true;      else if( detlist.Contains("+TRG", TString::kIgnoreCase) )TRG = true;
2462            
2463      if( detlist.Contains("-TOF", TString::kIgnoreCase) )TOF = false;      if( detlist.Contains("-TOF", TString::kIgnoreCase) )TOF = false;
2464      else if( detlist.Contains("+TOF", TString::kIgnoreCase) )TOF = true;      else if( detlist.Contains("+TOF", TString::kIgnoreCase) )TOF = true;
2465    
2466        if( detlist.Contains("-TOF0", TString::kIgnoreCase) )TOF0 = false;
2467        else if( detlist.Contains("+TOF0", TString::kIgnoreCase) )TOF0 = true;
2468            
2469      if( detlist.Contains("-S4",  TString::kIgnoreCase) )S4  = false;      if( detlist.Contains("-S4",  TString::kIgnoreCase) )S4  = false;
2470      else if( detlist.Contains("+S4",  TString::kIgnoreCase) )S4  = true;      else if( detlist.Contains("+S4",  TString::kIgnoreCase) )S4  = true;
# Line 1147  void PamLevel2::SetWhichTrees(TString de Line 2477  void PamLevel2::SetWhichTrees(TString de
2477            
2478      if( detlist.Contains("-ORB", TString::kIgnoreCase) )ORB = false;      if( detlist.Contains("-ORB", TString::kIgnoreCase) )ORB = false;
2479      else if( detlist.Contains("+ORB", TString::kIgnoreCase) )ORB = true;      else if( detlist.Contains("+ORB", TString::kIgnoreCase) )ORB = true;
2480    
2481    //     cout<< "Set detector list --> ";
2482    //     if(TRK1)cout<<"TRK1 ";
2483    //     if(TRK2)cout<<"TRK2 ";
2484    //     if(TRKh)cout<<"TRKH ";
2485    //     if(CAL1)cout<<"CAL1 ";
2486    //     if(CAL2)cout<<"CAL2 ";
2487    //     if(TOF)cout<<"TOF ";
2488    //     if(TRG)cout<<"TRG ";
2489    //     if(AC)cout<<"AC ";
2490    //     if(ND)cout<<"ND ";
2491    //     if(S4)cout<<"S4 ";
2492    //     if(ORB)cout<<"ORB ";
2493    //     cout << endl;
2494            
2495  };  };
2496    
2497    
2498    /**
2499     * Set tree/branch detector flags from the content of a tree
2500     */
2501    void  PamLevel2::GetWhichTrees(TFile* f){
2502        
2503    
2504    
2505        cout << "void  PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;
2506        // -----------
2507        // reset flags
2508        // -----------
2509        CAL1   = false;    
2510        CAL2   = false;    
2511        TRK2   = false;    
2512        TRK1   = false;    
2513        TRKh   = false;    
2514        TRG    = false;    
2515        TOF    = false;    
2516        S4     = false;    
2517        ND     = false;    
2518        AC     = false;    
2519        ORB    = false;    
2520        
2521        RUN    = false;
2522            
2523        cout << "Checking file: "<<f->GetName()<<endl;
2524        if( !f || f->IsZombie() ){
2525            cout << "File: "<< f->GetName() <<" Non valid root file"<< endl;
2526            return;
2527        }
2528    
2529        TList *lk = f->GetListOfKeys();
2530        TIter next(lk);
2531        TKey *key =0;
2532    
2533        Int_t nev = 0;
2534    
2535        while( (key = (TKey*)next()) ){
2536            
2537            if( !strcmp(key->GetName(),"Run"        ) )RUN = true;
2538    
2539            //=========================================================    
2540            if( !strcmp(key->GetName(),"Trigger"    ) ){
2541                TRG = true;
2542                Int_t nevt = ((TTree*)f->Get("Trigger"))->GetEntries();
2543                if( nev && nevt!=nev){
2544                    cout << "File: "<< f->GetName() <<" Trigger tree has "<<nevt<<" events instead of "<<nev<< endl;
2545                    TRG = false;
2546                }else nev=nevt;
2547            }
2548            //=========================================================    
2549            if( !strcmp(key->GetName(),"ToF"        ) ){
2550                TOF = true;
2551                Int_t nevt = ((TTree*)f->Get("ToF"))->GetEntries();
2552                if( nev && nevt!=nev){
2553                    cout << "File: "<< f->GetName() <<"     ToF tree has "<<nevt<<" events instead of "<<nev<< endl;
2554                    TOF = false;
2555                }else nev=nevt;
2556            }
2557            //=========================================================  
2558            if( !strcmp(key->GetName(),"S4"         ) ){
2559                S4 = true;
2560                Int_t nevt = ((TTree*)f->Get("S4"))->GetEntries();
2561                if( nev && nevt!=nev){
2562                    cout << "File: "<< f->GetName() <<"      S4 tree has "<<nevt<<" events instead of "<<nev<< endl;
2563                    S4 = false;
2564                }else nev=nevt;
2565            }
2566            //=========================================================  
2567    
2568            if( !strcmp(key->GetName(),"NeutronD"   ) ){
2569                ND = true;
2570                Int_t nevt = ((TTree*)f->Get("NeutronD"))->GetEntries();
2571                if( nev && nevt!=nev){
2572                    cout << "File: "<< f->GetName() <<"NeutronD tree has "<<nevt<<" events instead of "<<nev<< endl;
2573                    ND =false;
2574                }else nev=nevt;
2575            }      
2576            //=========================================================  
2577            if( !strcmp(key->GetName(),"Anticounter") ){
2578                AC = true;
2579                Int_t nevt = ((TTree*)f->Get("Anticounter"))->GetEntries();
2580                if( nev && nevt!=nev){
2581                    cout << "File: "<< f->GetName() <<" Anticounter tree has "<<nevt<<" events instead of "<<nev<< endl;
2582                    AC =false;
2583                }else nev=nevt;
2584            }
2585            //=========================================================  
2586            if( !strcmp(key->GetName(),"OrbitalInfo") ){
2587                ORB = true;
2588                Int_t nevt = ((TTree*)f->Get("OrbitalInfo"))->GetEntries();
2589                if( nev && nevt!=nev){
2590                    cout << "File: "<< f->GetName() <<" OrbitalInfo tree has "<<nevt<<" events instead of "<<nev<< endl;
2591                    ORB = false;
2592                }else nev=nevt;
2593            }
2594            //=========================================================  
2595            if( !strcmp(key->GetName(),"Tracker"    ) ){
2596                TTree *T = (TTree*)f->Get("Tracker");
2597                for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
2598                    TString name = T->GetListOfBranches()->At(i)->GetName();
2599                    if( !name.CompareTo("TrkLevel1") )TRK1=true;
2600                    if( !name.CompareTo("TrkLevel2") )TRK2=true;
2601                    if( !name.CompareTo("TrkHough") )TRKh=true;
2602                };      
2603                Int_t nevt = T->GetEntries();
2604                if( nev && nevt!=nev){
2605                    cout << "File: "<< f->GetName() <<" Tracker tree has "<<nevt<<" events instead of "<<nev<< endl;
2606                    TRK1 = false;
2607                    TRK2 = false;
2608                    TRKh = false;
2609                }else nev=nevt;
2610                T->Delete();
2611            };
2612            //=========================================================  
2613            if( !strcmp(key->GetName(),"Calorimeter"    ) ){
2614                TTree *T = (TTree*)f->Get("Calorimeter");
2615                for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
2616                    TString name = T->GetListOfBranches()->At(i)->GetName();
2617                    if( !name.CompareTo("CaloLevel1") )CAL1=true;
2618                    if( !name.CompareTo("CaloLevel2") )CAL2=true;
2619                };    
2620                Int_t nevt = T->GetEntries();
2621                if( nev && nevt!=nev){
2622                    cout << "File: "<< f->GetName() <<"  Calorimeter tree has "<<nevt<<" events instead of "<<nev<< endl;
2623                    CAL1 = false;
2624                    CAL2 = false;
2625                }else nev=nevt;
2626                T->Delete();
2627            };      
2628    
2629        };
2630        
2631        delete lk;
2632    
2633    //     cout<< "Get detector list from input file --> ";
2634    //     if(TRK1)cout<<"TRK1 ";
2635    //     if(TRK2)cout<<"TRK2 ";
2636    //     if(TRKh)cout<<"TRKH ";
2637    //     if(CAL1)cout<<"CAL1 ";
2638    //     if(CAL2)cout<<"CAL2 ";
2639    //     if(TOF)cout<<"TOF ";
2640    //     if(TRG)cout<<"TRG ";
2641    //     if(AC)cout<<"AC ";
2642    //     if(ND)cout<<"ND ";
2643    //     if(S4)cout<<"S4 ";
2644    //     if(ORB)cout<<"ORB ";
2645    //     cout << endl;
2646          
2647        return ;
2648            
2649    };
2650    
2651    
2652  //--------------------------------------  //--------------------------------------
2653  //  //
2654  //  //
# Line 1174  Bool_t  PamLevel2::CheckLevel2File(TStri Line 2674  Bool_t  PamLevel2::CheckLevel2File(TStri
2674            
2675      Bool_t RUN__ok    = false;      Bool_t RUN__ok    = false;
2676            
2677            Bool_t SELLI__ok  = false;
2678    
2679      cout << "Checking file: "<<name<<endl;      cout << "Checking file: "<<name<<endl;
2680      TFile *f = new TFile(name.Data());      TFile *f = new TFile(name.Data());
# Line 1192  Bool_t  PamLevel2::CheckLevel2File(TStri Line 2692  Bool_t  PamLevel2::CheckLevel2File(TStri
2692      while( (key = (TKey*)next()) ){      while( (key = (TKey*)next()) ){
2693                    
2694  //      cout << key->GetName() << endl;  //      cout << key->GetName() << endl;
2695  //      cout << " Get tree: " << f->Get(key->GetName())<<endl;  //      cout << key->GetName() << ""<<key->GetClassName()<<endl;
2696    //              cout << " Get tree: " << f->Get(key->GetName())<<endl;
2697  //      nev_previous = nev;  //      nev_previous = nev;
2698  //      cout << " n.entries  "<< nev <<endl;  //      cout << " n.entries  "<< nev <<endl;
2699  //      if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){  //      if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){
# Line 1201  Bool_t  PamLevel2::CheckLevel2File(TStri Line 2702  Bool_t  PamLevel2::CheckLevel2File(TStri
2702  //          return false;  //          return false;
2703  //      };  //      };
2704    
2705    
2706          if( !strcmp(key->GetName(),"Run"        ) )RUN__ok = true;                if( !strcmp(key->GetName(),"Run"        ) )RUN__ok = true;      
2707    
2708          //=========================================================              //=========================================================    
2709            if( !strcmp(key->GetName(),"SelectionList"    ) ){
2710                SELLI__ok = true;
2711                if(SELLI==1){
2712                    Int_t nevt = ((TTree*)f->Get("SelectionList"))->GetEntries();
2713                    if( nev && nevt!=nev){
2714                        cout << "File: "<< f->GetName() <<" discarded ---- SelectionList tree has "<<nevt<<" events instead of "<<nev<< endl;
2715                        return false;
2716                    }
2717                    nev=nevt;
2718                }
2719            }
2720    
2721            //=========================================================    
2722          if( !strcmp(key->GetName(),"Trigger"    ) ){          if( !strcmp(key->GetName(),"Trigger"    ) ){
2723              TRG__ok = true;              TRG__ok = true;
2724              if(TRG){              if(TRG){
# Line 1315  Bool_t  PamLevel2::CheckLevel2File(TStri Line 2830  Bool_t  PamLevel2::CheckLevel2File(TStri
2830          };                };      
2831    
2832      };      };
2833    
2834        if( SELLI==-1 )SELLI = (Int_t)SELLI__ok;
2835        if( SELLI==0 && SELLI__ok ){
2836            cout << "File: "<< f->GetName() <<" discarded ---- found SelectionList (it is not a full-event file)" << endl;
2837            return false;  
2838        }
2839        if( SELLI==1 && !SELLI__ok ){
2840            cout << "File: "<< f->GetName() <<" discarded ---- SelectionList missing" << endl;
2841            return false;
2842        }
2843            
2844    //    cout << "SELLI "<<SELLI<<endl;
2845    
2846    //     cout<< "CheckLevel2File(TString): detector list --> ";
2847    //     if(TRK1__ok)cout<<"TRK1 ";
2848    //     if(TRK2__ok)cout<<"TRK2 ";
2849    //     if(TRKh__ok)cout<<"TRKH ";
2850    //     if(CAL1__ok)cout<<"CAL1 ";
2851    //     if(CAL2__ok)cout<<"CAL2 ";
2852    //     if(TOF__ok)cout<<"TOF ";
2853    //     if(TRG__ok)cout<<"TRG ";
2854    //     if(AC__ok)cout<<"AC ";
2855    //     if(ND__ok)cout<<"ND ";
2856    //     if(S4__ok)cout<<"S4 ";
2857    //     if(ORB__ok)cout<<"ORB ";
2858    //     cout << endl;
2859    
2860    
2861        if(TRK2 && TRK1__ok)TRK1=1;
2862    // ----------------------------------------------------------------------------
2863    // NOTA
2864    // se c'e` il level1, lo devo necessarimente leggere.
2865    // infatti (non ho capito perche`) i cluster vengono letti e allocati in memoria
2866    // comunque, ma non vengono disallocati da PamLevel2::Clear()
2867    // ----------------------------------------------------------------------------
2868    
2869    
2870      if(!RUN__ok) {      if(!RUN__ok) {
# Line 1360  Bool_t  PamLevel2::CheckLevel2File(TStri Line 2909  Bool_t  PamLevel2::CheckLevel2File(TStri
2909          return false;          return false;
2910      };      };
2911    
2912          if(ND && !ND__ok){      if(ND && !ND__ok){
2913              cout << "File: "<< f->GetName() <<" discarded ---- Missing ND tree"<< endl;          cout << "File: "<< f->GetName() <<" discarded ---- Missing ND tree"<< endl;
2914              return false;          return false;
2915          };      };
2916          if(TRG && !TRG__ok){      if(TRG && !TRG__ok){
2917              cout << "File: "<< f->GetName() <<" discarded ---- Missing Trigger tree"<< endl;          cout << "File: "<< f->GetName() <<" discarded ---- Missing Trigger tree"<< endl;
2918              return false;          return false;
2919          };      };
2920        
   
   
   
2921    
2922      lk->Delete();  //    lk->Delete();
2923    //    delete lk;
2924      f->Close();      f->Close();
2925    
2926    //     cout<< "CheckLevel2File(TString): detector list --> ";
2927    //     if(TRK1)cout<<"TRK1 ";
2928    //     if(TRK2)cout<<"TRK2 ";
2929    //     if(TRKh)cout<<"TRKH ";
2930    //     if(CAL1)cout<<"CAL1 ";
2931    //     if(CAL2)cout<<"CAL2 ";
2932    //     if(TOF)cout<<"TOF ";
2933    //     if(TRG)cout<<"TRG ";
2934    //     if(AC)cout<<"AC ";
2935    //     if(ND)cout<<"ND ";
2936    //     if(S4)cout<<"S4 ";
2937    //     if(ORB)cout<<"ORB ";
2938    //     cout << endl;
2939                
2940      return true;      return true;
2941                    
# Line 1384  Bool_t  PamLevel2::CheckLevel2File(TStri Line 2945  Bool_t  PamLevel2::CheckLevel2File(TStri
2945  /**  /**
2946   * Create clone-trees   * Create clone-trees
2947   */   */
2948  void PamLevel2::CreateCloneTrees( TChain *fChain ){  void PamLevel2::CreateCloneTrees0( TChain *fChain, TFile *ofile ){
2949    
2950      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2951      cout << "Create clones of PAMELA trees "<<endl;      cout << "Create clones of PAMELA trees "<<endl;
2952            
2953      Int_t i=0;      Int_t i=0;
2954      tree_clone[i] = fChain->GetTree()->CloneTree(0);      pam_tree_clone[i] = fChain->GetTree()->CloneTree(0);
2955      TString name =  tree_clone[i]->GetName();      TString name =  pam_tree_clone[i]->GetName();
2956      name.Append("_clone");      name.Append("_clone");
2957  //    tree_clone[i]->SetName(name.Data());  //    pam_tree_clone[i]->SetName(name.Data());
2958      cout << tree_clone[i]->GetName() <<endl;      cout << pam_tree_clone[i]->GetName() <<endl;
2959      i++;      i++;
2960    
2961      TList *li = fChain->GetListOfFriends();      TList *li = fChain->GetListOfFriends();
2962      TIter next(li);      TIter next(li);
2963      TFriendElement* T_friend=0;      TFriendElement* T_friend=0;
2964        ofile->cd();
2965      while( (T_friend = (TFriendElement*)next()) ){      while( (T_friend = (TFriendElement*)next()) ){
2966  //      cout<<T_friend->IsA()->GetName()<<" "<<T_friend->GetName()<<hex << T_friend->GetTree() << dec<<endl;  //      cout<<T_friend->IsA()->GetName()<<" "<<T_friend->GetName()<<hex << T_friend->GetTree() << dec<<endl;
2967  //      cout<<T_friend->GetTree()->GetName()<< endl;  //      cout<<T_friend->GetTree()->GetName()<< endl;
2968          tree_clone[i] = T_friend->GetTree()->CloneTree(0);                pam_tree_clone[i] = T_friend->GetTree()->CloneTree(0);
2969          name =  tree_clone[i]->GetName();          pam_tree_clone[i]->SetAutoSave(1000000);
2970            name =  pam_tree_clone[i]->GetName();
2971          name.Append("_clone");          name.Append("_clone");
2972  //      tree_clone[i]->SetName(name.Data());  //      pam_tree_clone[i]->SetName(name.Data());
2973          cout << tree_clone[i]->GetName() << endl;          cout << pam_tree_clone[i]->GetName() << endl;
2974          i++;          i++;
2975      }      }
2976        
2977        delete li;
2978    
2979      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2980    
2981  }  }
2982    
2983  // /**  /**
2984  //  * Create clone-trees   * Create clone-trees
2985  //  */   */
2986  // void PamLevel2::CreateCloneTrees(){  void PamLevel2::CreateCloneTrees(TFile *ofile){
2987    
2988    
2989    //  if the pointer is null, create a default file
2990    
2991  //     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      if(!ofile){
2992  //     cout << "Create new PAMELA trees "<<endl;          cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root "<<endl;
2993            ofile = new TFile("clone-tree.root","recreate");
2994        }
2995    
2996        ofile->cd();
2997    
2998        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2999        cout << "Create new PAMELA trees "<<endl;
3000                    
 //     if(TRK1||TRK2||TRKh){  
 //      T_clone = new TTree("Tracker_clone","PAMELA tracker level2 data ");      
 //      if(TRK1) {  
 //          trk1_clone = new TrkLevel1();  
 //          T_clone->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));  
 //          T_clone->BranchRef();  
 //          cout << "Tracker      : branch TrkLevel1"<<endl;  
 //      };  
 //      if(TRK2) {  
 //          trk2_clone = new TrkLevel2();  
 //          T_clone->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));  
 //          cout << "Tracker      : branch TrkLevel2"<<endl;          
 //      };  
 //      if(TRKh) {  
 //          trkh_clone = new TrkHough();  
 //          T_clone->Branch("TrkHough","TrkHough", GetPointerTo("TrkHough"));  
 //          cout << "Tracker      : branch TrkHough"<<endl;  
 //      };  
 //      cout << hex << T_clone << dec << endl;  
 //     }  
3001    
3002  //     // Calorimeter      run_tree_clone = new TTree("Run","PAMELA Level2 data from the GL_RUN table ");
3003  //     if(CAL1||CAL2){      run_tree_clone->Branch("RunInfo","GL_RUN",GetPointerTo("RunInfo"));
3004  //      C_clone = new TTree("Calorimeter_clone","PAMELA calorimeter level2 data ");          cout << "Run          : branch RunInfo"<<endl;
3005  //      if(CAL1) {      run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));
3006  //          calo1_clone = new CaloLevel1();      cout << "Run          : branch SoftInfo"<<endl;
3007  //          C_clone->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));      // ------------------
3008  //          cout << "Calorimeter  : branch CaloLevel1"<<endl;      // replicate run tree
3009  //      };      // ------------------
3010  //      if(CAL2) {      cout << "----------------------------------------------------"<<endl;
3011  //          calo2_clone = new CaloLevel2();      cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
3012  //          C_clone->Branch("CaloLevel2","CaloLevel2", GetPointerTo("CaloLevel2"));      for (Int_t i=0; i<run_tree->GetEntries(); i++){
3013  //          cout << "Calorimeter  : branch CaloLevel2"<<endl;          run_tree->GetEntry(i);
3014  //      };          cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
3015  //     }              run_tree_clone->Fill();
3016        }
3017        cout << "----------------------------------------------------"<<endl;
3018    
3019  //     // ToF          // ------------------------------------
3020  //     if(TOF) {      // add branch with dead and live times
3021  //      O_clone = new TTree("ToF_clone","PAMELA ToF level2 data ");          // ------------------------------------
3022  //      tof_clone = new ToFLevel2();      run_tree_clone->Branch("DeadLiveTime",totdltime,"dltime[3]/l");
3023  //      O_clone->Branch("ToFLevel2","ToFLevel2", GetPointerTo("ToFLevel2"));      cout << "Run          : branch DeadLiveTime"<<endl;
 //      cout << "ToF          : branch ToFLevel2"<<endl;  
 //     };  
 //     // Trigger  
 //     if(TRG) {  
 //      R_clone = new TTree("Trigger_clone","PAMELA trigger level2 data ");      
 //      trig_clone = new TrigLevel2();  
 //      R_clone->Branch("TrigLevel2","TrigLevel2", GetPointerTo("TrigLevel2"));  
 //      cout << "Trigger      : branch TrigLevel2"<<endl;  
 //     };  
 //     // S4  
 //     if(S4) {  
 //      S_clone = new TTree("S4_clone","PAMELA S4 level2 data ");        
 //      s4_clone = new S4Level2();  
 //      S_clone->Branch("S4Level2","S4Level2", GetPointerTo("S4Level2"));  
 //      cout << "S4           : branch S4Level2"<<endl;  
 //     };  
 //     // Neutron Detector  
 //     if(ND) {  
 //      N_clone = new TTree("NeutronD_clone","PAMELA neutron detector level2 data ");    
 //      nd_clone = new NDLevel2();  
 //      N_clone->Branch("NDLevel2","NDLevel2", GetPointerTo("NDLevel2"));  
 //      cout << "NeutronD     : branch NDLevel2"<<endl;  
 //     };  
 //     // Anticounters  
 //     if(AC) {  
 //      A_clone = new TTree("Anticounter_clone","PAMELA anticounter detector level2 data ");      
 //      ac_clone = new AcLevel2();  
 //      A_clone->Branch("AcLevel2","AcLevel2", GetPointerTo("AcLevel2"));  
 //      cout << "Anticounter  : branch AcLevel2"<<endl;  
 //     };  
 //     // OrbitalInfo  
 //     if(ORB) {  
 //      B_clone = new TTree("OrbitalInfo_clone","PAMELA oribital info  ");        
 //      orb_clone = new OrbitalInfo();  
 //      B_clone->Branch("OrbitalInfo","OrbitalInfo", GetPointerTo("OrbitalInfo"));  
 //      cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;  
 //     };  
 //     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;  
3024    
 // }  
3025    
3026  // /**      sel_tree_clone = new TTree("SelectionList","List of selected events ");
3027  //  * Create clone-trees      sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
3028  //  */      sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/L");
3029  // void PamLevel2::CreateCloneTrees(){      
3030    
3031  //     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      Int_t i=0;
3032  //     cout << "Create new PAMELA trees "<<endl;      if(TRK1||TRK2||TRKh){
3033                    pam_tree_clone[i] = new TTree("Tracker","PAMELA tracker level2 data ");
3034  //     if(TRK1||TRK2||TRKh){          if(TRK1) {
3035  //      T_clone = new TTree("Tracker_clone","PAMELA tracker level2 data ");                  pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));
3036  //      if(TRK1) {              pam_tree_clone[i]->BranchRef();
3037  //          trk1_clone = new TrkLevel1();              cout << "Tracker      : branch TrkLevel1"<<endl;
3038  //          T_clone->Branch("TrkLevel1","TrkLevel1", &trk1_clone);  //          cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;
3039  //          T_clone->BranchRef();          };
3040  //          cout << "Tracker      : branch TrkLevel1"<<endl;          if(TRK2) {
3041  //      };              pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));
3042  //      if(TRK2) {              cout << "Tracker      : branch TrkLevel2"<<endl;        
3043  //          trk2_clone = new TrkLevel2();          };
3044  //          T_clone->Branch("TrkLevel2", "TrkLevel2",&trk2_clone);          if(TRKh) {
3045  //          cout << "Tracker      : branch TrkLevel2"<<endl;                      pam_tree_clone[i]->Branch("TrkHough","TrkHough", GetPointerTo("TrkHough"));
3046  //      };              cout << "Tracker      : branch TrkHough"<<endl;
3047  //      if(TRKh) {          };
3048  //          trkh_clone = new TrkHough();          i++;
3049  //          T_clone->Branch("TrkHough","TrkHough", &trkh_clone);      }
 //          cout << "Tracker      : branch TrkHough"<<endl;  
 //      };  
 //      cout << hex << T_clone << dec << endl;  
 //     }  
3050    
3051  //     // Calorimeter      // Calorimeter
3052  //     if(CAL1||CAL2){      if(CAL1||CAL2){
3053  //      C_clone = new TTree("Calorimeter_clone","PAMELA calorimeter level2 data ");              pam_tree_clone[i] = new TTree("Calorimeter","PAMELA calorimeter level2 data ");
3054  //      if(CAL1) {          if(CAL1) {
3055  //          calo1_clone = new CaloLevel1();              pam_tree_clone[i]->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));
3056  //          C_clone->Branch("CaloLevel1", "CaloLevel1", &calo1_clone);              cout << "Calorimeter  : branch CaloLevel1"<<endl;
3057  //          cout << "Calorimeter  : branch CaloLevel1"<<endl;          };
3058  //      };          if(CAL2) {
3059  //      if(CAL2) {              pam_tree_clone[i]->Branch("CaloLevel2","CaloLevel2", GetPointerTo("CaloLevel2"));
3060  //          calo2_clone = new CaloLevel2();              cout << "Calorimeter  : branch CaloLevel2"<<endl;
3061  //          C_clone->Branch("CaloLevel2","CaloLevel2", &calo2_clone);          };
3062  //          cout << "Calorimeter  : branch CaloLevel2"<<endl;          i++;
3063  //      };      }    
 //     }      
3064    
3065  //     // ToF          // ToF    
3066  //     if(TOF) {      if(TOF) {
3067  //      O_clone = new TTree("ToF_clone","PAMELA ToF level2 data ");              pam_tree_clone[i] = new TTree("ToF","PAMELA ToF level2 data ");
3068  //      tof_clone = new ToFLevel2();          pam_tree_clone[i]->Branch("ToFLevel2","ToFLevel2", GetPointerTo("ToFLevel2"));
3069  //      O_clone->Branch("ToFLevel2","ToFLevel2", &tof_clone);          cout << "ToF          : branch ToFLevel2"<<endl;
3070  //      cout << "ToF          : branch ToFLevel2"<<endl;          i++;
3071  //     };      };
3072  //     // Trigger      // Trigger
3073  //     if(TRG) {      if(TRG) {
3074  //      R_clone = new TTree("Trigger_clone","PAMELA trigger level2 data ");              pam_tree_clone[i] = new TTree("Trigger","PAMELA trigger level2 data ");
3075  //      trig_clone = new TrigLevel2();          pam_tree_clone[i]->Branch("TrigLevel2","TrigLevel2", GetPointerTo("TrigLevel2"));
3076  //      R_clone->Branch("TrigLevel2","TrigLevel2", &trig_clone);          cout << "Trigger      : branch TrigLevel2"<<endl;
3077  //      cout << "Trigger      : branch TrigLevel2"<<endl;          i++;
3078  //     };      };
3079  //     // S4      // S4
3080  //     if(S4) {      if(S4) {
3081  //      S_clone = new TTree("S4_clone","PAMELA S4 level2 data ");                pam_tree_clone[i] = new TTree("S4","PAMELA S4 level2 data ");  
3082  //      s4_clone = new S4Level2();          pam_tree_clone[i]->Branch("S4Level2","S4Level2", GetPointerTo("S4Level2"));
3083  //      S_clone->Branch("S4Level2","S4Level2", &s4_clone);          cout << "S4           : branch S4Level2"<<endl;
3084  //      cout << "S4           : branch S4Level2"<<endl;          i++;
3085  //     };      };
3086  //     // Neutron Detector      // Neutron Detector
3087  //     if(ND) {      if(ND) {
3088  //      N_clone = new TTree("NeutronD_clone","PAMELA neutron detector level2 data ");            pam_tree_clone[i] = new TTree("NeutronD","PAMELA neutron detector level2 data ");      
3089  //      nd_clone = new NDLevel2();          pam_tree_clone[i]->Branch("NDLevel2","NDLevel2", GetPointerTo("NDLevel2"));
3090  //      N_clone->Branch("NDLevel2","NDLevel2", &nd_clone);          cout << "NeutronD     : branch NDLevel2"<<endl;
3091  //      cout << "NeutronD     : branch NDLevel2"<<endl;          i++;
3092  //     };      };
3093  //     // Anticounters      // Anticounters
3094  //     if(AC) {      if(AC) {
3095  //      A_clone = new TTree("Anticounter_clone","PAMELA anticounter detector level2 data ");              pam_tree_clone[i] = new TTree("Anticounter","PAMELA anticounter detector level2 data ");        
3096  //      ac_clone = new AcLevel2();          pam_tree_clone[i]->Branch("AcLevel2","AcLevel2", GetPointerTo("AcLevel2"));
3097  //      A_clone->Branch("AcLevel2","AcLevel2", &ac_clone);          cout << "Anticounter  : branch AcLevel2"<<endl;
3098  //      cout << "Anticounter  : branch AcLevel2"<<endl;          i++;
3099  //     };      };
3100  //     // OrbitalInfo      // OrbitalInfo
3101  //     if(ORB) {      if(ORB) {
3102  //      B_clone = new TTree("OrbitalInfo_clone","PAMELA oribital info  ");                pam_tree_clone[i] = new TTree("OrbitalInfo","PAMELA oribital info  ");  
3103  //      orb_clone = new OrbitalInfo();          pam_tree_clone[i]->Branch("OrbitalInfo","OrbitalInfo", GetPointerTo("OrbitalInfo"));
3104  //      B_clone->Branch("OrbitalInfo","OrbitalInfo", &orb_clone);          cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;
3105  //      cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;          i++;
3106  //     };      };
3107  //     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
3108    
3109    }
3110    
 // }  
3111    
3112  /**  /**
3113   * Fill tree (created with CreateCloneTrees)   * Fill tree (created with CreateCloneTrees)
# Line 1601  void PamLevel2::CreateCloneTrees( TChain Line 3115  void PamLevel2::CreateCloneTrees( TChain
3115   */   */
3116  //void PamLevel2::FillNewPamTree(TTree *T){  //void PamLevel2::FillNewPamTree(TTree *T){
3117  void PamLevel2::FillCloneTrees(){  void PamLevel2::FillCloneTrees(){
3118                
3119  // //  --------------------------------------  //    cout << "PamLevel2::FillCloneTrees()" << irunentry << endl;
 // //  copy the event  
 // //  --------------------------------------  
 //     if(trk1_clone)  *trk1_clone = *trk1_obj;  
 //     if(trk2_clone){  
 //      trk2_clone->Clear();  
 //      trk2_obj->Copy(*trk2_clone);  
 // //   *trk2_clone = *trk2_obj;  
 //     }  
 //     if(trkh_clone)  *trkh_clone = *trkh_obj;  
 //     if(calo1_clone){  
 // //   *calo1_clone = *calo1_obj;  
 //      calo1_clone->Clear();  
 //      calo1_obj->Copy(*calo1_clone);  
 //     }  
 //     if(calo2_clone){  
 // //   *calo2_clone = *calo2_obj;  
 //      calo2_clone->Clear();  
 //      calo2_obj->Copy(*calo2_clone);  
 //     }  
 //     if(tof_clone)   *tof_clone  = *tof_obj;  
 //     if(trig_clone)  *trig_clone = *trig_obj;  
 //     if(s4_clone)    *s4_clone   = *s4_obj;  
 //     if(nd_clone)    *nd_clone   = *nd_obj;  
 //     if(ac_clone)    *ac_clone   = *ac_obj;  
 //     if(orb_clone)   *orb_clone  = *orb_obj;  
   
 //     if(T_clone)T_clone->Fill();  
 //     if(C_clone)C_clone->Fill();  
 //     if(O_clone)O_clone->Fill();  
 //     if(R_clone)R_clone->Fill();  
 //     if(S_clone)S_clone->Fill();  
 //     if(N_clone)N_clone->Fill();  
 //     if(A_clone)A_clone->Fill();  
 //     if(O_clone)O_clone->Fill();  
3120    
3121      for(Int_t i=0; i<8; i++){      for(Int_t i=0; i<NCLONES; i++){
3122          if(tree_clone[i])tree_clone[i]->Fill();          if(pam_tree_clone[i])pam_tree_clone[i]->Fill();
3123      }      }
3124        if(sel_tree_clone)sel_tree_clone->Fill();    
3125    
3126  }  }
3127    
3128    
3129  TTree* PamLevel2::GetCloneTree(TString name){  TTree* PamLevel2::GetCloneTree(TString name){
3130    
3131      for(Int_t i=0; i<8; i++){        for(Int_t i=0; i<NCLONES; i++){    
3132          if(tree_clone[i]){          if(pam_tree_clone[i]){
3133              TString na = tree_clone[i]->GetName();              TString na = pam_tree_clone[i]->GetName();
3134              if(!name.CompareTo(na))return tree_clone[i];              if(!name.CompareTo(na))return pam_tree_clone[i];
3135          };          };
3136      }      }
3137        if(run_tree_clone){
3138            TString na = run_tree_clone->GetName();
3139            if(!name.CompareTo(na))return run_tree_clone;
3140        }
3141        if(sel_tree_clone){
3142            TString na = sel_tree_clone->GetName();
3143            if(!name.CompareTo(na))return sel_tree_clone;
3144        }
3145      return NULL;      return NULL;
3146    
3147  }  }
3148  void PamLevel2::WriteCloneTrees(){  void PamLevel2::WriteCloneTrees(){
3149      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
3150      cout << "Write clones of PAMELA trees "<<endl;      cout << "Write clones of PAMELA trees "<<endl;
3151      for(Int_t i=0; i<8; i++){        cout << run_tree_clone->GetName()<<endl;  
3152          if(tree_clone[i]){      if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
3153              cout << tree_clone[i]->GetName()<<endl;          run_tree_clone->GetBranch("DeadLiveTime")->Fill();
3154              tree_clone[i]->Write();      run_tree_clone->Write();
3155        cout << sel_tree_clone->GetName()<<endl;    
3156        sel_tree_clone->Write();
3157        for(Int_t i=0; i<NCLONES; i++){    
3158            if(pam_tree_clone[i]){
3159                cout << pam_tree_clone[i]->GetName()<<endl;
3160                pam_tree_clone[i]->Write();
3161          };          };
3162      }      }
3163      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
# Line 1669  void PamLevel2::WriteCloneTrees(){ Line 3165  void PamLevel2::WriteCloneTrees(){
3165  }  }
3166    
3167  /**  /**
3168   * Create a new (empty) Pamela trees   * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.
3169     */
3170    //Int_t PamLevel2::GetEntry(Int_t iee){
3171    Int_t PamLevel2::GetEntry(Long64_t iee){
3172        
3173    //     cout << "-------------------------------------"<<endl;
3174    //     cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;
3175    
3176        if(!pam_tree){
3177            cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded"<<endl;
3178            return 0;
3179        }
3180    
3181    
3182        //
3183        // 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...
3184        //
3185        //    if(!run_tree ){
3186        //  cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loeaded"<<endl;
3187        //  return 0;
3188        //    }
3189    
3190        Long64_t ii=0;
3191        //-------------------------------
3192        ii = iee;
3193        if( !pam_tree->GetEntry(ii) ){      
3194            cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree"<<endl;
3195            return 0;
3196        }
3197        //
3198        // ... 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.
3199        // 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
3200        // a problem if you don't check the return code of getentry.
3201        //
3202        if(!run_tree ){
3203            if ( TRK0 || CAL0 || TOF0 || RUN ) { //forse cosi` va bene per tornare 1?
3204                cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded"<<endl;
3205                return 0;
3206            }  else {
3207                return 1; //cosi` se non c'e` run esce qua...
3208            }
3209        }
3210    
3211        //-------------------------------
3212        ii = iee;
3213    //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);
3214    //    Bool_t UPDATED = UpdateRunInfo(ii);
3215        UpdateRunInfo(ii);
3216        if(SELLI==0)irunentry = iee-runfirstentry;
3217    //    if(UPDATED && run_tree_clone)run_tree_clone->Fill();
3218      
3219    //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;
3220    
3221    //     cout << "irunentry     "<<irunentry << endl;
3222    //     cout << "runfirstentry "<<runfirstentry << endl;
3223    //     cout << "nevents       "<<GetRunInfo()->NEVENTS<< endl;
3224    
3225    //     if( TRK0 || CAL0 || TOF0 ){
3226    //      if( !GetYodaEntry( ) ){
3227    //          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading level0 tree"<<endl;
3228    //          return 0;
3229    //      }
3230    //     }
3231    
3232    
3233        return 1;
3234    
3235    }
3236    
3237    TrkLevel0    *PamLevel2::GetTrkLevel0(){
3238        if( !TRK0 )return NULL;
3239        if( !GetYodaEntry( ) ){
3240            cout << " Int_t PamLevel2::GetTrkLevel0() -- ERROR -- error reading level0 tree"<<endl;
3241            return 0;
3242        }
3243        return trk0_obj;
3244    };
3245    CaloLevel0    *PamLevel2::GetCaloLevel0(){
3246        if( !CAL0 )return NULL;
3247        if( !GetYodaEntry( ) ){
3248            cout << " Int_t PamLevel2::GetCaloLevel0() -- ERROR -- error reading level0 tree"<<endl;
3249            return 0;
3250        }
3251        return calo0_obj;
3252    };
3253    
3254    
3255    /**
3256     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
3257     * Given the run ID (...), if needed it query the DB and load the proper file.
3258     * @return Pointer to the tree
3259   */   */
 // TTree* PamLevel2::GetNewPamTree(){  
3260    
 //     if(tree_clone)return tree_clone;  
3261    
3262  //     TTree *Tout = 0;  TTree* PamLevel2::GetYodaTree( ){
3263        
3264    //    cout << "TTree* PamLevel2::GetYodaTree( )"<<endl;
3265        //===================================
3266        // check if iroot has changed
3267        //===================================
3268        if( irun<0 ){
3269            cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = "<<irun<<endl;
3270    //      cout << "In order to use this method you have to first load the RunInfo tree "<<endl;
3271            return NULL;
3272        }    
3273        Int_t irootnew = run_obj->ID_ROOT_L0;
3274    //      cout << "iroot    "<<iroot<<endl;
3275    //      cout << "irootnew "<<irootnew<<endl;
3276    
3277        //===================================
3278        // load the level0 file
3279        // (if not already loaded)
3280        //===================================
3281        if( iroot != irootnew || !l0_tree){
3282            iroot = irootnew;
3283            //===================================
3284            // open the DB connection
3285            // (if not already opened)
3286            //===================================
3287            if(!dbc || (dbc && !dbc->IsConnected())){
3288                cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3289                cout<<"Connecting to DB"<<endl;
3290                cout<<"HOST "<<host<<endl;
3291                cout<<"USER "<<user<<endl;
3292                cout<<"PSW  "<<psw<<endl;
3293                dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
3294                if( !dbc )return NULL;
3295                if( !dbc->IsConnected() )return NULL;    
3296    //          cout<<"...done"<<endl;
3297                cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3298            }else{
3299    //          cout<<"DB already connected"<<endl;
3300            }
3301            GL_ROOT glroot = GL_ROOT();
3302            if( glroot.Query_GL_ROOT(iroot,dbc) ){
3303                cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = "<<iroot<< " does not exists"<<endl;
3304                return NULL;
3305            };
3306            TString filename = glroot.PATH + glroot.NAME;
3307            if(l0_file){
3308                l0_file->Close();
3309                l0_file->Delete();
3310            }
3311            cout << "Opening LEVEL0 file: "<< filename << endl;
3312            FileStat_t t;
3313            if( gSystem->GetPathInfo(filename.Data(),t) ){
3314                cout << " PamLevel2::GetYodaTree() -- ERROR opening file "<<endl;
3315                return NULL;
3316            }
3317            l0_file = new TFile(filename);
3318            if( !l0_file )return NULL;
3319            l0_tree = (TTree*)l0_file->Get("Physics");
3320            if(!h0_obj)h0_obj = new EventHeader();
3321            l0_tree->SetBranchAddress("Header" ,&h0_obj);
3322            prevshift = 0;
3323            //---------------------------------------------------
3324            // TRACKER:
3325            if(TRK0){
3326                if(!trk0_obj){
3327                    trk0_obj = new TrkLevel0();
3328                    trk0_obj->Set();
3329                };
3330                l0_tree->SetBranchAddress("Tracker" ,trk0_obj->GetPointerToTrackerEvent());
3331            }
3332            //--------------------------------------------------
3333            // CALORIMETER:
3334            if(CAL0){
3335                if(!calo0_obj){
3336                    calo0_obj = new CaloLevel0();
3337                    calo0_obj->Set();
3338                };
3339                l0_tree->SetBranchAddress("Calorimeter" ,calo0_obj->GetPointerToCalorimeterEvent());
3340                //      cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;
3341            }
3342            //---------------------------------------------------
3343            // TOF:
3344            if(TOF0){
3345                cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented "<<endl;
3346            }
3347    
3348        };
3349    
3350        if(!dbc || (dbc && !dbc->IsConnected())){
3351            cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;
3352        }
3353    
3354        if ( TRK0 ){
3355            TrkParams::Load(6);
3356            if( !TrkParams::IsLoaded(6) ){
3357                cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- VK-mask not loaded"<<endl;
3358            };
3359            TrkParams::SetCalib(run_obj,dbc);
3360            TrkParams::LoadCalib( );
3361            if( !TrkParams::CalibIsLoaded() ){
3362                cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded"<<endl;
3363            };
3364        }
3365    
3366    //    cout << l0_tree << endl;
3367            
3368  //     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      return l0_tree;
3369  //     cout << "Create new PAMELA trees "<<endl;  
3370    }
3371    
3372    /**
3373     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
3374     */
3375    Int_t PamLevel2::GetYodaEntry(){
3376    
3377    //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;
3378        if(!GetYodaTree())return 0;
3379            
3380        // patch
3381        if( irunentry < 0){
3382    //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
3383            irunentry=0LL;
3384        }
3385        //  ---------------------------------
3386        //  if file is NOT a preselected file
3387        //  ---------------------------------
3388        Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM);
3389    //     cout << " irunentry "<<irunentry<<endl;
3390    //     cout << " EV_FROM "<<run_obj->EV_FROM<<endl;
3391    //     cout << " quellagiusta = irunentry + EV_FROM "<< quellagiusta << endl;
3392    
3393    //     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
3394    //     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
3395    //     cout << " time "<< abstime << endl;
3396    //     cout << " trk_calib_used "<<run_obj->TRK_CALIB_USED<< endl;
3397        
3398        ULong64_t obt     = 0;
3399        ULong64_t pktn    = 0;
3400        if( GetOrbitalInfo() ){
3401            obt     = GetOrbitalInfo()->OBT;
3402            pktn    = GetOrbitalInfo()->pkt_num;
3403        }
3404    
3405        if( !GetOrbitalInfo() && !ISGP){
3406            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo "<<endl;
3407            return 0;
3408        }
3409        if( obt==0 && pktn==0 && !ISGP ){
3410            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? "<<endl;
3411            return 0;
3412        }
3413    
3414        // ---------------------------------------------------------------------
3415        // ATTENTION!!!
3416        // If data are not pre-processed with cleaner, the level0 tree may contain
3417        // spurious nested physics packets.
3418        // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
3419        // while level2 tree DOES NOT!!
3420        // This means that "quellagiusta" in these cases is not correct.
3421        // In order to retrieve the correct level0 event, I implemented a check
3422        // of the OBT and pkt-number. In case of mismatch, the level0 entry number
3423        // is shift forward until when the packets match.
3424        // ---------------------------------------------------------------------
3425        Int_t answer = 0;
3426        Int_t shift =0;
3427        //    printf(" siamo qui %i %i \n",shift,prevshift);
3428        Int_t maxshift = 10;
3429        do{
3430            if(shift>0){    
3431                cout << " PKTNUM  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
3432                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;
3433                cout << "         L2 <--> L0 mismatch ( irun "<<irun<<" irunentry "<<irunentry<<" shift "<<shift<<" prevshift "<<prevshift<<" )"<<endl;
3434            }
3435            answer = l0_tree->GetEntry(quellagiusta+(Long64_t)shift+(Long64_t)prevshift);
3436            shift++;
3437            if( !GetEventHeader() ){
3438                cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader "<<endl;
3439                return 0;
3440            }
3441    
3442            if(ISGP){
3443                obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
3444                pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
3445            }
3446    
3447    //      cout << "PKTNUM "<<shift<<" ==  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
3448    //      cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;
3449    //      if( (quellagiusta+shift) == l0_tree->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;
3450    //      cout << " obt "<< obt << endl;
3451    //      cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;
3452    //      cout << " pktn "<< pktn << endl;
3453    //      cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
3454    //      printf(" IDRUN %u \n",GetRunInfo()->ID);
3455    //
3456            if ( prevshift != 0 && (quellagiusta+(Long64_t)shift) == GetYodaTree()->GetEntries() ){
3457              prevshift = 0;
3458              shift = -1;
3459            };
3460    
3461        }while( ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) ||
3462                  pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())       )
3463                && (quellagiusta+(Long64_t)shift) < GetYodaTree()->GetEntries() && shift < maxshift);
3464    
3465        if ( (quellagiusta+(Long64_t)shift+(Long64_t)prevshift) > GetYodaTree()->GetEntries() || shift == maxshift ) {
3466            cout << " Big trouble here, no such event in Level0 data! " <<endl;
3467            return 0;
3468        }
3469    //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
3470    //    return GetYodaTree()->GetEntry(quellagiusta);
3471        if ( shift > 1 ) prevshift += (shift-1);
3472            
3473  //     if(TRK1||TRK2||TRKh){      return answer;
3474  //      TTree *T = new TTree("Tracker_clone","PAMELA tracker level2 data ");          
3475  //      if(TRK1) {  }
3476  //          trk1_clone = new TrkLevel1();  /**
3477  //          T->Branch("TrkLevel1","TrkLevel1", &trk1_clone);   * \Brief Set DB connection
3478  //          T->BranchRef();   */
3479  //          cout << "Tracker      : branch TrkLevel1"<<endl;  Bool_t PamLevel2::SetDBConnection(){
 //      };  
 //      if(TRK2) {  
 //          trk2_clone = new TrkLevel2();  
 //          T->Branch("TrkLevel2", "TrkLevel2",&trk2_clone);  
 //          cout << "Tracker      : branch TrkLevel2"<<endl;          
 //      };  
 //      if(TRKh) {  
 //          trkh_clone = new TrkHough();  
 //          T->Branch("TrkHough","TrkHough", &trkh_clone);  
 //          cout << "Tracker      : branch TrkHough"<<endl;  
 //      };  
 //      if(!Tout)Tout=T;  
 //      else Tout->AddFriend("Tracker_clone");  
 //     }  
3480    
3481  //     // Calorimeter      cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3482  //     if(CAL1||CAL2){      cout<<"Connecting to DB"<<endl;
3483  //      TTree *C = new TTree("Calorimeter_clone","PAMELA calorimeter level2 data ");          cout<<"HOST "<<host<<endl;
3484  //      if(CAL1) {      cout<<"USER "<<user<<endl;
3485  //          calo1_clone = new CaloLevel1();      cout<<"PSW  "<<psw<<endl;
3486  //          C->Branch("CaloLevel1", "CaloLevel1", &calo1_clone);      dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
3487  //          cout << "Calorimeter  : branch CaloLevel1"<<endl;      if( !dbc )return false;
3488  //      };      if( !dbc->IsConnected() )return false;    
3489  //      if(CAL2) {      cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3490  //          calo2_clone = new CaloLevel2();      return true;
 //          C->Branch("CaloLevel2","CaloLevel2", &calo2_clone);  
 //          cout << "Calorimeter  : branch CaloLevel2"<<endl;  
 //      };  
 //      if(!Tout)Tout=C;  
 //      else Tout->AddFriend("Calorimeter_clone");  
 //     }      
   
 //     // ToF      
 //     if(TOF) {  
 //      TTree *O = new TTree("ToF_clone","PAMELA ToF level2 data ");      
 //      tof_clone = new ToFLevel2();  
 //      O->Branch("ToFLevel2","ToFLevel2", &tof_clone);  
 //      cout << "ToF          : branch ToFLevel2"<<endl;  
 //      if(!Tout)Tout=O;  
 //      else Tout->AddFriend("ToF_clone");  
 //     };  
 //     // Trigger  
 //     if(TRG) {  
 //      TTree *R = new TTree("Trigger_clone","PAMELA trigger level2 data ");      
 //      trig_clone = new TrigLevel2();  
 //      R->Branch("TrigLevel2","TrigLevel2", &trig_clone);  
 //      cout << "Trigger      : branch TrigLevel2"<<endl;  
 //      if(!Tout)Tout=R;  
 //      else Tout->AddFriend("Trigger_clone");  
 //     };  
 //     // S4  
 //     if(S4) {  
 //      TTree *S = new TTree("S4_clone","PAMELA S4 level2 data ");        
 //      s4_clone = new S4Level2();  
 //      S->Branch("S4Level2","S4Level2", &s4_clone);  
 //      cout << "S4           : branch S4Level2"<<endl;  
 //      if(!Tout)Tout=S;  
 //      else Tout->AddFriend("S4_clone");  
 //     };  
 //     // Neutron Detector  
 //     if(ND) {  
 //      TTree *N = new TTree("NeutronD_clone","PAMELA neutron detector level2 data ");    
 //      nd_clone = new NDLevel2();  
 //      N->Branch("NDLevel2","NDLevel2", &nd_clone);  
 //      cout << "NeutronD     : branch NDLevel2"<<endl;  
 //      if(!Tout)Tout=N;  
 //      else Tout->AddFriend("NeutronD_clone");  
 //     };  
 //     // Anticounters  
 //     if(AC) {  
 //      TTree *A = new TTree("Anticounter_clone","PAMELA anticounter detector level2 data ");    
 //      ac_clone = new AcLevel2();  
 //      A->Branch("AcLevel2","AcLevel2", &ac_clone);  
 //      cout << "Anticounter  : branch AcLevel2"<<endl;  
 //      if(!Tout)Tout=A;  
 //      else Tout->AddFriend("Anticounter_clone");  
 //     };  
 //     // OrbitalInfo  
 //     if(ORB) {  
 //      TTree *B = new TTree("OrbitalInfo_clone","PAMELA oribital info  ");      
 //      orb_clone = new OrbitalInfo();  
 //      B->Branch("OrbitalInfo","OrbitalInfo", &orb_clone);  
 //      cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;  
 //      if(!Tout)Tout=B;  
 //      else Tout->AddFriend("OrbitalInfo_clone");  
 //     };  
 //     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;  
3491    
3492  //     tree_clone = Tout;  }
 //     tree_clone->SetDirectory(0);  
3493    
3494  //     return Tout;  /**
3495  // }   * \Brief Add a friend to the pamela chain.
3496  // /**   * @param cname name of the chain to be added
3497  //  * Fill a tree (created with GetNewPamTree)   */
 //  *  
 //  */  
 // //void PamLevel2::FillNewPamTree(TTree *T){  
 // void PamLevel2::FillNewPamTree(){  
           
3498    
3499  //     if(trk1_clone)  *trk1_clone = *trk1_obj;  TChain* PamLevel2::AddFriend(TString cname){
 //     if(trk2_clone){  
 //      trk2_clone->Clear();  
 //      trk2_obj->Copy(*trk2_clone);  
 // //   *trk2_clone = *trk2_obj;  
 //     }  
 //     if(trkh_clone)  *trkh_clone = *trkh_obj;  
 //     if(calo1_clone){  
 // //   *calo1_clone = *calo1_obj;  
 //      calo1_clone->Clear();  
 //      calo1_obj->Copy(*calo1_clone);  
 //     }  
 //     if(calo2_clone){  
 // //   *calo2_clone = *calo2_obj;  
 //      calo2_clone->Clear();  
 //      calo2_obj->Copy(*calo2_clone);  
 //     }  
 //     if(tof_clone)   *tof_clone  = *tof_obj;  
 //     if(trig_clone)  *trig_clone = *trig_obj;  
 //     if(s4_clone)    *s4_clone   = *s4_obj;  
 //     if(nd_clone)    *nd_clone   = *nd_obj;  
 //     if(ac_clone)    *ac_clone   = *ac_obj;  
 //     if(orb_clone)   *orb_clone  = *orb_obj;  
   
 //     TTree *T = tree_clone;  
   
 //     T->Fill(); //fill main tree  
 // //    cout<<T->IsA()->GetName()<<" "<<T->GetName()<<endl;  
 //     TList *li = T->GetListOfFriends();  
 //     TIter next(li);  
 //     TFriendElement* T_friend=0;  
 //     while( (T_friend = (TFriendElement*)next()) ){  
 // //   cout<<T_friend->IsA()->GetName()<<" "<<T_friend->GetName()<<hex << T_friend->GetTree() << dec<<endl;  
 //      T_friend->GetTree()->Fill();//fill friends  
 //     }  
3500    
3501  // }      if(!GetPamTree()){
3502            cout << " TChain* PamLevel2::AddFriend(TString cname) --- a pamela tree must be created first"<<endl;
3503            return NULL;
3504        }
3505    
3506    
3507        TChain *c = new TChain( cname.Data() );
3508    
3509        TIter next( GetPamTree()->GetListOfFiles() );
3510        Int_t nf = 0;
3511        TChainElement* element = 0;    
3512        while ((element = (TChainElement*) next())) {      
3513            c->Add(element->GetTitle());
3514            nf++;
3515        }
3516    
3517        GetPamTree()->AddFriend(cname.Data());
3518    
3519        cout << "external chain created and added to pamela friends :"<<cname<<endl;
3520        cout << "n.files "<<nf<<endl;
3521        
3522    
3523        return c;    
3524    
3525    }

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.47

  ViewVC Help
Powered by ViewVC 1.1.23