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

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.69

  ViewVC Help
Powered by ViewVC 1.1.23