/[PAMELA software]/PamelaLevel2/src/PamLevel2.cpp
ViewVC logotype

Diff of /PamelaLevel2/src/PamLevel2.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.25 by mocchiut, Thu Mar 15 13:42:36 2007 UTC revision 1.78 by pam-fi, Fri Mar 13 10:30:11 2009 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      candeleteobj = 0;      if(r) orb_track  = r;
312    
313       candeleteobj = 0;
314    
315  };  };
316  PamTrack::PamTrack(const PamTrack& track){  PamTrack::PamTrack(const PamTrack& track){
# Line 38  PamTrack::PamTrack(const PamTrack& track Line 318  PamTrack::PamTrack(const PamTrack& track
318      TrkTrack   *t = track.trk_track;      TrkTrack   *t = track.trk_track;
319      CaloTrkVar *c = track.calo_track;      CaloTrkVar *c = track.calo_track;
320      ToFTrkVar  *o = track.tof_track;      ToFTrkVar  *o = track.tof_track;
321        OrbitalInfoTrkVar  *r = track.orb_track;
322    
323      trk_track  = 0;      trk_track  = 0;
324      calo_track = 0;      calo_track = 0;
325      tof_track  = 0;      tof_track  = 0;
326        orb_track  = 0;
327      if(t)trk_track  = new TrkTrack(*t);      if(t)trk_track  = new TrkTrack(*t);
328      if(c)calo_track = new CaloTrkVar(*c);      if(c)calo_track = new CaloTrkVar(*c);
329      if(o)tof_track  = new ToFTrkVar(*o);      if(o)tof_track  = new ToFTrkVar(*o);
330        if(r)orb_track  = new OrbitalInfoTrkVar(*r);
331      candeleteobj = 1;      candeleteobj = 1;
332            
333  }  }
# Line 55  void PamTrack::Clear(){ Line 338  void PamTrack::Clear(){
338          if(trk_track)  trk_track->TrkTrack::Clear();          if(trk_track)  trk_track->TrkTrack::Clear();
339          if(calo_track) calo_track->CaloTrkVar::Clear();//???          if(calo_track) calo_track->CaloTrkVar::Clear();//???
340          if(tof_track)  tof_track->ToFTrkVar::Clear();//???          if(tof_track)  tof_track->ToFTrkVar::Clear();//???
341            if(orb_track)  orb_track->OrbitalInfoTrkVar::Clear();//???
342      }else{      }else{
343          trk_track  = 0;          trk_track  = 0;
344          calo_track = 0;          calo_track = 0;
345          tof_track  = 0;          tof_track  = 0;
346            orb_track  = 0;
347      }      }
348        pscore = 0;
349        iscore = 0;
350    
351  }  }
352  void PamTrack::Delete(){  void PamTrack::Delete(){
353  //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;  //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;
# Line 76  void PamTrack::Delete(){ Line 364  void PamTrack::Delete(){
364              tof_track->ToFTrkVar::Clear();//???              tof_track->ToFTrkVar::Clear();//???
365              delete tof_track;              delete tof_track;
366          }          }
367            if(orb_track){
368                orb_track->OrbitalInfoTrkVar::Clear();//???
369                delete orb_track;
370            }
371      }else{      }else{
372          Clear();          Clear();
373      }      }
# Line 85  void PamTrack::Delete(){ Line 377  void PamTrack::Delete(){
377  //  //
378  //--------------------------------------  //--------------------------------------
379  /**  /**
380   * Constructor   * Default Constructor
381   */   */
382  PamLevel2::PamLevel2(){  PamLevel2::PamLevel2(){
383        Initialize();
384    };
385    
386    /**
387     * Constructor
388     * @param ddir Name of directory where level2 files are stored.
389     * @param list Name of an ascii file containing the list of file names
390     * @param detlist Options to chose what to load.
391     * Possible options are:
392     *       +AUTO --> load all trees/branches in the input files
393     *       +(-)ALL --> inlcude(exclude) all trees/branches
394     *       +(-)TRK1+(-)TRK2+(-)CAL1+(-)CAL2+(-)TOF+(-)TRG+(-)ND+(-)S4+(-)ORB+(-)AC --> inlcude(exclude) trees and branches
395     *       +(-)TRK0 --> include(exclude) tracker level0 tree
396     *       +(-)GP --> include exclude GPAMELA output tree
397     * If no options are specified, the default is assumed. Default is:
398     * +TRK2+CAL2+CAL1+TOF+TRG+ND+AC+S4+ORB
399     */
400    PamLevel2::PamLevel2(TString ddir,TString llist,TString detlist){
401        Initialize();
402        TList*  listf = GetListOfLevel2Files(ddir,llist);
403        if(listf)GetPamTree(listf,detlist);
404        if(listf)GetRunTree(listf);
405    };
406    
407    PamLevel2::PamLevel2(TString ddir,TList *llist,TString detlist){
408        Initialize();
409        GetPamTree(llist,detlist);
410        GetRunTree(llist);
411    };
412    /**
413     * Constructor
414     * @param ddir Name of directory where level2 files are stored.
415     * @param list Name of an ascii file containing the list of file names
416     * Default trees/branches are loaded. Default is:
417     * +TRK2+CAL2+CAL1+TOF+TRG+ND+AC+S4+ORB
418     */
419    PamLevel2::PamLevel2(TString ddir,TString llist){
420        Initialize();
421        TList*  listf = GetListOfLevel2Files(ddir,llist);
422        GetPamTree(listf,"");
423        GetRunTree(listf);
424    };
425    
426    
427    void PamLevel2::Initialize(){
428                    
 //     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();  
       
429      h0_obj    = 0;      h0_obj    = 0;
430      trk0_obj  = 0;      trk0_obj  = 0;
431        calo0_obj  = 0;
432    
433      trk2_obj  = 0;      trk2_obj  = 0;
434      trk1_obj  = 0;      trk1_obj  = 0;
# Line 115  PamLevel2::PamLevel2(){ Line 441  PamLevel2::PamLevel2(){
441      nd_obj    = 0;      nd_obj    = 0;
442      ac_obj    = 0;      ac_obj    = 0;
443      orb_obj   = 0;      orb_obj   = 0;
444        gp_obj    = 0;
445    
446      run_obj   = 0;//new GL_RUN();      run_obj   = 0;//new GL_RUN();
447      soft_obj   = 0;// Emiliano      soft_obj   = 0;// Emiliano
448      irun = -1;      irun = -1LL;
449      runfirstentry = 0ULL;      irunt = -1LL;
450      runlastentry = 0ULL;      runfirstentry = 0LL;
451        runlastentry = 0LL;
452        gltsync = 0; // Emiliano
453    
454      l0_file = NULL;      l0_file = NULL;
455      l0_tree = NULL;      l0_tree = NULL;
456      iroot   = -1;      iroot   = -1;
457      dbc     = 0;      dbc     = 0;
458    
459        prevshift = 0;
460        maxshift = 10; //EMILIANO
461            
     irun = -1;  
462      run_tree = NULL;      run_tree = NULL;
463      run_tree_clone = NULL;      run_tree_clone = NULL;
464      sel_tree = NULL;      sel_tree = NULL;
465      sel_tree_clone = NULL;      sel_tree_clone = NULL;
466            
467      irunentry = -1;      irunentry = -1LL;
468      pam_tree = NULL;      pam_tree = NULL;
469      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;
470    
471        totdltime[0] = 0LL;
472        totdltime[1] = 0LL;
473        totdltime[2] = 0LL;
474    
475      host = "mysql://localhost/pamelaprod";      host = "mysql://localhost/pamelaprod";
476      user = "anonymous";      user = "anonymous";
477      psw = "";      psw = "";
# Line 159  PamLevel2::PamLevel2(){ Line 494  PamLevel2::PamLevel2(){
494      CAL2 = true;      CAL2 = true;
495      TRK2 = true;      TRK2 = true;
496      TRK1 = false;      TRK1 = false;
497      TRKh = false;      TRK0 = false;
498      TRKh = false;      TRKh = false;
499      TRG  = true;      TRG  = true;
500      TOF  = true;      TOF  = true;
# Line 167  PamLevel2::PamLevel2(){ Line 502  PamLevel2::PamLevel2(){
502      S4   = true;      S4   = true;
503      ND   = true;      ND   = true;
504      AC   = true;      AC   = true;
505      ORB  = true;      ORB  = true;    
506            GP   = false;
507    
508      RUN  = true;      RUN  = true;
509    
510      SELLI = -1;      SELLI = -1;
511    
512        ISGP = false;
513    
514        DBG = false;
515    
516      tsorted=0;      tsorted=0;
517      timage=0;      timage=0;
518        
519        howtosort = "+CAL+TOF";
520        //howtosort = "+TOF";
521        sortthr = 100.;
522    
523  };  };
524  /**  /**
# Line 188  void PamLevel2::Delete(){ Line 532  void PamLevel2::Delete(){
532  //    cout << "void PamLevel2::Clear()"<<endl;  //    cout << "void PamLevel2::Clear()"<<endl;
533      if(h0_obj)   delete h0_obj;      if(h0_obj)   delete h0_obj;
534      if(trk0_obj) delete trk0_obj;            if(trk0_obj) delete trk0_obj;      
535        if(calo0_obj) delete calo0_obj;    
536      if(trk1_obj) delete trk1_obj;            if(trk1_obj) delete trk1_obj;      
537      if(trk2_obj) delete trk2_obj;      if(trk2_obj) delete trk2_obj;
538      if(trkh_obj) delete trkh_obj;      if(trkh_obj) delete trkh_obj;
# Line 199  void PamLevel2::Delete(){ Line 544  void PamLevel2::Delete(){
544      if(nd_obj)   delete nd_obj;      if(nd_obj)   delete nd_obj;
545      if(ac_obj)   delete ac_obj;      if(ac_obj)   delete ac_obj;
546      if(orb_obj)  delete orb_obj;      if(orb_obj)  delete orb_obj;
547        if(gp_obj)   delete gp_obj;
548            
549      if(tsorted){      if(tsorted){
550          tsorted->Delete();          tsorted->Delete();
# Line 213  void PamLevel2::Delete(){ Line 559  void PamLevel2::Delete(){
559          dbc->Close();          dbc->Close();
560          delete dbc;          delete dbc;
561      }      }
562    
563        if ( gltsync ) delete gltsync;
564            
565      if(l0_file)l0_file->Close();      if(l0_file)l0_file->Close();
566      if(pam_tree)pam_tree->Delete();;      //    if(pam_tree)pam_tree->Delete();;
567    
568        if ( pam_tree ){
569            //
570            // 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...
571            //
572            TList *temp = pam_tree->GetListOfFriends();
573            TList *contents  = new TList; // create chain friend list
574            contents->SetOwner();
575            TIter next(temp);
576            TChain *questo = 0;
577            while ( (questo = (TChain*) next()) ){
578                TString name =  questo->GetName();
579                contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
580            };
581            //
582            // deleting the main chain
583            //
584            pam_tree->Delete();
585            //
586            // deleting the friends...
587            //
588            TIter next2(contents);
589            TChain *questa = 0;
590            while ( questa = (TChain*)next2() ){
591                TString name =  questa->GetName();
592                questa->Delete();      
593                questa=NULL;
594            };
595            //
596        };
597        pam_tree = NULL;
598    
599      if(run_tree)run_tree->Delete();;      if(run_tree)run_tree->Delete();;
600      if(sel_tree)sel_tree->Delete();;      if(sel_tree)sel_tree->Delete();;
601      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();;
602      if(run_tree_clone)run_tree_clone->Delete();;      if(run_tree_clone)run_tree_clone->Delete();;
603      if(sel_tree_clone)sel_tree_clone->Delete();;      if(sel_tree_clone)sel_tree_clone->Delete();;
604            
605        if(irunoffset) delete [] irunoffset;
606    
607  };  };
608    
# Line 241  void PamLevel2::Clear(){ Line 622  void PamLevel2::Clear(){
622  //    if(soft_obj) soft_obj->Clear();  //    if(soft_obj) soft_obj->Clear();
623    
624      if(h0_obj)   h0_obj->Clear();            if(h0_obj)   h0_obj->Clear();      
625      if(trk0_obj) trk0_obj->Clear();      //    if(trk0_obj) trk0_obj->Clear();  
626      if(trk1_obj) trk1_obj->Clear();          if(trk1_obj) trk1_obj->Clear();    
627      if(trk2_obj) trk2_obj->Clear();      if(trk2_obj) trk2_obj->Clear();
628      if(trkh_obj) trkh_obj->Clear();      if(trkh_obj) trkh_obj->Clear();
629        if(calo0_obj) calo0_obj->Clear();  
630      if(calo1_obj)calo1_obj->Clear();      if(calo1_obj)calo1_obj->Clear();
631      if(calo2_obj)calo2_obj->Clear();      if(calo2_obj)calo2_obj->Clear();
632      if(tof_obj)  tof_obj->Clear();      if(tof_obj)  tof_obj->Clear();
# Line 253  void PamLevel2::Clear(){ Line 635  void PamLevel2::Clear(){
635      if(nd_obj)   nd_obj->Clear();      if(nd_obj)   nd_obj->Clear();
636      if(ac_obj)   ac_obj->Clear();      if(ac_obj)   ac_obj->Clear();
637      if(orb_obj)  orb_obj->Clear();      if(orb_obj)  orb_obj->Clear();
638        if(gp_obj)   gp_obj->Clear();
639            
640  //    if(sorted_tracks)sorted_tracks->Clear();  //    if(sorted_tracks)sorted_tracks->Clear();
641  //    sorted_tracks.Clear();  //    sorted_tracks.Clear();
# Line 263  void PamLevel2::Clear(){ Line 646  void PamLevel2::Clear(){
646      if(timage){      if(timage){
647          timage->Delete();          timage->Delete();
648      }      }
       
649  };  };
650    
651    void PamLevel2::Reset(){
652        //
653        // First of all clear everything
654        //
655        Clear();
656        //
657        // close and reset chains and pointers
658        //    
659        if ( pam_tree ){
660            //
661            // 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...
662            //
663            TList *temp = pam_tree->GetListOfFriends();
664            TList *contents  = new TList; // create chain friend list
665            contents->SetOwner();
666            TIter next(temp);
667            TChain *questo = 0;
668            while ( (questo = (TChain*) next()) ){
669                TString name =  questo->GetName();
670                contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
671            };
672            //
673            // deleting the main chain
674            //
675            pam_tree->Delete();
676            //
677            // deleting the friends...
678            //
679            TIter next2(contents);
680            TChain *questa = 0;
681            while ( questa = (TChain*)next2() ){
682                TString name =  questa->GetName();
683                questa->Delete();      
684                questa=NULL;
685            };
686            //
687        };
688        pam_tree = NULL;
689        //
690        if(run_tree)run_tree->Delete();;
691        run_tree = NULL;
692        if(sel_tree)sel_tree->Delete();;
693        sel_tree = NULL;
694        //
695        // Close file
696        //
697        if(l0_file)l0_file->Close("R");
698        l0_file = NULL;
699        //
700        h0_obj    = 0;
701        trk0_obj  = 0;
702        calo0_obj  = 0;
703        //
704        trk2_obj  = 0;
705        trk1_obj  = 0;
706        trkh_obj  = 0;
707        calo1_obj = 0;
708        calo2_obj = 0;
709        tof_obj   = 0;
710        trig_obj  = 0;
711        s4_obj    = 0;
712        nd_obj    = 0;
713        ac_obj    = 0;
714        orb_obj   = 0;
715        gp_obj    = 0;
716        //
717        // Reset run pointers
718        //
719        run_obj   = 0;//new GL_RUN();
720        soft_obj   = 0;// Emiliano
721        irun = -1;
722        irunt = -1;
723        runfirstentry = 0ULL;
724        runlastentry = 0ULL;    
725        //
726        totdltime[0] = 0LL;
727        totdltime[1] = 0LL;
728        totdltime[2] = 0LL;
729        //
730    };
731    
732    Bool_t PamLevel2::IsGood(Bool_t strict){
733      Bool_t goodev=true;
734      //
735      if(calo2_obj && !calo2_obj->IsGood(strict) ) goodev = false;
736      //
737      if ( strict ){
738        if(trk2_obj && trk2_obj->UnpackError() != 0 ) goodev = false;
739        if(tof_obj && tof_obj->unpackError != 0) goodev = false;  
740        if(trig_obj && trig_obj->unpackError != 0) goodev = false;
741        if(s4_obj && s4_obj->unpackError != 0) goodev = false;  
742        if(nd_obj && nd_obj->unpackError != 0) goodev = false;  
743        if(ac_obj && (ac_obj->unpackError != 0 || ((ac_obj->status[0]>>2)&1) || ((ac_obj->status[1]>>2)&1)) ) goodev = false;      
744        //  if(orb_obj)  
745      } else {
746        if(nd_obj && nd_obj->unpackError != 0) goodev = false;  
747        if(ac_obj && (ac_obj->unpackError != 0 || ((ac_obj->status[0]>>2)&1) || ((ac_obj->status[1]>>2)&1)) ) goodev = false;      
748      };
749      return(goodev);
750    };
751    
752  //--------------------------------------  //--------------------------------------
753  //  //
# Line 290  void *PamLevel2::GetPointerTo(const char Line 772  void *PamLevel2::GetPointerTo(const char
772          return &trk2_obj;          return &trk2_obj;
773      };      };
774      if(!objname.CompareTo("TrkHough"))   {      if(!objname.CompareTo("TrkHough"))   {
775          if(!trkh_obj)  trkh_obj  = new TrkHough();          if(!trkh_obj)  {
776                trkh_obj  = new TrkHough();
777                trkh_obj->Set();
778            }
779          return &trkh_obj;          return &trkh_obj;
780      };      };
781      if(!objname.CompareTo("CaloLevel1")) {      if(!objname.CompareTo("CaloLevel1")) {
# Line 327  void *PamLevel2::GetPointerTo(const char Line 812  void *PamLevel2::GetPointerTo(const char
812          if(!ac_obj)    ac_obj    = new AcLevel2();          if(!ac_obj)    ac_obj    = new AcLevel2();
813          return &ac_obj;          return &ac_obj;
814      };      };
815      if(!objname.CompareTo("OrbitalInfo")){      if(!objname.CompareTo("OrbitalInfo"))  {
816          if(!orb_obj)   orb_obj   = new OrbitalInfo();          if(!orb_obj){
817                orb_obj   = new OrbitalInfo();
818                orb_obj->Set();
819            }
820          return &orb_obj;          return &orb_obj;
821      };      };
822    //     if(!objname.CompareTo("OrbitalInfo")){
823    //      if(!orb_obj)   orb_obj   = new OrbitalInfo();
824    //      return &orb_obj;
825    //     };
826        if(!objname.CompareTo("GPamela")){
827            if(!gp_obj)   gp_obj   = new GPamela();
828            return &gp_obj;
829        };
830            
831      if(!objname.CompareTo("RunInfo"))return &run_obj;      if(!objname.CompareTo("RunInfo"))return &run_obj;
832    
# Line 346  void *PamLevel2::GetPointerTo(const char Line 842  void *PamLevel2::GetPointerTo(const char
842   * Retrieves the calorimeter track matching the seqno-th tracker stored track.   * Retrieves the calorimeter track matching the seqno-th tracker stored track.
843   * (If seqno = -1 retrieves the self-trigger calorimeter track)   * (If seqno = -1 retrieves the self-trigger calorimeter track)
844   */   */
845   CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno){  CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno){
846            
847       if( !calo2_obj )return 0;      if( !calo2_obj )return 0;
848    
849       if( calo2_obj->CaloLevel2::ntrk()==0 ){      if( calo2_obj->CaloLevel2::ntrk()==0 ){
850           cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no Calorimeter tracks are stored"<<endl;          cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no Calorimeter tracks are stored"<<endl;
851           return NULL;          return NULL;
852       };      };
853            
854       CaloTrkVar *c = 0;      CaloTrkVar *c = 0;
855       Int_t it_calo=0;      Int_t it_calo=0;
856            
857       do{      do{
858           c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);          c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);
859           it_calo++;          it_calo++;
860       } while( c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());          } while( c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());    
861            
862       if(!c || seqno != c->trkseqno){      if(!c || seqno != c->trkseqno){
863           c = 0;          c = 0;
864           if(seqno!=-1)cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match Calorimeter stored tracks"<<endl;          if(seqno!=-1)cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match Calorimeter stored tracks"<<endl;
865       };      };
866       return c;      return c;
867            
868   };  };
869  //--------------------------------------  //--------------------------------------
870   //  //
871   //  //
872  //--------------------------------------  //--------------------------------------
873  /**  /**
874    * Retrieves the ToF track matching the seqno-th tracker stored track.   * Retrieves the ToF track matching the seqno-th tracker stored track.
875    * (If seqno = -1 retrieves the tracker-independent tof track)   * (If seqno = -1 retrieves the tracker-independent tof track)
876   */   */
877   ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno){  ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno){
878                    
879       if( !tof_obj )return 0;      if( !tof_obj )return 0;
880    
881       if( tof_obj->ToFLevel2::ntrk()==0 ){      if( tof_obj->ToFLevel2::ntrk()==0 ){
882           cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no ToF tracks are stored"<<endl;          cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no ToF tracks are stored"<<endl;
883           return NULL;          return NULL;
884       };      };
885            
886       ToFTrkVar *c = 0;      ToFTrkVar *c = 0;
887       Int_t it_tof=0;      Int_t it_tof=0;
888            
889       do{      do{
890           c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof);          c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof);
891           it_tof++;          it_tof++;
892       } while( c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk());              } while( c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk());
893            
894       if(!c || seqno != c->trkseqno){      if(!c || seqno != c->trkseqno){
895           c = 0;          c = 0;
896           if(seqno!=-1)cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match ToF stored tracks"<<endl;          if(seqno!=-1)cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match ToF stored tracks"<<endl;
897       };      };
898       return c;      return c;
899            
900   };  };
901    
902  //--------------------------------------  //--------------------------------------
903   //  //
904   //  //
905  //--------------------------------------  //--------------------------------------
906  /**  /**
907   * Give the pamela track associated to a tracker track, retrieving related calorimeter and tof track information.   * Retrieves the OrbitalInfo track matching the seqno-th tracker stored track.
908     * (If seqno = -1 retrieves the tracker-independent tof related track)
909   */   */
910   PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t){  OrbitalInfoTrkVar *PamLevel2::GetOrbitalInfoStoredTrack(int seqno){
911            
912        if( !orb_obj )return 0;
913    
914    
915        if( orb_obj->OrbitalInfo::ntrk()==0 ){
916    //       // TRICK BEGIN
917    //       OrbitalInfoTrkVar  *r = new OrbitalInfoTrkVar(); // TEMPORARY TRICK
918    //       Int_t nn = 0;
919    //       TClonesArray &tor = *orb_obj->OrbitalInfoTrk;
920    //       for(Int_t nt=0; nt < tof_obj->ToFLevel2::ntrk(); nt++){  
921    //      //
922    //      ToFTrkVar *ptt = tof_obj->ToFLevel2::GetToFTrkVar(nt);
923    //      if ( ptt->trkseqno != -1  ){
924    //        //
925    //        r->trkseqno = ptt->trkseqno;
926    //        //
927    //        r->Eij = 0;  
928    //        //
929    //        r->Sij = 0;
930    //        //        
931    //        r->pitch = -1000.;
932    //        //
933    //        r->cutoff = -1000.;
934    //        //
935    //        new(tor[nn]) OrbitalInfoTrkVar(*r);
936    //        nn++;
937    //        //
938    //        r->Clear();
939    //        //
940    //      };
941    //       };
942    //       delete r;
943    //       OrbitalInfoTrkVar *c = 0;
944    //       c = orb_obj->OrbitalInfo::GetOrbitalInfoTrkVar(0);
945    //       return c;
946    //       //TRICK END
947          cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no OrbitalInfo tracks are stored"<<endl;
948          return NULL;
949        };
950        
951        OrbitalInfoTrkVar *c = 0;
952        Int_t it_tof=0;
953        
954        do{
955            c = orb_obj->OrbitalInfo::GetOrbitalInfoTrkVar(it_tof);
956            it_tof++;
957        } while( c && seqno != c->trkseqno && it_tof < orb_obj->OrbitalInfo::ntrk());      
958            
959       cout <<"PamLevel2::GetPamTrackAlong(TrkTrack* t) **obsolete** "<<endl;      if(!c || seqno != c->trkseqno){
960       cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;          c = 0;
961            if(seqno!=-1)cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match OrbitalInfo stored tracks"<<endl;
962        };
963        return c;
964        
965    };
966    
967       CaloTrkVar *c = 0;  //--------------------------------------
968       ToFTrkVar  *o = 0;  //
969    //
970    //--------------------------------------
971    /**
972     * Give the pamela track associated to a tracker track, retrieving related calorimeter, orbitalinfo and tof track information.
973     */
974    PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t){
975        
976        cout <<"PamLevel2::GetPamTrackAlong(TrkTrack* t) **obsolete** "<<endl;
977        cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;
978    
979        CaloTrkVar *c = 0;
980        ToFTrkVar  *o = 0;
981        OrbitalInfoTrkVar  *r = 0;
982            
983       if(CAL2) c = GetCaloStoredTrack(t->GetSeqNo());      if(CAL2) c = GetCaloStoredTrack(t->GetSeqNo());
984       if(TOF) o = GetToFStoredTrack(t->GetSeqNo());      if(TOF) o = GetToFStoredTrack(t->GetSeqNo());
985        if(ORB) r = GetOrbitalInfoStoredTrack(t->GetSeqNo());
986            
987  //    if(t && c && o)track = new PamTrack(t,c,o);  //    if(t && c && o)track = new PamTrack(t,c,o);
988       PamTrack *track = new PamTrack(t,c,o);      PamTrack *track = new PamTrack(t,c,o,r);
989            
990       return track;      return track;
991    
992   };  };
993  //--------------------------------------  //--------------------------------------
994  //  //
995  //  //
# Line 440  void *PamLevel2::GetPointerTo(const char Line 1003  void *PamLevel2::GetPointerTo(const char
1003  PamTrack* PamLevel2::GetStoredTrack(Int_t itrk){  PamTrack* PamLevel2::GetStoredTrack(Int_t itrk){
1004            
1005      cout <<"PamLevel2::GetStoredTrack(Int_t itrk) **to-be-updated** "<<endl;      cout <<"PamLevel2::GetStoredTrack(Int_t itrk) **to-be-updated** "<<endl;
1006      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;
1007      cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;      cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;
1008      PamTrack *track = 0;      PamTrack *track = 0;
1009            
# Line 460  PamTrack* PamLevel2::GetStoredTrack(Int_ Line 1023  PamTrack* PamLevel2::GetStoredTrack(Int_
1023  }  }
1024  //--------------------------------------  //--------------------------------------
1025  //  //
1026    
1027    /**
1028     * Sort physical (tracker) tracks. Left here as backward compatibility method.
1029     **/
1030    void PamLevel2::SortTracks(TString how){
1031        printf(" WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",how.Data());
1032        howtosort = how;  
1033        SortTracks();
1034    };
1035    
1036  //  //
1037  //--------------------------------------  //--------------------------------------
1038  /**  /**
1039   * Sort physical (tracker) tracks and stores them in the TRefArray (of TrkTrack objects) which pointer is  PamLevel2::sorted_tracks.   * Sort physical (tracker) tracks.
1040   * @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...).
1041   * Sorting cryteria:   * Sorting cryteria:
1042   * TRK: lower chi**2   * TRK: lower chi**2
1043   * CAL: lower Y spatial residual on the first calorimeter plane   * CAL: lower Y spatial residual on the first calorimeter plane
1044   * 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).
1045     * S1: (ask Emiliano)
1046     * S2: (ask Emiliano)
1047     * S3: (ask Emiliano)
1048     * GP: more GP hits
1049   * The default sorting cryterium is "TOF+CAL".   * The default sorting cryterium is "TOF+CAL".
1050   *   *
1051   * 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).
1052   */   */
1053  void PamLevel2::SortTracks(TString how){  void PamLevel2::SortTracks(){
1054        TString how = howtosort;
1055    
1056  //    cout <<" PamLevel2::SortTracks(TString how) "<<endl;      //  cout <<" PamLevel2::SortTracks(TString how) "<<endl;
1057      if( !trk2_obj ){      if( !trk2_obj ){
1058          cout << "void PamLevel2::SortTracks(TString how):  TrkLevel2 not loaded !!!";          cout << "void PamLevel2::SortTracks():  TrkLevel2 not loaded !!!";
1059          return;          return;
1060      };      };
1061  //    cout << "call SortTracs() "<<endl;      //Save current Object count
   //Save current Object count  
1062      Int_t ObjectNumber = TProcessID::GetObjectCount();      Int_t ObjectNumber = TProcessID::GetObjectCount();
1063    
1064  //    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();  
   
1065      if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());      if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
1066      tsorted->Delete();      tsorted->Delete();
1067      TClonesArray &ttsorted = *tsorted;      TClonesArray &ttsorted = *tsorted;
1068    
1069      if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());      if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
1070      timage->Delete();      timage->Delete();
1071      TClonesArray &ttimage = *timage;      TClonesArray &ttimage = *timage;
1072    
1073    
1074      // loop over the tracks sorted by the tracker  
1075        //--------------------------------------------------
1076        // retrieve sorting method
1077        //--------------------------------------------------
1078      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
1079      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
1080      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
1081            Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase);
1082        Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase);
1083        Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase);
1084        Bool_t use_GP = how.Contains("GP", TString::kIgnoreCase);
1085      
1086        if ( use_TOF ){
1087            use_S1 = true;
1088            use_S2 = true;
1089            use_S3 = true;
1090        };
1091      if( !CAL2 &&  use_CAL) use_CAL = false;      if( !CAL2 &&  use_CAL) use_CAL = false;
1092      if( !TOF &&  use_TOF) use_TOF = false;      if( !TOF ){
1093                use_TOF = false;
1094            use_S1  = false;
1095            use_S2  = false;
1096            use_S3  = false;
1097        }
1098        if( !GP ){
1099            use_GP  = false;
1100        }
1101    
1102      if( !TRK2 ){      if( !TRK2 ){
1103  //      cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;          cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
1104          return;          return;
1105      };      };
1106    
1107      //   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;
1108          
1109    
1110        //--------------------------------------------------
1111        // loop over "physical" tracks sorted by the tracker
1112        //--------------------------------------------------
1113      for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){      for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){
1114                    
1115          TrkTrack   *ts = 0;          TrkTrack   *ts = 0;
1116          CaloTrkVar *cs = 0;          CaloTrkVar *cs = 0;
1117          ToFTrkVar  *os = 0;          ToFTrkVar  *os = 0;
1118            OrbitalInfoTrkVar  *rs = 0;
1119                    
1120          // get tracker tracks          // get tracker tracks
1121          TrkTrack   *tp = trk2_obj->TrkLevel2::GetTrack(i);                    //tracker          TrkTrack   *tp = trk2_obj->TrkLevel2::GetTrack(i); //tracker
1122          CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());          CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());
1123          ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());          ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());
1124            OrbitalInfoTrkVar  *rp = GetOrbitalInfoStoredTrack(tp->GetSeqNo());
1125    
1126          TrkTrack   *ti = 0;              //tracker (image)          TrkTrack   *ti = 0; //tracker (image)
1127          CaloTrkVar *ci = 0;          CaloTrkVar *ci = 0;
1128          ToFTrkVar  *oi = 0;          ToFTrkVar  *oi = 0;
1129  //      cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;          OrbitalInfoTrkVar  *ri = 0;
1130            //      cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
1131          // if track has an image, check image selection          // if track has an image, check image selection
1132    
1133            Int_t tp_score = 0;  //main track sorted by the tracker
1134            Int_t ti_score = 0;  //image track
1135            Int_t totp_score = 0;  //main track sorted by the tracker
1136            Int_t toti_score = 0;  //image track
1137    
1138          if(tp->HasImage()){          if(tp->HasImage()){
1139                            
1140              ti = trk2_obj->TrkLevel2::GetTrackImage(i);              //tracker (image)              ti = trk2_obj->TrkLevel2::GetTrackImage(i);              //tracker (image)
1141              ci = GetCaloStoredTrack(ti->GetSeqNo());              ci = GetCaloStoredTrack(ti->GetSeqNo());
1142              oi = GetToFStoredTrack(ti->GetSeqNo());              oi = GetToFStoredTrack(ti->GetSeqNo());
1143                ri = GetOrbitalInfoStoredTrack(ti->GetSeqNo());
1144                            
1145  //          cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;              //      cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
1146    
1147              //assign starting scores              //assign starting scores
1148              Int_t tp_score = 0;  //main track sorted by the tracker              tp_score = 0;  //main track sorted by the tracker
1149              Int_t ti_score = 0;  //image track              ti_score = 0;  //image track
1150                            
1151              // ------------------------              // -----------------------------------------------------------------------------------------
1152                // *****************************************************************************************
1153                // -----------------------------------------------------------------------------------------
1154              // calorimeter check              // calorimeter check
1155              // ------------------------              // -----------------------------------------------------------------------------------------
             // check the Y spatial residual on the first calorimeter plane  
             // (cut on calorimeter variables from Emiliano)  
1156              if( use_CAL && !calo2_obj ){              if( use_CAL && !calo2_obj ){
1157                  cout << "void PamLevel2::SortTracks(TString how): how= "<<how<<" but CaloLevel2 not loaded !!!";                  cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but CaloLevel2 not loaded !!!";
1158                  return;                  return;
1159              };              };
1160                if( use_CAL && !cp &&  ci ){
1161                    ti_score++;
1162                    toti_score++;
1163                };
1164                if( use_CAL &&  cp && !ci ){
1165                    tp_score++;
1166                    totp_score++;
1167                };
1168              if(              if(
1169                  use_CAL            &&                  use_CAL            &&
1170                  calo2_obj->npcfit[1] > 15     &&   //no. of fit planes on Y view                  cp && ci &&
                 calo2_obj->varcfit[1] < 1000. &&  //fit variance on Y view  
1171                  true){                  true){
1172    
1173                                    
1174                  Float_t resy_p = cp->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_p < 0)resy_p= - resy_p;                  if(
1175                  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 &&
1176                                        true){
1177                  if(resy_p <= resy_i) tp_score++;                      tp_score++;
1178                  else                 ti_score++;                      totp_score++;
1179                    };
1180                    if(
1181                        cp->npresh < ci->npresh &&
1182                        true){
1183                        ti_score++;
1184                        toti_score++;
1185                    };
1186    
1187  //              cout << "CALO "<<tp_score<<ti_score<<endl;                  //      cout << "CALO "<<tp_score<<ti_score<<endl;
1188    
1189              };              };
1190              // ------------------------              // -----------------------------------------------------------------------------------------
1191                // *****************************************************************************************
1192                // -----------------------------------------------------------------------------------------
1193              // TOF check              // TOF check
1194              // ------------------------                  // -----------------------------------------------------------------------------------------
1195              // check the number of hit pmts along the track              // check the number of hit pmts along the track
1196              // on S12 S21 and S32, where paddles are parallel to Y axis              // on S12 S21 and S32, where paddles are parallel to Y axis
1197              if( use_TOF && !tof_obj ){              if( (use_TOF || use_S1 || use_S2 || use_S3 ) && !tof_obj ){
1198                  cout << "void PamLevel2::SortTracks(TString how): how= "<<how<<" but ToFLevel2 not loaded !!!";                  cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but ToFLevel2 not loaded !!!";
1199                  return;                  return;
1200              };              };
1201              if( use_TOF ){              //
1202                                if( (use_TOF || use_S1 || use_S2 || use_S3 ) && !op && oi ){
1203                  Int_t nphit_p =0;                  ti_score++;
1204                  Int_t nphit_i =0;                  toti_score++;
1205                                };
1206                                if( (use_TOF || use_S1 || use_S2 || use_S3 ) && op && !oi ){
1207  /*                              cout << "track: npmtadc "<< op->npmtadc << endl;                  tp_score++;
1208                                  cout << "track: npmttdc "<< op->npmttdc << endl;                  totp_score++;
1209                                  cout << "image: npmtadc "<< oi->npmtadc << endl;              };
1210                                  cout << "image: npmttdc "<< oi->npmttdc << endl;*/              if( (use_TOF || use_S1 || use_S2 || use_S3 ) && op && oi ){
1211                                    //
1212                    Float_t sen = 0.;
1213                  for (Int_t ih=0; ih < op->npmtadc; ih++){                  for (Int_t ih=0; ih < op->npmtadc; ih++){
1214                      Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );                      Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1215                      if(pl == 1 || pl == 2 || pl == 5)nphit_p++;                      if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (op->dedx).At(ih);
1216                  };                  };
                   
1217                  for (Int_t ih=0; ih < oi->npmtadc; ih++){                  for (Int_t ih=0; ih < oi->npmtadc; ih++){
1218                      Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );                      Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1219                      if(pl == 1 || pl == 2 || pl == 5)nphit_i++;                      if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (oi->dedx).At(ih);
1220                  };                  };
1221                    //
1222                    if (  sen >= sortthr && false){ // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!)
1223                        //printf(" IS A NUCLEUS! en = %f \n",sen);
1224                        //
1225                        // is a nucleus use a different algorithm
1226                        //
1227                        Int_t nz = 6; Float_t zin[6];                                          // << define TOF z-coordinates
1228                        for(Int_t ip=0; ip<nz; ip++)
1229                            zin[ip] = tof_obj->ToFLevel2::GetZTOF(tof_obj->ToFLevel2::GetToFPlaneID(ip));     // << read ToF plane z-coordinates
1230                        Trajectory *tr = new Trajectory(nz,zin);
1231                        //
1232                        Int_t nphit_p =0;
1233                        Int_t nphit_i =0;
1234                        Float_t enhit_p = 0.;
1235                        Float_t enhit_i = 0.;
1236                        //
1237                        for (Int_t ih=0; ih < op->npmtadc; ih++){
1238                            Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1239                            if(pl == 1 || pl == 2 || pl == 5){
1240                                nphit_p++;
1241                                enhit_p += (op->dedx).At(ih);
1242                            };
1243                        };
1244                        //
1245                        tp->DoTrack2(tr);
1246                        //
1247                        if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
1248                            for (Int_t ih=0; ih < op->npmtadc; ih++){
1249                                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1250                                if(pl == 0){
1251                                    nphit_p++;
1252                                    enhit_p += (op->dedx).At(ih);
1253                                };
1254                            };
1255                        };
1256                        if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
1257                            for (Int_t ih=0; ih < op->npmtadc; ih++){
1258                                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1259                                if(pl == 3){
1260                                    nphit_p++;
1261                                    enhit_p += (op->dedx).At(ih);
1262                                };
1263                            };
1264                        };
1265                        if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
1266                            for (Int_t ih=0; ih < op->npmtadc; ih++){
1267                                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1268                                if(pl == 4){
1269                                    nphit_p++;
1270                                    enhit_p += (op->dedx).At(ih);
1271                                };
1272                            };
1273                        };
1274    
1275                        for (Int_t ih=0; ih < oi->npmtadc; ih++){
1276                            Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1277                            if(pl == 1 || pl == 2 || pl == 5){
1278                                nphit_i++;  
1279                                enhit_i += (op->dedx).At(ih);
1280                            };
1281                        };
1282                        //
1283                        ti->DoTrack2(tr);
1284                        //
1285                        if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
1286                            for (Int_t ih=0; ih < oi->npmtadc; ih++){
1287                                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1288                                if(pl == 0){
1289                                    nphit_i++;
1290                                    enhit_i += (op->dedx).At(ih);
1291                                };
1292                            };
1293                        };
1294                        if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
1295                            for (Int_t ih=0; ih < oi->npmtadc; ih++){
1296                                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1297                                if(pl == 3){
1298                                    nphit_i++;
1299                                    enhit_i += (op->dedx).At(ih);
1300                                };
1301                            };
1302                        };
1303                        if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
1304                            for (Int_t ih=0; ih < oi->npmtadc; ih++){
1305                                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1306                                if(pl == 4){
1307                                    nphit_i++;
1308                                    enhit_i += (op->dedx).At(ih);
1309                                };
1310                            };
1311                        };          
1312    
1313                                    
1314                  if(                      if(
1315                      use_TOF            &&                          (use_TOF || use_S1 || use_S2 || use_S3)            &&
1316                      (nphit_p+nphit_i) !=0 &&                              (nphit_p+nphit_i) !=0 &&        
1317                      true){                          true){
1318                        
1319                            //          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);
1320                            //          printf(" score p %i score i %i \n",tp_score,ti_score);
1321                            //                if( enhit_p > enhit_i ) tp_score++;
1322                            //                if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
1323                            if ( nphit_p > nphit_i ) tp_score++;
1324                            if ( nphit_p < nphit_i ) ti_score++;
1325                            if ( nphit_p == nphit_i ){
1326                                if ( enhit_p > enhit_i ) tp_score++;
1327                                else ti_score++;
1328                            };
1329                            //          printf(" dopo score p %i score i %i \n",tp_score,ti_score);
1330                        };
1331                        delete tr;
1332                        //
1333                    } else {
1334                        // -------------
1335                        // NOT a NUCLEUS
1336                        // -------------
1337                        //printf(" NOT a NUCLEUS! en = %f \n",sen);
1338    
1339                        Int_t nphit_p =0;
1340                        Int_t nphit_i =0;
1341                    
1342                    
1343                        /*                          cout << "track: npmtadc "<< op->npmtadc << endl;
1344                                                    cout << "track: npmttdc "<< op->npmttdc << endl;
1345                                                    cout << "image: npmtadc "<< oi->npmtadc << endl;
1346                                                    cout << "image: npmttdc "<< oi->npmttdc << endl;*/
1347                    
1348    //        for (Int_t ih=0; ih < op->npmtadc; ih++){
1349    //          Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1350    //          if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
1351    //        };
1352                    
1353    //        for (Int_t ih=0; ih < oi->npmtadc; ih++){
1354    //          Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1355    //          if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
1356    //        };
1357                        // --- modified to count tdc signals (more efficient?)
1358                        // --- and to implement check on tdcflag
1359                        for (Int_t ih=0; ih < op->npmttdc; ih++){
1360                            Int_t pl = tof_obj->GetPlaneIndex( (op->pmttdc).At(ih) );
1361    //          if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
1362                            if ( (use_S1 && ( pl == 0 || pl == 1 )) || (use_S2 && ( pl == 2 || pl == 3 )) || (use_S3 && ( pl == 4 || pl == 5 )) ){
1363                                if( (op->tdcflag).At(ih)==0 )nphit_p++;
1364                            };
1365                        };
1366                    
1367                        for (Int_t ih=0; ih < oi->npmttdc; ih++){
1368                            Int_t pl = tof_obj->GetPlaneIndex( (oi->pmttdc).At(ih) );
1369    //          if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;  
1370                            if ( (use_S1 && ( pl == 0 || pl == 1 )) || (use_S2 && ( pl == 2 || pl == 3 )) || (use_S3 && ( pl == 4 || pl == 5 )) ){
1371                                if( (oi->tdcflag).At(ih)==0 )nphit_i++;    
1372                            };
1373                        };
1374                    
1375                        if(
1376                            (nphit_p+nphit_i) !=0 &&        
1377                            true){
1378                                            
1379                      if( nphit_p >= nphit_i) tp_score++;                          if ( nphit_p != nphit_i ){
1380                      else ti_score++;                              totp_score += nphit_p;
1381                                toti_score += nphit_i;
1382                                tp_score+=nphit_p;
1383                                ti_score+=nphit_i;
1384                            };
1385                            //          if     ( nphit_p > nphit_i) tp_score+=nphit_p;
1386                            //          else if( nphit_p < nphit_i) ti_score+=nphit_i;
1387                            //          else ;//niente
1388                        };
1389                  };                  };
1390  //              cout << "TOF "<<tp_score<<ti_score<<endl;                  //      cout << "TOF "<<tp_score<<ti_score<<endl;
1391              };              };
1392              if(tp_score == ti_score) use_TRK = true;  
1393              // ------------------------  
1394    //      if(tp_score == ti_score) use_TRK = true;
1395    
1396    
1397                // -----------------------------------------------------------------------------------------
1398                // *****************************************************************************************
1399                // -----------------------------------------------------------------------------------------
1400              // tracker check              // tracker check
1401              // ------------------------              // -----------------------------------------------------------------------------------------
1402              // chi**2 difference is not always large enough to distinguish among              // chi**2 difference is not always large enough to distinguish among
1403              // the real track and its image.              // the real track and its image.
1404              // 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.
1405                // *** MODIFIED ON AUGUST 2007 ***
1406              if(use_TRK){              if(use_TRK){
1407                  if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;  //      if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
1408                  else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;  //      else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
1409  //              cout << "TRK "<<tp_score<<ti_score<<endl;  
1410                    // CHECK 1 : number of points along X  
1411                    if ( tp->GetNX() >= ti->GetNX() ){
1412                        tp_score++ ;
1413                        totp_score++ ;
1414                    };
1415                    if ( tp->GetNX() <= ti->GetNX() ){
1416                        ti_score++ ;
1417                        toti_score++ ;
1418                    };
1419                    // CHECK 2 : number of points along Y  
1420                    if ( tp->GetNY() >= ti->GetNY() ){
1421                        tp_score++ ;
1422                        totp_score++ ;
1423                    };
1424                    if ( tp->GetNY() <= ti->GetNY() ){
1425                        ti_score++ ;
1426                        toti_score++ ;
1427                    };
1428    
1429                    //      cout << "TRK "<<tp_score<<ti_score<<endl;
1430              };              };
1431                            
1432              // ------------------------  
1433                // -----------------------------------------------------------------------------------------
1434                // *****************************************************************************************
1435                // -----------------------------------------------------------------------------------------
1436                // GPamela check
1437                // -----------------------------------------------------------------------------------------
1438                
1439                //---------------------------------------------------
1440                // count the number of GP hits
1441                //---------------------------------------------------
1442                if(use_GP){
1443                    int ngphits_p=0;
1444                    int ngphits_i=0;
1445                    float toll = 0.02; //200 micron
1446                    for(int ih=0; ih<GetGPamela()->Nthspe; ih++){
1447                        int ip = (Int_t) GetGPamela()->Itrpb[ih] - 1;
1448                        if(
1449                            tp &&
1450                            tp->YGood(ip) &&
1451                            fabs(tp->ym[ip]- GetGPamela()->Yavspe[ih])<toll &&
1452                            true) ngphits_p++;
1453                        if( ti &&
1454                            ti->YGood(ip) &&
1455                            fabs(ti->ym[ip]- GetGPamela()->Yavspe[ih])<toll &&
1456                            true) ngphits_i++;
1457                    }
1458                    if(
1459                        ngphits_p > ngphits_i &&
1460                        true){
1461                        tp_score++ ;
1462                        totp_score++ ;                  
1463                    }
1464                    if(
1465                        ngphits_p < ngphits_i &&
1466                        true){
1467                        ti_score++ ;
1468                        toti_score++ ;                  
1469                    }
1470                }
1471    
1472    
1473                // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1474              // the winner is....              // the winner is....
1475              // ------------------------                  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1476              if      (tp_score > ti_score) {              if      (tp_score > ti_score) {
1477  //              ts = tp;//the track sorted by the tracker!!          
1478  //              cs = cp;  
 //              os = op;  
