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

Legend:
Removed from v.1.39  
changed lines
  Added in v.1.82

  ViewVC Help
Powered by ViewVC 1.1.23