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

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

  ViewVC Help
Powered by ViewVC 1.1.23