/[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.6 by pam-fi, Tue Oct 24 10:24:27 2006 UTC revision 1.31 by mocchiut, Mon Mar 19 05:39:36 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  };  };
15  //--------------------------------------  //--------------------------------------
16  //  //
# Line 19  PamTrack::PamTrack(){ Line 20  PamTrack::PamTrack(){
20   * Constructor   * Constructor
21   */   */
22  PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o){  PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o){
23      trk_track  = this->TrkTrack::GetTrkTrack();  
24      calo_track = this->CaloTrkVar::GetCaloTrkVar();      trk_track  = 0;
25      tof_track  = this->ToFTrkVar::GetToFTrkVar();      calo_track = 0;
26      if(t) *trk_track  = *t;      tof_track  = 0;
27      if(c) *calo_track = *c;  //     if(t)trk_track  = new TrkTrack(*t);
28      if(o) *tof_track  = *o;      //     if(c)calo_track = new CaloTrkVar(*c);
29    //     if(o)tof_track  = new ToFTrkVar(*o);
30        if(t)trk_track  = t;
31        if(c)calo_track = c;
32        if(o)tof_track  = o;
33        candeleteobj = 0;
34    
35  };  };
36    PamTrack::PamTrack(const PamTrack& track){
37    
38        TrkTrack   *t = track.trk_track;
39        CaloTrkVar *c = track.calo_track;
40        ToFTrkVar  *o = track.tof_track;
41    
42        trk_track  = 0;
43        calo_track = 0;
44        tof_track  = 0;
45        if(t)trk_track  = new TrkTrack(*t);
46        if(c)calo_track = new CaloTrkVar(*c);
47        if(o)tof_track  = new ToFTrkVar(*o);
48        candeleteobj = 1;
49        
50    }
51    void PamTrack::Clear(){
52    
53    //    cout << "PamTrack::Clear() "<<candeleteobj<<endl;
54        if(candeleteobj){
55            if(trk_track)  trk_track->TrkTrack::Clear();
56            if(calo_track) calo_track->CaloTrkVar::Clear();//???
57            if(tof_track)  tof_track->ToFTrkVar::Clear();//???
58        }else{
59            trk_track  = 0;
60            calo_track = 0;
61            tof_track  = 0;
62        }
63    }
64    void PamTrack::Delete(){
65    //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;
66        if(candeleteobj){
67            if(trk_track)  {
68                trk_track->TrkTrack::Clear();
69                delete trk_track;
70            }
71            if(calo_track){
72                calo_track->CaloTrkVar::Clear();//???
73                delete calo_track;
74            }
75            if(tof_track){
76                tof_track->ToFTrkVar::Clear();//???
77                delete tof_track;
78            }
79        }else{
80            Clear();
81        }
82    }
83  //--------------------------------------  //--------------------------------------
84  //  //
85  //  //
# Line 36  PamTrack::PamTrack(TrkTrack* t, CaloTrkV Line 89  PamTrack::PamTrack(TrkTrack* t, CaloTrkV
89   */   */
90  PamLevel2::PamLevel2(){  PamLevel2::PamLevel2(){
91                    
92      trk_l1_obj  = this->TrkLevel1::GetTrkLevel1();        //     trk2_obj  = TrkLevel2::GetTrkLevel2();
93      trk_obj     = this->TrkLevel2::GetTrkLevel2();  //     trk1_obj  = TrkLevel1::GetTrkLevel1();
94      calo_obj    = this->CaloLevel2::GetCaloLevel2();  //     trkh_obj  = TrkHough::GetTrkHough();
95      tof_obj     = this->ToFLevel2::GetToFLevel2();  //     calo1_obj = CaloLevel1::GetCaloLevel1();
96      trig_obj    = this->TrigLevel2::GetTrigLevel2();  //     calo2_obj = CaloLevel2::GetCaloLevel2();
97      s4_obj      = this->S4Level2::GetS4Level2();  //     tof_obj   = ToFLevel2::GetToFLevel2();
98      nd_obj      = this->NDLevel2::GetNDLevel2();  //     trig_obj  = TrigLevel2::GetTrigLevel2();
99      ac_obj      = this->AcLevel2::GetAcLevel2();  //     s4_obj    = S4Level2::GetS4Level2();
100      orb_obj     = this->OrbitalInfo::GetOrbitalInfo();  //     nd_obj    = NDLevel2::GetNDLevel2();
101    //     ac_obj    = AcLevel2::GetAcLevel2();
102      run_obj     = new GL_RUN();  //     orb_obj   = OrbitalInfo::GetOrbitalInfo();
103            
104      sorted_tracks = new TRefArray();      h0_obj    = 0;
105            trk0_obj  = 0;
106      CAL = true;      calo0_obj  = 0;
107      TRK = true;  
108      TRG = true;      trk2_obj  = 0;
109      TOF = true;      trk1_obj  = 0;
110      S4  = true;      trkh_obj  = 0;
111      ND  = true;      calo1_obj = 0;
112      AC  = true;      calo2_obj = 0;
113      ORB = true;      tof_obj   = 0;
114        trig_obj  = 0;
115        s4_obj    = 0;
116        nd_obj    = 0;
117        ac_obj    = 0;
118        orb_obj   = 0;
119    
120        run_obj   = 0;//new GL_RUN();
121        soft_obj   = 0;// Emiliano
122        irun = -1;
123        runfirstentry = 0ULL;
124        runlastentry = 0ULL;
125    
126        l0_file = NULL;
127        l0_tree = NULL;
128        iroot   = -1;
129        dbc     = 0;
130    
131        prevshift = 0;
132        
133        irun = -1;
134        run_tree = NULL;
135        run_tree_clone = NULL;
136        sel_tree = NULL;
137        sel_tree_clone = NULL;
138        
139        irunentry = -1;
140        pam_tree = NULL;
141        for(Int_t i=0; i<NCLONES; i++ )pam_tree_clone[i]=NULL;
142    
143        host = "mysql://localhost/pamelaprod";
144        user = "anonymous";
145        psw = "";
146        const char *pamdbhost=gSystem->Getenv("PAM_DBHOST");
147        const char *pamdbuser=gSystem->Getenv("PAM_DBUSER");
148        const char *pamdbpsw=gSystem->Getenv("PAM_DBPSW");
149        if ( !pamdbhost ) pamdbhost = "";
150        if ( !pamdbuser ) pamdbuser = "";
151        if ( !pamdbpsw ) pamdbpsw = "";
152        if ( strcmp(pamdbhost,"") ) host = pamdbhost;
153        if ( strcmp(pamdbuser,"") ) user = pamdbuser;
154        if ( strcmp(pamdbpsw,"") ) psw = pamdbpsw;
155    
156        
157    
158    //    sorted_tracks = 0;//new TRefArray();
159        
160        CAL0 = false;
161        CAL1 = true;
162        CAL2 = true;
163        TRK2 = true;
164        TRK1 = false;
165        TRKh = false;
166        TRKh = false;
167        TRG  = true;
168        TOF  = true;
169        TOF0 = false;
170        S4   = true;
171        ND   = true;
172        AC   = true;
173        ORB  = true;
174            
175      TRK_L1 = false;      RUN  = true;
176        
177      Tout  = NULL;      SELLI = -1;
178    
179        tsorted=0;
180        timage=0;
181    
182  };  };
183  /**  /**
184   * Destructor   * Delete the event (act as Dtor)
185   */   */
186  PamLevel2::~PamLevel2(){  void PamLevel2::Delete(){
187                            
188      TrkLevel1::Clear();      if(run_obj)delete run_obj;
189            if(soft_obj)delete soft_obj; //Emiliano
190      TrkLevel2::Clear();  
191      CaloLevel2::Clear();  //    cout << "void PamLevel2::Clear()"<<endl;
192      ToFLevel2::Clear();      if(h0_obj)   delete h0_obj;
193      TrigLevel2::Clear();      if(trk0_obj) delete trk0_obj;      
194      S4Level2::Clear();      if(calo0_obj) delete calo0_obj;    
195      NDLevel2::Clear();      if(trk1_obj) delete trk1_obj;      
196      AcLevel2::Clear();      if(trk2_obj) delete trk2_obj;
197      OrbitalInfo::Clear();      if(trkh_obj) delete trkh_obj;
198        if(calo1_obj)delete calo1_obj;
199        if(calo2_obj)delete calo2_obj;
200        if(tof_obj)  delete tof_obj;
201        if(trig_obj) delete trig_obj;
202        if(s4_obj)   delete s4_obj;
203        if(nd_obj)   delete nd_obj;
204        if(ac_obj)   delete ac_obj;
205        if(orb_obj)  delete orb_obj;
206        
207        if(tsorted){
208            tsorted->Delete();
209            delete tsorted;
210        }
211        if(timage){
212            timage->Delete();
213            delete timage;
214        }
215    
216        if(dbc){
217            dbc->Close();
218            delete dbc;
219        }
220            
221      delete run_obj;      if(l0_file)l0_file->Close();
222        if(pam_tree)pam_tree->Delete();;
223        if(run_tree)run_tree->Delete();;
224        if(sel_tree)sel_tree->Delete();;
225        for(Int_t i=0; i<NCLONES; i++ )if(pam_tree_clone[i])pam_tree_clone[i]->Delete();;
226        if(run_tree_clone)run_tree_clone->Delete();;
227        if(sel_tree_clone)sel_tree_clone->Delete();;
228            
229  //      delete sorted_tracks;  
     sorted_tracks->Delete(); // clean the reference array  
           
     if(Tout)Tout->Delete(); // delete loaded tree from memory  
           
230  };  };
231    
232  /**  /**
233   * Clear the event   * Clear the event (NB! does not deallocate objects)
234   */   */
235  void PamLevel2::Clear(){  void PamLevel2::Clear(){
236                    
237      TrkLevel1::Clear();  //    cout << "void PamLevel2::Clear()"<<endl;
238            
239      TrkLevel2::Clear();  //
240      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
241      ToFLevel2::Clear();  // want to load them for each event even if they are the same...
242      TrigLevel2::Clear();  //
243      S4Level2::Clear();  //    if(run_obj)delete run_obj;
244      NDLevel2::Clear();  //    if(run_obj) run_obj->Clear();  // Emiliano: Do not deallocate run_obj here, it will give segmentation fault! call clear instead
245      AcLevel2::Clear();  //    if(soft_obj) soft_obj->Clear();
246      OrbitalInfo::Clear();  
247        if(h0_obj)   h0_obj->Clear();      
248        if(trk0_obj) trk0_obj->Clear();    
249        if(calo0_obj) calo0_obj->Clear();  
250        if(trk1_obj) trk1_obj->Clear();    
251        if(trk2_obj) trk2_obj->Clear();
252        if(trkh_obj) trkh_obj->Clear();
253        if(calo1_obj)calo1_obj->Clear();
254        if(calo2_obj)calo2_obj->Clear();
255        if(tof_obj)  tof_obj->Clear();
256        if(trig_obj) trig_obj->Clear();
257        if(s4_obj)   s4_obj->Clear();
258        if(nd_obj)   nd_obj->Clear();
259        if(ac_obj)   ac_obj->Clear();
260        if(orb_obj)  orb_obj->Clear();
261            
262      sorted_tracks->Delete(); // clean the reference array  //    if(sorted_tracks)sorted_tracks->Clear();
263    //    sorted_tracks.Clear();
264    
265        if(tsorted){
266            tsorted->Delete();
267        }
268        if(timage){
269            timage->Delete();
270        }
271            
272  };  };
273    
# Line 112  void PamLevel2::Clear(){ Line 276  void PamLevel2::Clear(){
276  //  //
277  //  //
278  //--------------------------------------  //--------------------------------------
279    void *PamLevel2::GetPointerTo(const char* c ){
280    
281        TString objname = c;
282    
283        if(!objname.CompareTo("TrkLevel1"))  {
284            if(!trk1_obj){
285                trk1_obj  = new TrkLevel1();
286                trk1_obj->Set();
287            }
288            return &trk1_obj;
289        };
290        if(!objname.CompareTo("TrkLevel2"))  {
291            if(!trk2_obj){  
292                trk2_obj  = new TrkLevel2();
293                trk2_obj->Set();
294            }
295            return &trk2_obj;
296        };
297        if(!objname.CompareTo("TrkHough"))   {
298            if(!trkh_obj)  trkh_obj  = new TrkHough();
299            return &trkh_obj;
300        };
301        if(!objname.CompareTo("CaloLevel1")) {
302            if(!calo1_obj) calo1_obj = new CaloLevel1();
303            return &calo1_obj;
304        };
305        if(!objname.CompareTo("CaloLevel2")) {
306            if(!calo2_obj){
307                calo2_obj = new CaloLevel2();
308                calo2_obj->Set();
309            };
310            return &calo2_obj;
311        };
312        if(!objname.CompareTo("ToFLevel2"))  {
313            if(!tof_obj){
314                tof_obj   = new ToFLevel2();
315                tof_obj->Set();
316            }
317            return &tof_obj;
318        };
319        if(!objname.CompareTo("TrigLevel2")) {
320            if(!trig_obj)  trig_obj  = new TrigLevel2();
321            return &trig_obj;
322        };
323        if(!objname.CompareTo("S4Level2"))   {
324            if(!s4_obj)    s4_obj    = new S4Level2();
325            return &s4_obj;
326        };
327        if(!objname.CompareTo("NDLevel2"))   {
328            if(!nd_obj)    nd_obj    = new NDLevel2();
329            return &nd_obj;
330        };
331        if(!objname.CompareTo("AcLevel2"))   {
332            if(!ac_obj)    ac_obj    = new AcLevel2();
333            return &ac_obj;
334        };
335        if(!objname.CompareTo("OrbitalInfo")){
336            if(!orb_obj)   orb_obj   = new OrbitalInfo();
337            return &orb_obj;
338        };
339        
340        if(!objname.CompareTo("RunInfo"))return &run_obj;
341    
342        if(!objname.CompareTo("SoftInfo"))return &soft_obj; // Emiliano
343    
344        return NULL;
345    };
346    //--------------------------------------
347    //
348    //
349    //--------------------------------------
350  /**  /**
351   * Retrieves the calorimeter track matching the seqno-th tracker stored track.   * Retrieves the calorimeter track matching the seqno-th tracker stored track.
352   * (If seqno = -1 retrieves the self-trigger calorimeter track)   * (If seqno = -1 retrieves the self-trigger calorimeter track)
353   */   */
354   CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno){   CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno){
355                
356       if( CaloLevel2::ntrk()==0 ){       if( !calo2_obj )return 0;
357    
358         if( calo2_obj->CaloLevel2::ntrk()==0 ){
359           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;
360           return NULL;           return NULL;
361       };       };
# Line 127  void PamLevel2::Clear(){ Line 364  void PamLevel2::Clear(){
364       Int_t it_calo=0;       Int_t it_calo=0;
365            
366       do{       do{
367           c = CaloLevel2::GetCaloTrkVar(it_calo);           c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);
368           it_calo++;           it_calo++;
369       } while( c && seqno != c->trkseqno && it_calo < CaloLevel2::ntrk());             } while( c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());    
370            
371       if(!c || seqno != c->trkseqno){       if(!c || seqno != c->trkseqno){
372           c = 0;           c = 0;
# Line 148  void PamLevel2::Clear(){ Line 385  void PamLevel2::Clear(){
385   */   */
386   ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno){   ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno){
387                    
388       if( ToFLevel2::ntrk()==0 ){       if( !tof_obj )return 0;
389    
390         if( tof_obj->ToFLevel2::ntrk()==0 ){
391           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;
392           return NULL;           return NULL;
393       };       };
# Line 157  void PamLevel2::Clear(){ Line 396  void PamLevel2::Clear(){
396       Int_t it_tof=0;       Int_t it_tof=0;
397            
398       do{       do{
399           c = ToFLevel2::GetToFTrkVar(it_tof);           c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof);
400           it_tof++;           it_tof++;
401       } while( c && seqno != c->trkseqno && it_tof < ToFLevel2::ntrk());       } while( c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk());        
402            
403       if(!c || seqno != c->trkseqno){       if(!c || seqno != c->trkseqno){
404           c = 0;           c = 0;
# Line 177  void PamLevel2::Clear(){ Line 416  void PamLevel2::Clear(){
416   * 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.
417   */   */
418   PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t){   PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t){
419                
420         cout <<"PamLevel2::GetPamTrackAlong(TrkTrack* t) **obsolete** "<<endl;
421         cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;
422    
423       CaloTrkVar *c = 0;       CaloTrkVar *c = 0;
424       ToFTrkVar  *o = 0;       ToFTrkVar  *o = 0;
425            
426       if(CAL) c = GetCaloStoredTrack(t->GetSeqNo());       if(CAL2) c = GetCaloStoredTrack(t->GetSeqNo());
427       if(TOF) o = GetToFStoredTrack(t->GetSeqNo());       if(TOF) o = GetToFStoredTrack(t->GetSeqNo());
428            
429  //    if(t && c && o)track = new PamTrack(t,c,o);  //    if(t && c && o)track = new PamTrack(t,c,o);
# Line 202  void PamLevel2::Clear(){ Line 444  void PamLevel2::Clear(){
444    
445  PamTrack* PamLevel2::GetStoredTrack(Int_t itrk){  PamTrack* PamLevel2::GetStoredTrack(Int_t itrk){
446            
447        cout <<"PamLevel2::GetStoredTrack(Int_t itrk) **to-be-updated** "<<endl;
448        cout <<"for the moment, better use separately the methods: TrkLevel2::GetStoredTrack(seqno) CaloLevel2::GetCaloTrkVar(Int_t notrack) ToFLevel2::GetToFTrkVar(Int_t notrack)"<<endl;
449        cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;
450      PamTrack *track = 0;      PamTrack *track = 0;
451            
452      if( itrk >=0 && itrk < TrkLevel2::ntrk() ){      if( itrk >=0 && itrk < trk2_obj->TrkLevel2::ntrk() ){
453                    
454          TrkTrack *t = TrkLevel2::GetStoredTrack(itrk);          TrkTrack *t = trk2_obj->TrkLevel2::GetStoredTrack(itrk);
455          track = GetPamTrackAlong(t);          track = GetPamTrackAlong(t);
456                    
457            
458    
459      }else{      }else{
460          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;
461      };      };
462            
463      return track;      return track;
# Line 224  PamTrack* PamLevel2::GetStoredTrack(Int_ Line 471  PamTrack* PamLevel2::GetStoredTrack(Int_
471   * Sort physical (tracker) tracks and stores them in the TRefArray (of TrkTrack objects) which pointer is  PamLevel2::sorted_tracks.   * Sort physical (tracker) tracks and stores them in the TRefArray (of TrkTrack objects) which pointer is  PamLevel2::sorted_tracks.
472   * @param how String to set the sorting cryterium (es: "CAL" or "TRK+CAL+TOF" ecc...).   * @param how String to set the sorting cryterium (es: "CAL" or "TRK+CAL+TOF" ecc...).
473   * Sorting cryteria:   * Sorting cryteria:
474   * TRK: lower chi**2   * TRK: lower chi**2
475   * CAL: lower Y spatial residual on the first calorimeter plane   * CAL: lower Y spatial residual on the first calorimeter plane
476   * TOF: bigger numebr of hit PMTs along the track, on S12 S21 S32 (where paddles are along the Y axis).   * TOF: bigger numebr of hit PMTs along the track, on S12 S21 S32 (where paddles are along the Y axis).
477   * The default sorting cryterium is "TOF+CAL".   * The default sorting cryterium is "TOF+CAL".
# Line 233  PamTrack* PamLevel2::GetStoredTrack(Int_ Line 480  PamTrack* PamLevel2::GetStoredTrack(Int_
480   */   */
481  void PamLevel2::SortTracks(TString how){  void PamLevel2::SortTracks(TString how){
482    
483    //    cout <<" PamLevel2::SortTracks(TString how) "<<endl;
484        if( !trk2_obj ){
485            cout << "void PamLevel2::SortTracks(TString how):  TrkLevel2 not loaded !!!";
486            return;
487        };
488    //    cout << "call SortTracs() "<<endl;
489    //Save current Object count    //Save current Object count
490      Int_t ObjectNumber = TProcessID::GetObjectCount();      Int_t ObjectNumber = TProcessID::GetObjectCount();
491        
492      sorted_tracks->Delete(); //temporaneo???  //    cout << "ObjectNumber  "<<ObjectNumber <<endl;
493            
494    //     if(!sorted_tracks)sorted_tracks = new TRefArray();
495    //     sorted_tracks->Clear();
496    //    sorted_tracks.Clear();
497    
498        if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
499        tsorted->Delete();
500        TClonesArray &ttsorted = *tsorted;
501        if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
502        timage->Delete();
503        TClonesArray &ttimage = *timage;
504    
505    
506      // loop over the tracks sorted by the tracker      // loop over the tracks sorted by the tracker
507      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
508      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
509      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
510            
511      if( !CAL &&  use_CAL) use_CAL = false;      if( !CAL2 &&  use_CAL) use_CAL = false;
512      if( !TOF &&  use_TOF) use_TOF = false;      if( !TOF &&  use_TOF) use_TOF = false;
513            
514      if( !TRK ){      if( !TRK2 ){
515          cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;  //      cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
516          return;          return;
517      };      };
518    
519        //   cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;
520            
521      for(Int_t i=0; i < TrkLevel2::GetNTracks(); i++){      for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){
522                    
523          TrkTrack *ts = 0;          TrkTrack   *ts = 0;
524            CaloTrkVar *cs = 0;
525            ToFTrkVar  *os = 0;
526                    
527          // get tracker tracks          // get tracker tracks
528          TrkTrack   *tp = TrkLevel2::GetTrack(i);                    //tracker          TrkTrack   *tp = trk2_obj->TrkLevel2::GetTrack(i);                    //tracker
529  //              CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());          CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());
530  //              ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());          ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());
531          CaloTrkVar *cp = 0;  
532          ToFTrkVar  *op = 0;          TrkTrack   *ti = 0;              //tracker (image)
533                    CaloTrkVar *ci = 0;
534            ToFTrkVar  *oi = 0;
535    //      cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
536          // if track has an image, check image selection          // if track has an image, check image selection
537          if(tp->HasImage()){          if(tp->HasImage()){
538                            
539              TrkTrack   *ti = TrkLevel2::GetTrackImage(i);              //tracker (image)              ti = trk2_obj->TrkLevel2::GetTrackImage(i);              //tracker (image)
540  //                      CaloTrkVar *ci = GetCaloStoredTrack(ti->GetSeqNo());              ci = GetCaloStoredTrack(ti->GetSeqNo());
541  //                      ToFTrkVar  *oi = GetToFStoredTrack(ti->GetSeqNo());              oi = GetToFStoredTrack(ti->GetSeqNo());
             CaloTrkVar *ci = 0;  
             ToFTrkVar  *oi = 0;  
542                            
543    //          cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
544    
545              //assign starting scores              //assign starting scores
546              Int_t tp_score = 0;  //main track sorted by the tracker              Int_t tp_score = 0;  //main track sorted by the tracker
547              Int_t ti_score = 0;  //image track              Int_t ti_score = 0;  //image track
# Line 280  void PamLevel2::SortTracks(TString how){ Line 551  void PamLevel2::SortTracks(TString how){
551              // ------------------------              // ------------------------
552              // check the Y spatial residual on the first calorimeter plane              // check the Y spatial residual on the first calorimeter plane
553              // (cut on calorimeter variables from Emiliano)              // (cut on calorimeter variables from Emiliano)
554                if( use_CAL && !calo2_obj ){
555                    cout << "void PamLevel2::SortTracks(TString how): how= "<<how<<" but CaloLevel2 not loaded !!!";
556                    return;
557                };
558              if(              if(
559                  use_CAL            &&                  use_CAL            &&
560                  npcfit[1] > 15     &&   //no. of fit planes on Y view                  calo2_obj->npcfit[1] > 15     &&   //no. of fit planes on Y view
561                  varcfit[1] < 1000. &&  //fit variance on Y view                  calo2_obj->varcfit[1] < 1000. &&  //fit variance on Y view
562                    cp && ci &&
563                  true){                  true){
564    
565                                    
566                  cp = GetCaloStoredTrack(tp->GetSeqNo());                  Float_t resy_p = cp->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_p < 0)resy_p= - resy_p;
567                  ci = GetCaloStoredTrack(ti->GetSeqNo());                  Float_t resy_i = ci->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_i < 0)resy_i= - resy_i;
                   
                 Float_t resy_p = cp->tbar[0][1] - cbar[0][1]; if(resy_p < 0)resy_p= - resy_p;  
                 Float_t resy_i = ci->tbar[0][1] - cbar[0][1]; if(resy_i < 0)resy_i= - resy_i;  
568                                    
569                  if(resy_p <= resy_i) tp_score++;                  if(resy_p <= resy_i) tp_score++;
570                  else                 ti_score++;                  else                 ti_score++;
571    
572    //              cout << "CALO "<<tp_score<<ti_score<<endl;
573    
574              };              };
575              // ------------------------              // ------------------------
576              // TOF check              // TOF check
577              // ------------------------                  // ------------------------    
578              // check the number of hit pmts along the track              // check the number of hit pmts along the track
579              // on S12 S21 and S32, where paddles are parallel to Y axis              // on S12 S21 and S32, where paddles are parallel to Y axis
580              if( use_TOF ){              if( use_TOF && !tof_obj ){
581                    cout << "void PamLevel2::SortTracks(TString how): how= "<<how<<" but ToFLevel2 not loaded !!!";
582                    return;
583                };
584                if( use_TOF && op && oi ){
585                                    
586                  Int_t nphit_p =0;                  Int_t nphit_p =0;
587                  Int_t nphit_i =0;                  Int_t nphit_i =0;
588                                    
                 op = GetToFStoredTrack(tp->GetSeqNo());  
                 oi = GetToFStoredTrack(ti->GetSeqNo());  
589                                    
590  /*                              cout << "track: npmtadc "<< op->npmtadc << endl;  /*                              cout << "track: npmtadc "<< op->npmtadc << endl;
591                                  cout << "track: npmttdc "<< op->npmttdc << endl;                                  cout << "track: npmttdc "<< op->npmttdc << endl;
# Line 314  void PamLevel2::SortTracks(TString how){ Line 593  void PamLevel2::SortTracks(TString how){
593                                  cout << "image: npmttdc "<< oi->npmttdc << endl;*/                                  cout << "image: npmttdc "<< oi->npmttdc << endl;*/
594                                    
595                  for (Int_t ih=0; ih < op->npmtadc; ih++){                  for (Int_t ih=0; ih < op->npmtadc; ih++){
596                      Int_t pl = GetPlaneIndex( (op->pmtadc).At(ih) );                      Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
597                      if(pl == 1 || pl == 2 || pl == 5)nphit_p++;                      if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
598                  };                  };
599                                    
600                  for (Int_t ih=0; ih < oi->npmtadc; ih++){                  for (Int_t ih=0; ih < oi->npmtadc; ih++){
601                      Int_t pl = GetPlaneIndex( (oi->pmtadc).At(ih) );                      Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
602                      if(pl == 1 || pl == 2 || pl == 5)nphit_i++;                      if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
603                  };                  };
604                                    
# Line 331  void PamLevel2::SortTracks(TString how){ Line 610  void PamLevel2::SortTracks(TString how){
610                      if( nphit_p >= nphit_i) tp_score++;                      if( nphit_p >= nphit_i) tp_score++;
611                      else ti_score++;                      else ti_score++;
612                  };                  };
613    //              cout << "TOF "<<tp_score<<ti_score<<endl;
614              };              };
615              if(tp_score == ti_score) use_TRK = true;              if(tp_score == ti_score) use_TRK = true;
616              // ------------------------              // ------------------------
# Line 342  void PamLevel2::SortTracks(TString how){ Line 622  void PamLevel2::SortTracks(TString how){
622              if(use_TRK){              if(use_TRK){
623                  if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;                  if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
624                  else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;                  else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
625    //              cout << "TRK "<<tp_score<<ti_score<<endl;
626              };              };
627                            
628              // ------------------------              // ------------------------
629              // the winner is....              // the winner is....
630              // ------------------------                  // ------------------------    
631              if      (tp_score > ti_score) ts = tp;//the track sorted by the tracker!!              if      (tp_score > ti_score) {
632              else if (tp_score < ti_score) ts = ti;//its image!!  //              ts = tp;//the track sorted by the tracker!!
633              else {  //              cs = cp;
634                  ts = tp;  //              os = op;
635                }else if (tp_score < ti_score) {
636                    ts = ti;//its image!!
637                    cs = ci;
638                    os = oi;
639    
640                    ti = tp;//its image!!
641                    ci = cp;
642                    oi = op;
643    
644                    tp = ts;//its image!!
645                    cp = cs;
646                    op = os;
647    
648                    
649                }else {
650    //              ts = tp;
651    //              cs = cp;
652    //              os = op;
653  //                              cout << "Warning - track image ambiguity not solved" << endl;  //                              cout << "Warning - track image ambiguity not solved" << endl;
654  //                              cout << ts->GetNtot() << " "<< ts->chi2 << " " << npcfit[1] << " "<< nphit_p << endl;  //                              cout << ts->GetNtot() << " "<< ts->chi2 << " " << npcfit[1] << " "<< nphit_p << endl;
655              };              };
656                            
657          }else{          }else{
658              ts = tp;  //          ts = tp;
659    //          cs = cp;
660    //          os = op;
661          };          };
662                    
663  //              cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;  //      cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
664          sorted_tracks->Add(ts);//save the track in the sorted array  //      sorted_tracks->Add(ts);//save the track in the sorted array
665  //              cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;  //      sorted_tracks.Add(ts);//save the track in the sorted array
666            //      sorted_tracks.Add(tp);//save the track in the sorted array
667      };  //      cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
668    //      cout<<"o "<<tp<<endl;
669    //      cout<<"o "<<cp<<endl;
670    //      cout<<"o "<<op<<endl;
671            new(ttsorted[i]) PamTrack(tp,cp,op);
672            new(ttimage[i])  PamTrack(ti,ci,oi);
673        };
674    
675        if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){
676            cout << "void PamLevel2::SortTracks(TString how): tsorted->GetEntries() "<<tsorted->GetEntries()<<" != trk2_obj->GetNTracks() = "<<trk2_obj->GetNTracks() <<endl;
677            tsorted->Delete(); tsorted=0;
678            timage->Delete(); timage=0;    
679        }
680    
681      //Restore Object count      //Restore Object count
682      //To save space in the table keeping track of all referenced objects      //To save space in the table keeping track of all referenced objects
683      //we assume that our events do not address each other. We reset the      //We reset the object count to what it was at the beginning of the event.
     //object count to what it was at the beginning of the event.  
