/[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.23 by mocchiut, Thu Mar 8 15:25:27 2007 UTC revision 1.70 by mocchiut, Wed Dec 10 11:56:35 2008 UTC
# Line 1  Line 1 
1  #include <PamLevel2.h>  #include <PamLevel2.h>
2    
3    
4    
5    /////////////////////////////////////////////////////////////////////////////
6    /////////////////////////////////////////////////////////////////////////////
7    /////////////////////////////////////////////////////////////////////////////
8    /////////////////////////////////////////////////////////////////////////////
9    
10    void GPamela::Clear(){
11        Irun   = 0;
12        Ievnt  = 0;
13        Ipa    = 0;
14        X0     = 0.;
15        Y0     = 0.;
16        Z0     = 0.;
17        Theta  = 0.;
18        Phi    = 0.;
19        P0     = 0.;
20        Nthtof  = 0;
21        Nthcat  = 0;
22        Nthcas  = 0;
23        Nthspe  = 0;
24        Nstrpx  = 0;
25        Nstrpy  = 0;
26        Nthcali = 0;
27        Nthcal  = 0;
28        Nthnd   = 0;
29        Nthcard = 0;
30    }
31    
32    void GPamela::Delete(){
33    
34        Clear();
35    
36        delete []    Ipltof;  
37        delete []    Ipaddle;
38        delete []    Ipartof;
39        delete []    Xintof;  
40        delete []    Yintof;  
41        delete []    Zintof;  
42        delete []    Xouttof;
43        delete []    Youttof;
44        delete []    Zouttof;
45        delete []    Ereltof;
46        delete []    Timetof;
47        delete []    Pathtof;
48        delete []    P0tof;  
49        delete []    Iparcat;
50        delete []    Icat;  
51        delete []    Xincat;  
52        delete []    Yincat;  
53        delete []    Zincat;  
54        delete []    Xoutcat;
55        delete []    Youtcat;
56        delete []    Zoutcat;
57        delete []    Erelcat;
58        delete []    Timecat;
59        delete []    Pathcat;
60        delete []    P0cat;  
61        delete []    Iparcas;
62        delete []    Icas;  
63        delete []    Xincas;  
64        delete []    Yincas;  
65        delete []    Zincas;  
66        delete []    Xoutcas;
67        delete []    Youtcas;
68        delete []    Zoutcas;
69        delete []    Erelcas;
70        delete []    Timecas;
71        delete []    Pathcas;
72        delete []    P0cas;  
73        delete []    Iparspe;
74        delete []    Itrpb;  
75        delete []    Itrsl;  
76        delete []    Itspa;  
77        delete []    Xinspe;  
78        delete []    Yinspe;  
79        delete []    Zinspe;  
80        delete []    Xoutspe;
81        delete []    Youtspe;
82        delete []    Zoutspe;
83        delete []    Xavspe;  
84        delete []    Yavspe;  
85        delete []    Zavspe;  
86        delete []    Erelspe;
87        delete []    Pathspe;
88        delete []    P0spe;  
89        delete []    Nxmult;  
90        delete []    Nymult;  
91        delete []    Npstripx;
92        delete []    Ntstripx;
93        delete []    Istripx;
94        delete []    Qstripx;
95        delete []    Xstripx;
96        delete []    Npstripy;
97        delete []    Ntstripy;
98        delete []    Istripy;
99        delete []    Qstripy;
100        delete []    Ystripy;
101        delete []    Icaplane;
102        delete []    Icastrip;
103        delete []    Icamod;  
104        delete []    Enestrip;
105        delete []    Icapl;  
106        delete []    Icasi;  
107        delete []    Icast;  
108        delete []    Xincal;  
109        delete []    Yincal;  
110        delete []    Zincal;  
111        delete []    Erelcal;
112        delete []    Itubend;
113        delete []    Iparnd;  
114        delete []    Xinnd;  
115        delete []    Yinnd;  
116        delete []    Zinnd;  
117        delete []    Xoutnd;  
118        delete []    Youtnd;  
119        delete []    Zoutnd;  
120        delete []    Erelnd;  
121        delete []    Timend;  
122        delete []    Pathnd;  
123        delete []    P0nd;  
124        delete []    Iparcard;
125        delete []    Icard;  
126        delete []    Xincard;
127        delete []    Yincard;
128        delete []    Zincard;
129        delete []    Xoutcard;
130        delete []    Youtcard;
131        delete []    Zoutcard;
132        delete []    Erelcard;
133        delete []    Timecard;
134        delete []    Pathcard;
135        delete []    P0card;  
136    
137    
138    };
139    
140    
141    
142    void GPamela::SetBranchAddress( TChain* fhBookTree ){
143    
144    //    cout << "fhBookTree "<<fhBookTree << endl;
145        // prepare tree
146        fhBookTree->SetBranchAddress("Irun",&Irun);
147        fhBookTree->SetBranchAddress("Ievnt",&Ievnt);
148        fhBookTree->SetBranchAddress("Ipa",&Ipa);
149        fhBookTree->SetBranchAddress("X0",&X0);
150        fhBookTree->SetBranchAddress("Y0",&Y0);
151        fhBookTree->SetBranchAddress("Z0",&Z0);
152        fhBookTree->SetBranchAddress("Theta",&Theta);
153        fhBookTree->SetBranchAddress("Phi",&Phi);
154        fhBookTree->SetBranchAddress("P0",&P0);
155        fhBookTree->SetBranchAddress("Nthtof",&Nthtof);
156        fhBookTree->SetBranchAddress("Ipltof",Ipltof);
157        fhBookTree->SetBranchAddress("Ipaddle",Ipaddle);
158        fhBookTree->SetBranchAddress("Ipartof",Ipartof);
159        fhBookTree->SetBranchAddress("Xintof",Xintof);
160        fhBookTree->SetBranchAddress("Yintof",Yintof);
161        fhBookTree->SetBranchAddress("Zintof",Zintof);
162        fhBookTree->SetBranchAddress("Xouttof",Xouttof);
163        fhBookTree->SetBranchAddress("Youttof",Youttof);
164        fhBookTree->SetBranchAddress("Zouttof",Zouttof);
165        fhBookTree->SetBranchAddress("Ereltof",Ereltof);
166        fhBookTree->SetBranchAddress("Timetof",Timetof);
167        fhBookTree->SetBranchAddress("Pathtof",Pathtof);
168        fhBookTree->SetBranchAddress("P0tof",P0tof);
169        fhBookTree->SetBranchAddress("Nthcat",&Nthcat);
170        fhBookTree->SetBranchAddress("Iparcat",Iparcat);
171        fhBookTree->SetBranchAddress("Icat",Icat);
172        fhBookTree->SetBranchAddress("Xincat",Xincat);
173        fhBookTree->SetBranchAddress("Yincat",Yincat);
174        fhBookTree->SetBranchAddress("Zincat",Zincat);
175        fhBookTree->SetBranchAddress("Xoutcat",Xoutcat);
176        fhBookTree->SetBranchAddress("Youtcat",Youtcat);
177        fhBookTree->SetBranchAddress("Zoutcat",Zoutcat);
178        fhBookTree->SetBranchAddress("Erelcat",Erelcat);
179        fhBookTree->SetBranchAddress("Timecat",Timecat);
180        fhBookTree->SetBranchAddress("Pathcat",Pathcat);
181        fhBookTree->SetBranchAddress("P0cat",P0cat);
182        fhBookTree->SetBranchAddress("Nthcas",&Nthcas);
183        fhBookTree->SetBranchAddress("Iparcas",Iparcas);
184        fhBookTree->SetBranchAddress("Icas",Icas);
185        fhBookTree->SetBranchAddress("Xincas",Xincas);
186        fhBookTree->SetBranchAddress("Yincas",Yincas);
187        fhBookTree->SetBranchAddress("Zincas",Zincas);
188        fhBookTree->SetBranchAddress("Xoutcas",Xoutcas);
189        fhBookTree->SetBranchAddress("Youtcas",Youtcas);
190        fhBookTree->SetBranchAddress("Zoutcas",Zoutcas);
191        fhBookTree->SetBranchAddress("Erelcas",Erelcas);
192        fhBookTree->SetBranchAddress("Timecas",Timecas);
193        fhBookTree->SetBranchAddress("Pathcas",Pathcas);
194        fhBookTree->SetBranchAddress("P0cas",P0cas);
195        fhBookTree->SetBranchAddress("Nthspe",&Nthspe);
196        fhBookTree->SetBranchAddress("Iparspe",Iparspe);
197        fhBookTree->SetBranchAddress("Itrpb",Itrpb);
198        fhBookTree->SetBranchAddress("Itrsl",Itrsl);
199        fhBookTree->SetBranchAddress("Itspa",Itspa);
200        fhBookTree->SetBranchAddress("Xinspe",Xinspe);
201        fhBookTree->SetBranchAddress("Yinspe",Yinspe);
202        fhBookTree->SetBranchAddress("Zinspe",Zinspe);
203        fhBookTree->SetBranchAddress("Xoutspe",Xoutspe);
204        fhBookTree->SetBranchAddress("Youtspe",Youtspe);
205        fhBookTree->SetBranchAddress("Zoutspe",Zoutspe);
206        fhBookTree->SetBranchAddress("Xavspe",Xavspe);
207        fhBookTree->SetBranchAddress("Yavspe",Yavspe);
208        fhBookTree->SetBranchAddress("Zavspe",Zavspe);
209        fhBookTree->SetBranchAddress("Erelspe",Erelspe);
210        fhBookTree->SetBranchAddress("Pathspe",Pathspe);
211        fhBookTree->SetBranchAddress("P0spe",P0spe);
212        fhBookTree->SetBranchAddress("Nxmult",Nxmult);
213        fhBookTree->SetBranchAddress("Nymult",Nymult);
214        fhBookTree->SetBranchAddress("Nstrpx",&Nstrpx);
215        fhBookTree->SetBranchAddress("Npstripx",Npstripx);
216        fhBookTree->SetBranchAddress("Ntstripx",Ntstripx);
217        fhBookTree->SetBranchAddress("Istripx",Istripx);
218        fhBookTree->SetBranchAddress("Qstripx",Qstripx);
219        fhBookTree->SetBranchAddress("Xstripx",Xstripx);
220        fhBookTree->SetBranchAddress("Nstrpy",&Nstrpy);
221        fhBookTree->SetBranchAddress("Npstripy",Npstripy);
222        fhBookTree->SetBranchAddress("Ntstripy",Ntstripy);
223        fhBookTree->SetBranchAddress("Istripy",Istripy);
224        fhBookTree->SetBranchAddress("Qstripy",Qstripy);
225        fhBookTree->SetBranchAddress("Ystripy",Ystripy);
226        fhBookTree->SetBranchAddress("Nthcali",&Nthcali);
227        fhBookTree->SetBranchAddress("Icaplane",Icaplane);
228        fhBookTree->SetBranchAddress("Icastrip",Icastrip);
229        fhBookTree->SetBranchAddress("Icamod",Icamod);
230        fhBookTree->SetBranchAddress("Enestrip",Enestrip);
231        fhBookTree->SetBranchAddress("Nthcal",&Nthcal);
232        fhBookTree->SetBranchAddress("Icapl",Icapl);
233        fhBookTree->SetBranchAddress("Icasi",Icasi);
234        fhBookTree->SetBranchAddress("Icast",Icast);
235        fhBookTree->SetBranchAddress("Xincal",Xincal);
236        fhBookTree->SetBranchAddress("Yincal",Yincal);
237        fhBookTree->SetBranchAddress("Zincal",Zincal);
238        fhBookTree->SetBranchAddress("Erelcal",Erelcal);
239        fhBookTree->SetBranchAddress("Nthnd",&Nthnd);
240        fhBookTree->SetBranchAddress("Itubend",Itubend);
241        fhBookTree->SetBranchAddress("Iparnd",Iparnd);
242        fhBookTree->SetBranchAddress("Xinnd",Xinnd);
243        fhBookTree->SetBranchAddress("Yinnd",Yinnd);
244        fhBookTree->SetBranchAddress("Zinnd",Zinnd);
245        fhBookTree->SetBranchAddress("Xoutnd",Xoutnd);
246        fhBookTree->SetBranchAddress("Youtnd",Youtnd);
247        fhBookTree->SetBranchAddress("Zoutnd",Zoutnd);
248        fhBookTree->SetBranchAddress("Erelnd",Erelnd);
249        fhBookTree->SetBranchAddress("Timend",Timend);
250        fhBookTree->SetBranchAddress("Pathnd",Pathnd);
251        fhBookTree->SetBranchAddress("P0nd",P0nd);
252        fhBookTree->SetBranchAddress("Nthcard",&Nthcard);
253        fhBookTree->SetBranchAddress("Iparcard",Iparcard);
254        fhBookTree->SetBranchAddress("Icard",Icard);
255        fhBookTree->SetBranchAddress("Xincard",Xincard);
256        fhBookTree->SetBranchAddress("Yincard",Yincard);
257        fhBookTree->SetBranchAddress("Zincard",Zincard);
258        fhBookTree->SetBranchAddress("Xoutcard",Xoutcard);
259        fhBookTree->SetBranchAddress("Youtcard",Youtcard);
260        fhBookTree->SetBranchAddress("Zoutcard",Zoutcard);
261        fhBookTree->SetBranchAddress("Erelcard",Erelcard);
262        fhBookTree->SetBranchAddress("Timecard",Timecard);
263        fhBookTree->SetBranchAddress("Pathcard",Pathcard);
264        fhBookTree->SetBranchAddress("P0card",P0card);
265    
266    //    fhBookTree->SetBranchStatus("*",0);
267    
268    }
269    
270    ClassImp(GPamela);
271    
272    /////////////////////////////////////////////////////////////////////////////
273    /////////////////////////////////////////////////////////////////////////////
274    /////////////////////////////////////////////////////////////////////////////
275    /////////////////////////////////////////////////////////////////////////////
276  //--------------------------------------  //--------------------------------------
277  //  //
278  //  //
# Line 10  PamTrack::PamTrack(){ Line 284  PamTrack::PamTrack(){
284      trk_track  = 0;      trk_track  = 0;
285      calo_track = 0;      calo_track = 0;
286      tof_track  = 0;      tof_track  = 0;
287        orb_track  = 0;
288      candeleteobj = 0;      candeleteobj = 0;
289        pscore = 0;
290        iscore = 0;
291  };  };
292  //--------------------------------------  //--------------------------------------
293  //  //
# Line 19  PamTrack::PamTrack(){ Line 296  PamTrack::PamTrack(){
296  /**  /**
297   * Constructor   * Constructor
298   */   */
299  PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o){  PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o, OrbitalInfoTrkVar *r){
300    
301      trk_track  = 0;      trk_track  = 0;
302      calo_track = 0;      calo_track = 0;
303      tof_track  = 0;      tof_track  = 0;
304        orb_track  = 0;
305  //     if(t)trk_track  = new TrkTrack(*t);  //     if(t)trk_track  = new TrkTrack(*t);
306  //     if(c)calo_track = new CaloTrkVar(*c);  //     if(c)calo_track = new CaloTrkVar(*c);
307  //     if(o)tof_track  = new ToFTrkVar(*o);  //     if(o)tof_track  = new ToFTrkVar(*o);
308      if(t)trk_track  = t;      if(t)trk_track  = t;
309      if(c)calo_track = c;      if(c)calo_track = c;
310      if(o)tof_track  = o;      if(o)tof_track  = o;
311        if(r)orb_track  = r;
312      candeleteobj = 0;      candeleteobj = 0;
313    
314  };  };
# Line 38  PamTrack::PamTrack(const PamTrack& track Line 317  PamTrack::PamTrack(const PamTrack& track
317      TrkTrack   *t = track.trk_track;      TrkTrack   *t = track.trk_track;
318      CaloTrkVar *c = track.calo_track;      CaloTrkVar *c = track.calo_track;
319      ToFTrkVar  *o = track.tof_track;      ToFTrkVar  *o = track.tof_track;
320        OrbitalInfoTrkVar  *r = track.orb_track;
321    
322      trk_track  = 0;      trk_track  = 0;
323      calo_track = 0;      calo_track = 0;
324      tof_track  = 0;      tof_track  = 0;
325        orb_track  = 0;
326      if(t)trk_track  = new TrkTrack(*t);      if(t)trk_track  = new TrkTrack(*t);
327      if(c)calo_track = new CaloTrkVar(*c);      if(c)calo_track = new CaloTrkVar(*c);
328      if(o)tof_track  = new ToFTrkVar(*o);      if(o)tof_track  = new ToFTrkVar(*o);
329        if(r)orb_track  = new OrbitalInfoTrkVar(*r);
330      candeleteobj = 1;      candeleteobj = 1;
331            
332  }  }
# Line 55  void PamTrack::Clear(){ Line 337  void PamTrack::Clear(){
337          if(trk_track)  trk_track->TrkTrack::Clear();          if(trk_track)  trk_track->TrkTrack::Clear();
338          if(calo_track) calo_track->CaloTrkVar::Clear();//???          if(calo_track) calo_track->CaloTrkVar::Clear();//???
339          if(tof_track)  tof_track->ToFTrkVar::Clear();//???          if(tof_track)  tof_track->ToFTrkVar::Clear();//???
340            if(orb_track)  orb_track->OrbitalInfoTrkVar::Clear();//???
341      }else{      }else{
342          trk_track  = 0;          trk_track  = 0;
343          calo_track = 0;          calo_track = 0;
344          tof_track  = 0;          tof_track  = 0;
345            orb_track  = 0;
346      }      }
347        pscore = 0;
348        iscore = 0;
349    
350  }  }
351  void PamTrack::Delete(){  void PamTrack::Delete(){
352  //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;  //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;
# Line 76  void PamTrack::Delete(){ Line 363  void PamTrack::Delete(){
363              tof_track->ToFTrkVar::Clear();//???              tof_track->ToFTrkVar::Clear();//???
364              delete tof_track;              delete tof_track;
365          }          }
366            if(orb_track){
367                orb_track->OrbitalInfoTrkVar::Clear();//???
368                delete orb_track;
369            }
370      }else{      }else{
371          Clear();          Clear();
372      }      }
# Line 85  void PamTrack::Delete(){ Line 376  void PamTrack::Delete(){
376  //  //
377  //--------------------------------------  //--------------------------------------
378  /**  /**
379   * Constructor   * Default Constructor
380   */   */
381  PamLevel2::PamLevel2(){  PamLevel2::PamLevel2(){
382        Initialize();
383    };
384    
385    /**
386     * Constructor
387     * @param ddir Name of directory where level2 files are stored.
388     * @param list Name of an ascii file containing the list of file names
389     * @param detlist Options to chose what to load.
390     * Possible options are:
391     *       +AUTO --> load all trees/branches in the input files
392     *       +(-)ALL --> inlcude(exclude) all trees/branches
393     *       +(-)TRK1+(-)TRK2+(-)CAL1+(-)CAL2+(-)TOF+(-)TRG+(-)ND+(-)S4+(-)ORB+(-)AC --> inlcude(exclude) trees and branches
394     *       +(-)TRK0 --> include(exclude) tracker level0 tree
395     *       +(-)GP --> include exclude GPAMELA output tree
396     * If no options are specified, the default is assumed. Default is:
397     * +TRK2+CAL2+CAL1+TOF+TRG+ND+AC+S4+ORB
398     */
399    PamLevel2::PamLevel2(TString ddir,TString list,TString detlist){
400        Initialize();
401        TList*  listf = GetListOfLevel2Files(ddir,list);
402        GetPamTree(listf,detlist);
403        GetRunTree(listf);
404    };
405    
406    PamLevel2::PamLevel2(TString ddir,TList *list,TString detlist){
407        Initialize();
408        GetPamTree(list,detlist);
409        GetRunTree(list);
410    };
411    /**
412     * Constructor
413     * @param ddir Name of directory where level2 files are stored.
414     * @param list Name of an ascii file containing the list of file names
415     * Default trees/branches are loaded. Default is:
416     * +TRK2+CAL2+CAL1+TOF+TRG+ND+AC+S4+ORB
417     */
418    PamLevel2::PamLevel2(TString ddir,TString list){
419        Initialize();
420        TList*  listf = GetListOfLevel2Files(ddir,list);
421        GetPamTree(listf,"");
422        GetRunTree(listf);
423    };
424    
425    
426    void PamLevel2::Initialize(){
427                    
 //     trk2_obj  = TrkLevel2::GetTrkLevel2();  
 //     trk1_obj  = TrkLevel1::GetTrkLevel1();  
 //     trkh_obj  = TrkHough::GetTrkHough();  
 //     calo1_obj = CaloLevel1::GetCaloLevel1();  
 //     calo2_obj = CaloLevel2::GetCaloLevel2();  
 //     tof_obj   = ToFLevel2::GetToFLevel2();  
 //     trig_obj  = TrigLevel2::GetTrigLevel2();  
 //     s4_obj    = S4Level2::GetS4Level2();  
 //     nd_obj    = NDLevel2::GetNDLevel2();  
 //     ac_obj    = AcLevel2::GetAcLevel2();  
 //     orb_obj   = OrbitalInfo::GetOrbitalInfo();  
       
428      h0_obj    = 0;      h0_obj    = 0;
429      trk0_obj  = 0;      trk0_obj  = 0;
430        calo0_obj  = 0;
431    
432      trk2_obj  = 0;      trk2_obj  = 0;
433      trk1_obj  = 0;      trk1_obj  = 0;
# Line 115  PamLevel2::PamLevel2(){ Line 440  PamLevel2::PamLevel2(){
440      nd_obj    = 0;      nd_obj    = 0;
441      ac_obj    = 0;      ac_obj    = 0;
442      orb_obj   = 0;      orb_obj   = 0;
443        gp_obj    = 0;
444    
445      run_obj   = 0;//new GL_RUN();      run_obj   = 0;//new GL_RUN();
446      soft_obj   = 0;// Emiliano      soft_obj   = 0;// Emiliano
447      irun = -1;      irun = -1LL;
448      runfirstentry = 0ULL;      irunt = -1LL;
449      runlastentry = 0ULL;      runfirstentry = 0LL;
450        runlastentry = 0LL;
451    
452      l0_file = NULL;      l0_file = NULL;
453      l0_tree = NULL;      l0_tree = NULL;
454      iroot   = -1;      iroot   = -1;
455      dbc     = 0;      dbc     = 0;
456    
457        prevshift = 0;
458        maxshift = 10; //EMILIANO
459            
     irun = -1;  
460      run_tree = NULL;      run_tree = NULL;
461      run_tree_clone = NULL;      run_tree_clone = NULL;
462      sel_tree = NULL;      sel_tree = NULL;
463      sel_tree_clone = NULL;      sel_tree_clone = NULL;
464            
465      irunentry = -1;      irunentry = -1LL;
466      pam_tree = NULL;      pam_tree = NULL;
467      for(Int_t i=0; i<NCLONES; i++ )pam_tree_clone[i]=NULL;      for(Int_t i=0; i<NCLONES; i++ )pam_tree_clone[i]=NULL;
468    
469        totdltime[0] = 0LL;
470        totdltime[1] = 0LL;
471        totdltime[2] = 0LL;
472    
473      host = "mysql://localhost/pamelaprod";      host = "mysql://localhost/pamelaprod";
474      user = "anonymous";      user = "anonymous";
475      psw = "";      psw = "";
# Line 159  PamLevel2::PamLevel2(){ Line 492  PamLevel2::PamLevel2(){
492      CAL2 = true;      CAL2 = true;
493      TRK2 = true;      TRK2 = true;
494      TRK1 = false;      TRK1 = false;
495      TRKh = false;      TRK0 = false;
496      TRKh = false;      TRKh = false;
497      TRG  = true;      TRG  = true;
498      TOF  = true;      TOF  = true;
# Line 167  PamLevel2::PamLevel2(){ Line 500  PamLevel2::PamLevel2(){
500      S4   = true;      S4   = true;
501      ND   = true;      ND   = true;
502      AC   = true;      AC   = true;
503      ORB  = true;      ORB  = true;    
504            GP   = false;
505    
506      RUN  = true;      RUN  = true;
507    
508      SELLI = -1;      SELLI = -1;
509    
510        ISGP = false;
511    
512        DBG = false;
513    
514      tsorted=0;      tsorted=0;
515      timage=0;      timage=0;
516        
517        howtosort = "+CAL+TOF";
518        //howtosort = "+TOF";
519        sortthr = 100.;
520    
521  };  };
522  /**  /**
# Line 188  void PamLevel2::Delete(){ Line 530  void PamLevel2::Delete(){
530  //    cout << "void PamLevel2::Clear()"<<endl;  //    cout << "void PamLevel2::Clear()"<<endl;
531      if(h0_obj)   delete h0_obj;      if(h0_obj)   delete h0_obj;
532      if(trk0_obj) delete trk0_obj;            if(trk0_obj) delete trk0_obj;      
533        if(calo0_obj) delete calo0_obj;    
534      if(trk1_obj) delete trk1_obj;            if(trk1_obj) delete trk1_obj;      
535      if(trk2_obj) delete trk2_obj;      if(trk2_obj) delete trk2_obj;
536      if(trkh_obj) delete trkh_obj;      if(trkh_obj) delete trkh_obj;
# Line 199  void PamLevel2::Delete(){ Line 542  void PamLevel2::Delete(){
542      if(nd_obj)   delete nd_obj;      if(nd_obj)   delete nd_obj;
543      if(ac_obj)   delete ac_obj;      if(ac_obj)   delete ac_obj;
544      if(orb_obj)  delete orb_obj;      if(orb_obj)  delete orb_obj;
545        if(gp_obj)   delete gp_obj;
546            
547      if(tsorted){      if(tsorted){
548          tsorted->Delete();          tsorted->Delete();
# Line 215  void PamLevel2::Delete(){ Line 559  void PamLevel2::Delete(){
559      }      }
560            
561      if(l0_file)l0_file->Close();      if(l0_file)l0_file->Close();
562      if(pam_tree)pam_tree->Delete();;      //    if(pam_tree)pam_tree->Delete();;
563    
564        if ( pam_tree ){
565            //
566            // 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...
567            //
568            TList *temp = pam_tree->GetListOfFriends();
569            TList *contents  = new TList; // create chain friend list
570            contents->SetOwner();
571            TIter next(temp);
572            TChain *questo = 0;
573            while ( (questo = (TChain*) next()) ){
574                TString name =  questo->GetName();
575                contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
576            };
577            //
578            // deleting the main chain
579            //
580            pam_tree->Delete();
581            //
582            // deleting the friends...
583            //
584            TIter next2(contents);
585            TChain *questa = 0;
586            while ( questa = (TChain*)next2() ){
587                TString name =  questa->GetName();
588                questa->Delete();      
589                questa=NULL;
590            };
591            //
592        };
593        pam_tree = NULL;
594    
595      if(run_tree)run_tree->Delete();;      if(run_tree)run_tree->Delete();;
596      if(sel_tree)sel_tree->Delete();;      if(sel_tree)sel_tree->Delete();;
597      for(Int_t i=0; i<NCLONES; i++ )if(pam_tree_clone[i])pam_tree_clone[i]->Delete();;      for(Int_t i=0; i<NCLONES; i++ )if(pam_tree_clone[i])pam_tree_clone[i]->Delete();;
598      if(run_tree_clone)run_tree_clone->Delete();;      if(run_tree_clone)run_tree_clone->Delete();;
599      if(sel_tree_clone)sel_tree_clone->Delete();;      if(sel_tree_clone)sel_tree_clone->Delete();;
600            
601        if(irunoffset) delete [] irunoffset;
602    
603  };  };
604    
# Line 241  void PamLevel2::Clear(){ Line 618  void PamLevel2::Clear(){
618  //    if(soft_obj) soft_obj->Clear();  //    if(soft_obj) soft_obj->Clear();
619    
620      if(h0_obj)   h0_obj->Clear();            if(h0_obj)   h0_obj->Clear();      
621      if(trk0_obj) trk0_obj->Clear();      //    if(trk0_obj) trk0_obj->Clear();  
622      if(trk1_obj) trk1_obj->Clear();          if(trk1_obj) trk1_obj->Clear();    
623      if(trk2_obj) trk2_obj->Clear();      if(trk2_obj) trk2_obj->Clear();
624      if(trkh_obj) trkh_obj->Clear();      if(trkh_obj) trkh_obj->Clear();
625        if(calo0_obj) calo0_obj->Clear();  
626      if(calo1_obj)calo1_obj->Clear();      if(calo1_obj)calo1_obj->Clear();
627      if(calo2_obj)calo2_obj->Clear();      if(calo2_obj)calo2_obj->Clear();
628      if(tof_obj)  tof_obj->Clear();      if(tof_obj)  tof_obj->Clear();
# Line 253  void PamLevel2::Clear(){ Line 631  void PamLevel2::Clear(){
631      if(nd_obj)   nd_obj->Clear();      if(nd_obj)   nd_obj->Clear();
632      if(ac_obj)   ac_obj->Clear();      if(ac_obj)   ac_obj->Clear();
633      if(orb_obj)  orb_obj->Clear();      if(orb_obj)  orb_obj->Clear();
634        if(gp_obj)   gp_obj->Clear();
635            
636  //    if(sorted_tracks)sorted_tracks->Clear();  //    if(sorted_tracks)sorted_tracks->Clear();
637  //    sorted_tracks.Clear();  //    sorted_tracks.Clear();
# Line 263  void PamLevel2::Clear(){ Line 642  void PamLevel2::Clear(){
642      if(timage){      if(timage){
643          timage->Delete();          timage->Delete();
644      }      }
       
645  };  };
646    
647    void PamLevel2::Reset(){
648        //
649        // First of all clear everything
650        //
651        Clear();
652        //
653        // close and reset chains and pointers
654        //    
655        if ( pam_tree ){
656            //
657            // 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...
658            //
659            TList *temp = pam_tree->GetListOfFriends();
660            TList *contents  = new TList; // create chain friend list
661            contents->SetOwner();
662            TIter next(temp);
663            TChain *questo = 0;
664            while ( (questo = (TChain*) next()) ){
665                TString name =  questo->GetName();
666                contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
667            };
668            //
669            // deleting the main chain
670            //
671            pam_tree->Delete();
672            //
673            // deleting the friends...
674            //
675            TIter next2(contents);
676            TChain *questa = 0;
677            while ( questa = (TChain*)next2() ){
678                TString name =  questa->GetName();
679                questa->Delete();      
680                questa=NULL;
681            };
682            //
683        };
684        pam_tree = NULL;
685        //
686        if(run_tree)run_tree->Delete();;
687        run_tree = NULL;
688        if(sel_tree)sel_tree->Delete();;
689        sel_tree = NULL;
690        //
691        // Close file
692        //
693        if(l0_file)l0_file->Close("R");
694        l0_file = NULL;
695        //
696        h0_obj    = 0;
697        trk0_obj  = 0;
698        calo0_obj  = 0;
699        //
700        trk2_obj  = 0;
701        trk1_obj  = 0;
702        trkh_obj  = 0;
703        calo1_obj = 0;
704        calo2_obj = 0;
705        tof_obj   = 0;
706        trig_obj  = 0;
707        s4_obj    = 0;
708        nd_obj    = 0;
709        ac_obj    = 0;
710        orb_obj   = 0;
711        gp_obj    = 0;
712        //
713        // Reset run pointers
714        //
715        run_obj   = 0;//new GL_RUN();
716        soft_obj   = 0;// Emiliano
717        irun = -1;
718        irunt = -1;
719        runfirstentry = 0ULL;
720        runlastentry = 0ULL;    
721        //
722        totdltime[0] = 0LL;
723        totdltime[1] = 0LL;
724        totdltime[2] = 0LL;
725        //
726    };
727    
728    Bool_t PamLevel2::IsGood(Bool_t strict){
729      Bool_t goodev=true;
730      //
731      if(calo2_obj && !calo2_obj->IsGood(strict) ) goodev = false;
732      //
733      if ( strict ){
734        if(trk2_obj && trk2_obj->UnpackError() != 0 ) goodev = false;
735        if(tof_obj && tof_obj->unpackError != 0) goodev = false;  
736        if(trig_obj && trig_obj->unpackError != 0) goodev = false;
737        if(s4_obj && s4_obj->unpackError != 0) goodev = false;  
738        if(nd_obj && nd_obj->unpackError != 0) goodev = false;  
739        if(ac_obj && (ac_obj->unpackError != 0 || ((ac_obj->status[0]>>3)&1) || ((ac_obj->status[0]>>3)&1)) ) goodev = false;      
740        //  if(orb_obj)  
741      } else {
742        if(nd_obj && nd_obj->unpackError != 0) goodev = false;  
743        if(ac_obj && (ac_obj->unpackError != 0 || ((ac_obj->status[0]>>3)&1) || ((ac_obj->status[0]>>3)&1)) ) goodev = false;      
744      };
745      return(goodev);
746    };
747    
748  //--------------------------------------  //--------------------------------------
749  //  //
# Line 290  void *PamLevel2::GetPointerTo(const char Line 768  void *PamLevel2::GetPointerTo(const char
768          return &trk2_obj;          return &trk2_obj;
769      };      };
770      if(!objname.CompareTo("TrkHough"))   {      if(!objname.CompareTo("TrkHough"))   {
771          if(!trkh_obj)  trkh_obj  = new TrkHough();          if(!trkh_obj)  {
772                trkh_obj  = new TrkHough();
773                trkh_obj->Set();
774            }
775          return &trkh_obj;          return &trkh_obj;
776      };      };
777      if(!objname.CompareTo("CaloLevel1")) {      if(!objname.CompareTo("CaloLevel1")) {
# Line 331  void *PamLevel2::GetPointerTo(const char Line 812  void *PamLevel2::GetPointerTo(const char
812          if(!orb_obj)   orb_obj   = new OrbitalInfo();          if(!orb_obj)   orb_obj   = new OrbitalInfo();
813          return &orb_obj;          return &orb_obj;
814      };      };
815        if(!objname.CompareTo("GPamela")){
816            if(!gp_obj)   gp_obj   = new GPamela();
817            return &gp_obj;
818        };
819            
820      if(!objname.CompareTo("RunInfo"))return &run_obj;      if(!objname.CompareTo("RunInfo"))return &run_obj;
821    
# Line 346  void *PamLevel2::GetPointerTo(const char Line 831  void *PamLevel2::GetPointerTo(const char
831   * Retrieves the calorimeter track matching the seqno-th tracker stored track.   * Retrieves the calorimeter track matching the seqno-th tracker stored track.
832   * (If seqno = -1 retrieves the self-trigger calorimeter track)   * (If seqno = -1 retrieves the self-trigger calorimeter track)
833   */   */
834   CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno){  CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno){
835        
836        if( !calo2_obj )return 0;
837    
838        if( calo2_obj->CaloLevel2::ntrk()==0 ){
839            cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no Calorimeter tracks are stored"<<endl;
840            return NULL;
841        };
842            
843       if( !calo2_obj )return 0;      CaloTrkVar *c = 0;
844        Int_t it_calo=0;
845        
846        do{
847            c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);
848            it_calo++;
849        } while( c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());    
850        
851        if(!c || seqno != c->trkseqno){
852            c = 0;
853            if(seqno!=-1)cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match Calorimeter stored tracks"<<endl;
854        };
855        return c;
856        
857    };
858    //--------------------------------------
859    //
860    //
861    //--------------------------------------
862    /**
863     * Retrieves the ToF track matching the seqno-th tracker stored track.
864     * (If seqno = -1 retrieves the tracker-independent tof track)
865     */
866    ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno){
867            
868        if( !tof_obj )return 0;
869    
870       if( calo2_obj->CaloLevel2::ntrk()==0 ){      if( tof_obj->ToFLevel2::ntrk()==0 ){
871           cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no Calorimeter tracks are stored"<<endl;          cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no ToF tracks are stored"<<endl;
872           return NULL;          return NULL;
873       };      };
874            
875       CaloTrkVar *c = 0;      ToFTrkVar *c = 0;
876       Int_t it_calo=0;      Int_t it_tof=0;
877            
878       do{      do{
879           c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);          c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof);
880           it_calo++;          it_tof++;
881       } while( c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());          } while( c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk());
882            
883       if(!c || seqno != c->trkseqno){      if(!c || seqno != c->trkseqno){
884           c = 0;          c = 0;
885           if(seqno!=-1)cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match Calorimeter stored tracks"<<endl;          if(seqno!=-1)cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match ToF stored tracks"<<endl;
886       };      };
887       return c;      return c;
888            
889   };  };
890    
891  //--------------------------------------  //--------------------------------------
892   //  //
893   //  //
894  //--------------------------------------  //--------------------------------------
895  /**  /**
896    * Retrieves the ToF track matching the seqno-th tracker stored track.   * Retrieves the OrbitalInfo track matching the seqno-th tracker stored track.
897    * (If seqno = -1 retrieves the tracker-independent tof track)   * (If seqno = -1 retrieves the tracker-independent tof related track)
898   */   */
899   ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno){  OrbitalInfoTrkVar *PamLevel2::GetOrbitalInfoStoredTrack(int seqno){
900                    
901       if( !tof_obj )return 0;      if( !orb_obj )return 0;
902    
903       if( tof_obj->ToFLevel2::ntrk()==0 ){      if( orb_obj->OrbitalInfo::ntrk()==0 ){
904           cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no ToF tracks are stored"<<endl;          cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no OrbitalInfo tracks are stored"<<endl;
905           return NULL;          return NULL;
906       };      };
907            
908       ToFTrkVar *c = 0;      OrbitalInfoTrkVar *c = 0;
909       Int_t it_tof=0;      Int_t it_tof=0;
910            
911       do{      do{
912           c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof);          c = orb_obj->OrbitalInfo::GetOrbitalInfoTrkVar(it_tof);
913           it_tof++;          it_tof++;
914       } while( c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk());              } while( c && seqno != c->trkseqno && it_tof < orb_obj->OrbitalInfo::ntrk());      
915            
916       if(!c || seqno != c->trkseqno){      if(!c || seqno != c->trkseqno){
917           c = 0;          c = 0;
918           if(seqno!=-1)cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match ToF stored tracks"<<endl;          if(seqno!=-1)cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match OrbitalInfo stored tracks"<<endl;
919       };      };
920       return c;      return c;
921            
922   };  };
923    
924  //--------------------------------------  //--------------------------------------
925   //  //
926   //  //
927  //--------------------------------------  //--------------------------------------
928  /**  /**
929   * Give the pamela track associated to a tracker track, retrieving related calorimeter and tof track information.   * Give the pamela track associated to a tracker track, retrieving related calorimeter, orbitalinfo and tof track information.
930   */   */
931   PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t){  PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t){
932            
933       cout <<"PamLevel2::GetPamTrackAlong(TrkTrack* t) **obsolete** "<<endl;      cout <<"PamLevel2::GetPamTrackAlong(TrkTrack* t) **obsolete** "<<endl;
934       cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;      cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;
935    
936       CaloTrkVar *c = 0;      CaloTrkVar *c = 0;
937       ToFTrkVar  *o = 0;      ToFTrkVar  *o = 0;
938        OrbitalInfoTrkVar  *r = 0;
939            
940       if(CAL2) c = GetCaloStoredTrack(t->GetSeqNo());      if(CAL2) c = GetCaloStoredTrack(t->GetSeqNo());
941       if(TOF) o = GetToFStoredTrack(t->GetSeqNo());      if(TOF) o = GetToFStoredTrack(t->GetSeqNo());
942        if(ORB) r = GetOrbitalInfoStoredTrack(t->GetSeqNo());
943            
944  //    if(t && c && o)track = new PamTrack(t,c,o);  //    if(t && c && o)track = new PamTrack(t,c,o);
945       PamTrack *track = new PamTrack(t,c,o);      PamTrack *track = new PamTrack(t,c,o,r);
946            
947       return track;      return track;
948    
949   };  };
950  //--------------------------------------  //--------------------------------------
951  //  //
952  //  //
# Line 440  void *PamLevel2::GetPointerTo(const char Line 960  void *PamLevel2::GetPointerTo(const char
960  PamTrack* PamLevel2::GetStoredTrack(Int_t itrk){  PamTrack* PamLevel2::GetStoredTrack(Int_t itrk){
961            
962      cout <<"PamLevel2::GetStoredTrack(Int_t itrk) **to-be-updated** "<<endl;      cout <<"PamLevel2::GetStoredTrack(Int_t itrk) **to-be-updated** "<<endl;
963      cout <<"for the moment, better use separately the methods: TrkLevel2::GetStoredTrack(seqno) CaloLevel2::GetCaloTrkVar(Int_t notrack) ToFLevel2::GetToFTrkVar(Int_t notrack)"<<endl;      cout <<"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)"<<endl;
964      cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;      cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;
965      PamTrack *track = 0;      PamTrack *track = 0;
966            
# Line 460  PamTrack* PamLevel2::GetStoredTrack(Int_ Line 980  PamTrack* PamLevel2::GetStoredTrack(Int_
980  }  }
981  //--------------------------------------  //--------------------------------------
982  //  //
983    
984    /**
985     * Sort physical (tracker) tracks. Left here as backward compatibility method.
986     **/
987    void PamLevel2::SortTracks(TString how){
988        printf(" WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",how.Data());
989        howtosort = how;  
990        SortTracks();
991    };
992    
993  //  //
994  //--------------------------------------  //--------------------------------------
995  /**  /**
996   * Sort physical (tracker) tracks and stores them in the TRefArray (of TrkTrack objects) which pointer is  PamLevel2::sorted_tracks.   * Sort physical (tracker) tracks.
997   * @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...).
998   * Sorting cryteria:   * Sorting cryteria:
999   * TRK: lower chi**2   * TRK: lower chi**2
1000   * CAL: lower Y spatial residual on the first calorimeter plane   * CAL: lower Y spatial residual on the first calorimeter plane
1001   * 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).
1002     * S1: (ask Emiliano)
1003     * S2: (ask Emiliano)
1004     * S3: (ask Emiliano)
1005     * GP: more GP hits
1006   * The default sorting cryterium is "TOF+CAL".   * The default sorting cryterium is "TOF+CAL".
1007   *   *
1008   * 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).
1009   */   */
1010  void PamLevel2::SortTracks(TString how){  void PamLevel2::SortTracks(){
1011        TString how = howtosort;
1012    
1013  //    cout <<" PamLevel2::SortTracks(TString how) "<<endl;      //  cout <<" PamLevel2::SortTracks(TString how) "<<endl;
1014      if( !trk2_obj ){      if( !trk2_obj ){
1015          cout << "void PamLevel2::SortTracks(TString how):  TrkLevel2 not loaded !!!";          cout << "void PamLevel2::SortTracks():  TrkLevel2 not loaded !!!";
1016          return;          return;
1017      };      };
1018  //    cout << "call SortTracs() "<<endl;      //Save current Object count
   //Save current Object count  
1019      Int_t ObjectNumber = TProcessID::GetObjectCount();      Int_t ObjectNumber = TProcessID::GetObjectCount();
1020    
1021  //    cout << "ObjectNumber  "<<ObjectNumber <<endl;      // create TCloneArrays to store tracks and its images
   
 //     if(!sorted_tracks)sorted_tracks = new TRefArray();  
 //     sorted_tracks->Clear();  
 //    sorted_tracks.Clear();  
   
1022      if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());      if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
1023      tsorted->Delete();      tsorted->Delete();
1024      TClonesArray &ttsorted = *tsorted;      TClonesArray &ttsorted = *tsorted;
1025    
1026      if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());      if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
1027      timage->Delete();      timage->Delete();
1028      TClonesArray &ttimage = *timage;      TClonesArray &ttimage = *timage;
1029    
1030    
1031      // loop over the tracks sorted by the tracker  
1032        //--------------------------------------------------
1033        // retrieve sorting method
1034        //--------------------------------------------------
1035      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
1036      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
1037      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
1038            Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase);
1039        Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase);
1040        Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase);
1041        Bool_t use_GP = how.Contains("GP", TString::kIgnoreCase);
1042      
1043        if ( use_TOF ){
1044            use_S1 = true;
1045            use_S2 = true;
1046            use_S3 = true;
1047        };
1048      if( !CAL2 &&  use_CAL) use_CAL = false;      if( !CAL2 &&  use_CAL) use_CAL = false;
1049      if( !TOF &&  use_TOF) use_TOF = false;      if( !TOF ){
1050                use_TOF = false;
1051            use_S1  = false;
1052            use_S2  = false;
1053            use_S3  = false;
1054        }
1055        if( !GP ){
1056            use_GP  = false;
1057        }
1058    
1059      if( !TRK2 ){      if( !TRK2 ){
1060  //      cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;          cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
1061          return;          return;
1062      };      };
1063    
1064      //   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;
1065          
1066    
1067        //--------------------------------------------------
1068        // loop over "physical" tracks sorted by the tracker
1069        //--------------------------------------------------
1070      for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){      for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){
1071                    
1072          TrkTrack   *ts = 0;          TrkTrack   *ts = 0;
1073          CaloTrkVar *cs = 0;          CaloTrkVar *cs = 0;
1074          ToFTrkVar  *os = 0;          ToFTrkVar  *os = 0;
1075            OrbitalInfoTrkVar  *rs = 0;
1076                    
1077          // get tracker tracks          // get tracker tracks
1078          TrkTrack   *tp = trk2_obj->TrkLevel2::GetTrack(i);                    //tracker          TrkTrack   *tp = trk2_obj->TrkLevel2::GetTrack(i); //tracker
1079          CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());          CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());
1080          ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());          ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());
1081            OrbitalInfoTrkVar  *rp = GetOrbitalInfoStoredTrack(tp->GetSeqNo());
1082    
1083          TrkTrack   *ti = 0;              //tracker (image)          TrkTrack   *ti = 0; //tracker (image)
1084          CaloTrkVar *ci = 0;          CaloTrkVar *ci = 0;
1085          ToFTrkVar  *oi = 0;          ToFTrkVar  *oi = 0;
1086  //      cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;          OrbitalInfoTrkVar  *ri = 0;
1087            //      cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
1088          // if track has an image, check image selection          // if track has an image, check image selection
1089    
1090            Int_t tp_score = 0;  //main track sorted by the tracker
1091            Int_t ti_score = 0;  //image track
1092            Int_t totp_score = 0;  //main track sorted by the tracker
1093            Int_t toti_score = 0;  //image track
1094    
1095          if(tp->HasImage()){          if(tp->HasImage()){
1096                            
1097              ti = trk2_obj->TrkLevel2::GetTrackImage(i);              //tracker (image)              ti = trk2_obj->TrkLevel2::GetTrackImage(i);              //tracker (image)
1098              ci = GetCaloStoredTrack(ti->GetSeqNo());              ci = GetCaloStoredTrack(ti->GetSeqNo());
1099              oi = GetToFStoredTrack(ti->GetSeqNo());              oi = GetToFStoredTrack(ti->GetSeqNo());
1100                ri = GetOrbitalInfoStoredTrack(ti->GetSeqNo());
1101                            
1102  //          cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;              //      cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
1103    
1104              //assign starting scores              //assign starting scores
1105              Int_t tp_score = 0;  //main track sorted by the tracker              tp_score = 0;  //main track sorted by the tracker
1106              Int_t ti_score = 0;  //image track              ti_score = 0;  //image track
1107                            
1108              // ------------------------              // -----------------------------------------------------------------------------------------
1109                // *****************************************************************************************
1110                // -----------------------------------------------------------------------------------------
1111              // calorimeter check              // calorimeter check
1112              // ------------------------              // -----------------------------------------------------------------------------------------
             // check the Y spatial residual on the first calorimeter plane  
             // (cut on calorimeter variables from Emiliano)  
1113              if( use_CAL && !calo2_obj ){              if( use_CAL && !calo2_obj ){
1114                  cout << "void PamLevel2::SortTracks(TString how): how= "<<how<<" but CaloLevel2 not loaded !!!";                  cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but CaloLevel2 not loaded !!!";
1115                  return;                  return;
1116              };              };
1117                if( use_CAL && !cp &&  ci ){
1118                    ti_score++;
1119                    toti_score++;
1120                };
1121                if( use_CAL &&  cp && !ci ){
1122                    tp_score++;
1123                    totp_score++;
1124                };
1125              if(              if(
1126                  use_CAL            &&                  use_CAL            &&
1127                  calo2_obj->npcfit[1] > 15     &&   //no. of fit planes on Y view                  cp && ci &&
                 calo2_obj->varcfit[1] < 1000. &&  //fit variance on Y view  
1128                  true){                  true){
1129    
1130                                    
1131                  Float_t resy_p = cp->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_p < 0)resy_p= - resy_p;                  if(
1132                  Float_t resy_i = ci->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_i < 0)resy_i= - resy_i;                      cp->npresh > ci->npresh &&
1133                                        true){
1134                  if(resy_p <= resy_i) tp_score++;                      tp_score++;
1135                  else                 ti_score++;                      totp_score++;
1136                    };
1137                    if(
1138                        cp->npresh < ci->npresh &&
1139                        true){
1140                        ti_score++;
1141                        toti_score++;
1142                    };
1143    
1144  //              cout << "CALO "<<tp_score<<ti_score<<endl;                  //      cout << "CALO "<<tp_score<<ti_score<<endl;
1145    
1146              };              };
1147              // ------------------------              // -----------------------------------------------------------------------------------------
1148                // *****************************************************************************************
1149                // -----------------------------------------------------------------------------------------
1150              // TOF check              // TOF check
1151              // ------------------------                  // -----------------------------------------------------------------------------------------
1152              // check the number of hit pmts along the track              // check the number of hit pmts along the track
1153              // on S12 S21 and S32, where paddles are parallel to Y axis              // on S12 S21 and S32, where paddles are parallel to Y axis
1154              if( use_TOF && !tof_obj ){              if( (use_TOF || use_S1 || use_S2 || use_S3 ) && !tof_obj ){
1155                  cout << "void PamLevel2::SortTracks(TString how): how= "<<how<<" but ToFLevel2 not loaded !!!";                  cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but ToFLevel2 not loaded !!!";
1156                  return;                  return;
1157              };              };
1158              if( use_TOF ){              //
1159                                if( (use_TOF || use_S1 || use_S2 || use_S3 ) && !op && oi ){
1160                  Int_t nphit_p =0;                  ti_score++;
1161                  Int_t nphit_i =0;                  toti_score++;
1162                                };
1163                                if( (use_TOF || use_S1 || use_S2 || use_S3 ) && op && !oi ){
1164  /*                              cout << "track: npmtadc "<< op->npmtadc << endl;                  tp_score++;
1165                                  cout << "track: npmttdc "<< op->npmttdc << endl;                  totp_score++;
1166                                  cout << "image: npmtadc "<< oi->npmtadc << endl;              };
1167                                  cout << "image: npmttdc "<< oi->npmttdc << endl;*/              if( (use_TOF || use_S1 || use_S2 || use_S3 ) && op && oi ){
1168                                    //
1169                    Float_t sen = 0.;
1170                  for (Int_t ih=0; ih < op->npmtadc; ih++){                  for (Int_t ih=0; ih < op->npmtadc; ih++){
1171                      Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );                      Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1172                      if(pl == 1 || pl == 2 || pl == 5)nphit_p++;                      if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (op->dedx).At(ih);
1173                  };                  };
                   
1174                  for (Int_t ih=0; ih < oi->npmtadc; ih++){                  for (Int_t ih=0; ih < oi->npmtadc; ih++){
1175                      Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );                      Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1176                      if(pl == 1 || pl == 2 || pl == 5)nphit_i++;                      if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (oi->dedx).At(ih);
1177                  };                  };
1178                    //
1179                    if (  sen >= sortthr && false){ // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!)
1180                        //printf(" IS A NUCLEUS! en = %f \n",sen);
1181                        //
1182                        // is a nucleus use a different algorithm
1183                        //
1184                        Int_t nz = 6; Float_t zin[6];                                          // << define TOF z-coordinates
1185                        for(Int_t ip=0; ip<nz; ip++)
1186                            zin[ip] = tof_obj->ToFLevel2::GetZTOF(tof_obj->ToFLevel2::GetToFPlaneID(ip));     // << read ToF plane z-coordinates
1187                        Trajectory *tr = new Trajectory(nz,zin);
1188                        //
1189                        Int_t nphit_p =0;
1190                        Int_t nphit_i =0;
1191                        Float_t enhit_p = 0.;
1192                        Float_t enhit_i = 0.;
1193                        //
1194                        for (Int_t ih=0; ih < op->npmtadc; ih++){
1195                            Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1196                            if(pl == 1 || pl == 2 || pl == 5){
1197                                nphit_p++;
1198                                enhit_p += (op->dedx).At(ih);
1199                            };
1200                        };
1201                        //
1202                        tp->DoTrack2(tr);
1203                        //
1204                        if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
1205                            for (Int_t ih=0; ih < op->npmtadc; ih++){
1206                                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1207                                if(pl == 0){
1208                                    nphit_p++;
1209                                    enhit_p += (op->dedx).At(ih);
1210                                };
1211                            };
1212                        };
1213                        if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
1214                            for (Int_t ih=0; ih < op->npmtadc; ih++){
1215                                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1216                                if(pl == 3){
1217                                    nphit_p++;
1218                                    enhit_p += (op->dedx).At(ih);
1219                                };
1220                            };
1221                        };
1222                        if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
1223                            for (Int_t ih=0; ih < op->npmtadc; ih++){
1224                                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1225                                if(pl == 4){
1226                                    nphit_p++;
1227                                    enhit_p += (op->dedx).At(ih);
1228                                };
1229                            };
1230                        };
1231    
1232                        for (Int_t ih=0; ih < oi->npmtadc; ih++){
1233                            Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1234                            if(pl == 1 || pl == 2 || pl == 5){
1235                                nphit_i++;  
1236                                enhit_i += (op->dedx).At(ih);
1237                            };
1238                        };
1239                        //
1240                        ti->DoTrack2(tr);
1241                        //
1242                        if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
1243                            for (Int_t ih=0; ih < oi->npmtadc; ih++){
1244                                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1245                                if(pl == 0){
1246                                    nphit_i++;
1247                                    enhit_i += (op->dedx).At(ih);
1248                                };
1249                            };
1250                        };
1251                        if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
1252                            for (Int_t ih=0; ih < oi->npmtadc; ih++){
1253                                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1254                                if(pl == 3){
1255                                    nphit_i++;
1256                                    enhit_i += (op->dedx).At(ih);
1257                                };
1258                            };
1259                        };
1260                        if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
1261                            for (Int_t ih=0; ih < oi->npmtadc; ih++){
1262                                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1263                                if(pl == 4){
1264                                    nphit_i++;
1265                                    enhit_i += (op->dedx).At(ih);
1266                                };
1267                            };
1268                        };          
1269    
1270                                    
1271                  if(                      if(
1272                      use_TOF            &&                          (use_TOF || use_S1 || use_S2 || use_S3)            &&
1273                      (nphit_p+nphit_i) !=0 &&                              (nphit_p+nphit_i) !=0 &&        
1274                      true){                          true){
1275                        
1276                            //          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);
1277                            //          printf(" score p %i score i %i \n",tp_score,ti_score);
1278                            //                if( enhit_p > enhit_i ) tp_score++;
1279                            //                if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
1280                            if ( nphit_p > nphit_i ) tp_score++;
1281                            if ( nphit_p < nphit_i ) ti_score++;
1282                            if ( nphit_p == nphit_i ){
1283                                if ( enhit_p > enhit_i ) tp_score++;
1284                                else ti_score++;
1285                            };
1286                            //          printf(" dopo score p %i score i %i \n",tp_score,ti_score);
1287                        };
1288                        delete tr;
1289                        //
1290                    } else {
1291                        // -------------
1292                        // NOT a NUCLEUS
1293                        // -------------
1294                        //printf(" NOT a NUCLEUS! en = %f \n",sen);
1295    
1296                        Int_t nphit_p =0;
1297                        Int_t nphit_i =0;
1298                    
1299                    
1300                        /*                          cout << "track: npmtadc "<< op->npmtadc << endl;
1301                                                    cout << "track: npmttdc "<< op->npmttdc << endl;
1302                                                    cout << "image: npmtadc "<< oi->npmtadc << endl;
1303                                                    cout << "image: npmttdc "<< oi->npmttdc << endl;*/
1304                    
1305    //        for (Int_t ih=0; ih < op->npmtadc; ih++){
1306    //          Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1307    //          if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
1308    //        };
1309                    
1310    //        for (Int_t ih=0; ih < oi->npmtadc; ih++){
1311    //          Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1312    //          if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
1313    //        };
1314                        // --- modified to count tdc signals (more efficient?)
1315                        // --- and to implement check on tdcflag
1316                        for (Int_t ih=0; ih < op->npmttdc; ih++){
1317                            Int_t pl = tof_obj->GetPlaneIndex( (op->pmttdc).At(ih) );
1318    //          if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
1319                            if ( (use_S1 && ( pl == 0 || pl == 1 )) || (use_S2 && ( pl == 2 || pl == 3 )) || (use_S3 && ( pl == 4 || pl == 5 )) ){
1320                                if( (op->tdcflag).At(ih)==0 )nphit_p++;
1321                            };
1322                        };
1323                    
1324                        for (Int_t ih=0; ih < oi->npmttdc; ih++){
1325                            Int_t pl = tof_obj->GetPlaneIndex( (oi->pmttdc).At(ih) );
1326    //          if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;  
1327                            if ( (use_S1 && ( pl == 0 || pl == 1 )) || (use_S2 && ( pl == 2 || pl == 3 )) || (use_S3 && ( pl == 4 || pl == 5 )) ){
1328                                if( (oi->tdcflag).At(ih)==0 )nphit_i++;    
1329                            };
1330                        };
1331                    
1332                        if(
1333                            (nphit_p+nphit_i) !=0 &&        
1334                            true){
1335                                            
1336                      if( nphit_p >= nphit_i) tp_score++;                          if ( nphit_p != nphit_i ){
1337                      else ti_score++;                              totp_score += nphit_p;
1338                                toti_score += nphit_i;
1339                                tp_score+=nphit_p;
1340                                ti_score+=nphit_i;
1341                            };
1342                            //          if     ( nphit_p > nphit_i) tp_score+=nphit_p;
1343                            //          else if( nphit_p < nphit_i) ti_score+=nphit_i;
1344                            //          else ;//niente
1345                        };
1346                  };                  };
1347  //              cout << "TOF "<<tp_score<<ti_score<<endl;                  //      cout << "TOF "<<tp_score<<ti_score<<endl;
1348              };              };
1349              if(tp_score == ti_score) use_TRK = true;  
1350              // ------------------------  
1351    //      if(tp_score == ti_score) use_TRK = true;
1352    
1353    
1354                // -----------------------------------------------------------------------------------------
1355                // *****************************************************************************************
1356                // -----------------------------------------------------------------------------------------
1357              // tracker check              // tracker check
1358              // ------------------------              // -----------------------------------------------------------------------------------------
1359              // chi**2 difference is not always large enough to distinguish among              // chi**2 difference is not always large enough to distinguish among
1360              // the real track and its image.              // the real track and its image.
1361              // 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.
1362                // *** MODIFIED ON AUGUST 2007 ***
1363              if(use_TRK){              if(use_TRK){
1364                  if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;  //      if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
1365                  else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;  //      else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
1366  //              cout << "TRK "<<tp_score<<ti_score<<endl;  
1367                    // CHECK 1 : number of points along X  
1368                    if ( tp->GetNX() >= ti->GetNX() ){
1369                        tp_score++ ;
1370                        totp_score++ ;
1371                    };
1372                    if ( tp->GetNX() <= ti->GetNX() ){
1373                        ti_score++ ;
1374                        toti_score++ ;
1375                    };
1376                    // CHECK 2 : number of points along Y  
1377                    if ( tp->GetNY() >= ti->GetNY() ){
1378                        tp_score++ ;
1379                        totp_score++ ;
1380                    };
1381                    if ( tp->GetNY() <= ti->GetNY() ){
1382                        ti_score++ ;
1383                        toti_score++ ;
1384                    };
1385    
1386                    //      cout << "TRK "<<tp_score<<ti_score<<endl;
1387              };              };
1388                            
1389              // ------------------------  
1390                // -----------------------------------------------------------------------------------------
1391                // *****************************************************************************************
1392                // -----------------------------------------------------------------------------------------
1393                // GPamela check
1394                // -----------------------------------------------------------------------------------------
1395                
1396                //---------------------------------------------------
1397                // count the number of GP hits
1398                //---------------------------------------------------
1399                if(use_GP){
1400                    int ngphits_p=0;
1401                    int ngphits_i=0;
1402                    float toll = 0.02; //200 micron
1403                    for(int ih=0; ih<GetGPamela()->Nthspe; ih++){
1404                        int ip = (Int_t) GetGPamela()->Itrpb[ih] - 1;
1405                        if(
1406                            tp &&
1407                            tp->YGood(ip) &&
1408                            fabs(tp->ym[ip]- GetGPamela()->Yavspe[ih])<toll &&
1409                            true) ngphits_p++;
1410                        if( ti &&
1411                            ti->YGood(ip) &&
1412                            fabs(ti->ym[ip]- GetGPamela()->Yavspe[ih])<toll &&
1413                            true) ngphits_i++;
1414                    }
1415                    if(
1416                        ngphits_p > ngphits_i &&
1417                        true){
1418                        tp_score++ ;
1419                        totp_score++ ;                  
1420                    }
1421                    if(
1422                        ngphits_p < ngphits_i &&
1423                        true){
1424                        ti_score++ ;
1425                        toti_score++ ;                  
1426                    }
1427                }
1428    
1429    
1430                // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1431              // the winner is....              // the winner is....
1432              // ------------------------                  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1433              if      (tp_score > ti_score) {              if      (tp_score > ti_score) {
1434  //              ts = tp;//the track sorted by the tracker!!          
1435  //              cs = cp;  
 //              os = op;  
