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

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

  ViewVC Help
Powered by ViewVC 1.1.23