1479              }else if (tp_score < ti_score) {              }else if (tp_score < ti_score) {
1480    
1481    
1482                  ts = ti;//its image!!                  ts = ti;//its image!!
1483                  cs = ci;                  cs = ci;
1484                  os = oi;                  os = oi;
1485                    rs = ri;
1486                    Int_t totis = toti_score;
1487    
1488                  ti = tp;//its image!!                  ti = tp;//its image!!
1489                  ci = cp;                  ci = cp;
1490                  oi = op;                  oi = op;
1491                    ri = rp;
1492    
1493                  tp = ts;//its image!!                  tp = ts;//its image!!
1494                  cp = cs;                  cp = cs;
1495                  op = os;                  op = os;
1496                    rp = rs;
1497    
1498                    toti_score = totp_score;
1499                    totp_score = totis;
1500    
1501                                    
1502              }else {              }else {
1503  //              ts = tp;  
1504  //              cs = cp;  //      cout << "Warning - track image ambiguity not solved" << endl;
1505  //              os = op;  
 //                              cout << "Warning - track image ambiguity not solved" << endl;  
 //                              cout << ts->GetNtot() << " "<< ts->chi2 << " " << npcfit[1] << " "<< nphit_p << endl;  
1506              };              };
1507                            
1508          }else{          }else{
1509  //          ts = tp;              totp_score = 1;
1510  //          cs = cp;              toti_score = 0;
1511  //          os = op;        
1512                //      ts = tp;
1513                //      cs = cp;
1514                //      os = op;
1515          };          };
1516                    
1517  //      cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;          //      cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
1518  //      sorted_tracks->Add(ts);//save the track in the sorted array          //      sorted_tracks->Add(ts);//save the track in the sorted array
1519  //      sorted_tracks.Add(ts);//save the track in the sorted array          //      sorted_tracks.Add(ts);//save the track in the sorted array
1520  //      sorted_tracks.Add(tp);//save the track in the sorted array          //      sorted_tracks.Add(tp);//save the track in the sorted array
1521  //      cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;          //      cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
1522  //      cout<<"o "<<tp<<endl;          //      cout<<"o "<<tp<<endl;
1523  //      cout<<"o "<<cp<<endl;          //      cout<<"o "<<cp<<endl;
1524  //      cout<<"o "<<op<<endl;          //      cout<<"o "<<op<<endl;
1525          new(ttsorted[i]) PamTrack(tp,cp,op);  
1526          new(ttimage[i])  PamTrack(ti,ci,oi);          new(ttsorted[i]) PamTrack(tp,cp,op,rp);
1527            new(ttimage[i])  PamTrack(ti,ci,oi,ri);
1528    
1529            ((PamTrack*)(ttsorted[i]))->SetPScore(totp_score);
1530            ((PamTrack*)(ttsorted[i]))->SetIScore(toti_score);
1531            ((PamTrack*)(ttimage[i]))->SetPScore(totp_score);
1532            ((PamTrack*)(ttimage[i]))->SetIScore(toti_score);
1533      };      };
1534    
1535      if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){      if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){
1536          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;
1537          tsorted->Delete(); tsorted=0;          tsorted->Delete(); tsorted=0;
1538          timage->Delete(); timage=0;              timage->Delete(); timage=0;    
1539      }      }
# Line 699  void PamLevel2::SortTracks(TString how){ Line 1565  void PamLevel2::SortTracks(TString how){
1565  TClonesArray *PamLevel2::GetTracks(){  TClonesArray *PamLevel2::GetTracks(){
1566    
1567  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1568      SortTracks("+CAL+TOF");      SortTracks();
1569  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1570    
1571      return tsorted;      return tsorted;
1572            
1573   };  };
1574  //--------------------------------------  //--------------------------------------
1575   //  //
1576   //  //
1577  //--------------------------------------  //--------------------------------------
1578  /**  /**
1579   * Retrieves the it-th Pamela "physical" track.   * Retrieves the it-th Pamela "physical" track.
# Line 718  PamTrack *PamLevel2::GetTrack(int it){ Line 1584  PamTrack *PamLevel2::GetTrack(int it){
1584    
1585  //    if(!trk2_obj) return 0;  //    if(!trk2_obj) return 0;
1586                    
1587  // //  *-*-*-*-*-*-*-*-*-*-*-*-*  // //  *-*-*-*+-*-*-*-*-*-*-*-*-*
1588  //     SortTracks("+CAL+TOF");  //     SortTracks("+CAL+TOF");
1589  // //  *-*-*-*-*-*-*-*-*-*-*-*-*  // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1590  // //    if(!sorted_tracks)return 0;  // //    if(!sorted_tracks)return 0;
# Line 737  PamTrack *PamLevel2::GetTrack(int it){ Line 1603  PamTrack *PamLevel2::GetTrack(int it){
1603    
1604  //    cout << "PamLevel2::GetTrack(int it) "<<endl;  //    cout << "PamLevel2::GetTrack(int it) "<<endl;
1605  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1606      SortTracks("+CAL+TOF");      SortTracks();
1607  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1608      if(!tsorted)return 0;      if(!tsorted)return 0;
1609      if(!tsorted->GetEntries())return 0;      if(!tsorted->GetEntries())return 0;
# Line 768  PamTrack *PamLevel2::GetTrack(int it){ Line 1634  PamTrack *PamLevel2::GetTrack(int it){
1634   */   */
1635  PamTrack *PamLevel2::GetTrackImage(int it){  PamTrack *PamLevel2::GetTrackImage(int it){
1636    
 //     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;  
   
   
1637  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1638      SortTracks("+CAL+TOF");      SortTracks();
1639  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1640      if(!timage)return 0;      if(!timage)return 0;
1641      if(!timage->GetEntries())return 0;      if(!timage->GetEntries())return 0;
# Line 829  PamTrack *PamLevel2::GetTrackImage(int i Line 1670  PamTrack *PamLevel2::GetTrackImage(int i
1670   */   */
1671  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ){  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ){
1672    
1673  //     if( !detlist.IsNull() ) SetWhichTrees(detlist);      if( detlist.Contains("+AUTO", TString::kIgnoreCase) ) {
1674  //     else                    GetWhichTrees(f);          cout << "+AUTO"<<endl;
1675                GetWhichTrees(f);
1676        };
1677        SetWhichTrees(detlist);
1678    
1679        if ( pam_tree ){
1680            printf("WARNING: TTree *PamLevel2::GetPamTree(TFile *fl, TString detlist) -- pam_tree already exists!\n ");
1681            return pam_tree;
1682        };
1683        //
1684    
1685      cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;      cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;
1686    
1687      SetWhichTrees(detlist);  //    SetWhichTrees(detlist);
1688    
1689      TTree *Trout =0;      TTree *Trout =0;
1690    
# Line 855  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1705  TTree *PamLevel2::GetPamTree(TFile *f, T
1705      if(ND)cout<<"ND ";      if(ND)cout<<"ND ";
1706      if(S4)cout<<"S4 ";      if(S4)cout<<"S4 ";
1707      if(ORB)cout<<"ORB ";      if(ORB)cout<<"ORB ";
1708        if(GP)cout<<"GP ";
1709      cout << endl;      cout << endl;
1710      if(SELLI)cout<<">>> Found selection-list <<<"<<endl;      if(SELLI && SELLI!=2)cout<<">>> Found selection-list <<<"<<endl; //EMILIANO
1711    
1712      f->cd();      f->cd();
1713    
# Line 907  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1758  TTree *PamLevel2::GetPamTree(TFile *f, T
1758      if(R && TRG) {      if(R && TRG) {
1759          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1760          cout << "Trigger      : set branch address TrigLevel2"<<endl;          cout << "Trigger      : set branch address TrigLevel2"<<endl;
1761          if(!Trout)Trout=O;          if(!Trout)Trout=R;
1762          else Trout->AddFriend(R);          else Trout->AddFriend(R);
1763      }else{      }else{
1764          cout << "Trigger      : missing tree"<<endl;          cout << "Trigger      : missing tree"<<endl;
# Line 917  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1768  TTree *PamLevel2::GetPamTree(TFile *f, T
1768      if(S && S4) {      if(S && S4) {
1769          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1770          cout << "S4           : set branch address S4Level2"<<endl;          cout << "S4           : set branch address S4Level2"<<endl;
1771          if(!Trout)Trout=O;          if(!Trout)Trout=S;
1772          else Trout->AddFriend(S);          else Trout->AddFriend(S);
1773      }else{      }else{
1774          cout << "S4           : missing tree"<<endl;          cout << "S4           : missing tree"<<endl;
# Line 927  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1778  TTree *PamLevel2::GetPamTree(TFile *f, T
1778      if(N && ND) {      if(N && ND) {
1779          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1780          cout << "NeutronD     : set branch address NDLevel2"<<endl;          cout << "NeutronD     : set branch address NDLevel2"<<endl;
1781          if(!Trout)Trout=O;          if(!Trout)Trout=N;
1782          else Trout->AddFriend(N);          else Trout->AddFriend(N);
1783      }else{      }else{
1784          cout << "NeutronD     : missing tree"<<endl;          cout << "NeutronD     : missing tree"<<endl;
# Line 937  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1788  TTree *PamLevel2::GetPamTree(TFile *f, T
1788      if(A && AC) {      if(A && AC) {
1789          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1790          cout << "Anticounter  : set branch address AcLevel2"<<endl;          cout << "Anticounter  : set branch address AcLevel2"<<endl;
1791          if(!Trout)Trout=O;          if(!Trout)Trout=A;
1792          else Trout->AddFriend(A);          else Trout->AddFriend(A);
1793      }else{      }else{
1794          cout << "Anticounter  : missing tree"<<endl;          cout << "Anticounter  : missing tree"<<endl;
# Line 947  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1798  TTree *PamLevel2::GetPamTree(TFile *f, T
1798      if(B && ORB) {      if(B && ORB) {
1799          B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1800          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
1801          if(!Trout)Trout=O;          if(!Trout)Trout=B;
1802          else Trout->AddFriend(B);          else Trout->AddFriend(B);
1803      }else{      }else{
1804          cout << "OrbitalInfo  : missing tree"<<endl;          cout << "OrbitalInfo  : missing tree"<<endl;
1805      };      };
1806    
1807        // GPamela
1808        TTree *G = (TTree*)f->Get("h20");
1809        if(G && GP) {
1810            if(!gp_obj)gp_obj=new GPamela();
1811    //      ------------------------------------
1812    //      ATTENZIONE!!!
1813    //      non so per quale arcano motivo,
1814    //      per l'albero di gpamela il branch address lo devo settare
1815    //      DOPO aver fatto friend
1816    //      FGRRRVZZZZUTSALKJMSLKJ!!!
1817    //      ------------------------------------
1818    //      gp_obj->SetBranchAddress(G); //ho dovuto fare in maniera diversa dagli altri
1819    //      cout << "h20          : set branch address GPamela "<<endl;
1820            if(!Trout)Trout=G;
1821            else Trout->AddFriend(G);
1822        }else{
1823    //      cout << "h20          : missing tree"<<endl;
1824        };
1825    
1826    
1827      TTree *L = (TTree*)f->Get("SelectionList");      TTree *L = (TTree*)f->Get("SelectionList");
1828      if(L && SELLI==1) {      if(L && SELLI==1) {
1829  //      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;  
1830          sel_tree = 0;          sel_tree = 0;
1831      }else{      }else{
1832          cout << "SelectionList  : missing tree"<<endl;          cout << "SelectionList  : missing tree"<<endl;
1833      };      };
1834            
1835      cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;      cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;
       
     pam_tree = (TChain*)Trout;  
1836    
1837    //      ------------------------------------
1838    //      ATTENZIONE!!!
1839    //      non so per quale arcano motivo,
1840    //      per l'albero di gpamela il branch address lo devo settare
1841    //      DOPO aver fatto friend
1842    //      FGRRRVZZZZUTSALKJMSLKJ!!!
1843    //      ------------------------------------
1844        if(G && GP) {      
1845            gp_obj->SetBranchAddress(Trout); //ho dovuto fare in maniera diversa dagli altri
1846            cout << "h20          : set branch address GPamela "<<endl;
1847        }else{
1848            cout << "h20          : missing tree"<<endl;
1849        };
1850    
1851        pam_tree = (TChain*)Trout;
1852        
1853      return Trout;      return Trout;
1854            
1855  }  }
# Line 988  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1867  TTree *PamLevel2::GetPamTree(TFile *f, T
1867  TList*  PamLevel2::GetListOfLevel2Files(TString ddir, TString flisttxt = ""){  TList*  PamLevel2::GetListOfLevel2Files(TString ddir, TString flisttxt = ""){
1868                    
1869      TString wdir = gSystem->WorkingDirectory();      TString wdir = gSystem->WorkingDirectory();
1870        //    cout << "Working directory: "<<wdir<<endl;
1871      if(ddir=="")ddir = wdir;  
1872        //    if(ddir=="")ddir = wdir;
1873  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1874      cout << "Level2 data directory : "<<  ddir << endl;      if ( ddir != ""){
1875                cout << "Level2 data directory : "<<  ddir << endl;
1876        } else {
1877            cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory " << endl;
1878        };
1879      TList *contents  = new TList; // create output list      TList *contents  = new TList; // create output list
1880      contents->SetOwner();      contents->SetOwner();
1881            
1882  //    char *fullpath;  //    char *fullpath;
1883  //    const char *fullpath;  //    const char *fullpath;
1884            
1885      // if input file list is given:          // if no input file list is given:  
1886      if ( flisttxt != "" ){      if ( flisttxt != "" ){
1887                    
1888  //      if( !gSystem->IsFileInIncludePath(flisttxt,&fullpath) ){          //      if( !gSystem->IsFileInIncludePath(flisttxt,&fullpath) ){        
# Line 1008  TList*  PamLevel2::GetListOfLevel2Files( Line 1891  TList*  PamLevel2::GetListOfLevel2Files(
1891  //          return 0;  //          return 0;
1892  //      }        //      }      
1893  //      flisttxt = fullpath;  //      flisttxt = fullpath;
1894            if ( !flisttxt.EndsWith(".root") ){
1895    
1896        if ( !flisttxt.EndsWith(".root") ){              TString dir = gSystem->DirName(flisttxt);
1897          flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));  //          cout << " List directory "<<dir<<endl;
1898                if(dir == "." || dir == "")
1899          if( !gSystem->ChangeDirectory(ddir) ){                  flisttxt = gSystem->ConcatFileName(wdir.Data(),gSystem->BaseName(flisttxt));
1900              cout << "Cannot change directory : "<<ddir<<endl;  //              flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1901              return 0;  
1902          }              if( !gSystem->ChangeDirectory(ddir) ){
1903                            cout << "Cannot change directory : "<<ddir<<endl;
1904          cout <<"Input file list : " << flisttxt <<endl;                  return 0;
         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;  
1905              }              }
1906              if(file.Contains("#"))file = file(0,file.First("#"));          
1907                cout <<"Input file list : " << flisttxt <<endl;
1908                ifstream in;
1909                in.open(flisttxt, ios::in); //open input file list
1910                if(!in.good()){
1911                    cout <<" TList*  PamLevel2::GetListOfLevel2Files(TString, TString) --> ERROR opening the file "<<endl;
1912                    gSystem->ChangeDirectory(wdir); // back to the working directory
1913                    return 0;
1914                }  
1915                int line=0;
1916                while (1) {
1917                    TString file;
1918                    in >> file;
1919                    if (!in.good()) break;
1920                    line++;
1921    //          cout <<"(1) " << file << endl;
1922                    if(file.IsNull()){
1923                        cout << "-- list interrupted at line "<<line <<endl;
1924                        break;
1925                    }
1926                    if(file.Contains("#"))file = file(0,file.First("#"));
1927  //          cout <<"(2) " << file << endl;  //          cout <<"(2) " << file << endl;
1928  //          if( gSystem->IsFileInIncludePath(file,&fullpath) ){  //          if( gSystem->IsFileInIncludePath(file,&fullpath) ){
1929  //          if( (fullpath = gSystem->FindFile(ddir,file)) ){  //          if( (fullpath = gSystem->FindFile(ddir,file)) ){
1930              if( file.EndsWith(".root") ){                  if( file.EndsWith(".root") ){
1931                  char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir),gSystem->BaseName(file));                      TString filedir;
1932                  contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list                      if (ddir != ""){
1933                  delete fullpath;                          filedir = ddir; // take the input dir
1934              }                      } else {
1935                            gSystem->ChangeDirectory(wdir); // back to the working directory
1936                            filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir
1937                        };
1938                        char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir),gSystem->BaseName(file));
1939                        contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list
1940                        delete fullpath;
1941                    }
1942  //          }else{  //          }else{
1943  //              if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;  //              if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;
1944  //          };  //          };
1945          };              };
1946          in.close();              in.close();
1947        } else {          } else {
1948            char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));              if(flisttxt.Contains("#"))flisttxt = flisttxt(0,flisttxt.First("#"));
1949            contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list              char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1950            delete fullpath;              contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list
1951        };                    delete fullpath;
1952            };      
1953      }else{      }else{
1954                    
1955          cout << "No input file list given."<<endl;          cout << "No input file list given."<<endl;
1956          cout << "Check for existing root files."<<endl;          cout << "Check for existing root files."<<endl;
1957  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;
1958                    if ( ddir == "" ){
1959                ddir = wdir;
1960                cout << "Level2 data directory : "<<  ddir << endl;
1961            };
1962    
1963          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1964          TList *temp = datadir->GetListOfFiles();          TList *temp = datadir->GetListOfFiles();
1965            if(!temp)return 0;
1966  //              temp->Print();  //              temp->Print();
1967  //              cout << "*************************************" << endl;  //              cout << "*************************************" << endl;
1968                    
# Line 1104  TList*  PamLevel2::GetListOfLevel2Files( Line 2004  TList*  PamLevel2::GetListOfLevel2Files(
2004   * @return Pointer to a TChain   * @return Pointer to a TChain
2005   */   */
2006  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){
2007        //
2008        //
2009        //
2010        if ( pam_tree ){
2011            printf("WARNING: TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) -- pam_tree already exists!\n ");
2012            return pam_tree;
2013        };
2014        //
2015            
2016      TChain *Trout =0;      TChain *Trout =0;
2017    
2018  //    if( !detlist.IsNull() )SetWhichTrees(detlist);      // -------------------------------------------
2019      SetWhichTrees(detlist);      // set flags to include/exclude trees/branches
2020            // -------------------------------------------
2021      cout<< "GetPamTree(TList*,TString): input detector list --> ";      if( detlist.Contains("+AUTO", TString::kIgnoreCase) ) {
2022      if(TRK1)cout<<"TRK1 ";          if( fl->GetEntries()>0 ){
2023      if(TRK2)cout<<"TRK2 ";              cout << "+AUTO"<<endl;
2024      if(TRKh)cout<<"TRKH ";              TFile *fprimo = new TFile( fl->At(0)->GetName() );
2025      if(CAL1)cout<<"CAL1 ";              GetWhichTrees(fprimo);
2026      if(CAL2)cout<<"CAL2 ";              fprimo->Close();// AAAAARGGGGGHHHHH!!!!!!! non commentare questa riga, altrimenti si incasina il TChain
2027      if(TOF)cout<<"TOF ";              fprimo->Delete();
2028      if(TRG)cout<<"TRG ";          }
2029      if(AC)cout<<"AC ";      };
2030      if(ND)cout<<"ND ";      SetWhichTrees(detlist);    
2031      if(S4)cout<<"S4 ";  
     if(ORB)cout<<"ORB ";  
     cout << endl;  
2032    
2033        // -------------------------------------------
2034        // build chains
2035        // -------------------------------------------
2036      TChain *T = 0;            TChain *T = 0;      
2037      TChain *C = 0;      TChain *C = 0;
2038      TChain *O = 0;      TChain *O = 0;
# Line 1132  TChain *PamLevel2::GetPamTree(TList *fl, Line 2041  TChain *PamLevel2::GetPamTree(TList *fl,
2041      TChain *N = 0;      TChain *N = 0;
2042      TChain *A = 0;      TChain *A = 0;
2043      TChain *B = 0;      TChain *B = 0;
2044        TChain *G = 0;
2045    
2046      TChain *L = 0;      TChain *L = 0;
2047            
# Line 1143  TChain *PamLevel2::GetPamTree(TList *fl, Line 2053  TChain *PamLevel2::GetPamTree(TList *fl,
2053      if(ND)               N = new TChain("NeutronD");      if(ND)               N = new TChain("NeutronD");
2054      if(AC)               A = new TChain("Anticounter");      if(AC)               A = new TChain("Anticounter");
2055      if(ORB)              B = new TChain("OrbitalInfo");      if(ORB)              B = new TChain("OrbitalInfo");
2056        if(GP)               G = new TChain("h20");
2057       L = new TChain("SelectionList");      L = new TChain("SelectionList");
2058            
2059      // loop over files and create chains              // loop over files and create chains        
2060      TIter next(fl);      TIter next(fl);
# Line 1161  TChain *PamLevel2::GetPamTree(TList *fl, Line 2071  TChain *PamLevel2::GetPamTree(TList *fl,
2071              if(ND)               N->Add(name);              if(ND)               N->Add(name);
2072              if(AC)               A->Add(name);              if(AC)               A->Add(name);
2073              if(ORB)              B->Add(name);              if(ORB)              B->Add(name);
2074                if(GP)               G->Add(name);
2075              if(SELLI==1)         L->Add(name);              if(SELLI==1)         L->Add(name);
2076          };          };
2077      }      };
2078            
2079      cout << "done chain \n";      cout << "done chains\n";
2080        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2081    
2082    
2083        // -------------------------------------------
2084        // make friends
2085        // -------------------------------------------
2086    
 //    UInt_t *found=0;  
