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

Legend:
Removed from v.1.42  
changed lines
  Added in v.1.87

  ViewVC Help
Powered by ViewVC 1.1.23