1436              }else if (tp_score < ti_score) {              }else if (tp_score < ti_score) {
1437    
1438    
1439                  ts = ti;//its image!!                  ts = ti;//its image!!
1440                  cs = ci;                  cs = ci;
1441                  os = oi;                  os = oi;
1442                    rs = ri;
1443                    Int_t totis = toti_score;
1444    
1445                  ti = tp;//its image!!                  ti = tp;//its image!!
1446                  ci = cp;                  ci = cp;
1447                  oi = op;                  oi = op;
1448                    ri = rp;
1449    
1450                  tp = ts;//its image!!                  tp = ts;//its image!!
1451                  cp = cs;                  cp = cs;
1452                  op = os;                  op = os;
1453                    rp = rs;
1454    
1455                    toti_score = totp_score;
1456                    totp_score = totis;
1457    
1458                                    
1459              }else {              }else {
1460  //              ts = tp;  
1461  //              cs = cp;  //      cout << "Warning - track image ambiguity not solved" << endl;
1462  //              os = op;  
 //                              cout << "Warning - track image ambiguity not solved" << endl;  
 //                              cout << ts->GetNtot() << " "<< ts->chi2 << " " << npcfit[1] << " "<< nphit_p << endl;  
1463              };              };
1464                            
1465          }else{          }else{
1466  //          ts = tp;              totp_score = 1;
1467  //          cs = cp;              toti_score = 0;
1468  //          os = op;        
1469                //      ts = tp;
1470                //      cs = cp;
1471                //      os = op;
1472          };          };
1473                    
1474  //      cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;          //      cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
1475  //      sorted_tracks->Add(ts);//save the track in the sorted array          //      sorted_tracks->Add(ts);//save the track in the sorted array
1476  //      sorted_tracks.Add(ts);//save the track in the sorted array          //      sorted_tracks.Add(ts);//save the track in the sorted array
1477  //      sorted_tracks.Add(tp);//save the track in the sorted array          //      sorted_tracks.Add(tp);//save the track in the sorted array
1478  //      cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;          //      cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
1479  //      cout<<"o "<<tp<<endl;          //      cout<<"o "<<tp<<endl;
1480  //      cout<<"o "<<cp<<endl;          //      cout<<"o "<<cp<<endl;
1481  //      cout<<"o "<<op<<endl;          //      cout<<"o "<<op<<endl;
1482          new(ttsorted[i]) PamTrack(tp,cp,op);  
1483          new(ttimage[i])  PamTrack(ti,ci,oi);          new(ttsorted[i]) PamTrack(tp,cp,op,rp);
1484            new(ttimage[i])  PamTrack(ti,ci,oi,ri);
1485    
1486            ((PamTrack*)(ttsorted[i]))->SetPScore(totp_score);
1487            ((PamTrack*)(ttsorted[i]))->SetIScore(toti_score);
1488            ((PamTrack*)(ttimage[i]))->SetPScore(totp_score);
1489            ((PamTrack*)(ttimage[i]))->SetIScore(toti_score);
1490      };      };
1491    
1492      if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){      if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){
1493          cout << "void PamLevel2::SortTracks(TString how): tsorted->GetEntries() "<<tsorted->GetEntries()<<" != trk2_obj->GetNTracks() = "<<trk2_obj->GetNTracks() <<endl;          cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() "<<tsorted->GetEntries()<<" != trk2_obj->GetNTracks() = "<<trk2_obj->GetNTracks() <<endl;
1494          tsorted->Delete(); tsorted=0;          tsorted->Delete(); tsorted=0;
1495          timage->Delete(); timage=0;              timage->Delete(); timage=0;    
1496      }      }
# Line 699  void PamLevel2::SortTracks(TString how){ Line 1522  void PamLevel2::SortTracks(TString how){
1522  TClonesArray *PamLevel2::GetTracks(){  TClonesArray *PamLevel2::GetTracks(){
1523    
1524  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1525      SortTracks("+CAL+TOF");      SortTracks();
1526  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1527    
1528      return tsorted;      return tsorted;
1529            
1530   };  };
1531  //--------------------------------------  //--------------------------------------
1532   //  //
1533   //  //
1534  //--------------------------------------  //--------------------------------------
1535  /**  /**
1536   * Retrieves the it-th Pamela "physical" track.   * Retrieves the it-th Pamela "physical" track.
# Line 718  PamTrack *PamLevel2::GetTrack(int it){ Line 1541  PamTrack *PamLevel2::GetTrack(int it){
1541    
1542  //    if(!trk2_obj) return 0;  //    if(!trk2_obj) return 0;
1543                    
1544  // //  *-*-*-*-*-*-*-*-*-*-*-*-*  // //  *-*-*-*+-*-*-*-*-*-*-*-*-*
1545  //     SortTracks("+CAL+TOF");  //     SortTracks("+CAL+TOF");
1546  // //  *-*-*-*-*-*-*-*-*-*-*-*-*  // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1547  // //    if(!sorted_tracks)return 0;  // //    if(!sorted_tracks)return 0;
# Line 737  PamTrack *PamLevel2::GetTrack(int it){ Line 1560  PamTrack *PamLevel2::GetTrack(int it){
1560    
1561  //    cout << "PamLevel2::GetTrack(int it) "<<endl;  //    cout << "PamLevel2::GetTrack(int it) "<<endl;
1562  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1563      SortTracks("+CAL+TOF");      SortTracks();
1564  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1565      if(!tsorted)return 0;      if(!tsorted)return 0;
1566      if(!tsorted->GetEntries())return 0;      if(!tsorted->GetEntries())return 0;
# Line 768  PamTrack *PamLevel2::GetTrack(int it){ Line 1591  PamTrack *PamLevel2::GetTrack(int it){
1591   */   */
1592  PamTrack *PamLevel2::GetTrackImage(int it){  PamTrack *PamLevel2::GetTrackImage(int it){
1593    
 //     if(!trk2_obj) return 0;  
   
 // //  *-*-*-*-*-*-*-*-*-*-*-*-*  
 //     SortTracks("+CAL+TOF");  
 // //  *-*-*-*-*-*-*-*-*-*-*-*-*  
 // //    if(!sorted_tracks)return 0;  
 //     if(sorted_tracks.IsEmpty())return 0;  
       
 //     PamTrack *image = 0;  
       
 //     if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() ){  
 //      TrkTrack *temp = (TrkTrack*)sorted_tracks.At(it);  
 //      if( temp->HasImage() ){  
 //          TrkTrack   *t = trk2_obj->TrkLevel2::GetStoredTrack(temp->GetImageSeqNo());  
 //          image = GetPamTrackAlong(t);  
 //      }else{  
 //          cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;  
 //      };  
 //     }else{  
 //      cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;  
 //     };  
       
 //     return image;  
   
   
1594  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1595      SortTracks("+CAL+TOF");      SortTracks();
1596  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1597      if(!timage)return 0;      if(!timage)return 0;
1598      if(!timage->GetEntries())return 0;      if(!timage->GetEntries())return 0;
# Line 829  PamTrack *PamLevel2::GetTrackImage(int i Line 1627  PamTrack *PamLevel2::GetTrackImage(int i
1627   */   */
1628  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ){  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ){
1629    
1630  //     if( !detlist.IsNull() ) SetWhichTrees(detlist);      if( detlist.Contains("+AUTO", TString::kIgnoreCase) ) {
1631  //     else                    GetWhichTrees(f);          cout << "+AUTO"<<endl;
1632                GetWhichTrees(f);
1633        };
1634        SetWhichTrees(detlist);
1635    
1636        if ( pam_tree ){
1637            printf("WARNING: TTree *PamLevel2::GetPamTree(TFile *fl, TString detlist) -- pam_tree already exists!\n ");
1638            return pam_tree;
1639        };
1640        //
1641    
1642      cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;      cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;
1643    
1644      SetWhichTrees(detlist);  //    SetWhichTrees(detlist);
1645    
1646      TTree *Trout =0;      TTree *Trout =0;
1647    
# Line 855  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1662  TTree *PamLevel2::GetPamTree(TFile *f, T
1662      if(ND)cout<<"ND ";      if(ND)cout<<"ND ";
1663      if(S4)cout<<"S4 ";      if(S4)cout<<"S4 ";
1664      if(ORB)cout<<"ORB ";      if(ORB)cout<<"ORB ";
1665        if(GP)cout<<"GP ";
1666      cout << endl;      cout << endl;
1667      if(SELLI)cout<<">>> Found selection-list <<<"<<endl;      if(SELLI)cout<<">>> Found selection-list <<<"<<endl;
1668    
# Line 907  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1715  TTree *PamLevel2::GetPamTree(TFile *f, T
1715      if(R && TRG) {      if(R && TRG) {
1716          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1717          cout << "Trigger      : set branch address TrigLevel2"<<endl;          cout << "Trigger      : set branch address TrigLevel2"<<endl;
1718          if(!Trout)Trout=O;          if(!Trout)Trout=R;
1719          else Trout->AddFriend(R);          else Trout->AddFriend(R);
1720      }else{      }else{
1721          cout << "Trigger      : missing tree"<<endl;          cout << "Trigger      : missing tree"<<endl;
# Line 917  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1725  TTree *PamLevel2::GetPamTree(TFile *f, T
1725      if(S && S4) {      if(S && S4) {
1726          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1727          cout << "S4           : set branch address S4Level2"<<endl;          cout << "S4           : set branch address S4Level2"<<endl;
1728          if(!Trout)Trout=O;          if(!Trout)Trout=S;
1729          else Trout->AddFriend(S);          else Trout->AddFriend(S);
1730      }else{      }else{
1731          cout << "S4           : missing tree"<<endl;          cout << "S4           : missing tree"<<endl;
# Line 927  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1735  TTree *PamLevel2::GetPamTree(TFile *f, T
1735      if(N && ND) {      if(N && ND) {
1736          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1737          cout << "NeutronD     : set branch address NDLevel2"<<endl;          cout << "NeutronD     : set branch address NDLevel2"<<endl;
1738          if(!Trout)Trout=O;          if(!Trout)Trout=N;
1739          else Trout->AddFriend(N);          else Trout->AddFriend(N);
1740      }else{      }else{
1741          cout << "NeutronD     : missing tree"<<endl;          cout << "NeutronD     : missing tree"<<endl;
# Line 937  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1745  TTree *PamLevel2::GetPamTree(TFile *f, T
1745      if(A && AC) {      if(A && AC) {
1746          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1747          cout << "Anticounter  : set branch address AcLevel2"<<endl;          cout << "Anticounter  : set branch address AcLevel2"<<endl;
1748          if(!Trout)Trout=O;          if(!Trout)Trout=A;
1749          else Trout->AddFriend(A);          else Trout->AddFriend(A);
1750      }else{      }else{
1751          cout << "Anticounter  : missing tree"<<endl;          cout << "Anticounter  : missing tree"<<endl;
# Line 947  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1755  TTree *PamLevel2::GetPamTree(TFile *f, T
1755      if(B && ORB) {      if(B && ORB) {
1756          B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1757          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
1758          if(!Trout)Trout=O;          if(!Trout)Trout=B;
1759          else Trout->AddFriend(B);          else Trout->AddFriend(B);
1760      }else{      }else{
1761          cout << "OrbitalInfo  : missing tree"<<endl;          cout << "OrbitalInfo  : missing tree"<<endl;
1762      };      };
1763    
1764        // GPamela
1765        TTree *G = (TTree*)f->Get("h20");
1766        if(G && GP) {
1767            if(!gp_obj)gp_obj=new GPamela();
1768    //      ------------------------------------
1769    //      ATTENZIONE!!!
1770    //      non so per quale arcano motivo,
1771    //      per l'albero di gpamela il branch address lo devo settare
1772    //      DOPO aver fatto friend
1773    //      FGRRRVZZZZUTSALKJMSLKJ!!!
1774    //      ------------------------------------
1775    //      gp_obj->SetBranchAddress(G); //ho dovuto fare in maniera diversa dagli altri
1776    //      cout << "h20          : set branch address GPamela "<<endl;
1777            if(!Trout)Trout=G;
1778            else Trout->AddFriend(G);
1779        }else{
1780    //      cout << "h20          : missing tree"<<endl;
1781        };
1782    
1783    
1784      TTree *L = (TTree*)f->Get("SelectionList");      TTree *L = (TTree*)f->Get("SelectionList");
1785      if(L && SELLI==1) {      if(L && SELLI==1) {
1786  //      L->SetBranchAddress("RunEntry",&irun);          cout << " TTree *PamLevel2::GetPamTree(TFile, TString) >>> SelectionList not implemented!!!!"<<endl;
 //      cout << "SelectionList: set branch address RunEntry"<<endl;  
 //      L->SetBranchAddress("EventEntry",&irunentry);  
 //      cout << "SelectionList: set branch address EventEntry"<<endl;  
 //      if(!Trout)Trout=O;  
 //      else Trout->AddFriend("SelectionList");  
         cout << " TTree *PamLevel2::GetPamTree(TFile, TString) >>> SelectionList not implemente!!!!"<<endl;  
1787          sel_tree = 0;          sel_tree = 0;
1788      }else{      }else{
1789          cout << "SelectionList  : missing tree"<<endl;          cout << "SelectionList  : missing tree"<<endl;
1790      };      };
1791            
1792      cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;      cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;
       
     pam_tree = (TChain*)Trout;  
1793    
1794    //      ------------------------------------
1795    //      ATTENZIONE!!!
1796    //      non so per quale arcano motivo,
1797    //      per l'albero di gpamela il branch address lo devo settare
1798    //      DOPO aver fatto friend
1799    //      FGRRRVZZZZUTSALKJMSLKJ!!!
1800    //      ------------------------------------
1801        if(G && GP) {      
1802            gp_obj->SetBranchAddress(Trout); //ho dovuto fare in maniera diversa dagli altri
1803            cout << "h20          : set branch address GPamela "<<endl;
1804        }else{
1805            cout << "h20          : missing tree"<<endl;
1806        };
1807    
1808        pam_tree = (TChain*)Trout;
1809        
1810      return Trout;      return Trout;
1811            
1812  }  }
# Line 989  TList*  PamLevel2::GetListOfLevel2Files( Line 1825  TList*  PamLevel2::GetListOfLevel2Files(
1825                    
1826      TString wdir = gSystem->WorkingDirectory();      TString wdir = gSystem->WorkingDirectory();
1827            
1828      if(ddir=="")ddir = wdir;      //    if(ddir=="")ddir = wdir;
1829  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1830      cout << "Level2 data directory : "<<  ddir << endl;      if ( ddir != ""){
1831                cout << "Level2 data directory : "<<  ddir << endl;
1832        } else {
1833            cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory " << endl;
1834        };
1835      TList *contents  = new TList; // create output list      TList *contents  = new TList; // create output list
1836      contents->SetOwner();      contents->SetOwner();
1837            
# Line 1008  TList*  PamLevel2::GetListOfLevel2Files( Line 1847  TList*  PamLevel2::GetListOfLevel2Files(
1847  //          return 0;  //          return 0;
1848  //      }        //      }      
1849  //      flisttxt = fullpath;  //      flisttxt = fullpath;
1850            if ( !flisttxt.EndsWith(".root") ){
1851    
1852          flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));              flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1853    
1854          if( !gSystem->ChangeDirectory(ddir) ){              if( !gSystem->ChangeDirectory(ddir) ){
1855              cout << "Cannot change directory : "<<ddir<<endl;                  cout << "Cannot change directory : "<<ddir<<endl;
1856              return 0;                  return 0;
         }  
           
         cout <<"Input file list : " << flisttxt <<endl;  
         ifstream in;  
         in.open(flisttxt, ios::in); //open input file list  
         if(!in.good()){  
             cout <<" ERROR opening the file "<<endl;  
             gSystem->ChangeDirectory(wdir); // back to the working directory  
             return 0;  
         }        
         int line=0;  
         while (1) {  
             TString file;  
             in >> file;  
             if (!in.good()) break;  
             line++;  
 //          cout <<"(1) " << file << endl;  
             if(file.IsNull()){  
                 cout << "-- list interrupted at line "<<line <<endl;  
                 break;  
1857              }              }
1858              if(file.Contains("#"))file = file(0,file.First("#"));          
1859                cout <<"Input file list : " << flisttxt <<endl;
1860                ifstream in;
1861                in.open(flisttxt, ios::in); //open input file list
1862                if(!in.good()){
1863                    cout <<" ERROR opening the file "<<endl;
1864                    gSystem->ChangeDirectory(wdir); // back to the working directory
1865                    return 0;
1866                }  
1867                int line=0;
1868                while (1) {
1869                    TString file;
1870                    in >> file;
1871                    if (!in.good()) break;
1872                    line++;
1873    //          cout <<"(1) " << file << endl;
1874                    if(file.IsNull()){
1875                        cout << "-- list interrupted at line "<<line <<endl;
1876                        break;
1877                    }
1878                    if(file.Contains("#"))file = file(0,file.First("#"));
1879  //          cout <<"(2) " << file << endl;  //          cout <<"(2) " << file << endl;
1880  //          if( gSystem->IsFileInIncludePath(file,&fullpath) ){  //          if( gSystem->IsFileInIncludePath(file,&fullpath) ){
1881  //          if( (fullpath = gSystem->FindFile(ddir,file)) ){  //          if( (fullpath = gSystem->FindFile(ddir,file)) ){
1882              if( file.EndsWith(".root") ){                  if( file.EndsWith(".root") ){
1883                  char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir),gSystem->BaseName(file));                      TString filedir;
1884                  contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list                      if (ddir != ""){
1885                  delete fullpath;                          filedir = ddir; // take the input dir
1886              }                      } else {
1887                            gSystem->ChangeDirectory(wdir); // back to the working directory
1888                            filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir
1889                        };
1890                        char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir),gSystem->BaseName(file));
1891                        contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list
1892                        delete fullpath;
1893                    }
1894  //          }else{  //          }else{
1895  //              if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;  //              if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;
1896  //          };  //          };
1897          };              };
1898          in.close();              in.close();
1899                    } else {
1900                if(flisttxt.Contains("#"))flisttxt = flisttxt(0,flisttxt.First("#"));
1901                char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1902                contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list
1903                delete fullpath;
1904            };      
1905      }else{      }else{
1906                    
1907          cout << "No input file list given."<<endl;          cout << "No input file list given."<<endl;
1908          cout << "Check for existing root files."<<endl;          cout << "Check for existing root files."<<endl;
1909  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;
1910                    if ( ddir == "" ){
1911                ddir = wdir;
1912                cout << "Level2 data directory : "<<  ddir << endl;
1913            };
1914    
1915          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1916          TList *temp = datadir->GetListOfFiles();          TList *temp = datadir->GetListOfFiles();
1917  //              temp->Print();  //              temp->Print();
# Line 1099  TList*  PamLevel2::GetListOfLevel2Files( Line 1955  TList*  PamLevel2::GetListOfLevel2Files(
1955   * @return Pointer to a TChain   * @return Pointer to a TChain
1956   */   */
1957  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){
1958        //
1959        //
1960        //
1961        if ( pam_tree ){
1962            printf("WARNING: TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) -- pam_tree already exists!\n ");
1963            return pam_tree;
1964        };
1965        //
1966            
1967      TChain *Trout =0;      TChain *Trout =0;
1968    
1969  //    if( !detlist.IsNull() )SetWhichTrees(detlist);      // -------------------------------------------
1970      SetWhichTrees(detlist);      // set flags to include/exclude trees/branches
1971            // -------------------------------------------
1972      cout<< "GetPamTree(TList*,TString): input detector list --> ";      if( detlist.Contains("+AUTO", TString::kIgnoreCase) ) {
1973      if(TRK1)cout<<"TRK1 ";          if( fl->GetEntries()>0 ){
1974      if(TRK2)cout<<"TRK2 ";              cout << "+AUTO"<<endl;
1975      if(TRKh)cout<<"TRKH ";              TFile *fprimo = new TFile( fl->At(0)->GetName() );
1976      if(CAL1)cout<<"CAL1 ";              GetWhichTrees(fprimo);
1977      if(CAL2)cout<<"CAL2 ";              fprimo->Close();// AAAAARGGGGGHHHHH!!!!!!! non commentare questa riga, altrimenti si incasina il TChain
1978      if(TOF)cout<<"TOF ";              fprimo->Delete();
1979      if(TRG)cout<<"TRG ";          }
1980      if(AC)cout<<"AC ";      };
1981      if(ND)cout<<"ND ";      SetWhichTrees(detlist);    
1982      if(S4)cout<<"S4 ";  
     if(ORB)cout<<"ORB ";  
     cout << endl;  
1983    
1984        // -------------------------------------------
1985        // build chains
1986        // -------------------------------------------
1987      TChain *T = 0;            TChain *T = 0;      
1988      TChain *C = 0;      TChain *C = 0;
1989      TChain *O = 0;      TChain *O = 0;
# Line 1127  TChain *PamLevel2::GetPamTree(TList *fl, Line 1992  TChain *PamLevel2::GetPamTree(TList *fl,
1992      TChain *N = 0;      TChain *N = 0;
1993      TChain *A = 0;      TChain *A = 0;
1994      TChain *B = 0;      TChain *B = 0;
1995        TChain *G = 0;
1996    
1997      TChain *L = 0;      TChain *L = 0;
1998            
# Line 1138  TChain *PamLevel2::GetPamTree(TList *fl, Line 2004  TChain *PamLevel2::GetPamTree(TList *fl,
2004      if(ND)               N = new TChain("NeutronD");      if(ND)               N = new TChain("NeutronD");
2005      if(AC)               A = new TChain("Anticounter");      if(AC)               A = new TChain("Anticounter");
2006      if(ORB)              B = new TChain("OrbitalInfo");      if(ORB)              B = new TChain("OrbitalInfo");
2007        if(GP)               G = new TChain("h20");
2008       L = new TChain("SelectionList");      L = new TChain("SelectionList");
2009            
2010      // loop over files and create chains              // loop over files and create chains        
2011      TIter next(fl);      TIter next(fl);
# Line 1156  TChain *PamLevel2::GetPamTree(TList *fl, Line 2022  TChain *PamLevel2::GetPamTree(TList *fl,
2022              if(ND)               N->Add(name);              if(ND)               N->Add(name);
2023              if(AC)               A->Add(name);              if(AC)               A->Add(name);
2024              if(ORB)              B->Add(name);              if(ORB)              B->Add(name);
2025                if(GP)               G->Add(name);
2026              if(SELLI==1)         L->Add(name);              if(SELLI==1)         L->Add(name);
2027          };          };
2028      }      };
2029            
2030      cout << "done chain \n";      cout << "done chains\n";
2031        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2032    
2033    
2034        // -------------------------------------------
2035        // make friends
2036        // -------------------------------------------
2037    
 //    UInt_t *found=0;  
