/[PAMELA software]/PamelaLevel2/src/PamLevel2.cpp
ViewVC logotype

Diff of /PamelaLevel2/src/PamLevel2.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by pam-fi, Tue Oct 24 10:24:27 2006 UTC revision 1.81 by pam-fi, Wed Apr 1 09:25:04 2009 UTC
# Line 1  Line 1 
1  #include <PamLevel2.h>  #include <PamLevel2.h>
2    
3    /////////////////////////////////////////////////////////////////////////////
4    /////////////////////////////////////////////////////////////////////////////
5    /////////////////////////////////////////////////////////////////////////////
6    /////////////////////////////////////////////////////////////////////////////
7    
8    void GPamela::Clear() {
9      Irun = 0;
10      Ievnt = 0;
11      Ipa = 0;
12      X0 = 0.;
13      Y0 = 0.;
14      Z0 = 0.;
15      Theta = 0.;
16      Phi = 0.;
17      P0 = 0.;
18      Nthtof = 0;
19      Nthcat = 0;
20      Nthcas = 0;
21      Nthspe = 0;
22      Nstrpx = 0;
23      Nstrpy = 0;
24      Nthcali = 0;
25      Nthcal = 0;
26      Nthnd = 0;
27      Nthcard = 0;
28    }
29    
30    void GPamela::Delete() {
31    
32      Clear();
33    
34      delete[] Ipltof;
35      delete[] Ipaddle;
36      delete[] Ipartof;
37      delete[] Xintof;
38      delete[] Yintof;
39      delete[] Zintof;
40      delete[] Xouttof;
41      delete[] Youttof;
42      delete[] Zouttof;
43      delete[] Ereltof;
44      delete[] Timetof;
45      delete[] Pathtof;
46      delete[] P0tof;
47      delete[] Iparcat;
48      delete[] Icat;
49      delete[] Xincat;
50      delete[] Yincat;
51      delete[] Zincat;
52      delete[] Xoutcat;
53      delete[] Youtcat;
54      delete[] Zoutcat;
55      delete[] Erelcat;
56      delete[] Timecat;
57      delete[] Pathcat;
58      delete[] P0cat;
59      delete[] Iparcas;
60      delete[] Icas;
61      delete[] Xincas;
62      delete[] Yincas;
63      delete[] Zincas;
64      delete[] Xoutcas;
65      delete[] Youtcas;
66      delete[] Zoutcas;
67      delete[] Erelcas;
68      delete[] Timecas;
69      delete[] Pathcas;
70      delete[] P0cas;
71      delete[] Iparspe;
72      delete[] Itrpb;
73      delete[] Itrsl;
74      delete[] Itspa;
75      delete[] Xinspe;
76      delete[] Yinspe;
77      delete[] Zinspe;
78      delete[] Xoutspe;
79      delete[] Youtspe;
80      delete[] Zoutspe;
81      delete[] Xavspe;
82      delete[] Yavspe;
83      delete[] Zavspe;
84      delete[] Erelspe;
85      delete[] Pathspe;
86      delete[] P0spe;
87      delete[] Nxmult;
88      delete[] Nymult;
89      delete[] Npstripx;
90      delete[] Ntstripx;
91      delete[] Istripx;
92      delete[] Qstripx;
93      delete[] Xstripx;
94      delete[] Npstripy;
95      delete[] Ntstripy;
96      delete[] Istripy;
97      delete[] Qstripy;
98      delete[] Ystripy;
99      delete[] Icaplane;
100      delete[] Icastrip;
101      delete[] Icamod;
102      delete[] Enestrip;
103      delete[] Icapl;
104      delete[] Icasi;
105      delete[] Icast;
106      delete[] Xincal;
107      delete[] Yincal;
108      delete[] Zincal;
109      delete[] Erelcal;
110      delete[] Itubend;
111      delete[] Iparnd;
112      delete[] Xinnd;
113      delete[] Yinnd;
114      delete[] Zinnd;
115      delete[] Xoutnd;
116      delete[] Youtnd;
117      delete[] Zoutnd;
118      delete[] Erelnd;
119      delete[] Timend;
120      delete[] Pathnd;
121      delete[] P0nd;
122      delete[] Iparcard;
123      delete[] Icard;
124      delete[] Xincard;
125      delete[] Yincard;
126      delete[] Zincard;
127      delete[] Xoutcard;
128      delete[] Youtcard;
129      delete[] Zoutcard;
130      delete[] Erelcard;
131      delete[] Timecard;
132      delete[] Pathcard;
133      delete[] P0card;
134    
135    }
136    ;
137    
138    void GPamela::SetBranchAddress(TChain* fhBookTree) {
139    
140      //    cout << "fhBookTree "<<fhBookTree << endl;
141      // prepare tree
142      fhBookTree->SetBranchAddress("Irun", &Irun);
143      fhBookTree->SetBranchAddress("Ievnt", &Ievnt);
144      fhBookTree->SetBranchAddress("Ipa", &Ipa);
145      fhBookTree->SetBranchAddress("X0", &X0);
146      fhBookTree->SetBranchAddress("Y0", &Y0);
147      fhBookTree->SetBranchAddress("Z0", &Z0);
148      fhBookTree->SetBranchAddress("Theta", &Theta);
149      fhBookTree->SetBranchAddress("Phi", &Phi);
150      fhBookTree->SetBranchAddress("P0", &P0);
151      fhBookTree->SetBranchAddress("Nthtof", &Nthtof);
152      fhBookTree->SetBranchAddress("Ipltof", Ipltof);
153      fhBookTree->SetBranchAddress("Ipaddle", Ipaddle);
154      fhBookTree->SetBranchAddress("Ipartof", Ipartof);
155      fhBookTree->SetBranchAddress("Xintof", Xintof);
156      fhBookTree->SetBranchAddress("Yintof", Yintof);
157      fhBookTree->SetBranchAddress("Zintof", Zintof);
158      fhBookTree->SetBranchAddress("Xouttof", Xouttof);
159      fhBookTree->SetBranchAddress("Youttof", Youttof);
160      fhBookTree->SetBranchAddress("Zouttof", Zouttof);
161      fhBookTree->SetBranchAddress("Ereltof", Ereltof);
162      fhBookTree->SetBranchAddress("Timetof", Timetof);
163      fhBookTree->SetBranchAddress("Pathtof", Pathtof);
164      fhBookTree->SetBranchAddress("P0tof", P0tof);
165      fhBookTree->SetBranchAddress("Nthcat", &Nthcat);
166      fhBookTree->SetBranchAddress("Iparcat", Iparcat);
167      fhBookTree->SetBranchAddress("Icat", Icat);
168      fhBookTree->SetBranchAddress("Xincat", Xincat);
169      fhBookTree->SetBranchAddress("Yincat", Yincat);
170      fhBookTree->SetBranchAddress("Zincat", Zincat);
171      fhBookTree->SetBranchAddress("Xoutcat", Xoutcat);
172      fhBookTree->SetBranchAddress("Youtcat", Youtcat);
173      fhBookTree->SetBranchAddress("Zoutcat", Zoutcat);
174      fhBookTree->SetBranchAddress("Erelcat", Erelcat);
175      fhBookTree->SetBranchAddress("Timecat", Timecat);
176      fhBookTree->SetBranchAddress("Pathcat", Pathcat);
177      fhBookTree->SetBranchAddress("P0cat", P0cat);
178      fhBookTree->SetBranchAddress("Nthcas", &Nthcas);
179      fhBookTree->SetBranchAddress("Iparcas", Iparcas);
180      fhBookTree->SetBranchAddress("Icas", Icas);
181      fhBookTree->SetBranchAddress("Xincas", Xincas);
182      fhBookTree->SetBranchAddress("Yincas", Yincas);
183      fhBookTree->SetBranchAddress("Zincas", Zincas);
184      fhBookTree->SetBranchAddress("Xoutcas", Xoutcas);
185      fhBookTree->SetBranchAddress("Youtcas", Youtcas);
186      fhBookTree->SetBranchAddress("Zoutcas", Zoutcas);
187      fhBookTree->SetBranchAddress("Erelcas", Erelcas);
188      fhBookTree->SetBranchAddress("Timecas", Timecas);
189      fhBookTree->SetBranchAddress("Pathcas", Pathcas);
190      fhBookTree->SetBranchAddress("P0cas", P0cas);
191      fhBookTree->SetBranchAddress("Nthspe", &Nthspe);
192      fhBookTree->SetBranchAddress("Iparspe", Iparspe);
193      fhBookTree->SetBranchAddress("Itrpb", Itrpb);
194      fhBookTree->SetBranchAddress("Itrsl", Itrsl);
195      fhBookTree->SetBranchAddress("Itspa", Itspa);
196      fhBookTree->SetBranchAddress("Xinspe", Xinspe);
197      fhBookTree->SetBranchAddress("Yinspe", Yinspe);
198      fhBookTree->SetBranchAddress("Zinspe", Zinspe);
199      fhBookTree->SetBranchAddress("Xoutspe", Xoutspe);
200      fhBookTree->SetBranchAddress("Youtspe", Youtspe);
201      fhBookTree->SetBranchAddress("Zoutspe", Zoutspe);
202      fhBookTree->SetBranchAddress("Xavspe", Xavspe);
203      fhBookTree->SetBranchAddress("Yavspe", Yavspe);
204      fhBookTree->SetBranchAddress("Zavspe", Zavspe);
205      fhBookTree->SetBranchAddress("Erelspe", Erelspe);
206      fhBookTree->SetBranchAddress("Pathspe", Pathspe);
207      fhBookTree->SetBranchAddress("P0spe", P0spe);
208      fhBookTree->SetBranchAddress("Nxmult", Nxmult);
209      fhBookTree->SetBranchAddress("Nymult", Nymult);
210      fhBookTree->SetBranchAddress("Nstrpx", &Nstrpx);
211      fhBookTree->SetBranchAddress("Npstripx", Npstripx);
212      fhBookTree->SetBranchAddress("Ntstripx", Ntstripx);
213      fhBookTree->SetBranchAddress("Istripx", Istripx);
214      fhBookTree->SetBranchAddress("Qstripx", Qstripx);
215      fhBookTree->SetBranchAddress("Xstripx", Xstripx);
216      fhBookTree->SetBranchAddress("Nstrpy", &Nstrpy);
217      fhBookTree->SetBranchAddress("Npstripy", Npstripy);
218      fhBookTree->SetBranchAddress("Ntstripy", Ntstripy);
219      fhBookTree->SetBranchAddress("Istripy", Istripy);
220      fhBookTree->SetBranchAddress("Qstripy", Qstripy);
221      fhBookTree->SetBranchAddress("Ystripy", Ystripy);
222      fhBookTree->SetBranchAddress("Nthcali", &Nthcali);
223      fhBookTree->SetBranchAddress("Icaplane", Icaplane);
224      fhBookTree->SetBranchAddress("Icastrip", Icastrip);
225      fhBookTree->SetBranchAddress("Icamod", Icamod);
226      fhBookTree->SetBranchAddress("Enestrip", Enestrip);
227      fhBookTree->SetBranchAddress("Nthcal", &Nthcal);
228      fhBookTree->SetBranchAddress("Icapl", Icapl);
229      fhBookTree->SetBranchAddress("Icasi", Icasi);
230      fhBookTree->SetBranchAddress("Icast", Icast);
231      fhBookTree->SetBranchAddress("Xincal", Xincal);
232      fhBookTree->SetBranchAddress("Yincal", Yincal);
233      fhBookTree->SetBranchAddress("Zincal", Zincal);
234      fhBookTree->SetBranchAddress("Erelcal", Erelcal);
235      fhBookTree->SetBranchAddress("Nthnd", &Nthnd);
236      fhBookTree->SetBranchAddress("Itubend", Itubend);
237      fhBookTree->SetBranchAddress("Iparnd", Iparnd);
238      fhBookTree->SetBranchAddress("Xinnd", Xinnd);
239      fhBookTree->SetBranchAddress("Yinnd", Yinnd);
240      fhBookTree->SetBranchAddress("Zinnd", Zinnd);
241      fhBookTree->SetBranchAddress("Xoutnd", Xoutnd);
242      fhBookTree->SetBranchAddress("Youtnd", Youtnd);
243      fhBookTree->SetBranchAddress("Zoutnd", Zoutnd);
244      fhBookTree->SetBranchAddress("Erelnd", Erelnd);
245      fhBookTree->SetBranchAddress("Timend", Timend);
246      fhBookTree->SetBranchAddress("Pathnd", Pathnd);
247      fhBookTree->SetBranchAddress("P0nd", P0nd);
248      fhBookTree->SetBranchAddress("Nthcard", &Nthcard);
249      fhBookTree->SetBranchAddress("Iparcard", Iparcard);
250      fhBookTree->SetBranchAddress("Icard", Icard);
251      fhBookTree->SetBranchAddress("Xincard", Xincard);
252      fhBookTree->SetBranchAddress("Yincard", Yincard);
253      fhBookTree->SetBranchAddress("Zincard", Zincard);
254      fhBookTree->SetBranchAddress("Xoutcard", Xoutcard);
255      fhBookTree->SetBranchAddress("Youtcard", Youtcard);
256      fhBookTree->SetBranchAddress("Zoutcard", Zoutcard);
257      fhBookTree->SetBranchAddress("Erelcard", Erelcard);
258      fhBookTree->SetBranchAddress("Timecard", Timecard);
259      fhBookTree->SetBranchAddress("Pathcard", Pathcard);
260      fhBookTree->SetBranchAddress("P0card", P0card);
261    
262      //    fhBookTree->SetBranchStatus("*",0);
263    
264    }
265    
266    ClassImp( GPamela);
267    
268    /////////////////////////////////////////////////////////////////////////////
269    /////////////////////////////////////////////////////////////////////////////
270    /////////////////////////////////////////////////////////////////////////////
271    /////////////////////////////////////////////////////////////////////////////
272  //--------------------------------------  //--------------------------------------
273  //  //
274  //  //
275  //--------------------------------------  //--------------------------------------
276  /**  /**
277   * Default constructor   * Default constructor
278   */   */
279  PamTrack::PamTrack(){  PamTrack::PamTrack() {
280      trk_track  = this->TrkTrack::GetTrkTrack();    trk_track = 0;
281      calo_track = this->CaloTrkVar::GetCaloTrkVar();    calo_track = 0;
282      tof_track  = this->ToFTrkVar::GetToFTrkVar();    tof_track = 0;
283  };    orb_track = 0;
284      candeleteobj = 0;
285      pscore = 0;
286      iscore = 0;
287    }
288    ;
289  //--------------------------------------  //--------------------------------------
290  //  //
291  //  //
292  //--------------------------------------  //--------------------------------------
293  /**  /**
294   * Constructor   * Constructor
295   */   */
296  PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o){  PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o, OrbitalInfoTrkVar *r) {
     trk_track  = this->TrkTrack::GetTrkTrack();  
     calo_track = this->CaloTrkVar::GetCaloTrkVar();  
     tof_track  = this->ToFTrkVar::GetToFTrkVar();  
     if(t) *trk_track  = *t;  
     if(c) *calo_track = *c;  
     if(o) *tof_track  = *o;      
 };  
297    
298  //--------------------------------------    trk_track = 0;
299  //    calo_track = 0;
300  //    tof_track = 0;
301  //--------------------------------------    orb_track = 0;
302  /**    //     if(t)trk_track  = new TrkTrack(*t);
303   * Constructor    //     if(c)calo_track = new CaloTrkVar(*c);
304   */    //     if(o)tof_track  = new ToFTrkVar(*o);
305  PamLevel2::PamLevel2(){    if (t)
306                trk_track = t;
307      trk_l1_obj  = this->TrkLevel1::GetTrkLevel1();          if (c)
308      trk_obj     = this->TrkLevel2::GetTrkLevel2();      calo_track = c;
309      calo_obj    = this->CaloLevel2::GetCaloLevel2();    if (o)
310      tof_obj     = this->ToFLevel2::GetToFLevel2();      tof_track = o;
311      trig_obj    = this->TrigLevel2::GetTrigLevel2();    if (r)
312      s4_obj      = this->S4Level2::GetS4Level2();      orb_track = r;
     nd_obj      = this->NDLevel2::GetNDLevel2();  
     ac_obj      = this->AcLevel2::GetAcLevel2();  
     orb_obj     = this->OrbitalInfo::GetOrbitalInfo();  
313    
314      run_obj     = new GL_RUN();    candeleteobj = 0;
315        
316      sorted_tracks = new TRefArray();  }
317        ;
318      CAL = true;  PamTrack::PamTrack(const PamTrack& track) {
319      TRK = true;  
320      TRG = true;    TrkTrack *t = track.trk_track;
321      TOF = true;    CaloTrkVar *c = track.calo_track;
322      S4  = true;    ToFTrkVar *o = track.tof_track;
323      ND  = true;    OrbitalInfoTrkVar *r = track.orb_track;
324      AC  = true;  
325      ORB = true;    trk_track = 0;
326          calo_track = 0;
327      TRK_L1 = false;    tof_track = 0;
328          orb_track = 0;
329      Tout  = NULL;    if (t)
330  };      trk_track = new TrkTrack(*t);
331  /**    if (c)
332   * Destructor      calo_track = new CaloTrkVar(*c);
333   */    if (o)
334  PamLevel2::~PamLevel2(){      tof_track = new ToFTrkVar(*o);
335                      if (r)
336      TrkLevel1::Clear();      orb_track = new OrbitalInfoTrkVar(*r);
337          candeleteobj = 1;
     TrkLevel2::Clear();  
     CaloLevel2::Clear();  
     ToFLevel2::Clear();  
     TrigLevel2::Clear();  
     S4Level2::Clear();  
     NDLevel2::Clear();  
     AcLevel2::Clear();  
     OrbitalInfo::Clear();  
       
     delete run_obj;  
       
 //      delete sorted_tracks;  
     sorted_tracks->Delete(); // clean the reference array  
           
     if(Tout)Tout->Delete(); // delete loaded tree from memory  
           
 };  
 /**  
  * Clear the event  
  */  
 void PamLevel2::Clear(){  
           
     TrkLevel1::Clear();  
           
     TrkLevel2::Clear();  
     CaloLevel2::Clear();  
     ToFLevel2::Clear();  
     TrigLevel2::Clear();  
     S4Level2::Clear();  
     NDLevel2::Clear();  
     AcLevel2::Clear();  
     OrbitalInfo::Clear();  
       
     sorted_tracks->Delete(); // clean the reference array  
       
 };  
338    
339    }
340    void PamTrack::Clear() {
341    
342      //    cout << "PamTrack::Clear() "<<candeleteobj<<endl;
343      if (candeleteobj) {
344        if (trk_track)
345          trk_track->TrkTrack::Clear();
346        if (calo_track)
347          calo_track->CaloTrkVar::Clear();//???
348        if (tof_track)
349          tof_track->ToFTrkVar::Clear();//???
350        if (orb_track)
351          orb_track->OrbitalInfoTrkVar::Clear();//???
352      }
353      else {
354        trk_track = 0;
355        calo_track = 0;
356        tof_track = 0;
357        orb_track = 0;
358      }
359      pscore = 0;
360      iscore = 0;
361    
362    }
363    void PamTrack::Delete() {
364      //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;
365      if (candeleteobj) {
366        if (trk_track) {
367          trk_track->TrkTrack::Clear();
368          delete trk_track;
369        }
370        if (calo_track) {
371          calo_track->CaloTrkVar::Clear();//???
372          delete calo_track;
373        }
374        if (tof_track) {
375          tof_track->ToFTrkVar::Clear();//???
376          delete tof_track;
377        }
378        if (orb_track) {
379          orb_track->OrbitalInfoTrkVar::Clear();//???
380          delete orb_track;
381        }
382      }
383      else {
384        Clear();
385      }
386    }
387  //--------------------------------------  //--------------------------------------
388  //  //
389  //  //
390  //--------------------------------------  //--------------------------------------
391  /**  /**
392   * Retrieves the calorimeter track matching the seqno-th tracker stored track.   * Default Constructor
  * (If seqno = -1 retrieves the self-trigger calorimeter track)  
393   */   */
394   CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno){  PamLevel2::PamLevel2() {
395              Initialize();
396       if( CaloLevel2::ntrk()==0 ){  }
397           cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no Calorimeter tracks are stored"<<endl;  ;
398           return NULL;  
      };  
       
      CaloTrkVar *c = 0;  
      Int_t it_calo=0;  
       
      do{  
          c = CaloLevel2::GetCaloTrkVar(it_calo);  
          it_calo++;  
      } while( c && seqno != c->trkseqno && it_calo < CaloLevel2::ntrk());        
       
      if(!c || seqno != c->trkseqno){  
          c = 0;  
          if(seqno!=-1)cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match Calorimeter stored tracks"<<endl;  
      };  
      return c;  
       
  };  
 //--------------------------------------  
  //  
  //  
 //--------------------------------------  
 /**  
   * Retrieves the ToF track matching the seqno-th tracker stored track.  
   * (If seqno = -1 retrieves the tracker-independent tof track)  
  */  
  ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno){  
           
      if( ToFLevel2::ntrk()==0 ){  
          cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no ToF tracks are stored"<<endl;  
          return NULL;  
      };  
       
      ToFTrkVar *c = 0;  
      Int_t it_tof=0;  
       
      do{  
          c = ToFLevel2::GetToFTrkVar(it_tof);  
          it_tof++;  
      } while( c && seqno != c->trkseqno && it_tof < ToFLevel2::ntrk());  
       
      if(!c || seqno != c->trkseqno){  
          c = 0;  
          if(seqno!=-1)cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match ToF stored tracks"<<endl;  
      };  
      return c;  
       
  };  
   
 //--------------------------------------  
  //  
  //  
 //--------------------------------------  
 /**  
  * Give the pamela track associated to a tracker track, retrieving related calorimeter and tof track information.  
  */  
  PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t){  
           
      CaloTrkVar *c = 0;  
      ToFTrkVar  *o = 0;  
       
      if(CAL) c = GetCaloStoredTrack(t->GetSeqNo());  
      if(TOF) o = GetToFStoredTrack(t->GetSeqNo());  
       
 //    if(t && c && o)track = new PamTrack(t,c,o);  
      PamTrack *track = new PamTrack(t,c,o);  
       
      return track;  
   
  };  
 //--------------------------------------  
 //  
 //  
 //--------------------------------------  
399  /**  /**
400   * Retrieves the it-th stored track.   * Constructor
401   * It override TrkLevel2::GetTrack(int it).   * @param ddir Name of directory where level2 files are stored.
402   * @param itrk Track number, ranging from 0 to GetNTracks().   * @param list Name of an ascii file containing the list of file names
403     * @param detlist Options to chose what to load.
404     * Possible options are:
405     *       +AUTO --> load all trees/branches in the input files
406     *       +(-)ALL --> inlcude(exclude) all trees/branches
407     *       +(-)TRK1+(-)TRK2+(-)CAL1+(-)CAL2+(-)TOF+(-)TRG+(-)ND+(-)S4+(-)ORB+(-)AC --> inlcude(exclude) trees and branches
408     *       +(-)TRK0 --> include(exclude) tracker level0 tree
409     *       +(-)GP --> include exclude GPAMELA output tree
410     * If no options are specified, the default is assumed. Default is:
411     * +TRK2+CAL2+CAL1+TOF+TRG+ND+AC+S4+ORB
412   */   */
413    PamLevel2::PamLevel2(TString ddir, TString llist, TString detlist) {
414      Initialize();
415      TList* listf = GetListOfLevel2Files(ddir, llist);
416      if (listf)
417        GetPamTree(listf, detlist);
418      if (listf)
419        GetRunTree(listf);
420    }
421    ;
422    
423  PamTrack* PamLevel2::GetStoredTrack(Int_t itrk){  PamLevel2::PamLevel2(TString ddir, TList *llist, TString detlist) {
424          Initialize();
425      PamTrack *track = 0;    GetPamTree(llist, detlist);
426          GetRunTree(llist);
     if( itrk >=0 && itrk < TrkLevel2::ntrk() ){  
           
         TrkTrack *t = TrkLevel2::GetStoredTrack(itrk);  
         track = GetPamTrackAlong(t);  
           
     }else{  
         cout << "PamLevel2::GetStoredTrack(int) : tracker track SeqNo "<< itrk <<" does not exist (GetNTracks() = "<<TrkLevel2::GetNTracks()<<")"<<endl;  
     };  
       
     return track;  
       
427  }  }
428  //--------------------------------------  ;
 //  
 //  
 //--------------------------------------  