2087  // Tracker  // Tracker
2088        cout << "Friends: "<<endl;
2089      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;  
2090          if(!Trout)Trout=T;          if(!Trout)Trout=T;
2091          else Trout->AddFriend("Tracker");          else Trout->AddFriend("Tracker");
2092      }else{  //      cout << "+Tacker"<<endl;
         cout << "Tracker      : missing tree"<<endl;  
2093      };      };
2094      // Calorimeter      // Calorimeter
2095      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;  
2096          if(!Trout)Trout=C;          if(!Trout)Trout=C;
2097          else Trout->AddFriend("Calorimeter");          else Trout->AddFriend("Calorimeter");
2098      }else{  //      cout << "+Calorimeter"<<endl;
         cout << "Calorimeter  : missing tree"<<endl;  
2099      };      };
2100      // ToF          // ToF    
2101      if(O && TOF) {      if(O && TOF) {
         O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));  
         cout << "ToF          : set branch address ToFLevel2"<<endl;  
2102          if(!Trout)Trout=O;          if(!Trout)Trout=O;
2103          else Trout->AddFriend("ToF");          else Trout->AddFriend("ToF");
2104      }else{  //      cout << "+ToF"<<endl;
         cout << "ToF         : missing tree"<<endl;  
2105      };      };
2106      // Trigger      // Trigger
2107      if(R && TRG) {      if(R && TRG) {
         R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));  
         cout << "Trigger      : set branch address TrigLevel2"<<endl;  