2038  // Tracker  // Tracker
2039        cout << "Friends: "<<endl;
2040      if(T && (TRK2||TRK1||TRKh)) {      if(T && (TRK2||TRK1||TRKh)) {
         if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));  
 //      else    T->SetBranchStatus("TrkLevel2",0,found);  
         if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;  
         if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));  
 //      else    T->SetBranchStatus("TrkLevel1",0,found);  
         if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;  
         if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));  
 //      else    T->SetBranchStatus("TrkHough",0,found);  
         if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;  
2041          if(!Trout)Trout=T;          if(!Trout)Trout=T;
2042          else Trout->AddFriend("Tracker");          else Trout->AddFriend("Tracker");
2043      }else{  //      cout << "+Tacker"<<endl;
         cout << "Tracker      : missing tree"<<endl;  
2044      };      };
2045      // Calorimeter      // Calorimeter
2046      if(C && (CAL2||CAL1)) {      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;  
2047          if(!Trout)Trout=C;          if(!Trout)Trout=C;
2048          else Trout->AddFriend("Calorimeter");          else Trout->AddFriend("Calorimeter");
2049      }else{  //      cout << "+Calorimeter"<<endl;
         cout << "Calorimeter  : missing tree"<<endl;  
2050      };      };
2051      // ToF          // ToF    
2052      if(O && TOF) {      if(O && TOF) {
         O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));  
         cout << "ToF          : set branch address ToFLevel2"<<endl;  
