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

Legend:
Removed from v.1.44  
changed lines
  Added in v.1.88

  ViewVC Help
Powered by ViewVC 1.1.23