2108          if(!Trout)Trout=O;          if(!Trout)Trout=O;
2109          else Trout->AddFriend("Trigger");          else Trout->AddFriend("Trigger");
2110      }else{  //      cout << "+Trigger"<<endl;
         cout << "Trigger      : missing tree"<<endl;  
2111      };      };
2112      // S4      // S4
2113      if(S && S4) {      if(S && S4) {
         S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));  
         cout << "S4           : set branch address S4Level2"<<endl;  
2114          if(!Trout)Trout=O;          if(!Trout)Trout=O;
2115          else Trout->AddFriend("S4");          else Trout->AddFriend("S4");
2116      }else{  //      cout << "+S4"<<endl;
         cout << "S4           : missing tree"<<endl;  
2117      };      };
2118      // Neutron Detector      // Neutron Detector
2119      if(N && ND) {      if(N && ND) {
         N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));  
         cout << "NeutronD     : set branch address NDLevel2"<<endl;  
2120          if(!Trout)Trout=O;          if(!Trout)Trout=O;
2121          else Trout->AddFriend("NeutronD");          else Trout->AddFriend("NeutronD");
2122      }else{  //      cout << "+NeutronD"<<endl;
         cout << "NeutronD     : missing tree"<<endl;  
2123      };      };
2124      // Anticounters      // Anticounters
2125      if(A && AC) {      if(A && AC) {
         A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));  
         cout << "Anticounter  : set branch address AcLevel2"<<endl;  