2053          if(!Trout)Trout=O;          if(!Trout)Trout=O;
2054          else Trout->AddFriend("ToF");          else Trout->AddFriend("ToF");
2055      }else{  //      cout << "+ToF"<<endl;
         cout << "ToF         : missing tree"<<endl;  
2056      };      };
2057      // Trigger      // Trigger
2058      if(R && TRG) {      if(R && TRG) {
         R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));  
         cout << "Trigger      : set branch address TrigLevel2"<<endl;  
2059          if(!Trout)Trout=O;          if(!Trout)Trout=O;
2060          else Trout->AddFriend("Trigger");          else Trout->AddFriend("Trigger");
2061      }else{  //      cout << "+Trigger"<<endl;
         cout << "Trigger      : missing tree"<<endl;  
2062      };      };
2063      // S4      // S4
2064      if(S && S4) {      if(S && S4) {
         S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));  
         cout << "S4           : set branch address S4Level2"<<endl;  
2065          if(!Trout)Trout=O;          if(!Trout)Trout=O;
2066          else Trout->AddFriend("S4");          else Trout->AddFriend("S4");
2067      }else{  //      cout << "+S4"<<endl;
         cout << "S4           : missing tree"<<endl;  
2068      };      };
2069      // Neutron Detector      // Neutron Detector
2070      if(N && ND) {      if(N && ND) {
         N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));  
         cout << "NeutronD     : set branch address NDLevel2"<<endl;  