429  /**  /**
430   * Sort physical (tracker) tracks and stores them in the TRefArray (of TrkTrack objects) which pointer is  PamLevel2::sorted_tracks.   * Constructor
431   * @param how String to set the sorting cryterium (es: "CAL" or "TRK+CAL+TOF" ecc...).   * @param ddir Name of directory where level2 files are stored.
432   * Sorting cryteria:   * @param list Name of an ascii file containing the list of file names
433   * TRK: lower chi**2   * Default trees/branches are loaded. Default is:
434   * CAL: lower Y spatial residual on the first calorimeter plane   * +TRK2+CAL2+CAL1+TOF+TRG+ND+AC+S4+ORB
  * TOF: bigger numebr of hit PMTs along the track, on S12 S21 S32 (where paddles are along the Y axis).  
  * The default sorting cryterium is "TOF+CAL".  
  *  
  * 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).  
435   */   */
436  void PamLevel2::SortTracks(TString how){  PamLevel2::PamLevel2(TString ddir, TString llist) {
437      Initialize();
438      TList* listf = GetListOfLevel2Files(ddir, llist);
439      GetPamTree(listf, "");
440      GetRunTree(listf);
441    }
442    ;
443    
444    //Save current Object count  void PamLevel2::Initialize() {
445      Int_t ObjectNumber = TProcessID::GetObjectCount();  
446          h0_obj = 0;
447      sorted_tracks->Delete(); //temporaneo???    trk0_obj = 0;
448              calo0_obj = 0;
449      // loop over the tracks sorted by the tracker  
450      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);    trk2_obj = 0;
451      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);    trk1_obj = 0;
452      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);    trkh_obj = 0;
453          calo1_obj = 0;
454      if( !CAL &&  use_CAL) use_CAL = false;    calo2_obj = 0;
455      if( !TOF &&  use_TOF) use_TOF = false;    tof_obj = 0;
456          trig_obj = 0;
457      if( !TRK ){    s4_obj = 0;
458          cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;    nd_obj = 0;
459          return;    ac_obj = 0;
460      };    orb_obj = 0;
461          gp_obj = 0;
462      for(Int_t i=0; i < TrkLevel2::GetNTracks(); i++){  
463              run_obj = 0;//new GL_RUN();
464          TrkTrack *ts = 0;    soft_obj = 0;// Emiliano
465              irun = -1LL;
466          // get tracker tracks    irunt = -1LL;
467          TrkTrack   *tp = TrkLevel2::GetTrack(i);                    //tracker    runfirstentry = 0LL;
468  //              CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());    runlastentry = 0LL;
469  //              ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());    gltsync = 0; // Emiliano
470          CaloTrkVar *cp = 0;  
471          ToFTrkVar  *op = 0;    l0_file = NULL;
472              l0_tree = NULL;
473          // if track has an image, check image selection    iroot = -1;
474          if(tp->HasImage()){    dbc = 0;
475                
476              TrkTrack   *ti = TrkLevel2::GetTrackImage(i);              //tracker (image)    prevshift = 0;
477  //                      CaloTrkVar *ci = GetCaloStoredTrack(ti->GetSeqNo());    maxshift = 10; //EMILIANO
478  //                      ToFTrkVar  *oi = GetToFStoredTrack(ti->GetSeqNo());  
479              CaloTrkVar *ci = 0;    run_tree = NULL;
480              ToFTrkVar  *oi = 0;    run_tree_clone = NULL;
481                  sel_tree = NULL;
482              //assign starting scores    sel_tree_clone = NULL;
483              Int_t tp_score = 0;  //main track sorted by the tracker  
484              Int_t ti_score = 0;  //image track    irunentry = -1LL;
485                  pam_tree = NULL;
486              // ------------------------    for (Int_t i = 0; i < NCLONES; i++)
487              // calorimeter check      pam_tree_clone[i] = NULL;
488              // ------------------------  
489              // check the Y spatial residual on the first calorimeter plane    totdltime[0] = 0LL;
490              // (cut on calorimeter variables from Emiliano)    totdltime[1] = 0LL;
491              if(    totdltime[2] = 0LL;
492                  use_CAL            &&  
493                  npcfit[1] > 15     &&   //no. of fit planes on Y view    host = "mysql://localhost/pamelaprod";
494                  varcfit[1] < 1000. &&  //fit variance on Y view    user = "anonymous";
495                  true){    psw = "";
496                      const char *pamdbhost = gSystem->Getenv("PAM_DBHOST");
497                  cp = GetCaloStoredTrack(tp->GetSeqNo());    const char *pamdbuser = gSystem->Getenv("PAM_DBUSER");
498                  ci = GetCaloStoredTrack(ti->GetSeqNo());    const char *pamdbpsw = gSystem->Getenv("PAM_DBPSW");
499                      if (!pamdbhost)
500                  Float_t resy_p = cp->tbar[0][1] - cbar[0][1]; if(resy_p < 0)resy_p= - resy_p;      pamdbhost = "";
501                  Float_t resy_i = ci->tbar[0][1] - cbar[0][1]; if(resy_i < 0)resy_i= - resy_i;    if (!pamdbuser)
502                        pamdbuser = "";
503                  if(resy_p <= resy_i) tp_score++;    if (!pamdbpsw)
504                  else                 ti_score++;      pamdbpsw = "";
505              };    if (strcmp(pamdbhost, ""))
506              // ------------------------      host = pamdbhost;
507              // TOF check    if (strcmp(pamdbuser, ""))
508              // ------------------------          user = pamdbuser;
509              // check the number of hit pmts along the track    if (strcmp(pamdbpsw, ""))
510              // on S12 S21 and S32, where paddles are parallel to Y axis      psw = pamdbpsw;
511              if( use_TOF ){  
512                      //    sorted_tracks = 0;//new TRefArray();
513                  Int_t nphit_p =0;  
514                  Int_t nphit_i =0;    CAL0 = false;
515                      CAL1 = true;
516                  op = GetToFStoredTrack(tp->GetSeqNo());    CAL2 = true;
517                  oi = GetToFStoredTrack(ti->GetSeqNo());    TRK2 = true;
518                      TRK1 = false;
519  /*                              cout << "track: npmtadc "<< op->npmtadc << endl;    TRK0 = false;
520                                  cout << "track: npmttdc "<< op->npmttdc << endl;    TRKh = false;
521                                  cout << "image: npmtadc "<< oi->npmtadc << endl;    TRG = true;
522                                  cout << "image: npmttdc "<< oi->npmttdc << endl;*/    TOF = true;
523                      TOF0 = false;
524                  for (Int_t ih=0; ih < op->npmtadc; ih++){    S4 = true;
525                      Int_t pl = GetPlaneIndex( (op->pmtadc).At(ih) );    ND = true;
526                      if(pl == 1 || pl == 2 || pl == 5)nphit_p++;    AC = true;
527                  };    ORB = true;
528                      GP = false;
529                  for (Int_t ih=0; ih < oi->npmtadc; ih++){  
530                      Int_t pl = GetPlaneIndex( (oi->pmtadc).At(ih) );    RUN = true;
531                      if(pl == 1 || pl == 2 || pl == 5)nphit_i++;  
532                  };    SELLI = -1;
533                    
534                  if(    ISGP = false;
535                      use_TOF            &&  
536                      (nphit_p+nphit_i) !=0 &&        DBG = false;
537                      true){  
538                          tsorted = 0;
539                      if( nphit_p >= nphit_i) tp_score++;    timage = 0;
540                      else ti_score++;  
541                  };    howtosort = "+CAL+TOF";
542              };    //howtosort = "+TOF";
543              if(tp_score == ti_score) use_TRK = true;    sortthr = 100.;
544              // ------------------------  
545              // tracker check    issorted = false;
546              // ------------------------    lastsorted = -1;
547              // chi**2 difference is not always large enough to distinguish among  
548              // the real track and its image.  }
549              // Tracker check will be applied always when calorimeter and tof information is ambiguous.  ;
             if(use_TRK){  
                 if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;  
                 else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;  
             };  
               
             // ------------------------  
             // the winner is....  
             // ------------------------      
             if      (tp_score > ti_score) ts = tp;//the track sorted by the tracker!!  
             else if (tp_score < ti_score) ts = ti;//its image!!  
             else {  
                 ts = tp;  
 //                              cout << "Warning - track image ambiguity not solved" << endl;  
 //                              cout << ts->GetNtot() << " "<< ts->chi2 << " " << npcfit[1] << " "<< nphit_p << endl;  
             };  
               
         }else{  
             ts = tp;  
         };  
           
 //              cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;  
         sorted_tracks->Add(ts);//save the track in the sorted array  
 //              cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;  
           
     };  
     //Restore Object count  
     //To save space in the table keeping track of all referenced objects  
     //we assume that our events do not address each other. We reset the  
     //object count to what it was at the beginning of the event.  
     TProcessID::SetObjectCount(ObjectNumber);  
       
 };  
 //--------------------------------------  
 //  
 //  
 //--------------------------------------  
 /**  
  * This method overrides TrkLevel2::GetTracks(), where sorting is done by decreasing number of fit points and increasing chi^2.  
  * PamLevel2::GetTracks() keeps the same track order given by TrkLevel2::GetTracks(), but checks image selection by using calorimeter and ToF tracking information.  
  */  
  TRefArray *PamLevel2::GetTracks(){  
          //  
 //      SortTracks("+CAL+TOF");  
          SortTracks("+CAL+TRK");//TEMP!!!!!!!!!!!!!  
         //  
         return sorted_tracks;  
  };  
 //--------------------------------------  
  //  
  //  
 //--------------------------------------  
550  /**  /**
551   * Retrieves the it-th Pamela "physical" track.   * Delete the event (act as Dtor)
  * It override TrkLevel2::GetTrack(int it).  
  * @param it Track number, ranging from 0 to GetNTracks().  
552   */   */
553  PamTrack *PamLevel2::GetTrack(int it){  void PamLevel2::Delete() {
554            
555  //  *-*-*-*-*-*-*    if (run_obj)
556  //      SortTracks("+CAL+TOF");      delete run_obj;
557      SortTracks("+CAL+TRK");//TEMP!!!!!!!!!!!!!    if (soft_obj)
558  //  *-*-*-*-*-*-*      delete soft_obj; //Emiliano
559    
560      PamTrack *track = 0;    //    cout << "void PamLevel2::Clear()"<<endl;
561          if (h0_obj)
562      if( it >=0 && it < TrkLevel2::GetNTracks() && it<sorted_tracks->GetEntriesFast() ){      delete h0_obj;
563          TrkTrack   *t = (TrkTrack*)sorted_tracks->At(it);    if (trk0_obj)
564          track = GetPamTrackAlong(t);      delete trk0_obj;
565      }else{    if (calo0_obj)
566          cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<TrkLevel2::GetNTracks()<<")"<<endl;      delete calo0_obj;
567      if (trk1_obj)
568        delete trk1_obj;
569      if (trk2_obj)
570        delete trk2_obj;
571      if (trkh_obj)
572        delete trkh_obj;
573      if (calo1_obj)
574        delete calo1_obj;
575      if (calo2_obj)
576        delete calo2_obj;
577      if (tof_obj)
578        delete tof_obj;
579      if (trig_obj)
580        delete trig_obj;
581      if (s4_obj)
582        delete s4_obj;
583      if (nd_obj)
584        delete nd_obj;
585      if (ac_obj)
586        delete ac_obj;
587      if (orb_obj)
588        delete orb_obj;
589      if (gp_obj)
590        delete gp_obj;
591    
592      if (tsorted) {
593        tsorted->Delete();
594        delete tsorted;
595      }
596      if (timage) {
597        timage->Delete();
598        delete timage;
599      }
600    
601      if (dbc) {
602        dbc->Close();
603        delete dbc;
604      }
605    
606      if (gltsync)
607        delete gltsync;
608    
609      if (l0_file)
610        l0_file->Close();
611      //    if(pam_tree)pam_tree->Delete();;
612    
613      if (pam_tree) {
614        //
615        // we have first to find which chains we have to delete, then delete the main chain and only after delete the friend chains. Any other order causes a segfault...
616        //
617        TList *temp = pam_tree->GetListOfFriends();
618        TList *contents = new TList; // create chain friend list
619        contents->SetOwner();
620        TIter next(temp);
621        TChain *questo = 0;
622        while ((questo = (TChain*) next())) {
623          TString name = questo->GetName();
624          contents->Add((TChain*) gROOT->FindObject(name.Data()));// add object to the list
625      };      };
626            //
627      return track;      // deleting the main chain
628            //
629  };      pam_tree->Delete();
630        //
631        // deleting the friends...
632        //
633        TIter next2(contents);
634        TChain *questa = 0;
635        while (questa = (TChain*) next2()) {
636          TString name = questa->GetName();
637          questa->Delete();
638          questa = NULL;
639        };
640        //
641      };
642      pam_tree = NULL;
643    
644      if (run_tree)
645        run_tree->Delete();;
646      if (sel_tree)
647        sel_tree->Delete();;
648      for (Int_t i = 0; i < NCLONES; i++)
649        if (pam_tree_clone[i])
650          pam_tree_clone[i]->Delete();;
651      if (run_tree_clone)
652        run_tree_clone->Delete();;
653      if (sel_tree_clone)
654        sel_tree_clone->Delete();;
655    
656      if (irunoffset)
657        delete[] irunoffset;
658    
659    }
660    ;
661    
 //--------------------------------------  
 //  
 //  
 //--------------------------------------  
662  /**  /**
663   * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().   * Clear the event (NB! does not deallocate objects)
  * @param it Track number, ranging from 0 to GetNTracks().  
664   */   */
665  PamTrack *PamLevel2::GetTrackImage(int it){  void PamLevel2::Clear() {
666    
667  //      SortTracks("+CAL+TOF");    //    cout << "void PamLevel2::Clear()"<<endl;
668          SortTracks("+CAL+TRK");//TEMP!!!!!!!!!!!!!  
669              //
670          PamTrack *image = 0;    // 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
671      // want to load them for each event even if they are the same...
672          if( it >=0 && it < TrkLevel2::GetNTracks() ){    //
673                  TrkTrack *temp = (TrkTrack*)sorted_tracks->At(it);    //    if(run_obj)delete run_obj;
674                  if( temp->HasImage() ){    //    if(run_obj) run_obj->Clear();  // Emiliano: Do not deallocate run_obj here, it will give segmentation fault! call clear instead
675                          TrkTrack   *t = TrkLevel2::GetStoredTrack(temp->GetImageSeqNo());    //    if(soft_obj) soft_obj->Clear();
676                          image = GetPamTrackAlong(t);  
677                  }else{    if (h0_obj)
678                          cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;      h0_obj->Clear();
679                  };    //    if(trk0_obj) trk0_obj->Clear();
680          }else{    if (trk1_obj)
681                  cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<TrkLevel2::GetNTracks()<<")"<<endl;      trk1_obj->Clear();
682          };    if (trk2_obj)
683            trk2_obj->Clear();
684          return image;    if (trkh_obj)
685        trkh_obj->Clear();
686      if (calo0_obj)
687        calo0_obj->Clear();
688      if (calo1_obj)
689        calo1_obj->Clear();
690      if (calo2_obj)
691        calo2_obj->Clear();
692      if (tof_obj)
693        tof_obj->Clear();
694      if (trig_obj)
695        trig_obj->Clear();
696      if (s4_obj)
697        s4_obj->Clear();
698      if (nd_obj)
699        nd_obj->Clear();
700      if (ac_obj)
701        ac_obj->Clear();
702      if (orb_obj)
703        orb_obj->Clear();
704      if (gp_obj)
705        gp_obj->Clear();
706    
707      //    if(sorted_tracks)sorted_tracks->Clear();
708      //    sorted_tracks.Clear();
709    
710      if (tsorted) {
711        tsorted->Delete();
712      }
713      if (timage) {
714        timage->Delete();
715      }
716    }
717    ;
718    
719    void PamLevel2::Reset() {
720      //
721      // First of all clear everything
722      //
723      Clear();
724      //
725      // close and reset chains and pointers
726      //
727      if (pam_tree) {
728        //
729        // we have first to find which chains we have to delete, then delete the main chain and only after delete the friend chains. Any other order causes a segfault...
730        //
731        TList *temp = pam_tree->GetListOfFriends();
732        TList *contents = new TList; // create chain friend list
733        contents->SetOwner();
734        TIter next(temp);
735        TChain *questo = 0;
736        while ((questo = (TChain*) next())) {
737          TString name = questo->GetName();
738          contents->Add((TChain*) gROOT->FindObject(name.Data()));// add object to the list
739        };
740        //
741        // deleting the main chain
742        //
743        pam_tree->Delete();
744        //
745        // deleting the friends...
746        //
747        TIter next2(contents);
748        TChain *questa = 0;
749        while (questa = (TChain*) next2()) {
750          TString name = questa->GetName();
751          questa->Delete();
752          questa = NULL;
753        };
754        //
755      };
756      pam_tree = NULL;
757      //
758      if (run_tree)
759        run_tree->Delete();;
760      run_tree = NULL;
761      if (sel_tree)
762        sel_tree->Delete();;
763      sel_tree = NULL;
764      //
765      // Close file
766      //
767      if (l0_file)
768        l0_file->Close("R");
769      l0_file = NULL;
770      //
771      h0_obj = 0;
772      trk0_obj = 0;
773      calo0_obj = 0;
774      //
775      trk2_obj = 0;
776      trk1_obj = 0;
777      trkh_obj = 0;
778      calo1_obj = 0;
779      calo2_obj = 0;
780      tof_obj = 0;
781      trig_obj = 0;
782      s4_obj = 0;
783      nd_obj = 0;
784      ac_obj = 0;
785      orb_obj = 0;
786      gp_obj = 0;
787      //
788      // Reset run pointers
789      //
790      run_obj = 0;//new GL_RUN();
791      soft_obj = 0;// Emiliano
792      irun = -1;
793      irunt = -1;
794      runfirstentry = 0ULL;
795      runlastentry = 0ULL;
796      //
797      totdltime[0] = 0LL;
798      totdltime[1] = 0LL;
799      totdltime[2] = 0LL;
800      //
801    }
802    ;
803    
804    Bool_t PamLevel2::IsGood(Bool_t strict) {
805      Bool_t goodev = true;
806      //
807      if (calo2_obj && !calo2_obj->IsGood(strict))
808        goodev = false;
809      //
810      if (strict) {
811        if (trk2_obj && trk2_obj->UnpackError() != 0)
812          goodev = false;
813        if (tof_obj && tof_obj->unpackError != 0)
814          goodev = false;
815        if (trig_obj && trig_obj->unpackError != 0)
816          goodev = false;
817        if (s4_obj && s4_obj->unpackError != 0)
818          goodev = false;
819        if (nd_obj && nd_obj->unpackError != 0)
820          goodev = false;
821        if (ac_obj && (ac_obj->unpackError != 0 || ((ac_obj->status[0] >> 2) & 1) || ((ac_obj->status[1] >> 2) & 1)))
822          goodev = false;
823        //  if(orb_obj)
824      }
825      else {
826        if (nd_obj && nd_obj->unpackError != 0)
827          goodev = false;
828        if (ac_obj && (ac_obj->unpackError != 0 || ((ac_obj->status[0] >> 2) & 1) || ((ac_obj->status[1] >> 2) & 1)))
829          goodev = false;
830      };
831      return (goodev);
832  }  }
833    ;
834    
835  //--------------------------------------  //--------------------------------------
836  //  //
837  //  //
838  //--------------------------------------  //--------------------------------------
839  /**  void *PamLevel2::GetPointerTo(const char* c) {
840   * Get the Pamela detector trees in a single file and make them friends.  
841   * @param f TFile pointer    TString objname = c;
842   * @param detlist String to select trees to be included  
843   * @return Pointer to a TTree    if (!objname.CompareTo("TrkLevel1")) {
844   */      if (!trk1_obj) {
845  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist="+ALL"){        trk1_obj = new TrkLevel1();
846          trk1_obj->Set();
847        }
848        return &trk1_obj;
849      };
850      if (!objname.CompareTo("TrkLevel2")) {
851        if (!trk2_obj) {
852          trk2_obj = new TrkLevel2();
853          trk2_obj->Set();
854        }
855        return &trk2_obj;
856      };
857      if (!objname.CompareTo("TrkHough")) {
858        if (!trkh_obj) {
859          trkh_obj = new TrkHough();
860          trkh_obj->Set();
861        }
862        return &trkh_obj;
863      };
864      if (!objname.CompareTo("CaloLevel1")) {
865        if (!calo1_obj)
866          calo1_obj = new CaloLevel1();
867        return &calo1_obj;
868      };
869      if (!objname.CompareTo("CaloLevel2")) {
870        if (!calo2_obj) {
871          calo2_obj = new CaloLevel2();
872          calo2_obj->Set();
873        };
874        return &calo2_obj;
875      };
876      if (!objname.CompareTo("ToFLevel2")) {
877        if (!tof_obj) {
878          tof_obj = new ToFLevel2();
879          tof_obj->Set();
880        }
881        return &tof_obj;
882      };
883      if (!objname.CompareTo("TrigLevel2")) {
884        if (!trig_obj)
885          trig_obj = new TrigLevel2();
886        return &trig_obj;
887      };
888      if (!objname.CompareTo("S4Level2")) {
889        if (!s4_obj)
890          s4_obj = new S4Level2();
891        return &s4_obj;
892      };
893      if (!objname.CompareTo("NDLevel2")) {
894        if (!nd_obj)
895          nd_obj = new NDLevel2();
896        return &nd_obj;
897      };
898      if (!objname.CompareTo("AcLevel2")) {
899        if (!ac_obj)
900          ac_obj = new AcLevel2();
901        return &ac_obj;
902      };
903      if (!objname.CompareTo("OrbitalInfo")) {
904        if (!orb_obj) {
905          orb_obj = new OrbitalInfo();
906          orb_obj->Set();
907        }
908        return &orb_obj;
909      };
910      //     if(!objname.CompareTo("OrbitalInfo")){
911      //    if(!orb_obj)   orb_obj   = new OrbitalInfo();
912      //    return &orb_obj;
913      //     };
914      if (!objname.CompareTo("GPamela")) {
915        if (!gp_obj)
916          gp_obj = new GPamela();
917        return &gp_obj;
918      };
919    
920      if (!objname.CompareTo("RunInfo"))
921        return &run_obj;
922    
923  //      cout << "WARNING!!! -- obsolete method -- \n";    if (!objname.CompareTo("SoftInfo"))
924  //      cout << "better use TChain *PamLevel2::GetPamTree(TList*, TString) \n";      return &soft_obj; // Emiliano
925            
926      TTree *Trout =0;    return NULL;
       
     SetWhichTrees(detlist);  
 // Tracker  
     TTree *T = (TTree*)f->Get("Tracker");  
     if(T && TRK) {  
         if(TRK_L1){  
             T->SetBranchAddress("TrkLevel1", GetPointerToTrk(1));  
             cout << "Tracker      : set branch address TrkLevel1"<<endl;  
         };  
         T->SetBranchAddress("TrkLevel2", GetPointerToTrk(2));  
         cout << "Tracker      : set branch address TrkLevel2"<<endl;  
         Trout=T;  
     }else{  
         cout << "Tracker      : missing tree"<<endl;  
     };  
     // Calorimeter  
     TTree *C = (TTree*)f->Get("Calorimeter");  
     if(C && CAL) {  
         C->SetBranchAddress("CaloLevel2", GetPointerToCalo());  
         cout << "Calorimeter  : set branch address CaloLevel2"<<endl;  
         if(!Trout)Trout=C;  
         else Trout->AddFriend(C);  
     }else{  
         cout << "Calorimeter  : missing tree"<<endl;  
     };  
     // ToF      
     TTree *O = (TTree*)f->Get("ToF");  
     if(O && TOF) {  
         O->SetBranchAddress("ToFLevel2", GetPointerToToF());  
         cout << "ToF          : set branch address ToFLevel2"<<endl;  
         if(!Trout)Trout=O;  
         else Trout->AddFriend(O);  
     }else{  
         cout << "ToF         : missing tree"<<endl;  
     };  
     // Trigger  
     TTree *R = (TTree*)f->Get("Trigger");  
     if(R && TRG) {  
         R->SetBranchAddress("TrigLevel2", GetPointerToTrig());  
         cout << "Trigger      : set branch address TrigLevel2"<<endl;  
         if(!Trout)Trout=O;  
         else Trout->AddFriend(R);  
     }else{  
         cout << "Trigger      : missing tree"<<endl;  
     };  
     // S4  
     TTree *S = (TTree*)f->Get("S4");  
     if(S && S4) {  
         S->SetBranchAddress("S4Level2", GetPointerToS4());  
         cout << "S4           : set branch address S4Level2"<<endl;  
         if(!Trout)Trout=O;  
         else Trout->AddFriend(S);  
     }else{  
         cout << "S4           : missing tree"<<endl;  
     };  
     // Neutron Detector  
     TTree *N = (TTree*)f->Get("NeutronD");  
     if(N && ND) {  
         N->SetBranchAddress("NDLevel2", GetPointerToND());  
         cout << "NeutronD     : set branch address NDLevel2"<<endl;  
         if(!Trout)Trout=O;  
         else Trout->AddFriend(N);  
     }else{  
         cout << "NeutronD     : missing tree"<<endl;  
     };  
     // Anticounters  
     TTree *A = (TTree*)f->Get("Anticounter");  
     if(A && AC) {  
         A->SetBranchAddress("AcLevel2", GetPointerToAc());  
         cout << "Anticounter  : set branch address AcLevel2"<<endl;  
         if(!Trout)Trout=O;  
         else Trout->AddFriend(A);  
     }else{  
         cout << "Anticounter  : missing tree"<<endl;  
     };  
     // Orbital Info  
         TTree *B = (TTree*)f->Get("OrbitalInfo");  
         if(B && ORB) {  
                 B->SetBranchAddress("OrbitalInfo", GetPointerToOrb());  
                 cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;  
                 if(!Trout)Trout=O;  
                 else Trout->AddFriend(B);  
         }else{  
                 cout << "OrbitalInfo  : missing tree"<<endl;  
         };  
       
         cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;  
           
         return Trout;  
       