684      TProcessID::SetObjectCount(ObjectNumber);      TProcessID::SetObjectCount(ObjectNumber);
685            
686  };  };
# Line 379  void PamLevel2::SortTracks(TString how){ Line 692  void PamLevel2::SortTracks(TString how){
692   * 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.
693   * 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.
694   */   */
695   TRefArray *PamLevel2::GetTracks(){  // TRefArray *PamLevel2::GetTracks(){
696           //  
697  //      SortTracks("+CAL+TOF");  // //  *-*-*-*-*-*-*-*-*-*-*-*-*
698           SortTracks("+CAL+TRK");//TEMP!!!!!!!!!!!!!  //     SortTracks("+CAL+TOF");
699          //  // //  *-*-*-*-*-*-*-*-*-*-*-*-*
700          return sorted_tracks;  
701    // //   return  sorted_tracks;
702    //     return &sorted_tracks;
703        
704    //  };
705    TClonesArray *PamLevel2::GetTracks(){
706    
707    //  *-*-*-*-*-*-*-*-*-*-*-*-*
708        SortTracks("+CAL+TOF");
709    //  *-*-*-*-*-*-*-*-*-*-*-*-*
710    
711        return tsorted;
712        
713   };   };
714  //--------------------------------------  //--------------------------------------
715   //   //
# Line 396  void PamLevel2::SortTracks(TString how){ Line 721  void PamLevel2::SortTracks(TString how){
721   * @param it Track number, ranging from 0 to GetNTracks().   * @param it Track number, ranging from 0 to GetNTracks().
722   */   */
723  PamTrack *PamLevel2::GetTrack(int it){  PamTrack *PamLevel2::GetTrack(int it){
724    
725    //    if(!trk2_obj) return 0;
726                    
727  //  *-*-*-*-*-*-*  // //  *-*-*-*-*-*-*-*-*-*-*-*-*
728  //      SortTracks("+CAL+TOF");  //     SortTracks("+CAL+TOF");
729      SortTracks("+CAL+TRK");//TEMP!!!!!!!!!!!!!  // //  *-*-*-*-*-*-*-*-*-*-*-*-*
730  //  *-*-*-*-*-*-*  // //    if(!sorted_tracks)return 0;
731    //     if(sorted_tracks.IsEmpty())return 0;
732    
733    //     PamTrack *track = 0;
734        
735    //     if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() && it<sorted_tracks.GetEntriesFast() ){    
736    //      TrkTrack   *t = (TrkTrack*)sorted_tracks.At(it);
737    //      track = GetPamTrackAlong(t);
738    //     }else{
739    //      cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
740    //     };
741        
742    //     return track;
743    
744    //    cout << "PamLevel2::GetTrack(int it) "<<endl;
745    //  *-*-*-*-*-*-*-*-*-*-*-*-*
746        SortTracks("+CAL+TOF");
747    //  *-*-*-*-*-*-*-*-*-*-*-*-*
748        if(!tsorted)return 0;
749        if(!tsorted->GetEntries())return 0;
750    
751      PamTrack *track = 0;      PamTrack *track = 0;
752            
753      if( it >=0 && it < TrkLevel2::GetNTracks() && it<sorted_tracks->GetEntriesFast() ){      if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks()){
754          TrkTrack   *t = (TrkTrack*)sorted_tracks->At(it);  //      TrkTrack   *t = (TrkTrack*)sorted_tracks.At(it);
755          track = GetPamTrackAlong(t);  //      track = GetPamTrackAlong(t);
756            TClonesArray &t = *(tsorted);
757            track = (PamTrack*)t[it];
758            
759      }else{      }else{
760          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;
761      };      };
762            
763      return track;      return track;
# Line 425  PamTrack *PamLevel2::GetTrack(int it){ Line 774  PamTrack *PamLevel2::GetTrack(int it){
774   */   */
775  PamTrack *PamLevel2::GetTrackImage(int it){  PamTrack *PamLevel2::GetTrackImage(int it){
776    
777  //      SortTracks("+CAL+TOF");  //     if(!trk2_obj) return 0;
         SortTracks("+CAL+TRK");//TEMP!!!!!!!!!!!!!  
           
         PamTrack *image = 0;  
778    
779          if( it >=0 && it < TrkLevel2::GetNTracks() ){  // //  *-*-*-*-*-*-*-*-*-*-*-*-*
780                  TrkTrack *temp = (TrkTrack*)sorted_tracks->At(it);  //     SortTracks("+CAL+TOF");
781                  if( temp->HasImage() ){  // //  *-*-*-*-*-*-*-*-*-*-*-*-*
782                          TrkTrack   *t = TrkLevel2::GetStoredTrack(temp->GetImageSeqNo());  // //    if(!sorted_tracks)return 0;
783                          image = GetPamTrackAlong(t);  //     if(sorted_tracks.IsEmpty())return 0;
784                  }else{      
785                          cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;  //     PamTrack *image = 0;
786                  };      
787    //     if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() ){
788    //      TrkTrack *temp = (TrkTrack*)sorted_tracks.At(it);
789    //      if( temp->HasImage() ){
790    //          TrkTrack   *t = trk2_obj->TrkLevel2::GetStoredTrack(temp->GetImageSeqNo());
791    //          image = GetPamTrackAlong(t);
792    //      }else{
793    //          cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;
794    //      };
795    //     }else{
796    //      cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
797    //     };
798        
799    //     return image;
800    
801    
802    //  *-*-*-*-*-*-*-*-*-*-*-*-*
803        SortTracks("+CAL+TOF");
804    //  *-*-*-*-*-*-*-*-*-*-*-*-*
805        if(!timage)return 0;
806        if(!timage->GetEntries())return 0;
807        
808        PamTrack *image = 0;
809        
810        if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() ){
811            TClonesArray &t = *(tsorted);
812            PamTrack *temp = (PamTrack*)t[it];
813            if( temp->GetTrkTrack()->HasImage() ){
814                TClonesArray &t = *(timage);
815                image = (PamTrack*)t[it];
816          }else{          }else{
817                  cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<TrkLevel2::GetNTracks()<<")"<<endl;  //          cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;
818          };          };
819        }else{
820            cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
821        };
822            
823          return image;      return image;
824  }  }
825    
826  //--------------------------------------  //--------------------------------------
# Line 455  PamTrack *PamLevel2::GetTrackImage(int i Line 833  PamTrack *PamLevel2::GetTrackImage(int i
833   * @param detlist String to select trees to be included   * @param detlist String to select trees to be included
834   * @return Pointer to a TTree   * @return Pointer to a TTree
835   */   */
836  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist="+ALL"){  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ){
837    
838  //      cout << "WARNING!!! -- obsolete method -- \n";  //     if( !detlist.IsNull() ) SetWhichTrees(detlist);
839  //      cout << "better use TChain *PamLevel2::GetPamTree(TList*, TString) \n";  //     else                    GetWhichTrees(f);
           
     TTree *Trout =0;  
840            
841        cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;
842    
843      SetWhichTrees(detlist);      SetWhichTrees(detlist);
844    
845        TTree *Trout =0;
846    
847        TString fname = f->GetName();
848        if(!CheckLevel2File(fname))return NULL;
849    
850    //    UInt_t *found=0;    
851    
852        cout<< "GetPamTree(TFile*,TString): detector list --> ";
853        if(TRK1)cout<<"TRK1 ";
854        if(TRK2)cout<<"TRK2 ";
855        if(TRKh)cout<<"TRKH ";
856        if(CAL1)cout<<"CAL1 ";
857        if(CAL2)cout<<"CAL2 ";
858        if(TOF)cout<<"TOF ";
859        if(TRG)cout<<"TRG ";
860        if(AC)cout<<"AC ";
861        if(ND)cout<<"ND ";
862        if(S4)cout<<"S4 ";
863        if(ORB)cout<<"ORB ";
864        cout << endl;
865        if(SELLI)cout<<">>> Found selection-list <<<"<<endl;
866    
867        f->cd();
868    
869  // Tracker  // Tracker
870      TTree *T = (TTree*)f->Get("Tracker");      TTree *T = (TTree*)f->Get("Tracker");
871      if(T && TRK) {      if(T && (TRK2||TRK1||TRKh)) {
872          if(TRK_L1){          if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
873              T->SetBranchAddress("TrkLevel1", GetPointerToTrk(1));  //      else    T->SetBranchStatus("TrkLevel2",0,found);
874              cout << "Tracker      : set branch address TrkLevel1"<<endl;          if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;
875          };          if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
876          T->SetBranchAddress("TrkLevel2", GetPointerToTrk(2));  //      else    T->SetBranchStatus("TrkLevel1",0,found);
877          cout << "Tracker      : set branch address TrkLevel2"<<endl;          if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;
878          Trout=T;          if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
879    //      else    T->SetBranchStatus("TrkHough",0,found);
880            if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;
881            if(!Trout)Trout=T;
882            else Trout->AddFriend(T);
883      }else{      }else{
884          cout << "Tracker      : missing tree"<<endl;          cout << "Tracker      : missing tree"<<endl;
885      };      };
886      // Calorimeter      // Calorimeter
887      TTree *C = (TTree*)f->Get("Calorimeter");      TTree *C = (TTree*)f->Get("Calorimeter");
888      if(C && CAL) {      if(C && (CAL2||CAL1)) {
889          C->SetBranchAddress("CaloLevel2", GetPointerToCalo());          if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
890          cout << "Calorimeter  : set branch address CaloLevel2"<<endl;  //      else    C->SetBranchStatus("CaloLevel2",0,found);
891            if(CAL2)cout << "Calorimeter  : set branch address CaloLevel2"<<endl;
892            if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
893    //      else    C->SetBranchStatus("CaloLevel1",0,found);
894            if(CAL1)cout << "Calorimeter  : set branch address CaloLevel1"<<endl;
895          if(!Trout)Trout=C;          if(!Trout)Trout=C;
896          else Trout->AddFriend(C);          else Trout->AddFriend(C);
897      }else{      }else{
898          cout << "Calorimeter  : missing tree"<<endl;          cout << "Calorimeter  : missing tree"<<endl;
899      };      };
900    
901      // ToF          // ToF    
902      TTree *O = (TTree*)f->Get("ToF");      TTree *O = (TTree*)f->Get("ToF");
903      if(O && TOF) {      if(O && TOF) {
904          O->SetBranchAddress("ToFLevel2", GetPointerToToF());          O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
905          cout << "ToF          : set branch address ToFLevel2"<<endl;          cout << "ToF          : set branch address ToFLevel2"<<endl;
906          if(!Trout)Trout=O;          if(!Trout)Trout=O;
907          else Trout->AddFriend(O);          else Trout->AddFriend(O);
# Line 499  TTree *PamLevel2::GetPamTree(TFile *f, T Line 911  TTree *PamLevel2::GetPamTree(TFile *f, T
911      // Trigger      // Trigger
912      TTree *R = (TTree*)f->Get("Trigger");      TTree *R = (TTree*)f->Get("Trigger");
913      if(R && TRG) {      if(R && TRG) {
914          R->SetBranchAddress("TrigLevel2", GetPointerToTrig());          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
915          cout << "Trigger      : set branch address TrigLevel2"<<endl;          cout << "Trigger      : set branch address TrigLevel2"<<endl;
916          if(!Trout)Trout=O;          if(!Trout)Trout=O;
917          else Trout->AddFriend(R);          else Trout->AddFriend(R);
# Line 509  TTree *PamLevel2::GetPamTree(TFile *f, T Line 921  TTree *PamLevel2::GetPamTree(TFile *f, T
921      // S4      // S4
922      TTree *S = (TTree*)f->Get("S4");      TTree *S = (TTree*)f->Get("S4");
923      if(S && S4) {      if(S && S4) {
924          S->SetBranchAddress("S4Level2", GetPointerToS4());          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
925          cout << "S4           : set branch address S4Level2"<<endl;          cout << "S4           : set branch address S4Level2"<<endl;
926          if(!Trout)Trout=O;          if(!Trout)Trout=O;
927          else Trout->AddFriend(S);          else Trout->AddFriend(S);
# Line 519  TTree *PamLevel2::GetPamTree(TFile *f, T Line 931  TTree *PamLevel2::GetPamTree(TFile *f, T
931      // Neutron Detector      // Neutron Detector
932      TTree *N = (TTree*)f->Get("NeutronD");      TTree *N = (TTree*)f->Get("NeutronD");
933      if(N && ND) {      if(N && ND) {
934          N->SetBranchAddress("NDLevel2", GetPointerToND());          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
935          cout << "NeutronD     : set branch address NDLevel2"<<endl;          cout << "NeutronD     : set branch address NDLevel2"<<endl;
936          if(!Trout)Trout=O;          if(!Trout)Trout=O;
937          else Trout->AddFriend(N);          else Trout->AddFriend(N);
# Line 529  TTree *PamLevel2::GetPamTree(TFile *f, T Line 941  TTree *PamLevel2::GetPamTree(TFile *f, T
941      // Anticounters      // Anticounters
942      TTree *A = (TTree*)f->Get("Anticounter");      TTree *A = (TTree*)f->Get("Anticounter");
943      if(A && AC) {      if(A && AC) {
944          A->SetBranchAddress("AcLevel2", GetPointerToAc());          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
945          cout << "Anticounter  : set branch address AcLevel2"<<endl;          cout << "Anticounter  : set branch address AcLevel2"<<endl;
946          if(!Trout)Trout=O;          if(!Trout)Trout=O;
947          else Trout->AddFriend(A);          else Trout->AddFriend(A);
# Line 537  TTree *PamLevel2::GetPamTree(TFile *f, T Line 949  TTree *PamLevel2::GetPamTree(TFile *f, T
949          cout << "Anticounter  : missing tree"<<endl;          cout << "Anticounter  : missing tree"<<endl;
950      };      };
951      // Orbital Info      // Orbital Info
952          TTree *B = (TTree*)f->Get("OrbitalInfo");      TTree *B = (TTree*)f->Get("OrbitalInfo");
953          if(B && ORB) {      if(B && ORB) {
954                  B->SetBranchAddress("OrbitalInfo", GetPointerToOrb());          B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
955                  cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
956                  if(!Trout)Trout=O;          if(!Trout)Trout=O;
957                  else Trout->AddFriend(B);          else Trout->AddFriend(B);
958          }else{      }else{
959                  cout << "OrbitalInfo  : missing tree"<<endl;          cout << "OrbitalInfo  : missing tree"<<endl;
960          };      };
961    
962        TTree *L = (TTree*)f->Get("SelectionList");
963        if(L && SELLI==1) {
964    //      L->SetBranchAddress("RunEntry",&irun);
965    //      cout << "SelectionList: set branch address RunEntry"<<endl;
966    //      L->SetBranchAddress("EventEntry",&irunentry);
967    //      cout << "SelectionList: set branch address EventEntry"<<endl;
968    //      if(!Trout)Trout=O;
969    //      else Trout->AddFriend("SelectionList");
970            cout << " TTree *PamLevel2::GetPamTree(TFile, TString) >>> SelectionList not implemente!!!!"<<endl;
971            sel_tree = 0;
972        }else{
973            cout << "SelectionList  : missing tree"<<endl;
974        };
975            
976          cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;      cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;
977            
978          return Trout;      pam_tree = (TChain*)Trout;
979        
980        return Trout;
981            
982  }  }
983  //--------------------------------------  //--------------------------------------
# Line 574  TList*  PamLevel2::GetListOfLevel2Files( Line 1002  TList*  PamLevel2::GetListOfLevel2Files(
1002      TList *contents  = new TList; // create output list      TList *contents  = new TList; // create output list
1003      contents->SetOwner();      contents->SetOwner();
1004            
1005      char *fullpath;  //    char *fullpath;
1006    //    const char *fullpath;
1007            
1008      // if no input file list is given:        // if no input file list is given:  
1009      if ( flisttxt != "" ){      if ( flisttxt != "" ){
1010                    
1011          if( !gSystem->IsFileInIncludePath(flisttxt,&fullpath) )return 0;  //      if( !gSystem->IsFileInIncludePath(flisttxt,&fullpath) ){        
1012            //      if( !(fullpath = gSystem->FindFile("./",flisttxt)) ){
1013  //              flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));  //          cout <<"File "<<flisttxt<<" not found"<<endl;
1014          flisttxt = fullpath;  //          return 0;
1015            //      }      
1016          if( !gSystem->ChangeDirectory(ddir) )return 0;  //      flisttxt = fullpath;
1017          if ( !flisttxt.EndsWith(".root") ){
1018    
1019            flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1020    
1021            if( !gSystem->ChangeDirectory(ddir) ){
1022                cout << "Cannot change directory : "<<ddir<<endl;
1023                return 0;
1024            }
1025                    
1026          cout <<"Input file list : " << flisttxt <<endl;          cout <<"Input file list : " << flisttxt <<endl;
1027          ifstream in;          ifstream in;
1028          in.open(flisttxt, ios::in); //open input file list          in.open(flisttxt, ios::in); //open input file list
1029            if(!in.good()){
1030                cout <<" ERROR opening the file "<<endl;
1031                gSystem->ChangeDirectory(wdir); // back to the working directory
1032                return 0;
1033            }      
1034            int line=0;
1035          while (1) {          while (1) {
1036              TString file;              TString file;
1037              in >> file;              in >> file;
1038              if (!in.good()) break;              if (!in.good()) break;
1039              if( gSystem->IsFileInIncludePath(file,&fullpath) ){              line++;
1040  //                              contents->Add(new TSystemDirectory(fullpath,ddir)); // add file to the list  //          cout <<"(1) " << file << endl;
1041  //                              contents->Add(new TSystemFile(fullpath,ddir)); // add file to the list              if(file.IsNull()){
1042                    cout << "-- list interrupted at line "<<line <<endl;
1043                    break;
1044                }
1045                if(file.Contains("#"))file = file(0,file.First("#"));
1046    //          cout <<"(2) " << file << endl;
1047    //          if( gSystem->IsFileInIncludePath(file,&fullpath) ){
1048    //          if( (fullpath = gSystem->FindFile(ddir,file)) ){
1049                if( file.EndsWith(".root") ){
1050                    char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir),gSystem->BaseName(file));
1051                  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
1052              }else{                  delete fullpath;
1053                  cout << "warning! --- File "<<file<<" does not exists"<< endl;              }
1054              };  //          }else{
1055    //              if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;
1056    //          };
1057          };          };
1058          in.close();          in.close();
1059                  } else {
1060              char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1061              contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list
1062              delete fullpath;
1063          };        
1064      }else{      }else{
1065                    
1066          cout << "No input file list given."<<endl;          cout << "No input file list given."<<endl;
# Line 621  TList*  PamLevel2::GetListOfLevel2Files( Line 1079  TList*  PamLevel2::GetListOfLevel2Files(
1079          while ( (questo = (TSystemFile*) next()) ) {          while ( (questo = (TSystemFile*) next()) ) {
1080              TString name =  questo-> GetName();              TString name =  questo-> GetName();
1081              if( name.EndsWith(".root") ){              if( name.EndsWith(".root") ){
1082                  char *fullpath;  //              const char *fullpath = gSystem->FindFile(ddir,name);
1083                  gSystem->IsFileInIncludePath(name,&fullpath);  //              char *fullpath;
1084    //              gSystem->IsFileInIncludePath(name,&fullpath);
1085                    char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir),gSystem->BaseName(name));
1086                  contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));                  contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));
1087                    delete fullpath;
1088              };              };
1089          }          }
1090          delete temp;          delete temp;
# Line 633  TList*  PamLevel2::GetListOfLevel2Files( Line 1094  TList*  PamLevel2::GetListOfLevel2Files(
1094      gSystem->ChangeDirectory(wdir); // back to the working directory      gSystem->ChangeDirectory(wdir); // back to the working directory
1095  //      cout << endl << "Selected files:" << endl;  //      cout << endl << "Selected files:" << endl;
1096  //      contents->Print();  //      contents->Print();
1097      cout << contents->GetEntries()<<" files selected\n";      cout << contents->GetEntries()<<" files \n";
1098  //      cout << endl;  //      cout << endl;
1099  //      cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;  //      cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;
1100      return contents;      return contents;
# Line 648  TList*  PamLevel2::GetListOfLevel2Files( Line 1109  TList*  PamLevel2::GetListOfLevel2Files(
1109   * @param detlist String to select trees to be included   * @param detlist String to select trees to be included
1110   * @return Pointer to a TChain   * @return Pointer to a TChain
1111   */   */
1112  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist="+ALL"){  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){
   
 //      TChain *Tout=0;  
     if(Tout)Tout->Delete();  
     Tout = NULL;  
1113            
1114        TChain *Trout =0;
1115    
1116    //    if( !detlist.IsNull() )SetWhichTrees(detlist);
1117      SetWhichTrees(detlist);      SetWhichTrees(detlist);
1118            
1119        cout<< "GetPamTree(TList*,TString): input detector list --> ";
1120        if(TRK1)cout<<"TRK1 ";
1121        if(TRK2)cout<<"TRK2 ";
1122        if(TRKh)cout<<"TRKH ";
1123        if(CAL1)cout<<"CAL1 ";
1124        if(CAL2)cout<<"CAL2 ";
1125        if(TOF)cout<<"TOF ";
1126        if(TRG)cout<<"TRG ";
1127        if(AC)cout<<"AC ";
1128        if(ND)cout<<"ND ";
1129        if(S4)cout<<"S4 ";
1130        if(ORB)cout<<"ORB ";
1131        cout << endl;
1132    
1133      TChain *T = 0;            TChain *T = 0;      
1134      TChain *C = 0;      TChain *C = 0;
1135      TChain *O = 0;      TChain *O = 0;
# Line 664  TChain *PamLevel2::GetPamTree(TList *fl, Line 1138  TChain *PamLevel2::GetPamTree(TList *fl,
1138      TChain *N = 0;      TChain *N = 0;
1139      TChain *A = 0;      TChain *A = 0;
1140      TChain *B = 0;      TChain *B = 0;
1141    
1142        TChain *L = 0;
1143            
1144      if(TRK) T = new TChain("Tracker");        if(TRK2||TRK1||TRKh) T = new TChain("Tracker");    
1145      if(CAL) C = new TChain("Calorimeter");      if(CAL2||CAL1)       C = new TChain("Calorimeter");
1146      if(TOF) O = new TChain("ToF");      if(TOF)              O = new TChain("ToF");
1147      if(TRG) R = new TChain("Trigger");      if(TRG)              R = new TChain("Trigger");
1148      if(S4)  S = new TChain("S4");      if(S4)               S = new TChain("S4");
1149      if(ND)  N = new TChain("NeutronD");      if(ND)               N = new TChain("NeutronD");
1150      if(AC)  A = new TChain("Anticounter");      if(AC)               A = new TChain("Anticounter");
1151      if(ORB) B = new TChain("OrbitalInfo");      if(ORB)              B = new TChain("OrbitalInfo");
1152    
1153         L = new TChain("SelectionList");
1154            
1155      // loop over files and create chains              // loop over files and create chains        
1156      TIter next(fl);      TIter next(fl);
1157      TSystemFile *questo = 0;      TSystemFile *questo = 0;
1158      while ( (questo = (TSystemFile*) next()) ) {      while ( (questo = (TSystemFile*) next()) ) {
1159          TString name =  questo->GetName();          TString name =  questo->GetName();
1160  //              cout << "File: "<< name << endl;          cout << "File: "<< name << endl;
1161          if( CheckLevel2File(name) ){          if( CheckLevel2File(name) ){
1162              if(TRK) T->Add(name);              if(TRK2||TRK1||TRKh) T->Add(name);
1163              if(CAL) C->Add(name);              if(CAL1||CAL2)       C->Add(name);
1164              if(TOF) O->Add(name);              if(TOF)              O->Add(name);
1165              if(TRG) R->Add(name);              if(TRG)              R->Add(name);
1166              if(S4)  S->Add(name);              if(S4)               S->Add(name);
1167              if(ND)  N->Add(name);              if(ND)               N->Add(name);
1168              if(AC)  A->Add(name);              if(AC)               A->Add(name);
1169              if(ORB) B->Add(name);              if(ORB)              B->Add(name);
1170                if(SELLI==1)         L->Add(name);
1171          };          };
1172      }      }
1173            
1174      cout << "done chain \n";      cout << "done chain \n";
1175    
1176    //    UInt_t *found=0;
1177    // Tracker
1178        if(T && (TRK2||TRK1||TRKh)) {
1179            if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
1180    //      else    T->SetBranchStatus("TrkLevel2",0,found);
1181            if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;
1182            if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
1183    //      else    T->SetBranchStatus("TrkLevel1",0,found);
1184            if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;
1185            if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1186    //      else    T->SetBranchStatus("TrkHough",0,found);
1187            if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;
1188            if(!Trout)Trout=T;
1189            else Trout->AddFriend("Tracker");
1190        }else{
1191            cout << "Tracker      : missing tree"<<endl;
1192        };
1193        // Calorimeter
1194        if(C && (CAL2||CAL1)) {
1195            if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1196    //      else    C->SetBranchStatus("CaloLevel2",0,found);
1197            if(CAL2)cout << "Calorimeter  : set branch address CaloLevel2"<<endl;
1198            if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1199    //      else    C->SetBranchStatus("CaloLevel1",0,found);
1200            if(CAL1)cout << "Calorimeter  : set branch address CaloLevel1"<<endl;
1201            if(!Trout)Trout=C;
1202            else Trout->AddFriend("Calorimeter");
1203        }else{
1204            cout << "Calorimeter  : missing tree"<<endl;
1205        };
1206        // ToF    
1207        if(O && TOF) {
1208            O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
1209            cout << "ToF          : set branch address ToFLevel2"<<endl;
1210            if(!Trout)Trout=O;
1211            else Trout->AddFriend("ToF");
1212        }else{
1213            cout << "ToF         : missing tree"<<endl;
1214        };
1215        // Trigger
1216        if(R && TRG) {
1217            R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1218            cout << "Trigger      : set branch address TrigLevel2"<<endl;
1219            if(!Trout)Trout=O;
1220            else Trout->AddFriend("Trigger");
1221        }else{
1222            cout << "Trigger      : missing tree"<<endl;
1223        };
1224        // S4
1225        if(S && S4) {
1226            S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1227            cout << "S4           : set branch address S4Level2"<<endl;
1228            if(!Trout)Trout=O;
1229            else Trout->AddFriend("S4");
1230        }else{
1231            cout << "S4           : missing tree"<<endl;
1232        };
1233        // Neutron Detector
1234        if(N && ND) {
1235            N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1236            cout << "NeutronD     : set branch address NDLevel2"<<endl;
1237            if(!Trout)Trout=O;
1238            else Trout->AddFriend("NeutronD");
1239        }else{
1240            cout << "NeutronD     : missing tree"<<endl;
1241        };
1242        // Anticounters
1243        if(A && AC) {
1244            A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1245            cout << "Anticounter  : set branch address AcLevel2"<<endl;
1246            if(!Trout)Trout=O;
1247            else Trout->AddFriend("Anticounter");
1248        }else{
1249            cout << "Anticounter  : missing tree"<<endl;
1250        };
1251        // Orbital Info
1252        if(B && ORB) {
1253            B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1254            cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
1255            if(!Trout)Trout=O;
1256            else Trout->AddFriend("OrbitalInfo");
1257        }else{
1258            cout << "OrbitalInfo  : missing tree"<<endl;
1259        };
1260    
1261        // Selection List
1262        if(L && SELLI==1) {
1263            cout<<">>> Found selection-list <<<"<<endl;
1264            L->SetBranchAddress("RunEntry",&irun);
1265            cout << "SelectionList: set branch address RunEntry"<<endl;
1266            L->SetBranchAddress("EventEntry",&irunentry);
1267            cout << "SelectionList: set branch address EventEntry"<<endl;
1268            sel_tree = L;
1269    //      if(!Trout)Trout=O;
1270    //      else Trout->AddFriend("SelectionList");
1271        }else{
1272    //      cout << "SelectionList  : missing tree"<<endl;
1273            if(L)L->Delete();
1274        };
1275        
1276    //    cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;
1277    
1278        pam_tree = Trout;
1279    
1280        return Trout;
1281    }
1282    
1283    
1284    
1285    //--------------------------------------
1286    //
1287    //
1288    //--------------------------------------
1289    /**
1290     * Set branch addresses for Pamela friend trees
1291     */
1292    void PamLevel2::SetBranchAddress(TTree *t){
1293    
1294        TRK2    = TRK2 & t->GetBranchStatus("TrkLevel2");
1295        TRK1    = TRK1 & t->GetBranchStatus("TrkLevel1");
1296        TRKh    = TRKh & t->GetBranchStatus("TrkHough");
1297        CAL2    = CAL2 & t->GetBranchStatus("CaloLevel2");
1298        CAL1    = CAL1 & t->GetBranchStatus("CaloLevel1");
1299        TOF    = TOF & t->GetBranchStatus("ToFLevel2");
1300        TRG    = TRG & t->GetBranchStatus("TrigLevel2");
1301        S4     = S4  & t->GetBranchStatus("S4Level2");
1302        ND     = ND  & t->GetBranchStatus("NDLevel2");
1303        AC     = AC  & t->GetBranchStatus("AcLevel2");
1304        ORB    = ORB & t->GetBranchStatus("OrbitalInfo");
1305    
1306    
1307      // Tracker      // Tracker
1308      if(TRK) if(!Tout)Tout=T;      if(TRK1) {
1309            t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel2"));
1310            cout << "Tracker      : set branch address TrkLevel1"<<endl;
1311        };
1312        if(TRK2) {
1313            t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));
1314            cout << "Tracker      : set branch address TrkLevel2"<<endl;
1315        };
1316        if(TRKh) {
1317            t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1318            cout << "Tracker      : set branch address TrkHough"<<endl;
1319        };
1320        
1321      // Calorimeter      // Calorimeter
1322      if(CAL) {                if(CAL1) {
1323          if(!Tout)Tout=C;          t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1324          else Tout->AddFriend("Calorimeter");          cout << "Calorimeter  : set branch address CaloLevel1"<<endl;
1325        };
1326        if(CAL2) {
1327            t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1328            cout << "Calorimeter  : set branch address CaloLevel2"<<endl;
1329      };      };
1330        
1331      // ToF          // ToF    
1332      if(TOF) {      if(TOF) {
1333          if(!Tout)Tout=O;          t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
1334          else Tout->AddFriend("ToF");          cout << "ToF          : set branch address ToFLevel2"<<endl;
1335      };      };
1336      // Trigger      // Trigger
1337      if(TRG) {      if(TRG) {
1338          if(!Tout)Tout=O;          t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1339          else Tout->AddFriend("Trigger");          cout << "Trigger      : set branch address TrigLevel2"<<endl;
1340      };      };
1341      // S4      // S4
1342      if(S4) {      if(S4) {
1343          if(!Tout)Tout=O;          t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1344          else Tout->AddFriend("S4");          cout << "S4           : set branch address S4Level2"<<endl;
1345      };      };
1346      // Neutron Detector      // Neutron Detector
1347      if(ND) {      if(ND) {
1348          if(!Tout)Tout=O;          t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1349          else Tout->AddFriend("NeutronD");          cout << "NeutronD     : set branch address NDLevel2"<<endl;
1350      };      };
1351      // Anticounters      // Anticounters
1352      if(AC) {      if(AC) {
1353          if(!Tout)Tout=O;          t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1354          else Tout->AddFriend("Anticounter");          cout << "Anticounter  : set branch address AcLevel2"<<endl;
1355      };      };
1356      // OrbitalInfo      // OrbitalInfo
1357      if(ORB) {      if(ORB) {
1358          if(!Tout)Tout=O;          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1359          else Tout->AddFriend("OrbitalInfo");          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
1360        };
1361        // SelectionList
1362        if(SELLI==1) {
1363            t->SetBranchAddress("RunEntry", &irun);
1364            cout << "SelectionList: set branch address RunEntry"<<endl;
1365            t->SetBranchAddress("EventEntry", &irunentry);
1366            cout << "SelectionList: set branch address EventEntry"<<endl;
1367      };      };
1368            
 //    cout<<endl<<" Number of entries: "<<Tout->GetEntries()<<endl<<endl;  
       
     if( Tout->GetEntries() )PamLevel2::SetBranchAddress();  
       
     return Tout;  
