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

Legend:
Removed from v.1.24  
changed lines
  Added in v.1.85

  ViewVC Help
Powered by ViewVC 1.1.23