2071          if(!Trout)Trout=O;          if(!Trout)Trout=O;
2072          else Trout->AddFriend("NeutronD");          else Trout->AddFriend("NeutronD");
2073      }else{  //      cout << "+NeutronD"<<endl;
         cout << "NeutronD     : missing tree"<<endl;  
2074      };      };
2075      // Anticounters      // Anticounters
2076      if(A && AC) {      if(A && AC) {
         A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));  
         cout << "Anticounter  : set branch address AcLevel2"<<endl;  
2077          if(!Trout)Trout=O;          if(!Trout)Trout=O;
2078          else Trout->AddFriend("Anticounter");          else Trout->AddFriend("Anticounter");
2079      }else{  //      cout << "+Anticounter"<<endl;
         cout << "Anticounter  : missing tree"<<endl;  
2080      };      };
2081      // Orbital Info      // Orbital Info
2082      if(B && ORB) {      if(B && ORB) {
         B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));  
         cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;  
2083          if(!Trout)Trout=O;          if(!Trout)Trout=O;
2084          else Trout->AddFriend("OrbitalInfo");          else Trout->AddFriend("OrbitalInfo");
2085      }else{  //      cout << "+OrbitalInfo"<<endl;
         cout << "OrbitalInfo  : missing tree"<<endl;  
2086      };      };
2087        // GPamela
2088        if(G && GP) {
2089            if(!Trout)Trout=G;
2090            else Trout->AddFriend("h20");
2091    //      cout << "+h20"<<endl;
2092        };
2093    
2094    //  =====================================
2095    //  SET BRANCH-ADDRESS AFTER CHAIN+FRIEND
2096    //  =====================================
2097        SetBranchAddress(Trout);
2098    
2099    
2100    //  ------------------------------------
2101    //  finally handle selection trees...
2102    //  (it is not friend of pamela tree)
2103    //  ------------------------------------
2104    
2105        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2106    
2107      // Selection List      // Selection List
2108      if(L && SELLI==1) {      if(L && SELLI==1) {
2109          cout<<">>> Found selection-list <<<"<<endl;          cout<<">>> Found selection-list <<<"<<endl;
2110          L->SetBranchAddress("RunEntry",&irun);  //      L->SetBranchAddress("RunEntry",&irun);
2111            L->SetBranchAddress("RunEntry",&irunt);//NEWNEW
2112          cout << "SelectionList: set branch address RunEntry"<<endl;          cout << "SelectionList: set branch address RunEntry"<<endl;
2113          L->SetBranchAddress("EventEntry",&irunentry);          L->SetBranchAddress("EventEntry",&irunentry);
2114          cout << "SelectionList: set branch address EventEntry"<<endl;          cout << "SelectionList: set branch address EventEntry"<<endl;
2115          sel_tree = L;          sel_tree = L;
2116  //      if(!Trout)Trout=O;  //      if(!Trout)Trout=O;
2117  //      else Trout->AddFriend("SelectionList");  //      else Trout->AddFriend("SelectionList");
2118            cout << "+SelectionList"<<endl;
2119            cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2120      }else{      }else{
2121  //      cout << "SelectionList  : missing tree"<<endl;  //      cout << "SelectionList  : missing tree"<<endl;
2122          if(L)L->Delete();          if(L)L->Delete();
2123      };      };
       
 //    cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;  