927  }  }
928    ;
929  //--------------------------------------  //--------------------------------------
930  //  //
931  //  //
932  //--------------------------------------  //--------------------------------------
933  /**  /**
934   * Get list of Level2 files.   * Retrieves the calorimeter track matching the seqno-th tracker stored track.
935   * @param ddir Level2 data directory.   * (If seqno = -1 retrieves the self-trigger calorimeter track)
  * @param flisttxt Name of txt file containing file list.  
  * @return Pointer to a TList of TSystemFiles  
  * If no input file list is given , all the Level2 files inside the directory are processed.  
936   */   */
937  TList*  PamLevel2::GetListOfLevel2Files(TString ddir, TString flisttxt = ""){  CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno) {
938            
939      TString wdir = gSystem->WorkingDirectory();    if (!calo2_obj)
940            return 0;
941      if(ddir=="")ddir = wdir;  
942  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);    if (calo2_obj->CaloLevel2::ntrk() == 0) {
943      cout << "Level2 data directory : "<<  ddir << endl;      cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo " << seqno
944                << " but no Calorimeter tracks are stored" << endl;
945      TList *contents  = new TList; // create output list      return NULL;
946      contents->SetOwner();    };
947        
948      char *fullpath;    CaloTrkVar *c = 0;
949          Int_t it_calo = 0;
950      // if no input file list is given:    
951      if ( flisttxt != "" ){    do {
952                c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);
953          if( !gSystem->IsFileInIncludePath(flisttxt,&fullpath) )return 0;      it_calo++;
954              } while (c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());
955  //              flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));  
956          flisttxt = fullpath;    if (!c || seqno != c->trkseqno) {
957                c = 0;
958          if( !gSystem->ChangeDirectory(ddir) )return 0;      if (seqno != -1)
959                  cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo " << seqno
960          cout <<"Input file list : " << flisttxt <<endl;            << " does not match Calorimeter stored tracks" << endl;
961          ifstream in;    };
962          in.open(flisttxt, ios::in); //open input file list    return c;
963          while (1) {  
964              TString file;  }
965              in >> file;  ;
             if (!in.good()) break;  
             if( gSystem->IsFileInIncludePath(file,&fullpath) ){  
 //                              contents->Add(new TSystemDirectory(fullpath,ddir)); // add file to the list  
 //                              contents->Add(new TSystemFile(fullpath,ddir)); // add file to the list  
                 contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list  
             }else{  
                 cout << "warning! --- File "<<file<<" does not exists"<< endl;  
             };  
         };  
         in.close();  
           
     }else{  
           
         cout << "No input file list given."<<endl;  
         cout << "Check for existing root files."<<endl;  
 //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;  
           
         TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);  
         TList *temp = datadir->GetListOfFiles();  
 //              temp->Print();  
 //              cout << "*************************************" << endl;  
           
         TIter next(temp);  
         TSystemFile *questo = 0;  
           
           
         while ( (questo = (TSystemFile*) next()) ) {  
             TString name =  questo-> GetName();  
             if( name.EndsWith(".root") ){  
                 char *fullpath;  
                 gSystem->IsFileInIncludePath(name,&fullpath);  
                 contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));  
             };  
         }  
         delete temp;  
         delete datadir;  
           
     };  
     gSystem->ChangeDirectory(wdir); // back to the working directory  
 //      cout << endl << "Selected files:" << endl;  
 //      contents->Print();  
     cout << contents->GetEntries()<<" files selected\n";  
 //      cout << endl;  
 //      cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;  
     return contents;  
 };  
966  //--------------------------------------  //--------------------------------------
967  //  //
968  //  //
969  //--------------------------------------  //--------------------------------------
970  /**  /**
971   * Get the Pamela detector chains from a list of files and make them friends.   * Retrieves the ToF track matching the seqno-th tracker stored track.
972   * @param fl Pointer to a TList of TSystemFiles   * (If seqno = -1 retrieves the tracker-independent tof track)
  * @param detlist String to select trees to be included  
  * @return Pointer to a TChain  
973   */   */
974  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist="+ALL"){  ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno) {
975    
976      if (!tof_obj)
977        return 0;
978    
979      if (tof_obj->ToFLevel2::ntrk() == 0) {
980        cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo " << seqno << " but no ToF tracks are stored"
981            << endl;
982        return NULL;
983      };
984    
985      ToFTrkVar *c = 0;
986      Int_t it_tof = 0;
987    
988      do {
989        c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof);
990        it_tof++;
991      } while (c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk());
992    
993      if (!c || seqno != c->trkseqno) {
994        c = 0;
995        if (seqno != -1)
996          cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo " << seqno
997              << " does not match ToF stored tracks" << endl;
998      };
999      return c;
1000    
 //      TChain *Tout=0;  
     if(Tout)Tout->Delete();  
     Tout = NULL;  
       
     SetWhichTrees(detlist);  
       
     TChain *T = 0;        
     TChain *C = 0;  
     TChain *O = 0;  
     TChain *R = 0;  
     TChain *S = 0;  
     TChain *N = 0;  
     TChain *A = 0;  
     TChain *B = 0;  
       
     if(TRK) T = new TChain("Tracker");    
     if(CAL) C = new TChain("Calorimeter");  
     if(TOF) O = new TChain("ToF");  
     if(TRG) R = new TChain("Trigger");  
     if(S4)  S = new TChain("S4");  
     if(ND)  N = new TChain("NeutronD");  
     if(AC)  A = new TChain("Anticounter");  
     if(ORB) B = new TChain("OrbitalInfo");  
       
     // loop over files and create chains          
     TIter next(fl);  
     TSystemFile *questo = 0;  
     while ( (questo = (TSystemFile*) next()) ) {  
         TString name =  questo->GetName();  
 //              cout << "File: "<< name << endl;  
         if( CheckLevel2File(name) ){  
             if(TRK) T->Add(name);  
             if(CAL) C->Add(name);  
             if(TOF) O->Add(name);  
             if(TRG) R->Add(name);  
             if(S4)  S->Add(name);  
             if(ND)  N->Add(name);  
             if(AC)  A->Add(name);  
             if(ORB) B->Add(name);  
         };  
     }  
       
     cout << "done chain \n";  
   
     // Tracker  
     if(TRK) if(!Tout)Tout=T;  
     // Calorimeter  
     if(CAL) {            
         if(!Tout)Tout=C;  
         else Tout->AddFriend("Calorimeter");  
     };  
     // ToF      
     if(TOF) {  
         if(!Tout)Tout=O;  
         else Tout->AddFriend("ToF");  
     };  
     // Trigger  
     if(TRG) {  
         if(!Tout)Tout=O;  
         else Tout->AddFriend("Trigger");  
     };  
     // S4  
     if(S4) {  
         if(!Tout)Tout=O;  
         else Tout->AddFriend("S4");  
     };  
     // Neutron Detector  
     if(ND) {  
         if(!Tout)Tout=O;  
         else Tout->AddFriend("NeutronD");  
     };  
     // Anticounters  
     if(AC) {  
         if(!Tout)Tout=O;  
         else Tout->AddFriend("Anticounter");  
     };  
     // OrbitalInfo  
     if(ORB) {  
         if(!Tout)Tout=O;  
         else Tout->AddFriend("OrbitalInfo");  
     };  
       
 //    cout<<endl<<" Number of entries: "<<Tout->GetEntries()<<endl<<endl;  
       
     if( Tout->GetEntries() )PamLevel2::SetBranchAddress();  
       
     return Tout;  
1001  }  }
1002    ;
1003    
1004  //--------------------------------------  //--------------------------------------
1005  //  //
1006  //  //
1007  //--------------------------------------  //--------------------------------------
1008  /**  /**
1009   * Set branch addresses for Pamela friend trees   * Retrieves the OrbitalInfo track matching the seqno-th tracker stored track.
1010     * (If seqno = -1 retrieves the tracker-independent tof related track)
1011   */   */
1012  void PamLevel2::SetBranchAddress(){  OrbitalInfoTrkVar *PamLevel2::GetOrbitalInfoStoredTrack(int seqno) {
     // Tracker  
     if(TRK) {  
         if(TRK_L1){  
             Tout->SetBranchAddress("TrkLevel1", GetPointerToTrk(1));  
             cout << "Tracker      : set branch address TrkLevel1"<<endl;  
         };  
         Tout->SetBranchAddress("TrkLevel2", GetPointerToTrk(2));  
         cout << "Tracker      : set branch address TrkLevel2"<<endl;  
     };  
       
     // Calorimeter  
     if(CAL) {  
         Tout->SetBranchAddress("CaloLevel2", GetPointerToCalo());  
         cout << "Calorimeter  : set branch address CaloLevel2"<<endl;  
     };  
       
     // ToF      
     if(TOF) {  
         Tout->SetBranchAddress("ToFLevel2", GetPointerToToF());  
         cout << "ToF          : set branch address ToFLevel2"<<endl;  
     };  
     // Trigger  
     if(TRG) {  
         Tout->SetBranchAddress("TrigLevel2", GetPointerToTrig());  
         cout << "Trigger      : set branch address TrigLevel2"<<endl;  
     };  
     // S4  
     if(S4) {  
         Tout->SetBranchAddress("S4Level2", GetPointerToS4());  
         cout << "S4           : set branch address S4Level2"<<endl;  
     };  
     // Neutron Detector  
     if(ND) {  
         Tout->SetBranchAddress("NDLevel2", GetPointerToND());  
         cout << "NeutronD     : set branch address NDLevel2"<<endl;  
     };  
     // Anticounters  
     if(AC) {  
         Tout->SetBranchAddress("AcLevel2", GetPointerToAc());  
         cout << "Anticounter  : set branch address AcLevel2"<<endl;  
     };  
     // OrbitalInfo  
     if(ORB) {  
         Tout->SetBranchAddress("OrbitalInfo", GetPointerToOrb());  
         cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;  
     };  
       
 }  
1013    
1014  //--------------------------------------    if (!orb_obj)
1015  //      return 0;
1016  //  
1017  //--------------------------------------    if (orb_obj->OrbitalInfo::ntrk() == 0) {
1018  void *PamLevel2::GetPointerTo(const char* c ){      //       // TRICK BEGIN
1019  //      cout << "objname "<< objname << endl;      //       OrbitalInfoTrkVar  *r = new OrbitalInfoTrkVar(); // TEMPORARY TRICK
1020      TString objname = c;      //       Int_t nn = 0;
1021      if(!objname.CompareTo("TrkLevel1"))return &trk_l1_obj;      //       TClonesArray &tor = *orb_obj->OrbitalInfoTrk;
1022      if(!objname.CompareTo("TrkLevel2"))return &trk_obj;      //       for(Int_t nt=0; nt < tof_obj->ToFLevel2::ntrk(); nt++){
1023            //  //
1024        //  ToFTrkVar *ptt = tof_obj->ToFLevel2::GetToFTrkVar(nt);
1025        //  if ( ptt->trkseqno != -1  ){
1026        //    //
1027        //    r->trkseqno = ptt->trkseqno;
1028        //    //
1029        //    r->Eij = 0;
1030        //    //
1031        //    r->Sij = 0;
1032        //    //
1033        //    r->pitch = -1000.;
1034        //    //
1035        //    r->cutoff = -1000.;
1036        //    //
1037        //    new(tor[nn]) OrbitalInfoTrkVar(*r);
1038        //    nn++;
1039        //    //
1040        //    r->Clear();
1041        //    //
1042        //  };
1043        //       };
1044        //       delete r;
1045        //       OrbitalInfoTrkVar *c = 0;
1046        //       c = orb_obj->OrbitalInfo::GetOrbitalInfoTrkVar(0);
1047        //       return c;
1048        //       //TRICK END
1049        cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo " << seqno
1050            << " but no OrbitalInfo tracks are stored" << endl;
1051      return NULL;      return NULL;
1052  };    };
1053    
1054      OrbitalInfoTrkVar *c = 0;
1055      Int_t it_tof = 0;
1056    
1057      do {
1058        c = orb_obj->OrbitalInfo::GetOrbitalInfoTrkVar(it_tof);
1059        it_tof++;
1060      } while (c && seqno != c->trkseqno && it_tof < orb_obj->OrbitalInfo::ntrk());
1061    
1062      if (!c || seqno != c->trkseqno) {
1063        c = 0;
1064        if (seqno != -1)
1065          cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo " << seqno
1066              << " does not match OrbitalInfo stored tracks" << endl;
1067      };
1068      return c;
1069    
1070    }
1071    ;
1072    
1073  //--------------------------------------  //--------------------------------------
1074  //  //
1075  //  //
1076  //--------------------------------------  //--------------------------------------
1077  /**  /**
1078   * Get the Run tree chain from a list of files.   * Give the pamela track associated to a tracker track, retrieving related calorimeter, orbitalinfo and tof track information.
  * @param fl Pointer to a TList of TSystemFiles  
  * @return Pointer to a TChain  
1079   */   */
1080  TChain *PamLevel2::GetRunTree(TList *fl){  PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t) {
1081            
1082      TChain *R = new TChain("Run");    cout << "PamLevel2::GetPamTrackAlong(TrkTrack* t) **obsolete** " << endl;
1083          cout << "(if you use it, remember to delete the PamTrack object)" << endl;
1084      // loop over files and create chains          
1085      TIter next(fl);    CaloTrkVar *c = 0;
1086      TSystemFile *questo = 0;    ToFTrkVar *o = 0;
1087      while ( (questo = (TSystemFile*) next()) ) {    OrbitalInfoTrkVar *r = 0;
1088          TString name =  questo->GetName();  
1089  //              cout << "File: "<< name << endl;    if (CAL2)
1090          if( CheckLevel2File(name) ){      c = GetCaloStoredTrack(t->GetSeqNo());
1091              R->Add(name);    if (TOF)
1092          };      o = GetToFStoredTrack(t->GetSeqNo());
1093      }    if (ORB)
1094            r = GetOrbitalInfoStoredTrack(t->GetSeqNo());
1095      R->SetBranchAddress("RunInfo", GetPointerToRun());  
1096      cout << "Run         : set branch address RunInfo"<<endl;    //    if(t && c && o)track = new PamTrack(t,c,o);
1097          PamTrack *track = new PamTrack(t, c, o, r);
1098      return R;  
1099          return track;
1100    
1101  }  }
1102    ;
1103  //--------------------------------------  //--------------------------------------
1104  //  //
1105  //  //
1106  //--------------------------------------  //--------------------------------------
1107  /**  /**
1108   * Get the Run tree  from a file.   * Retrieves the it-th stored track.
1109   * @param f Pointer to a TFile   * It override TrkLevel2::GetTrack(int it).
1110   * @return Pointer to a TTree   * @param itrk Track number, ranging from 0 to GetNTracks().
1111   */   */
 TTree *PamLevel2::GetRunTree(TFile *f){  
1112    
1113    PamTrack* PamLevel2::GetStoredTrack(Int_t itrk) {
1114    
1115      cout << "PamLevel2::GetStoredTrack(Int_t itrk) **to-be-updated** " << endl;
1116      cout
1117          << "for the moment, better use separately the methods: TrkLevel2::GetStoredTrack(seqno) CaloLevel2::GetCaloTrkVar(Int_t notrack) ToFLevel2::GetToFTrkVar(Int_t notrack) OrbitalInfo::GetOrbitalInfoTrkVar(Int_t notrack)"
1118          << endl;
1119      cout << "(if you use it, remember to delete the PamTrack object)" << endl;
1120      PamTrack *track = 0;
1121    
1122      if (itrk >= 0 && itrk < trk2_obj->TrkLevel2::ntrk()) {
1123    
1124        TrkTrack *t = trk2_obj->TrkLevel2::GetStoredTrack(itrk);
1125        track = GetPamTrackAlong(t);
1126    
1127      }
1128      else {
1129        cout << "PamLevel2::GetStoredTrack(int) : tracker track SeqNo " << itrk << " does not exist (GetNTracks() = "
1130            << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
1131      };
1132    
1133      return track;
1134    
     TTree *R = (TTree*)f->Get("Run");  
       
     R->SetBranchAddress("RunInfo", GetPointerToRun());  
     cout << "Run         : set branch address RunInfo"<<endl;  
       
     return R;  
       
1135  }  }
1136  //--------------------------------------  //--------------------------------------
1137  //  //
1138  //  
 //--------------------------------------  
1139  /**  /**
1140   * Set which trees should be analysed   * Sort physical (tracker) tracks. Left here as backward compatibility method.
1141   * @param detlist TString containing the sequence of trees required   **/
1142  */  void PamLevel2::SortTracks(TString how) {
1143  void PamLevel2::SetWhichTrees(TString detlist){    printf(
1144                  " WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",
1145      if(detlist.Contains("+ALL", TString::kIgnoreCase)){        how.Data());
1146          CAL = true;    howtosort = how;
1147          TRK = true;    SortTracks();
1148          TRK_L1 = false;  }
1149          TRG = true;  ;
1150          TOF = true;  
         S4  = true;  
         ND  = true;  
         AC  = true;  
         ORB = true;  
     }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){  
         CAL = false;  
         TRK = false;  
         TRK_L1 = false;  
         TRG = false;  
         TOF = false;  
         S4  = false;  
         ND  = false;  
         AC  = false;  
         ORB = false;  
     };  
       
     if( detlist.Contains("-CAL", TString::kIgnoreCase) )CAL = false;  
     else if( detlist.Contains("+CAL", TString::kIgnoreCase) )CAL = true;  
       
     if( detlist.Contains("-TRK", TString::kIgnoreCase) )TRK = false;  
     else if( detlist.Contains("+TRK", TString::kIgnoreCase) )TRK = true;  
       
     if( detlist.Contains("-TRK1", TString::kIgnoreCase) )TRK_L1 = false;  
     else if( detlist.Contains("+TRK1", TString::kIgnoreCase) )TRK_L1 = true;  
       
     if( detlist.Contains("-TRG", TString::kIgnoreCase) )TRG = false;  
     else if( detlist.Contains("+TRG", TString::kIgnoreCase) )TRG = true;  
       
     if( detlist.Contains("-TOF", TString::kIgnoreCase) )TOF = false;  
     else if( detlist.Contains("+TOF", TString::kIgnoreCase) )TOF = true;  
       
     if( detlist.Contains("-S4",  TString::kIgnoreCase) )S4  = false;  
     else if( detlist.Contains("+S4",  TString::kIgnoreCase) )S4  = true;  
       
     if( detlist.Contains("-ND",  TString::kIgnoreCase) )ND  = false;  
     else if( detlist.Contains("+ND",  TString::kIgnoreCase) )ND  = true;  
       
     if( detlist.Contains("-AC",  TString::kIgnoreCase) )AC  = false;  
     else if( detlist.Contains("+AC",  TString::kIgnoreCase) )AC  = true;  
       
     if( detlist.Contains("-ORB", TString::kIgnoreCase) )ORB = false;  
     else if( detlist.Contains("+ORB", TString::kIgnoreCase) )ORB = true;  
       
 };  
 //--------------------------------------  
 //  