1369  }  }
 //--------------------------------------  
 //  
 //  
 //--------------------------------------  
1370  /**  /**
1371   * Set branch addresses for Pamela friend trees   * Set branch addresses for Pamela friend trees
1372   */   */
1373  void PamLevel2::SetBranchAddress(){  void PamLevel2::SetBranchAddress(TChain *t){
1374    
1375        TRK2    = TRK2 & t->GetBranchStatus("TrkLevel2");
1376        TRK1    = TRK1 & t->GetBranchStatus("TrkLevel1");
1377        TRKh    = TRKh & t->GetBranchStatus("TrkHough");
1378        CAL1    = CAL1 & t->GetBranchStatus("CaloLevel1");
1379        CAL2    = CAL2 & t->GetBranchStatus("CaloLevel2");
1380        TOF    = TOF & t->GetBranchStatus("ToFLevel2");
1381        TRG    = TRG & t->GetBranchStatus("TrigLevel2");
1382        S4     = S4  & t->GetBranchStatus("S4Level2");
1383        ND     = ND  & t->GetBranchStatus("NDLevel2");
1384        AC     = AC  & t->GetBranchStatus("AcLevel2");
1385        ORB    = ORB & t->GetBranchStatus("OrbitalInfo");
1386    
1387      // Tracker      // Tracker
1388      if(TRK) {       if(TRK2) {
1389          if(TRK_L1){          t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
             Tout->SetBranchAddress("TrkLevel1", GetPointerToTrk(1));  
             cout << "Tracker      : set branch address TrkLevel1"<<endl;  
         };  
         Tout->SetBranchAddress("TrkLevel2", GetPointerToTrk(2));  
1390          cout << "Tracker      : set branch address TrkLevel2"<<endl;          cout << "Tracker      : set branch address TrkLevel2"<<endl;
1391      };      };
1392        if(TRK1) {
1393            t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
1394            cout << "Tracker      : set branch address TrkLevel1"<<endl;
1395        };
1396         if(TRKh) {
1397            t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1398            cout << "Tracker      : set branch address TrkHough"<<endl;
1399         };
1400            
1401      // Calorimeter      // Calorimeter
1402      if(CAL) {      if(CAL2) {
1403          Tout->SetBranchAddress("CaloLevel2", GetPointerToCalo());          t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1404          cout << "Calorimeter  : set branch address CaloLevel2"<<endl;          cout << "Calorimeter  : set branch address CaloLevel2"<<endl;
1405        };  
1406        if(CAL1) {
1407            t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1408            cout << "Calorimeter  : set branch address CaloLevel1"<<endl;
1409      };      };
1410            
1411      // ToF          // ToF    
1412      if(TOF) {      if(TOF) {
1413          Tout->SetBranchAddress("ToFLevel2", GetPointerToToF());          t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
1414          cout << "ToF          : set branch address ToFLevel2"<<endl;          cout << "ToF          : set branch address ToFLevel2"<<endl;
1415      };      };
1416      // Trigger      // Trigger
1417      if(TRG) {      if(TRG) {
1418          Tout->SetBranchAddress("TrigLevel2", GetPointerToTrig());          t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1419          cout << "Trigger      : set branch address TrigLevel2"<<endl;          cout << "Trigger      : set branch address TrigLevel2"<<endl;
1420      };      };
1421      // S4      // S4
1422      if(S4) {      if(S4) {
1423          Tout->SetBranchAddress("S4Level2", GetPointerToS4());          t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1424          cout << "S4           : set branch address S4Level2"<<endl;          cout << "S4           : set branch address S4Level2"<<endl;
1425      };      };
1426      // Neutron Detector      // Neutron Detector
1427      if(ND) {      if(ND) {
1428          Tout->SetBranchAddress("NDLevel2", GetPointerToND());          t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1429          cout << "NeutronD     : set branch address NDLevel2"<<endl;          cout << "NeutronD     : set branch address NDLevel2"<<endl;
1430      };      };
1431      // Anticounters      // Anticounters
1432      if(AC) {      if(AC) {
1433          Tout->SetBranchAddress("AcLevel2", GetPointerToAc());          t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1434          cout << "Anticounter  : set branch address AcLevel2"<<endl;          cout << "Anticounter  : set branch address AcLevel2"<<endl;
1435      };      };
1436      // OrbitalInfo      // OrbitalInfo
1437      if(ORB) {      if(ORB) {
1438          Tout->SetBranchAddress("OrbitalInfo", GetPointerToOrb());          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1439          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
1440      };      };
1441        // SelectionList
1442        if(SELLI==1) {
1443            t->SetBranchAddress("RunEntry", &irun);
1444            cout << "SelectionList: set branch address RunEntry"<<endl;
1445            t->SetBranchAddress("EventEntry", &irunentry);
1446            cout << "SelectionList: set branch address EventEntry"<<endl;
1447        };
1448            
1449  }  }
1450    
1451  //--------------------------------------  
 //  
 //  
 //--------------------------------------  
 void *PamLevel2::GetPointerTo(const char* c ){  
 //      cout << "objname "<< objname << endl;  
     TString objname = c;  
     if(!objname.CompareTo("TrkLevel1"))return &trk_l1_obj;  
     if(!objname.CompareTo("TrkLevel2"))return &trk_obj;  
       
     return NULL;  
 };  