2126          if(!Trout)Trout=O;          if(!Trout)Trout=O;
2127          else Trout->AddFriend("Anticounter");          else Trout->AddFriend("Anticounter");
2128      }else{  //      cout << "+Anticounter"<<endl;
         cout << "Anticounter  : missing tree"<<endl;  
2129      };      };
2130      // Orbital Info      // Orbital Info
2131      if(B && ORB) {      if(B && ORB) {
         B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));  
         cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;  
2132          if(!Trout)Trout=O;          if(!Trout)Trout=O;
2133          else Trout->AddFriend("OrbitalInfo");          else Trout->AddFriend("OrbitalInfo");
2134      }else{  //      cout << "+OrbitalInfo"<<endl;
2135          cout << "OrbitalInfo  : missing tree"<<endl;      };
2136        // GPamela
2137        if(G && GP) {
2138            if(!Trout)Trout=G;
2139            else Trout->AddFriend("h20");
2140    //      cout << "+h20"<<endl;
2141      };      };
2142    
2143    //  =====================================
2144    //  SET BRANCH-ADDRESS AFTER CHAIN+FRIEND
2145    //  =====================================
2146        SetBranchAddress(Trout);
2147    
2148    
2149    //  ------------------------------------
2150    //  finally handle selection trees...
2151    //  (it is not friend of pamela tree)
2152    //  ------------------------------------
2153    
2154        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2155    
2156      // Selection List      // Selection List
2157      if(L && SELLI==1) {      if(L && SELLI==1) {
2158          cout<<">>> Found selection-list <<<"<<endl;          cout<<">>> Found selection-list <<<"<<endl;
2159          L->SetBranchAddress("RunEntry",&irun);  //      L->SetBranchAddress("RunEntry",&irun);
2160            L->SetBranchAddress("RunEntry",&irunt);//NEWNEW
2161          cout << "SelectionList: set branch address RunEntry"<<endl;          cout << "SelectionList: set branch address RunEntry"<<endl;
2162          L->SetBranchAddress("EventEntry",&irunentry);          L->SetBranchAddress("EventEntry",&irunentry);
2163          cout << "SelectionList: set branch address EventEntry"<<endl;          cout << "SelectionList: set branch address EventEntry"<<endl;
2164          sel_tree = L;          sel_tree = L;
2165  //      if(!Trout)Trout=O;  //      if(!Trout)Trout=O;
2166  //      else Trout->AddFriend("SelectionList");  //      else Trout->AddFriend("SelectionList");
2167            cout << "+SelectionList"<<endl;
2168            cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2169      }else{      }else{
2170  //      cout << "SelectionList  : missing tree"<<endl;  //      cout << "SelectionList  : missing tree"<<endl;
2171          if(L)L->Delete();          if(L)L->Delete();
2172      };      };
       
 //    cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;  