1151  //  //
1152  //--------------------------------------  //--------------------------------------
1153  /**  /**
1154   * Check if a file contains selected Pamela Level2 trees.   * Sort physical (tracker) tracks.
1155   * @param name File name   * @param how String to set the sorting cryterium (es: "CAL" or "TRK+CAL+TOF" ecc...).
1156   * @return true if the file is ok.   * Sorting cryteria:
1157     * TRK: lower chi**2
1158     * CAL: lower Y spatial residual on the first calorimeter plane
1159     * TOF: bigger numebr of hit PMTs along the track, on S12 S21 S32 (where paddles are along the Y axis).
1160     * S1: (ask Emiliano)
1161     * S2: (ask Emiliano)
1162     * S3: (ask Emiliano)
1163     * GP: more GP hits
1164     * The default sorting cryterium is "TOF+CAL".
1165     *
1166     * 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).
1167   */   */
1168  Bool_t  PamLevel2::CheckLevel2File(TString name){  void PamLevel2::SortTracks() {
1169            
1170      Bool_t CAL__ok = false;        //Check if the current event has already been sorted
1171      Bool_t TRK__ok = false;        if (issorted && lastsorted == GetReadEntry()) {
1172      Bool_t TRK_L1__ok = false;          return;
1173      Bool_t TRG__ok = false;        }
1174      Bool_t TOF__ok = false;        //Reset the sort flags, just in case something will go wrong...
1175      Bool_t S4__ok  = false;        issorted = false;
1176      Bool_t ND__ok  = false;        lastsorted = -1;
1177      Bool_t AC__ok  = false;      
1178      Bool_t ORB__ok = false;        TString how = howtosort;
1179        
1180      Bool_t RUN__ok = false;    //  cout <<" PamLevel2::SortTracks(TString how) "<<endl;
1181          if (!trk2_obj) {
1182  //    cout << "Checking file: "<<name<<endl;      cout << "void PamLevel2::SortTracks():  TrkLevel2 not loaded !!!";
1183      TFile *f = new TFile(name.Data());      return;
1184      if( !f || f->IsZombie() ){    };
1185          cout << "File: "<< f->GetName() <<" discarded ---- Non valid root file"<< endl; return false;    //Save current Object count
1186      }    Int_t ObjectNumber = TProcessID::GetObjectCount();
1187      //   cout << "Get list of keys: "<<f<<endl;  
1188      TList *lk = f->GetListOfKeys();    // create TCloneArrays to store tracks and its images
1189  //      lk->Print();    if (!tsorted)
1190      TIter next(lk);      tsorted = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
1191      TKey *key =0;    tsorted->Delete();
1192      Int_t nev =0;    TClonesArray &ttsorted = *tsorted;
1193      Int_t nev_previous =0;  
1194      while( (key = (TKey*)next()) ){    if (!timage)
1195                timage = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
1196          nev_previous = nev;    timage->Delete();
1197          nev = ((TTree*)f->Get(key->GetName()))->GetEntries();    TClonesArray &ttimage = *timage;
1198          if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){  
1199              cout << "File: "<< f->GetName() <<" discarded ---- "<< key->GetName() << " tree: n.entries does not match "<<nev<<" "<<nev_previous<< endl;    //--------------------------------------------------
1200              return false;    // retrieve sorting method
1201          };    //--------------------------------------------------
1202      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
1203          if( !strcmp(key->GetName(),"Calorimeter") )CAL__ok = true;    Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
1204          if( !strcmp(key->GetName(),"Trigger"    ) )TRG__ok = true;    Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
1205          if( !strcmp(key->GetName(),"ToF"        ) )TOF__ok = true;    Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase);
1206          if( !strcmp(key->GetName(),"S4"         ) )S4__ok = true;    Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase);
1207          if( !strcmp(key->GetName(),"NeutronD"   ) )ND__ok = true;    Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase);
1208          if( !strcmp(key->GetName(),"Anticounter") )AC__ok = true;    Bool_t use_GP = how.Contains("GP", TString::kIgnoreCase);
1209          if( !strcmp(key->GetName(),"OrbitalInfo") )ORB__ok = true;  
1210          if( !strcmp(key->GetName(),"Run"        ) )RUN__ok = true;          if (use_TOF) {
1211          if( !strcmp(key->GetName(),"Tracker"    ) ){      use_S1 = true;
1212              TRK__ok = true;      use_S2 = true;
1213              TTree *T = (TTree*)f->Get("Tracker");      use_S3 = true;
1214              for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){    };
1215                  TString name = T->GetListOfBranches()->At(i)->GetName();    if (!CAL2 && use_CAL)
1216                  if( !name.CompareTo("TrkLevel1") )TRK_L1__ok=true;      use_CAL = false;
1217              };    if (!TOF) {
1218          };      use_TOF = false;
1219                use_S1 = false;
1220      };      use_S2 = false;
1221            use_S3 = false;
1222      lk->Delete();    }
1223      f->Close();    if (!GP) {
1224            use_GP = false;
1225      Bool_t FLAG = true;    }
1226      if(!RUN__ok) {  
1227          cout << "File: "<< f->GetName() <<" discarded ---- Missing RunInfo tree"<< endl; return false;    if (!TRK2) {
1228  //      FLAG = false;      cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
1229      };      return;
1230      if(CAL && !CAL__ok){    };
1231          cout << "File: "<< f->GetName() <<" discarded ---- Missing Calorimeter tree"<< endl; return false;  
1232  //      FLAG = false;    //   cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;
1233      };  
1234      if(TRK && !TRK__ok){  
1235          cout << "File: "<< f->GetName() <<" discarded ---- Missing Tracker tree"<< endl; return false;    //--------------------------------------------------
1236  //      FLAG = false;    // loop over "physical" tracks sorted by the tracker
1237      };    //--------------------------------------------------
1238      if(TRK_L1 && !TRK_L1__ok){    for (Int_t i = 0; i < trk2_obj->TrkLevel2::GetNTracks(); i++) {
1239          cout << "File: "<< f->GetName() <<" discarded ---- Missing Tracker Level1 Branch"<< endl; return false;  
1240  //      FLAG = false;      TrkTrack *ts = 0;
1241      };      CaloTrkVar *cs = 0;
1242      if(TRG && !TRG__ok){      ToFTrkVar *os = 0;
1243          cout << "File: "<< f->GetName() <<" discarded ---- Missing Trigger tree"<< endl; return false;      OrbitalInfoTrkVar *rs = 0;
1244  //      FLAG = false;  
1245      };      // get tracker tracks
1246      if(TOF && !TOF__ok){      TrkTrack *tp = trk2_obj->TrkLevel2::GetTrack(i); //tracker
1247          cout << "File: "<< f->GetName() <<" discarded ---- Missing ToF tree"<< endl; return false;      CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());
1248  //      FLAG = false;      ToFTrkVar *op = GetToFStoredTrack(tp->GetSeqNo());
1249      };      OrbitalInfoTrkVar *rp = GetOrbitalInfoStoredTrack(tp->GetSeqNo());
1250      if(S4 && !S4__ok){  
1251          cout << "File: "<< f->GetName() <<" discarded ---- Missing S4 tree"<< endl; return false;      TrkTrack *ti = 0; //tracker (image)
1252  //      FLAG = false;      CaloTrkVar *ci = 0;
1253      };      ToFTrkVar *oi = 0;
1254      if(ND && !ND__ok){      OrbitalInfoTrkVar *ri = 0;
1255          cout << "File: "<< f->GetName() <<" discarded ---- Missing ND tree"<< endl; return false;      //  cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
1256  //      FLAG = false;      // if track has an image, check image selection
1257      };  
1258      if(AC && !AC__ok){      Int_t tp_score = 0; //main track sorted by the tracker
1259          cout << "File: "<< f->GetName() <<" discarded ---- Missing AC tree"<< endl; return false;      Int_t ti_score = 0; //image track
1260  //      FLAG = false;      Int_t totp_score = 0; //main track sorted by the tracker
1261      };      Int_t toti_score = 0; //image track
1262      if(ORB && !ORB__ok){  
1263          cout << "File: "<< f->GetName() <<" discarded ---- Missing ORB tree"<< endl; return false;      if (tp->HasImage()) {
1264  //      FLAG = false;  
1265      };        ti = trk2_obj->TrkLevel2::GetTrackImage(i); //tracker (image)
1266              ci = GetCaloStoredTrack(ti->GetSeqNo());
1267      return FLAG;        oi = GetToFStoredTrack(ti->GetSeqNo());
1268                  ri = GetOrbitalInfoStoredTrack(ti->GetSeqNo());
1269  };  
1270          //            cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
1271    
1272          //assign starting scores
1273          tp_score = 0; //main track sorted by the tracker
1274          ti_score = 0; //image track
1275    
1276          // -----------------------------------------------------------------------------------------
1277          // *****************************************************************************************
1278          // -----------------------------------------------------------------------------------------
1279          // calorimeter check
1280          // -----------------------------------------------------------------------------------------
1281          if (use_CAL && !calo2_obj) {
1282            cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but CaloLevel2 not loaded !!!";
1283            return;
1284          };
1285          if (use_CAL && !cp && ci) {
1286            ti_score++;
1287            toti_score++;
1288          };
1289          if (use_CAL && cp && !ci) {
1290            tp_score++;
1291            totp_score++;
1292          };
1293          if (use_CAL && cp && ci && true) {
1294    
1295            if (cp->npresh > ci->npresh && true) {
1296              tp_score++;
1297              totp_score++;
1298            };
1299            if (cp->npresh < ci->npresh && true) {
1300              ti_score++;
1301              toti_score++;
1302            };
1303    
1304            //      cout << "CALO "<<tp_score<<ti_score<<endl;
1305    
1306          };
1307          // -----------------------------------------------------------------------------------------
1308          // *****************************************************************************************
1309          // -----------------------------------------------------------------------------------------
1310          // TOF check
1311          // -----------------------------------------------------------------------------------------
1312          // check the number of hit pmts along the track
1313          // on S12 S21 and S32, where paddles are parallel to Y axis
1314          if ((use_TOF || use_S1 || use_S2 || use_S3) && !tof_obj) {
1315            cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but ToFLevel2 not loaded !!!";
1316            return;
1317          };
1318          //
1319          if ((use_TOF || use_S1 || use_S2 || use_S3) && !op && oi) {
1320            ti_score++;
1321            toti_score++;
1322          };
1323          if ((use_TOF || use_S1 || use_S2 || use_S3) && op && !oi) {
1324            tp_score++;
1325            totp_score++;
1326          };
1327          if ((use_TOF || use_S1 || use_S2 || use_S3) && op && oi) {
1328            //
1329            Float_t sen = 0.;
1330            for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1331              Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));
1332              if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
1333                sen += (op->dedx).At(ih);
1334            };
1335            for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1336              Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1337              if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
1338                sen += (oi->dedx).At(ih);
1339            };
1340            //
1341            if (sen >= sortthr && false) { // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!)
1342              //printf(" IS A NUCLEUS! en = %f \n",sen);
1343              //
1344              // is a nucleus use a different algorithm
1345              //
1346              Int_t nz = 6;
1347              Float_t zin[6]; // << define TOF z-coordinates
1348              for (Int_t ip = 0; ip < nz; ip++)
1349                zin[ip] = tof_obj->ToFLevel2::GetZTOF(tof_obj->ToFLevel2::GetToFPlaneID(ip)); // << read ToF plane z-coordinates
1350              Trajectory *tr = new Trajectory(nz, zin);
1351              //
1352              Int_t nphit_p = 0;
1353              Int_t nphit_i = 0;
1354              Float_t enhit_p = 0.;
1355              Float_t enhit_i = 0.;
1356              //
1357              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1358                Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));
1359                if (pl == 1 || pl == 2 || pl == 5) {
1360                  nphit_p++;
1361                  enhit_p += (op->dedx).At(ih);
1362                };
1363              };
1364              //
1365              tp->DoTrack2(tr);
1366              //
1367              if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
1368                for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1369                  Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));
1370                  if (pl == 0) {
1371                    nphit_p++;
1372                    enhit_p += (op->dedx).At(ih);
1373                  };
1374                };
1375              };
1376              if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
1377                for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1378                  Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));
1379                  if (pl == 3) {
1380                    nphit_p++;
1381                    enhit_p += (op->dedx).At(ih);
1382                  };
1383                };
1384              };
1385              if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
1386                for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1387                  Int_t pl = tof_obj->GetPlaneIndex((op->pmtadc).At(ih));
1388                  if (pl == 4) {
1389                    nphit_p++;
1390                    enhit_p += (op->dedx).At(ih);
1391                  };
1392                };
1393              };
1394    
1395              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1396                Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1397                if (pl == 1 || pl == 2 || pl == 5) {
1398                  nphit_i++;
1399                  enhit_i += (op->dedx).At(ih);
1400                };
1401              };
1402              //
1403              ti->DoTrack2(tr);
1404              //
1405              if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
1406                for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1407                  Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1408                  if (pl == 0) {
1409                    nphit_i++;
1410                    enhit_i += (op->dedx).At(ih);
1411                  };
1412                };
1413              };
1414              if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
1415                for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1416                  Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1417                  if (pl == 3) {
1418                    nphit_i++;
1419                    enhit_i += (op->dedx).At(ih);
1420                  };
1421                };
1422              };
1423              if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
1424                for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1425                  Int_t pl = tof_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1426                  if (pl == 4) {
1427                    nphit_i++;
1428                    enhit_i += (op->dedx).At(ih);
1429                  };
1430                };
1431              };
1432    
1433              if ((use_TOF || use_S1 || use_S2 || use_S3) && (nphit_p + nphit_i) != 0 && true) {
1434    
1435                //      printf(" seqno %i nphit_p %i nphit_i %i enhit_p %f enhit_i %f \n",trk2_obj->TrkLevel2::GetSeqNo(i),nphit_p,nphit_i,enhit_p,enhit_i);
1436                //      printf(" score p %i score i %i \n",tp_score,ti_score);
1437                //            if( enhit_p > enhit_i ) tp_score++;
1438                //            if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
1439                if (nphit_p > nphit_i)
1440                  tp_score++;
1441                if (nphit_p < nphit_i)
1442                  ti_score++;
1443                if (nphit_p == nphit_i) {
1444                  if (enhit_p > enhit_i)
1445                    tp_score++;
1446                  else
1447                    ti_score++;
1448                };
1449                //      printf(" dopo score p %i score i %i \n",tp_score,ti_score);
1450              };
1451              delete tr;
1452              //
1453            }
1454            else {
1455              // -------------
1456              // NOT a NUCLEUS
1457              // -------------
1458              //printf(" NOT a NUCLEUS! en = %f \n",sen);
1459    
1460              Int_t nphit_p = 0;
1461              Int_t nphit_i = 0;
1462    
1463              /*                            cout << "track: npmtadc "<< op->npmtadc << endl;
1464               cout << "track: npmttdc "<< op->npmttdc << endl;
1465               cout << "image: npmtadc "<< oi->npmtadc << endl;
1466               cout << "image: npmttdc "<< oi->npmttdc << endl;*/
1467    
1468              //      for (Int_t ih=0; ih < op->npmtadc; ih++){
1469              //        Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1470              //        if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
1471              //      };
1472    
1473              //      for (Int_t ih=0; ih < oi->npmtadc; ih++){
1474              //        Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1475              //        if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
1476              //      };
1477              // --- modified to count tdc signals (more efficient?)
1478              // --- and to implement check on tdcflag
1479              for (Int_t ih = 0; ih < op->npmttdc; ih++) {
1480                Int_t pl = tof_obj->GetPlaneIndex((op->pmttdc).At(ih));
1481                //      if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
1482                if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
1483                    || (use_S3 && (pl == 4 || pl == 5))) {
1484                  if ((op->tdcflag).At(ih) == 0)
1485                    nphit_p++;
1486                };
1487              };
1488    
1489              for (Int_t ih = 0; ih < oi->npmttdc; ih++) {
1490                Int_t pl = tof_obj->GetPlaneIndex((oi->pmttdc).At(ih));
1491                //      if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;
1492                if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
1493                    || (use_S3 && (pl == 4 || pl == 5))) {
1494                  if ((oi->tdcflag).At(ih) == 0)
1495                    nphit_i++;
1496                };
1497              };
1498    
1499              if ((nphit_p + nphit_i) != 0 && true) {
1500    
1501                if (nphit_p != nphit_i) {
1502                  totp_score += nphit_p;
1503                  toti_score += nphit_i;
1504                  tp_score += nphit_p;
1505                  ti_score += nphit_i;
1506                };
1507                //      if     ( nphit_p > nphit_i) tp_score+=nphit_p;
1508                //      else if( nphit_p < nphit_i) ti_score+=nphit_i;
1509                //      else ;//niente
1510              };
1511            };
1512            //      cout << "TOF "<<tp_score<<ti_score<<endl;
1513          };
1514    
1515          //      if(tp_score == ti_score) use_TRK = true;
1516    
1517    
1518          // -----------------------------------------------------------------------------------------
1519          // *****************************************************************************************
1520          // -----------------------------------------------------------------------------------------
1521          // tracker check
1522          // -----------------------------------------------------------------------------------------
1523          // chi**2 difference is not always large enough to distinguish among
1524          // the real track and its image.
1525          // Tracker check will be applied always when calorimeter and tof information is ambiguous.
1526          // *** MODIFIED ON AUGUST 2007 ***
1527          if (use_TRK) {
1528            //      if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
1529            //      else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
1530    
1531            // CHECK 1 : number of points along X
1532            if (tp->GetNX() >= ti->GetNX()) {
1533              tp_score++;
1534              totp_score++;
1535            };
1536            if (tp->GetNX() <= ti->GetNX()) {
1537              ti_score++;
1538              toti_score++;
1539            };
1540            // CHECK 2 : number of points along Y
1541            if (tp->GetNY() >= ti->GetNY()) {
1542              tp_score++;
1543              totp_score++;
1544            };
1545            if (tp->GetNY() <= ti->GetNY()) {
1546              ti_score++;
1547              toti_score++;
1548            };
1549    
1550            //      cout << "TRK "<<tp_score<<ti_score<<endl;
1551          };
1552    
1553          // -----------------------------------------------------------------------------------------
1554          // *****************************************************************************************
1555          // -----------------------------------------------------------------------------------------
1556          // GPamela check
1557          // -----------------------------------------------------------------------------------------
1558    
1559          //---------------------------------------------------
1560          // count the number of GP hits
1561          //---------------------------------------------------
1562          if (use_GP) {
1563            int ngphits_p = 0;
1564            int ngphits_i = 0;
1565            float toll = 0.02; //200 micron
1566            for (int ih = 0; ih < GetGPamela()->Nthspe; ih++) {
1567              int ip = (Int_t) GetGPamela()->Itrpb[ih] - 1;
1568              if (tp && tp->YGood(ip) && fabs(tp->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
1569                ngphits_p++;
1570              if (ti && ti->YGood(ip) && fabs(ti->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
1571                ngphits_i++;
1572            }
1573            if (ngphits_p > ngphits_i && true) {
1574              tp_score++;
1575              totp_score++;
1576            }
1577            if (ngphits_p < ngphits_i && true) {
1578              ti_score++;
1579              toti_score++;
1580            }
1581          }
1582    
1583          // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1584          // the winner is....
1585          // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1586          if (tp_score > ti_score) {
1587    
1588          }
1589          else if (tp_score < ti_score) {
1590    
1591            ts = ti;//its image!!
1592            cs = ci;
1593            os = oi;
1594            rs = ri;
1595            Int_t totis = toti_score;
1596    
1597            ti = tp;//its image!!
1598            ci = cp;
1599            oi = op;
1600            ri = rp;
1601    
1602            tp = ts;//its image!!
1603            cp = cs;
1604            op = os;
1605            rp = rs;
1606    
1607            toti_score = totp_score;
1608            totp_score = totis;
1609    
1610          }
1611          else {
1612    
1613            //      cout << "Warning - track image ambiguity not solved" << endl;
1614    
1615          };
1616    
1617        }
1618        else {
1619          totp_score = 1;
1620          toti_score = 0;
1621    
1622          //            ts = tp;
1623          //            cs = cp;
1624          //            os = op;
1625        };
1626    
1627        //  cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
1628        //  sorted_tracks->Add(ts);//save the track in the sorted array
1629        //  sorted_tracks.Add(ts);//save the track in the sorted array
1630        //  sorted_tracks.Add(tp);//save the track in the sorted array
1631        //  cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
1632        //  cout<<"o "<<tp<<endl;
1633        //  cout<<"o "<<cp<<endl;
1634        //  cout<<"o "<<op<<endl;
1635    
1636        new (ttsorted[i]) PamTrack(tp, cp, op, rp);
1637        new (ttimage[i]) PamTrack(ti, ci, oi, ri);
1638    
1639        ((PamTrack*) (ttsorted[i]))->SetPScore(totp_score);
1640        ((PamTrack*) (ttsorted[i]))->SetIScore(toti_score);
1641        ((PamTrack*) (ttimage[i]))->SetPScore(totp_score);
1642        ((PamTrack*) (ttimage[i]))->SetIScore(toti_score);
1643      };
1644    
1645      if (tsorted->GetEntries() != trk2_obj->GetNTracks()) {
1646        cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() " << tsorted->GetEntries()
1647            << " != trk2_obj->GetNTracks() = " << trk2_obj->GetNTracks() << endl;
1648        tsorted->Delete();
1649        tsorted = 0;
1650        timage->Delete();
1651        timage = 0;
1652      }
1653    
1654      //Restore Object count
1655      //To save space in the table keeping track of all referenced objects
1656      //We reset the object count to what it was at the beginning of the event.
1657      TProcessID::SetObjectCount(ObjectNumber);
1658    
1659      //Everything went fine so the current event can be tagged as sorted
1660      issorted = true;
1661      lastsorted = GetReadEntry();
1662    
1663    }
1664    ;
1665    //--------------------------------------
1666    //
1667    //
1668    //--------------------------------------
1669    /**
1670     * This method overrides TrkLevel2::GetTracks(), where sorting is done by decreasing number of fit points and increasing chi^2.
1671     * PamLevel2::GetTracks() keeps the same track order given by TrkLevel2::GetTracks(), but checks image selection by using calorimeter and ToF tracking information.
1672     */
1673    // TRefArray *PamLevel2::GetTracks(){
1674    
1675    // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1676    //     SortTracks("+CAL+TOF");
1677    // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1678    
1679    // //   return  sorted_tracks;
1680    //     return &sorted_tracks;
1681    
1682    //  };
1683    TClonesArray *PamLevel2::GetTracks() {
1684    
1685      //  *-*-*-*-*-*-*-*-*-*-*-*-*
1686      SortTracks();
1687      //  *-*-*-*-*-*-*-*-*-*-*-*-*
1688    
1689      return tsorted;
1690    
1691    }
1692    ;
1693    //--------------------------------------
1694    //
1695    //
1696    //--------------------------------------
1697    /**
1698     * Retrieves the it-th Pamela "physical" track.
1699     * It override TrkLevel2::GetTrack(int it).
1700     * @param it Track number, ranging from 0 to GetNTracks().
1701     */
1702    PamTrack *PamLevel2::GetTrack(int it) {
1703    
1704      //    if(!trk2_obj) return 0;
1705    
1706      // //  *-*-*-*+-*-*-*-*-*-*-*-*-*
1707      //     SortTracks("+CAL+TOF");
1708      // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1709      // //    if(!sorted_tracks)return 0;
1710      //     if(sorted_tracks.IsEmpty())return 0;
1711    
1712      //     PamTrack *track = 0;
1713    
1714      //     if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() && it<sorted_tracks.GetEntriesFast() ){
1715      //    TrkTrack   *t = (TrkTrack*)sorted_tracks.At(it);
1716      //    track = GetPamTrackAlong(t);
1717      //     }else{
1718      //    cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1719      //     };
1720    
1721      //     return track;
1722    
1723      //    cout << "PamLevel2::GetTrack(int it) "<<endl;
1724      //  *-*-*-*-*-*-*-*-*-*-*-*-*
1725      SortTracks();
1726      //  *-*-*-*-*-*-*-*-*-*-*-*-*
1727      if (!tsorted)
1728        return 0;
1729      if (!tsorted->GetEntries())
1730        return 0;
1731    
1732      PamTrack *track = 0;
1733    
1734      if (it >= 0 && it < trk2_obj->TrkLevel2::GetNTracks()) {
1735        //  TrkTrack   *t = (TrkTrack*)sorted_tracks.At(it);
1736        //  track = GetPamTrackAlong(t);
1737        //TClonesArray &t = *(tsorted);
1738        //track = (PamTrack*)t[it];
1739        track = (PamTrack*)((*tsorted)[it]);
1740      }
1741      else {
1742        cout << "PamLevel2::GetTrack(int) : tracker track SeqNo " << it << " does not exist (GetNTracks() = "
1743            << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
1744      };
1745    
1746      return track;
1747    
1748    }
1749    ;
1750    
1751    //--------------------------------------
1752    //
1753    //
1754    //--------------------------------------
1755    /**
1756     * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().
1757     * @param it Track number, ranging from 0 to GetNTracks().
1758     */
1759    PamTrack *PamLevel2::GetTrackImage(int it) {
1760    
1761      //  *-*-*-*-*-*-*-*-*-*-*-*-*
1762      SortTracks();
1763      //  *-*-*-*-*-*-*-*-*-*-*-*-*
1764      if (!timage)
1765        return 0;
1766      if (!timage->GetEntries())
1767        return 0;
1768    
1769      PamTrack *image = 0;
1770    
1771      if (it >= 0 && it < trk2_obj->TrkLevel2::GetNTracks()) {
1772        TClonesArray &t = *(tsorted);
1773        PamTrack *temp = (PamTrack*) t[it];
1774        if (temp->GetTrkTrack()->HasImage()) {
1775          TClonesArray & t = *(timage);
1776          image = (PamTrack*) t[it];
1777        }
1778        else {
1779          //            cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;
1780        };
1781      }
1782      else {
1783        cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo " << it << " does not exist (GetNTracks() = "
1784            << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
1785      };
1786    
1787      return image;
1788    }
1789    
1790    //--------------------------------------
1791    //
1792    //
1793    //--------------------------------------
1794    /**
1795     * Get the Pamela detector trees in a single file and make them friends.
1796     * @param f TFile pointer
1797     * @param detlist String to select trees to be included
1798     * @return Pointer to a TTree
1799     */
1800    TTree *PamLevel2::GetPamTree(TFile *f, TString detlist) {
1801    
1802      if (detlist.Contains("+AUTO", TString::kIgnoreCase)) {
1803        cout << "+AUTO" << endl;
1804        GetWhichTrees(f);
1805      };
1806      SetWhichTrees(detlist);
1807    
1808      if (pam_tree) {
1809        printf("WARNING: TTree *PamLevel2::GetPamTree(TFile *fl, TString detlist) -- pam_tree already exists!\n ");
1810        return pam_tree;
1811      };
1812      //
1813    
1814      cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte " << endl;
1815    
1816      //    SetWhichTrees(detlist);
1817    
1818      TTree *Trout = 0;
1819    
1820      TString fname = f->GetName();
1821      if (!CheckLevel2File(fname))
1822        return NULL;
1823    
1824      //    UInt_t *found=0;
1825    
1826      cout << "GetPamTree(TFile*,TString): detector list --> ";
1827      if (TRK1)
1828        cout << "TRK1 ";
1829      if (TRK2)
1830        cout << "TRK2 ";
1831      if (TRKh)
1832        cout << "TRKH ";
1833      if (CAL1)
1834        cout << "CAL1 ";
1835      if (CAL2)
1836        cout << "CAL2 ";
1837      if (TOF)
1838        cout << "TOF ";
1839      if (TRG)
1840        cout << "TRG ";
1841      if (AC)
1842        cout << "AC ";
1843      if (ND)
1844        cout << "ND ";
1845      if (S4)
1846        cout << "S4 ";
1847      if (ORB)
1848        cout << "ORB ";
1849      if (GP)
1850        cout << "GP ";
1851      cout << endl;
1852      if (SELLI && SELLI != 2)
1853        cout << ">>> Found selection-list <<<" << endl; //EMILIANO
1854    
1855      f->cd();
1856    
1857      // Tracker
1858      TTree *T = (TTree*) f->Get("Tracker");
1859      if (T && (TRK2 || TRK1 || TRKh)) {
1860        if (TRK2)
1861          T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
1862        //  else    T->SetBranchStatus("TrkLevel2",0,found);
1863        if (TRK2)
1864          cout << "Tracker      : set branch address TrkLevel2" << endl;
1865        if (TRK1)
1866          T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
1867        //  else    T->SetBranchStatus("TrkLevel1",0,found);
1868        if (TRK1)
1869          cout << "Tracker      : set branch address TrkLevel1" << endl;
1870        if (TRKh)
1871          T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1872        //  else    T->SetBranchStatus("TrkHough",0,found);
1873        if (TRKh)
1874          cout << "Tracker      : set branch address TrkHough" << endl;
1875        if (!Trout)
1876          Trout = T;
1877        else
1878          Trout->AddFriend(T);
1879      }
1880      else {
1881        cout << "Tracker      : missing tree" << endl;
1882      };
1883      // Calorimeter
1884      TTree *C = (TTree*) f->Get("Calorimeter");
1885      if (C && (CAL2 || CAL1)) {
1886        if (CAL2)
1887          C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1888        //  else    C->SetBranchStatus("CaloLevel2",0,found);
1889        if (CAL2)
1890          cout << "Calorimeter  : set branch address CaloLevel2" << endl;
1891        if (CAL1)
1892          C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1893        //  else    C->SetBranchStatus("CaloLevel1",0,found);
1894        if (CAL1)
1895          cout << "Calorimeter  : set branch address CaloLevel1" << endl;
1896        if (!Trout)
1897          Trout = C;
1898        else
1899          Trout->AddFriend(C);
1900      }
1901      else {
1902        cout << "Calorimeter  : missing tree" << endl;
1903      };
1904    
1905      // ToF
1906      TTree *O = (TTree*) f->Get("ToF");
1907      if (O && TOF) {
1908        O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
1909        cout << "ToF          : set branch address ToFLevel2" << endl;
1910        if (!Trout)
1911          Trout = O;
1912        else
1913          Trout->AddFriend(O);
1914      }
1915      else {
1916        cout << "ToF         : missing tree" << endl;
1917      };
1918      // Trigger
1919      TTree *R = (TTree*) f->Get("Trigger");
1920      if (R && TRG) {
1921        R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1922        cout << "Trigger      : set branch address TrigLevel2" << endl;
1923        if (!Trout)
1924          Trout = R;
1925        else
1926          Trout->AddFriend(R);
1927      }
1928      else {
1929        cout << "Trigger      : missing tree" << endl;
1930      };
1931      // S4
1932      TTree *S = (TTree*) f->Get("S4");
1933      if (S && S4) {
1934        S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1935        cout << "S4           : set branch address S4Level2" << endl;
1936        if (!Trout)
1937          Trout = S;
1938        else
1939          Trout->AddFriend(S);
1940      }
1941      else {
1942        cout << "S4           : missing tree" << endl;
1943      };
1944      // Neutron Detector
1945      TTree *N = (TTree*) f->Get("NeutronD");
1946      if (N && ND) {
1947        N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1948        cout << "NeutronD     : set branch address NDLevel2" << endl;
1949        if (!Trout)
1950          Trout = N;
1951        else
1952          Trout->AddFriend(N);
1953      }
1954      else {
1955        cout << "NeutronD     : missing tree" << endl;
1956      };
1957      // Anticounters
1958      TTree *A = (TTree*) f->Get("Anticounter");
1959      if (A && AC) {
1960        A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1961        cout << "Anticounter  : set branch address AcLevel2" << endl;
1962        if (!Trout)
1963          Trout = A;
1964        else
1965          Trout->AddFriend(A);
1966      }
1967      else {
1968        cout << "Anticounter  : missing tree" << endl;
1969      };
1970      // Orbital Info
1971      TTree *B = (TTree*) f->Get("OrbitalInfo");
1972      if (B && ORB) {
1973        B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1974        cout << "OrbitalInfo  : set branch address OrbitalInfo" << endl;
1975        if (!Trout)
1976          Trout = B;
1977        else
1978          Trout->AddFriend(B);
1979      }
1980      else {
1981        cout << "OrbitalInfo  : missing tree" << endl;
1982      };
1983    
1984      // GPamela
1985      TTree *G = (TTree*) f->Get("h20");
1986      if (G && GP) {
1987        if (!gp_obj)
1988          gp_obj = new GPamela();
1989        //      ------------------------------------
1990        //      ATTENZIONE!!!
1991        //      non so per quale arcano motivo,
1992        //      per l'albero di gpamela il branch address lo devo settare
1993        //      DOPO aver fatto friend
1994        //      FGRRRVZZZZUTSALKJMSLKJ!!!
1995        //      ------------------------------------
1996        //  gp_obj->SetBranchAddress(G); //ho dovuto fare in maniera diversa dagli altri
1997        //  cout << "h20          : set branch address GPamela "<<endl;
1998        if (!Trout)
1999          Trout = G;
2000        else
2001          Trout->AddFriend(G);
2002      }
2003      else {
2004        //  cout << "h20          : missing tree"<<endl;
2005      };
2006    
2007      TTree *L = (TTree*) f->Get("SelectionList");
2008      if (L && SELLI == 1) {
2009        cout << " TTree *PamLevel2::GetPamTree(TFile, TString) >>> SelectionList not implemented!!!!" << endl;
2010        sel_tree = 0;
2011      }
2012      else {
2013        cout << "SelectionList  : missing tree" << endl;
2014      };
2015    
2016      cout << endl << " Number of entries: " << Trout->GetEntries() << endl << endl;
2017    
2018      //      ------------------------------------
2019      //      ATTENZIONE!!!
2020      //      non so per quale arcano motivo,
2021      //      per l'albero di gpamela il branch address lo devo settare
2022      //      DOPO aver fatto friend
2023      //      FGRRRVZZZZUTSALKJMSLKJ!!!
2024      //      ------------------------------------
2025      if (G && GP) {
2026        gp_obj->SetBranchAddress(Trout); //ho dovuto fare in maniera diversa dagli altri
2027        cout << "h20          : set branch address GPamela " << endl;
2028      }
2029      else {
2030        cout << "h20          : missing tree" << endl;
2031      };
2032    
2033      pam_tree = (TChain*) Trout;
2034    
2035      return Trout;
2036    
2037    }
2038    //--------------------------------------
2039    //
2040    //
2041    //--------------------------------------
2042    /**
2043     * Get list of Level2 files.
2044     * @param ddir Level2 data directory.
2045     * @param flisttxt Name of txt file containing file list.
2046     * @return Pointer to a TList of TSystemFiles
2047     * If no input file list is given , all the Level2 files inside the directory are processed.
2048     */
2049    TList* PamLevel2::GetListOfLevel2Files(TString ddir, TString flisttxt = "") {
2050    
2051      TString wdir = gSystem->WorkingDirectory();
2052    
2053      if (ddir != "") {
2054        cout << "Level2 data directory : " << ddir << endl;
2055      }
2056      else {
2057        cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory "
2058            << endl;
2059      };
2060      TList *contents = new TList; // create output list
2061      contents->SetOwner();
2062    
2063      // --------------------------------------
2064      // case 1 : input file/file-list provided
2065      // --------------------------------------
2066      if (flisttxt != "") {
2067    
2068        // --------------------------------------
2069        // a list of files given as input
2070        // --------------------------------------
2071        if (!flisttxt.EndsWith(".root")) {
2072    
2073          TString dir = gSystem->DirName(flisttxt);
2074          //            cout << " List directory "<<dir<<endl;
2075          if (dir == "." || dir == "")
2076            flisttxt = gSystem->ConcatFileName(wdir.Data(), gSystem->BaseName(flisttxt));
2077          //                flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
2078    
2079          if (!gSystem->ChangeDirectory(ddir)) {
2080            cout << "Cannot change directory : " << ddir << endl;
2081            return 0;
2082          }
2083    
2084          cout << "Input file list : " << flisttxt << endl;
2085          ifstream in;
2086          in.open(flisttxt, ios::in); //open input file list
2087          if (!in.good()) {
2088            cout << " TList*  PamLevel2::GetListOfLevel2Files(TString, TString) --> ERROR opening the file " << endl;
2089            gSystem->ChangeDirectory(wdir); // back to the working directory
2090            return 0;
2091          }
2092          int line = 0;
2093          // ...........................
2094          // loop over file-list records
2095          // ...........................
2096          while (1) {
2097            TString file;
2098            in >> file;
2099            if (!in.good())
2100              break;
2101            line++;
2102            cout << "(*) " << file << endl;
2103            if (file.IsNull()) {
2104              cout << "-- list interrupted at line " << line << endl;
2105              break;
2106            }
2107            if (file.Contains("#"))
2108              file = file(0, file.First("#"));
2109            //
2110            // take only root files
2111            //
2112            if (file.EndsWith(".root")) {
2113              TString filedir;
2114              cout << ddir << endl;
2115              if (ddir != "") {
2116                filedir = ddir; // take the input dir
2117              }
2118              else {
2119                gSystem->ChangeDirectory(wdir); // back to the working directory
2120                filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir
2121              };
2122              filedir.Append("/");
2123              char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file));
2124              contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));// add file to the list
2125              cout << fullpath << endl;
2126              delete fullpath;
2127            }
2128            //          }else{
2129            //              if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;
2130            //          };
2131          };
2132          in.close();
2133          // --------------------------------------
2134          // a single root file given as input
2135          // --------------------------------------
2136        }
2137        else {
2138          if (flisttxt.Contains("#"))
2139            flisttxt = flisttxt(0, flisttxt.First("#"));
2140          char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt), gSystem->BaseName(flisttxt));
2141          contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));// add file to the list
2142          delete fullpath;
2143        };
2144        // ---------------------------------------------------------------------------------
2145        // case 2 : no input file/file-list provided --> read all files insede the directory
2146        // ---------------------------------------------------------------------------------
2147      }
2148      else {
2149    
2150        cout << "No input file list given." << endl;
2151        cout << "Check for existing root files." << endl;
2152        //          cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;
2153        if (ddir == "") {
2154          ddir = wdir;
2155          cout << "Level2 data directory : " << ddir << endl;
2156        };
2157    
2158        TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir), ddir);
2159        TList *temp = datadir->GetListOfFiles();
2160        if (!temp)
2161          return 0;
2162        //          temp->Print();
2163        //          cout << "*************************************" << endl;
2164    
2165        TIter next(temp);
2166        TSystemFile *questo = 0;
2167    
2168        while ((questo = (TSystemFile*) next())) {
2169          TString name = questo-> GetName();
2170          if (name.EndsWith(".root")) {
2171            //              const char *fullpath = gSystem->FindFile(ddir,name);
2172            //              char *fullpath;
2173            //              gSystem->IsFileInIncludePath(name,&fullpath);
2174            char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir), gSystem->BaseName(name));
2175            contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));
2176            delete fullpath;
2177          };
2178        }
2179        delete temp;
2180        delete datadir;
2181    
2182      };
2183      gSystem->ChangeDirectory(wdir); // back to the working directory
2184      //    cout << endl << "Selected files:" << endl;
2185      //    contents->Print();
2186      cout << contents->GetEntries() << " files \n";
2187      //    cout << endl;
2188      //    cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;
2189      return contents;
2190    }
2191    ;
2192    //--------------------------------------
2193    //
2194    //
2195    //--------------------------------------
2196    /**
2197     * Get the Pamela detector chains from a list of files and make them friends.
2198     * @param fl Pointer to a TList of TSystemFiles
2199     * @param detlist String to select trees to be included
2200     * @return Pointer to a TChain
2201     */
2202    TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) {
2203      //
2204      //
2205      //
2206      if (pam_tree) {
2207        printf("WARNING: TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) -- pam_tree already exists!\n ");
2208        return pam_tree;
2209      };
2210      //
2211    
2212      TChain *Trout = 0;
2213    
2214      // -------------------------------------------
2215      // set flags to include/exclude trees/branches
2216      // -------------------------------------------
2217      if (detlist.Contains("+AUTO", TString::kIgnoreCase)) {
2218        if (fl->GetEntries() > 0) {
2219          cout << "+AUTO" << endl;
2220          TFile *fprimo = new TFile(fl->At(0)->GetName());
2221          GetWhichTrees(fprimo);
2222          fprimo->Close();// AAAAARGGGGGHHHHH!!!!!!! non commentare questa riga, altrimenti si incasina il TChain
2223          fprimo->Delete();
2224        }
2225      };
2226      SetWhichTrees(detlist);
2227    
2228      // -------------------------------------------
2229      // build chains
2230      // -------------------------------------------
2231      TChain *T = 0;
2232      TChain *C = 0;
2233      TChain *O = 0;
2234      TChain *R = 0;
2235      TChain *S = 0;
2236      TChain *N = 0;
2237      TChain *A = 0;
2238      TChain *B = 0;
2239      TChain *G = 0;
2240    
2241      TChain *L = 0;
2242    
2243      if (TRK2 || TRK1 || TRKh)
2244        T = new TChain("Tracker");
2245      if (CAL2 || CAL1)
2246        C = new TChain("Calorimeter");
2247      if (TOF)
2248        O = new TChain("ToF");
2249      if (TRG)
2250        R = new TChain("Trigger");
2251      if (S4)
2252        S = new TChain("S4");
2253      if (ND)
2254        N = new TChain("NeutronD");
2255      if (AC)
2256        A = new TChain("Anticounter");
2257      if (ORB)
2258        B = new TChain("OrbitalInfo");
2259      if (GP)
2260        G = new TChain("h20");
2261      L = new TChain("SelectionList");
2262    
2263      // loop over files and create chains
2264      TIter next(fl);
2265      TSystemFile *questo = 0;
2266      while ((questo = (TSystemFile*) next())) {
2267        TString name = questo->GetName();
2268        cout << "File: " << name << endl;
2269        if (CheckLevel2File(name)) {
2270          if (TRK2 || TRK1 || TRKh)
2271            T->Add(name);
2272          if (CAL1 || CAL2)
2273            C->Add(name);
2274          if (TOF)
2275            O->Add(name);
2276          if (TRG)
2277            R->Add(name);
2278          if (S4)
2279            S->Add(name);
2280          if (ND)
2281            N->Add(name);
2282          if (AC)
2283            A->Add(name);
2284          if (ORB)
2285            B->Add(name);
2286          if (GP)
2287            G->Add(name);
2288          if (SELLI == 1)
2289            L->Add(name);
2290        };
2291      };
2292    
2293      cout << "done chains\n";
2294      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
2295    
2296      // -------------------------------------------
2297      // make friends
2298      // -------------------------------------------
2299    
2300      // Tracker
2301      cout << "Friends: " << endl;
2302      if (T && (TRK2 || TRK1 || TRKh)) {
2303        if (!Trout)
2304          Trout = T;
2305        else
2306          Trout->AddFriend("Tracker");
2307        //  cout << "+Tacker"<<endl;
2308      };
2309      // Calorimeter
2310      if (C && (CAL2 || CAL1)) {
2311        if (!Trout)
2312          Trout = C;
2313        else
2314          Trout->AddFriend("Calorimeter");
2315        //  cout << "+Calorimeter"<<endl;
2316      };
2317      // ToF
2318      if (O && TOF) {
2319        if (!Trout)
2320          Trout = O;
2321        else
2322          Trout->AddFriend("ToF");
2323        //  cout << "+ToF"<<endl;
2324      };
2325      // Trigger
2326      if (R && TRG) {
2327        if (!Trout)
2328          Trout = O;
2329        else
2330          Trout->AddFriend("Trigger");
2331        //  cout << "+Trigger"<<endl;
2332      };
2333      // S4
2334      if (S && S4) {
2335        if (!Trout)
2336          Trout = O;
2337        else
2338          Trout->AddFriend("S4");
2339        //  cout << "+S4"<<endl;
2340      };
2341      // Neutron Detector
2342      if (N && ND) {
2343        if (!Trout)
2344          Trout = O;
2345        else
2346          Trout->AddFriend("NeutronD");
2347        //  cout << "+NeutronD"<<endl;
2348      };
2349      // Anticounters
2350      if (A && AC) {
2351        if (!Trout)
2352          Trout = O;
2353        else
2354          Trout->AddFriend("Anticounter");
2355        //  cout << "+Anticounter"<<endl;
2356      };
2357      // Orbital Info
2358      if (B && ORB) {
2359        if (!Trout)
2360          Trout = O;
2361        else
2362          Trout->AddFriend("OrbitalInfo");
2363        //  cout << "+OrbitalInfo"<<endl;
2364      };
2365      // GPamela
2366      if (G && GP) {
2367        if (!Trout)
2368          Trout = G;
2369        else
2370          Trout->AddFriend("h20");
2371        //  cout << "+h20"<<endl;
2372      };
2373    
2374      //  =====================================
2375      //  SET BRANCH-ADDRESS AFTER CHAIN+FRIEND
2376      //  =====================================
2377      SetBranchAddress(Trout);
2378    
2379      //  ------------------------------------
2380      //  finally handle selection trees...
2381      //  (it is not friend of pamela tree)
2382      //  ------------------------------------
2383    
2384      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
2385    
2386      // Selection List
2387      if (L && SELLI == 1) {
2388        cout << ">>> Found selection-list <<<" << endl;
2389        //  L->SetBranchAddress("RunEntry",&irun);
2390        L->SetBranchAddress("RunEntry", &irunt);//NEWNEW
2391        cout << "SelectionList: set branch address RunEntry" << endl;
2392        L->SetBranchAddress("EventEntry", &irunentry);
2393        cout << "SelectionList: set branch address EventEntry" << endl;
2394        sel_tree = L;
2395        //          if(!Trout)Trout=O;
2396        //          else Trout->AddFriend("SelectionList");
2397        cout << "+SelectionList" << endl;
2398        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
2399      }
2400      else {
2401        //  cout << "SelectionList  : missing tree"<<endl;
2402        if (L)
2403          L->Delete();
2404      };
2405    
2406      //  --------------------------------------------
2407      //  return the pamela chain with all the friends
2408      //  --------------------------------------------
2409    
2410      pam_tree = Trout;
2411    
2412      return Trout;
2413    }
2414    
2415    //--------------------------------------
2416    //
2417    //
2418    //--------------------------------------
2419    /**
2420     * Set branch addresses for Pamela friend trees
2421     */
2422    void PamLevel2::SetBranchAddress(TTree *t) {
2423    
2424      TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");
2425      TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");
2426      TRKh = TRKh & t->GetBranchStatus("TrkHough");
2427      CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");
2428      CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");
2429      TOF = TOF & t->GetBranchStatus("ToFLevel2");
2430      TRG = TRG & t->GetBranchStatus("TrigLevel2");
2431      S4 = S4 & t->GetBranchStatus("S4Level2");
2432      ND = ND & t->GetBranchStatus("NDLevel2");
2433      AC = AC & t->GetBranchStatus("AcLevel2");
2434      ORB = ORB & t->GetBranchStatus("OrbitalInfo");
2435      GP = GP & t->GetBranchStatus("h20");
2436    
2437      // Tracker
2438      if (TRK1) {
2439        t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel2"));
2440        cout << "Tracker      : set branch address TrkLevel1" << endl;
2441      };
2442      if (TRK2) {
2443        t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));
2444        cout << "Tracker      : set branch address TrkLevel2" << endl;
2445      };
2446      if (TRKh) {
2447        t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
2448        cout << "Tracker      : set branch address TrkHough" << endl;
2449      };
2450    
2451      // Calorimeter
2452      if (CAL1) {
2453        t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
2454        cout << "Calorimeter  : set branch address CaloLevel1" << endl;
2455      };
2456      if (CAL2) {
2457        t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
2458        cout << "Calorimeter  : set branch address CaloLevel2" << endl;
2459      };
2460    
2461      // ToF
2462      if (TOF) {
2463        t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
2464        cout << "ToF          : set branch address ToFLevel2" << endl;
2465      };
2466      // Trigger
2467      if (TRG) {
2468        t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
2469        cout << "Trigger      : set branch address TrigLevel2" << endl;
2470      };
2471      // S4
2472      if (S4) {
2473        t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
2474        cout << "S4           : set branch address S4Level2" << endl;
2475      };
2476      // Neutron Detector
2477      if (ND) {
2478        t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
2479        cout << "NeutronD     : set branch address NDLevel2" << endl;
2480      };
2481      // Anticounters
2482      if (AC) {
2483        t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
2484        cout << "Anticounter  : set branch address AcLevel2" << endl;
2485      };
2486      // OrbitalInfo
2487      if (ORB) {
2488        t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
2489        cout << "OrbitalInfo  : set branch address OrbitalInfo" << endl;
2490      };
2491      // GPamela
2492      if (GP) {
2493        //  GetPointerTo("GPamela");
2494        if (!gp_obj)
2495          gp_obj = new GPamela();
2496        //  gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2497        // //       t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
2498        if (SELLI)
2499          t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
2500        else
2501          gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2502    
2503        cout << "h20          : set branch address GPamela " << endl;
2504      };
2505    
2506    }
2507    /**
2508     * Set branch addresses for Pamela friend trees
2509     */
2510    void PamLevel2::SetBranchAddress(TChain *t) {
2511    
2512      //     TRK2    = TRK2 & t->GetBranchStatus("TrkLevel2");
2513      //     TRK1    = TRK1 & t->GetBranchStatus("TrkLevel1");
2514      //     TRKh    = TRKh & t->GetBranchStatus("TrkHough");
2515      //     CAL1    = CAL1 & t->GetBranchStatus("CaloLevel1");
2516      //     CAL2    = CAL2 & t->GetBranchStatus("CaloLevel2");
2517      //     TOF    = TOF & t->GetBranchStatus("ToFLevel2");
2518      //     TRG    = TRG & t->GetBranchStatus("TrigLevel2");
2519      //     S4     = S4  & t->GetBranchStatus("S4Level2");
2520      //     ND     = ND  & t->GetBranchStatus("NDLevel2");
2521      //     AC     = AC  & t->GetBranchStatus("AcLevel2");
2522      //     ORB    = ORB & t->GetBranchStatus("OrbitalInfo");
2523      //    GP     = GP & t->GetBranchStatus("h20");
2524    
2525      // Tracker
2526      if (TRK2) {
2527        t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
2528        cout << "Tracker      : set branch address TrkLevel2" << endl;
2529      };
2530      if (TRK1) {
2531        t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
2532        cout << "Tracker      : set branch address TrkLevel1" << endl;
2533      };
2534      if (TRKh) {
2535        t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
2536        cout << "Tracker      : set branch address TrkHough" << endl;
2537      };
2538    
2539      // Calorimeter
2540      if (CAL2) {
2541        t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
2542        cout << "Calorimeter  : set branch address CaloLevel2" << endl;
2543      };
2544      if (CAL1) {
2545        t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
2546        cout << "Calorimeter  : set branch address CaloLevel1" << endl;
2547      };
2548    
2549      // ToF
2550      if (TOF) {
2551        t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
2552        cout << "ToF          : set branch address ToFLevel2" << endl;
2553      };
2554      // Trigger
2555      if (TRG) {
2556        t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
2557        cout << "Trigger      : set branch address TrigLevel2" << endl;
2558      };
2559      // S4
2560      if (S4) {
2561        t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
2562        cout << "S4           : set branch address S4Level2" << endl;
2563      };
2564      // Neutron Detector
2565      if (ND) {
2566        t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
2567        cout << "NeutronD     : set branch address NDLevel2" << endl;
2568      };
2569      // Anticounters
2570      if (AC) {
2571        t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
2572        cout << "Anticounter  : set branch address AcLevel2" << endl;
2573      };
2574      // OrbitalInfo
2575      if (ORB) {
2576        t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
2577        cout << "OrbitalInfo  : set branch address OrbitalInfo" << endl;
2578      };
2579      // GPamela
2580      //    cout <<"GP "<<GP<<endl;
2581      if (GP) {
2582        //  GetPointerTo("GPamela");
2583        if (!gp_obj)
2584          gp_obj = new GPamela();
2585        if (SELLI)
2586          t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
2587        else
2588          gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2589        //  gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2590        cout << "h20          : set branch address GPamela " << endl;
2591      };
2592      // SelectionList
2593      //     if(SELLI==1){
2594      //    t->SetBranchAddress("RunEntry",&irunt);//NEWNEW
2595      //    cout << "SelectionList: set branch address RunEntry"<<endl;
2596      //    t->SetBranchAddress("EventEntry",&irunentry);
2597      //    cout << "SelectionList: set branch address EventEntry"<<endl;
2598    
2599      //     }
2600    
2601    }
2602    
2603    //--------------------------------------
2604    //
2605    //
2606    //--------------------------------------
2607    /**
2608     * Get the Run tree chain from a list of files.
2609     * @param fl Pointer to a TList of TSystemFiles
2610     * @return Pointer to a TChain
2611     */
2612    TChain *PamLevel2::GetRunTree(TList *fl) {
2613      //
2614      //
2615      //
2616      if (run_tree) {
2617        printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");
2618        return run_tree;
2619      };
2620      //
2621    
2622    
2623      TChain *R = new TChain("Run");
2624    
2625      // loop over files and create chains
2626      TIter next(fl);
2627      TSystemFile *questo = 0;
2628      while ((questo = (TSystemFile*) next())) {
2629        TString name = questo->GetName();
2630        //          cout << "File: "<< name << endl;
2631        if (CheckLevel2File(name)) {
2632          R->Add(name);
2633        };
2634      }
2635    
2636      if (RUN && R->GetNtrees()) {
2637    
2638        R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
2639        cout << "Run         : set branch address RunInfo" << endl;
2640        R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
2641        cout << "Software    : set branch address SoftInfo" << endl; // Emiliano
2642    
2643        irunoffset = new int[R->GetNtrees()];
2644        if (DBG) {
2645          cout << "----------------------------------------------------" << endl;
2646          cout << "irun\t | ";
2647          cout << "tree\t |";
2648          //        cout << "offset\t |";
2649          cout << "RUN\t";
2650          cout << "FRAG\t";
2651          cout << "NEVENTS\t";
2652          cout << "absolute time\t\t\t";
2653          cout << "on-board time";
2654          cout << endl;
2655        }
2656        for (Int_t ii = 0; ii < R->GetEntries(); ii++) {
2657          R->GetEntry(ii);
2658          if (DBG) {
2659            cout << ii << "\t | ";
2660            cout << R->GetTreeNumber() << "\t |";
2661            //          cout << R->GetChainOffset()<< "\t |";
2662            cout << GetRunInfo()->ID << "\t";
2663            cout << GetRunInfo()->ID_RUN_FRAG << "\t";
2664            cout << GetRunInfo()->NEVENTS << "\t";
2665            cout << GetRunInfo()->RUNHEADER_TIME << " <---> " << GetRunInfo()->RUNTRAILER_TIME << "\t";
2666            cout << GetRunInfo()->RUNHEADER_OBT << " <---> " << GetRunInfo()->RUNTRAILER_OBT << "\t";
2667            cout << endl;
2668          }
2669          irunoffset[R->GetTreeNumber()] = R->GetChainOffset();
2670        }
2671        cout << "N.run = " << R->GetEntries() << endl;
2672        cout << "----------------------------------------------------" << endl;
2673    
2674      }
2675      else {
2676        delete R;
2677        R = 0;
2678      }
2679    
2680      run_tree = R;
2681    
2682      return R;
2683    
2684    }
2685    //--------------------------------------
2686    //
2687    //
2688    //--------------------------------------
2689    /**
2690     * Get the Run tree  from a file.
2691     * @param f Pointer to a TFile
2692     * @return Pointer to a TTree
2693     */
2694    TTree *PamLevel2::GetRunTree(TFile *f) {
2695      if (run_tree) {
2696        printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");
2697        return run_tree;
2698      };
2699    
2700      cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte " << endl;
2701    
2702      TTree *T = (TTree*) f->Get("Run");
2703    
2704      if (T) {
2705        T->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
2706        cout << "Run         : set branch address RunInfo" << endl;
2707        T->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
2708        cout << "Software    : set branch address SoftInfo" << endl; // Emiliano
2709    
2710      }
2711    
2712      run_tree = (TChain*) T;
2713    
2714      return T;
2715    
2716    }
2717    /**
2718     * Update the runinfo informations (to be used to have Run infos event by event basis)
2719     * @param run Pointer to the chain/tree which contains run infos
2720     * @return true if a new run has been read, false if it is still the same run
2721     */
2722    Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) {
2723      //
2724      // check if we have already called once GetEntry, if not call it
2725      //
2726      cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) --- ATTENZIONE --- NON E` MANTENUTA!!!!!!!.... "
2727          << endl;
2728      if (!run) {
2729        cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing RunInfo tree " << endl;
2730        return (false);
2731      }
2732      if (run->GetEntries() <= 0)
2733        return (false);
2734      //
2735    
2736      //  Int_t oldrun = irun;
2737      Long64_t oldrun = irun;
2738    
2739      // --------------------------------------
2740      // if it is a full file (not preselected)
2741      // --------------------------------------
2742      if (SELLI == 0) {
2743    
2744        //
2745        // the absolute time is necessary to relate the event with the run
2746        //
2747        if (!GetOrbitalInfo() && !ISGP) {
2748          cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing OrbitalInfo " << endl;
2749          return (false);
2750        }
2751    
2752        ULong64_t abstime = 0;
2753        if (GetOrbitalInfo())
2754          abstime = GetOrbitalInfo()->absTime;
2755    
2756        //
2757        // the first time the routine is called, set run search from the beginning
2758        //
2759        if (irun < 0LL) {
2760          irun = 0LL;
2761          run->GetEntry(irun);
2762          runfirstentry = 0LL;
2763          runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2764          if ((Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL)
2765            runlastentry -= 1LL;
2766    
2767          if (ISGP && run->GetEntries() != 1) {
2768            cout << "** WARNING ** simulated files are assumed to have 1 single run, not " << run->GetEntries() << endl;
2769            cout << "** WARNING ** run will not be updated" << endl;
2770          }
2771    
2772        };
2773        //
2774        if (ISGP)
2775          abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO
2776        //
2777        if (irun == run->GetEntries() - 1LL && !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime
2778            <= GetRunInfo()->RUNTRAILER_TIME)) {
2779          irun = -1LL;
2780          irunt = -1LL;
2781          runfirstentry = 0LL;
2782          runlastentry = -1LL;
2783        };
2784        // modificato il controllo sull'aggiornamento del run, per evitare problemi
2785        // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)
2786        //
2787        bool fromfirst = true;
2788        //
2789        while (!(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME) && irun
2790            < run->GetEntries() - 1LL) {
2791          //      while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){
2792          irun++;
2793          run->GetEntry(irun);
2794          runfirstentry = runlastentry;
2795          if ((Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL)
2796            runfirstentry += 1LL;
2797          runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2798          //          cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2799          //          cout << "runfirstentry "<<runfirstentry<<endl;
2800          //          printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));
2801          //          printf(" abstime %u trailertime %u \n",abstime,GetRunInfo()->RUNTRAILER_TIME);
2802          //          printf(" IDRUN %u \n",GetRunInfo()->ID);
2803          //
2804          //          prevshift = 0;
2805          //
2806          if (irun == (Long64_t)(run->GetEntries() - 1LL) && fromfirst && !(abstime >= GetRunInfo()->RUNHEADER_TIME
2807              && abstime <= GetRunInfo()->RUNTRAILER_TIME)) {
2808            printf(" resetting irun  (it should NOT happen!!!)\n");
2809            fromfirst = false;
2810            irun = 0;
2811            run->GetEntry(irun);
2812            runfirstentry = 0ULL;
2813            runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2814            if ((Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL)
2815              runlastentry -= 1LL;
2816          };
2817          //
2818        };
2819        //
2820        if (!(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME)) {
2821          printf(" Something very wrong here: cannot find RUN containing absolute time %llu \n", abstime);
2822          return false;
2823        }
2824        //
2825        if (irun == oldrun || irun >= run->GetEntries())
2826          return (false);
2827        //
2828        //  printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);
2829        //
2830        prevshift = 0;
2831        cout << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun << endl;
2832        //      cout << "runfirstentry "<<runfirstentry<<endl;
2833        return (true);
2834      };
2835      // ----------------------------------------------------
2836      // if it is a preselected file (there is SelectionList)
2837      // NBNB - the event tree MUST be read first
2838      // ----------------------------------------------------
2839      if (SELLI == 1) {
2840        sel_tree->GetEntry(iev);
2841        //      cout << irun << " "<< irunentry << endl;
2842        if (irun != oldrun) {
2843          run->GetEntry(irun);
2844          cout << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun << endl;
2845          prevshift = 0;
2846          return true;
2847        }
2848        return false;
2849      }
2850    
2851      return false;
2852      //
2853    }
2854    ;
2855    
2856    Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) {
2857    
2858      if (!run_tree) {
2859        cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded" << endl;
2860        return false;
2861      }
2862      if (run_tree->GetEntries() <= 0) {
2863        cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty" << endl;
2864        return (false);
2865      }
2866    
2867      Int_t oldrun = irun; // store current run index
2868    
2869      // -----------------------------------------------------------------------
2870      // the first time the routine is called, set run search from the beginning
2871      // -----------------------------------------------------------------------
2872    
2873      if (irun < 0) {
2874        irun = 0LL;
2875        irunt = 0LL;
2876        irunentry = 0;
2877        prevshift = 0;
2878        run_tree->GetEntry(irun);
2879        if (!GetOrbitalInfo())
2880          cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated " << endl;
2881        if (gltsync)
2882          delete gltsync; //Emiliano
2883        if (!dbc || (dbc && !dbc->IsConnected()))
2884          SetDBConnection(); //Emiliano
2885        gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); //Emiliano // the "false" means not to use level0 file (not necessary here)
2886        if (dbc)
2887          dbc->Close();// Emiliano
2888      };
2889      // ---------------------------------------------------------------
2890      // retrieve OBT and absolute time of the event
2891      // ---------------------------------------------------------------
2892      ULong64_t abstime = 0LL;
2893      //    ULong64_t obt     = 0LL; // Emiliano
2894      Long64_t obt = 0LL; // Emiliano, Long64_t GL_TIMESYNC::DBobt(UInt_t obt) since depending on the situation OBT is lowered or boosted
2895      if (GetOrbitalInfo()) {
2896        abstime = GetOrbitalInfo()->absTime;
2897        obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano
2898      }
2899      else {
2900        abstime = GetRunInfo()->RUNHEADER_TIME;
2901        obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano
2902      }
2903    
2904      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2905      // if it is a full file (not preselected)
2906      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2907      if (SELLI == 0 || SELLI == 2) { // Emiliano
2908    
2909        // ---------------------------------------------------------------
2910        // increment dead and live-time counters
2911        // (only when reading a file not preselected)
2912        // ---------------------------------------------------------------
2913        if (SELLI == 0) {
2914          if (GetTrigLevel2()) {
2915            totdltime[0] += GetTrigLevel2()->dltime[0];
2916            totdltime[1] += GetTrigLevel2()->dltime[1];
2917          }
2918          totdltime[2]++;
2919        }
2920    
2921        //
2922        bool fromfirst = true; // first loop over runs
2923    
2924    
2925        // ------------------------------------------------------
2926        // loop over runs to find the one that contains the event
2927        // ------------------------------------------------------
2928        while ((
2929        //          (
2930        //              !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)
2931        //                abstime <= GetRunInfo()->RUNTRAILER_TIME) &&
2932        //              !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)
2933        //                obt <= GetRunInfo()->RUNTRAILER_OBT)
2934        //              )
2935    
2936        !(abstime >= GetRunInfo()->RUNHEADER_TIME && // check on absolute time (s)
2937            abstime <= GetRunInfo()->RUNTRAILER_TIME) || !(obt >= gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) && // additional check on OBT (ms) // Emiliano
2938            obt <= gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) // Emiliano
2939            || GetRunInfo()->NEVENTS == 0
2940        //          || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift) // ERRORE!!! fa saltare i run con 1 evento
2941            || !(irunentry <= GetRunInfo()->NEVENTS - 1 - prevshift)) && irun < run_tree->GetEntries()) {
2942    
2943          //            if( !(abstime >= GetRunInfo()->RUNHEADER_TIME &&abstime <= GetRunInfo()->RUNTRAILER_TIME)  )cout << "ABS TIME "<<abstime << " " <<GetRunInfo()->RUNTRAILER_TIME <<endl;
2944          //            if( !(obt >= GetRunInfo()->RUNHEADER_OBT && obt <= GetRunInfo()->RUNTRAILER_OBT) )cout << "OBT TIME "<< obt <<" "<< GetRunInfo()->RUNTRAILER_OBT << endl;
2945          //            if( GetRunInfo()->NEVENTS==0  )cout <<"GetRunInfo()->NEVENTS==0 "<<endl;
2946          //            if( !(irunentry <= GetRunInfo()->NEVENTS-1-prevshift) ) cout << "irunentry > "<<GetRunInfo()->NEVENTS-1-prevshift << endl;
2947          // - - - - - - - - - - - - -
2948          // irunentry = position of current entry, relative to the run
2949          // prevshift = shift needed to synchronize l0 and l2 data (nested events)
2950          // - - - - - - - - - - - - -
2951    
2952          // -----------------------------------------
2953          // store dead and live-time of previous run
2954          // -----------------------------------------
2955          //            if(SELLI==0){
2956          if (SELLI != 2) {
2957            if (fromfirst) {
2958              if (oldrun == irun) {
2959                /// decrement counters
2960                if (GetTrigLevel2()) {
2961                  totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time
2962                  totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time
2963                }
2964                totdltime[2]--; //event counter
2965                if (DBG) {
2966                  cout << endl;
2967                  cout << "n.events     : " << totdltime[2] << endl;
2968                  cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;
2969                  cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;
2970                }
2971              }
2972              else {
2973                totdltime[0] = 0;//live-time
2974                totdltime[1] = 0;//dead-time
2975                totdltime[2] = 0; //event counter
2976                if (DBG)
2977                  cout << " *** JUMP RUN *** irun " << irun << endl;
2978              }
2979              /// add an entry
2980              if (run_tree_clone)
2981                if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
2982                  run_tree_clone->GetBranch("DeadLiveTime")->Fill();
2983              /// reset counters
2984              if (GetTrigLevel2()) {
2985                totdltime[0] = GetTrigLevel2()->dltime[0];//live-time
2986                totdltime[1] = 0; //dead-time
2987              }
2988              totdltime[2] = 1; //event counter
2989            }
2990          }
2991          //            }
2992    
2993          irun++;
2994          // ------------------------------------
2995          // if the end of run tree is reached...
2996          // ------------------------------------
2997          if (irun == run_tree->GetEntries()) {
2998            if (!fromfirst) {
2999              // -----------------------------------------------------
3000              // if it happened already once and the run was not found
3001              // ---> exit with error
3002              // -----------------------------------------------------
3003              cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- event entry #" << iev
3004                  << " does not belong to any run (should not happen)" << endl;
3005              return false;
3006            }
3007            // -----------------------------------------
3008            // ...otherwise repeat search from beginning
3009            // -----------------------------------------
3010            cout
3011                << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- reached end of run tree. searchin again from beginning "
3012                << endl;
3013            fromfirst = false;
3014            irun = 0LL;
3015            runfirstentry = 0LL;
3016          }
3017          // -------------------------------------------------------------------
3018          // save the index of the first entry of the run, relative to pam_tree,
3019          // and read a new run
3020          // -------------------------------------------------------------------
3021          if (irun > 0)
3022            runfirstentry += (GetRunInfo()->NEVENTS) - prevshift;
3023          irunentry = 0;
3024          prevshift = 0;
3025          run_tree->GetEntry(irun);//update runinfo
3026          irunt = irun - irunoffset[run_tree->GetTreeNumber()];
3027          if (gltsync)
3028            delete gltsync; // Emiliano
3029          if (!dbc || (dbc && !dbc->IsConnected()))
3030            SetDBConnection(); //Emiliano
3031          gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
3032          if (dbc)
3033            dbc->Close(); // Emiliano
3034          if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
3035            cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
3036                << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
3037            cout
3038                << "                                                            (NB!! in this case some events are assigned to a wrong run)"
3039                << endl;
3040          }
3041          //            if(hasfrag &&  fragid != GetRunInfo()->ID){
3042          //                cout << "... where is the next fragment ??"<<endl;
3043          //            }
3044        };
3045    
3046        // --------------------------------------
3047        // if there was no need to update the run
3048        // ---> exit with FALSE
3049        // --------------------------------------
3050        if (irun == oldrun)
3051          return (false);
3052    
3053        // --------------------------------------
3054        // ... otherwise
3055        // --------------------------------------
3056    
3057    
3058        // --------------------------------------
3059        // ---> exit with TRUE
3060        // --------------------------------------
3061        if (DBG)
3062          cout << endl << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun
3063              << endl;
3064        // ----------------------------------------------------
3065        // update the tracker parameters
3066        // (non ho trovato nessun altro modo sicuro di farlo...)
3067        // ----------------------------------------------------
3068        if (!dbc || (dbc && !dbc->IsConnected()))
3069          SetDBConnection();
3070        TrkParams::Set(GetRunInfo(), dbc);
3071        if (dbc)
3072          dbc->Close();
3073    
3074        // ----------------------------------------------------
3075        // then check if the run has a fragment
3076        // in this case we have to switch to the next fragment
3077        // when the end of the first fragment is reached
3078        // ----------------------------------------------------
3079        if (GetRunInfo()->ID_RUN_FRAG != 0 &&
3080        //      GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID &&
3081            DBG && true) {
3082          cout << "* fragment *" << endl;
3083        }
3084    
3085        return (true);
3086      };
3087      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
3088      // if it is a preselected file (there is SelectionList)
3089      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
3090      // irun  = run entry relative to the chain
3091      // irunt = run entry relative to the tree
3092      if (SELLI == 1) {
3093        sel_tree->GetEntry(iev);// read irunt from SelectionList
3094        irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW
3095        if (irun != oldrun) {
3096          if (irun < run_tree->GetEntries())
3097            run_tree->GetEntry(irun);
3098          // check if the time is ok (with merged files it is not...)
3099          // if not loop over run and look for the proper entry
3100          bool SECONDO_GIRO = false;
3101          //            Long64_t irun_start   = irun;
3102          int offset_start = irunoffset[sel_tree->GetTreeNumber()];
3103          while (((!(abstime >= GetRunInfo()->RUNHEADER_TIME && // check on absolute time (s)
3104              abstime <= GetRunInfo()->RUNTRAILER_TIME)
3105          //                        ||
3106          //                        !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)
3107          //                          obt <= GetRunInfo()->RUNTRAILER_OBT)
3108          ) || GetRunInfo()->NEVENTS == 0)
3109          //                && irun < run_tree->GetEntries()
3110          ) {
3111    
3112            if (DBG) {
3113              cout << " (test) ";
3114              cout << " tree " << sel_tree->GetTreeNumber();
3115              cout << " irunt " << irunt;
3116              cout << " offset " << irunoffset[sel_tree->GetTreeNumber()];
3117              cout << " abs " << abstime;
3118              cout << " >> " << GetRunInfo()->RUNHEADER_TIME << " " << GetRunInfo()->RUNTRAILER_TIME;
3119              cout << " obt " << obt;
3120              cout << " >> " << GetRunInfo()->RUNHEADER_OBT << " " << GetRunInfo()->RUNTRAILER_OBT;
3121              cout << " *** JUMP RUN *** irun " << irun;
3122              cout << endl;
3123            }
3124            //              irun++;
3125            irunoffset[sel_tree->GetTreeNumber()]++;
3126            irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW
3127            if (irun == run_tree->GetEntries() && SECONDO_GIRO) {
3128              //            if(irun == irun_start ){
3129              cout << " ...grrrvzzkhhhajsdkj!!!! " << endl;
3130              irunoffset[sel_tree->GetTreeNumber()] = offset_start;
3131              return false;
3132            }
3133            if (irun >= run_tree->GetEntries() || irun < 0) {
3134              cout << "irun = " << irun << " >>  search from the beginning... <<" << endl;
3135              SECONDO_GIRO = true;
3136              irun = 0;
3137              irunoffset[sel_tree->GetTreeNumber()] = -irunt;
3138            }
3139            run_tree->GetEntry(irun);
3140          }
3141    
3142          if (DBG) {
3143            cout << " (test) ";
3144            cout << " tree " << sel_tree->GetTreeNumber();
3145            cout << " irunt " << irunt;
3146            cout << " offset " << irunoffset[sel_tree->GetTreeNumber()];
3147            cout << " abs " << abstime;
3148            cout << " >> " << GetRunInfo()->RUNHEADER_TIME << " " << GetRunInfo()->RUNTRAILER_TIME;
3149            cout << " obt " << obt;
3150            cout << " >> " << GetRunInfo()->RUNHEADER_OBT << " " << GetRunInfo()->RUNTRAILER_OBT;
3151          }
3152          if (DBG)
3153            cout << endl;
3154          if (DBG)
3155            cout << endl << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " (run-entry "
3156                << irun << ")" << endl;
3157          // ----------------------------------------------------
3158          // update the tracker parameters
3159          // (non ho trovato nessun altro modo sicuro di farlo...)
3160          // ----------------------------------------------------
3161          if (!dbc || (dbc && !dbc->IsConnected()))
3162            SetDBConnection();
3163          TrkParams::Set(GetRunInfo(), dbc);
3164          if (dbc)
3165            dbc->Close();
3166          //            cout << endl;
3167          prevshift = 0;
3168          return true;
3169        }
3170        return false;
3171      }
3172    
3173      return false;
3174      //
3175    }
3176    ;
3177    /**
3178     * Update the runinfo informations (to be used to have Run infos event by event basis)
3179     * @param run Pointer to the chain/tree which contains run infos
3180     * @return true if a new run has been read, false if it is still the same run
3181     */
3182    Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {
3183      return (UpdateRunInfo((TChain*) run, iev));
3184    }
3185    ;
3186    
3187    //--------------------------------------
3188    //
3189    //
3190    //--------------------------------------
3191    /**
3192     * Set which trees shoul be analysed
3193     * @param detlist TString containing the sequence of trees required
3194     */
3195    void PamLevel2::SetWhichTrees(TString detlist) {
3196    
3197      //    if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){
3198      if (detlist.Contains("+ALL", TString::kIgnoreCase)) {
3199    
3200        cout << " ======================================================== " << endl;
3201        cout << "                       (( WARNING ))                      " << endl;
3202        cout << " The meaning of the option +ALL has changed!!             " << endl;
3203        cout << " Now it includes really all (level0+level1+level2+gpamela)" << endl;
3204        cout << " and the file is discarded if it does not contain         " << endl;
3205        cout << " all trees or  if level0 files are not available!!        " << endl;
3206        cout << " ======================================================== " << endl;
3207    
3208        CAL0 = true;
3209        CAL1 = true;
3210        CAL2 = true;
3211        TRK2 = true;
3212        TRK1 = true;
3213        TRKh = true;
3214        TRK0 = true;
3215        TRG = true;
3216        TOF = true;
3217        TOF0 = true;
3218        S4 = true;
3219        ND = true;
3220        AC = true;
3221        ORB = true;
3222        GP = true;
3223      }
3224      else if (detlist.Contains("-ALL", TString::kIgnoreCase)) {
3225        CAL0 = false;
3226        CAL1 = false;
3227        CAL2 = false;
3228        TRK2 = false;
3229        TRK1 = false;
3230        TRKh = false;
3231        TRK0 = false;
3232        TRG = false;
3233        TOF = false;
3234        TOF0 = false;
3235        S4 = false;
3236        ND = false;
3237        AC = false;
3238        ORB = false;
3239        GP = false;
3240      };
3241    
3242      //  -------------------------------------------------------------------------
3243      if (detlist.Contains("CAL1", TString::kIgnoreCase)) {
3244        if (detlist.Contains("-CAL1", TString::kIgnoreCase))
3245          CAL1 = false;
3246        if (detlist.Contains("+CAL1", TString::kIgnoreCase))
3247          CAL1 = true;
3248      };
3249    
3250      if (detlist.Contains("CAL0", TString::kIgnoreCase)) {
3251        if (detlist.Contains("-CAL0", TString::kIgnoreCase))
3252          CAL0 = false;
3253        if (detlist.Contains("+CAL0", TString::kIgnoreCase))
3254          CAL0 = true;
3255      };
3256    
3257      if (detlist.Contains("CAL2", TString::kIgnoreCase)) {
3258        if (detlist.Contains("-CAL2", TString::kIgnoreCase))
3259          CAL2 = false;
3260        if (detlist.Contains("+CAL2", TString::kIgnoreCase))
3261          CAL2 = true;
3262      };
3263    
3264      if (detlist.Contains("+CAL", TString::kIgnoreCase) && !CAL1 && !CAL2)
3265        CAL2 = true;
3266      if (detlist.Contains("-CAL", TString::kIgnoreCase) && CAL1 && CAL2) {
3267        CAL2 = false;
3268        CAL1 = false;
3269      }
3270      //  -------------------------------------------------------------------------
3271      if (detlist.Contains("TRK0", TString::kIgnoreCase)) {
3272        if (detlist.Contains("-TRK0", TString::kIgnoreCase))
3273          TRK0 = false;
3274        if (detlist.Contains("+TRK0", TString::kIgnoreCase))
3275          TRK0 = true;
3276      };
3277    
3278      if (detlist.Contains("TRK1", TString::kIgnoreCase)) {
3279        if (detlist.Contains("-TRK1", TString::kIgnoreCase))
3280          TRK1 = false;
3281        if (detlist.Contains("+TRK1", TString::kIgnoreCase))
3282          TRK1 = true;
3283      };
3284    
3285      if (detlist.Contains("TRK2", TString::kIgnoreCase)) {
3286        if (detlist.Contains("-TRK2", TString::kIgnoreCase))
3287          TRK2 = false;
3288        if (detlist.Contains("+TRK2", TString::kIgnoreCase))
3289          TRK2 = true;
3290      };
3291    
3292      if (detlist.Contains("TRKh", TString::kIgnoreCase)) {
3293        if (detlist.Contains("-TRKh", TString::kIgnoreCase))
3294          TRKh = false;
3295        if (detlist.Contains("+TRKh", TString::kIgnoreCase))
3296          TRKh = true;
3297      };
3298    
3299      if (detlist.Contains("+TRK", TString::kIgnoreCase) && !TRK1 && !TRK2 && !TRKh)
3300        TRK2 = true;
3301      if (detlist.Contains("-TRK", TString::kIgnoreCase) && TRK1 && TRK2 && TRKh) {
3302        TRK2 = false;
3303        TRK1 = false;
3304        TRKh = false;
3305      }
3306      //  -------------------------------------------------------------------------
3307    
3308      if (detlist.Contains("-TRG", TString::kIgnoreCase))
3309        TRG = false;
3310      else if (detlist.Contains("+TRG", TString::kIgnoreCase))
3311        TRG = true;
3312    
3313      if (detlist.Contains("-TOF", TString::kIgnoreCase))
3314        TOF = false;
3315      else if (detlist.Contains("+TOF", TString::kIgnoreCase))
3316        TOF = true;
3317    
3318      if (detlist.Contains("-TOF0", TString::kIgnoreCase))
3319        TOF0 = false;
3320      else if (detlist.Contains("+TOF0", TString::kIgnoreCase))
3321        TOF0 = true;
3322    
3323      if (detlist.Contains("-S4", TString::kIgnoreCase))
3324        S4 = false;
3325      else if (detlist.Contains("+S4", TString::kIgnoreCase))
3326        S4 = true;
3327    
3328      if (detlist.Contains("-ND", TString::kIgnoreCase))
3329        ND = false;
3330      else if (detlist.Contains("+ND", TString::kIgnoreCase))
3331        ND = true;
3332    
3333      if (detlist.Contains("-AC", TString::kIgnoreCase))
3334        AC = false;
3335      else if (detlist.Contains("+AC", TString::kIgnoreCase))
3336        AC = true;
3337    
3338      if (detlist.Contains("-ORB", TString::kIgnoreCase))
3339        ORB = false;
3340      else if (detlist.Contains("+ORB", TString::kIgnoreCase))
3341        ORB = true;
3342    
3343      if (detlist.Contains("-GP", TString::kIgnoreCase))
3344        GP = false;
3345      else if (detlist.Contains("+GP", TString::kIgnoreCase))
3346        GP = true;
3347    
3348      cout << "tree/branch list from input --> ";
3349      if (TRK0)
3350        cout << "TRK0 ";
3351      if (TRK1)
3352        cout << "TRK1 ";
3353      if (TRK2)
3354        cout << "TRK2 ";
3355      if (TRKh)
3356        cout << "TRKH ";
3357      if (CAL0)
3358        cout << "CAL0 ";
3359      if (CAL1)
3360        cout << "CAL1 ";
3361      if (CAL2)
3362        cout << "CAL2 ";
3363      if (TOF)
3364        cout << "TOF ";
3365      if (TRG)
3366        cout << "TRG ";
3367      if (AC)
3368        cout << "AC ";
3369      if (ND)
3370        cout << "ND ";
3371      if (S4)
3372        cout << "S4 ";
3373      if (ORB)
3374        cout << "ORB ";
3375      if (GP)
3376        cout << "GP ";
3377      cout << endl;
3378      //     cout<< "Set detector list --> ";
3379      //     if(TRK1)cout<<"TRK1 ";
3380      //     if(TRK2)cout<<"TRK2 ";
3381      //     if(TRKh)cout<<"TRKH ";
3382      //     if(CAL1)cout<<"CAL1 ";
3383      //     if(CAL2)cout<<"CAL2 ";
3384      //     if(TOF0)cout<<"TOF0 ";
3385      //     if(TOF)cout<<"TOF ";
3386      //     if(TRG)cout<<"TRG ";
3387      //     if(AC)cout<<"AC ";
3388      //     if(ND)cout<<"ND ";
3389      //     if(S4)cout<<"S4 ";
3390      //     if(ORB)cout<<"ORB ";
3391      //     cout << endl;
3392    
3393    }
3394    ;
3395    
3396    /**
3397     * Set tree/branch detector flags from the content of a tree
3398     */
3399    void PamLevel2::GetWhichTrees(TFile* f) {
3400    
3401      //    cout << "void  PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;
3402      // -----------
3403      // reset flags
3404      // -----------
3405      CAL1 = false;
3406      CAL2 = false;
3407      TRK2 = false;
3408      TRK1 = false;
3409      TRKh = false;
3410      TRG = false;
3411      TOF = false;
3412      S4 = false;
3413      ND = false;
3414      AC = false;
3415      ORB = false;
3416      GP = false;
3417    
3418      RUN = false;
3419    
3420      //    cout << "Checking file: "<<f->GetName()<<endl;
3421      if (!f || f->IsZombie()) {
3422        cout << "File: " << f->GetName() << " Non valid root file" << endl;
3423        return;
3424      }
3425    
3426      TList *lk = f->GetListOfKeys();
3427      if (!lk)
3428        return;
3429      TIter next(lk);
3430      TKey *key = 0;
3431    
3432      Int_t nev = 0;
3433    
3434      while ((key = (TKey*) next())) {
3435    
3436        if (!strcmp(key->GetName(), "Run"))
3437          RUN = true;
3438    
3439        //=========================================================
3440        if (!strcmp(key->GetName(), "Trigger")) {
3441          TRG = true;
3442          Int_t nevt = ((TTree*) f->Get("Trigger"))->GetEntries();
3443          if (nev && nevt != nev) {
3444            cout << "File: " << f->GetName() << " Trigger tree has " << nevt << " events instead of " << nev << endl;
3445            TRG = false;
3446          }
3447          else
3448            nev = nevt;
3449        }
3450        //=========================================================
3451        if (!strcmp(key->GetName(), "ToF")) {
3452          TOF = true;
3453          Int_t nevt = ((TTree*) f->Get("ToF"))->GetEntries();
3454          if (nev && nevt != nev) {
3455            cout << "File: " << f->GetName() << "     ToF tree has " << nevt << " events instead of " << nev << endl;
3456            TOF = false;
3457          }
3458          else
3459            nev = nevt;
3460        }
3461        //=========================================================
3462        if (!strcmp(key->GetName(), "S4")) {
3463          S4 = true;
3464          Int_t nevt = ((TTree*) f->Get("S4"))->GetEntries();
3465          if (nev && nevt != nev) {
3466            cout << "File: " << f->GetName() << "      S4 tree has " << nevt << " events instead of " << nev << endl;
3467            S4 = false;
3468          }
3469          else
3470            nev = nevt;
3471        }
3472        //=========================================================
3473    
3474        if (!strcmp(key->GetName(), "NeutronD")) {
3475          ND = true;
3476          Int_t nevt = ((TTree*) f->Get("NeutronD"))->GetEntries();
3477          if (nev && nevt != nev) {
3478            cout << "File: " << f->GetName() << "NeutronD tree has " << nevt << " events instead of " << nev << endl;
3479            ND = false;
3480          }
3481          else
3482            nev = nevt;
3483        }
3484        //=========================================================
3485        if (!strcmp(key->GetName(), "Anticounter")) {
3486          AC = true;
3487          Int_t nevt = ((TTree*) f->Get("Anticounter"))->GetEntries();
3488          if (nev && nevt != nev) {
3489            cout << "File: " << f->GetName() << " Anticounter tree has " << nevt << " events instead of " << nev << endl;
3490            AC = false;
3491          }
3492          else
3493            nev = nevt;
3494        }
3495        //=========================================================
3496        if (!strcmp(key->GetName(), "OrbitalInfo")) {
3497          ORB = true;
3498          Int_t nevt = ((TTree*) f->Get("OrbitalInfo"))->GetEntries();
3499          if (nev && nevt != nev) {
3500            cout << "File: " << f->GetName() << " OrbitalInfo tree has " << nevt << " events instead of " << nev << endl;
3501            ORB = false;
3502          }
3503          else
3504            nev = nevt;
3505        }
3506        //=========================================================
3507        if (!strcmp(key->GetName(), "Tracker")) {
3508          TTree *T = (TTree*) f->Get("Tracker");
3509          for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
3510            TString name = T->GetListOfBranches()->At(i)->GetName();
3511            if (!name.CompareTo("TrkLevel1"))
3512              TRK1 = true;
3513            if (!name.CompareTo("TrkLevel2"))
3514              TRK2 = true;
3515            if (!name.CompareTo("TrkHough"))
3516              TRKh = true;
3517          };
3518          Int_t nevt = T->GetEntries();
3519          if (nev && nevt != nev) {
3520            cout << "File: " << f->GetName() << " Tracker tree has " << nevt << " events instead of " << nev << endl;
3521            TRK1 = false;
3522            TRK2 = false;
3523            TRKh = false;
3524          }
3525          else
3526            nev = nevt;
3527          //            T->Delete();
3528        };
3529        //=========================================================
3530        if (!strcmp(key->GetName(), "Calorimeter")) {
3531          TTree *T = (TTree*) f->Get("Calorimeter");
3532          for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
3533            TString name = T->GetListOfBranches()->At(i)->GetName();
3534            if (!name.CompareTo("CaloLevel1"))
3535              CAL1 = true;
3536            if (!name.CompareTo("CaloLevel2"))
3537              CAL2 = true;
3538          };
3539          Int_t nevt = T->GetEntries();
3540          if (nev && nevt != nev) {
3541            cout << "File: " << f->GetName() << "  Calorimeter tree has " << nevt << " events instead of " << nev << endl;
3542            CAL1 = false;
3543            CAL2 = false;
3544          }
3545          else
3546            nev = nevt;
3547          //            T->Delete();
3548        };
3549        //=========================================================
3550        if (!strcmp(key->GetName(), "h20")) {
3551          GP = true;
3552          Int_t nevt = ((TTree*) f->Get("h20"))->GetEntries();
3553          if (nev && nevt != nev) {
3554            cout << "File: " << f->GetName() << " h20 tree has " << nevt << " events instead of " << nev << endl;
3555            GP = false;
3556          }
3557          else
3558            nev = nevt;
3559        }
3560    
3561      };
3562    
3563      //    delete lk;
3564    
3565      cout << "tree/branch list from file  --> ";
3566      if (TRK1)
3567        cout << "TRK1 ";
3568      if (TRK2)
3569        cout << "TRK2 ";
3570      if (TRKh)
3571        cout << "TRKH ";
3572      if (CAL1)
3573        cout << "CAL1 ";
3574      if (CAL2)
3575        cout << "CAL2 ";
3576      if (TOF)
3577        cout << "TOF ";
3578      if (TRG)
3579        cout << "TRG ";
3580      if (AC)
3581        cout << "AC ";
3582      if (ND)
3583        cout << "ND ";
3584      if (S4)
3585        cout << "S4 ";
3586      if (ORB)
3587        cout << "ORB ";
3588      if (GP)
3589        cout << "GP ";
3590      cout << endl;
3591    
3592      return;
3593    
3594    }
3595    ;
3596    
3597    //--------------------------------------
3598    //
3599    //
3600    //--------------------------------------
3601    /**
3602     * Check if a file contains selected Pamela Level2 trees.
3603     * @param name File name
3604     * @return true if the file is ok.
3605     */
3606    Bool_t PamLevel2::CheckLevel2File(TString name) {
3607    
3608      Bool_t CAL1__ok = false;
3609      Bool_t CAL2__ok = false;
3610      Bool_t TRK2__ok = false;
3611      Bool_t TRK1__ok = false;
3612      Bool_t TRKh__ok = false;
3613      Bool_t TRG__ok = false;
3614      Bool_t TOF__ok = false;
3615      Bool_t S4__ok = false;
3616      Bool_t ND__ok = false;
3617      Bool_t AC__ok = false;
3618      Bool_t ORB__ok = false;
3619      Bool_t GP__ok = false;
3620    
3621      Bool_t RUN__ok = false;
3622    
3623      Bool_t SELLI__ok = false;
3624    
3625      //    cout << "Checking file: "<<name<<endl;
3626      TFile *f = new TFile(name.Data());
3627      if (!f || f->IsZombie()) {
3628        cout << "File: " << f->GetName() << " discarded ---- Non valid root file" << endl;
3629        return false;
3630      }
3631      //    cout << "Get list of keys: "<<f<<endl;
3632      TList *lk = f->GetListOfKeys();
3633      //    lk->Print();
3634      TIter next(lk);
3635      TKey *key = 0;
3636    
3637      Int_t nev = 0;
3638    
3639      while ((key = (TKey*) next())) {
3640    
3641        //  cout << key->GetName() << endl;
3642        //  cout << key->GetName() << ""<<key->GetClassName()<<endl;
3643        //          cout << " Get tree: " << f->Get(key->GetName())<<endl;
3644        //  nev_previous = nev;
3645        //  cout << " n.entries  "<< nev <<endl;
3646        //  if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){
3647        //      nev = ((TTree*)f->Get(key->GetName()))->GetEntries();
3648        //      cout << "File: "<< f->GetName() <<" discarded ---- "<< key->GetName() << " tree: n.entries does not match "<<nev<<" "<<nev_previous<< endl;
3649        //      return false;
3650        //  };
3651    
3652        //=========================================================
3653        // check if the file
3654    
3655    
3656        if (!strcmp(key->GetName(), "Run"))
3657          RUN__ok = true;
3658    
3659        //=========================================================
3660        if (!strcmp(key->GetName(), "SelectionList")) {
3661          SELLI__ok = true;
3662          if (SELLI == 1) {
3663            Int_t nevt = ((TTree*) f->Get("SelectionList"))->GetEntries();
3664            if (nev && nevt != nev) {
3665              cout << "File: " << f->GetName() << " discarded ---- SelectionList tree has " << nevt
3666                  << " events instead of " << nev << endl;
3667              return false;
3668            }
3669            nev = nevt;
3670          }
3671        }
3672    
3673        //=========================================================
3674        if (!strcmp(key->GetName(), "Trigger")) {
3675          TRG__ok = true;
3676          if (TRG) {
3677            Int_t nevt = ((TTree*) f->Get("Trigger"))->GetEntries();
3678            if (nev && nevt != nev) {
3679              cout << "File: " << f->GetName() << " discarded ---- Trigger tree has " << nevt << " events instead of "
3680                  << nev << endl;
3681              return false;
3682            }
3683            nev = nevt;
3684          }
3685        }
3686        //=========================================================
3687        if (!strcmp(key->GetName(), "ToF")) {
3688          TOF__ok = true;
3689          if (TOF) {
3690            Int_t nevt = ((TTree*) f->Get("ToF"))->GetEntries();
3691            if (nev && nevt != nev) {
3692              cout << "File: " << f->GetName() << " discarded ---- ToF tree has " << nevt << " events instead of " << nev
3693                  << endl;
3694              return false;
3695            }
3696            nev = nevt;
3697          }
3698        }
3699        //=========================================================
3700        if (!strcmp(key->GetName(), "S4")) {
3701          S4__ok = true;
3702          if (S4) {
3703            Int_t nevt = ((TTree*) f->Get("S4"))->GetEntries();
3704            if (nev && nevt != nev) {
3705              cout << "File: " << f->GetName() << " discarded ---- S4 tree has " << nevt << " events instead of " << nev
3706                  << endl;
3707              return false;
3708            }
3709            nev = nevt;
3710          }
3711        }
3712        //=========================================================
3713    
3714        if (!strcmp(key->GetName(), "NeutronD")) {
3715          ND__ok = true;
3716          if (ND) {
3717            Int_t nevt = ((TTree*) f->Get("NeutronD"))->GetEntries();
3718            if (nev && nevt != nev) {
3719              cout << "File: " << f->GetName() << " discarded ---- NeutronD tree has " << nevt << " events instead of "
3720                  << nev << endl;
3721              return false;
3722            }
3723            nev = nevt;
3724          }
3725        }
3726        //=========================================================
3727        if (!strcmp(key->GetName(), "Anticounter")) {
3728          AC__ok = true;
3729          if (AC) {
3730            Int_t nevt = ((TTree*) f->Get("Anticounter"))->GetEntries();
3731            if (nev && nevt != nev) {
3732              cout << "File: " << f->GetName() << " discarded ---- Anticounter tree has " << nevt << " events instead of "
3733                  << nev << endl;
3734              return false;
3735            }
3736            nev = nevt;
3737          }
3738        }
3739        //=========================================================
3740        if (!strcmp(key->GetName(), "OrbitalInfo")) {
3741          ORB__ok = true;
3742          if (ORB) {
3743            Int_t nevt = ((TTree*) f->Get("OrbitalInfo"))->GetEntries();
3744            if (nev && nevt != nev) {
3745              cout << "File: " << f->GetName() << " discarded ---- OrbitalInfo tree has " << nevt << " events instead of "
3746                  << nev << endl;
3747              return false;
3748            }
3749            nev = nevt;
3750          }
3751        }
3752        //=========================================================
3753        if (!strcmp(key->GetName(), "Tracker")) {
3754          TTree *T = (TTree*) f->Get("Tracker");
3755          if (TRK1 || TRK2 || TRKh) {
3756            Int_t nevt = T->GetEntries();
3757            if (nev && nevt != nev) {
3758              cout << "File: " << f->GetName() << " discarded ---- Tracker tree has " << nevt << " events instead of "
3759                  << nev << endl;
3760              return false;
3761            }
3762            nev = nevt;
3763          }
3764          for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
3765            TString name = T->GetListOfBranches()->At(i)->GetName();
3766            if (!name.CompareTo("TrkLevel1"))
3767              TRK1__ok = true;
3768            if (!name.CompareTo("TrkLevel2"))
3769              TRK2__ok = true;
3770            if (!name.CompareTo("TrkHough"))
3771              TRKh__ok = true;
3772          };
3773          T->Delete();
3774        };
3775        //=========================================================
3776        if (!strcmp(key->GetName(), "Calorimeter")) {
3777          TTree *T = (TTree*) f->Get("Calorimeter");
3778          if (CAL1 || CAL2) {
3779            Int_t nevt = T->GetEntries();
3780            if (nev && nevt != nev) {
3781              cout << "File: " << f->GetName() << " discarded ---- Calorimeter tree has " << nevt << " events instead of "
3782                  << nev << endl;
3783              return false;
3784            }
3785            nev = nevt;
3786          }
3787          for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
3788            TString name = T->GetListOfBranches()->At(i)->GetName();
3789            if (!name.CompareTo("CaloLevel1"))
3790              CAL1__ok = true;
3791            if (!name.CompareTo("CaloLevel2"))
3792              CAL2__ok = true;
3793          };
3794          T->Delete();
3795        };
3796        //=========================================================
3797        if (!strcmp(key->GetName(), "h20")) {
3798          ISGP = true;
3799          GP__ok = true;
3800          if (GP) {
3801            Int_t nevt = ((TTree*) f->Get("h20"))->GetEntries();
3802            if (nev && nevt != nev) {
3803              cout << "File: " << f->GetName() << " discarded ---- h20 tree has " << nevt << " events instead of " << nev
3804                  << endl;
3805              return false;
3806            }
3807            nev = nevt;
3808          }
3809        }
3810    
3811      };
3812    
3813      if (SELLI == -1)
3814        SELLI = (Int_t) SELLI__ok;
3815      if (SELLI == 0 && SELLI__ok) {
3816        cout << "File: " << f->GetName() << " discarded ---- found SelectionList (it is not a full-event file)" << endl;
3817        return false;
3818      }
3819      if (SELLI == 1 && !SELLI__ok) {
3820        cout << "File: " << f->GetName() << " discarded ---- SelectionList missing" << endl;
3821        return false;
3822      }
3823    
3824      //    cout << "SELLI "<<SELLI<<endl;
3825    
3826      //     cout<< "CheckLevel2File(TString): detector list --> ";
3827      //     if(TRK1__ok)cout<<"TRK1 ";
3828      //     if(TRK2__ok)cout<<"TRK2 ";
3829      //     if(TRKh__ok)cout<<"TRKH ";
3830      //     if(CAL1__ok)cout<<"CAL1 ";
3831      //     if(CAL2__ok)cout<<"CAL2 ";
3832      //     if(TOF__ok)cout<<"TOF ";
3833      //     if(TRG__ok)cout<<"TRG ";
3834      //     if(AC__ok)cout<<"AC ";
3835      //     if(ND__ok)cout<<"ND ";
3836      //     if(S4__ok)cout<<"S4 ";
3837      //     if(ORB__ok)cout<<"ORB ";
3838      //     cout << endl;
3839    
3840    
3841      if (TRK2 && TRK1__ok)
3842        TRK1 = 1;
3843      // ----------------------------------------------------------------------------
3844      // NOTA
3845      // se c'e` il level1, lo devo necessarimente leggere.
3846      // infatti (non ho capito perche`) i cluster vengono letti e allocati in memoria
3847      // comunque, ma non vengono disallocati da PamLevel2::Clear()
3848      // ----------------------------------------------------------------------------
3849    
3850    
3851      if (!RUN__ok) {
3852        cout << "File: " << f->GetName() << " *WARNING* ---- Missing RunInfo tree (NB: RUN infos will not be updated)"
3853            << endl;
3854        RUN = false;
3855      };
3856    
3857      if (CAL1 && !CAL1__ok) {
3858        cout << "File: " << f->GetName() << " discarded ---- Missing CaloLevel1 branch" << endl;
3859        return false;
3860      };
3861      if (CAL2 && !CAL2__ok) {
3862        cout << "File: " << f->GetName() << " discarded ---- Missing CaloLevel2 branch" << endl;
3863        return false;
3864      };
3865      if (TRK2 && !TRK2__ok) {
3866        cout << "File: " << f->GetName() << " discarded ---- Missing TrkLevel2 branch" << endl;
3867        return false;
3868      };
3869      if (TRK1 && !TRK1__ok) {
3870        cout << "File: " << f->GetName() << " discarded ---- Missing TrkLevel1 branch" << endl;
3871        return false;
3872      };
3873      if (TRKh && !TRKh__ok) {
3874        cout << "File: " << f->GetName() << " discarded ---- Missing TrkHough branch" << endl;
3875        return false;
3876      };
3877      if (ORB && !ORB__ok) {
3878        cout << "File: " << f->GetName() << " discarded ---- Missing ORB tree" << endl;
3879        return false;
3880      };
3881      if (AC && !AC__ok) {
3882        cout << "File: " << f->GetName() << " discarded ---- Missing AC tree" << endl;
3883        return false;
3884      };
3885      if (S4 && !S4__ok) {
3886        cout << "File: " << f->GetName() << " discarded ---- Missing S4 tree" << endl;
3887        return false;
3888      };
3889      if (TOF && !TOF__ok) {
3890        cout << "File: " << f->GetName() << " discarded ---- Missing ToF tree" << endl;
3891        return false;
3892      };
3893    
3894      if (ND && !ND__ok) {
3895        cout << "File: " << f->GetName() << " discarded ---- Missing ND tree" << endl;
3896        return false;
3897      };
3898      if (TRG && !TRG__ok) {
3899        cout << "File: " << f->GetName() << " discarded ---- Missing Trigger tree" << endl;
3900        return false;
3901      };
3902      if (GP && !GP__ok) {
3903        cout << "File: " << f->GetName() << " discarded ---- Missing h20 tree" << endl;
3904        return false;
3905      };
3906    
3907      //    lk->Delete();
3908      //    delete lk;
3909      f->Close();
3910    
3911      //     cout<< "CheckLevel2File(TString): detector list --> ";
3912      //     if(TRK1)cout<<"TRK1 ";
3913      //     if(TRK2)cout<<"TRK2 ";
3914      //     if(TRKh)cout<<"TRKH ";
3915      //     if(CAL1)cout<<"CAL1 ";
3916      //     if(CAL2)cout<<"CAL2 ";
3917      //     if(TOF)cout<<"TOF ";
3918      //     if(TRG)cout<<"TRG ";
3919      //     if(AC)cout<<"AC ";
3920      //     if(ND)cout<<"ND ";
3921      //     if(S4)cout<<"S4 ";
3922      //     if(ORB)cout<<"ORB ";
3923      //     if(GP)cout<<"GP ";
3924      //     cout << endl;
3925    
3926      return true;
3927    
3928    }
3929    ;
3930    
3931    /**
3932     * Create clone-trees
3933     */
3934    void PamLevel2::CreateCloneTrees0(TChain *fChain, TFile *ofile) {
3935    
3936      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
3937      cout << "Create clones of PAMELA trees " << endl;
3938    
3939      Int_t i = 0;
3940      pam_tree_clone[i] = fChain->GetTree()->CloneTree(0);
3941      TString name = pam_tree_clone[i]->GetName();
3942      name.Append("_clone");
3943      //    pam_tree_clone[i]->SetName(name.Data());
3944      cout << pam_tree_clone[i]->GetName() << endl;
3945      i++;
3946    
3947      TList *li = fChain->GetListOfFriends();
3948      TIter next(li);
3949      TFriendElement* T_friend = 0;
3950      ofile->cd();
3951      while ((T_friend = (TFriendElement*) next())) {
3952        //  cout<<T_friend->IsA()->GetName()<<" "<<T_friend->GetName()<<hex << T_friend->GetTree() << dec<<endl;
3953        //  cout<<T_friend->GetTree()->GetName()<< endl;
3954        pam_tree_clone[i] = T_friend->GetTree()->CloneTree(0);
3955        pam_tree_clone[i]->SetAutoSave(1000000);
3956        name = pam_tree_clone[i]->GetName();
3957        name.Append("_clone");
3958        //  pam_tree_clone[i]->SetName(name.Data());
3959        cout << pam_tree_clone[i]->GetName() << endl;
3960        i++;
3961      }
3962    
3963      delete li;
3964    
3965      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
3966    
3967    }
3968    
3969    /**
3970     * Create clone-trees
3971     */
3972    void PamLevel2::CreateCloneTrees(TFile *ofile) {
3973    
3974      //  if the pointer is null, create a default file
3975      if (!run_tree)
3976        return;
3977    
3978      if (!ofile) {
3979        cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root " << endl;
3980        ofile = new TFile("clone-tree.root", "recreate");
3981      }
3982    
3983      ofile->cd();
3984    
3985      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
3986      cout << "Create new PAMELA trees " << endl;
3987    
3988      run_tree_clone = new TTree("Run", "PAMELA Level2 data from the GL_RUN table ");
3989      run_tree_clone->Branch("RunInfo", "GL_RUN", GetPointerTo("RunInfo"));
3990      cout << "Run          : branch RunInfo" << endl;
3991      run_tree_clone->Branch("SoftInfo", "SoftInfo", GetPointerTo("SoftInfo"));
3992      cout << "Run          : branch SoftInfo" << endl;
3993      // ------------------
3994      // replicate run tree
3995      // ------------------
3996      //    cout << "----------------------------------------------------"<<endl;
3997      //    cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
3998      for (Int_t i = 0; i < run_tree->GetEntries(); i++) {
3999        run_tree->GetEntry(i);
4000        //  cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
4001        run_tree_clone->Fill();
4002      }
4003      cout << "----------------------------------------------------" << endl;
4004    
4005      // ------------------------------------
4006      // add branch with dead and live times
4007      // ------------------------------------
4008      if (SELLI != 2) { // EMILIANO
4009        run_tree_clone->Branch("DeadLiveTime", totdltime, "dltime[3]/l");
4010        cout << "Run          : branch DeadLiveTime" << endl;
4011    
4012        sel_tree_clone = new TTree("SelectionList", "List of selected events ");
4013        //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
4014        sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW
4015        sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");
4016      };
4017    
4018      Int_t i = 0;
4019      if (TRK1 || TRK2 || TRKh) {
4020        pam_tree_clone[i] = new TTree("Tracker", "PAMELA tracker level2 data ");
4021        if (TRK1) {
4022          pam_tree_clone[i]->Branch("TrkLevel1", "TrkLevel1", GetPointerTo("TrkLevel1"));
4023          pam_tree_clone[i]->BranchRef();
4024          cout << "Tracker      : branch TrkLevel1" << endl;
4025          //            cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;
4026        };
4027        if (TRK2) {
4028          pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2", GetPointerTo("TrkLevel2"));
4029          cout << "Tracker      : branch TrkLevel2" << endl;
4030        };
4031        if (TRKh) {
4032          pam_tree_clone[i]->Branch("TrkHough", "TrkHough", GetPointerTo("TrkHough"));
4033          cout << "Tracker      : branch TrkHough" << endl;
4034        };
4035        i++;
4036      }
4037    
4038      // Calorimeter
4039      if (CAL1 || CAL2) {
4040        pam_tree_clone[i] = new TTree("Calorimeter", "PAMELA calorimeter level2 data ");
4041        if (CAL1) {
4042          pam_tree_clone[i]->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));
4043          cout << "Calorimeter  : branch CaloLevel1" << endl;
4044        };
4045        if (CAL2) {
4046          pam_tree_clone[i]->Branch("CaloLevel2", "CaloLevel2", GetPointerTo("CaloLevel2"));
4047          cout << "Calorimeter  : branch CaloLevel2" << endl;
4048        };
4049        i++;
4050      }
4051    
4052      // ToF
4053      if (TOF) {
4054        pam_tree_clone[i] = new TTree("ToF", "PAMELA ToF level2 data ");
4055        pam_tree_clone[i]->Branch("ToFLevel2", "ToFLevel2", GetPointerTo("ToFLevel2"));
4056        cout << "ToF          : branch ToFLevel2" << endl;
4057        i++;
4058      };
4059      // Trigger
4060      if (TRG) {
4061        pam_tree_clone[i] = new TTree("Trigger", "PAMELA trigger level2 data ");
4062        pam_tree_clone[i]->Branch("TrigLevel2", "TrigLevel2", GetPointerTo("TrigLevel2"));
4063        cout << "Trigger      : branch TrigLevel2" << endl;
4064        i++;
4065      };
4066      // S4
4067      if (S4) {
4068        pam_tree_clone[i] = new TTree("S4", "PAMELA S4 level2 data ");
4069        pam_tree_clone[i]->Branch("S4Level2", "S4Level2", GetPointerTo("S4Level2"));
4070        cout << "S4           : branch S4Level2" << endl;
4071        i++;
4072      };
4073      // Neutron Detector
4074      if (ND) {
4075        pam_tree_clone[i] = new TTree("NeutronD", "PAMELA neutron detector level2 data ");
4076        pam_tree_clone[i]->Branch("NDLevel2", "NDLevel2", GetPointerTo("NDLevel2"));
4077        cout << "NeutronD     : branch NDLevel2" << endl;
4078        i++;
4079      };
4080      // Anticounters
4081      if (AC) {
4082        pam_tree_clone[i] = new TTree("Anticounter", "PAMELA anticounter detector level2 data ");
4083        pam_tree_clone[i]->Branch("AcLevel2", "AcLevel2", GetPointerTo("AcLevel2"));
4084        cout << "Anticounter  : branch AcLevel2" << endl;
4085        i++;
4086      };
4087      // OrbitalInfo
4088      if (ORB) {
4089        pam_tree_clone[i] = new TTree("OrbitalInfo", "PAMELA orbital info  ");
4090        pam_tree_clone[i]->Branch("OrbitalInfo", "OrbitalInfo", GetPointerTo("OrbitalInfo"));
4091        cout << "OrbitalInfo  : branch OrbitalInfo" << endl;
4092        i++;
4093      };
4094      // GPamela
4095      if (GP) {
4096        pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");
4097        pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split
4098        cout << "OrbitalInfo  : branch OrbitalInfo" << endl;
4099        i++;
4100      };
4101      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
4102    
4103    }
4104    
4105    /**
4106     * Fill tree (created with CreateCloneTrees)
4107     *
4108     */
4109    //void PamLevel2::FillNewPamTree(TTree *T){
4110    void PamLevel2::FillCloneTrees() {
4111    
4112      //    cout << "PamLevel2::FillCloneTrees()" << irunentry << endl;
4113    
4114      for (Int_t i = 0; i < NCLONES; i++) {
4115        if (pam_tree_clone[i])
4116          pam_tree_clone[i]->Fill();
4117      }
4118      if (sel_tree_clone)
4119        sel_tree_clone->Fill();
4120    
4121    }
4122    
4123    TTree* PamLevel2::GetCloneTree(TString name) {
4124    
4125      for (Int_t i = 0; i < NCLONES; i++) {
4126        if (pam_tree_clone[i]) {
4127          TString na = pam_tree_clone[i]->GetName();
4128          if (!name.CompareTo(na))
4129            return pam_tree_clone[i];
4130        };
4131      }
4132      if (run_tree_clone) {
4133        TString na = run_tree_clone->GetName();
4134        if (!name.CompareTo(na))
4135          return run_tree_clone;
4136      }
4137      if (sel_tree_clone) {
4138        TString na = sel_tree_clone->GetName();
4139        if (!name.CompareTo(na))
4140          return sel_tree_clone;
4141      }
4142      return NULL;
4143    
4144    }
4145    void PamLevel2::WriteCloneTrees() {
4146      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
4147      cout << "Write clones of PAMELA trees " << endl;
4148      cout << run_tree_clone->GetName() << endl;
4149      if (SELLI != 2) {// Emiliano
4150        if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
4151          run_tree_clone->GetBranch("DeadLiveTime")->Fill();
4152      };
4153      run_tree_clone->Write();
4154      if (SELLI != 2) { //Emiliano
4155        cout << sel_tree_clone->GetName() << endl;
4156        sel_tree_clone->Write();
4157      };
4158      for (Int_t i = 0; i < NCLONES; i++) {
4159        if (pam_tree_clone[i]) {
4160          cout << pam_tree_clone[i]->GetName() << endl;
4161          pam_tree_clone[i]->Write();
4162        };
4163      }
4164      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
4165    
4166    }
4167    
4168    /**
4169     * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.
4170     */
4171    //Int_t PamLevel2::GetEntry(Int_t iee){
4172    Int_t PamLevel2::GetEntry(Long64_t iee) {
4173    
4174      //     cout << "-------------------------------------"<<endl;
4175      //     cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;
4176    
4177      if (!pam_tree) {
4178        cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded" << endl;
4179        return 0;
4180      }
4181    
4182      //
4183      // This is a sort of bug: if you don't have the run tree you don't want to exit here you want to have loaded the event anyway...
4184      //
4185      //    if(!run_tree ){
4186      //    cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loeaded"<<endl;
4187      //    return 0;
4188      //    }
4189    
4190      Long64_t ii = 0;
4191      //-------------------------------
4192      ii = iee;
4193      if (!pam_tree->GetEntry(ii)) {
4194        cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree" << endl;
4195        return 0;
4196      }
4197      //
4198      // ... that's way I put it here. Notice that nothing change in the code (is backward compatible) since in any case you return with 0.
4199      // in theory one would like to return 1 if run is not loaded but now I don't have the will to add that 2 lines of code and it is not
4200      // a problem if you don't check the return code of getentry.
4201      //
4202      if (!RUN || !run_tree) {
4203        if (TRK0 || CAL0 || TOF0 || RUN) { //forse cosi` va bene per tornare 1?
4204          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded" << endl;
4205          return 0;
4206        }
4207        else {
4208          return 1; //cosi` se non c'e` run esce qua...
4209        }
4210      }
4211    
4212      //-------------------------------
4213      ii = iee;
4214      //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);
4215      //    Bool_t UPDATED = UpdateRunInfo(ii);
4216      UpdateRunInfo(ii);
4217      if (SELLI == 0 || SELLI == 2)
4218        irunentry = iee - runfirstentry;
4219      //    if(UPDATED && run_tree_clone)run_tree_clone->Fill();
4220    
4221      //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;
4222    
4223      //     cout << "irunentry     "<<irunentry << endl;
4224      //     cout << "runfirstentry "<<runfirstentry << endl;
4225      //     cout << "nevents       "<<GetRunInfo()->NEVENTS<< endl;
4226    
4227      //     if( TRK0 || CAL0 || TOF0 ){
4228      //    if( !GetYodaEntry( ) ){
4229      //        cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading level0 tree"<<endl;
4230      //        return 0;
4231      //    }
4232      //     }
4233    
4234    
4235      return 1;
4236    
4237    }
4238    
4239    TrkLevel0 *PamLevel2::GetTrkLevel0() {
4240      if (!TRK0)
4241        return NULL;
4242      if (!GetYodaEntry()) {
4243        cout << " Int_t PamLevel2::GetTrkLevel0() -- ERROR -- error reading level0 tree" << endl;
4244        return 0;
4245      }
4246      return trk0_obj;
4247    }
4248    ;
4249    CaloLevel0 *PamLevel2::GetCaloLevel0() {
4250      if (!CAL0)
4251        return NULL;
4252      if (!GetYodaEntry()) {
4253        cout << " Int_t PamLevel2::GetCaloLevel0() -- ERROR -- error reading level0 tree" << endl;
4254        return 0;
4255      }
4256      return calo0_obj;
4257    }
4258    ;
4259    
4260    /**
4261     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
4262     * Given the run ID (...), if needed it query the DB and load the proper file.
4263     * @return Pointer to the tree
4264     */
4265    
4266    TTree* PamLevel2::GetYodaTree() {
4267    
4268      //    cout << "TTree* PamLevel2::GetYodaTree( )"<<endl;
4269      //===================================
4270      // check if iroot has changed
4271      //===================================
4272      if (irun < 0) {
4273        cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;
4274        //  cout << "In order to use this method you have to first load the RunInfo tree "<<endl;
4275        return NULL;
4276      }
4277      Int_t irootnew = run_obj->ID_ROOT_L0;
4278      //      cout << "iroot    "<<iroot<<endl;
4279      //      cout << "irootnew "<<irootnew<<endl;
4280    
4281      //===================================
4282      // load the level0 file
4283      // (if not already loaded)
4284      //===================================
4285      if (iroot != irootnew || !l0_tree) {
4286        iroot = irootnew;
4287        //===================================
4288        // open the DB connection
4289        // (if not already opened)
4290        //===================================
4291        if (!dbc || (dbc && !dbc->IsConnected()))
4292          SetDBConnection();
4293        GL_ROOT glroot = GL_ROOT();
4294        if (glroot.Query_GL_ROOT(iroot, dbc)) {
4295          cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = " << iroot << " does not exists"
4296              << endl;
4297          return NULL;
4298        };
4299        TString filename = glroot.PATH + glroot.NAME;
4300        if (l0_file) {
4301          l0_file->Close();
4302          l0_file->Delete();
4303        }
4304        cout << "Opening LEVEL0 file: " << filename << endl;
4305        FileStat_t t;
4306        if (gSystem->GetPathInfo(filename.Data(), t)) {
4307          cout << " PamLevel2::GetYodaTree() -- ERROR opening file " << endl;
4308          return NULL;
4309        }
4310        l0_file = new TFile(filename);
4311        if (!l0_file)
4312          return NULL;
4313        l0_tree = (TTree*) l0_file->Get("Physics");
4314        if (!h0_obj)
4315          h0_obj = new EventHeader();
4316        l0_tree->SetBranchAddress("Header", &h0_obj);
4317        prevshift = 0;
4318        //---------------------------------------------------
4319        // TRACKER:
4320        if (TRK0) {
4321          if (!trk0_obj) {
4322            trk0_obj = new TrkLevel0();
4323            trk0_obj->Set();
4324          };
4325          l0_tree->SetBranchAddress("Tracker", trk0_obj->GetPointerToTrackerEvent());
4326        }
4327        //--------------------------------------------------
4328        // CALORIMETER:
4329        if (CAL0) {
4330          if (!calo0_obj) {
4331            calo0_obj = new CaloLevel0();
4332            calo0_obj->Set();
4333          };
4334          l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());
4335          //            cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;
4336        }
4337        //---------------------------------------------------
4338        // TOF:
4339        if (TOF0) {
4340          cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented " << endl;
4341        }
4342    
4343        dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
4344    
4345    
4346      };
4347    
4348      //     if(!dbc || (dbc && !dbc->IsConnected())){
4349      //    cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;
4350      //     }
4351    
4352      if (TRK0) {
4353        //  TrkParams::Load(6);
4354        //  if( !TrkParams::IsLoaded(6) ){
4355        //      cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- VK-mask not loaded"<<endl;
4356        //  };
4357        //  if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection();
4358        TrkParams::SetCalib(run_obj, dbc);
4359        TrkParams::LoadCalib();
4360        if (!TrkParams::CalibIsLoaded()) {
4361          cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded" << endl;
4362        };
4363        //  if(dbc)dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
4364      }
4365    
4366      //    cout << l0_tree << endl;
4367      return l0_tree;
4368    
4369    }
4370    
4371    /**
4372     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
4373     */
4374    Int_t PamLevel2::GetYodaEntry() {
4375    
4376      //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;
4377      if (!GetYodaTree())
4378        return 0;
4379    
4380      // patch
4381      if (irunentry < 0) {
4382        //  cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
4383        irunentry = 0LL;
4384      }
4385      //  ---------------------------------
4386      //  if file is NOT a preselected file
4387      //  ---------------------------------
4388      Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM);
4389      //     cout << " irunentry "<<irunentry<<endl;
4390      //     cout << " EV_FROM "<<run_obj->EV_FROM<<endl;
4391      //     cout << " quellagiusta = irunentry + EV_FROM "<< quellagiusta << endl;
4392    
4393      //     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
4394      //     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
4395      //     cout << " time "<< abstime << endl;
4396      //     cout << " trk_calib_used "<<run_obj->TRK_CALIB_USED<< endl;
4397    
4398      ULong64_t obt = 0;
4399      ULong64_t pktn = 0;
4400      if (GetOrbitalInfo()) {
4401        obt = GetOrbitalInfo()->OBT;
4402        pktn = GetOrbitalInfo()->pkt_num;
4403      }
4404    
4405      if (!GetOrbitalInfo() && !ISGP) {
4406        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;
4407        return 0;
4408      }
4409      if (obt == 0 && pktn == 0 && !ISGP) {
4410        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;
4411        return 0;
4412      }
4413    
4414      // ---------------------------------------------------------------------
4415      // ATTENTION!!!
4416      // If data are not pre-processed with cleaner, the level0 tree may contain
4417      // spurious nested physics packets.
4418      // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
4419      // while level2 tree DOES NOT!!
4420      // This means that "quellagiusta" in these cases is not correct.
4421      // In order to retrieve the correct level0 event, I implemented a check
4422      // of the OBT and pkt-number. In case of mismatch, the level0 entry number
4423      // is shift forward until when the packets match.
4424      // ---------------------------------------------------------------------
4425      Int_t answer = 0;
4426      Int_t shift = 0;
4427      //    printf(" siamo qui %i %i \n",shift,prevshift);
4428      //    Int_t maxshift = 50; // EMILIANO
4429      do {
4430        if (shift > 0) {
4431          cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
4432          if (DBG)
4433            cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
4434                << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
4435          if (DBG)
4436            cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
4437                << " prevshift " << prevshift << " )" << endl;
4438        }
4439        answer = l0_tree->GetEntry(quellagiusta + (Long64_t) shift + (Long64_t) prevshift);
4440        shift++;
4441        if (!GetEventHeader()) {
4442          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
4443          return 0;
4444        }
4445    
4446        if (ISGP) {
4447          obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
4448          pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
4449        }
4450    
4451        //  cout << "PKTNUM "<<shift<<" ==  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
4452        //  cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;
4453        //  if( (quellagiusta+shift) == l0_tree->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;
4454        //  cout << " obt "<< obt << endl;
4455        //  cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;
4456        //  cout << " pktn "<< pktn << endl;
4457        //  cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
4458        //  printf(" IDRUN %u \n",GetRunInfo()->ID);
4459        //
4460        if (prevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
4461          prevshift = 0;
4462          shift = -1;
4463        };
4464    
4465      } while ((obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(
4466          GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift)
4467          < GetYodaTree()->GetEntries() && shift < maxshift);
4468    
4469      if ((quellagiusta + (Long64_t) shift + (Long64_t) prevshift) > GetYodaTree()->GetEntries() || shift == maxshift) {
4470        cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;
4471        return 0;
4472      }
4473      //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
4474      //    return GetYodaTree()->GetEntry(quellagiusta);
4475      if (shift > 1)
4476        prevshift += (shift - 1);
4477    
4478      return answer;
4479    
4480    }
4481    /**
4482     * \Brief Set DB connection
4483     */
4484    Bool_t PamLevel2::SetDBConnection() {
4485    
4486      //    cout << "PamLevel2::SetDBConnection()" << endl;
4487      if (DBG) {
4488        cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
4489        cout << "Connecting to DB" << endl;
4490        cout << "HOST " << host << endl;
4491        cout << "USER " << user << endl;
4492        cout << "PSW  " << psw << endl;
4493      }
4494      dbc = TSQLServer::Connect(host.Data(), user.Data(), psw.Data());
4495      if (!dbc)
4496        return false;
4497      if (!dbc->IsConnected())
4498        return false;
4499      stringstream myquery; // EMILIANO
4500      myquery.str(""); // EMILIANO
4501      myquery << "SET time_zone='+0:00'"; // EMILIANO
4502      dbc->Query(myquery.str().c_str()); // EMILIANO
4503      if (DBG)
4504        cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
4505      return true;
4506    
4507    }
4508    
4509    /**
4510     * \Brief Add a friend to the pamela chain.
4511     * @param cname name of the chain to be added
4512     */
4513    
4514    TChain* PamLevel2::AddFriend(TString cname) {
4515    
4516      if (!GetPamTree()) {
4517        cout << " TChain* PamLevel2::AddFriend(TString cname) --- a pamela tree must be created first" << endl;
4518        return NULL;
4519      }
4520    
4521      TChain *c = new TChain(cname.Data());
4522    
4523      TIter next(GetPamTree()->GetListOfFiles());
4524      Int_t nf = 0;
4525      TChainElement* element = 0;
4526      while ((element = (TChainElement*) next())) {
4527        c->Add(element->GetTitle());
4528        nf++;
4529      }
4530    
4531      GetPamTree()->AddFriend(cname.Data());
4532    
4533      cout << "external chain created and added to pamela friends :" << cname << endl;
4534      cout << "n.files " << nf << endl;
4535    
4536      return c;
4537    
4538    }
4539    
4540    /**
4541     * Returns the current read entry. This method simply returns the result of the call to
4542     * pam_tree->GetReadEntry(), so it is entirely handled by ROOT.
4543     */
4544    Long64_t PamLevel2::GetReadEntry() {
4545      return pam_tree->GetReadEntry();
4546    }
4547    
4548    /**
4549     * Sets the sorting method. If the new method is different from the previous, the issorted
4550     * flag is set to false, forcing a new sort the next time GetTrack is called.
4551     * @see GetTrack
4552     */
4553    void PamLevel2::SetSortingMethod(TString how) {
4554      if (howtosort != how) {
4555        issorted = false;
4556      }
4557      howtosort = how;
4558    }

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

  ViewVC Help
Powered by ViewVC 1.1.23