1452  //--------------------------------------  //--------------------------------------
1453  //  //
1454  //  //
# Line 830  TChain *PamLevel2::GetRunTree(TList *fl) Line 1472  TChain *PamLevel2::GetRunTree(TList *fl)
1472              R->Add(name);              R->Add(name);
1473          };          };
1474      }      }
1475    
1476        if(R->GetNtrees()){
1477            
1478      R->SetBranchAddress("RunInfo", GetPointerToRun());          R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
1479      cout << "Run         : set branch address RunInfo"<<endl;          cout << "Run         : set branch address RunInfo"<<endl;
1480                R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
1481            cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano
1482        }else{
1483            delete R;
1484            R=0;
1485        }
1486    
1487        run_tree = R;
1488    
1489      return R;      return R;
1490            
1491  }  }
# Line 848  TChain *PamLevel2::GetRunTree(TList *fl) Line 1500  TChain *PamLevel2::GetRunTree(TList *fl)
1500   */   */
1501  TTree *PamLevel2::GetRunTree(TFile *f){  TTree *PamLevel2::GetRunTree(TFile *f){
1502    
1503        cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;
1504    
1505      TTree *R = (TTree*)f->Get("Run");      TTree *R = (TTree*)f->Get("Run");
1506            
1507      R->SetBranchAddress("RunInfo", GetPointerToRun());      if(R){
1508      cout << "Run         : set branch address RunInfo"<<endl;          R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
1509                cout << "Run         : set branch address RunInfo"<<endl;
1510            R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
1511            cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano
1512        }
1513    
1514        run_tree = (TChain*)R;
1515    
1516      return R;      return R;
1517            
1518  }  }
1519    /**
1520     * Update the runinfo informations (to be used to have Run infos event by event basis)
1521     * @param run Pointer to the chain/tree which contains run infos
1522     * @return true if a new run has been read, false if it is still the same run
1523     */
1524    Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev){
1525      //
1526      // check if we have already called once GetEntry, if not call it
1527      //
1528      if ( run->GetEntries() <= 0 ) return(false);
1529      //
1530      
1531      Int_t oldrun = irun;
1532      // --------------------------------------
1533      // if it is a full file (not preselected)
1534      // --------------------------------------
1535      if(SELLI==0){
1536          if ( irun < 0 ){
1537              irun = 0;
1538              run->GetEntry(irun);
1539              runfirstentry = 0ULL;
1540              runlastentry += (ULong64_t)(this->GetRunInfo()->NEVENTS);
1541              if ( (ULong64_t)(this->GetRunInfo()->NEVENTS) > 0ULL ) runlastentry -= 1ULL;
1542              return(true);
1543          };      
1544    
1545          if( !GetOrbitalInfo() ){
1546              cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
1547              return(false);
1548          }
1549          // modificato il controllo sull'aggiornamento del run, per evitare problemi
1550          // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)
1551          //
1552          bool fromfirst = true;
1553          //
1554          while ( !(GetOrbitalInfo()->absTime >= GetRunInfo()->RUNHEADER_TIME && GetOrbitalInfo()->absTime <= GetRunInfo()->RUNTRAILER_TIME) && irun < run->GetEntries() ){
1555    //      while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){
1556              irun++;
1557              run->GetEntry(irun);
1558              runfirstentry = runlastentry;
1559              if ( (ULong64_t)(this->GetRunInfo()->NEVENTS) > 0ULL ) runfirstentry += 1ULL;
1560              runlastentry += (ULong64_t)(this->GetRunInfo()->NEVENTS);
1561              //      printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));
1562              //      printf(" abstime %u trailertime %u \n",GetOrbitalInfo()->absTime,GetRunInfo()->RUNTRAILER_TIME);
1563              //      printf(" IDRUN %u \n",GetRunInfo()->ID);
1564              //
1565              prevshift = 0;
1566              //
1567              if ( irun == run->GetEntries()-1 && fromfirst && !(GetOrbitalInfo()->absTime >= GetRunInfo()->RUNHEADER_TIME && GetOrbitalInfo()->absTime <= GetRunInfo()->RUNTRAILER_TIME)){
1568                //      printf(" resetting irun \n");
1569                fromfirst = false;
1570                irun = 0;
1571                run->GetEntry(irun);
1572                runfirstentry = 0ULL;
1573                runlastentry += (ULong64_t)(this->GetRunInfo()->NEVENTS);
1574                if ( (ULong64_t)(this->GetRunInfo()->NEVENTS) > 0ULL ) runlastentry -= 1ULL;
1575              };
1576              //
1577          };
1578          //
1579          if ( !fromfirst && !(GetOrbitalInfo()->absTime >= GetRunInfo()->RUNHEADER_TIME && GetOrbitalInfo()->absTime <= GetRunInfo()->RUNTRAILER_TIME) ) printf(" Something very wrong here: cannot find RUN containing absolute time %u \n",GetOrbitalInfo()->absTime);
1580          //
1581          if ( irun == oldrun || irun >= run->GetEntries() ) return(false);
1582          //
1583          //  printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);
1584          //
1585          return(true);    
1586      };
1587      // ----------------------------------------------------
1588      // if it is a preselected file (there is SelectionList)
1589      // NBNB - the event tree MUST be read first
1590      // ----------------------------------------------------
1591      if(SELLI==1){      
1592          sel_tree->GetEntry(iev);
1593          if(irun != oldrun){
1594              run->GetEntry(irun);
1595              return true;
1596          }
1597          return false;
1598      }
1599    
1600      return false;
1601      //
1602    };
1603    /**
1604     * Update the runinfo informations (to be used to have Run infos event by event basis)
1605     * @param run Pointer to the chain/tree which contains run infos
1606     * @return true if a new run has been read, false if it is still the same run
1607     */
1608    Bool_t PamLevel2::UpdateRunInfo(TTree *run, ULong64_t iev){
1609      return(UpdateRunInfo((TChain*)run,iev));
1610    };
1611    
1612  //--------------------------------------  //--------------------------------------
1613  //  //
1614  //  //
1615  //--------------------------------------  //--------------------------------------
1616  /**  /**
1617   * Set which trees should be analysed   * Set which trees shoul be analysed
1618   * @param detlist TString containing the sequence of trees required   * @param detlist TString containing the sequence of trees required
1619  */  */
1620  void PamLevel2::SetWhichTrees(TString detlist){  void PamLevel2::SetWhichTrees(TString detlist){
1621                    
1622      if(detlist.Contains("+ALL", TString::kIgnoreCase)){      if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){
1623          CAL = true;          CAL0 = false;
1624          TRK = true;          CAL1 = true;
1625          TRK_L1 = false;          CAL2 = true;
1626            TRK2 = true;
1627            TRK1 = false;
1628            TRKh = false;
1629            TRK0 = false;
1630          TRG = true;          TRG = true;
1631          TOF = true;          TOF = true;
1632            TOF0 = false;
1633          S4  = true;          S4  = true;
1634          ND  = true;          ND  = true;
1635          AC  = true;          AC  = true;
1636          ORB = true;          ORB = true;
1637      }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){      }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){
1638          CAL = false;          CAL0 = false;
1639          TRK = false;          CAL1 = false;
1640          TRK_L1 = false;          CAL2 = false;
1641            TRK2 = false;
1642            TRK1 = false;
1643            TRKh = false;
1644            TRK0 = false;
1645          TRG = false;          TRG = false;
1646          TOF = false;          TOF = false;
1647            TOF0 = false;
1648          S4  = false;          S4  = false;
1649          ND  = false;          ND  = false;
1650          AC  = false;          AC  = false;
1651          ORB = false;          ORB = false;
1652      };      };
1653            
1654      if( detlist.Contains("-CAL", TString::kIgnoreCase) )CAL = false;  //  -------------------------------------------------------------------------
1655      else if( detlist.Contains("+CAL", TString::kIgnoreCase) )CAL = true;      if( detlist.Contains("CAL1", TString::kIgnoreCase) ){
1656                if ( detlist.Contains("-CAL1", TString::kIgnoreCase) )CAL1=false;
1657      if( detlist.Contains("-TRK", TString::kIgnoreCase) )TRK = false;          if ( detlist.Contains("+CAL1", TString::kIgnoreCase) )CAL1=true;
1658      else if( detlist.Contains("+TRK", TString::kIgnoreCase) )TRK = true;      };
1659        
1660      if( detlist.Contains("-TRK1", TString::kIgnoreCase) )TRK_L1 = false;      if( detlist.Contains("CAL0", TString::kIgnoreCase) ){
1661      else if( detlist.Contains("+TRK1", TString::kIgnoreCase) )TRK_L1 = true;          if ( detlist.Contains("-CAL0", TString::kIgnoreCase) )CAL0=false;
1662            if ( detlist.Contains("+CAL0", TString::kIgnoreCase) )CAL0=true;
1663        };
1664            
1665        if( detlist.Contains("CAL2", TString::kIgnoreCase)){
1666            if ( detlist.Contains("-CAL2", TString::kIgnoreCase) )CAL2=false;
1667            if ( detlist.Contains("+CAL2", TString::kIgnoreCase) )CAL2=true;
1668        };
1669            
1670        if( detlist.Contains("+CAL", TString::kIgnoreCase) && !CAL1 && !CAL2 )CAL2=true;
1671        if( detlist.Contains("-CAL", TString::kIgnoreCase) && CAL1 && CAL2 ){
1672            CAL2=false;
1673            CAL1=false;
1674        }
1675    //  -------------------------------------------------------------------------
1676        if( detlist.Contains("TRK0", TString::kIgnoreCase) ){
1677            if ( detlist.Contains("-TRK0", TString::kIgnoreCase) )TRK0=false;
1678            if ( detlist.Contains("+TRK0", TString::kIgnoreCase) )TRK0=true;
1679        };
1680    
1681        if( detlist.Contains("TRK1", TString::kIgnoreCase) ){
1682            if ( detlist.Contains("-TRK1", TString::kIgnoreCase) )TRK1=false;
1683            if ( detlist.Contains("+TRK1", TString::kIgnoreCase) )TRK1=true;
1684        };
1685            
1686        if( detlist.Contains("TRK2", TString::kIgnoreCase)){
1687            if ( detlist.Contains("-TRK2", TString::kIgnoreCase) )TRK2=false;
1688            if ( detlist.Contains("+TRK2", TString::kIgnoreCase) )TRK2=true;
1689        };
1690    
1691        if( detlist.Contains("TRKh", TString::kIgnoreCase)){
1692            if ( detlist.Contains("-TRKh", TString::kIgnoreCase) )TRKh=false;
1693            if ( detlist.Contains("+TRKh", TString::kIgnoreCase) )TRKh=true;
1694        };
1695            
1696        if( detlist.Contains("+TRK", TString::kIgnoreCase) && !TRK1 && !TRK2 && !TRKh )TRK2=true;
1697        if( detlist.Contains("-TRK", TString::kIgnoreCase) && TRK1 && TRK2 && TRKh){
1698            TRK2=false;
1699            TRK1=false;
1700            TRKh=false;
1701        }
1702    //  -------------------------------------------------------------------------
1703            
1704      if( detlist.Contains("-TRG", TString::kIgnoreCase) )TRG = false;      if( detlist.Contains("-TRG", TString::kIgnoreCase) )TRG = false;
1705      else if( detlist.Contains("+TRG", TString::kIgnoreCase) )TRG = true;      else if( detlist.Contains("+TRG", TString::kIgnoreCase) )TRG = true;
1706            
1707      if( detlist.Contains("-TOF", TString::kIgnoreCase) )TOF = false;      if( detlist.Contains("-TOF", TString::kIgnoreCase) )TOF = false;
1708      else if( detlist.Contains("+TOF", TString::kIgnoreCase) )TOF = true;      else if( detlist.Contains("+TOF", TString::kIgnoreCase) )TOF = true;
1709    
1710        if( detlist.Contains("-TOF0", TString::kIgnoreCase) )TOF0 = false;
1711        else if( detlist.Contains("+TOF0", TString::kIgnoreCase) )TOF0 = true;
1712            
1713      if( detlist.Contains("-S4",  TString::kIgnoreCase) )S4  = false;      if( detlist.Contains("-S4",  TString::kIgnoreCase) )S4  = false;
1714      else if( detlist.Contains("+S4",  TString::kIgnoreCase) )S4  = true;      else if( detlist.Contains("+S4",  TString::kIgnoreCase) )S4  = true;
# Line 915  void PamLevel2::SetWhichTrees(TString de Line 1721  void PamLevel2::SetWhichTrees(TString de
1721            
1722      if( detlist.Contains("-ORB", TString::kIgnoreCase) )ORB = false;      if( detlist.Contains("-ORB", TString::kIgnoreCase) )ORB = false;
1723      else if( detlist.Contains("+ORB", TString::kIgnoreCase) )ORB = true;      else if( detlist.Contains("+ORB", TString::kIgnoreCase) )ORB = true;
1724    
1725    //     cout<< "Set detector list --> ";
1726    //     if(TRK1)cout<<"TRK1 ";
1727    //     if(TRK2)cout<<"TRK2 ";
1728    //     if(TRKh)cout<<"TRKH ";
1729    //     if(CAL1)cout<<"CAL1 ";
1730    //     if(CAL2)cout<<"CAL2 ";
1731    //     if(TOF)cout<<"TOF ";
1732    //     if(TRG)cout<<"TRG ";
1733    //     if(AC)cout<<"AC ";
1734    //     if(ND)cout<<"ND ";
1735    //     if(S4)cout<<"S4 ";
1736    //     if(ORB)cout<<"ORB ";
1737    //     cout << endl;
1738        
1739    };
1740    
1741    
1742    /**
1743     * Set tree/branch detector flags from the content of a tree
1744     */
1745    void  PamLevel2::GetWhichTrees(TFile* f){
1746        
1747    
1748    
1749        cout << "void  PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;
1750        // -----------
1751        // reset flags
1752        // -----------
1753        CAL1   = false;    
1754        CAL2   = false;    
1755        TRK2   = false;    
1756        TRK1   = false;    
1757        TRKh   = false;    
1758        TRG    = false;    
1759        TOF    = false;    
1760        S4     = false;    
1761        ND     = false;    
1762        AC     = false;    
1763        ORB    = false;    
1764        
1765        RUN    = false;
1766            
1767        cout << "Checking file: "<<f->GetName()<<endl;
1768        if( !f || f->IsZombie() ){
1769            cout << "File: "<< f->GetName() <<" Non valid root file"<< endl;
1770            return;
1771        }
1772    
1773        TList *lk = f->GetListOfKeys();
1774        TIter next(lk);
1775        TKey *key =0;
1776    
1777        Int_t nev = 0;
1778    
1779        while( (key = (TKey*)next()) ){
1780            
1781            if( !strcmp(key->GetName(),"Run"        ) )RUN = true;
1782    
1783            //=========================================================    
1784            if( !strcmp(key->GetName(),"Trigger"    ) ){
1785                TRG = true;
1786                Int_t nevt = ((TTree*)f->Get("Trigger"))->GetEntries();
1787                if( nev && nevt!=nev){
1788                    cout << "File: "<< f->GetName() <<" Trigger tree has "<<nevt<<" events instead of "<<nev<< endl;
1789                    TRG = false;
1790                }else nev=nevt;
1791            }
1792            //=========================================================    
1793            if( !strcmp(key->GetName(),"ToF"        ) ){
1794                TOF = true;
1795                Int_t nevt = ((TTree*)f->Get("ToF"))->GetEntries();
1796                if( nev && nevt!=nev){
1797                    cout << "File: "<< f->GetName() <<"     ToF tree has "<<nevt<<" events instead of "<<nev<< endl;
1798                    TOF = false;
1799                }else nev=nevt;
1800            }
1801            //=========================================================  
1802            if( !strcmp(key->GetName(),"S4"         ) ){
1803                S4 = true;
1804                Int_t nevt = ((TTree*)f->Get("S4"))->GetEntries();
1805                if( nev && nevt!=nev){
1806                    cout << "File: "<< f->GetName() <<"      S4 tree has "<<nevt<<" events instead of "<<nev<< endl;
1807                    S4 = false;
1808                }else nev=nevt;
1809            }
1810            //=========================================================  
1811    
1812            if( !strcmp(key->GetName(),"NeutronD"   ) ){
1813                ND = true;
1814                Int_t nevt = ((TTree*)f->Get("NeutronD"))->GetEntries();
1815                if( nev && nevt!=nev){
1816                    cout << "File: "<< f->GetName() <<"NeutronD tree has "<<nevt<<" events instead of "<<nev<< endl;
1817                    ND =false;
1818                }else nev=nevt;
1819            }      
1820            //=========================================================  
1821            if( !strcmp(key->GetName(),"Anticounter") ){
1822                AC = true;
1823                Int_t nevt = ((TTree*)f->Get("Anticounter"))->GetEntries();
1824                if( nev && nevt!=nev){
1825                    cout << "File: "<< f->GetName() <<" Anticounter tree has "<<nevt<<" events instead of "<<nev<< endl;
1826                    AC =false;
1827                }else nev=nevt;
1828            }
1829            //=========================================================  
1830            if( !strcmp(key->GetName(),"OrbitalInfo") ){
1831                ORB = true;
1832                Int_t nevt = ((TTree*)f->Get("OrbitalInfo"))->GetEntries();
1833                if( nev && nevt!=nev){
1834                    cout << "File: "<< f->GetName() <<" OrbitalInfo tree has "<<nevt<<" events instead of "<<nev<< endl;
1835                    ORB = false;
1836                }else nev=nevt;
1837            }
1838            //=========================================================  
1839            if( !strcmp(key->GetName(),"Tracker"    ) ){
1840                TTree *T = (TTree*)f->Get("Tracker");
1841                for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
1842                    TString name = T->GetListOfBranches()->At(i)->GetName();
1843                    if( !name.CompareTo("TrkLevel1") )TRK1=true;
1844                    if( !name.CompareTo("TrkLevel2") )TRK2=true;
1845                    if( !name.CompareTo("TrkHough") )TRKh=true;
1846                };      
1847                Int_t nevt = T->GetEntries();
1848                if( nev && nevt!=nev){
1849                    cout << "File: "<< f->GetName() <<" Tracker tree has "<<nevt<<" events instead of "<<nev<< endl;
1850                    TRK1 = false;
1851                    TRK2 = false;
1852                    TRKh = false;
1853                }else nev=nevt;
1854                T->Delete();
1855            };
1856            //=========================================================  
1857            if( !strcmp(key->GetName(),"Calorimeter"    ) ){
1858                TTree *T = (TTree*)f->Get("Calorimeter");
1859                for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
1860                    TString name = T->GetListOfBranches()->At(i)->GetName();
1861                    if( !name.CompareTo("CaloLevel1") )CAL1=true;
1862                    if( !name.CompareTo("CaloLevel2") )CAL2=true;
1863                };    
1864                Int_t nevt = T->GetEntries();
1865                if( nev && nevt!=nev){
1866                    cout << "File: "<< f->GetName() <<"  Calorimeter tree has "<<nevt<<" events instead of "<<nev<< endl;
1867                    CAL1 = false;
1868                    CAL2 = false;
1869                }else nev=nevt;
1870                T->Delete();
1871            };      
1872    
1873        };
1874            
1875        delete lk;
1876    
1877    //     cout<< "Get detector list from input file --> ";
1878    //     if(TRK1)cout<<"TRK1 ";
1879    //     if(TRK2)cout<<"TRK2 ";
1880    //     if(TRKh)cout<<"TRKH ";
1881    //     if(CAL1)cout<<"CAL1 ";
1882    //     if(CAL2)cout<<"CAL2 ";
1883    //     if(TOF)cout<<"TOF ";
1884    //     if(TRG)cout<<"TRG ";
1885    //     if(AC)cout<<"AC ";
1886    //     if(ND)cout<<"ND ";
1887    //     if(S4)cout<<"S4 ";
1888    //     if(ORB)cout<<"ORB ";
1889    //     cout << endl;
1890          
1891        return ;
1892            
1893  };  };
1894    
1895    
1896  //--------------------------------------  //--------------------------------------
1897  //  //
1898  //  //
# Line 928  void PamLevel2::SetWhichTrees(TString de Line 1904  void PamLevel2::SetWhichTrees(TString de
1904   */   */
1905  Bool_t  PamLevel2::CheckLevel2File(TString name){  Bool_t  PamLevel2::CheckLevel2File(TString name){
1906                    
1907      Bool_t CAL__ok = false;          Bool_t CAL1__ok   = false;    
1908      Bool_t TRK__ok = false;          Bool_t CAL2__ok   = false;    
1909      Bool_t TRK_L1__ok = false;          Bool_t TRK2__ok   = false;    
1910      Bool_t TRG__ok = false;          Bool_t TRK1__ok   = false;    
1911      Bool_t TOF__ok = false;          Bool_t TRKh__ok   = false;    
1912      Bool_t S4__ok  = false;          Bool_t TRG__ok    = false;    
1913      Bool_t ND__ok  = false;          Bool_t TOF__ok    = false;    
1914      Bool_t AC__ok  = false;          Bool_t S4__ok     = false;    
1915      Bool_t ORB__ok = false;          Bool_t ND__ok     = false;    
1916        Bool_t AC__ok     = false;    
1917        Bool_t ORB__ok    = false;    
1918            
1919      Bool_t RUN__ok = false;      Bool_t RUN__ok    = false;
1920            
1921  //    cout << "Checking file: "<<name<<endl;      Bool_t SELLI__ok  = false;
1922    
1923        cout << "Checking file: "<<name<<endl;
1924      TFile *f = new TFile(name.Data());      TFile *f = new TFile(name.Data());
1925      if( !f || f->IsZombie() ){      if( !f || f->IsZombie() ){
1926          cout << "File: "<< f->GetName() <<" discarded ---- Non valid root file"<< endl; return false;          cout << "File: "<< f->GetName() <<" discarded ---- Non valid root file"<< endl; return false;
1927      }      }
1928      //   cout << "Get list of keys: "<<f<<endl;  //    cout << "Get list of keys: "<<f<<endl;
1929      TList *lk = f->GetListOfKeys();      TList *lk = f->GetListOfKeys();
1930  //      lk->Print();  //    lk->Print();
1931      TIter next(lk);      TIter next(lk);
1932      TKey *key =0;      TKey *key =0;
1933      Int_t nev =0;  
1934      Int_t nev_previous =0;      Int_t nev = 0;
1935    
1936      while( (key = (TKey*)next()) ){      while( (key = (TKey*)next()) ){
1937                    
1938          nev_previous = nev;  //      cout << key->GetName() << endl;
1939          nev = ((TTree*)f->Get(key->GetName()))->GetEntries();  //      cout << key->GetName() << ""<<key->GetClassName()<<endl;
1940          if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){  //              cout << " Get tree: " << f->Get(key->GetName())<<endl;
1941              cout << "File: "<< f->GetName() <<" discarded ---- "<< key->GetName() << " tree: n.entries does not match "<<nev<<" "<<nev_previous<< endl;  //      nev_previous = nev;
1942              return false;  //      cout << " n.entries  "<< nev <<endl;
1943          };  //      if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){
1944    //          nev = ((TTree*)f->Get(key->GetName()))->GetEntries();
1945    //          cout << "File: "<< f->GetName() <<" discarded ---- "<< key->GetName() << " tree: n.entries does not match "<<nev<<" "<<nev_previous<< endl;
1946    //          return false;
1947    //      };
1948    
1949    
         if( !strcmp(key->GetName(),"Calorimeter") )CAL__ok = true;  
         if( !strcmp(key->GetName(),"Trigger"    ) )TRG__ok = true;  
         if( !strcmp(key->GetName(),"ToF"        ) )TOF__ok = true;  
         if( !strcmp(key->GetName(),"S4"         ) )S4__ok = true;  
         if( !strcmp(key->GetName(),"NeutronD"   ) )ND__ok = true;  
         if( !strcmp(key->GetName(),"Anticounter") )AC__ok = true;  
         if( !strcmp(key->GetName(),"OrbitalInfo") )ORB__ok = true;  
1950          if( !strcmp(key->GetName(),"Run"        ) )RUN__ok = true;                if( !strcmp(key->GetName(),"Run"        ) )RUN__ok = true;      
1951    
1952            //=========================================================    
1953            if( !strcmp(key->GetName(),"SelectionList"    ) ){
1954                SELLI__ok = true;
1955                if(SELLI==1){
1956                    Int_t nevt = ((TTree*)f->Get("SelectionList"))->GetEntries();
1957                    if( nev && nevt!=nev){
1958                        cout << "File: "<< f->GetName() <<" discarded ---- SelectionList tree has "<<nevt<<" events instead of "<<nev<< endl;
1959                        return false;
1960                    }
1961                    nev=nevt;
1962                }
1963            }
1964    
1965            //=========================================================    
1966            if( !strcmp(key->GetName(),"Trigger"    ) ){
1967                TRG__ok = true;
1968                if(TRG){
1969                    Int_t nevt = ((TTree*)f->Get("Trigger"))->GetEntries();
1970                    if( nev && nevt!=nev){
1971                        cout << "File: "<< f->GetName() <<" discarded ---- Trigger tree has "<<nevt<<" events instead of "<<nev<< endl;
1972                        return false;
1973                    }
1974                    nev=nevt;
1975                }
1976            }
1977            //=========================================================    
1978            if( !strcmp(key->GetName(),"ToF"        ) ){
1979                TOF__ok = true;
1980                if(TOF){
1981                    Int_t nevt = ((TTree*)f->Get("ToF"))->GetEntries();
1982                    if( nev && nevt!=nev){
1983                        cout << "File: "<< f->GetName() <<" discarded ---- ToF tree has "<<nevt<<" events instead of "<<nev<< endl;
1984                        return false;
1985                    }
1986                    nev=nevt;
1987                }
1988            }
1989            //=========================================================  
1990            if( !strcmp(key->GetName(),"S4"         ) ){
1991                S4__ok = true;
1992                if(S4){
1993                    Int_t nevt = ((TTree*)f->Get("S4"))->GetEntries();
1994                    if( nev && nevt!=nev){
1995                        cout << "File: "<< f->GetName() <<" discarded ---- S4 tree has "<<nevt<<" events instead of "<<nev<< endl;
1996                        return false;
1997                    }
1998                    nev=nevt;
1999                }
2000            }
2001            //=========================================================  
2002    
2003            if( !strcmp(key->GetName(),"NeutronD"   ) ){
2004                ND__ok = true;
2005                if(ND){
2006                    Int_t nevt = ((TTree*)f->Get("NeutronD"))->GetEntries();
2007                    if( nev && nevt!=nev){
2008                        cout << "File: "<< f->GetName() <<" discarded ---- NeutronD tree has "<<nevt<<" events instead of "<<nev<< endl;
2009                        return false;
2010                    }
2011                    nev=nevt;
2012                }
2013            }      
2014            //=========================================================  
2015            if( !strcmp(key->GetName(),"Anticounter") ){
2016                AC__ok = true;
2017                if(AC){
2018                    Int_t nevt = ((TTree*)f->Get("Anticounter"))->GetEntries();
2019                    if( nev && nevt!=nev){
2020                        cout << "File: "<< f->GetName() <<" discarded ---- Anticounter tree has "<<nevt<<" events instead of "<<nev<< endl;
2021                        return false;
2022                    }
2023                    nev=nevt;
2024                }
2025            }
2026            //=========================================================  
2027            if( !strcmp(key->GetName(),"OrbitalInfo") ){
2028                ORB__ok = true;
2029                if(ORB){
2030                    Int_t nevt = ((TTree*)f->Get("OrbitalInfo"))->GetEntries();
2031                    if( nev && nevt!=nev){
2032                        cout << "File: "<< f->GetName() <<" discarded ---- OrbitalInfo tree has "<<nevt<<" events instead of "<<nev<< endl;
2033                        return false;
2034                    }
2035                    nev=nevt;
2036                }
2037            }
2038            //=========================================================  
2039          if( !strcmp(key->GetName(),"Tracker"    ) ){          if( !strcmp(key->GetName(),"Tracker"    ) ){
             TRK__ok = true;  
2040              TTree *T = (TTree*)f->Get("Tracker");              TTree *T = (TTree*)f->Get("Tracker");
2041                if(TRK1||TRK2||TRKh){
2042                    Int_t nevt = T->GetEntries();
2043                    if( nev && nevt!=nev){
2044                        cout << "File: "<< f->GetName() <<" discarded ---- Tracker tree has "<<nevt<<" events instead of "<<nev<< endl;
2045                        return false;
2046                    }
2047                    nev=nevt;
2048                }
2049              for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){              for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
2050                  TString name = T->GetListOfBranches()->At(i)->GetName();                  TString name = T->GetListOfBranches()->At(i)->GetName();
2051                  if( !name.CompareTo("TrkLevel1") )TRK_L1__ok=true;                  if( !name.CompareTo("TrkLevel1") )TRK1__ok=true;
2052              };                  if( !name.CompareTo("TrkLevel2") )TRK2__ok=true;
2053                    if( !name.CompareTo("TrkHough") )TRKh__ok=true;
2054                };      
2055                T->Delete();
2056          };          };
2057                    //=========================================================  
2058            if( !strcmp(key->GetName(),"Calorimeter"    ) ){
2059                TTree *T = (TTree*)f->Get("Calorimeter");
2060                if(CAL1||CAL2){
2061                    Int_t nevt = T->GetEntries();
2062                    if( nev && nevt!=nev){
2063                        cout << "File: "<< f->GetName() <<" discarded ---- Calorimeter tree has "<<nevt<<" events instead of "<<nev<< endl;
2064                        return false;
2065                    }
2066                    nev=nevt;
2067                }
2068                for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
2069                    TString name = T->GetListOfBranches()->At(i)->GetName();
2070                    if( !name.CompareTo("CaloLevel1") )CAL1__ok=true;
2071                    if( !name.CompareTo("CaloLevel2") )CAL2__ok=true;
2072                };    
2073                T->Delete();
2074            };      
2075    
2076      };      };
2077    
2078        if( SELLI==-1 )SELLI = (Int_t)SELLI__ok;
2079        if( SELLI==0 && SELLI__ok ){
2080            cout << "File: "<< f->GetName() <<" discarded ---- found SelectionList (it is not a full-event file)" << endl;
2081            return false;  
2082        }
2083        if( SELLI==1 && !SELLI__ok ){
2084            cout << "File: "<< f->GetName() <<" discarded ---- SelectionList missing" << endl;
2085            return false;
2086        }
2087            
2088      lk->Delete();  //    cout << "SELLI "<<SELLI<<endl;
2089      f->Close();  
2090        //     cout<< "CheckLevel2File(TString): detector list --> ";
2091      Bool_t FLAG = true;  //     if(TRK1__ok)cout<<"TRK1 ";
2092    //     if(TRK2__ok)cout<<"TRK2 ";
2093    //     if(TRKh__ok)cout<<"TRKH ";
2094    //     if(CAL1__ok)cout<<"CAL1 ";
2095    //     if(CAL2__ok)cout<<"CAL2 ";
2096    //     if(TOF__ok)cout<<"TOF ";
2097    //     if(TRG__ok)cout<<"TRG ";
2098    //     if(AC__ok)cout<<"AC ";
2099    //     if(ND__ok)cout<<"ND ";
2100    //     if(S4__ok)cout<<"S4 ";
2101    //     if(ORB__ok)cout<<"ORB ";
2102    //     cout << endl;
2103    
2104    
2105        if(TRK2 && TRK1__ok)TRK1=1;
2106    // ----------------------------------------------------------------------------
2107    // NOTA
2108    // se c'e` il level1, lo devo necessarimente leggere.
2109    // infatti (non ho capito perche`) i cluster vengono letti e allocati in memoria
2110    // comunque, ma non vengono disallocati da PamLevel2::Clear()
2111    // ----------------------------------------------------------------------------
2112    
2113    
2114      if(!RUN__ok) {      if(!RUN__ok) {
2115          cout << "File: "<< f->GetName() <<" discarded ---- Missing RunInfo tree"<< endl; return false;          cout << "File: "<< f->GetName() <<" *WARNING* ---- Missing RunInfo tree"<< endl;
2116  //      FLAG = false;  //      return false;
2117      };      };
2118      if(CAL && !CAL__ok){  
2119          cout << "File: "<< f->GetName() <<" discarded ---- Missing Calorimeter tree"<< endl; return false;      if(CAL1 && !CAL1__ok){
2120  //      FLAG = false;          cout << "File: "<< f->GetName() <<" discarded ---- Missing CaloLevel1 branch"<< endl;
2121      };          return false;
     if(TRK && !TRK__ok){  
         cout << "File: "<< f->GetName() <<" discarded ---- Missing Tracker tree"<< endl; return false;  
 //      FLAG = false;  
     };  
     if(TRK_L1 && !TRK_L1__ok){  
         cout << "File: "<< f->GetName() <<" discarded ---- Missing Tracker Level1 Branch"<< endl; return false;  
 //      FLAG = false;  
2122      };      };
2123      if(TRG && !TRG__ok){      if(CAL2 && !CAL2__ok){
2124          cout << "File: "<< f->GetName() <<" discarded ---- Missing Trigger tree"<< endl; return false;          cout << "File: "<< f->GetName() <<" discarded ---- Missing CaloLevel2 branch"<< endl;
2125  //      FLAG = false;          return false;
2126      };      };
2127      if(TOF && !TOF__ok){      if(TRK2 && !TRK2__ok){
2128          cout << "File: "<< f->GetName() <<" discarded ---- Missing ToF tree"<< endl; return false;          cout << "File: "<< f->GetName() <<" discarded ---- Missing TrkLevel2 branch"<< endl;
2129  //      FLAG = false;          return false;
2130      };      };
2131      if(S4 && !S4__ok){      if(TRK1 && !TRK1__ok){
2132          cout << "File: "<< f->GetName() <<" discarded ---- Missing S4 tree"<< endl; return false;          cout << "File: "<< f->GetName() <<" discarded ---- Missing TrkLevel1 branch"<< endl;
2133  //      FLAG = false;          return false;
2134      };      };
2135      if(ND && !ND__ok){      if(TRKh && !TRKh__ok){
2136          cout << "File: "<< f->GetName() <<" discarded ---- Missing ND tree"<< endl; return false;          cout << "File: "<< f->GetName() <<" discarded ---- Missing TrkHough branch"<< endl;
2137  //      FLAG = false;          return false;
2138        };
2139        if(ORB && !ORB__ok){
2140            cout << "File: "<< f->GetName() <<" discarded ---- Missing ORB tree"<< endl;
2141            return false;
2142      };      };
2143      if(AC && !AC__ok){      if(AC && !AC__ok){
2144          cout << "File: "<< f->GetName() <<" discarded ---- Missing AC tree"<< endl; return false;          cout << "File: "<< f->GetName() <<" discarded ---- Missing AC tree"<< endl;
2145  //      FLAG = false;          return false;
2146      };      };
2147      if(ORB && !ORB__ok){      if(S4 && !S4__ok){
2148          cout << "File: "<< f->GetName() <<" discarded ---- Missing ORB tree"<< endl; return false;          cout << "File: "<< f->GetName() <<" discarded ---- Missing S4 tree"<< endl;
2149  //      FLAG = false;          return false;
2150        };
2151        if(TOF && !TOF__ok){
2152            cout << "File: "<< f->GetName() <<" discarded ---- Missing ToF tree"<< endl;
2153            return false;
2154        };
2155    
2156        if(ND && !ND__ok){
2157            cout << "File: "<< f->GetName() <<" discarded ---- Missing ND tree"<< endl;
2158            return false;
2159        };
2160        if(TRG && !TRG__ok){
2161            cout << "File: "<< f->GetName() <<" discarded ---- Missing Trigger tree"<< endl;
2162            return false;
2163      };      };
2164            
2165      return FLAG;  
2166    //    lk->Delete();
2167    //    delete lk;
2168        f->Close();
2169    
2170    //     cout<< "CheckLevel2File(TString): detector list --> ";
2171    //     if(TRK1)cout<<"TRK1 ";
2172    //     if(TRK2)cout<<"TRK2 ";
2173    //     if(TRKh)cout<<"TRKH ";
2174    //     if(CAL1)cout<<"CAL1 ";
2175    //     if(CAL2)cout<<"CAL2 ";
2176    //     if(TOF)cout<<"TOF ";
2177    //     if(TRG)cout<<"TRG ";
2178    //     if(AC)cout<<"AC ";
2179    //     if(ND)cout<<"ND ";
2180    //     if(S4)cout<<"S4 ";
2181    //     if(ORB)cout<<"ORB ";
2182    //     cout << endl;
2183          
2184        return true;
2185                    
2186  };  };
2187    
2188    
2189    /**
2190     * Create clone-trees
2191     */
2192    void PamLevel2::CreateCloneTrees0( TChain *fChain, TFile *ofile ){
2193    
2194        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2195        cout << "Create clones of PAMELA trees "<<endl;
2196        
2197        Int_t i=0;
2198        pam_tree_clone[i] = fChain->GetTree()->CloneTree(0);
2199        TString name =  pam_tree_clone[i]->GetName();
2200        name.Append("_clone");
2201    //    pam_tree_clone[i]->SetName(name.Data());
2202        cout << pam_tree_clone[i]->GetName() <<endl;
2203        i++;
2204    
2205        TList *li = fChain->GetListOfFriends();
2206        TIter next(li);
2207        TFriendElement* T_friend=0;
2208        ofile->cd();
2209        while( (T_friend = (TFriendElement*)next()) ){
2210    //      cout<<T_friend->IsA()->GetName()<<" "<<T_friend->GetName()<<hex << T_friend->GetTree() << dec<<endl;
2211    //      cout<<T_friend->GetTree()->GetName()<< endl;
2212            pam_tree_clone[i] = T_friend->GetTree()->CloneTree(0);
2213            pam_tree_clone[i]->SetAutoSave(1000000);
2214            name =  pam_tree_clone[i]->GetName();
2215            name.Append("_clone");
2216    //      pam_tree_clone[i]->SetName(name.Data());
2217            cout << pam_tree_clone[i]->GetName() << endl;
2218            i++;
2219        }
2220        
2221        delete li;
2222    
2223        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2224    
2225    }
2226    
2227    /**
2228     * Create clone-trees
2229     */
2230    void PamLevel2::CreateCloneTrees(TFile *ofile){
2231    
2232        ofile->cd();
2233    
2234        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2235        cout << "Create new PAMELA trees "<<endl;
2236            
2237    
2238        run_tree_clone = new TTree("Run","PAMELA Level2 data from the GL_RUN table ");
2239        run_tree_clone->Branch("RunInfo","GL_RUN",GetPointerTo("RunInfo"));
2240        cout << "Run          : branch RunInfo"<<endl;
2241        run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));
2242        cout << "Run          : branch SoftInfo"<<endl;
2243                
2244    
2245    
2246        sel_tree_clone = new TTree("SelectionList","List of selected events ");
2247        sel_tree_clone->Branch("RunEntry",&irun,"runentry/I");
2248        sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/I");
2249        
2250    
2251        Int_t i=0;
2252        if(TRK1||TRK2||TRKh){
2253            pam_tree_clone[i] = new TTree("Tracker","PAMELA tracker level2 data ");
2254            if(TRK1) {
2255                pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));
2256                pam_tree_clone[i]->BranchRef();
2257                cout << "Tracker      : branch TrkLevel1"<<endl;
2258                cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;
2259            };
2260            if(TRK2) {
2261                pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));
2262                cout << "Tracker      : branch TrkLevel2"<<endl;        
2263            };
2264            if(TRKh) {
2265                pam_tree_clone[i]->Branch("TrkHough","TrkHough", GetPointerTo("TrkHough"));
2266                cout << "Tracker      : branch TrkHough"<<endl;
2267            };
2268            i++;
2269        }
2270    
2271        // Calorimeter
2272        if(CAL1||CAL2){
2273            pam_tree_clone[i] = new TTree("Calorimeter","PAMELA calorimeter level2 data ");
2274            if(CAL1) {
2275                pam_tree_clone[i]->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));
2276                cout << "Calorimeter  : branch CaloLevel1"<<endl;
2277            };
2278            if(CAL2) {
2279                pam_tree_clone[i]->Branch("CaloLevel2","CaloLevel2", GetPointerTo("CaloLevel2"));
2280                cout << "Calorimeter  : branch CaloLevel2"<<endl;
2281            };
2282            i++;
2283        }    
2284    
2285        // ToF    
2286        if(TOF) {
2287            pam_tree_clone[i] = new TTree("ToF","PAMELA ToF level2 data ");
2288            pam_tree_clone[i]->Branch("ToFLevel2","ToFLevel2", GetPointerTo("ToFLevel2"));
2289            cout << "ToF          : branch ToFLevel2"<<endl;
2290            i++;
2291        };
2292        // Trigger
2293        if(TRG) {
2294            pam_tree_clone[i] = new TTree("Trigger","PAMELA trigger level2 data ");
2295            pam_tree_clone[i]->Branch("TrigLevel2","TrigLevel2", GetPointerTo("TrigLevel2"));
2296            cout << "Trigger      : branch TrigLevel2"<<endl;
2297            i++;
2298        };
2299        // S4
2300        if(S4) {
2301            pam_tree_clone[i] = new TTree("S4","PAMELA S4 level2 data ");  
2302            pam_tree_clone[i]->Branch("S4Level2","S4Level2", GetPointerTo("S4Level2"));
2303            cout << "S4           : branch S4Level2"<<endl;
2304            i++;
2305        };
2306        // Neutron Detector
2307        if(ND) {
2308            pam_tree_clone[i] = new TTree("NeutronD","PAMELA neutron detector level2 data ");      
2309            pam_tree_clone[i]->Branch("NDLevel2","NDLevel2", GetPointerTo("NDLevel2"));
2310            cout << "NeutronD     : branch NDLevel2"<<endl;
2311            i++;
2312        };
2313        // Anticounters
2314        if(AC) {
2315            pam_tree_clone[i] = new TTree("Anticounter","PAMELA anticounter detector level2 data ");        
2316            pam_tree_clone[i]->Branch("AcLevel2","AcLevel2", GetPointerTo("AcLevel2"));
2317            cout << "Anticounter  : branch AcLevel2"<<endl;
2318            i++;
2319        };
2320        // OrbitalInfo
2321        if(ORB) {
2322            pam_tree_clone[i] = new TTree("OrbitalInfo","PAMELA oribital info  ");  
2323            pam_tree_clone[i]->Branch("OrbitalInfo","OrbitalInfo", GetPointerTo("OrbitalInfo"));
2324            cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;
2325            i++;
2326        };
2327        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2328    
2329    }
2330    
2331    
2332    /**
2333     * Fill tree (created with CreateCloneTrees)
2334     *
2335     */
2336    //void PamLevel2::FillNewPamTree(TTree *T){
2337    void PamLevel2::FillCloneTrees(){
2338        
2339    //    cout << "PamLevel2::FillCloneTrees()" << irunentry << endl;
2340    
2341        for(Int_t i=0; i<NCLONES; i++){
2342            if(pam_tree_clone[i])pam_tree_clone[i]->Fill();
2343        }
2344        if(sel_tree_clone)sel_tree_clone->Fill();    
2345    
2346    }
2347    
2348    
2349    TTree* PamLevel2::GetCloneTree(TString name){
2350    
2351        for(Int_t i=0; i<NCLONES; i++){    
2352            if(pam_tree_clone[i]){
2353                TString na = pam_tree_clone[i]->GetName();
2354                if(!name.CompareTo(na))return pam_tree_clone[i];
2355            };
2356        }
2357        if(run_tree_clone){
2358            TString na = run_tree_clone->GetName();
2359            if(!name.CompareTo(na))return run_tree_clone;
2360        }
2361        if(sel_tree_clone){
2362            TString na = sel_tree_clone->GetName();
2363            if(!name.CompareTo(na))return sel_tree_clone;
2364        }
2365        return NULL;
2366    
2367    }
2368    void PamLevel2::WriteCloneTrees(){
2369        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2370        cout << "Write clones of PAMELA trees "<<endl;
2371        cout << run_tree_clone->GetName()<<endl;    
2372        run_tree_clone->Write();
2373        cout << sel_tree_clone->GetName()<<endl;    
2374        sel_tree_clone->Write();
2375        for(Int_t i=0; i<NCLONES; i++){    
2376            if(pam_tree_clone[i]){
2377                cout << pam_tree_clone[i]->GetName()<<endl;
2378                pam_tree_clone[i]->Write();
2379            };
2380        }
2381        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2382    
2383    }
2384    
2385    /**
2386     * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.
2387     */
2388    Int_t PamLevel2::GetEntry(Int_t iee){
2389        
2390        if(!pam_tree){
2391            cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded"<<endl;
2392            return 0;
2393        }
2394    
2395    
2396        //
2397        // 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...
2398        //
2399        //    if(!run_tree ){
2400        //  cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loeaded"<<endl;
2401        //  return 0;
2402        //    }
2403    
2404        Int_t ii=0;
2405        //-------------------------------
2406        ii = iee;
2407        if( !pam_tree->GetEntry(ii) ) return 0;
2408    
2409        //
2410        // ... 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.
2411        // 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
2412        // a problem if you don't check the return code of getentry.
2413        //
2414        if(!run_tree ){
2415            if ( TRK0 || CAL0 || TOF0 || RUN ) { //forse cosi` va bene per tornare 1?
2416                    cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded"<<endl;
2417                    return 0;
2418            }  else {
2419                    return 1; //cosi` se non c'e` run esce qua...
2420            }
2421        }
2422    
2423        //-------------------------------
2424        ii = iee;
2425        Bool_t UPDATED = UpdateRunInfo(run_tree,ii);
2426        if(SELLI==0)irunentry = iee-runfirstentry;
2427        if(UPDATED && run_tree_clone)run_tree_clone->Fill();
2428    
2429    //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;
2430    
2431        if( TRK0 || CAL0 || TOF0 )GetYodaEntry( );
2432    
2433        return 1;
2434    
2435    }
2436    
2437    /**
2438     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
2439     * Given the run ID (...), if needed it query the DB and load the proper file.
2440     * @return Pointer to the tree
2441     */
2442    
2443    
2444    TTree* PamLevel2::GetYodaTree( ){
2445        
2446    //    cout << "TTree* PamLevel2::GetYodaTree( )"<<endl;
2447        //===================================
2448        // check if iroot has changed
2449        //===================================
2450        if( irun<0 ){
2451            cout << "PamLevel2::GetYodaTree() -- ERROR "<<endl;
2452            cout << "In order to use this method you have to load the RunInfo tree "<<endl;
2453            cout << "with the method TChain* PamLevel2::GetRunTree(TString, TString)"<<endl;
2454    //      cout << " - read the event with PamLevel2::GetEntry(Int_t)"<<endl;
2455            return NULL;
2456        }    
2457        Int_t irootnew = run_obj->ID_ROOT_L0;
2458    //     cout << "iroot    "<<iroot<<endl;
2459    //     cout << "irootnew "<<irootnew<<endl;
2460    
2461        //===================================
2462        // load the level0 file
2463        // (if not already loaded)
2464        //===================================
2465        if( iroot != irootnew || !l0_tree){
2466            iroot = irootnew;
2467            //===================================
2468            // open the DB connection
2469            // (if not already opened)
2470            //===================================
2471            if(!dbc || (dbc && !dbc->IsConnected())){
2472                cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
2473                cout<<"Connecting to DB"<<endl;
2474                cout<<"HOST "<<host<<endl;
2475                cout<<"USER "<<user<<endl;
2476                cout<<"PSW  "<<psw<<endl;
2477                dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
2478                if( !dbc )return NULL;
2479                if( !dbc->IsConnected() )return NULL;    
2480    //          cout<<"...done"<<endl;
2481                cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
2482            }else{
2483    //          cout<<"DB already connected"<<endl;
2484            }
2485            GL_ROOT glroot = GL_ROOT();
2486            if( glroot.Query_GL_ROOT(iroot,dbc) ){
2487                cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = "<<iroot<< " does not exists"<<endl;
2488                return NULL;
2489            };
2490            TString filename = glroot.PATH + glroot.NAME;
2491            if(l0_file){
2492                l0_file->Close();
2493                l0_file->Delete();
2494            }
2495            cout << "Opening LEVEL0 file: "<< filename << endl;
2496            FileStat_t t;
2497            if( gSystem->GetPathInfo(filename.Data(),t) ){
2498                cout << " PamLevel2::GetYodaTree() -- ERROR opening file "<<endl;
2499                return NULL;
2500            }
2501            l0_file = new TFile(filename);
2502            if( !l0_file )return NULL;
2503            l0_tree = (TTree*)l0_file->Get("Physics");
2504            if(!h0_obj)h0_obj = new EventHeader();
2505            l0_tree->SetBranchAddress("Header" ,&h0_obj);
2506            prevshift = 0;
2507            //---------------------------------------------------
2508            // TRACKER:
2509            if(TRK0){
2510                if(!trk0_obj){
2511                    trk0_obj = new TrkLevel0();
2512                    trk0_obj->Set();
2513                };
2514                l0_tree->SetBranchAddress("Tracker" ,trk0_obj->GetPointerToTrackerEvent());
2515            }
2516            //---------------------------------------------------
2517            // CALORIMETER:
2518            if(CAL0){
2519                if(!calo0_obj){
2520                    calo0_obj = new CaloLevel0();
2521                    calo0_obj->Set();
2522                };
2523                l0_tree->SetBranchAddress("Calorimeter" ,calo0_obj->GetPointerToCalorimeterEvent());
2524                //      cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;
2525            }
2526            //---------------------------------------------------
2527            // TOF:
2528            if(TOF0){
2529                cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented "<<endl;
2530            }
2531    
2532        };
2533    
2534        if(!dbc || (dbc && !dbc->IsConnected())){
2535            cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;
2536        }
2537    
2538        if ( TRK0 ) TrkParams::SetCalib(run_obj,dbc);
2539    
2540    
2541    //    cout << l0_tree << endl;
2542        
2543        return l0_tree;
2544    
2545    }
2546    
2547    /**
2548     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
2549     */
2550    Int_t PamLevel2::GetYodaEntry(){
2551    
2552    //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;
2553        if(!GetYodaTree())return 0;
2554        
2555        // patch
2556        if( (Int_t)irunentry < 0){
2557            cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
2558            irunentry=0;
2559        }
2560        //  ---------------------------------
2561        //  if file is NOT a preselected file
2562        //  ---------------------------------
2563        UInt_t quellagiusta = irunentry + run_obj->EV_FROM;
2564        
2565    
2566    //     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
2567    //     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
2568    //     cout << " time "<< GetOrbitalInfo()->absTime << endl;
2569    //     cout << " trk_calib_used "<<run_obj->TRK_CALIB_USED<< endl;
2570        
2571        if( !GetOrbitalInfo() ){
2572            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo "<<endl;
2573            return 0;
2574        }
2575    
2576        // ---------------------------------------------------------------------
2577        // ATTENTION!!!
2578        // If data are not pre-processed with cleaner, the level0 tree may contain
2579        // spurious nested physics packets.
2580        // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
2581        // while level2 tree DOES NOT!!
2582        // This means that "quellagiusta" in these cases is not correct.
2583        // In order to retrieve the correct level0 event, I implemented a check
2584        // of the OBT and pkt-number. In case of mismatch, the level0 entry number
2585        // is shift forward until when the packets match.
2586        // ---------------------------------------------------------------------
2587        Int_t answer = 0;
2588        Int_t shift =0;
2589        //    printf(" siamo qui %i %i \n",shift,prevshift);
2590        do{
2591            if(shift>0){    
2592              cout << " level0 <--> level2 mismatch ( irun "<<irun<<" irunentry "<<irunentry<<" shift "<<shift<<" )"<<endl;
2593            }
2594            answer = l0_tree->GetEntry(quellagiusta+shift+prevshift);
2595            shift++;
2596            if( !GetEventHeader() ){
2597                cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader "<<endl;
2598                return 0;
2599            }
2600    //      if( (quellagiusta+shift) == l0_tree->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;
2601    //      cout << " GetOrbitalInfo()->OBT "<< GetOrbitalInfo()->OBT << endl;
2602    //      cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;
2603    //      cout << " GetOrbitalInfo()->pkt_num "<< GetOrbitalInfo()->pkt_num << endl;
2604    //      cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
2605    //      printf(" IDRUN %u \n",GetRunInfo()->ID);
2606    //
2607            if ( prevshift != 0 && (quellagiusta+shift) == GetYodaTree()->GetEntries() ){
2608              prevshift = 0;
2609              shift = -1;
2610            };
2611    
2612        }while( ( GetOrbitalInfo()->OBT != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || GetOrbitalInfo()->pkt_num != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta+shift) < GetYodaTree()->GetEntries() );
2613    
2614        if ( (quellagiusta+shift) == GetYodaTree()->GetEntries() ) cout << " Big trouble here, no such event in Level0 data! " <<endl;
2615    
2616    //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
2617    //    return GetYodaTree()->GetEntry(quellagiusta);
2618        if ( shift > 1 ) prevshift += (shift-1);
2619        
2620        return answer;
2621        
2622    }
2623    
2624    

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.31

  ViewVC Help
Powered by ViewVC 1.1.23