2173    
2174      pam_tree = Trout;  //  --------------------------------------------
2175    //  return the pamela chain with all the friends
2176    //  --------------------------------------------
2177    
2178        pam_tree = Trout;
2179        
2180      return Trout;      return Trout;
2181  }  }
2182    
# Line 1296  void PamLevel2::SetBranchAddress(TTree * Line 2202  void PamLevel2::SetBranchAddress(TTree *
2202      ND     = ND  & t->GetBranchStatus("NDLevel2");      ND     = ND  & t->GetBranchStatus("NDLevel2");
2203      AC     = AC  & t->GetBranchStatus("AcLevel2");      AC     = AC  & t->GetBranchStatus("AcLevel2");
2204      ORB    = ORB & t->GetBranchStatus("OrbitalInfo");      ORB    = ORB & t->GetBranchStatus("OrbitalInfo");
2205        GP     = GP  & t->GetBranchStatus("h20");
2206    
2207    
2208      // Tracker      // Tracker
# Line 1352  void PamLevel2::SetBranchAddress(TTree * Line 2259  void PamLevel2::SetBranchAddress(TTree *
2259          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
2260          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
2261      };      };
2262      // SelectionList      // GPamela
2263      if(SELLI==1) {      if(GP) {
2264          t->SetBranchAddress("RunEntry", &irun);  //      GetPointerTo("GPamela");
2265          cout << "SelectionList: set branch address RunEntry"<<endl;          if(!gp_obj)gp_obj = new GPamela();
2266          t->SetBranchAddress("EventEntry", &irunentry);  //      gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2267          cout << "SelectionList: set branch address EventEntry"<<endl;  // //   t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
2268            if(SELLI) t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
2269            else      gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2270    
2271            cout << "h20          : set branch address GPamela "<<endl;
2272      };      };
2273            
2274  }  }
# Line 1366  void PamLevel2::SetBranchAddress(TTree * Line 2277  void PamLevel2::SetBranchAddress(TTree *
2277   */   */
2278  void PamLevel2::SetBranchAddress(TChain *t){  void PamLevel2::SetBranchAddress(TChain *t){
2279    
2280      TRK2    = TRK2 & t->GetBranchStatus("TrkLevel2");  //     TRK2    = TRK2 & t->GetBranchStatus("TrkLevel2");
2281      TRK1    = TRK1 & t->GetBranchStatus("TrkLevel1");  //     TRK1    = TRK1 & t->GetBranchStatus("TrkLevel1");
2282      TRKh    = TRKh & t->GetBranchStatus("TrkHough");  //     TRKh    = TRKh & t->GetBranchStatus("TrkHough");
2283      CAL1    = CAL1 & t->GetBranchStatus("CaloLevel1");  //     CAL1    = CAL1 & t->GetBranchStatus("CaloLevel1");
2284      CAL2    = CAL2 & t->GetBranchStatus("CaloLevel2");  //     CAL2    = CAL2 & t->GetBranchStatus("CaloLevel2");
2285      TOF    = TOF & t->GetBranchStatus("ToFLevel2");  //     TOF    = TOF & t->GetBranchStatus("ToFLevel2");
2286      TRG    = TRG & t->GetBranchStatus("TrigLevel2");  //     TRG    = TRG & t->GetBranchStatus("TrigLevel2");
2287      S4     = S4  & t->GetBranchStatus("S4Level2");  //     S4     = S4  & t->GetBranchStatus("S4Level2");
2288      ND     = ND  & t->GetBranchStatus("NDLevel2");  //     ND     = ND  & t->GetBranchStatus("NDLevel2");
2289      AC     = AC  & t->GetBranchStatus("AcLevel2");  //     AC     = AC  & t->GetBranchStatus("AcLevel2");
2290      ORB    = ORB & t->GetBranchStatus("OrbitalInfo");  //     ORB    = ORB & t->GetBranchStatus("OrbitalInfo");
2291    //    GP     = GP & t->GetBranchStatus("h20");
2292    
2293      // Tracker      // Tracker
2294       if(TRK2) {      if(TRK2) {
2295          t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));          t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
2296          cout << "Tracker      : set branch address TrkLevel2"<<endl;          cout << "Tracker      : set branch address TrkLevel2"<<endl;
2297      };      };
# Line 1387  void PamLevel2::SetBranchAddress(TChain Line 2299  void PamLevel2::SetBranchAddress(TChain
2299          t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));          t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
2300          cout << "Tracker      : set branch address TrkLevel1"<<endl;          cout << "Tracker      : set branch address TrkLevel1"<<endl;
2301      };      };
2302       if(TRKh) {      if(TRKh) {
2303          t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));          t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
2304          cout << "Tracker      : set branch address TrkHough"<<endl;          cout << "Tracker      : set branch address TrkHough"<<endl;
2305       };      };
2306            
2307      // Calorimeter      // Calorimeter
2308      if(CAL2) {      if(CAL2) {
# Line 1432  void PamLevel2::SetBranchAddress(TChain Line 2344  void PamLevel2::SetBranchAddress(TChain
2344          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
2345          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
2346      };      };
2347      // SelectionList      // GPamela
2348      if(SELLI==1) {  //    cout <<"GP "<<GP<<endl;
2349          t->SetBranchAddress("RunEntry", &irun);      if(GP) {
2350          cout << "SelectionList: set branch address RunEntry"<<endl;  //      GetPointerTo("GPamela");
2351          t->SetBranchAddress("EventEntry", &irunentry);          if(!gp_obj)gp_obj = new GPamela();
2352          cout << "SelectionList: set branch address EventEntry"<<endl;          if(SELLI) t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
2353            else      gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2354    //      gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2355            cout << "h20          : set branch address GPamela "<<endl;
2356      };      };
2357            // SelectionList
2358    //     if(SELLI==1){
2359    //      t->SetBranchAddress("RunEntry",&irunt);//NEWNEW
2360    //      cout << "SelectionList: set branch address RunEntry"<<endl;
2361    //      t->SetBranchAddress("EventEntry",&irunentry);
2362    //      cout << "SelectionList: set branch address EventEntry"<<endl;
2363            
2364    //     }
2365    
2366  }  }
2367    
2368    
# Line 1453  void PamLevel2::SetBranchAddress(TChain Line 2376  void PamLevel2::SetBranchAddress(TChain
2376   * @return Pointer to a TChain   * @return Pointer to a TChain
2377   */   */
2378  TChain *PamLevel2::GetRunTree(TList *fl){  TChain *PamLevel2::GetRunTree(TList *fl){
2379                //
2380        //
2381        //
2382        if ( run_tree ){
2383            printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");
2384            return run_tree;
2385        };  
2386        //
2387    
2388    
2389      TChain *R = new TChain("Run");      TChain *R = new TChain("Run");
2390            
2391      // loop over files and create chains              // loop over files and create chains        
# Line 1466  TChain *PamLevel2::GetRunTree(TList *fl) Line 2398  TChain *PamLevel2::GetRunTree(TList *fl)
2398              R->Add(name);              R->Add(name);
2399          };          };
2400      }      }
2401        
2402    
2403      if(R->GetNtrees()){      if(RUN && R->GetNtrees()){
2404            
2405          R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));          R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
2406          cout << "Run         : set branch address RunInfo"<<endl;          cout << "Run         : set branch address RunInfo"<<endl;
2407          R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano          R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
2408          cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano          cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano
2409    
2410            irunoffset = new int[R->GetNtrees()];
2411            if(DBG){
2412                cout << "----------------------------------------------------"<<endl;
2413                cout << "irun\t | ";
2414                cout << "tree\t |";
2415    //      cout << "offset\t |";
2416                cout << "RUN\t";
2417                cout << "FRAG\t";
2418                cout << "NEVENTS\t";
2419                cout << "absolute time\t\t\t";
2420                cout << "on-board time";
2421                cout<<endl;
2422            }
2423            for (Int_t ii=0; ii<R->GetEntries(); ii++){
2424                R->GetEntry(ii);
2425                if(DBG){
2426                    cout << ii<< "\t | ";
2427                    cout << R->GetTreeNumber()<< "\t |";
2428    //          cout << R->GetChainOffset()<< "\t |";
2429                    cout <<GetRunInfo()->ID<<"\t";
2430                    cout <<GetRunInfo()->ID_RUN_FRAG<<"\t";
2431                    cout <<GetRunInfo()->NEVENTS<< "\t";
2432                    cout <<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME <<"\t";
2433                    cout <<GetRunInfo()->RUNHEADER_OBT<<" <---> "<<GetRunInfo()->RUNTRAILER_OBT<<"\t";
2434                    cout <<endl;
2435                }
2436                irunoffset[R->GetTreeNumber()]=R->GetChainOffset();
2437            }
2438            cout << "N.run = "<<R->GetEntries()<<endl;
2439            cout << "----------------------------------------------------"<<endl;
2440    
2441    
2442      }else{      }else{
2443          delete R;          delete R;
2444          R=0;          R=0;
2445      }      }
2446        
2447    
2448      run_tree = R;      run_tree = R;
2449    
# Line 1493  TChain *PamLevel2::GetRunTree(TList *fl) Line 2460  TChain *PamLevel2::GetRunTree(TList *fl)
2460   * @return Pointer to a TTree   * @return Pointer to a TTree
2461   */   */
2462  TTree *PamLevel2::GetRunTree(TFile *f){  TTree *PamLevel2::GetRunTree(TFile *f){
2463        if ( run_tree ){
2464            printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");
2465            return run_tree;
2466        };
2467            
2468    
2469      cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;      cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;
2470    
2471      TTree *R = (TTree*)f->Get("Run");      TTree *T = (TTree*)f->Get("Run");
2472            
2473      if(R){      if(T){
2474          R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));          T->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
2475          cout << "Run         : set branch address RunInfo"<<endl;          cout << "Run         : set branch address RunInfo"<<endl;
2476          R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano          T->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
2477          cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano          cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano
2478    
2479      }      }
2480    
2481      run_tree = (TChain*)R;      run_tree = (TChain*)T;
2482    
2483      return R;      return T;
2484            
2485  }  }
2486  /**  /**
# Line 1515  TTree *PamLevel2::GetRunTree(TFile *f){ Line 2488  TTree *PamLevel2::GetRunTree(TFile *f){
2488   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
2489   * @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
2490   */   */
2491  Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev){  Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev){
2492    //      //
2493    // check if we have already called once GetEntry, if not call it      // check if we have already called once GetEntry, if not call it
2494    //      //
2495    if ( run->GetEntries() <= 0 ) return(false);      cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) --- ATTENZIONE --- NON E` MANTENUTA!!!!!!!.... "<<endl;
2496    //      if(!run){
2497            cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing RunInfo tree "<<endl;
2498            return(false);  
2499        }
2500        if ( run->GetEntries() <= 0 ) return(false);
2501        //
2502        
2503    Int_t oldrun = irun;      //  Int_t oldrun = irun;
2504    // --------------------------------------      Long64_t oldrun = irun;
2505    // if it is a full file (not preselected)  
2506    // --------------------------------------      // --------------------------------------
2507    if(SELLI==0){      // if it is a full file (not preselected)
2508        if ( irun < 0 ){      // --------------------------------------
2509            irun = 0;      if(SELLI==0){
2510            run->GetEntry(0);  
2511            runfirstentry = 0ULL;          //
2512            runlastentry += (ULong64_t)(this->GetRunInfo()->NEVENTS) - 1ULL;          // the absolute time is necessary to relate the event with the run
2513        };                //
2514            if( !GetOrbitalInfo() && !ISGP){
2515        if( !GetOrbitalInfo() ){              cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
2516            cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;              return(false);
2517            return(false);          }
2518        }  
2519        // modificato il controllo sull'aggiornamento del run, per evitare problemi          ULong64_t abstime = 0;
2520        // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)          if( GetOrbitalInfo() )abstime=GetOrbitalInfo()->absTime;
2521        while ( GetOrbitalInfo()->absTime > GetRunInfo()->RUNTRAILER_TIME && irun < run->GetEntries() ){  
2522    
2523            //
2524            // the first time the routine is called, set run search from the beginning
2525            //
2526            if ( irun < 0LL ){
2527                irun = 0LL;
2528                run->GetEntry(irun);
2529                runfirstentry = 0LL;
2530                runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2531                if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
2532              
2533                if( ISGP && run->GetEntries()!=1 ){
2534                    cout << "** WARNING ** simulated files are assumed to have 1 single run, not "<< run->GetEntries() << endl;
2535                    cout << "** WARNING ** run will not be updated"<<endl;
2536                }
2537    
2538            };      
2539            //
2540            if(ISGP)abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO
2541            //
2542            if ( irun == run->GetEntries()-1LL &&
2543                 !(abstime >= GetRunInfo()->RUNHEADER_TIME &&
2544                   abstime <= GetRunInfo()->RUNTRAILER_TIME)
2545                ){
2546                irun = -1LL;
2547                irunt = -1LL;
2548                runfirstentry = 0LL;
2549                runlastentry = -1LL;
2550            };
2551            // modificato il controllo sull'aggiornamento del run, per evitare problemi
2552            // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)
2553            //
2554            bool fromfirst = true;
2555            //
2556            while ( !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME) && irun < run->GetEntries()-1LL ){
2557  //      while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){  //      while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){
2558            //    printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));              irun++;
2559            irun++;              run->GetEntry(irun);
2560            run->GetEntry(irun);              runfirstentry = runlastentry;
2561            runfirstentry = runlastentry+1ULL;              if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runfirstentry += 1LL;
2562            runlastentry += (ULong64_t)(this->GetRunInfo()->NEVENTS);              runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2563        };  //        cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2564          //        cout << "runfirstentry "<<runfirstentry<<endl;
2565        //              //    printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));
2566        if ( irun == oldrun || irun >= run->GetEntries() ) return(false);              //    printf(" abstime %u trailertime %u \n",abstime,GetRunInfo()->RUNTRAILER_TIME);
2567        //              //    printf(" IDRUN %u \n",GetRunInfo()->ID);
2568        //  printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);              //
2569        //  //        prevshift = 0;
2570        return(true);                  //
2571    };              if ( irun == (Long64_t)(run->GetEntries()-1LL) && fromfirst && !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME)){
2572    // ----------------------------------------------------                  printf(" resetting irun  (it should NOT happen!!!)\n");
2573    // if it is a preselected file (there is SelectionList)                  fromfirst = false;
2574    // NBNB - the event tree MUST be read first                  irun = 0;
2575    // ----------------------------------------------------                  run->GetEntry(irun);
2576    if(SELLI==1){                        runfirstentry = 0ULL;
2577        sel_tree->GetEntry(iev);                  runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2578        if(irun != oldrun){                  if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
2579            run->GetEntry(irun);              };
2580            return true;              //
2581        }          };
2582        return false;          //
2583    }          if (
2584                !(abstime >= GetRunInfo()->RUNHEADER_TIME &&
2585                  abstime <= GetRunInfo()->RUNTRAILER_TIME)
2586                ) {
2587                printf(" Something very wrong here: cannot find RUN containing absolute time %llu \n",abstime);
2588                return false;
2589            }
2590            //
2591            if ( irun == oldrun || irun >= run->GetEntries() ) return(false);
2592            //
2593            //  printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);
2594            //
2595            prevshift = 0;
2596            cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2597    //      cout << "runfirstentry "<<runfirstentry<<endl;
2598            return(true);    
2599        };
2600        // ----------------------------------------------------
2601        // if it is a preselected file (there is SelectionList)
2602        // NBNB - the event tree MUST be read first
2603        // ----------------------------------------------------
2604        if(SELLI==1){      
2605            sel_tree->GetEntry(iev);
2606    //      cout << irun << " "<< irunentry << endl;
2607            if(irun != oldrun){
2608                run->GetEntry(irun);
2609                cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2610                prevshift = 0;
2611                return true;
2612            }
2613            return false;
2614        }
2615    
2616    return false;      return false;
2617    //      //
2618    };
2619    
2620    Bool_t PamLevel2::UpdateRunInfo(Long64_t iev){
2621    
2622        if(!run_tree){
2623            cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded"<<endl;
2624            return false;
2625        }
2626        if ( run_tree->GetEntries() <= 0 ) {
2627            cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty"<<endl;
2628            return(false);
2629        }
2630      
2631        Int_t oldrun = irun; // store current run index
2632    
2633        // -----------------------------------------------------------------------
2634        // the first time the routine is called, set run search from the beginning
2635        // -----------------------------------------------------------------------
2636    
2637        if ( irun < 0 ){
2638            irun = 0LL;
2639            irunt = 0LL;
2640            irunentry = 0;
2641            prevshift = 0;
2642            run_tree->GetEntry(irun);
2643            if( !GetOrbitalInfo() )cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated "<<endl;
2644            if ( gltsync ) delete gltsync; //Emiliano
2645            if(!dbc || (dbc && !dbc->IsConnected()) )SetDBConnection(); //Emiliano
2646            gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0,"ID",dbc,false); //Emiliano // the "false" means not to use level0 file (not necessary here)
2647            if ( dbc ) dbc->Close();// Emiliano
2648        };      
2649        // ---------------------------------------------------------------
2650        // retrieve OBT and absolute time of the event
2651        // ---------------------------------------------------------------
2652        ULong64_t abstime = 0LL;
2653        //    ULong64_t obt     = 0LL; // Emiliano
2654        Long64_t obt     = 0LL; // Emiliano, Long64_t GL_TIMESYNC::DBobt(UInt_t obt) since depending on the situation OBT is lowered or boosted
2655        if( GetOrbitalInfo() ){
2656            abstime = GetOrbitalInfo()->absTime;
2657            obt     = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano
2658        }else{
2659            abstime = GetRunInfo()->RUNHEADER_TIME;
2660            obt     = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano
2661        }
2662        
2663    
2664        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2665        // if it is a full file (not preselected)
2666        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2667        if(SELLI==0 || SELLI==2){ // Emiliano
2668    
2669            // ---------------------------------------------------------------
2670            // increment dead and live-time counters
2671            // (only when reading a file not preselected)
2672            // ---------------------------------------------------------------
2673            if(SELLI==0){
2674                if( GetTrigLevel2() ){
2675                    totdltime[0]+=GetTrigLevel2()->dltime[0];
2676                    totdltime[1]+=GetTrigLevel2()->dltime[1];
2677                }
2678                totdltime[2]++;
2679            }
2680    
2681            //
2682            bool fromfirst = true; // first loop over runs
2683    
2684    
2685            // ------------------------------------------------------
2686            // loop over runs to find the one that contains the event
2687            // ------------------------------------------------------
2688            while (
2689                (
2690    //              (
2691    //                  !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)
2692    //                    abstime <= GetRunInfo()->RUNTRAILER_TIME) &&
2693    //                  !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)
2694    //                    obt <= GetRunInfo()->RUNTRAILER_OBT)
2695    //                  )
2696                    
2697                    !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)
2698                      abstime <= GetRunInfo()->RUNTRAILER_TIME)
2699                    ||
2700                    !(obt >= gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) &&         // additional check on OBT (ms) // Emiliano
2701                      obt <= gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT))           // Emiliano
2702                    ||
2703                    GetRunInfo()->NEVENTS==0
2704    //              || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift) // ERRORE!!! fa saltare i run con 1 evento
2705                    ||
2706                    !(irunentry <= GetRunInfo()->NEVENTS-1-prevshift)
2707                    )
2708                && irun < run_tree->GetEntries() ){
2709    
2710    
2711    //          if( !(abstime >= GetRunInfo()->RUNHEADER_TIME &&abstime <= GetRunInfo()->RUNTRAILER_TIME)  )cout << "ABS TIME "<<abstime << " " <<GetRunInfo()->RUNTRAILER_TIME <<endl;
2712    //          if( !(obt >= GetRunInfo()->RUNHEADER_OBT && obt <= GetRunInfo()->RUNTRAILER_OBT) )cout << "OBT TIME "<< obt <<" "<< GetRunInfo()->RUNTRAILER_OBT << endl;
2713    //          if( GetRunInfo()->NEVENTS==0  )cout <<"GetRunInfo()->NEVENTS==0 "<<endl;
2714    //          if( !(irunentry <= GetRunInfo()->NEVENTS-1-prevshift) ) cout << "irunentry > "<<GetRunInfo()->NEVENTS-1-prevshift << endl;
2715                // - - - - - - - - - - - - -
2716                // irunentry = position of current entry, relative to the run
2717                // prevshift = shift needed to synchronize l0 and l2 data (nested events)
2718                // - - - - - - - - - - - - -
2719    
2720                // -----------------------------------------
2721                // store dead and live-time of previous run
2722                // -----------------------------------------
2723    //          if(SELLI==0){
2724              if(SELLI!=2){
2725                if(fromfirst){
2726                    if(oldrun==irun){
2727                        /// decrement counters
2728                        if( GetTrigLevel2()){
2729                            totdltime[0]-=GetTrigLevel2()->dltime[0];//live-time
2730                            totdltime[1]-=GetTrigLevel2()->dltime[1];//dead-time
2731                        }
2732                        totdltime[2]--;                              //event counter
2733                        cout << endl;
2734                        cout << "n.events     : "<<totdltime[2]<<endl;
2735                        cout << "RUN LIVE-TIME: "<<totdltime[0]*0.16<<" ms"<<endl;
2736                        cout << "RUN DEAD-TIME: "<<totdltime[1]*0.01<<" ms"<<endl;      
2737                    }else{
2738                        totdltime[0]=0;//live-time
2739                        totdltime[1]=0;//dead-time
2740                        totdltime[2]=0;                             //event counter
2741                        cout << " *** JUMP RUN *** irun "<<irun<<endl;
2742                    }
2743                    /// add an entry
2744                    if(run_tree_clone)
2745                        if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
2746                            run_tree_clone->GetBranch("DeadLiveTime")->Fill();
2747                    /// reset counters
2748                    if( GetTrigLevel2() ){
2749                        totdltime[0]=GetTrigLevel2()->dltime[0];//live-time
2750                        totdltime[1]=0;                         //dead-time
2751                    }
2752                    totdltime[2]=1;                             //event counter
2753                }
2754              }
2755              //        }
2756    
2757                irun++;        
2758                // ------------------------------------
2759                // if the end of run tree is reached...
2760                // ------------------------------------
2761                if( irun == run_tree->GetEntries() ){
2762                    if(!fromfirst){
2763                        // -----------------------------------------------------
2764                        // if it happened already once and the run was not found
2765                        // ---> exit with error
2766                        // -----------------------------------------------------
2767                        cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- event entry #"<<iev<<" does not belong to any run (should not happen)" <<endl;
2768                        return false;
2769                    }
2770                    // -----------------------------------------
2771                    // ...otherwise repeat search from beginning
2772                    // -----------------------------------------
2773                    cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- reached end of run tree. searchin again from beginning " <<endl;
2774                    fromfirst = false;
2775                    irun = 0LL;
2776                    runfirstentry = 0LL;
2777                }
2778                // -------------------------------------------------------------------
2779                // save the index of the first entry of the run, relative to pam_tree,
2780                // and read a new run
2781                // -------------------------------------------------------------------
2782                if(irun>0)runfirstentry += (GetRunInfo()->NEVENTS)-prevshift;
2783                irunentry = 0;
2784                prevshift = 0;          
2785                run_tree->GetEntry(irun);//update runinfo
2786                irunt = irun - irunoffset[run_tree->GetTreeNumber()];
2787                if ( gltsync ) delete gltsync; // Emiliano
2788                if(!dbc || (dbc && !dbc->IsConnected()) )SetDBConnection(); //Emiliano
2789                gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0,"ID",dbc,false); // Emiliano
2790                if ( dbc ) dbc->Close(); // Emiliano
2791                if(gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT)>gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ){ // Emiliano
2792                    cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun "<<irun<<"  has RUNHEADER_OBT>=RUNTRAILER_OBT " <<endl;
2793                    cout << "                                                            (NB!! in this case some events are assigned to a wrong run)"<<endl;
2794                }
2795    //          if(hasfrag &&  fragid != GetRunInfo()->ID){
2796    //              cout << "... where is the next fragment ??"<<endl;
2797    //          }
2798            };
2799    
2800    
2801            // --------------------------------------
2802            // if there was no need to update the run
2803            // ---> exit with FALSE
2804            // --------------------------------------
2805            if ( irun == oldrun ) return(false);
2806    
2807            // --------------------------------------
2808            // ... otherwise
2809            // --------------------------------------
2810    
2811    
2812            // --------------------------------------
2813            // ---> exit with TRUE
2814            // --------------------------------------
2815            if(DBG)cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2816            // ----------------------------------------------------
2817            // update the tracker parameters
2818            // (non ho trovato nessun altro modo sicuro di farlo...)
2819            // ----------------------------------------------------
2820            if(!dbc || (dbc && !dbc->IsConnected()) )SetDBConnection();
2821            TrkParams::Set(GetRunInfo() ,dbc);
2822            if(dbc)dbc->Close();
2823    
2824            // ----------------------------------------------------
2825            // then check if the run has a fragment
2826            // in this case we have to switch to the next fragment
2827            // when the end of the first fragment is reached
2828            // ----------------------------------------------------
2829            if(
2830                GetRunInfo()->ID_RUN_FRAG != 0 &&
2831    //          GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID &&
2832                DBG &&
2833                true ){
2834                cout << "* fragment *"<<endl;              
2835            }
2836    
2837            return(true);    
2838        };
2839        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2840        // if it is a preselected file (there is SelectionList)
2841        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2842        // irun  = run entry relative to the chain
2843        // irunt = run entry relative to the tree
2844        if(SELLI==1){      
2845            sel_tree->GetEntry(iev);// read irunt from SelectionList
2846            irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW
2847            if(irun != oldrun ){
2848                if( irun < run_tree->GetEntries() )run_tree->GetEntry(irun);
2849                // check if the time is ok (with merged files it is not...)
2850                // if not loop over run and look for the proper entry
2851                bool SECONDO_GIRO=false;
2852                //      Long64_t irun_start   = irun;
2853                int      offset_start = irunoffset[sel_tree->GetTreeNumber()];
2854                while (
2855                    (
2856                        (
2857                            !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)
2858                              abstime <= GetRunInfo()->RUNTRAILER_TIME)
2859    //                      ||
2860    //                      !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)
2861    //                        obt <= GetRunInfo()->RUNTRAILER_OBT)
2862                            )
2863                        || GetRunInfo()->NEVENTS==0
2864                        )
2865    //              && irun < run_tree->GetEntries()
2866                    ){
2867    
2868                    if(DBG){
2869                        cout << " (test) ";
2870                        cout << " tree "<<sel_tree->GetTreeNumber();
2871                        cout << " irunt "<<irunt;
2872                        cout << " offset "<<irunoffset[sel_tree->GetTreeNumber()];
2873                        cout << " abs "<<abstime;
2874                        cout <<" >> "<<GetRunInfo()->RUNHEADER_TIME<<" "<<GetRunInfo()->RUNTRAILER_TIME;
2875                        cout << " obt "<<obt;
2876                        cout <<" >> "<<GetRunInfo()->RUNHEADER_OBT<<" "<<GetRunInfo()->RUNTRAILER_OBT;
2877                        cout << " *** JUMP RUN *** irun "<<irun;
2878                        cout << endl;
2879                    }
2880    //              irun++;
2881                    irunoffset[sel_tree->GetTreeNumber()]++;
2882                    irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW          
2883                    if(irun == run_tree->GetEntries() && SECONDO_GIRO){
2884    //              if(irun == irun_start ){
2885                        cout << " ...grrrvzzkhhhajsdkj!!!! "<<endl;
2886                        irunoffset[sel_tree->GetTreeNumber()] = offset_start;
2887                        return false;
2888                    }
2889                    if( irun >= run_tree->GetEntries() || irun < 0){
2890                        cout << "irun = "<<irun<<" >>  search from the beginning... <<"<<endl;
2891                        SECONDO_GIRO=true;
2892                        irun=0;
2893                        irunoffset[sel_tree->GetTreeNumber()]=-irunt;
2894                    }
2895                    run_tree->GetEntry(irun);
2896                }
2897    
2898    
2899                if(DBG){
2900                    cout << " (test) ";
2901                    cout << " tree "<<sel_tree->GetTreeNumber();
2902                    cout << " irunt "<<irunt;
2903                    cout << " offset "<<irunoffset[sel_tree->GetTreeNumber()];
2904                    cout << " abs "<<abstime;
2905                    cout <<" >> "<<GetRunInfo()->RUNHEADER_TIME<<" "<<GetRunInfo()->RUNTRAILER_TIME;
2906                    cout << " obt "<<obt;
2907                    cout <<" >> "<<GetRunInfo()->RUNHEADER_OBT<<" "<<GetRunInfo()->RUNTRAILER_OBT;
2908                }
2909                if(DBG)cout << endl;
2910                if(DBG)cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" (run-entry "<<irun<<")"<<endl;
2911                // ----------------------------------------------------
2912                // update the tracker parameters
2913                // (non ho trovato nessun altro modo sicuro di farlo...)
2914                // ----------------------------------------------------
2915                if(!dbc || (dbc && !dbc->IsConnected()) )SetDBConnection();
2916                TrkParams::Set(GetRunInfo() ,dbc);
2917                if(dbc)dbc->Close();
2918    //          cout << endl;
2919                prevshift = 0;
2920                return true;
2921            }
2922            return false;
2923        }
2924    
2925        return false;
2926        //
2927  };  };
2928  /**  /**
2929   * 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)
2930   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
2931   * @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
2932   */   */
2933  Bool_t PamLevel2::UpdateRunInfo(TTree *run, ULong64_t iev){  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev){
2934    return(UpdateRunInfo((TChain*)run,iev));      return(UpdateRunInfo((TChain*)run,iev));
2935  };  };
2936    
2937  //--------------------------------------  //--------------------------------------
# Line 1588  Bool_t PamLevel2::UpdateRunInfo(TTree *r Line 2941  Bool_t PamLevel2::UpdateRunInfo(TTree *r
2941  /**  /**
2942   * Set which trees shoul be analysed   * Set which trees shoul be analysed
2943   * @param detlist TString containing the sequence of trees required   * @param detlist TString containing the sequence of trees required
2944  */   */
2945  void PamLevel2::SetWhichTrees(TString detlist){  void PamLevel2::SetWhichTrees(TString detlist){
2946                
2947      if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){  //    if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){
2948          CAL0 = false;      if( detlist.Contains("+ALL", TString::kIgnoreCase)){
2949    
2950            cout << " ======================================================== "<<endl;
2951            cout << "                       (( WARNING ))                      "<<endl;
2952            cout << " The meaning of the option +ALL has changed!!             "<<endl;
2953            cout << " Now it includes really all (level0+level1+level2+gpamela)"<<endl;
2954            cout << " and the file is discarded if it does not contain         "<<endl;
2955            cout << " all trees or  if level0 files are not available!!        "<<endl;
2956            cout << " ======================================================== "<<endl;
2957                
2958            CAL0 = true;
2959          CAL1 = true;          CAL1 = true;
2960          CAL2 = true;          CAL2 = true;
2961          TRK2 = true;          TRK2 = true;
2962          TRK1 = false;          TRK1 = true;
2963          TRKh = false;          TRKh = true;
2964          TRK0 = false;          TRK0 = true;
2965          TRG = true;          TRG = true;
2966          TOF = true;          TOF = true;
2967          TOF0 = false;          TOF0 = true;
2968          S4  = true;          S4  = true;
2969          ND  = true;          ND  = true;
2970          AC  = true;          AC  = true;
2971          ORB = true;          ORB = true;
2972            GP  = true;
2973      }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){      }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){
2974          CAL0 = false;          CAL0 = false;
2975          CAL1 = false;          CAL1 = false;
# Line 1621  void PamLevel2::SetWhichTrees(TString de Line 2985  void PamLevel2::SetWhichTrees(TString de
2985          ND  = false;          ND  = false;
2986          AC  = false;          AC  = false;
2987          ORB = false;          ORB = false;
2988            GP  = false;
2989      };      };
2990            
2991  //  -------------------------------------------------------------------------  //  -------------------------------------------------------------------------
# Line 1694  void PamLevel2::SetWhichTrees(TString de Line 3059  void PamLevel2::SetWhichTrees(TString de
3059      if( detlist.Contains("-ORB", TString::kIgnoreCase) )ORB = false;      if( detlist.Contains("-ORB", TString::kIgnoreCase) )ORB = false;
3060      else if( detlist.Contains("+ORB", TString::kIgnoreCase) )ORB = true;      else if( detlist.Contains("+ORB", TString::kIgnoreCase) )ORB = true;
3061    
3062        if( detlist.Contains("-GP", TString::kIgnoreCase) )GP = false;
3063        else if( detlist.Contains("+GP", TString::kIgnoreCase) )GP = true;
3064    
3065        cout<< "tree/branch list from input --> ";
3066        if(TRK0)cout<<"TRK0 ";
3067        if(TRK1)cout<<"TRK1 ";
3068        if(TRK2)cout<<"TRK2 ";
3069        if(TRKh)cout<<"TRKH ";
3070        if(CAL0)cout<<"CAL0 ";
3071        if(CAL1)cout<<"CAL1 ";
3072        if(CAL2)cout<<"CAL2 ";
3073        if(TOF)cout<<"TOF ";
3074        if(TRG)cout<<"TRG ";
3075        if(AC)cout<<"AC ";
3076        if(ND)cout<<"ND ";
3077        if(S4)cout<<"S4 ";
3078        if(ORB)cout<<"ORB ";
3079        if(GP)cout<<"GP ";
3080        cout << endl;
3081  //     cout<< "Set detector list --> ";  //     cout<< "Set detector list --> ";
3082  //     if(TRK1)cout<<"TRK1 ";  //     if(TRK1)cout<<"TRK1 ";
3083  //     if(TRK2)cout<<"TRK2 ";  //     if(TRK2)cout<<"TRK2 ";
3084  //     if(TRKh)cout<<"TRKH ";  //     if(TRKh)cout<<"TRKH ";
3085  //     if(CAL1)cout<<"CAL1 ";  //     if(CAL1)cout<<"CAL1 ";
3086  //     if(CAL2)cout<<"CAL2 ";  //     if(CAL2)cout<<"CAL2 ";
3087    //     if(TOF0)cout<<"TOF0 ";
3088  //     if(TOF)cout<<"TOF ";  //     if(TOF)cout<<"TOF ";
3089  //     if(TRG)cout<<"TRG ";  //     if(TRG)cout<<"TRG ";
3090  //     if(AC)cout<<"AC ";  //     if(AC)cout<<"AC ";
# Line 1718  void  PamLevel2::GetWhichTrees(TFile* f) Line 3103  void  PamLevel2::GetWhichTrees(TFile* f)
3103            
3104    
3105    
3106      cout << "void  PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;  //    cout << "void  PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;
3107      // -----------      // -----------
3108      // reset flags      // reset flags
3109      // -----------      // -----------
# Line 1733  void  PamLevel2::GetWhichTrees(TFile* f) Line 3118  void  PamLevel2::GetWhichTrees(TFile* f)
3118      ND     = false;          ND     = false;    
3119      AC     = false;          AC     = false;    
3120      ORB    = false;          ORB    = false;    
3121        GP     = false;    
3122            
3123      RUN    = false;      RUN    = false;
3124                    
3125      cout << "Checking file: "<<f->GetName()<<endl;  //    cout << "Checking file: "<<f->GetName()<<endl;
3126      if( !f || f->IsZombie() ){      if( !f || f->IsZombie() ){
3127          cout << "File: "<< f->GetName() <<" Non valid root file"<< endl;          cout << "File: "<< f->GetName() <<" Non valid root file"<< endl;
3128          return;          return;
3129      }      }
3130    
3131      TList *lk = f->GetListOfKeys();      TList *lk = f->GetListOfKeys();
3132        if(!lk)return;
3133      TIter next(lk);      TIter next(lk);
3134      TKey *key =0;      TKey *key =0;
3135    
# Line 1823  void  PamLevel2::GetWhichTrees(TFile* f) Line 3210  void  PamLevel2::GetWhichTrees(TFile* f)
3210                  TRK2 = false;                  TRK2 = false;
3211                  TRKh = false;                  TRKh = false;
3212              }else nev=nevt;              }else nev=nevt;
3213              T->Delete();  //          T->Delete();
3214          };          };
3215          //=========================================================            //=========================================================  
3216          if( !strcmp(key->GetName(),"Calorimeter"    ) ){          if( !strcmp(key->GetName(),"Calorimeter"    ) ){
# Line 1839  void  PamLevel2::GetWhichTrees(TFile* f) Line 3226  void  PamLevel2::GetWhichTrees(TFile* f)
3226                  CAL1 = false;                  CAL1 = false;
3227                  CAL2 = false;                  CAL2 = false;
3228              }else nev=nevt;              }else nev=nevt;
3229              T->Delete();  //          T->Delete();
3230          };                };      
3231            //=========================================================  
3232            if( !strcmp(key->GetName(),"h20") ){
3233                GP = true;
3234                Int_t nevt = ((TTree*)f->Get("h20"))->GetEntries();
3235                if( nev && nevt!=nev){
3236                    cout << "File: "<< f->GetName() <<" h20 tree has "<<nevt<<" events instead of "<<nev<< endl;
3237                    GP = false;
3238                }else nev=nevt;
3239            }
3240    
3241      };      };
3242            
3243      delete lk;  //    delete lk;
3244    
3245  //     cout<< "Get detector list from input file --> ";      cout<< "tree/branch list from file  --> ";
3246  //     if(TRK1)cout<<"TRK1 ";      if(TRK1)cout<<"TRK1 ";
3247  //     if(TRK2)cout<<"TRK2 ";      if(TRK2)cout<<"TRK2 ";
3248  //     if(TRKh)cout<<"TRKH ";      if(TRKh)cout<<"TRKH ";
3249  //     if(CAL1)cout<<"CAL1 ";      if(CAL1)cout<<"CAL1 ";
3250  //     if(CAL2)cout<<"CAL2 ";      if(CAL2)cout<<"CAL2 ";
3251  //     if(TOF)cout<<"TOF ";      if(TOF)cout<<"TOF ";
3252  //     if(TRG)cout<<"TRG ";      if(TRG)cout<<"TRG ";
3253  //     if(AC)cout<<"AC ";      if(AC)cout<<"AC ";
3254  //     if(ND)cout<<"ND ";      if(ND)cout<<"ND ";
3255  //     if(S4)cout<<"S4 ";      if(S4)cout<<"S4 ";
3256  //     if(ORB)cout<<"ORB ";      if(ORB)cout<<"ORB ";
3257  //     cout << endl;      if(GP)cout<<"GP ";
3258        cout << endl;
3259                
3260      return ;      return ;
3261                    
# Line 1887  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3284  Bool_t  PamLevel2::CheckLevel2File(TStri
3284      Bool_t ND__ok     = false;          Bool_t ND__ok     = false;    
3285      Bool_t AC__ok     = false;          Bool_t AC__ok     = false;    
3286      Bool_t ORB__ok    = false;          Bool_t ORB__ok    = false;    
3287        Bool_t GP__ok     = false;    
3288            
3289      Bool_t RUN__ok    = false;      Bool_t RUN__ok    = false;
3290            
3291      Bool_t SELLI__ok  = false;      Bool_t SELLI__ok  = false;
3292    
3293      cout << "Checking file: "<<name<<endl;  //    cout << "Checking file: "<<name<<endl;
3294      TFile *f = new TFile(name.Data());      TFile *f = new TFile(name.Data());
3295      if( !f || f->IsZombie() ){      if( !f || f->IsZombie() ){
3296          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 1918  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3316  Bool_t  PamLevel2::CheckLevel2File(TStri
3316  //          return false;  //          return false;
3317  //      };  //      };
3318    
3319            //=========================================================    
3320            // check if the file
3321            
3322    
3323          if( !strcmp(key->GetName(),"Run"        ) )RUN__ok = true;                if( !strcmp(key->GetName(),"Run"        ) )RUN__ok = true;      
3324    
# Line 2044  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3445  Bool_t  PamLevel2::CheckLevel2File(TStri
3445              };                  };    
3446              T->Delete();              T->Delete();
3447          };                };      
3448            //=========================================================    
3449            if( !strcmp(key->GetName(),"h20") ){
3450                ISGP = true;    
3451                GP__ok = true;
3452                if(GP){
3453                    Int_t nevt = ((TTree*)f->Get("h20"))->GetEntries();
3454                    if( nev && nevt!=nev){
3455                        cout << "File: "<< f->GetName() <<" discarded ---- h20 tree has "<<nevt<<" events instead of "<<nev<< endl;
3456                        return false;
3457                    }
3458                    nev=nevt;
3459                }
3460            }
3461    
3462      };      };
3463    
# Line 2084  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3498  Bool_t  PamLevel2::CheckLevel2File(TStri
3498    
3499    
3500      if(!RUN__ok) {      if(!RUN__ok) {
3501          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;
3502  //      return false;          RUN = false;
3503      };      };
3504    
3505      if(CAL1 && !CAL1__ok){      if(CAL1 && !CAL1__ok){
# Line 2133  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3547  Bool_t  PamLevel2::CheckLevel2File(TStri
3547          cout << "File: "<< f->GetName() <<" discarded ---- Missing Trigger tree"<< endl;          cout << "File: "<< f->GetName() <<" discarded ---- Missing Trigger tree"<< endl;
3548          return false;          return false;
3549      };      };
3550        if(GP && !GP__ok){
3551            cout << "File: "<< f->GetName() <<" discarded ---- Missing h20 tree"<< endl;
3552            return false;
3553        };
3554            
3555    
3556  //    lk->Delete();  //    lk->Delete();
# Line 2151  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3569  Bool_t  PamLevel2::CheckLevel2File(TStri
3569  //     if(ND)cout<<"ND ";  //     if(ND)cout<<"ND ";
3570  //     if(S4)cout<<"S4 ";  //     if(S4)cout<<"S4 ";
3571  //     if(ORB)cout<<"ORB ";  //     if(ORB)cout<<"ORB ";
3572    //     if(GP)cout<<"GP ";
3573  //     cout << endl;  //     cout << endl;
3574                
3575      return true;      return true;
# Line 2201  void PamLevel2::CreateCloneTrees0( TChai Line 3620  void PamLevel2::CreateCloneTrees0( TChai
3620   */   */
3621  void PamLevel2::CreateCloneTrees(TFile *ofile){  void PamLevel2::CreateCloneTrees(TFile *ofile){
3622    
3623    
3624    //  if the pointer is null, create a default file
3625        if(!run_tree)return;
3626    
3627        if(!ofile){
3628            cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root "<<endl;
3629            ofile = new TFile("clone-tree.root","recreate");
3630        }
3631    
3632      ofile->cd();      ofile->cd();
3633    
3634      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
# Line 2212  void PamLevel2::CreateCloneTrees(TFile * Line 3640  void PamLevel2::CreateCloneTrees(TFile *
3640      cout << "Run          : branch RunInfo"<<endl;      cout << "Run          : branch RunInfo"<<endl;
3641      run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));      run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));
3642      cout << "Run          : branch SoftInfo"<<endl;      cout << "Run          : branch SoftInfo"<<endl;
3643                    // ------------------
3644        // replicate run tree
3645        // ------------------
3646    //    cout << "----------------------------------------------------"<<endl;
3647    //    cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
3648        for (Int_t i=0; i<run_tree->GetEntries(); i++){
3649            run_tree->GetEntry(i);
3650    //      cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
3651            run_tree_clone->Fill();
3652        }
3653        cout << "----------------------------------------------------"<<endl;
3654    
3655        // ------------------------------------
3656        // add branch with dead and live times
3657        // ------------------------------------
3658        if ( SELLI != 2 ){ // EMILIANO
3659          run_tree_clone->Branch("DeadLiveTime",totdltime,"dltime[3]/l");
3660          cout << "Run          : branch DeadLiveTime"<<endl;
3661    
3662      sel_tree_clone = new TTree("SelectionList","List of selected events ");  
3663      sel_tree_clone->Branch("RunEntry",&irun,"runentry/I");        sel_tree_clone = new TTree("SelectionList","List of selected events ");
3664      sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/I");        //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
3665              sel_tree_clone->Branch("RunEntry",&irunt,"runentry/L");//NEWNEW
3666          sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/L");
3667        };
3668    
3669      Int_t i=0;      Int_t i=0;
3670      if(TRK1||TRK2||TRKh){      if(TRK1||TRK2||TRKh){
# Line 2227  void PamLevel2::CreateCloneTrees(TFile * Line 3673  void PamLevel2::CreateCloneTrees(TFile *
3673              pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));              pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));
3674              pam_tree_clone[i]->BranchRef();              pam_tree_clone[i]->BranchRef();
3675              cout << "Tracker      : branch TrkLevel1"<<endl;              cout << "Tracker      : branch TrkLevel1"<<endl;
3676              cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;  //          cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;
3677          };          };
3678          if(TRK2) {          if(TRK2) {
3679              pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));              pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));
# Line 2291  void PamLevel2::CreateCloneTrees(TFile * Line 3737  void PamLevel2::CreateCloneTrees(TFile *
3737      };      };
3738      // OrbitalInfo      // OrbitalInfo
3739      if(ORB) {      if(ORB) {
3740          pam_tree_clone[i] = new TTree("OrbitalInfo","PAMELA oribital info  ");            pam_tree_clone[i] = new TTree("OrbitalInfo","PAMELA orbital info  ");  
3741          pam_tree_clone[i]->Branch("OrbitalInfo","OrbitalInfo", GetPointerTo("OrbitalInfo"));          pam_tree_clone[i]->Branch("OrbitalInfo","OrbitalInfo", GetPointerTo("OrbitalInfo"));
3742          cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;          cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;
3743          i++;          i++;
3744      };      };
3745        // GPamela
3746        if(GP) {
3747            pam_tree_clone[i] = new TTree("h20","GPAMELA info ");  
3748            pam_tree_clone[i]->Branch("GPamela","GPamela", GetPointerTo("GPamela"),32000,1);//split
3749            cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;
3750            i++;
3751        };
3752      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
3753    
3754  }  }
# Line 2340  TTree* PamLevel2::GetCloneTree(TString n Line 3793  TTree* PamLevel2::GetCloneTree(TString n
3793  void PamLevel2::WriteCloneTrees(){  void PamLevel2::WriteCloneTrees(){
3794      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
3795      cout << "Write clones of PAMELA trees "<<endl;      cout << "Write clones of PAMELA trees "<<endl;
3796      cout << run_tree_clone->GetName()<<endl;          cout << run_tree_clone->GetName()<<endl;  
3797        if ( SELLI != 2){// Emiliano
3798          if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
3799            run_tree_clone->GetBranch("DeadLiveTime")->Fill();
3800        };
3801      run_tree_clone->Write();      run_tree_clone->Write();
3802      cout << sel_tree_clone->GetName()<<endl;          if ( SELLI != 2){ //Emiliano
3803      sel_tree_clone->Write();        cout << sel_tree_clone->GetName()<<endl;    
3804          sel_tree_clone->Write();
3805        };
3806      for(Int_t i=0; i<NCLONES; i++){          for(Int_t i=0; i<NCLONES; i++){    
3807          if(pam_tree_clone[i]){          if(pam_tree_clone[i]){
3808              cout << pam_tree_clone[i]->GetName()<<endl;              cout << pam_tree_clone[i]->GetName()<<endl;
# Line 2357  void PamLevel2::WriteCloneTrees(){ Line 3816  void PamLevel2::WriteCloneTrees(){
3816  /**  /**
3817   * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.   * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.
3818   */   */
3819  Int_t PamLevel2::GetEntry(Int_t iee){  //Int_t PamLevel2::GetEntry(Int_t iee){
3820    Int_t PamLevel2::GetEntry(Long64_t iee){
3821            
3822    //     cout << "-------------------------------------"<<endl;
3823    //     cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;
3824    
3825      if(!pam_tree){      if(!pam_tree){
3826          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;
3827          return 0;          return 0;
3828      }      }
3829    
3830      Int_t ii=0;  
3831        //
3832        // 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...
3833        //
3834        //    if(!run_tree ){
3835        //  cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loeaded"<<endl;
3836        //  return 0;
3837        //    }
3838    
3839        Long64_t ii=0;
3840      //-------------------------------      //-------------------------------
3841      ii = iee;      ii = iee;
3842      if( !pam_tree->GetEntry(ii) ) return 0;      if( !pam_tree->GetEntry(ii) ){      
3843      //-------------------------------        cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree"<<endl;
3844          return 0;
3845      if(!run_tree ){      }
3846          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded"<<endl;      //
3847          return 0;      // ... 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.
3848        // 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
3849        // a problem if you don't check the return code of getentry.
3850        //
3851        if(!RUN || !run_tree ){
3852            if ( TRK0 || CAL0 || TOF0 || RUN ) { //forse cosi` va bene per tornare 1?
3853                cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded"<<endl;
3854                return 0;
3855            }  else {
3856                return 1; //cosi` se non c'e` run esce qua...
3857            }
3858      }      }
3859    
3860        //-------------------------------
3861      ii = iee;      ii = iee;
3862      Bool_t UPDATED = UpdateRunInfo(run_tree,ii);  //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);
3863      if(SELLI==0)irunentry = iee-runfirstentry;  //    Bool_t UPDATED = UpdateRunInfo(ii);
3864      if(UPDATED && run_tree_clone)run_tree_clone->Fill();      UpdateRunInfo(ii);
3865        if(SELLI==0||SELLI==2)irunentry = iee-runfirstentry;
3866    //    if(UPDATED && run_tree_clone)run_tree_clone->Fill();
3867      
3868  //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;  //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;
3869    
3870      if( TRK0 || CAL0 || TOF0 )GetYodaEntry( );  //     cout << "irunentry     "<<irunentry << endl;
3871    //     cout << "runfirstentry "<<runfirstentry << endl;
3872    //     cout << "nevents       "<<GetRunInfo()->NEVENTS<< endl;
3873    
3874    //     if( TRK0 || CAL0 || TOF0 ){
3875    //      if( !GetYodaEntry( ) ){
3876    //          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading level0 tree"<<endl;
3877    //          return 0;
3878    //      }
3879    //     }
3880    
3881    
3882      return 1;      return 1;
3883    
3884  }  }
3885    
3886    TrkLevel0    *PamLevel2::GetTrkLevel0(){
3887        if( !TRK0 )return NULL;
3888        if( !GetYodaEntry( ) ){
3889            cout << " Int_t PamLevel2::GetTrkLevel0() -- ERROR -- error reading level0 tree"<<endl;
3890            return 0;
3891        }
3892        return trk0_obj;
3893    };
3894    CaloLevel0    *PamLevel2::GetCaloLevel0(){
3895        if( !CAL0 )return NULL;
3896        if( !GetYodaEntry( ) ){
3897            cout << " Int_t PamLevel2::GetCaloLevel0() -- ERROR -- error reading level0 tree"<<endl;
3898            return 0;
3899        }
3900        return calo0_obj;
3901    };
3902    
3903    
3904  /**  /**
3905   * 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.
3906   * 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 2402  TTree* PamLevel2::GetYodaTree( ){ Line 3915  TTree* PamLevel2::GetYodaTree( ){
3915      // check if iroot has changed      // check if iroot has changed
3916      //===================================      //===================================
3917      if( irun<0 ){      if( irun<0 ){
3918          cout << "PamLevel2::GetYodaTree() -- ERROR "<<endl;          cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = "<<irun<<endl;
3919          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;  
3920          return NULL;          return NULL;
3921      }          }    
3922      Int_t irootnew = run_obj->ID_ROOT_L0;      Int_t irootnew = run_obj->ID_ROOT_L0;
3923  //     cout << "iroot    "<<iroot<<endl;  //      cout << "iroot    "<<iroot<<endl;
3924  //     cout << "irootnew "<<irootnew<<endl;  //      cout << "irootnew "<<irootnew<<endl;
3925    
3926      //===================================      //===================================
3927      // load the level0 file      // load the level0 file
# Line 2422  TTree* PamLevel2::GetYodaTree( ){ Line 3933  TTree* PamLevel2::GetYodaTree( ){
3933          // open the DB connection          // open the DB connection
3934          // (if not already opened)          // (if not already opened)
3935          //===================================          //===================================
3936          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;  
         }  
3937          GL_ROOT glroot = GL_ROOT();          GL_ROOT glroot = GL_ROOT();
3938          if( glroot.Query_GL_ROOT(iroot,dbc) ){          if( glroot.Query_GL_ROOT(iroot,dbc) ){
3939              cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = "<<iroot<< " does not exists"<<endl;              cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = "<<iroot<< " does not exists"<<endl;
# Line 2457  TTree* PamLevel2::GetYodaTree( ){ Line 3955  TTree* PamLevel2::GetYodaTree( ){
3955          l0_tree = (TTree*)l0_file->Get("Physics");          l0_tree = (TTree*)l0_file->Get("Physics");
3956          if(!h0_obj)h0_obj = new EventHeader();          if(!h0_obj)h0_obj = new EventHeader();
3957          l0_tree->SetBranchAddress("Header" ,&h0_obj);          l0_tree->SetBranchAddress("Header" ,&h0_obj);
3958            prevshift = 0;
3959          //---------------------------------------------------          //---------------------------------------------------
3960          // TRACKER:          // TRACKER:
3961          if(TRK0){          if(TRK0){
# Line 2465  TTree* PamLevel2::GetYodaTree( ){ Line 3964  TTree* PamLevel2::GetYodaTree( ){
3964                  trk0_obj->Set();                  trk0_obj->Set();
3965              };              };
3966              l0_tree->SetBranchAddress("Tracker" ,trk0_obj->GetPointerToTrackerEvent());              l0_tree->SetBranchAddress("Tracker" ,trk0_obj->GetPointerToTrackerEvent());
             TrkParams::SetCalib(run_obj,dbc);  
3967          }          }
3968          //---------------------------------------------------          //--------------------------------------------------
3969          // CALORIMETER:          // CALORIMETER:
3970          if(CAL0){          if(CAL0){
3971              cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;              if(!calo0_obj){
3972                    calo0_obj = new CaloLevel0();
3973                    calo0_obj->Set();
3974                };
3975                l0_tree->SetBranchAddress("Calorimeter" ,calo0_obj->GetPointerToCalorimeterEvent());
3976                //      cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;
3977          }          }
3978          //---------------------------------------------------          //---------------------------------------------------
3979          // TOF:          // TOF:
# Line 2478  TTree* PamLevel2::GetYodaTree( ){ Line 3981  TTree* PamLevel2::GetYodaTree( ){
3981              cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented "<<endl;              cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented "<<endl;
3982          }          }
3983    
3984            dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
3985    
3986    
3987      };      };
3988    
3989    //     if(!dbc || (dbc && !dbc->IsConnected())){
3990    //      cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;
3991    //     }
3992    
3993        if ( TRK0 ){
3994    //      TrkParams::Load(6);
3995    //      if( !TrkParams::IsLoaded(6) ){
3996    //          cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- VK-mask not loaded"<<endl;
3997    //      };
3998    //      if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection();
3999            TrkParams::SetCalib(run_obj,dbc);
4000            TrkParams::LoadCalib( );
4001            if( !TrkParams::CalibIsLoaded() ){
4002                cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded"<<endl;
4003            };
4004    //      if(dbc)dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
4005        }
4006    
4007    
4008  //    cout << l0_tree << endl;  //    cout << l0_tree << endl;
       
4009      return l0_tree;      return l0_tree;
4010    
4011  }  }
# Line 2494  Int_t PamLevel2::GetYodaEntry(){ Line 4018  Int_t PamLevel2::GetYodaEntry(){
4018  //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;  //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;
4019      if(!GetYodaTree())return 0;      if(!GetYodaTree())return 0;
4020            
4021        // patch
4022        if( irunentry < 0){
4023    //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
4024            irunentry=0LL;
4025        }
4026      //  ---------------------------------      //  ---------------------------------
4027      //  if file is NOT a preselected file      //  if file is NOT a preselected file
4028      //  ---------------------------------      //  ---------------------------------
4029      Int_t quellagiusta = irunentry + run_obj->EV_FROM;      Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM);
4030    //     cout << " irunentry "<<irunentry<<endl;
4031    //     cout << " EV_FROM "<<run_obj->EV_FROM<<endl;
4032    //     cout << " quellagiusta = irunentry + EV_FROM "<< quellagiusta << endl;
4033    
4034    //     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
4035    //     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
4036    //     cout << " time "<< abstime << endl;
4037    //     cout << " trk_calib_used "<<run_obj->TRK_CALIB_USED<< endl;
4038        
4039        ULong64_t obt     = 0;
4040        ULong64_t pktn    = 0;
4041        if( GetOrbitalInfo() ){
4042            obt     = GetOrbitalInfo()->OBT;
4043            pktn    = GetOrbitalInfo()->pkt_num;
4044        }
4045    
4046      if( !GetOrbitalInfo() ){      if( !GetOrbitalInfo() && !ISGP){
4047          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo "<<endl;          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo "<<endl;
4048          return 0;          return 0;
4049      }      }
4050        if( obt==0 && pktn==0 && !ISGP ){
4051            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? "<<endl;
4052            return 0;
4053        }
4054    
4055      // ---------------------------------------------------------------------      // ---------------------------------------------------------------------
4056      // ATTENTION!!!      // ATTENTION!!!
# Line 2517  Int_t PamLevel2::GetYodaEntry(){ Line 4065  Int_t PamLevel2::GetYodaEntry(){
4065      // ---------------------------------------------------------------------      // ---------------------------------------------------------------------
4066      Int_t answer = 0;      Int_t answer = 0;
4067      Int_t shift =0;      Int_t shift =0;
4068        //    printf(" siamo qui %i %i \n",shift,prevshift);
4069        //    Int_t maxshift = 50; // EMILIANO
4070      do{      do{
4071  //      if(shift>0){          if(shift>0){    
4072  //          cout << " level0 <--> level2 mismatch ( irun "<<irun<<" irunentry "<<irunentry<<" shift "<<shift<<" )"<<endl;              cout << " PKTNUM  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
4073  //      }              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;
4074          answer = GetYodaTree()->GetEntry(quellagiusta+shift);              if(DBG)cout << "         L2 <--> L0 mismatch ( irun "<<irun<<" irunentry "<<irunentry<<" shift "<<shift<<" prevshift "<<prevshift<<" )"<<endl;
4075            }
4076            answer = l0_tree->GetEntry(quellagiusta+(Long64_t)shift+(Long64_t)prevshift);
4077          shift++;          shift++;
4078          if( !GetEventHeader() ){          if( !GetEventHeader() ){
4079              cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader "<<endl;              cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader "<<endl;
4080              return 0;              return 0;
4081          }          }
 //      if( (quellagiusta+shift) == GetYodaTree()->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;  
     }while( ( (UInt_t)(GetOrbitalInfo()->OBT) != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || GetOrbitalInfo()->pkt_num != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta+shift) < (UInt_t)(GetYodaTree()->GetEntries()) );  
4082    
4083            if(ISGP){
4084                obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
4085                pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
4086            }
4087    
4088    //      cout << "PKTNUM "<<shift<<" ==  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
4089    //      cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;
4090    //      if( (quellagiusta+shift) == l0_tree->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;
4091    //      cout << " obt "<< obt << endl;
4092    //      cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;
4093    //      cout << " pktn "<< pktn << endl;
4094    //      cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
4095    //      printf(" IDRUN %u \n",GetRunInfo()->ID);
4096    //
4097            if ( prevshift != 0 && (quellagiusta+(Long64_t)shift) == GetYodaTree()->GetEntries() ){
4098                prevshift = 0;
4099                shift = -1;
4100            };
4101    
4102        }while( ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) ||
4103                  pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())       )
4104                && (quellagiusta+(Long64_t)shift) < GetYodaTree()->GetEntries() && shift < maxshift);
4105    
4106        if ( (quellagiusta+(Long64_t)shift+(Long64_t)prevshift) > GetYodaTree()->GetEntries() || shift == maxshift ) {
4107            cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to "<<maxshift<<" )" <<endl;
4108            return 0;
4109        }
4110  //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;  //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
4111  //    return GetYodaTree()->GetEntry(quellagiusta);  //    return GetYodaTree()->GetEntry(quellagiusta);
4112        if ( shift > 1 ) prevshift += (shift-1);
4113            
4114      return answer;      return answer;
4115            
4116  }  }
4117    /**
4118     * \Brief Set DB connection
4119     */
4120    Bool_t PamLevel2::SetDBConnection(){
4121    
4122    //    cout << "PamLevel2::SetDBConnection()" << endl;
4123        if(DBG){
4124            cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
4125            cout<<"Connecting to DB"<<endl;
4126            cout<<"HOST "<<host<<endl;
4127            cout<<"USER "<<user<<endl;
4128            cout<<"PSW  "<<psw<<endl;
4129        }
4130        dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
4131        if( !dbc )return false;
4132        if( !dbc->IsConnected() )return false;    
4133        stringstream myquery;  // EMILIANO
4134        myquery.str("");  // EMILIANO
4135        myquery << "SET time_zone='+0:00'";  // EMILIANO
4136        dbc->Query(myquery.str().c_str());  // EMILIANO
4137        if(DBG)cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
4138        return true;
4139    
4140    }
4141    
4142    /**
4143     * \Brief Add a friend to the pamela chain.
4144     * @param cname name of the chain to be added
4145     */
4146    
4147    TChain* PamLevel2::AddFriend(TString cname){
4148    
4149        if(!GetPamTree()){
4150            cout << " TChain* PamLevel2::AddFriend(TString cname) --- a pamela tree must be created first"<<endl;
4151            return NULL;
4152        }
4153    
4154    
4155        TChain *c = new TChain( cname.Data() );
4156    
4157        TIter next( GetPamTree()->GetListOfFiles() );
4158        Int_t nf = 0;
4159        TChainElement* element = 0;    
4160        while ((element = (TChainElement*) next())) {      
4161            c->Add(element->GetTitle());
4162            nf++;
4163        }
4164    
4165        GetPamTree()->AddFriend(cname.Data());
4166    
4167        cout << "external chain created and added to pamela friends :"<<cname<<endl;
4168        cout << "n.files "<<nf<<endl;
4169        
4170    
4171        return c;    
4172    
4173    }
4174    
4175    

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

  ViewVC Help
Powered by ViewVC 1.1.23