2124    
2125      pam_tree = Trout;  //  --------------------------------------------
2126    //  return the pamela chain with all the friends
2127    //  --------------------------------------------
2128    
2129        pam_tree = Trout;
2130        
2131      return Trout;      return Trout;
2132  }  }
2133    
# Line 1291  void PamLevel2::SetBranchAddress(TTree * Line 2153  void PamLevel2::SetBranchAddress(TTree *
2153      ND     = ND  & t->GetBranchStatus("NDLevel2");      ND     = ND  & t->GetBranchStatus("NDLevel2");
2154      AC     = AC  & t->GetBranchStatus("AcLevel2");      AC     = AC  & t->GetBranchStatus("AcLevel2");
2155      ORB    = ORB & t->GetBranchStatus("OrbitalInfo");      ORB    = ORB & t->GetBranchStatus("OrbitalInfo");
2156        GP     = GP  & t->GetBranchStatus("h20");
2157    
2158    
2159      // Tracker      // Tracker
# Line 1347  void PamLevel2::SetBranchAddress(TTree * Line 2210  void PamLevel2::SetBranchAddress(TTree *
2210          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
2211          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
2212      };      };
2213      // SelectionList      // GPamela
2214      if(SELLI==1) {      if(GP) {
2215          t->SetBranchAddress("RunEntry", &irun);  //      GetPointerTo("GPamela");
2216          cout << "SelectionList: set branch address RunEntry"<<endl;          if(!gp_obj)gp_obj = new GPamela();
2217          t->SetBranchAddress("EventEntry", &irunentry);  //      gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2218          cout << "SelectionList: set branch address EventEntry"<<endl;  // //   t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
2219            if(SELLI) t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
2220            else      gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2221    
2222            cout << "h20          : set branch address GPamela "<<endl;
2223      };      };
2224            
2225  }  }
2226  /**  /**
2227   * Set branch addresses for Pamela friend trees   * Set branch addresses for Pamela friend trees
2228   */   */
2229  void PamLevel2::SetBranchAddress(TChain *t){  void PamLevel2::SetBranchAddress(TChain *t){
2230    
2231      TRK2    = TRK2 & t->GetBranchStatus("TrkLevel2");  //     TRK2    = TRK2 & t->GetBranchStatus("TrkLevel2");
2232      TRK1    = TRK1 & t->GetBranchStatus("TrkLevel1");  //     TRK1    = TRK1 & t->GetBranchStatus("TrkLevel1");
2233      TRKh    = TRKh & t->GetBranchStatus("TrkHough");  //     TRKh    = TRKh & t->GetBranchStatus("TrkHough");
2234      CAL1    = CAL1 & t->GetBranchStatus("CaloLevel1");  //     CAL1    = CAL1 & t->GetBranchStatus("CaloLevel1");
2235      CAL2    = CAL2 & t->GetBranchStatus("CaloLevel2");  //     CAL2    = CAL2 & t->GetBranchStatus("CaloLevel2");
2236      TOF    = TOF & t->GetBranchStatus("ToFLevel2");  //     TOF    = TOF & t->GetBranchStatus("ToFLevel2");
2237      TRG    = TRG & t->GetBranchStatus("TrigLevel2");  //     TRG    = TRG & t->GetBranchStatus("TrigLevel2");
2238      S4     = S4  & t->GetBranchStatus("S4Level2");  //     S4     = S4  & t->GetBranchStatus("S4Level2");
2239      ND     = ND  & t->GetBranchStatus("NDLevel2");  //     ND     = ND  & t->GetBranchStatus("NDLevel2");
2240      AC     = AC  & t->GetBranchStatus("AcLevel2");  //     AC     = AC  & t->GetBranchStatus("AcLevel2");
2241      ORB    = ORB & t->GetBranchStatus("OrbitalInfo");  //     ORB    = ORB & t->GetBranchStatus("OrbitalInfo");
2242    //    GP     = GP & t->GetBranchStatus("h20");
2243    
2244      // Tracker      // Tracker
2245       if(TRK2) {      if(TRK2) {
2246          t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));          t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
2247          cout << "Tracker      : set branch address TrkLevel2"<<endl;          cout << "Tracker      : set branch address TrkLevel2"<<endl;
2248      };      };
# Line 1382  void PamLevel2::SetBranchAddress(TChain Line 2250  void PamLevel2::SetBranchAddress(TChain
2250          t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));          t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
2251          cout << "Tracker      : set branch address TrkLevel1"<<endl;          cout << "Tracker      : set branch address TrkLevel1"<<endl;
2252      };      };
2253       if(TRKh) {      if(TRKh) {
2254          t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));          t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
2255          cout << "Tracker      : set branch address TrkHough"<<endl;          cout << "Tracker      : set branch address TrkHough"<<endl;
2256       };      };
2257            
2258      // Calorimeter      // Calorimeter
2259      if(CAL2) {      if(CAL2) {
# Line 1427  void PamLevel2::SetBranchAddress(TChain Line 2295  void PamLevel2::SetBranchAddress(TChain
2295          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
2296          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
2297      };      };
2298      // SelectionList      // GPamela
2299      if(SELLI==1) {  //    cout <<"GP "<<GP<<endl;
2300          t->SetBranchAddress("RunEntry", &irun);      if(GP) {
2301          cout << "SelectionList: set branch address RunEntry"<<endl;  //      GetPointerTo("GPamela");
2302          t->SetBranchAddress("EventEntry", &irunentry);          if(!gp_obj)gp_obj = new GPamela();
2303          cout << "SelectionList: set branch address EventEntry"<<endl;          if(SELLI) t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
2304            else      gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2305    //      gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2306            cout << "h20          : set branch address GPamela "<<endl;
2307      };      };
2308            // SelectionList
2309    //     if(SELLI==1){
2310    //      t->SetBranchAddress("RunEntry",&irunt);//NEWNEW
2311    //      cout << "SelectionList: set branch address RunEntry"<<endl;
2312    //      t->SetBranchAddress("EventEntry",&irunentry);
2313    //      cout << "SelectionList: set branch address EventEntry"<<endl;
2314            
2315    //     }
2316    
2317  }  }
2318    
2319    
# Line 1448  void PamLevel2::SetBranchAddress(TChain Line 2327  void PamLevel2::SetBranchAddress(TChain
2327   * @return Pointer to a TChain   * @return Pointer to a TChain
2328   */   */
2329  TChain *PamLevel2::GetRunTree(TList *fl){  TChain *PamLevel2::GetRunTree(TList *fl){
2330                //
2331        //
2332        //
2333        if ( run_tree ){
2334            printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");
2335            return run_tree;
2336        };  
2337        //
2338    
2339    
2340      TChain *R = new TChain("Run");      TChain *R = new TChain("Run");
2341            
2342      // loop over files and create chains              // loop over files and create chains        
# Line 1461  TChain *PamLevel2::GetRunTree(TList *fl) Line 2349  TChain *PamLevel2::GetRunTree(TList *fl)
2349              R->Add(name);              R->Add(name);
2350          };          };
2351      }      }
2352        
2353    
2354      if(R->GetNtrees()){      if(RUN && R->GetNtrees()){
2355            
2356          R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));          R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
2357          cout << "Run         : set branch address RunInfo"<<endl;          cout << "Run         : set branch address RunInfo"<<endl;
2358          R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano          R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
2359          cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano          cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano
2360    
2361            irunoffset = new int[R->GetNtrees()];
2362            if(DBG){
2363                cout << "----------------------------------------------------"<<endl;
2364                cout << "irun\t | ";
2365                cout << "tree\t |";
2366    //      cout << "offset\t |";
2367                cout << "RUN\t";
2368                cout << "FRAG\t";
2369                cout << "NEVENTS\t";
2370                cout << "absolute time\t\t\t";
2371                cout << "on-board time";
2372                cout<<endl;
2373            }
2374            for (Int_t ii=0; ii<R->GetEntries(); ii++){
2375                R->GetEntry(ii);
2376                if(DBG){
2377                    cout << ii<< "\t | ";
2378                    cout << R->GetTreeNumber()<< "\t |";
2379    //          cout << R->GetChainOffset()<< "\t |";
2380                    cout <<GetRunInfo()->ID<<"\t";
2381                    cout <<GetRunInfo()->ID_RUN_FRAG<<"\t";
2382                    cout <<GetRunInfo()->NEVENTS<< "\t";
2383                    cout <<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME <<"\t";
2384                    cout <<GetRunInfo()->RUNHEADER_OBT<<" <---> "<<GetRunInfo()->RUNTRAILER_OBT<<"\t";
2385                    cout <<endl;
2386                }
2387                irunoffset[R->GetTreeNumber()]=R->GetChainOffset();
2388            }
2389            cout << "N.run = "<<R->GetEntries()<<endl;
2390            cout << "----------------------------------------------------"<<endl;
2391    
2392    
2393      }else{      }else{
2394          delete R;          delete R;
2395          R=0;          R=0;
2396      }      }
2397        
2398    
2399      run_tree = R;      run_tree = R;
2400    
# Line 1488  TChain *PamLevel2::GetRunTree(TList *fl) Line 2411  TChain *PamLevel2::GetRunTree(TList *fl)
2411   * @return Pointer to a TTree   * @return Pointer to a TTree
2412   */   */
2413  TTree *PamLevel2::GetRunTree(TFile *f){  TTree *PamLevel2::GetRunTree(TFile *f){
2414        if ( run_tree ){
2415            printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");
2416            return run_tree;
2417        };
2418            
2419    
2420      cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;      cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;
2421    
2422      TTree *R = (TTree*)f->Get("Run");      TTree *T = (TTree*)f->Get("Run");
2423            
2424      if(R){      if(T){
2425          R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));          T->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
2426          cout << "Run         : set branch address RunInfo"<<endl;          cout << "Run         : set branch address RunInfo"<<endl;
2427          R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano          T->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
2428          cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano          cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano
2429    
2430      }      }
2431    
2432      run_tree = (TChain*)R;      run_tree = (TChain*)T;
2433    
2434      return R;      return T;
2435            
2436  }  }
2437  /**  /**
# Line 1510  TTree *PamLevel2::GetRunTree(TFile *f){ Line 2439  TTree *PamLevel2::GetRunTree(TFile *f){
2439   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
2440   * @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
2441   */   */
2442  Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev){  Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev){
2443    //      //
2444    // check if we have already called once GetEntry, if not call it      // check if we have already called once GetEntry, if not call it
2445    //      //
2446    if ( run->GetEntries() <= 0 ) return(false);      cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) --- ATTENZIONE --- NON E` MANTENUTA!!!!!!!.... "<<endl;
2447    //      if(!run){
2448            cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing RunInfo tree "<<endl;
2449            return(false);  
2450        }
2451        if ( run->GetEntries() <= 0 ) return(false);
2452        //
2453        
2454    Int_t oldrun = irun;      //  Int_t oldrun = irun;
2455    // --------------------------------------      Long64_t oldrun = irun;
   // if it is a full file (not preselected)  
   // --------------------------------------  
   if(SELLI==0){  
       if ( irun < 0 ){  
           irun = 0;  
           run->GetEntry(0);  
           runfirstentry = 0ULL;  
           runlastentry += (ULong64_t)(this->GetRunInfo()->NEVENTS) - 1ULL;  
       };  
       while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){  
           //    printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));  
           irun++;  
           run->GetEntry(irun);  
           runfirstentry = runlastentry+1ULL;  
           runlastentry += (ULong64_t)(this->GetRunInfo()->NEVENTS);  
       };  
       //  
       if ( irun == oldrun || irun >= run->GetEntries() ) return(false);  
       //  
       //  printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);  
       //  
       return(true);      
   };  
   // ----------------------------------------------------  
   // if it is a preselected file (there is SelectionList)  
   // NBNB - the event tree MUST be read first  
   // ----------------------------------------------------  
   if(SELLI==1){        
       sel_tree->GetEntry(iev);  
       if(irun != oldrun){  
           run->GetEntry(irun);  
           return true;  
       }  
       return false;  
   }  
2456    
2457    return false;      // --------------------------------------
2458    //      // if it is a full file (not preselected)
2459        // --------------------------------------
2460        if(SELLI==0){
2461    
2462            //
2463            // the absolute time is necessary to relate the event with the run
2464            //
2465            if( !GetOrbitalInfo() && !ISGP){
2466                cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
2467                return(false);
2468            }
2469    
2470            ULong64_t abstime = 0;
2471            if( GetOrbitalInfo() )abstime=GetOrbitalInfo()->absTime;
2472    
2473    
2474            //
2475            // the first time the routine is called, set run search from the beginning
2476            //
2477            if ( irun < 0LL ){
2478                irun = 0LL;
2479                run->GetEntry(irun);
2480                runfirstentry = 0LL;
2481                runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2482                if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
2483              
2484                if( ISGP && run->GetEntries()!=1 ){
2485                    cout << "** WARNING ** simulated files are assumed to have 1 single run, not "<< run->GetEntries() << endl;
2486                    cout << "** WARNING ** run will not be updated"<<endl;
2487                }
2488    
2489            };      
2490            //
2491            if(ISGP)abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO
2492            //
2493            if ( irun == run->GetEntries()-1LL &&
2494                 !(abstime >= GetRunInfo()->RUNHEADER_TIME &&
2495                   abstime <= GetRunInfo()->RUNTRAILER_TIME)
2496                ){
2497                irun = -1LL;
2498                irunt = -1LL;
2499                runfirstentry = 0LL;
2500                runlastentry = -1LL;
2501            };
2502            // modificato il controllo sull'aggiornamento del run, per evitare problemi
2503            // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)
2504            //
2505            bool fromfirst = true;
2506            //
2507            while ( !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME) && irun < run->GetEntries()-1LL ){
2508    //      while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){
2509                irun++;
2510                run->GetEntry(irun);
2511                runfirstentry = runlastentry;
2512                if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runfirstentry += 1LL;
2513                runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2514    //        cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2515    //        cout << "runfirstentry "<<runfirstentry<<endl;
2516                //    printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));
2517                //    printf(" abstime %u trailertime %u \n",abstime,GetRunInfo()->RUNTRAILER_TIME);
2518                //    printf(" IDRUN %u \n",GetRunInfo()->ID);
2519                //
2520    //        prevshift = 0;
2521                //
2522                if ( irun == (Long64_t)(run->GetEntries()-1LL) && fromfirst && !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME)){
2523                    printf(" resetting irun  (it should NOT happen!!!)\n");
2524                    fromfirst = false;
2525                    irun = 0;
2526                    run->GetEntry(irun);
2527                    runfirstentry = 0ULL;
2528                    runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2529                    if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
2530                };
2531                //
2532            };
2533            //
2534            if (
2535                !(abstime >= GetRunInfo()->RUNHEADER_TIME &&
2536                  abstime <= GetRunInfo()->RUNTRAILER_TIME)
2537                ) {
2538                printf(" Something very wrong here: cannot find RUN containing absolute time %llu \n",abstime);
2539                return false;
2540            }
2541            //
2542            if ( irun == oldrun || irun >= run->GetEntries() ) return(false);
2543            //
2544            //  printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);
2545            //
2546            prevshift = 0;
2547            cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2548    //      cout << "runfirstentry "<<runfirstentry<<endl;
2549            return(true);    
2550        };
2551        // ----------------------------------------------------
2552        // if it is a preselected file (there is SelectionList)
2553        // NBNB - the event tree MUST be read first
2554        // ----------------------------------------------------
2555        if(SELLI==1){      
2556            sel_tree->GetEntry(iev);
2557    //      cout << irun << " "<< irunentry << endl;
2558            if(irun != oldrun){
2559                run->GetEntry(irun);
2560                cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2561                prevshift = 0;
2562                return true;
2563            }
2564            return false;
2565        }
2566    
2567        return false;
2568        //
2569    };
2570    
2571    Bool_t PamLevel2::UpdateRunInfo(Long64_t iev){
2572    
2573        if(!run_tree){
2574            cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded"<<endl;
2575            return false;
2576        }
2577        if ( run_tree->GetEntries() <= 0 ) {
2578            cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty"<<endl;
2579            return(false);
2580        }
2581      
2582        Int_t oldrun = irun; // store current run index
2583    
2584        // -----------------------------------------------------------------------
2585        // the first time the routine is called, set run search from the beginning
2586        // -----------------------------------------------------------------------
2587    
2588        if ( irun < 0 ){
2589            irun = 0LL;
2590            irunt = 0LL;
2591            irunentry = 0;
2592            prevshift = 0;
2593            run_tree->GetEntry(irun);
2594            if( !GetOrbitalInfo() )cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated "<<endl;
2595        };      
2596        // ---------------------------------------------------------------
2597        // retrieve OBT and absolute time of the event
2598        // ---------------------------------------------------------------
2599        ULong64_t abstime = 0LL;
2600        ULong64_t obt     = 0LL;
2601        if( GetOrbitalInfo() ){
2602            abstime = GetOrbitalInfo()->absTime;
2603            obt     = GetOrbitalInfo()->OBT;
2604        }else{
2605            abstime = GetRunInfo()->RUNHEADER_TIME;
2606            obt     = GetRunInfo()->RUNHEADER_OBT;
2607        }
2608        
2609    
2610        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2611        // if it is a full file (not preselected)
2612        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2613        if(SELLI==0){
2614    
2615            // ---------------------------------------------------------------
2616            // increment dead and live-time counters
2617            // (only when reading a file not preselected)
2618            // ---------------------------------------------------------------
2619            if(SELLI==0){
2620                if( GetTrigLevel2() ){
2621                    totdltime[0]+=GetTrigLevel2()->dltime[0];
2622                    totdltime[1]+=GetTrigLevel2()->dltime[1];
2623                }
2624                totdltime[2]++;
2625            }
2626    
2627            //
2628            bool fromfirst = true; // first loop over runs
2629    
2630    
2631            // ------------------------------------------------------
2632            // loop over runs to find the one that contains the event
2633            // ------------------------------------------------------
2634            while (
2635                (
2636    //              (
2637    //                  !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)
2638    //                    abstime <= GetRunInfo()->RUNTRAILER_TIME) &&
2639    //                  !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)
2640    //                    obt <= GetRunInfo()->RUNTRAILER_OBT)
2641    //                  )
2642                    
2643                    !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)
2644                      abstime <= GetRunInfo()->RUNTRAILER_TIME)
2645                    ||
2646                    !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)
2647                      obt <= GetRunInfo()->RUNTRAILER_OBT)          
2648                    ||
2649                    GetRunInfo()->NEVENTS==0
2650    //              || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift) // ERRORE!!! fa saltare i run con 1 evento
2651                    ||
2652                    !(irunentry <= GetRunInfo()->NEVENTS-1-prevshift)
2653                    )
2654                && irun < run_tree->GetEntries() ){
2655    
2656    
2657    //          if( !(abstime >= GetRunInfo()->RUNHEADER_TIME &&abstime <= GetRunInfo()->RUNTRAILER_TIME)  )cout << "ABS TIME "<<abstime << " " <<GetRunInfo()->RUNTRAILER_TIME <<endl;
2658    //          if( !(obt >= GetRunInfo()->RUNHEADER_OBT && obt <= GetRunInfo()->RUNTRAILER_OBT) )cout << "OBT TIME "<< obt <<" "<< GetRunInfo()->RUNTRAILER_OBT << endl;
2659    //          if( GetRunInfo()->NEVENTS==0  )cout <<"GetRunInfo()->NEVENTS==0 "<<endl;
2660    //          if( !(irunentry <= GetRunInfo()->NEVENTS-1-prevshift) ) cout << "irunentry > "<<GetRunInfo()->NEVENTS-1-prevshift << endl;
2661                // - - - - - - - - - - - - -
2662                // irunentry = position of current entry, relative to the run
2663                // prevshift = shift needed to synchronize l0 and l2 data (nested events)
2664                // - - - - - - - - - - - - -
2665    
2666                // -----------------------------------------
2667                // store dead and live-time of previous run
2668                // -----------------------------------------
2669    //          if(SELLI==0){
2670                if(fromfirst){
2671                    if(oldrun==irun){
2672                        /// decrement counters
2673                        if( GetTrigLevel2()){
2674                            totdltime[0]-=GetTrigLevel2()->dltime[0];//live-time
2675                            totdltime[1]-=GetTrigLevel2()->dltime[1];//dead-time
2676                        }
2677                        totdltime[2]--;                              //event counter
2678                        cout << endl;
2679                        cout << "n.events     : "<<totdltime[2]<<endl;
2680                        cout << "RUN LIVE-TIME: "<<totdltime[0]*0.16<<" ms"<<endl;
2681                        cout << "RUN DEAD-TIME: "<<totdltime[1]*0.01<<" ms"<<endl;      
2682                    }else{
2683                        totdltime[0]=0;//live-time
2684                        totdltime[1]=0;//dead-time
2685                        totdltime[2]=0;                             //event counter
2686                        cout << " *** JUMP RUN *** irun "<<irun<<endl;
2687                    }
2688                    /// add an entry
2689                    if(run_tree_clone)
2690                        if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
2691                            run_tree_clone->GetBranch("DeadLiveTime")->Fill();
2692                    /// reset counters
2693                    if( GetTrigLevel2() ){
2694                        totdltime[0]=GetTrigLevel2()->dltime[0];//live-time
2695                        totdltime[1]=0;                         //dead-time
2696                    }
2697                    totdltime[2]=1;                             //event counter
2698                }
2699    //          }
2700    
2701                irun++;        
2702                // ------------------------------------
2703                // if the end of run tree is reached...
2704                // ------------------------------------
2705                if( irun == run_tree->GetEntries() ){
2706                    if(!fromfirst){
2707                        // -----------------------------------------------------
2708                        // if it happened already once and the run was not found
2709                        // ---> exit with error
2710                        // -----------------------------------------------------
2711                        cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- event entry #"<<iev<<" does not belong to any run (should not happen)" <<endl;
2712                        return false;
2713                    }
2714                    // -----------------------------------------
2715                    // ...otherwise repeat search from beginning
2716                    // -----------------------------------------
2717                    cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- reached end of run tree. searchin again from beginning " <<endl;
2718                    fromfirst = false;
2719                    irun = 0LL;
2720                    runfirstentry = 0LL;
2721                }
2722                // -------------------------------------------------------------------
2723                // save the index of the first entry of the run, relative to pam_tree,
2724                // and read a new run
2725                // -------------------------------------------------------------------
2726                if(irun>0)runfirstentry += (GetRunInfo()->NEVENTS)-prevshift;
2727                irunentry = 0;
2728                prevshift = 0;          
2729                run_tree->GetEntry(irun);//update runinfo
2730                irunt = irun - irunoffset[run_tree->GetTreeNumber()];
2731                if(GetRunInfo()->RUNHEADER_OBT>GetRunInfo()->RUNTRAILER_OBT ){
2732                    cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun "<<irun<<"  has RUNHEADER_OBT>=RUNTRAILER_OBT " <<endl;
2733                    cout << "                                                            (NB!! in this case some events are assigned to a wrong run)"<<endl;
2734                }
2735    //          if(hasfrag &&  fragid != GetRunInfo()->ID){
2736    //              cout << "... where is the next fragment ??"<<endl;
2737    //          }
2738            };
2739    
2740    
2741            // --------------------------------------
2742            // if there was no need to update the run
2743            // ---> exit with FALSE
2744            // --------------------------------------
2745            if ( irun == oldrun ) return(false);
2746    
2747            // --------------------------------------
2748            // ... otherwise
2749            // --------------------------------------
2750    
2751    
2752            // --------------------------------------
2753            // ---> exit with TRUE
2754            // --------------------------------------
2755            if(DBG)cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2756            // ----------------------------------------------------
2757            // update the tracker parameters
2758            // (non ho trovato nessun altro modo sicuro di farlo...)
2759            // ----------------------------------------------------
2760            if(!dbc || (dbc && !dbc->IsConnected()) )SetDBConnection();
2761            TrkParams::Set(GetRunInfo() ,dbc);
2762            if(dbc)dbc->Close();
2763    
2764            // ----------------------------------------------------
2765            // then check if the run has a fragment
2766            // in this case we have to switch to the next fragment
2767            // when the end of the first fragment is reached
2768            // ----------------------------------------------------
2769            if(
2770                GetRunInfo()->ID_RUN_FRAG != 0 &&
2771    //          GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID &&
2772                DBG &&
2773                true ){
2774                cout << "* fragment *"<<endl;              
2775            }
2776    
2777            return(true);    
2778        };
2779        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2780        // if it is a preselected file (there is SelectionList)
2781        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2782        // irun  = run entry relative to the chain
2783        // irunt = run entry relative to the tree
2784        if(SELLI==1){      
2785            sel_tree->GetEntry(iev);// read irunt from SelectionList
2786            irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW
2787            if(irun != oldrun ){
2788                if( irun < run_tree->GetEntries() )run_tree->GetEntry(irun);
2789                // check if the time is ok (with merged files it is not...)
2790                // if not loop over run and look for the proper entry
2791                bool SECONDO_GIRO=false;
2792                //      Long64_t irun_start   = irun;
2793                int      offset_start = irunoffset[sel_tree->GetTreeNumber()];
2794                while (
2795                    (
2796                        (
2797                            !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)
2798                              abstime <= GetRunInfo()->RUNTRAILER_TIME)
2799    //                      ||
2800    //                      !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)
2801    //                        obt <= GetRunInfo()->RUNTRAILER_OBT)
2802                            )
2803                        || GetRunInfo()->NEVENTS==0
2804                        )
2805    //              && irun < run_tree->GetEntries()
2806                    ){
2807    
2808                    if(DBG){
2809                        cout << " (test) ";
2810                        cout << " tree "<<sel_tree->GetTreeNumber();
2811                        cout << " irunt "<<irunt;
2812                        cout << " offset "<<irunoffset[sel_tree->GetTreeNumber()];
2813                        cout << " abs "<<abstime;
2814                        cout <<" >> "<<GetRunInfo()->RUNHEADER_TIME<<" "<<GetRunInfo()->RUNTRAILER_TIME;
2815                        cout << " obt "<<obt;
2816                        cout <<" >> "<<GetRunInfo()->RUNHEADER_OBT<<" "<<GetRunInfo()->RUNTRAILER_OBT;
2817                        cout << " *** JUMP RUN *** irun "<<irun;
2818                        cout << endl;
2819                    }
2820    //              irun++;
2821                    irunoffset[sel_tree->GetTreeNumber()]++;
2822                    irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW          
2823                    if(irun == run_tree->GetEntries() && SECONDO_GIRO){
2824    //              if(irun == irun_start ){
2825                        cout << " ...grrrvzzkhhhajsdkj!!!! "<<endl;
2826                        irunoffset[sel_tree->GetTreeNumber()] = offset_start;
2827                        return false;
2828                    }
2829                    if( irun >= run_tree->GetEntries() || irun < 0){
2830                        cout << "irun = "<<irun<<" >>  search from the beginning... <<"<<endl;
2831                        SECONDO_GIRO=true;
2832                        irun=0;
2833                        irunoffset[sel_tree->GetTreeNumber()]=-irunt;
2834                    }
2835                    run_tree->GetEntry(irun);
2836                }
2837    
2838    
2839                if(DBG){
2840                    cout << " (test) ";
2841                    cout << " tree "<<sel_tree->GetTreeNumber();
2842                    cout << " irunt "<<irunt;
2843                    cout << " offset "<<irunoffset[sel_tree->GetTreeNumber()];
2844                    cout << " abs "<<abstime;
2845                    cout <<" >> "<<GetRunInfo()->RUNHEADER_TIME<<" "<<GetRunInfo()->RUNTRAILER_TIME;
2846                    cout << " obt "<<obt;
2847                    cout <<" >> "<<GetRunInfo()->RUNHEADER_OBT<<" "<<GetRunInfo()->RUNTRAILER_OBT;
2848                }
2849                if(DBG)cout << endl;
2850                if(DBG)cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" (run-entry "<<irun<<")"<<endl;
2851                // ----------------------------------------------------
2852                // update the tracker parameters
2853                // (non ho trovato nessun altro modo sicuro di farlo...)
2854                // ----------------------------------------------------
2855                if(!dbc || (dbc && !dbc->IsConnected()) )SetDBConnection();
2856                TrkParams::Set(GetRunInfo() ,dbc);
2857                if(dbc)dbc->Close();
2858    //          cout << endl;
2859                prevshift = 0;
2860                return true;
2861            }
2862            return false;
2863        }
2864    
2865        return false;
2866        //
2867  };  };
2868  /**  /**
2869   * 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)
2870   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
2871   * @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
2872   */   */
2873  Bool_t PamLevel2::UpdateRunInfo(TTree *run, ULong64_t iev){  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev){
2874    return(UpdateRunInfo((TChain*)run,iev));      return(UpdateRunInfo((TChain*)run,iev));
2875  };  };
2876    
2877  //--------------------------------------  //--------------------------------------
# Line 1574  Bool_t PamLevel2::UpdateRunInfo(TTree *r Line 2881  Bool_t PamLevel2::UpdateRunInfo(TTree *r
2881  /**  /**
2882   * Set which trees shoul be analysed   * Set which trees shoul be analysed
2883   * @param detlist TString containing the sequence of trees required   * @param detlist TString containing the sequence of trees required
2884  */   */
2885  void PamLevel2::SetWhichTrees(TString detlist){  void PamLevel2::SetWhichTrees(TString detlist){
2886                
2887      if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){  //    if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){
2888          CAL0 = false;      if( detlist.Contains("+ALL", TString::kIgnoreCase)){
2889    
2890            cout << " ======================================================== "<<endl;
2891            cout << "                       (( WARNING ))                      "<<endl;
2892            cout << " The meaning of the option +ALL has changed!!             "<<endl;
2893            cout << " Now it includes really all (level0+level1+level2+gpamela)"<<endl;
2894            cout << " and the file is discarded if it does not contain         "<<endl;
2895            cout << " all trees or  if level0 files are not available!!        "<<endl;
2896            cout << " ======================================================== "<<endl;
2897                
2898            CAL0 = true;
2899          CAL1 = true;          CAL1 = true;
2900          CAL2 = true;          CAL2 = true;
2901          TRK2 = true;          TRK2 = true;
2902          TRK1 = false;          TRK1 = true;
2903          TRKh = false;          TRKh = true;
2904          TRK0 = false;          TRK0 = true;
2905          TRG = true;          TRG = true;
2906          TOF = true;          TOF = true;
2907          TOF0 = false;          TOF0 = true;
2908          S4  = true;          S4  = true;
2909          ND  = true;          ND  = true;
2910          AC  = true;          AC  = true;
2911          ORB = true;          ORB = true;
2912            GP  = true;
2913      }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){      }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){
2914          CAL0 = false;          CAL0 = false;
2915          CAL1 = false;          CAL1 = false;
# Line 1607  void PamLevel2::SetWhichTrees(TString de Line 2925  void PamLevel2::SetWhichTrees(TString de
2925          ND  = false;          ND  = false;
2926          AC  = false;          AC  = false;
2927          ORB = false;          ORB = false;
2928            GP  = false;
2929      };      };
2930            
2931  //  -------------------------------------------------------------------------  //  -------------------------------------------------------------------------
# Line 1680  void PamLevel2::SetWhichTrees(TString de Line 2999  void PamLevel2::SetWhichTrees(TString de
2999      if( detlist.Contains("-ORB", TString::kIgnoreCase) )ORB = false;      if( detlist.Contains("-ORB", TString::kIgnoreCase) )ORB = false;
3000      else if( detlist.Contains("+ORB", TString::kIgnoreCase) )ORB = true;      else if( detlist.Contains("+ORB", TString::kIgnoreCase) )ORB = true;
3001    
3002        if( detlist.Contains("-GP", TString::kIgnoreCase) )GP = false;
3003        else if( detlist.Contains("+GP", TString::kIgnoreCase) )GP = true;
3004    
3005        cout<< "tree/branch list from input --> ";
3006        if(TRK0)cout<<"TRK0 ";
3007        if(TRK1)cout<<"TRK1 ";
3008        if(TRK2)cout<<"TRK2 ";
3009        if(TRKh)cout<<"TRKH ";
3010        if(CAL0)cout<<"CAL0 ";
3011        if(CAL1)cout<<"CAL1 ";
3012        if(CAL2)cout<<"CAL2 ";
3013        if(TOF)cout<<"TOF ";
3014        if(TRG)cout<<"TRG ";
3015        if(AC)cout<<"AC ";
3016        if(ND)cout<<"ND ";
3017        if(S4)cout<<"S4 ";
3018        if(ORB)cout<<"ORB ";
3019        if(GP)cout<<"GP ";
3020        cout << endl;
3021  //     cout<< "Set detector list --> ";  //     cout<< "Set detector list --> ";
3022  //     if(TRK1)cout<<"TRK1 ";  //     if(TRK1)cout<<"TRK1 ";
3023  //     if(TRK2)cout<<"TRK2 ";  //     if(TRK2)cout<<"TRK2 ";
3024  //     if(TRKh)cout<<"TRKH ";  //     if(TRKh)cout<<"TRKH ";
3025  //     if(CAL1)cout<<"CAL1 ";  //     if(CAL1)cout<<"CAL1 ";
3026  //     if(CAL2)cout<<"CAL2 ";  //     if(CAL2)cout<<"CAL2 ";
3027    //     if(TOF0)cout<<"TOF0 ";
3028  //     if(TOF)cout<<"TOF ";  //     if(TOF)cout<<"TOF ";
3029  //     if(TRG)cout<<"TRG ";  //     if(TRG)cout<<"TRG ";
3030  //     if(AC)cout<<"AC ";  //     if(AC)cout<<"AC ";
# Line 1704  void  PamLevel2::GetWhichTrees(TFile* f) Line 3043  void  PamLevel2::GetWhichTrees(TFile* f)
3043            
3044    
3045    
3046      cout << "void  PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;  //    cout << "void  PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;
3047      // -----------      // -----------
3048      // reset flags      // reset flags
3049      // -----------      // -----------
# Line 1719  void  PamLevel2::GetWhichTrees(TFile* f) Line 3058  void  PamLevel2::GetWhichTrees(TFile* f)
3058      ND     = false;          ND     = false;    
3059      AC     = false;          AC     = false;    
3060      ORB    = false;          ORB    = false;    
3061        GP     = false;    
3062            
3063      RUN    = false;      RUN    = false;
3064                    
3065      cout << "Checking file: "<<f->GetName()<<endl;  //    cout << "Checking file: "<<f->GetName()<<endl;
3066      if( !f || f->IsZombie() ){      if( !f || f->IsZombie() ){
3067          cout << "File: "<< f->GetName() <<" Non valid root file"<< endl;          cout << "File: "<< f->GetName() <<" Non valid root file"<< endl;
3068          return;          return;
# Line 1809  void  PamLevel2::GetWhichTrees(TFile* f) Line 3149  void  PamLevel2::GetWhichTrees(TFile* f)
3149                  TRK2 = false;                  TRK2 = false;
3150                  TRKh = false;                  TRKh = false;
3151              }else nev=nevt;              }else nev=nevt;
3152              T->Delete();  //          T->Delete();
3153          };          };
3154          //=========================================================            //=========================================================  
3155          if( !strcmp(key->GetName(),"Calorimeter"    ) ){          if( !strcmp(key->GetName(),"Calorimeter"    ) ){
# Line 1825  void  PamLevel2::GetWhichTrees(TFile* f) Line 3165  void  PamLevel2::GetWhichTrees(TFile* f)
3165                  CAL1 = false;                  CAL1 = false;
3166                  CAL2 = false;                  CAL2 = false;
3167              }else nev=nevt;              }else nev=nevt;
3168              T->Delete();  //          T->Delete();
3169          };                };      
3170            //=========================================================  
3171            if( !strcmp(key->GetName(),"h20") ){
3172                GP = true;
3173                Int_t nevt = ((TTree*)f->Get("h20"))->GetEntries();
3174                if( nev && nevt!=nev){
3175                    cout << "File: "<< f->GetName() <<" h20 tree has "<<nevt<<" events instead of "<<nev<< endl;
3176                    GP = false;
3177                }else nev=nevt;
3178            }
3179    
3180      };      };
3181            
3182      delete lk;  //    delete lk;
3183    
3184  //     cout<< "Get detector list from input file --> ";      cout<< "tree/branch list from file  --> ";
3185  //     if(TRK1)cout<<"TRK1 ";      if(TRK1)cout<<"TRK1 ";
3186  //     if(TRK2)cout<<"TRK2 ";      if(TRK2)cout<<"TRK2 ";
3187  //     if(TRKh)cout<<"TRKH ";      if(TRKh)cout<<"TRKH ";
3188  //     if(CAL1)cout<<"CAL1 ";      if(CAL1)cout<<"CAL1 ";
3189  //     if(CAL2)cout<<"CAL2 ";      if(CAL2)cout<<"CAL2 ";
3190  //     if(TOF)cout<<"TOF ";      if(TOF)cout<<"TOF ";
3191  //     if(TRG)cout<<"TRG ";      if(TRG)cout<<"TRG ";
3192  //     if(AC)cout<<"AC ";      if(AC)cout<<"AC ";
3193  //     if(ND)cout<<"ND ";      if(ND)cout<<"ND ";
3194  //     if(S4)cout<<"S4 ";      if(S4)cout<<"S4 ";
3195  //     if(ORB)cout<<"ORB ";      if(ORB)cout<<"ORB ";
3196  //     cout << endl;      if(GP)cout<<"GP ";
3197        cout << endl;
3198                
3199      return ;      return ;
3200                    
# Line 1873  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3223  Bool_t  PamLevel2::CheckLevel2File(TStri
3223      Bool_t ND__ok     = false;          Bool_t ND__ok     = false;    
3224      Bool_t AC__ok     = false;          Bool_t AC__ok     = false;    
3225      Bool_t ORB__ok    = false;          Bool_t ORB__ok    = false;    
3226        Bool_t GP__ok     = false;    
3227            
3228      Bool_t RUN__ok    = false;      Bool_t RUN__ok    = false;
3229            
3230      Bool_t SELLI__ok  = false;      Bool_t SELLI__ok  = false;
3231    
3232      cout << "Checking file: "<<name<<endl;  //    cout << "Checking file: "<<name<<endl;
3233      TFile *f = new TFile(name.Data());      TFile *f = new TFile(name.Data());
3234      if( !f || f->IsZombie() ){      if( !f || f->IsZombie() ){
3235          cout << "File: "<< f->GetName() <<" discarded ---- Non valid root file"<< endl; return false;          cout << "File: "<< f->GetName() <<" discarded ---- Non valid root file"<< endl; return false;
# Line 1904  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3255  Bool_t  PamLevel2::CheckLevel2File(TStri
3255  //          return false;  //          return false;
3256  //      };  //      };
3257    
3258            //=========================================================    
3259            // check if the file
3260            
3261    
3262          if( !strcmp(key->GetName(),"Run"        ) )RUN__ok = true;                if( !strcmp(key->GetName(),"Run"        ) )RUN__ok = true;      
3263    
# Line 2030  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3384  Bool_t  PamLevel2::CheckLevel2File(TStri
3384              };                  };    
3385              T->Delete();              T->Delete();
3386          };                };      
3387            //=========================================================    
3388            if( !strcmp(key->GetName(),"h20") ){
3389                ISGP = true;    
3390                GP__ok = true;
3391                if(GP){
3392                    Int_t nevt = ((TTree*)f->Get("h20"))->GetEntries();
3393                    if( nev && nevt!=nev){
3394                        cout << "File: "<< f->GetName() <<" discarded ---- OrbitalInfo tree has "<<nevt<<" events instead of "<<nev<< endl;
3395                        return false;
3396                    }
3397                    nev=nevt;
3398                }
3399            }
3400    
3401      };      };
3402    
# Line 2070  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3437  Bool_t  PamLevel2::CheckLevel2File(TStri
3437    
3438    
3439      if(!RUN__ok) {      if(!RUN__ok) {
3440          cout << "File: "<< f->GetName() <<" *WARNING* ---- Missing RunInfo tree"<< endl;          cout << "File: "<< f->GetName() <<" *WARNING* ---- Missing RunInfo tree (NB: RUN infos will not be updated)"<< endl;
3441  //      return false;          RUN = false;
3442      };      };
3443    
3444      if(CAL1 && !CAL1__ok){      if(CAL1 && !CAL1__ok){
# Line 2119  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3486  Bool_t  PamLevel2::CheckLevel2File(TStri
3486          cout << "File: "<< f->GetName() <<" discarded ---- Missing Trigger tree"<< endl;          cout << "File: "<< f->GetName() <<" discarded ---- Missing Trigger tree"<< endl;
3487          return false;          return false;
3488      };      };
3489        if(GP && !GP__ok){
3490            cout << "File: "<< f->GetName() <<" discarded ---- Missing h20 tree"<< endl;
3491            return false;
3492        };
3493            
3494    
3495  //    lk->Delete();  //    lk->Delete();
# Line 2137  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3508  Bool_t  PamLevel2::CheckLevel2File(TStri
3508  //     if(ND)cout<<"ND ";  //     if(ND)cout<<"ND ";
3509  //     if(S4)cout<<"S4 ";  //     if(S4)cout<<"S4 ";
3510  //     if(ORB)cout<<"ORB ";  //     if(ORB)cout<<"ORB ";
3511    //     if(GP)cout<<"GP ";
3512  //     cout << endl;  //     cout << endl;
3513                
3514      return true;      return true;
# Line 2187  void PamLevel2::CreateCloneTrees0( TChai Line 3559  void PamLevel2::CreateCloneTrees0( TChai
3559   */   */
3560  void PamLevel2::CreateCloneTrees(TFile *ofile){  void PamLevel2::CreateCloneTrees(TFile *ofile){
3561    
3562    
3563    //  if the pointer is null, create a default file
3564        if(!run_tree)return;
3565    
3566        if(!ofile){
3567            cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root "<<endl;
3568            ofile = new TFile("clone-tree.root","recreate");
3569        }
3570    
3571      ofile->cd();      ofile->cd();
3572    
3573      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
# Line 2198  void PamLevel2::CreateCloneTrees(TFile * Line 3579  void PamLevel2::CreateCloneTrees(TFile *
3579      cout << "Run          : branch RunInfo"<<endl;      cout << "Run          : branch RunInfo"<<endl;
3580      run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));      run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));
3581      cout << "Run          : branch SoftInfo"<<endl;      cout << "Run          : branch SoftInfo"<<endl;
3582                    // ------------------
3583        // replicate run tree
3584        // ------------------
3585    //    cout << "----------------------------------------------------"<<endl;
3586    //    cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
3587        for (Int_t i=0; i<run_tree->GetEntries(); i++){
3588            run_tree->GetEntry(i);
3589    //      cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
3590            run_tree_clone->Fill();
3591        }
3592        cout << "----------------------------------------------------"<<endl;
3593    
3594        // ------------------------------------
3595        // add branch with dead and live times
3596        // ------------------------------------
3597        run_tree_clone->Branch("DeadLiveTime",totdltime,"dltime[3]/l");
3598        cout << "Run          : branch DeadLiveTime"<<endl;
3599    
3600    
3601      sel_tree_clone = new TTree("SelectionList","List of selected events ");      sel_tree_clone = new TTree("SelectionList","List of selected events ");
3602      sel_tree_clone->Branch("RunEntry",&irun,"runentry/I");  //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
3603      sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/I");      sel_tree_clone->Branch("RunEntry",&irunt,"runentry/L");//NEWNEW
3604        sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/L");
3605            
3606    
3607      Int_t i=0;      Int_t i=0;
# Line 2213  void PamLevel2::CreateCloneTrees(TFile * Line 3611  void PamLevel2::CreateCloneTrees(TFile *
3611              pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));              pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));
3612              pam_tree_clone[i]->BranchRef();              pam_tree_clone[i]->BranchRef();
3613              cout << "Tracker      : branch TrkLevel1"<<endl;              cout << "Tracker      : branch TrkLevel1"<<endl;
3614    //          cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;
3615          };          };
3616          if(TRK2) {          if(TRK2) {
3617              pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));              pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));
# Line 2281  void PamLevel2::CreateCloneTrees(TFile * Line 3680  void PamLevel2::CreateCloneTrees(TFile *
3680          cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;          cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;
3681          i++;          i++;
3682      };      };
3683        // GPamela
3684        if(GP) {
3685            pam_tree_clone[i] = new TTree("h20","PAMELA oribital info  ");  
3686            pam_tree_clone[i]->Branch("GPamela","GPamela", GetPointerTo("GPamela"),32000,1);//split
3687            cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;
3688            i++;
3689        };
3690      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
3691    
3692  }  }
# Line 2294  void PamLevel2::CreateCloneTrees(TFile * Line 3700  void PamLevel2::CreateCloneTrees(TFile *
3700  void PamLevel2::FillCloneTrees(){  void PamLevel2::FillCloneTrees(){
3701            
3702  //    cout << "PamLevel2::FillCloneTrees()" << irunentry << endl;  //    cout << "PamLevel2::FillCloneTrees()" << irunentry << endl;
3703    
3704      for(Int_t i=0; i<NCLONES; i++){      for(Int_t i=0; i<NCLONES; i++){
3705          if(pam_tree_clone[i])pam_tree_clone[i]->Fill();          if(pam_tree_clone[i])pam_tree_clone[i]->Fill();
3706      }      }
3707      if(sel_tree_clone)sel_tree_clone->Fill();          if(sel_tree_clone)sel_tree_clone->Fill();    
 //    if( irunentry == 0 || run_tree_clone->GetEntries()==0 )run_tree_clone->Fill();  
 //    Int_t nstoredruns = run_tree_clone->GetEntries();  
 //     if( irun ==  nstoredruns )run_tree_clone->Fill();  
 //     else if( irun > nstoredruns || irun < nstoredruns-1){  
 //      cout << " PamLevel2::FillCloneTrees() -- ERROR -- current run "<< irun <<" stored runs "<< nstoredruns <<endl;  
 //     }  
3708    
3709  }  }
3710    
# Line 2330  TTree* PamLevel2::GetCloneTree(TString n Line 3731  TTree* PamLevel2::GetCloneTree(TString n
3731  void PamLevel2::WriteCloneTrees(){  void PamLevel2::WriteCloneTrees(){
3732      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
3733      cout << "Write clones of PAMELA trees "<<endl;      cout << "Write clones of PAMELA trees "<<endl;
3734      cout << run_tree_clone->GetName()<<endl;          cout << run_tree_clone->GetName()<<endl;  
3735        if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
3736            run_tree_clone->GetBranch("DeadLiveTime")->Fill();
3737      run_tree_clone->Write();      run_tree_clone->Write();
3738      cout << sel_tree_clone->GetName()<<endl;          cout << sel_tree_clone->GetName()<<endl;    
3739      sel_tree_clone->Write();      sel_tree_clone->Write();
# Line 2345  void PamLevel2::WriteCloneTrees(){ Line 3748  void PamLevel2::WriteCloneTrees(){
3748  }  }
3749    
3750  /**  /**
3751   * Method to get both level2-trees entry and corresponding run entry.   * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.
3752   */   */
3753    //Int_t PamLevel2::GetEntry(Int_t iee){
3754    Int_t PamLevel2::GetEntry(Long64_t iee){
 Int_t PamLevel2::GetEntry(Int_t iee){  
3755            
3756    //     cout << "-------------------------------------"<<endl;
3757    //     cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;
3758    
3759      if(!pam_tree){      if(!pam_tree){
3760          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded"<<endl;          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded"<<endl;
3761          return 0;          return 0;
3762      }      }
3763    
3764      Int_t ii=0;  
3765        //
3766        // 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...
3767        //
3768        //    if(!run_tree ){
3769        //  cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loeaded"<<endl;
3770        //  return 0;
3771        //    }
3772    
3773        Long64_t ii=0;
3774      //-------------------------------      //-------------------------------
3775      ii = iee;      ii = iee;
3776      if( !pam_tree->GetEntry(ii) ) return 0;      if( !pam_tree->GetEntry(ii) ){      
3777      //-------------------------------          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree"<<endl;
   
     if(!run_tree ){  
         cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded"<<endl;  
3778          return 0;          return 0;
3779      }      }
3780    
3781        //
3782        // ... 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.
3783        // 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
3784        // a problem if you don't check the return code of getentry.
3785        //
3786        if(!RUN || !run_tree ){
3787            if ( TRK0 || CAL0 || TOF0 || RUN ) { //forse cosi` va bene per tornare 1?
3788                cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded"<<endl;
3789                return 0;
3790            }  else {
3791                return 1; //cosi` se non c'e` run esce qua...
3792            }
3793        }
3794    
3795        //-------------------------------
3796      ii = iee;      ii = iee;
3797      Bool_t UPDATED = UpdateRunInfo(run_tree,ii);  //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);
3798    //    Bool_t UPDATED = UpdateRunInfo(ii);
3799        UpdateRunInfo(ii);
3800      if(SELLI==0)irunentry = iee-runfirstentry;      if(SELLI==0)irunentry = iee-runfirstentry;
3801      if(UPDATED && run_tree_clone)run_tree_clone->Fill();  //    if(UPDATED && run_tree_clone)run_tree_clone->Fill();
3802      
3803  //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;  //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;
3804    
3805      if( TRK0 || CAL0 || TOF0 )GetYodaEntry( );  //     cout << "irunentry     "<<irunentry << endl;
3806    //     cout << "runfirstentry "<<runfirstentry << endl;
3807    //     cout << "nevents       "<<GetRunInfo()->NEVENTS<< endl;
3808    
3809    //     if( TRK0 || CAL0 || TOF0 ){
3810    //      if( !GetYodaEntry( ) ){
3811    //          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading level0 tree"<<endl;
3812    //          return 0;
3813    //      }
3814    //     }
3815    
3816    
3817      return 1;      return 1;
3818    
3819  }  }
3820    
3821    TrkLevel0    *PamLevel2::GetTrkLevel0(){
3822        if( !TRK0 )return NULL;
3823        if( !GetYodaEntry( ) ){
3824            cout << " Int_t PamLevel2::GetTrkLevel0() -- ERROR -- error reading level0 tree"<<endl;
3825            return 0;
3826        }
3827        return trk0_obj;
3828    };
3829    CaloLevel0    *PamLevel2::GetCaloLevel0(){
3830        if( !CAL0 )return NULL;
3831        if( !GetYodaEntry( ) ){
3832            cout << " Int_t PamLevel2::GetCaloLevel0() -- ERROR -- error reading level0 tree"<<endl;
3833            return 0;
3834        }
3835        return calo0_obj;
3836    };
3837    
3838    
3839  /**  /**
3840   * 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.
3841   * Given the run ID (...), if needed it query the DB and load the proper file.   * Given the run ID (...), if needed it query the DB and load the proper file.
# Line 2394  TTree* PamLevel2::GetYodaTree( ){ Line 3850  TTree* PamLevel2::GetYodaTree( ){
3850      // check if iroot has changed      // check if iroot has changed
3851      //===================================      //===================================
3852      if( irun<0 ){      if( irun<0 ){
3853          cout << "PamLevel2::GetYodaTree() -- ERROR "<<endl;          cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = "<<irun<<endl;
3854          cout << "In order to use this method you have to load the RunInfo tree "<<endl;  //      cout << "In order to use this method you have to first load the RunInfo tree "<<endl;
         cout << "with the method TChain* PamLevel2::GetRunTree(TString, TString)"<<endl;  
 //      cout << " - read the event with PamLevel2::GetEntry(Int_t)"<<endl;  
3855          return NULL;          return NULL;
3856      }          }    
3857      Int_t irootnew = run_obj->ID_ROOT_L0;      Int_t irootnew = run_obj->ID_ROOT_L0;
3858  //    cout << "iroot    "<<iroot<<endl;  //      cout << "iroot    "<<iroot<<endl;
3859  //    cout << "irootnew "<<irootnew<<endl;  //      cout << "irootnew "<<irootnew<<endl;
3860    
3861      //===================================      //===================================
3862      // load the level0 file      // load the level0 file
# Line 2414  TTree* PamLevel2::GetYodaTree( ){ Line 3868  TTree* PamLevel2::GetYodaTree( ){
3868          // open the DB connection          // open the DB connection
3869          // (if not already opened)          // (if not already opened)
3870          //===================================          //===================================
3871          if(!dbc || (dbc && !dbc->IsConnected())){          if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection();
             cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;  
             cout<<"Connecting to DB"<<endl;  
             cout<<"HOST "<<host<<endl;  
             cout<<"USER "<<user<<endl;  
             cout<<"PSW  "<<psw<<endl;  
             dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());  
             if( !dbc )return NULL;  
             if( !dbc->IsConnected() )return NULL;      
 //          cout<<"...done"<<endl;  
             cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;  
         }else{  
 //          cout<<"DB already connected"<<endl;  
         }  
3872          GL_ROOT glroot = GL_ROOT();          GL_ROOT glroot = GL_ROOT();
3873          if( glroot.Query_GL_ROOT(iroot,dbc) )return NULL;          if( glroot.Query_GL_ROOT(iroot,dbc) ){
3874                cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = "<<iroot<< " does not exists"<<endl;
3875                return NULL;
3876            };
3877          TString filename = glroot.PATH + glroot.NAME;          TString filename = glroot.PATH + glroot.NAME;
3878          if(l0_file){          if(l0_file){
3879              l0_file->Close();              l0_file->Close();
# Line 2446  TTree* PamLevel2::GetYodaTree( ){ Line 3890  TTree* PamLevel2::GetYodaTree( ){
3890          l0_tree = (TTree*)l0_file->Get("Physics");          l0_tree = (TTree*)l0_file->Get("Physics");
3891          if(!h0_obj)h0_obj = new EventHeader();          if(!h0_obj)h0_obj = new EventHeader();
3892          l0_tree->SetBranchAddress("Header" ,&h0_obj);          l0_tree->SetBranchAddress("Header" ,&h0_obj);
3893            prevshift = 0;
3894          //---------------------------------------------------          //---------------------------------------------------
3895          // TRACKER:          // TRACKER:
3896          if(TRK0){          if(TRK0){
# Line 2454  TTree* PamLevel2::GetYodaTree( ){ Line 3899  TTree* PamLevel2::GetYodaTree( ){
3899                  trk0_obj->Set();                  trk0_obj->Set();
3900              };              };
3901              l0_tree->SetBranchAddress("Tracker" ,trk0_obj->GetPointerToTrackerEvent());              l0_tree->SetBranchAddress("Tracker" ,trk0_obj->GetPointerToTrackerEvent());
             TrkParams::SetCalib(run_obj,dbc);  
3902          }          }
3903          //---------------------------------------------------          //--------------------------------------------------
3904          // CALORIMETER:          // CALORIMETER:
3905          if(CAL0){          if(CAL0){
3906              cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;              if(!calo0_obj){
3907                    calo0_obj = new CaloLevel0();
3908                    calo0_obj->Set();
3909                };
3910                l0_tree->SetBranchAddress("Calorimeter" ,calo0_obj->GetPointerToCalorimeterEvent());
3911                //      cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;
3912          }          }
3913          //---------------------------------------------------          //---------------------------------------------------
3914          // TOF:          // TOF:
# Line 2467  TTree* PamLevel2::GetYodaTree( ){ Line 3916  TTree* PamLevel2::GetYodaTree( ){
3916              cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented "<<endl;              cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented "<<endl;
3917          }          }
3918    
3919            dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
3920    
3921    
3922      };      };
3923    
3924    //     if(!dbc || (dbc && !dbc->IsConnected())){
3925    //      cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;
3926    //     }
3927    
3928        if ( TRK0 ){
3929    //      TrkParams::Load(6);
3930    //      if( !TrkParams::IsLoaded(6) ){
3931    //          cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- VK-mask not loaded"<<endl;
3932    //      };
3933    //      if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection();
3934            TrkParams::SetCalib(run_obj,dbc);
3935            TrkParams::LoadCalib( );
3936            if( !TrkParams::CalibIsLoaded() ){
3937                cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded"<<endl;
3938            };
3939    //      if(dbc)dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
3940        }
3941    
3942    
3943    //    cout << l0_tree << endl;
3944      return l0_tree;      return l0_tree;
3945    
3946  }  }
# Line 2481  Int_t PamLevel2::GetYodaEntry(){ Line 3953  Int_t PamLevel2::GetYodaEntry(){
3953  //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;  //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;
3954      if(!GetYodaTree())return 0;      if(!GetYodaTree())return 0;
3955            
3956        // patch
3957        if( irunentry < 0){
3958    //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
3959            irunentry=0LL;
3960        }
3961      //  ---------------------------------      //  ---------------------------------
3962      //  if file is NOT a preselected file      //  if file is NOT a preselected file
3963      //  ---------------------------------      //  ---------------------------------
3964      Int_t quellagiusta = irunentry + run_obj->EV_FROM;      Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM);
3965      //  ------------------------------------------  //     cout << " irunentry "<<irunentry<<endl;
3966      //  otherwise read the "preselected-list" tree  //     cout << " EV_FROM "<<run_obj->EV_FROM<<endl;
3967      //  ------------------------------------------  //     cout << " quellagiusta = irunentry + EV_FROM "<< quellagiusta << endl;
3968    
3969    //     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
3970    //     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
3971    //     cout << " time "<< abstime << endl;
3972    //     cout << " trk_calib_used "<<run_obj->TRK_CALIB_USED<< endl;
3973        
3974        ULong64_t obt     = 0;
3975        ULong64_t pktn    = 0;
3976        if( GetOrbitalInfo() ){
3977            obt     = GetOrbitalInfo()->OBT;
3978            pktn    = GetOrbitalInfo()->pkt_num;
3979        }
3980    
3981        if( !GetOrbitalInfo() && !ISGP){
3982            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo "<<endl;
3983            return 0;
3984        }
3985        if( obt==0 && pktn==0 && !ISGP ){
3986            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? "<<endl;
3987            return 0;
3988        }
3989    
3990        // ---------------------------------------------------------------------
3991        // ATTENTION!!!
3992        // If data are not pre-processed with cleaner, the level0 tree may contain
3993        // spurious nested physics packets.
3994        // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
3995        // while level2 tree DOES NOT!!
3996        // This means that "quellagiusta" in these cases is not correct.
3997        // In order to retrieve the correct level0 event, I implemented a check
3998        // of the OBT and pkt-number. In case of mismatch, the level0 entry number
3999        // is shift forward until when the packets match.
4000        // ---------------------------------------------------------------------
4001        Int_t answer = 0;
4002        Int_t shift =0;
4003        //    printf(" siamo qui %i %i \n",shift,prevshift);
4004        //    Int_t maxshift = 50; // EMILIANO
4005        do{
4006            if(shift>0){    
4007                cout << " PKTNUM  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
4008                if(DBG)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;
4009                if(DBG)cout << "         L2 <--> L0 mismatch ( irun "<<irun<<" irunentry "<<irunentry<<" shift "<<shift<<" prevshift "<<prevshift<<" )"<<endl;
4010            }
4011            answer = l0_tree->GetEntry(quellagiusta+(Long64_t)shift+(Long64_t)prevshift);
4012            shift++;
4013            if( !GetEventHeader() ){
4014                cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader "<<endl;
4015                return 0;
4016            }
4017    
4018            if(ISGP){
4019                obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
4020                pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
4021            }
4022    
4023    //      cout << "PKTNUM "<<shift<<" ==  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
4024    //      cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;
4025    //      if( (quellagiusta+shift) == l0_tree->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;
4026    //      cout << " obt "<< obt << endl;
4027    //      cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;
4028    //      cout << " pktn "<< pktn << endl;
4029    //      cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
4030    //      printf(" IDRUN %u \n",GetRunInfo()->ID);
4031    //
4032            if ( prevshift != 0 && (quellagiusta+(Long64_t)shift) == GetYodaTree()->GetEntries() ){
4033                prevshift = 0;
4034                shift = -1;
4035            };
4036    
4037        }while( ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) ||
4038                  pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())       )
4039                && (quellagiusta+(Long64_t)shift) < GetYodaTree()->GetEntries() && shift < maxshift);
4040    
4041        if ( (quellagiusta+(Long64_t)shift+(Long64_t)prevshift) > GetYodaTree()->GetEntries() || shift == maxshift ) {
4042            cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to "<<maxshift<<" )" <<endl;
4043            return 0;
4044        }
4045  //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;  //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
4046      return GetYodaTree()->GetEntry(quellagiusta);  //    return GetYodaTree()->GetEntry(quellagiusta);
4047        if ( shift > 1 ) prevshift += (shift-1);
4048            
4049      return 1;      return answer;
4050        
4051    }
4052    /**
4053     * \Brief Set DB connection
4054     */
4055    Bool_t PamLevel2::SetDBConnection(){
4056    
4057    //    cout << "PamLevel2::SetDBConnection()" << endl;
4058        if(DBG){
4059            cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
4060            cout<<"Connecting to DB"<<endl;
4061            cout<<"HOST "<<host<<endl;
4062            cout<<"USER "<<user<<endl;
4063            cout<<"PSW  "<<psw<<endl;
4064        }
4065        dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
4066        if( !dbc )return false;
4067        if( !dbc->IsConnected() )return false;    
4068        stringstream myquery;  // EMILIANO
4069        myquery.str("");  // EMILIANO
4070        myquery << "SET time_zone='+0:00'";  // EMILIANO
4071        dbc->Query(myquery.str().c_str());  // EMILIANO
4072        if(DBG)cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
4073        return true;
4074    
4075    }
4076    
4077    /**
4078     * \Brief Add a friend to the pamela chain.
4079     * @param cname name of the chain to be added
4080     */
4081    
4082    TChain* PamLevel2::AddFriend(TString cname){
4083    
4084        if(!GetPamTree()){
4085            cout << " TChain* PamLevel2::AddFriend(TString cname) --- a pamela tree must be created first"<<endl;
4086            return NULL;
4087        }
4088    
4089    
4090        TChain *c = new TChain( cname.Data() );
4091    
4092        TIter next( GetPamTree()->GetListOfFiles() );
4093        Int_t nf = 0;
4094        TChainElement* element = 0;    
4095        while ((element = (TChainElement*) next())) {      
4096            c->Add(element->GetTitle());
4097            nf++;
4098        }
4099    
4100        GetPamTree()->AddFriend(cname.Data());
4101    
4102        cout << "external chain created and added to pamela friends :"<<cname<<endl;
4103        cout << "n.files "<<nf<<endl;
4104            
4105    
4106        return c;    
4107    
4108  }  }
4109    
4110    

Legend:
Removed from v.1.23  
changed lines
  Added in v.1.70

  ViewVC Help
Powered by ViewVC 1.1.23