/[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.10 by pam-fi, Wed Dec 6 11:07:35 2006 UTC revision 1.61 by pam-fi, Thu Apr 3 14:39:51 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 7  Line 281 
281   * Default constructor   * Default constructor
282   */   */
283  PamTrack::PamTrack(){  PamTrack::PamTrack(){
284      trk_track  = this->TrkTrack::GetTrkTrack();      trk_track  = 0;
285      calo_track = this->CaloTrkVar::GetCaloTrkVar();      calo_track = 0;
286      tof_track  = this->ToFTrkVar::GetToFTrkVar();      tof_track  = 0;
287        candeleteobj = 0;
288        pscore = 0;
289        iscore = 0;
290  };  };
291  //--------------------------------------  //--------------------------------------
292  //  //
# Line 19  PamTrack::PamTrack(){ Line 296  PamTrack::PamTrack(){
296   * Constructor   * Constructor
297   */   */
298  PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o){  PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o){
299      trk_track  = this->TrkTrack::GetTrkTrack();  
300      calo_track = this->CaloTrkVar::GetCaloTrkVar();      trk_track  = 0;
301      tof_track  = this->ToFTrkVar::GetToFTrkVar();      calo_track = 0;
302      if(t) *trk_track  = *t;      tof_track  = 0;
303      if(c) *calo_track = *c;  //     if(t)trk_track  = new TrkTrack(*t);
304      if(o) *tof_track  = *o;      //     if(c)calo_track = new CaloTrkVar(*c);
305    //     if(o)tof_track  = new ToFTrkVar(*o);
306        if(t)trk_track  = t;
307        if(c)calo_track = c;
308        if(o)tof_track  = o;
309        candeleteobj = 0;
310    
311  };  };
312    PamTrack::PamTrack(const PamTrack& track){
313    
314        TrkTrack   *t = track.trk_track;
315        CaloTrkVar *c = track.calo_track;
316        ToFTrkVar  *o = track.tof_track;
317    
318        trk_track  = 0;
319        calo_track = 0;
320        tof_track  = 0;
321        if(t)trk_track  = new TrkTrack(*t);
322        if(c)calo_track = new CaloTrkVar(*c);
323        if(o)tof_track  = new ToFTrkVar(*o);
324        candeleteobj = 1;
325        
326    }
327    void PamTrack::Clear(){
328    
329    //    cout << "PamTrack::Clear() "<<candeleteobj<<endl;
330        if(candeleteobj){
331            if(trk_track)  trk_track->TrkTrack::Clear();
332            if(calo_track) calo_track->CaloTrkVar::Clear();//???
333            if(tof_track)  tof_track->ToFTrkVar::Clear();//???
334        }else{
335            trk_track  = 0;
336            calo_track = 0;
337            tof_track  = 0;
338        }
339        pscore = 0;
340        iscore = 0;
341    
342    }
343    void PamTrack::Delete(){
344    //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;
345        if(candeleteobj){
346            if(trk_track)  {
347                trk_track->TrkTrack::Clear();
348                delete trk_track;
349            }
350            if(calo_track){
351                calo_track->CaloTrkVar::Clear();//???
352                delete calo_track;
353            }
354            if(tof_track){
355                tof_track->ToFTrkVar::Clear();//???
356                delete tof_track;
357            }
358        }else{
359            Clear();
360        }
361    }
362  //--------------------------------------  //--------------------------------------
363  //  //
364  //  //
365  //--------------------------------------  //--------------------------------------
366  /**  /**
367   * Constructor   * Default Constructor
368   */   */
369  PamLevel2::PamLevel2(){  PamLevel2::PamLevel2(){
370        Initialize();
371    };
372    
373    /**
374     * Constructor
375     * @param ddir Name of directory where level2 files are stored.
376     * @param list Name of an ascii file containing the list of file names
377     * @param detlist Options to chose what to load.
378     * Possible options are:
379     *       +AUTO --> load all trees/branches in the input files
380     *       +(-)ALL --> inlcude(exclude) all trees/branches
381     *       +(-)TRK1+(-)TRK2+(-)CAL1+(-)CAL2+(-)TOF+(-)TRG+(-)ND+(-)S4+(-)ORB+(-)AC --> inlcude(exclude) trees and branches
382     *       +(-)TRK0 --> include(exclude) tracker level0 tree
383     *       +(-)GP --> include exclude GPAMELA output tree
384     * If no options are specified, the default is assumed. Default is:
385     * +TRK2+CAL2+CAL1+TOF+TRG+ND+AC+S4+ORB
386     */
387    PamLevel2::PamLevel2(TString ddir,TString list,TString detlist){
388        Initialize();
389        TList*  listf = GetListOfLevel2Files(ddir,list);
390        GetPamTree(listf,detlist);
391        GetRunTree(listf);
392    };
393    
394    /**
395     * Constructor
396     * @param ddir Name of directory where level2 files are stored.
397     * @param list Name of an ascii file containing the list of file names
398     * Default trees/branches are loaded. Default is:
399     * +TRK2+CAL2+CAL1+TOF+TRG+ND+AC+S4+ORB
400     */
401    PamLevel2::PamLevel2(TString ddir,TString list){
402        Initialize();
403        TList*  listf = GetListOfLevel2Files(ddir,list);
404        GetPamTree(listf,"");
405        GetRunTree(listf);
406    };
407    
408    
409    void PamLevel2::Initialize(){
410                    
411      trk2_obj  = TrkLevel2::GetTrkLevel2();      h0_obj    = 0;
412      trk1_obj  = TrkLevel1::GetTrkLevel1();      trk0_obj  = 0;
413      trkh_obj  = TrkHough::GetTrkHough();      calo0_obj  = 0;
414      calo1_obj = CaloLevel1::GetCaloLevel1();  
415      calo2_obj = CaloLevel2::GetCaloLevel2();      trk2_obj  = 0;
416      tof_obj   = ToFLevel2::GetToFLevel2();      trk1_obj  = 0;
417      trig_obj  = TrigLevel2::GetTrigLevel2();      trkh_obj  = 0;
418      s4_obj    = S4Level2::GetS4Level2();      calo1_obj = 0;
419      nd_obj    = NDLevel2::GetNDLevel2();      calo2_obj = 0;
420      ac_obj    = AcLevel2::GetAcLevel2();      tof_obj   = 0;
421      orb_obj   = OrbitalInfo::GetOrbitalInfo();      trig_obj  = 0;
422        s4_obj    = 0;
423      run_obj   = new GL_RUN();      nd_obj    = 0;
424            ac_obj    = 0;
425  //     trk1_clone  = 0;      orb_obj   = 0;
426  //     trk2_clone  = 0;      gp_obj    = 0;
427  //     trkh_clone  = 0;  
428  //     calo1_clone = 0;      run_obj   = 0;//new GL_RUN();
429  //     calo2_clone = 0;      soft_obj   = 0;// Emiliano
430  //     tof_clone   = 0;      irun = -1LL;
431  //     trig_clone  = 0;      irunt = -1LL;
432  //     s4_clone    = 0;      runfirstentry = 0LL;
433  //     nd_clone    = 0;      runlastentry = 0LL;
434  //     ac_clone    = 0;  
435  //     orb_clone   = 0;      l0_file = NULL;
436        l0_tree = NULL;
437  //     T_clone  = 0;      iroot   = -1;
438  //     C_clone  = 0;      dbc     = 0;
439  //     O_clone  = 0;  
440  //     R_clone  = 0;      prevshift = 0;
441  //     S_clone  = 0;      maxshift = 10; //EMILIANO
442  //     N_clone  = 0;      
443  //     A_clone  = 0;      run_tree = NULL;
444  //     B_clone  = 0;      run_tree_clone = NULL;
445        sel_tree = NULL;
446        sel_tree_clone = NULL;
447        
448        irunentry = -1LL;
449        pam_tree = NULL;
450        for(Int_t i=0; i<NCLONES; i++ )pam_tree_clone[i]=NULL;
451    
452        totdltime[0] = 0LL;
453        totdltime[1] = 0LL;
454        totdltime[2] = 0LL;
455    
456        host = "mysql://localhost/pamelaprod";
457        user = "anonymous";
458        psw = "";
459        const char *pamdbhost=gSystem->Getenv("PAM_DBHOST");
460        const char *pamdbuser=gSystem->Getenv("PAM_DBUSER");
461        const char *pamdbpsw=gSystem->Getenv("PAM_DBPSW");
462        if ( !pamdbhost ) pamdbhost = "";
463        if ( !pamdbuser ) pamdbuser = "";
464        if ( !pamdbpsw ) pamdbpsw = "";
465        if ( strcmp(pamdbhost,"") ) host = pamdbhost;
466        if ( strcmp(pamdbuser,"") ) user = pamdbuser;
467        if ( strcmp(pamdbpsw,"") ) psw = pamdbpsw;
468    
469      for(Int_t i=0; i<8; i++ )tree_clone[i]=NULL;      
470    
471      sorted_tracks = new TRefArray();  //    sorted_tracks = 0;//new TRefArray();
472            
473        CAL0 = false;
474      CAL1 = true;      CAL1 = true;
475      CAL2 = true;      CAL2 = true;
476      TRK2 = true;      TRK2 = true;
477      TRK1 = false;      TRK1 = false;
478        TRK0 = false;
479      TRKh = false;      TRKh = false;
480      TRG = true;      TRG  = true;
481      TOF = true;      TOF  = true;
482      S4  = true;      TOF0 = false;
483      ND  = true;      S4   = true;
484      AC  = true;      ND   = true;
485      ORB = true;      AC   = true;
486        ORB  = true;    
487        GP   = false;
488    
489        RUN  = true;
490    
491        SELLI = -1;
492    
493        ISGP = false;
494    
495        DBG = false;
496    
497        tsorted=0;
498        timage=0;
499            
500        howtosort = "+CAL+TOF";
501        //howtosort = "+TOF";
502        sortthr = 100.;
503    
504  };  };
505  /**  /**
506   * Destructor   * Delete the event (act as Dtor)
507   */   */
508  PamLevel2::~PamLevel2(){                  void PamLevel2::Delete(){
509      delete run_obj;          
510      Clear();      if(run_obj)delete run_obj;
511        if(soft_obj)delete soft_obj; //Emiliano
512    
513    //    cout << "void PamLevel2::Clear()"<<endl;
514        if(h0_obj)   delete h0_obj;
515        if(trk0_obj) delete trk0_obj;      
516        if(calo0_obj) delete calo0_obj;    
517        if(trk1_obj) delete trk1_obj;      
518        if(trk2_obj) delete trk2_obj;
519        if(trkh_obj) delete trkh_obj;
520        if(calo1_obj)delete calo1_obj;
521        if(calo2_obj)delete calo2_obj;
522        if(tof_obj)  delete tof_obj;
523        if(trig_obj) delete trig_obj;
524        if(s4_obj)   delete s4_obj;
525        if(nd_obj)   delete nd_obj;
526        if(ac_obj)   delete ac_obj;
527        if(orb_obj)  delete orb_obj;
528        if(gp_obj)   delete gp_obj;
529        
530        if(tsorted){
531            tsorted->Delete();
532            delete tsorted;
533        }
534        if(timage){
535            timage->Delete();
536            delete timage;
537        }
538    
539        if(dbc){
540            dbc->Close();
541            delete dbc;
542        }
543        
544        if(l0_file)l0_file->Close();
545        //    if(pam_tree)pam_tree->Delete();;
546    
547        if ( pam_tree ){
548            //
549            // 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...
550            //
551            TList *temp = pam_tree->GetListOfFriends();
552            TList *contents  = new TList; // create chain friend list
553            contents->SetOwner();
554            TIter next(temp);
555            TChain *questo = 0;
556            while ( (questo = (TChain*) next()) ){
557                TString name =  questo->GetName();
558                contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
559            };
560            //
561            // deleting the main chain
562            //
563            pam_tree->Delete();
564            //
565            // deleting the friends...
566            //
567            TIter next2(contents);
568            TChain *questa = 0;
569            while ( questa = (TChain*)next2() ){
570                TString name =  questa->GetName();
571                questa->Delete();      
572                questa=NULL;
573            };
574            //
575        };
576        pam_tree = NULL;
577    
578        if(run_tree)run_tree->Delete();;
579        if(sel_tree)sel_tree->Delete();;
580        for(Int_t i=0; i<NCLONES; i++ )if(pam_tree_clone[i])pam_tree_clone[i]->Delete();;
581        if(run_tree_clone)run_tree_clone->Delete();;
582        if(sel_tree_clone)sel_tree_clone->Delete();;
583        
584        if(irunoffset) delete [] irunoffset;
585    
586  };  };
587    
588  /**  /**
589   * Clear the event   * Clear the event (NB! does not deallocate objects)
590   */   */
591  void PamLevel2::Clear(){  void PamLevel2::Clear(){
592                    
593      TrkLevel1::Clear();  //    cout << "void PamLevel2::Clear()"<<endl;
594            
595      TrkLevel2::Clear();  //
596      CaloLevel2::Clear();  // 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
597      ToFLevel2::Clear();  // want to load them for each event even if they are the same...
598      TrigLevel2::Clear();  //
599      S4Level2::Clear();  //    if(run_obj)delete run_obj;
600      NDLevel2::Clear();  //    if(run_obj) run_obj->Clear();  // Emiliano: Do not deallocate run_obj here, it will give segmentation fault! call clear instead
601      AcLevel2::Clear();  //    if(soft_obj) soft_obj->Clear();
602      OrbitalInfo::Clear();  
603        if(h0_obj)   h0_obj->Clear();      
604  //     if(trk1_clone)trk1_clone->Clear();  //    if(trk0_obj) trk0_obj->Clear();  
605  //     if(trk2_clone)trk2_clone->Clear();      if(trk1_obj) trk1_obj->Clear();    
606  //     if(trkh_clone)trkh_clone->Clear();      if(trk2_obj) trk2_obj->Clear();
607  //     if(calo1_clone)calo1_clone->Clear();      if(trkh_obj) trkh_obj->Clear();
608  //     if(calo2_clone)calo2_clone->Clear();      if(calo0_obj) calo0_obj->Clear();  
609  //     if(tof_clone)tof_clone->Clear();      if(calo1_obj)calo1_obj->Clear();
610  //     if(trig_clone)trig_clone->Clear();      if(calo2_obj)calo2_obj->Clear();
611  //     if(s4_clone)s4_clone->Clear();      if(tof_obj)  tof_obj->Clear();
612  //     if(nd_clone)nd_clone->Clear();      if(trig_obj) trig_obj->Clear();
613  //     if(ac_clone)ac_clone->Clear();      if(s4_obj)   s4_obj->Clear();
614  //     if(orb_clone)orb_clone->Clear();      if(nd_obj)   nd_obj->Clear();
615            if(ac_obj)   ac_obj->Clear();
616      sorted_tracks->Delete(); // clean the reference array      if(orb_obj)  orb_obj->Clear();
617        if(gp_obj)   gp_obj->Clear();
618            
619    //    if(sorted_tracks)sorted_tracks->Clear();
620    //    sorted_tracks.Clear();
621    
622        if(tsorted){
623            tsorted->Delete();
624        }
625        if(timage){
626            timage->Delete();
627        }
628    };
629    
630    void PamLevel2::Reset(){
631        //
632        // First of all clear everything
633        //
634        Clear();
635        //
636        // close and reset chains and pointers
637        //    
638        if ( pam_tree ){
639            //
640            // 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...
641            //
642            TList *temp = pam_tree->GetListOfFriends();
643            TList *contents  = new TList; // create chain friend list
644            contents->SetOwner();
645            TIter next(temp);
646            TChain *questo = 0;
647            while ( (questo = (TChain*) next()) ){
648                TString name =  questo->GetName();
649                contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
650            };
651            //
652            // deleting the main chain
653            //
654            pam_tree->Delete();
655            //
656            // deleting the friends...
657            //
658            TIter next2(contents);
659            TChain *questa = 0;
660            while ( questa = (TChain*)next2() ){
661                TString name =  questa->GetName();
662                questa->Delete();      
663                questa=NULL;
664            };
665            //
666        };
667        pam_tree = NULL;
668        //
669        if(run_tree)run_tree->Delete();;
670        run_tree = NULL;
671        if(sel_tree)sel_tree->Delete();;
672        sel_tree = NULL;
673        //
674        // Close file
675        //
676        if(l0_file)l0_file->Close("R");
677        l0_file = NULL;
678        //
679        h0_obj    = 0;
680        trk0_obj  = 0;
681        calo0_obj  = 0;
682        //
683        trk2_obj  = 0;
684        trk1_obj  = 0;
685        trkh_obj  = 0;
686        calo1_obj = 0;
687        calo2_obj = 0;
688        tof_obj   = 0;
689        trig_obj  = 0;
690        s4_obj    = 0;
691        nd_obj    = 0;
692        ac_obj    = 0;
693        orb_obj   = 0;
694        gp_obj    = 0;
695        //
696        // Reset run pointers
697        //
698        run_obj   = 0;//new GL_RUN();
699        soft_obj   = 0;// Emiliano
700        irun = -1;
701        irunt = -1;
702        runfirstentry = 0ULL;
703        runlastentry = 0ULL;    
704        //
705        totdltime[0] = 0LL;
706        totdltime[1] = 0LL;
707        totdltime[2] = 0LL;
708        //
709  };  };
710    
711    Bool_t PamLevel2::IsGood(){
712        Bool_t goodev=true;
713        //  if(trk2_obj && trk2_obj->UnpackError() != 0 ) goodev = false;
714        if(calo2_obj && calo2_obj->good != 1) goodev = false;
715        if(tof_obj && tof_obj->unpackError != 0) goodev = false;  
716        if(trig_obj && trig_obj->unpackError != 0) goodev = false;
717        if(s4_obj && s4_obj->unpackError != 0) goodev = false;  
718        if(nd_obj && nd_obj->unpackError != 0) goodev = false;  
719        if(ac_obj && ac_obj->unpackError != 255) goodev = false;  
720        //  if(orb_obj)  
721        return goodev;
722    };
723    
724  //--------------------------------------  //--------------------------------------
725  //  //
# Line 136  void PamLevel2::Clear(){ Line 728  void PamLevel2::Clear(){
728  void *PamLevel2::GetPointerTo(const char* c ){  void *PamLevel2::GetPointerTo(const char* c ){
729    
730      TString objname = c;      TString objname = c;
731      if(!objname.CompareTo("TrkLevel1"))return &trk1_obj;  
732      if(!objname.CompareTo("TrkLevel2"))return &trk2_obj;      if(!objname.CompareTo("TrkLevel1"))  {
733      if(!objname.CompareTo("TrkHough"))return &trkh_obj;          if(!trk1_obj){
734      if(!objname.CompareTo("CaloLevel1"))return &calo1_obj;              trk1_obj  = new TrkLevel1();
735      if(!objname.CompareTo("CaloLevel2"))return &calo2_obj;              trk1_obj->Set();
736      if(!objname.CompareTo("ToFLevel2"))return &tof_obj;          }
737      if(!objname.CompareTo("TrigLevel2"))return &trig_obj;          return &trk1_obj;
738      if(!objname.CompareTo("S4Level2"))return &s4_obj;      };
739      if(!objname.CompareTo("NDLevel2"))return &nd_obj;      if(!objname.CompareTo("TrkLevel2"))  {
740      if(!objname.CompareTo("AcLevel2"))return &ac_obj;          if(!trk2_obj){  
741      if(!objname.CompareTo("OrbitalInfo"))return &orb_obj;              trk2_obj  = new TrkLevel2();
742                trk2_obj->Set();
743            }
744            return &trk2_obj;
745        };
746        if(!objname.CompareTo("TrkHough"))   {
747            if(!trkh_obj)  trkh_obj  = new TrkHough();
748            return &trkh_obj;
749        };
750        if(!objname.CompareTo("CaloLevel1")) {
751            if(!calo1_obj) calo1_obj = new CaloLevel1();
752            return &calo1_obj;
753        };
754        if(!objname.CompareTo("CaloLevel2")) {
755            if(!calo2_obj){
756                calo2_obj = new CaloLevel2();
757                calo2_obj->Set();
758            };
759            return &calo2_obj;
760        };
761        if(!objname.CompareTo("ToFLevel2"))  {
762            if(!tof_obj){
763                tof_obj   = new ToFLevel2();
764                tof_obj->Set();
765            }
766            return &tof_obj;
767        };
768        if(!objname.CompareTo("TrigLevel2")) {
769            if(!trig_obj)  trig_obj  = new TrigLevel2();
770            return &trig_obj;
771        };
772        if(!objname.CompareTo("S4Level2"))   {
773            if(!s4_obj)    s4_obj    = new S4Level2();
774            return &s4_obj;
775        };
776        if(!objname.CompareTo("NDLevel2"))   {
777            if(!nd_obj)    nd_obj    = new NDLevel2();
778            return &nd_obj;
779        };
780        if(!objname.CompareTo("AcLevel2"))   {
781            if(!ac_obj)    ac_obj    = new AcLevel2();
782            return &ac_obj;
783        };
784        if(!objname.CompareTo("OrbitalInfo")){
785            if(!orb_obj)   orb_obj   = new OrbitalInfo();
786            return &orb_obj;
787        };
788        if(!objname.CompareTo("GPamela")){
789            if(!gp_obj)   gp_obj   = new GPamela();
790            return &gp_obj;
791        };
792        
793      if(!objname.CompareTo("RunInfo"))return &run_obj;      if(!objname.CompareTo("RunInfo"))return &run_obj;
794    
795        if(!objname.CompareTo("SoftInfo"))return &soft_obj; // Emiliano
796    
797      return NULL;      return NULL;
798  };  };
799  //--------------------------------------  //--------------------------------------
# Line 158  void *PamLevel2::GetPointerTo(const char Line 804  void *PamLevel2::GetPointerTo(const char
804   * Retrieves the calorimeter track matching the seqno-th tracker stored track.   * Retrieves the calorimeter track matching the seqno-th tracker stored track.
805   * (If seqno = -1 retrieves the self-trigger calorimeter track)   * (If seqno = -1 retrieves the self-trigger calorimeter track)
806   */   */
807   CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno){  CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno){
808                
809       if( CaloLevel2::ntrk()==0 ){      if( !calo2_obj )return 0;
810           cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no Calorimeter tracks are stored"<<endl;  
811           return NULL;      if( calo2_obj->CaloLevel2::ntrk()==0 ){
812       };          cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no Calorimeter tracks are stored"<<endl;
813            return NULL;
814        };
815            
816       CaloTrkVar *c = 0;      CaloTrkVar *c = 0;
817       Int_t it_calo=0;      Int_t it_calo=0;
818            
819       do{      do{
820           c = CaloLevel2::GetCaloTrkVar(it_calo);          c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);
821           it_calo++;          it_calo++;
822       } while( c && seqno != c->trkseqno && it_calo < CaloLevel2::ntrk());            } while( c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());    
823            
824       if(!c || seqno != c->trkseqno){      if(!c || seqno != c->trkseqno){
825           c = 0;          c = 0;
826           if(seqno!=-1)cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match Calorimeter stored tracks"<<endl;          if(seqno!=-1)cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match Calorimeter stored tracks"<<endl;
827       };      };
828       return c;      return c;
829            
830   };  };
831  //--------------------------------------  //--------------------------------------
832   //  //
833   //  //
834  //--------------------------------------  //--------------------------------------
835  /**  /**
836    * Retrieves the ToF track matching the seqno-th tracker stored track.   * Retrieves the ToF track matching the seqno-th tracker stored track.
837    * (If seqno = -1 retrieves the tracker-independent tof track)   * (If seqno = -1 retrieves the tracker-independent tof track)
838   */   */
839   ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno){  ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno){
840                    
841       if( ToFLevel2::ntrk()==0 ){      if( !tof_obj )return 0;
842           cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no ToF tracks are stored"<<endl;  
843           return NULL;      if( tof_obj->ToFLevel2::ntrk()==0 ){
844       };          cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no ToF tracks are stored"<<endl;
845            return NULL;
846        };
847            
848       ToFTrkVar *c = 0;      ToFTrkVar *c = 0;
849       Int_t it_tof=0;      Int_t it_tof=0;
850            
851       do{      do{
852           c = ToFLevel2::GetToFTrkVar(it_tof);          c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof);
853           it_tof++;          it_tof++;
854       } while( c && seqno != c->trkseqno && it_tof < ToFLevel2::ntrk());      } while( c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk());
855            
856       if(!c || seqno != c->trkseqno){      if(!c || seqno != c->trkseqno){
857           c = 0;          c = 0;
858           if(seqno!=-1)cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match ToF stored tracks"<<endl;          if(seqno!=-1)cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match ToF stored tracks"<<endl;
859       };      };
860       return c;      return c;
861            
862   };  };
863    
864  //--------------------------------------  //--------------------------------------
865   //  //
866   //  //
867  //--------------------------------------  //--------------------------------------
868  /**  /**
869   * 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 and tof track information.
870   */   */
871   PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t){  PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t){
872                
873       CaloTrkVar *c = 0;      cout <<"PamLevel2::GetPamTrackAlong(TrkTrack* t) **obsolete** "<<endl;
874       ToFTrkVar  *o = 0;      cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;
875    
876        CaloTrkVar *c = 0;
877        ToFTrkVar  *o = 0;
878            
879       if(CAL2) c = GetCaloStoredTrack(t->GetSeqNo());      if(CAL2) c = GetCaloStoredTrack(t->GetSeqNo());
880       if(TOF) o = GetToFStoredTrack(t->GetSeqNo());      if(TOF) o = GetToFStoredTrack(t->GetSeqNo());
881            
882  //    if(t && c && o)track = new PamTrack(t,c,o);  //    if(t && c && o)track = new PamTrack(t,c,o);
883       PamTrack *track = new PamTrack(t,c,o);      PamTrack *track = new PamTrack(t,c,o);
884            
885       return track;      return track;
886    
887   };  };
888  //--------------------------------------  //--------------------------------------
889  //  //
890  //  //
# Line 244  void *PamLevel2::GetPointerTo(const char Line 897  void *PamLevel2::GetPointerTo(const char
897    
898  PamTrack* PamLevel2::GetStoredTrack(Int_t itrk){  PamTrack* PamLevel2::GetStoredTrack(Int_t itrk){
899            
900        cout <<"PamLevel2::GetStoredTrack(Int_t itrk) **to-be-updated** "<<endl;
901        cout <<"for the moment, better use separately the methods: TrkLevel2::GetStoredTrack(seqno) CaloLevel2::GetCaloTrkVar(Int_t notrack) ToFLevel2::GetToFTrkVar(Int_t notrack)"<<endl;
902        cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;
903      PamTrack *track = 0;      PamTrack *track = 0;
904            
905      if( itrk >=0 && itrk < TrkLevel2::ntrk() ){      if( itrk >=0 && itrk < trk2_obj->TrkLevel2::ntrk() ){
906                    
907          TrkTrack *t = TrkLevel2::GetStoredTrack(itrk);          TrkTrack *t = trk2_obj->TrkLevel2::GetStoredTrack(itrk);
908          track = GetPamTrackAlong(t);          track = GetPamTrackAlong(t);
909                    
910            
911    
912      }else{      }else{
913          cout << "PamLevel2::GetStoredTrack(int) : tracker track SeqNo "<< itrk <<" does not exist (GetNTracks() = "<<TrkLevel2::GetNTracks()<<")"<<endl;          cout << "PamLevel2::GetStoredTrack(int) : tracker track SeqNo "<< itrk <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
914      };      };
915            
916      return track;      return track;
# Line 260  PamTrack* PamLevel2::GetStoredTrack(Int_ Line 918  PamTrack* PamLevel2::GetStoredTrack(Int_
918  }  }
919  //--------------------------------------  //--------------------------------------
920  //  //
921    
922    /**
923     * Sort physical (tracker) tracks. Left here as backward compatibility method.
924     **/
925    void PamLevel2::SortTracks(TString how){
926        printf(" WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",how.Data());
927        howtosort = how;  
928        SortTracks();
929    };
930    
931  //  //
932  //--------------------------------------  //--------------------------------------
933  /**  /**
934   * Sort physical (tracker) tracks and stores them in the TRefArray (of TrkTrack objects) which pointer is  PamLevel2::sorted_tracks.   * Sort physical (tracker) tracks.
935   * @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...).
936   * Sorting cryteria:   * Sorting cryteria:
937   * TRK: lower chi**2   * TRK: lower chi**2
938   * CAL: lower Y spatial residual on the first calorimeter plane   * CAL: lower Y spatial residual on the first calorimeter plane
939   * 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).
940     * S1: (ask Emiliano)
941     * S2: (ask Emiliano)
942     * S3: (ask Emiliano)
943     * GP: more GP hits
944   * The default sorting cryterium is "TOF+CAL".   * The default sorting cryterium is "TOF+CAL".
945   *   *
946   * 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).
947   */   */
948  void PamLevel2::SortTracks(TString how){  void PamLevel2::SortTracks(){
949        TString how = howtosort;
950    
951  //    cout << "call SortTracs() "<<endl;      //  cout <<" PamLevel2::SortTracks(TString how) "<<endl;
952    //Save current Object count      if( !trk2_obj ){
953            cout << "void PamLevel2::SortTracks():  TrkLevel2 not loaded !!!";
954            return;
955        };
956        //Save current Object count
957      Int_t ObjectNumber = TProcessID::GetObjectCount();      Int_t ObjectNumber = TProcessID::GetObjectCount();
958    
959  //    cout << "ObjectNumber  "<<ObjectNumber <<endl;      // create TCloneArrays to store tracks and its images
960            if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
961      sorted_tracks->Delete(); //temporaneo???      tsorted->Delete();
962                TClonesArray &ttsorted = *tsorted;
963      // loop over the tracks sorted by the tracker  
964        if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
965        timage->Delete();
966        TClonesArray &ttimage = *timage;
967    
968    
969    
970        //--------------------------------------------------
971        // retrieve sorting method
972        //--------------------------------------------------
973      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
974      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
975      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
976            Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase);
977        Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase);
978        Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase);
979        Bool_t use_GP = how.Contains("GP", TString::kIgnoreCase);
980      
981        if ( use_TOF ){
982            use_S1 = true;
983            use_S2 = true;
984            use_S3 = true;
985        };
986      if( !CAL2 &&  use_CAL) use_CAL = false;      if( !CAL2 &&  use_CAL) use_CAL = false;
987      if( !TOF &&  use_TOF) use_TOF = false;      if( !TOF ){
988                use_TOF = false;
989            use_S1  = false;
990            use_S2  = false;
991            use_S3  = false;
992        }
993        if( !GP ){
994            use_GP  = false;
995        }
996    
997      if( !TRK2 ){      if( !TRK2 ){
998  //      cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;          cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
999          return;          return;
1000      };      };
1001    
1002      //   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;
1003          
1004      for(Int_t i=0; i < TrkLevel2::GetNTracks(); i++){  
1005                //--------------------------------------------------
1006          TrkTrack *ts = 0;      // loop over "physical" tracks sorted by the tracker
1007        //--------------------------------------------------
1008        for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){
1009            
1010            TrkTrack   *ts = 0;
1011            CaloTrkVar *cs = 0;
1012            ToFTrkVar  *os = 0;
1013                    
1014          // get tracker tracks          // get tracker tracks
1015          TrkTrack   *tp = TrkLevel2::GetTrack(i);                    //tracker          TrkTrack   *tp = trk2_obj->TrkLevel2::GetTrack(i); //tracker
1016          CaloTrkVar *cp = 0;          CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());
1017          ToFTrkVar  *op = 0;          ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());
1018    
1019  //      cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;          TrkTrack   *ti = 0; //tracker (image)
1020            CaloTrkVar *ci = 0;
1021            ToFTrkVar  *oi = 0;
1022            //      cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
1023          // if track has an image, check image selection          // if track has an image, check image selection
1024    
1025            Int_t tp_score = 0;  //main track sorted by the tracker
1026            Int_t ti_score = 0;  //image track
1027            Int_t totp_score = 0;  //main track sorted by the tracker
1028            Int_t toti_score = 0;  //image track
1029    
1030          if(tp->HasImage()){          if(tp->HasImage()){
1031                            
1032              TrkTrack   *ti = TrkLevel2::GetTrackImage(i);              //tracker (image)              ti = trk2_obj->TrkLevel2::GetTrackImage(i);              //tracker (image)
1033              CaloTrkVar *ci = 0;              ci = GetCaloStoredTrack(ti->GetSeqNo());
1034              ToFTrkVar  *oi = 0;              oi = GetToFStoredTrack(ti->GetSeqNo());
1035                            
1036  //          cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;              //      cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
1037    
1038              //assign starting scores              //assign starting scores
1039              Int_t tp_score = 0;  //main track sorted by the tracker              tp_score = 0;  //main track sorted by the tracker
1040              Int_t ti_score = 0;  //image track              ti_score = 0;  //image track
1041                            
1042              // ------------------------              // -----------------------------------------------------------------------------------------
1043                // *****************************************************************************************
1044                // -----------------------------------------------------------------------------------------
1045              // calorimeter check              // calorimeter check
1046              // ------------------------              // -----------------------------------------------------------------------------------------
1047              // check the Y spatial residual on the first calorimeter plane              if( use_CAL && !calo2_obj ){
1048              // (cut on calorimeter variables from Emiliano)                  cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but CaloLevel2 not loaded !!!";
1049                    return;
1050                };
1051                if( use_CAL && !cp &&  ci ){
1052                    ti_score++;
1053                    toti_score++;
1054                };
1055                if( use_CAL &&  cp && !ci ){
1056                    tp_score++;
1057                    totp_score++;
1058                };
1059              if(              if(
1060                  use_CAL            &&                  use_CAL            &&
1061                  npcfit[1] > 15     &&   //no. of fit planes on Y view                  cp && ci &&
                 varcfit[1] < 1000. &&  //fit variance on Y view  
1062                  true){                  true){
1063    
1064                  cp = GetCaloStoredTrack(tp->GetSeqNo());                  
1065                  ci = GetCaloStoredTrack(ti->GetSeqNo());                  if(
1066                                        cp->npresh > ci->npresh &&
1067                  Float_t resy_p = cp->tbar[0][1] - cbar[0][1]; if(resy_p < 0)resy_p= - resy_p;                      true){
1068                  Float_t resy_i = ci->tbar[0][1] - cbar[0][1]; if(resy_i < 0)resy_i= - resy_i;                      tp_score++;
1069                                        totp_score++;
1070                  if(resy_p <= resy_i) tp_score++;                  };
1071                  else                 ti_score++;                  if(
1072                        cp->npresh < ci->npresh &&
1073                        true){
1074                        ti_score++;
1075                        toti_score++;
1076                    };
1077    
1078  //              cout << "CALO "<<tp_score<<ti_score<<endl;                  //      cout << "CALO "<<tp_score<<ti_score<<endl;
1079    
1080              };              };
1081              // ------------------------              // -----------------------------------------------------------------------------------------
1082                // *****************************************************************************************
1083                // -----------------------------------------------------------------------------------------
1084              // TOF check              // TOF check
1085              // ------------------------                  // -----------------------------------------------------------------------------------------
1086              // check the number of hit pmts along the track              // check the number of hit pmts along the track
1087              // on S12 S21 and S32, where paddles are parallel to Y axis              // on S12 S21 and S32, where paddles are parallel to Y axis
1088              if( use_TOF ){              if( (use_TOF || use_S1 || use_S2 || use_S3 ) && !tof_obj ){
1089                    cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but ToFLevel2 not loaded !!!";
1090                    return;
1091                };
1092                //
1093                if( (use_TOF || use_S1 || use_S2 || use_S3 ) && !op && oi ){
1094                    ti_score++;
1095                    toti_score++;
1096                };
1097                if( (use_TOF || use_S1 || use_S2 || use_S3 ) && op && !oi ){
1098                    tp_score++;
1099                    totp_score++;
1100                };
1101                if( (use_TOF || use_S1 || use_S2 || use_S3 ) && op && oi ){
1102                    //
1103                    Float_t sen = 0.;
1104                    for (Int_t ih=0; ih < op->npmtadc; ih++){
1105                        Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1106                        if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (op->dedx).At(ih);
1107                    };
1108                    for (Int_t ih=0; ih < oi->npmtadc; ih++){
1109                        Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1110                        if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (oi->dedx).At(ih);
1111                    };
1112                    //
1113                    if (  sen >= sortthr && false){ // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!)
1114                        //printf(" IS A NUCLEUS! en = %f \n",sen);
1115                        //
1116                        // is a nucleus use a different algorithm
1117                        //
1118                        Int_t nz = 6; Float_t zin[6];                                          // << define TOF z-coordinates
1119                        for(Int_t ip=0; ip<nz; ip++)
1120                            zin[ip] = tof_obj->ToFLevel2::GetZTOF(tof_obj->ToFLevel2::GetToFPlaneID(ip));     // << read ToF plane z-coordinates
1121                        Trajectory *tr = new Trajectory(nz,zin);
1122                        //
1123                        Int_t nphit_p =0;
1124                        Int_t nphit_i =0;
1125                        Float_t enhit_p = 0.;
1126                        Float_t enhit_i = 0.;
1127                        //
1128                        for (Int_t ih=0; ih < op->npmtadc; ih++){
1129                            Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1130                            if(pl == 1 || pl == 2 || pl == 5){
1131                                nphit_p++;
1132                                enhit_p += (op->dedx).At(ih);
1133                            };
1134                        };
1135                        //
1136                        tp->DoTrack2(tr);
1137                        //
1138                        if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
1139                            for (Int_t ih=0; ih < op->npmtadc; ih++){
1140                                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1141                                if(pl == 0){
1142                                    nphit_p++;
1143                                    enhit_p += (op->dedx).At(ih);
1144                                };
1145                            };
1146                        };
1147                        if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
1148                            for (Int_t ih=0; ih < op->npmtadc; ih++){
1149                                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1150                                if(pl == 3){
1151                                    nphit_p++;
1152                                    enhit_p += (op->dedx).At(ih);
1153                                };
1154                            };
1155                        };
1156                        if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
1157                            for (Int_t ih=0; ih < op->npmtadc; ih++){
1158                                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1159                                if(pl == 4){
1160                                    nphit_p++;
1161                                    enhit_p += (op->dedx).At(ih);
1162                                };
1163                            };
1164                        };
1165    
1166                        for (Int_t ih=0; ih < oi->npmtadc; ih++){
1167                            Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1168                            if(pl == 1 || pl == 2 || pl == 5){
1169                                nphit_i++;  
1170                                enhit_i += (op->dedx).At(ih);
1171                            };
1172                        };
1173                        //
1174                        ti->DoTrack2(tr);
1175                        //
1176                        if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
1177                            for (Int_t ih=0; ih < oi->npmtadc; ih++){
1178                                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1179                                if(pl == 0){
1180                                    nphit_i++;
1181                                    enhit_i += (op->dedx).At(ih);
1182                                };
1183                            };
1184                        };
1185                        if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
1186                            for (Int_t ih=0; ih < oi->npmtadc; ih++){
1187                                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1188                                if(pl == 3){
1189                                    nphit_i++;
1190                                    enhit_i += (op->dedx).At(ih);
1191                                };
1192                            };
1193                        };
1194                        if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
1195                            for (Int_t ih=0; ih < oi->npmtadc; ih++){
1196                                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1197                                if(pl == 4){
1198                                    nphit_i++;
1199                                    enhit_i += (op->dedx).At(ih);
1200                                };
1201                            };
1202                        };          
1203    
1204                                    
1205                  Int_t nphit_p =0;                      if(
1206                  Int_t nphit_i =0;                          (use_TOF || use_S1 || use_S2 || use_S3)            &&
1207                            (nphit_p+nphit_i) !=0 &&        
1208                            true){
1209                        
1210                            //          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);
1211                            //          printf(" score p %i score i %i \n",tp_score,ti_score);
1212                            //                if( enhit_p > enhit_i ) tp_score++;
1213                            //                if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
1214                            if ( nphit_p > nphit_i ) tp_score++;
1215                            if ( nphit_p < nphit_i ) ti_score++;
1216                            if ( nphit_p == nphit_i ){
1217                                if ( enhit_p > enhit_i ) tp_score++;
1218                                else ti_score++;
1219                            };
1220                            //          printf(" dopo score p %i score i %i \n",tp_score,ti_score);
1221                        };
1222                        delete tr;
1223                        //
1224                    } else {
1225                        // -------------
1226                        // NOT a NUCLEUS
1227                        // -------------
1228                        //printf(" NOT a NUCLEUS! en = %f \n",sen);
1229    
1230                        Int_t nphit_p =0;
1231                        Int_t nphit_i =0;
1232                                    
                 op = GetToFStoredTrack(tp->GetSeqNo());  
                 oi = GetToFStoredTrack(ti->GetSeqNo());  
1233                                    
1234  /*                              cout << "track: npmtadc "<< op->npmtadc << endl;                      /*                          cout << "track: npmtadc "<< op->npmtadc << endl;
1235                                  cout << "track: npmttdc "<< op->npmttdc << endl;                                                  cout << "track: npmttdc "<< op->npmttdc << endl;
1236                                  cout << "image: npmtadc "<< oi->npmtadc << endl;                                                  cout << "image: npmtadc "<< oi->npmtadc << endl;
1237                                  cout << "image: npmttdc "<< oi->npmttdc << endl;*/                                                  cout << "image: npmttdc "<< oi->npmttdc << endl;*/
1238                                    
1239                  for (Int_t ih=0; ih < op->npmtadc; ih++){  //        for (Int_t ih=0; ih < op->npmtadc; ih++){
1240                      Int_t pl = GetPlaneIndex( (op->pmtadc).At(ih) );  //          Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1241                      if(pl == 1 || pl == 2 || pl == 5)nphit_p++;  //          if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
1242                  };  //        };
1243                                    
1244                  for (Int_t ih=0; ih < oi->npmtadc; ih++){  //        for (Int_t ih=0; ih < oi->npmtadc; ih++){
1245                      Int_t pl = GetPlaneIndex( (oi->pmtadc).At(ih) );  //          Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1246                      if(pl == 1 || pl == 2 || pl == 5)nphit_i++;  //          if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
1247                  };  //        };
1248                        // --- modified to count tdc signals (more efficient?)
1249                        // --- and to implement check on tdcflag
1250                        for (Int_t ih=0; ih < op->npmttdc; ih++){
1251                            Int_t pl = tof_obj->GetPlaneIndex( (op->pmttdc).At(ih) );
1252    //          if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
1253                            if ( (use_S1 && ( pl == 0 || pl == 1 )) || (use_S2 && ( pl == 2 || pl == 3 )) || (use_S3 && ( pl == 4 || pl == 5 )) ){
1254                                if( (op->tdcflag).At(ih)==0 )nphit_p++;
1255                            };
1256                        };
1257                                    
1258                  if(                      for (Int_t ih=0; ih < oi->npmttdc; ih++){
1259                      use_TOF            &&                          Int_t pl = tof_obj->GetPlaneIndex( (oi->pmttdc).At(ih) );
1260                      (nphit_p+nphit_i) !=0 &&      //          if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;  
1261                      true){                          if ( (use_S1 && ( pl == 0 || pl == 1 )) || (use_S2 && ( pl == 2 || pl == 3 )) || (use_S3 && ( pl == 4 || pl == 5 )) ){
1262                                if( (oi->tdcflag).At(ih)==0 )nphit_i++;    
1263                            };
1264                        };
1265                    
1266                        if(
1267                            (nphit_p+nphit_i) !=0 &&        
1268                            true){
1269                                            
1270                      if( nphit_p >= nphit_i) tp_score++;                          if ( nphit_p != nphit_i ){
1271                      else ti_score++;                              totp_score += nphit_p;
1272                                toti_score += nphit_i;
1273                                tp_score+=nphit_p;
1274                                ti_score+=nphit_i;
1275                            };
1276                            //          if     ( nphit_p > nphit_i) tp_score+=nphit_p;
1277                            //          else if( nphit_p < nphit_i) ti_score+=nphit_i;
1278                            //          else ;//niente
1279                        };
1280                  };                  };
1281  //              cout << "TOF "<<tp_score<<ti_score<<endl;                  //      cout << "TOF "<<tp_score<<ti_score<<endl;
1282              };              };
1283              if(tp_score == ti_score) use_TRK = true;  
1284              // ------------------------  
1285    //      if(tp_score == ti_score) use_TRK = true;
1286    
1287    
1288                // -----------------------------------------------------------------------------------------
1289                // *****************************************************************************************
1290                // -----------------------------------------------------------------------------------------
1291              // tracker check              // tracker check
1292              // ------------------------              // -----------------------------------------------------------------------------------------
1293              // chi**2 difference is not always large enough to distinguish among              // chi**2 difference is not always large enough to distinguish among
1294              // the real track and its image.              // the real track and its image.
1295              // 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.
1296                // *** MODIFIED ON AUGUST 2007 ***
1297              if(use_TRK){              if(use_TRK){
1298                  if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;  //      if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
1299                  else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;  //      else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
1300  //              cout << "TRK "<<tp_score<<ti_score<<endl;  
1301                    // CHECK 1 : number of points along X  
1302                    if ( tp->GetNX() >= ti->GetNX() ){
1303                        tp_score++ ;
1304                        totp_score++ ;
1305                    };
1306                    if ( tp->GetNX() <= ti->GetNX() ){
1307                        ti_score++ ;
1308                        toti_score++ ;
1309                    };
1310                    // CHECK 2 : number of points along Y  
1311                    if ( tp->GetNY() >= ti->GetNY() ){
1312                        tp_score++ ;
1313                        totp_score++ ;
1314                    };
1315                    if ( tp->GetNY() <= ti->GetNY() ){
1316                        ti_score++ ;
1317                        toti_score++ ;
1318                    };
1319    
1320                    //      cout << "TRK "<<tp_score<<ti_score<<endl;
1321              };              };
1322                            
1323              // ------------------------  
1324                // -----------------------------------------------------------------------------------------
1325                // *****************************************************************************************
1326                // -----------------------------------------------------------------------------------------
1327                // GPamela check
1328                // -----------------------------------------------------------------------------------------
1329                
1330                //---------------------------------------------------
1331                // count the number of GP hits
1332                //---------------------------------------------------
1333                if(use_GP){
1334                    int ngphits_p=0;
1335                    int ngphits_i=0;
1336                    float toll = 0.02; //200 micron
1337                    for(int ih=0; ih<GetGPamela()->Nthspe; ih++){
1338                        int ip = (Int_t) GetGPamela()->Itrpb[ih] - 1;
1339                        if(
1340                            tp &&
1341                            tp->YGood(ip) &&
1342                            fabs(tp->ym[ip]- GetGPamela()->Yavspe[ih])<toll &&
1343                            true) ngphits_p++;
1344                        if( ti &&
1345                            ti->YGood(ip) &&
1346                            fabs(ti->ym[ip]- GetGPamela()->Yavspe[ih])<toll &&
1347                            true) ngphits_i++;
1348                    }
1349                    if(
1350                        ngphits_p > ngphits_i &&
1351                        true){
1352                        tp_score++ ;
1353                        totp_score++ ;                  
1354                    }
1355                    if(
1356                        ngphits_p < ngphits_i &&
1357                        true){
1358                        ti_score++ ;
1359                        toti_score++ ;                  
1360                    }
1361                }
1362    
1363    
1364                // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1365              // the winner is....              // the winner is....
1366              // ------------------------                  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1367              if      (tp_score > ti_score) ts = tp;//the track sorted by the tracker!!              if      (tp_score > ti_score) {
1368              else if (tp_score < ti_score) ts = ti;//its image!!          
1369              else {  
1370                  ts = tp;              }else if (tp_score < ti_score) {
1371  //                              cout << "Warning - track image ambiguity not solved" << endl;  
1372  //                              cout << ts->GetNtot() << " "<< ts->chi2 << " " << npcfit[1] << " "<< nphit_p << endl;  
1373                    ts = ti;//its image!!
1374                    cs = ci;
1375                    os = oi;
1376                    Int_t totis = toti_score;
1377    
1378                    ti = tp;//its image!!
1379                    ci = cp;
1380                    oi = op;
1381    
1382                    tp = ts;//its image!!
1383                    cp = cs;
1384                    op = os;
1385    
1386                    toti_score = totp_score;
1387                    totp_score = totis;
1388    
1389                    
1390                }else {
1391    
1392    //      cout << "Warning - track image ambiguity not solved" << endl;
1393    
1394              };              };
1395                            
1396          }else{          }else{
1397              ts = tp;              totp_score = 1;
1398                toti_score = 0;
1399          
1400                //      ts = tp;
1401                //      cs = cp;
1402                //      os = op;
1403          };          };
1404                    
1405  //      cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;          //      cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
1406          sorted_tracks->Add(ts);//save the track in the sorted array          //      sorted_tracks->Add(ts);//save the track in the sorted array
1407  //      cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;          //      sorted_tracks.Add(ts);//save the track in the sorted array
1408                    //      sorted_tracks.Add(tp);//save the track in the sorted array
1409      };          //      cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
1410            //      cout<<"o "<<tp<<endl;
1411            //      cout<<"o "<<cp<<endl;
1412            //      cout<<"o "<<op<<endl;
1413    
1414            new(ttsorted[i]) PamTrack(tp,cp,op);
1415            new(ttimage[i])  PamTrack(ti,ci,oi);
1416    
1417            ((PamTrack*)(ttsorted[i]))->SetPScore(totp_score);
1418            ((PamTrack*)(ttsorted[i]))->SetIScore(toti_score);
1419            ((PamTrack*)(ttimage[i]))->SetPScore(totp_score);
1420            ((PamTrack*)(ttimage[i]))->SetIScore(toti_score);
1421        };
1422    
1423        if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){
1424            cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() "<<tsorted->GetEntries()<<" != trk2_obj->GetNTracks() = "<<trk2_obj->GetNTracks() <<endl;
1425            tsorted->Delete(); tsorted=0;
1426            timage->Delete(); timage=0;    
1427        }
1428    
1429      //Restore Object count      //Restore Object count
1430      //To save space in the table keeping track of all referenced objects      //To save space in the table keeping track of all referenced objects
1431      //we assume that our events do not address each other. We reset the      //We reset the object count to what it was at the beginning of the event.
     //object count to what it was at the beginning of the event.  
1432      TProcessID::SetObjectCount(ObjectNumber);      TProcessID::SetObjectCount(ObjectNumber);
1433            
1434  };  };
# Line 430  void PamLevel2::SortTracks(TString how){ Line 1440  void PamLevel2::SortTracks(TString how){
1440   * This method overrides TrkLevel2::GetTracks(), where sorting is done by decreasing number of fit points and increasing chi^2.   * This method overrides TrkLevel2::GetTracks(), where sorting is done by decreasing number of fit points and increasing chi^2.
1441   * PamLevel2::GetTracks() keeps the same track order given by TrkLevel2::GetTracks(), but checks image selection by using calorimeter and ToF tracking information.   * PamLevel2::GetTracks() keeps the same track order given by TrkLevel2::GetTracks(), but checks image selection by using calorimeter and ToF tracking information.
1442   */   */
1443  TRefArray *PamLevel2::GetTracks(){  // TRefArray *PamLevel2::GetTracks(){
1444    
1445    // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1446    //     SortTracks("+CAL+TOF");
1447    // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1448    
1449    // //   return  sorted_tracks;
1450    //     return &sorted_tracks;
1451        
1452    //  };
1453    TClonesArray *PamLevel2::GetTracks(){
1454    
1455  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1456      SortTracks("+CAL+TOF");      SortTracks();
1457  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1458    
1459      return sorted_tracks;      return tsorted;
1460   };      
1461    };
1462  //--------------------------------------  //--------------------------------------
1463   //  //
1464   //  //
1465  //--------------------------------------  //--------------------------------------
1466  /**  /**
1467   * Retrieves the it-th Pamela "physical" track.   * Retrieves the it-th Pamela "physical" track.
# Line 448  TRefArray *PamLevel2::GetTracks(){ Line 1469  TRefArray *PamLevel2::GetTracks(){
1469   * @param it Track number, ranging from 0 to GetNTracks().   * @param it Track number, ranging from 0 to GetNTracks().
1470   */   */
1471  PamTrack *PamLevel2::GetTrack(int it){  PamTrack *PamLevel2::GetTrack(int it){
1472    
1473    //    if(!trk2_obj) return 0;
1474                    
1475    // //  *-*-*-*+-*-*-*-*-*-*-*-*-*
1476    //     SortTracks("+CAL+TOF");
1477    // //  *-*-*-*-*-*-*-*-*-*-*-*-*
1478    // //    if(!sorted_tracks)return 0;
1479    //     if(sorted_tracks.IsEmpty())return 0;
1480    
1481    //     PamTrack *track = 0;
1482        
1483    //     if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() && it<sorted_tracks.GetEntriesFast() ){    
1484    //      TrkTrack   *t = (TrkTrack*)sorted_tracks.At(it);
1485    //      track = GetPamTrackAlong(t);
1486    //     }else{
1487    //      cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1488    //     };
1489        
1490    //     return track;
1491    
1492    //    cout << "PamLevel2::GetTrack(int it) "<<endl;
1493  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1494      SortTracks("+CAL+TOF");      SortTracks();
1495  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1496            if(!tsorted)return 0;
1497        if(!tsorted->GetEntries())return 0;
1498    
1499      PamTrack *track = 0;      PamTrack *track = 0;
1500            
1501      if( it >=0 && it < TrkLevel2::GetNTracks() && it<sorted_tracks->GetEntriesFast() ){      if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks()){
1502          TrkTrack   *t = (TrkTrack*)sorted_tracks->At(it);  //      TrkTrack   *t = (TrkTrack*)sorted_tracks.At(it);
1503          track = GetPamTrackAlong(t);  //      track = GetPamTrackAlong(t);
1504            TClonesArray &t = *(tsorted);
1505            track = (PamTrack*)t[it];
1506            
1507      }else{      }else{
1508          cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<TrkLevel2::GetNTracks()<<")"<<endl;          cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1509      };      };
1510            
1511      return track;      return track;
# Line 477  PamTrack *PamLevel2::GetTrack(int it){ Line 1523  PamTrack *PamLevel2::GetTrack(int it){
1523  PamTrack *PamLevel2::GetTrackImage(int it){  PamTrack *PamLevel2::GetTrackImage(int it){
1524    
1525  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1526      SortTracks("+CAL+TOF");      SortTracks();
1527  //  *-*-*-*-*-*-*-*-*-*-*-*-*  //  *-*-*-*-*-*-*-*-*-*-*-*-*
1528        if(!timage)return 0;
1529        if(!timage->GetEntries())return 0;
1530            
1531      PamTrack *image = 0;      PamTrack *image = 0;
1532            
1533      if( it >=0 && it < TrkLevel2::GetNTracks() ){      if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() ){
1534          TrkTrack *temp = (TrkTrack*)sorted_tracks->At(it);          TClonesArray &t = *(tsorted);
1535          if( temp->HasImage() ){          PamTrack *temp = (PamTrack*)t[it];
1536              TrkTrack   *t = TrkLevel2::GetStoredTrack(temp->GetImageSeqNo());          if( temp->GetTrkTrack()->HasImage() ){
1537              image = GetPamTrackAlong(t);              TClonesArray &t = *(timage);
1538                image = (PamTrack*)t[it];
1539          }else{          }else{
1540              cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;  //          cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;
1541          };          };
1542      }else{      }else{
1543          cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<TrkLevel2::GetNTracks()<<")"<<endl;          cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1544      };      };
1545            
1546      return image;      return image;
# Line 507  PamTrack *PamLevel2::GetTrackImage(int i Line 1556  PamTrack *PamLevel2::GetTrackImage(int i
1556   * @param detlist String to select trees to be included   * @param detlist String to select trees to be included
1557   * @return Pointer to a TTree   * @return Pointer to a TTree
1558   */   */
1559  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist="+ALL"){  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ){
   
 //      cout << "WARNING!!! -- obsolete method -- \n";  
 //      cout << "better use TChain *PamLevel2::GetPamTree(TList*, TString) \n";  
1560    
1561        if( detlist.Contains("+AUTO", TString::kIgnoreCase) ) {
1562            cout << "+AUTO"<<endl;
1563            GetWhichTrees(f);
1564        };
1565      SetWhichTrees(detlist);      SetWhichTrees(detlist);
     TTree *Trout =0;  
1566    
1567        if ( pam_tree ){
1568            printf("WARNING: TTree *PamLevel2::GetPamTree(TFile *fl, TString detlist) -- pam_tree already exists!\n ");
1569            return pam_tree;
1570        };
1571        //
1572    
1573        cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;
1574    
1575    //    SetWhichTrees(detlist);
1576    
1577        TTree *Trout =0;
1578    
1579      TString fname = f->GetName();      TString fname = f->GetName();
1580      if(!CheckLevel2File(fname))return NULL;      if(!CheckLevel2File(fname))return NULL;
1581    
1582        //    UInt_t *found=0;    
1583    
1584        cout<< "GetPamTree(TFile*,TString): detector list --> ";
1585        if(TRK1)cout<<"TRK1 ";
1586        if(TRK2)cout<<"TRK2 ";
1587        if(TRKh)cout<<"TRKH ";
1588        if(CAL1)cout<<"CAL1 ";
1589        if(CAL2)cout<<"CAL2 ";
1590        if(TOF)cout<<"TOF ";
1591        if(TRG)cout<<"TRG ";
1592        if(AC)cout<<"AC ";
1593        if(ND)cout<<"ND ";
1594        if(S4)cout<<"S4 ";
1595        if(ORB)cout<<"ORB ";
1596        if(GP)cout<<"GP ";
1597        cout << endl;
1598        if(SELLI)cout<<">>> Found selection-list <<<"<<endl;
1599    
1600        f->cd();
1601    
1602  // Tracker  // Tracker
1603      TTree *T = (TTree*)f->Get("Tracker");      TTree *T = (TTree*)f->Get("Tracker");
1604      if(T && (TRK2||TRK1||TRKh)) {      if(T && (TRK2||TRK1||TRKh)) {
1605          if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));          if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
1606    //      else    T->SetBranchStatus("TrkLevel2",0,found);
1607          if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;          if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;
1608          if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));          if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
1609    //      else    T->SetBranchStatus("TrkLevel1",0,found);
1610          if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;          if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;
1611          if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));          if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1612    //      else    T->SetBranchStatus("TrkHough",0,found);
1613          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;
1614          if(!Trout)Trout=T;          if(!Trout)Trout=T;
1615          else Trout->AddFriend(T);          else Trout->AddFriend(T);
# Line 538  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1620  TTree *PamLevel2::GetPamTree(TFile *f, T
1620      TTree *C = (TTree*)f->Get("Calorimeter");      TTree *C = (TTree*)f->Get("Calorimeter");
1621      if(C && (CAL2||CAL1)) {      if(C && (CAL2||CAL1)) {
1622          if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));          if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1623    //      else    C->SetBranchStatus("CaloLevel2",0,found);
1624          if(CAL2)cout << "Calorimeter  : set branch address CaloLevel2"<<endl;          if(CAL2)cout << "Calorimeter  : set branch address CaloLevel2"<<endl;
1625          if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));          if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1626    //      else    C->SetBranchStatus("CaloLevel1",0,found);
1627          if(CAL1)cout << "Calorimeter  : set branch address CaloLevel1"<<endl;          if(CAL1)cout << "Calorimeter  : set branch address CaloLevel1"<<endl;
1628          if(!Trout)Trout=C;          if(!Trout)Trout=C;
1629          else Trout->AddFriend(C);          else Trout->AddFriend(C);
1630      }else{      }else{
1631          cout << "Calorimeter  : missing tree"<<endl;          cout << "Calorimeter  : missing tree"<<endl;
1632      };      };
1633    
1634      // ToF          // ToF    
1635      TTree *O = (TTree*)f->Get("ToF");      TTree *O = (TTree*)f->Get("ToF");
1636      if(O && TOF) {      if(O && TOF) {
# Line 561  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1646  TTree *PamLevel2::GetPamTree(TFile *f, T
1646      if(R && TRG) {      if(R && TRG) {
1647          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1648          cout << "Trigger      : set branch address TrigLevel2"<<endl;          cout << "Trigger      : set branch address TrigLevel2"<<endl;
1649          if(!Trout)Trout=O;          if(!Trout)Trout=R;
1650          else Trout->AddFriend(R);          else Trout->AddFriend(R);
1651      }else{      }else{
1652          cout << "Trigger      : missing tree"<<endl;          cout << "Trigger      : missing tree"<<endl;
# Line 571  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1656  TTree *PamLevel2::GetPamTree(TFile *f, T
1656      if(S && S4) {      if(S && S4) {
1657          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1658          cout << "S4           : set branch address S4Level2"<<endl;          cout << "S4           : set branch address S4Level2"<<endl;
1659          if(!Trout)Trout=O;          if(!Trout)Trout=S;
1660          else Trout->AddFriend(S);          else Trout->AddFriend(S);
1661      }else{      }else{
1662          cout << "S4           : missing tree"<<endl;          cout << "S4           : missing tree"<<endl;
# Line 581  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1666  TTree *PamLevel2::GetPamTree(TFile *f, T
1666      if(N && ND) {      if(N && ND) {
1667          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1668          cout << "NeutronD     : set branch address NDLevel2"<<endl;          cout << "NeutronD     : set branch address NDLevel2"<<endl;
1669          if(!Trout)Trout=O;          if(!Trout)Trout=N;
1670          else Trout->AddFriend(N);          else Trout->AddFriend(N);
1671      }else{      }else{
1672          cout << "NeutronD     : missing tree"<<endl;          cout << "NeutronD     : missing tree"<<endl;
# Line 591  TTree *PamLevel2::GetPamTree(TFile *f, T Line 1676  TTree *PamLevel2::GetPamTree(TFile *f, T
1676      if(A && AC) {      if(A && AC) {
1677          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1678          cout << "Anticounter  : set branch address AcLevel2"<<endl;          cout << "Anticounter  : set branch address AcLevel2"<<endl;
1679          if(!Trout)Trout=O;          if(!Trout)Trout=A;
1680          else Trout->AddFriend(A);          else Trout->AddFriend(A);
1681      }else{      }else{
1682          cout << "Anticounter  : missing tree"<<endl;          cout << "Anticounter  : missing tree"<<endl;
1683      };      };
1684      // Orbital Info      // Orbital Info
1685          TTree *B = (TTree*)f->Get("OrbitalInfo");      TTree *B = (TTree*)f->Get("OrbitalInfo");
1686          if(B && ORB) {      if(B && ORB) {
1687                  B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1688                  cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
1689                  if(!Trout)Trout=O;          if(!Trout)Trout=B;
1690                  else Trout->AddFriend(B);          else Trout->AddFriend(B);
1691          }else{      }else{
1692                  cout << "OrbitalInfo  : missing tree"<<endl;          cout << "OrbitalInfo  : missing tree"<<endl;
1693          };      };
1694    
1695        // GPamela
1696        TTree *G = (TTree*)f->Get("h20");
1697        if(G && GP) {
1698            if(!gp_obj)gp_obj=new GPamela();
1699    //      ------------------------------------
1700    //      ATTENZIONE!!!
1701    //      non so per quale arcano motivo,
1702    //      per l'albero di gpamela il branch address lo devo settare
1703    //      DOPO aver fatto friend
1704    //      FGRRRVZZZZUTSALKJMSLKJ!!!
1705    //      ------------------------------------
1706    //      gp_obj->SetBranchAddress(G); //ho dovuto fare in maniera diversa dagli altri
1707    //      cout << "h20          : set branch address GPamela "<<endl;
1708            if(!Trout)Trout=G;
1709            else Trout->AddFriend(G);
1710        }else{
1711    //      cout << "h20          : missing tree"<<endl;
1712        };
1713    
1714    
1715        TTree *L = (TTree*)f->Get("SelectionList");
1716        if(L && SELLI==1) {
1717            cout << " TTree *PamLevel2::GetPamTree(TFile, TString) >>> SelectionList not implemented!!!!"<<endl;
1718            sel_tree = 0;
1719        }else{
1720            cout << "SelectionList  : missing tree"<<endl;
1721        };
1722            
1723          cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;      cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;
1724                    
1725          return Trout;  //      ------------------------------------
1726    //      ATTENZIONE!!!
1727    //      non so per quale arcano motivo,
1728    //      per l'albero di gpamela il branch address lo devo settare
1729    //      DOPO aver fatto friend
1730    //      FGRRRVZZZZUTSALKJMSLKJ!!!
1731    //      ------------------------------------
1732        if(G && GP) {      
1733            gp_obj->SetBranchAddress(Trout); //ho dovuto fare in maniera diversa dagli altri
1734            cout << "h20          : set branch address GPamela "<<endl;
1735        }else{
1736            cout << "h20          : missing tree"<<endl;
1737        };
1738    
1739        pam_tree = (TChain*)Trout;
1740        
1741        return Trout;
1742            
1743  }  }
1744  //--------------------------------------  //--------------------------------------
# Line 627  TList*  PamLevel2::GetListOfLevel2Files( Line 1756  TList*  PamLevel2::GetListOfLevel2Files(
1756                    
1757      TString wdir = gSystem->WorkingDirectory();      TString wdir = gSystem->WorkingDirectory();
1758            
1759      if(ddir=="")ddir = wdir;      //    if(ddir=="")ddir = wdir;
1760  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1761      cout << "Level2 data directory : "<<  ddir << endl;      if ( ddir != ""){
1762                cout << "Level2 data directory : "<<  ddir << endl;
1763        } else {
1764            cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory " << endl;
1765        };
1766      TList *contents  = new TList; // create output list      TList *contents  = new TList; // create output list
1767      contents->SetOwner();      contents->SetOwner();
1768            
1769      char *fullpath;  //    char *fullpath;
1770    //    const char *fullpath;
1771            
1772      // if no input file list is given:        // if no input file list is given:  
1773      if ( flisttxt != "" ){      if ( flisttxt != "" ){
1774                    
1775          if( !gSystem->IsFileInIncludePath(flisttxt,&fullpath) )return 0;  //      if( !gSystem->IsFileInIncludePath(flisttxt,&fullpath) ){        
1776            //      if( !(fullpath = gSystem->FindFile("./",flisttxt)) ){
1777  //              flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));  //          cout <<"File "<<flisttxt<<" not found"<<endl;
1778          flisttxt = fullpath;  //          return 0;
1779            //      }      
1780          if( !gSystem->ChangeDirectory(ddir) )return 0;  //      flisttxt = fullpath;
1781            if ( !flisttxt.EndsWith(".root") ){
1782    
1783                flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1784    
1785                if( !gSystem->ChangeDirectory(ddir) ){
1786                    cout << "Cannot change directory : "<<ddir<<endl;
1787                    return 0;
1788                }
1789                    
1790          cout <<"Input file list : " << flisttxt <<endl;              cout <<"Input file list : " << flisttxt <<endl;
1791          ifstream in;              ifstream in;
1792          in.open(flisttxt, ios::in); //open input file list              in.open(flisttxt, ios::in); //open input file list
1793          int line=0;              if(!in.good()){
1794          while (1) {                  cout <<" ERROR opening the file "<<endl;
1795              TString file;                  gSystem->ChangeDirectory(wdir); // back to the working directory
1796              in >> file;                  return 0;
1797              if (!in.good()) break;              }  
1798              line++;              int line=0;
1799                while (1) {
1800                    TString file;
1801                    in >> file;
1802                    if (!in.good()) break;
1803                    line++;
1804  //          cout <<"(1) " << file << endl;  //          cout <<"(1) " << file << endl;
1805              if(file.IsNull()){                  if(file.IsNull()){
1806                  cout << "-- list interrupted at line "<<line <<endl;                      cout << "-- list interrupted at line "<<line <<endl;
1807                  break;                      break;
1808              }                  }
1809              if(file.Contains("#"))file = file(0,file.First("#"));                  if(file.Contains("#"))file = file(0,file.First("#"));
1810  //          cout <<"(2) " << file << endl;  //          cout <<"(2) " << file << endl;
1811              if( gSystem->IsFileInIncludePath(file,&fullpath) ){  //          if( gSystem->IsFileInIncludePath(file,&fullpath) ){
1812    //          if( (fullpath = gSystem->FindFile(ddir,file)) ){
1813                  contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list                  if( file.EndsWith(".root") ){
1814                        TString filedir;
1815              }else{                      if (ddir != ""){
1816                            filedir = ddir; // take the input dir
1817                        } else {
1818                            gSystem->ChangeDirectory(wdir); // back to the working directory
1819                            filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir
1820                        };
1821                        char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir),gSystem->BaseName(file));
1822                        contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list
1823                        delete fullpath;
1824                    }
1825    //          }else{
1826  //              if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;  //              if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;
1827    //          };
1828              };              };
1829          };              in.close();
1830          in.close();          } else {
1831                        if(flisttxt.Contains("#"))flisttxt = flisttxt(0,flisttxt.First("#"));
1832                char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1833                contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list
1834                delete fullpath;
1835            };      
1836      }else{      }else{
1837                    
1838          cout << "No input file list given."<<endl;          cout << "No input file list given."<<endl;
1839          cout << "Check for existing root files."<<endl;          cout << "Check for existing root files."<<endl;
1840  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;
1841                    if ( ddir == "" ){
1842                ddir = wdir;
1843                cout << "Level2 data directory : "<<  ddir << endl;
1844            };
1845    
1846          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1847          TList *temp = datadir->GetListOfFiles();          TList *temp = datadir->GetListOfFiles();
1848  //              temp->Print();  //              temp->Print();
# Line 690  TList*  PamLevel2::GetListOfLevel2Files( Line 1855  TList*  PamLevel2::GetListOfLevel2Files(
1855          while ( (questo = (TSystemFile*) next()) ) {          while ( (questo = (TSystemFile*) next()) ) {
1856              TString name =  questo-> GetName();              TString name =  questo-> GetName();
1857              if( name.EndsWith(".root") ){              if( name.EndsWith(".root") ){
1858                  char *fullpath;  //              const char *fullpath = gSystem->FindFile(ddir,name);
1859                  gSystem->IsFileInIncludePath(name,&fullpath);  //              char *fullpath;
1860    //              gSystem->IsFileInIncludePath(name,&fullpath);
1861                    char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir),gSystem->BaseName(name));
1862                  contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));                  contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));
1863                    delete fullpath;
1864              };              };
1865          }          }
1866          delete temp;          delete temp;
# Line 702  TList*  PamLevel2::GetListOfLevel2Files( Line 1870  TList*  PamLevel2::GetListOfLevel2Files(
1870      gSystem->ChangeDirectory(wdir); // back to the working directory      gSystem->ChangeDirectory(wdir); // back to the working directory
1871  //      cout << endl << "Selected files:" << endl;  //      cout << endl << "Selected files:" << endl;
1872  //      contents->Print();  //      contents->Print();
1873      cout << contents->GetEntries()<<" files selected\n";      cout << contents->GetEntries()<<" files \n";
1874  //      cout << endl;  //      cout << endl;
1875  //      cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;  //      cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;
1876      return contents;      return contents;
# Line 717  TList*  PamLevel2::GetListOfLevel2Files( Line 1885  TList*  PamLevel2::GetListOfLevel2Files(
1885   * @param detlist String to select trees to be included   * @param detlist String to select trees to be included
1886   * @return Pointer to a TChain   * @return Pointer to a TChain
1887   */   */
1888  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist="+ALL"){  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){
1889        //
1890        //
1891        //
1892        if ( pam_tree ){
1893            printf("WARNING: TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) -- pam_tree already exists!\n ");
1894            return pam_tree;
1895        };
1896        //
1897        
1898        TChain *Trout =0;
1899    
1900        // -------------------------------------------
1901        // set flags to include/exclude trees/branches
1902        // -------------------------------------------
1903        if( detlist.Contains("+AUTO", TString::kIgnoreCase) ) {
1904            if( fl->GetEntries()>0 ){
1905                cout << "+AUTO"<<endl;
1906                TFile *fprimo = new TFile( fl->At(0)->GetName() );
1907                GetWhichTrees(fprimo);
1908                fprimo->Close();// AAAAARGGGGGHHHHH!!!!!!! non commentare questa riga, altrimenti si incasina il TChain
1909                fprimo->Delete();
1910            }
1911        };
1912        SetWhichTrees(detlist);    
1913    
 //      TChain *Tout=0;  
 //    if(Tout)Tout->Delete();  
 //    Tout = NULL;  
       
     TChain *Tout =0;  
1914    
1915      SetWhichTrees(detlist);      // -------------------------------------------
1916            // build chains
1917        // -------------------------------------------
1918      TChain *T = 0;            TChain *T = 0;      
1919      TChain *C = 0;      TChain *C = 0;
1920      TChain *O = 0;      TChain *O = 0;
# Line 735  TChain *PamLevel2::GetPamTree(TList *fl, Line 1923  TChain *PamLevel2::GetPamTree(TList *fl,
1923      TChain *N = 0;      TChain *N = 0;
1924      TChain *A = 0;      TChain *A = 0;
1925      TChain *B = 0;      TChain *B = 0;
1926        TChain *G = 0;
1927    
1928        TChain *L = 0;
1929            
1930      if(TRK2||TRK1||TRKh) T = new TChain("Tracker");          if(TRK2||TRK1||TRKh) T = new TChain("Tracker");    
1931      if(CAL2||CAL1) C = new TChain("Calorimeter");      if(CAL2||CAL1)       C = new TChain("Calorimeter");
1932      if(TOF) O = new TChain("ToF");      if(TOF)              O = new TChain("ToF");
1933      if(TRG) R = new TChain("Trigger");      if(TRG)              R = new TChain("Trigger");
1934      if(S4)  S = new TChain("S4");      if(S4)               S = new TChain("S4");
1935      if(ND)  N = new TChain("NeutronD");      if(ND)               N = new TChain("NeutronD");
1936      if(AC)  A = new TChain("Anticounter");      if(AC)               A = new TChain("Anticounter");
1937      if(ORB) B = new TChain("OrbitalInfo");      if(ORB)              B = new TChain("OrbitalInfo");
1938        if(GP)               G = new TChain("h20");
1939        L = new TChain("SelectionList");
1940            
1941      // loop over files and create chains              // loop over files and create chains        
1942      TIter next(fl);      TIter next(fl);
1943      TSystemFile *questo = 0;      TSystemFile *questo = 0;
1944      while ( (questo = (TSystemFile*) next()) ) {      while ( (questo = (TSystemFile*) next()) ) {
1945          TString name =  questo->GetName();          TString name =  questo->GetName();
1946  //              cout << "File: "<< name << endl;          cout << "File: "<< name << endl;
1947          if( CheckLevel2File(name) ){          if( CheckLevel2File(name) ){
1948              if(TRK2||TRK1||TRKh) T->Add(name);              if(TRK2||TRK1||TRKh) T->Add(name);
1949              if(CAL1||CAL2) C->Add(name);              if(CAL1||CAL2)       C->Add(name);
1950              if(TOF) O->Add(name);              if(TOF)              O->Add(name);
1951              if(TRG) R->Add(name);              if(TRG)              R->Add(name);
1952              if(S4)  S->Add(name);              if(S4)               S->Add(name);
1953              if(ND)  N->Add(name);              if(ND)               N->Add(name);
1954              if(AC)  A->Add(name);              if(AC)               A->Add(name);
1955              if(ORB) B->Add(name);              if(ORB)              B->Add(name);
1956                if(GP)               G->Add(name);
1957                if(SELLI==1)         L->Add(name);
1958          };          };
1959      }      };
       
     cout << "done chain \n";  
1960            
1961        cout << "done chains\n";
1962        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
1963    
1964    
1965        // -------------------------------------------
1966        // make friends
1967        // -------------------------------------------
1968    
1969    // Tracker
1970        cout << "Friends: "<<endl;
1971        if(T && (TRK2||TRK1||TRKh)) {
1972            if(!Trout)Trout=T;
1973            else Trout->AddFriend("Tracker");
1974    //      cout << "+Tacker"<<endl;
1975        };
1976      // Calorimeter      // Calorimeter
1977      if(CAL2||CAL1) {                  if(C && (CAL2||CAL1)) {
1978          if(!Tout)Tout=C;          if(!Trout)Trout=C;
1979          else Tout->AddFriend("Calorimeter");          else Trout->AddFriend("Calorimeter");
1980    //      cout << "+Calorimeter"<<endl;
1981      };      };
1982      // ToF          // ToF    
1983      if(TOF) {      if(O && TOF) {
1984          if(!Tout)Tout=O;          if(!Trout)Trout=O;
1985          else Tout->AddFriend("ToF");          else Trout->AddFriend("ToF");
1986    //      cout << "+ToF"<<endl;
1987      };      };
     // Tracker  
     if(TRK2||TRK1||TRKh){  
         if(!Tout)Tout=T;  
         else Tout->AddFriend("Tracker");  
     }  
1988      // Trigger      // Trigger
1989      if(TRG) {      if(R && TRG) {
1990          if(!Tout)Tout=R;          if(!Trout)Trout=O;
1991          else Tout->AddFriend("Trigger");          else Trout->AddFriend("Trigger");
1992    //      cout << "+Trigger"<<endl;
1993      };      };
1994      // S4      // S4
1995      if(S4) {      if(S && S4) {
1996          if(!Tout)Tout=S;          if(!Trout)Trout=O;
1997          else Tout->AddFriend("S4");          else Trout->AddFriend("S4");
1998    //      cout << "+S4"<<endl;
1999      };      };
2000      // Neutron Detector      // Neutron Detector
2001      if(ND) {      if(N && ND) {
2002          if(!Tout)Tout=N;          if(!Trout)Trout=O;
2003          else Tout->AddFriend("NeutronD");          else Trout->AddFriend("NeutronD");
2004    //      cout << "+NeutronD"<<endl;
2005      };      };
2006      // Anticounters      // Anticounters
2007      if(AC) {      if(A && AC) {
2008          if(!Tout)Tout=A;          if(!Trout)Trout=O;
2009          else Tout->AddFriend("Anticounter");          else Trout->AddFriend("Anticounter");
2010    //      cout << "+Anticounter"<<endl;
2011      };      };
2012      // OrbitalInfo      // Orbital Info
2013      if(ORB) {      if(B && ORB) {
2014          if(!Tout)Tout=B;          if(!Trout)Trout=O;
2015          else Tout->AddFriend("OrbitalInfo");          else Trout->AddFriend("OrbitalInfo");
2016    //      cout << "+OrbitalInfo"<<endl;
2017        };
2018        // GPamela
2019        if(G && GP) {
2020            if(!Trout)Trout=G;
2021            else Trout->AddFriend("h20");
2022    //      cout << "+h20"<<endl;
2023        };
2024    
2025    //  =====================================
2026    //  SET BRANCH-ADDRESS AFTER CHAIN+FRIEND
2027    //  =====================================
2028        SetBranchAddress(Trout);
2029    
2030    
2031    //  ------------------------------------
2032    //  finally handle selection trees...
2033    //  (it is not friend of pamela tree)
2034    //  ------------------------------------
2035    
2036        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2037    
2038        // Selection List
2039        if(L && SELLI==1) {
2040            cout<<">>> Found selection-list <<<"<<endl;
2041    //      L->SetBranchAddress("RunEntry",&irun);
2042            L->SetBranchAddress("RunEntry",&irunt);//NEWNEW
2043            cout << "SelectionList: set branch address RunEntry"<<endl;
2044            L->SetBranchAddress("EventEntry",&irunentry);
2045            cout << "SelectionList: set branch address EventEntry"<<endl;
2046            sel_tree = L;
2047    //      if(!Trout)Trout=O;
2048    //      else Trout->AddFriend("SelectionList");
2049            cout << "+SelectionList"<<endl;
2050            cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2051        }else{
2052    //      cout << "SelectionList  : missing tree"<<endl;
2053            if(L)L->Delete();
2054      };      };
2055    
2056    //  --------------------------------------------
2057    //  return the pamela chain with all the friends
2058    //  --------------------------------------------
2059    
2060        pam_tree = Trout;
2061            
2062  //    cout<<endl<<" Number of entries: "<<Tout->GetEntries()<<endl<<endl;      return Trout;
       
 //    if( Tout->GetEntries() )PamLevel2::SetBranchAddress();  
     if( Tout->GetEntries() )PamLevel2::SetBranchAddress( Tout );  
       
     return Tout;  
2063  }  }
2064    
2065    
# Line 836  void PamLevel2::SetBranchAddress(TTree * Line 2084  void PamLevel2::SetBranchAddress(TTree *
2084      ND     = ND  & t->GetBranchStatus("NDLevel2");      ND     = ND  & t->GetBranchStatus("NDLevel2");
2085      AC     = AC  & t->GetBranchStatus("AcLevel2");      AC     = AC  & t->GetBranchStatus("AcLevel2");
2086      ORB    = ORB & t->GetBranchStatus("OrbitalInfo");      ORB    = ORB & t->GetBranchStatus("OrbitalInfo");
2087        GP     = GP  & t->GetBranchStatus("h20");
2088    
2089    
2090      // Tracker      // Tracker
# Line 892  void PamLevel2::SetBranchAddress(TTree * Line 2141  void PamLevel2::SetBranchAddress(TTree *
2141          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
2142          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
2143      };      };
2144        // GPamela
2145        if(GP) {
2146    //      GetPointerTo("GPamela");
2147            if(!gp_obj)gp_obj = new GPamela();
2148    //      gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2149    // //   t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
2150            if(SELLI) t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
2151            else      gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2152    
2153            cout << "h20          : set branch address GPamela "<<endl;
2154        };
2155            
2156  }  }
2157  /**  /**
# Line 899  void PamLevel2::SetBranchAddress(TTree * Line 2159  void PamLevel2::SetBranchAddress(TTree *
2159   */   */
2160  void PamLevel2::SetBranchAddress(TChain *t){  void PamLevel2::SetBranchAddress(TChain *t){
2161    
2162      TRK2    = TRK2 & t->GetBranchStatus("TrkLevel2");  //     TRK2    = TRK2 & t->GetBranchStatus("TrkLevel2");
2163      TRK1    = TRK1 & t->GetBranchStatus("TrkLevel1");  //     TRK1    = TRK1 & t->GetBranchStatus("TrkLevel1");
2164      TRKh    = TRKh & t->GetBranchStatus("TrkHough");  //     TRKh    = TRKh & t->GetBranchStatus("TrkHough");
2165      CAL1    = CAL1 & t->GetBranchStatus("CaloLevel1");  //     CAL1    = CAL1 & t->GetBranchStatus("CaloLevel1");
2166      CAL2    = CAL2 & t->GetBranchStatus("CaloLevel2");  //     CAL2    = CAL2 & t->GetBranchStatus("CaloLevel2");
2167      TOF    = TOF & t->GetBranchStatus("ToFLevel2");  //     TOF    = TOF & t->GetBranchStatus("ToFLevel2");
2168      TRG    = TRG & t->GetBranchStatus("TrigLevel2");  //     TRG    = TRG & t->GetBranchStatus("TrigLevel2");
2169      S4     = S4  & t->GetBranchStatus("S4Level2");  //     S4     = S4  & t->GetBranchStatus("S4Level2");
2170      ND     = ND  & t->GetBranchStatus("NDLevel2");  //     ND     = ND  & t->GetBranchStatus("NDLevel2");
2171      AC     = AC  & t->GetBranchStatus("AcLevel2");  //     AC     = AC  & t->GetBranchStatus("AcLevel2");
2172      ORB    = ORB & t->GetBranchStatus("OrbitalInfo");  //     ORB    = ORB & t->GetBranchStatus("OrbitalInfo");
2173    //    GP     = GP & t->GetBranchStatus("h20");
2174    
2175      // Tracker      // Tracker
2176       if(TRK2) {      if(TRK2) {
2177          t->SetBranchAddress("TrkLevel2", this->GetPointerTo("TrkLevel2"));          t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
2178          cout << "Tracker      : set branch address TrkLevel2"<<endl;          cout << "Tracker      : set branch address TrkLevel2"<<endl;
2179      };      };
2180      if(TRK1) {      if(TRK1) {
2181          t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));          t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
2182          cout << "Tracker      : set branch address TrkLevel1"<<endl;          cout << "Tracker      : set branch address TrkLevel1"<<endl;
2183      };      };
2184       if(TRKh) {      if(TRKh) {
2185          t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));          t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
2186          cout << "Tracker      : set branch address TrkHough"<<endl;          cout << "Tracker      : set branch address TrkHough"<<endl;
2187       };      };
2188            
2189      // Calorimeter      // Calorimeter
2190      if(CAL2) {      if(CAL2) {
# Line 965  void PamLevel2::SetBranchAddress(TChain Line 2226  void PamLevel2::SetBranchAddress(TChain
2226          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
2227          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;
2228      };      };
2229            // GPamela
2230    //    cout <<"GP "<<GP<<endl;
2231        if(GP) {
2232    //      GetPointerTo("GPamela");
2233            if(!gp_obj)gp_obj = new GPamela();
2234            if(SELLI) t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
2235            else      gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2236    //      gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
2237            cout << "h20          : set branch address GPamela "<<endl;
2238        };
2239        // SelectionList
2240    //     if(SELLI==1){
2241    //      t->SetBranchAddress("RunEntry",&irunt);//NEWNEW
2242    //      cout << "SelectionList: set branch address RunEntry"<<endl;
2243    //      t->SetBranchAddress("EventEntry",&irunentry);
2244    //      cout << "SelectionList: set branch address EventEntry"<<endl;
2245            
2246    //     }
2247    
2248  }  }
2249    
 // void* PamLevel2::GetPointerToTrk()  {return &trk2_obj;};  
 // void* PamLevel2::GetPointerToTrk(int s){  
 //     switch (s) {  
 //     case 1:  return &trk1_obj;  
 //     case 2:  return &trk2_obj;  
 //     case 3:  return &trkh_obj;  
 //     default: return 0;  
 //     };  
 // };  
 // void* PamLevel2::GetPointerToCalo() {return &calo2_obj; };  
 // void*       GetPointerToCalo(int s){  
 //     switch (s) {  
 //     case 1:  return &calo1_obj;  
 //     case 2:  return &calo2_obj;  
 //     default: return 0;  
 //     };  
 // };  
 // void*       GetPointerToToF()  {return &tof_obj;  };  
 // void*       GetPointerTo("TrigLevel2") {return &trig_obj; };  
 // void*       GetPointerTo("S4Level2")   {return &s4_obj;   };  
 // void*       GetPointerTo("NDLevel2")   {return &nd_obj;   };  
 // void*       GetPointerTo("AcLevel2")   {return &ac_obj;   };  
 // void*       GetPointerTo("OrbitalInfo")  {return &orb_obj;  };  
 // void*       GetPointerTo("RunInfo")  {return &run_obj;  };  
2250    
2251  //--------------------------------------  //--------------------------------------
2252  //  //
# Line 1003  void PamLevel2::SetBranchAddress(TChain Line 2258  void PamLevel2::SetBranchAddress(TChain
2258   * @return Pointer to a TChain   * @return Pointer to a TChain
2259   */   */
2260  TChain *PamLevel2::GetRunTree(TList *fl){  TChain *PamLevel2::GetRunTree(TList *fl){
2261                //
2262        //
2263        //
2264        if ( run_tree ){
2265            printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");
2266            return run_tree;
2267        };  
2268        //
2269    
2270    
2271      TChain *R = new TChain("Run");      TChain *R = new TChain("Run");
2272            
2273      // loop over files and create chains              // loop over files and create chains        
# Line 1017  TChain *PamLevel2::GetRunTree(TList *fl) Line 2281  TChain *PamLevel2::GetRunTree(TList *fl)
2281          };          };
2282      }      }
2283            
2284      R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));  
2285      cout << "Run         : set branch address RunInfo"<<endl;      if(RUN && R->GetNtrees()){
2286        
2287            R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
2288            cout << "Run         : set branch address RunInfo"<<endl;
2289            R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
2290            cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano
2291    
2292            irunoffset = new int[R->GetNtrees()];
2293            if(DBG){
2294                cout << "----------------------------------------------------"<<endl;
2295                cout << "irun\t | ";
2296                cout << "tree\t |";
2297    //      cout << "offset\t |";
2298                cout << "RUN\t";
2299                cout << "FRAG\t";
2300                cout << "NEVENTS\t";
2301                cout << "absolute time\t\t\t";
2302                cout << "on-board time";
2303                cout<<endl;
2304            }
2305            for (Int_t ii=0; ii<R->GetEntries(); ii++){
2306                R->GetEntry(ii);
2307                if(DBG){
2308                    cout << ii<< "\t | ";
2309                    cout << R->GetTreeNumber()<< "\t |";
2310    //          cout << R->GetChainOffset()<< "\t |";
2311                    cout <<GetRunInfo()->ID<<"\t";
2312                    cout <<GetRunInfo()->ID_RUN_FRAG<<"\t";
2313                    cout <<GetRunInfo()->NEVENTS<< "\t";
2314                    cout <<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME <<"\t";
2315                    cout <<GetRunInfo()->RUNHEADER_OBT<<" <---> "<<GetRunInfo()->RUNTRAILER_OBT<<"\t";
2316                    cout <<endl;
2317                }
2318                irunoffset[R->GetTreeNumber()]=R->GetChainOffset();
2319            }
2320            cout << "N.run = "<<R->GetEntries()<<endl;
2321            cout << "----------------------------------------------------"<<endl;
2322    
2323    
2324        }else{
2325            delete R;
2326            R=0;
2327        }
2328            
2329    
2330        run_tree = R;
2331    
2332      return R;      return R;
2333            
2334  }  }
# Line 1033  TChain *PamLevel2::GetRunTree(TList *fl) Line 2342  TChain *PamLevel2::GetRunTree(TList *fl)
2342   * @return Pointer to a TTree   * @return Pointer to a TTree
2343   */   */
2344  TTree *PamLevel2::GetRunTree(TFile *f){  TTree *PamLevel2::GetRunTree(TFile *f){
2345        if ( run_tree ){
2346            printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");
2347            return run_tree;
2348        };
2349            
2350    
2351        cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;
2352    
2353      TTree *R = (TTree*)f->Get("Run");      TTree *T = (TTree*)f->Get("Run");
       
     R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));  
     cout << "Run         : set branch address RunInfo"<<endl;  
2354            
2355      return R;      if(T){
2356            T->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
2357            cout << "Run         : set branch address RunInfo"<<endl;
2358            T->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
2359            cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano
2360    
2361        }
2362    
2363        run_tree = (TChain*)T;
2364    
2365        return T;
2366            
2367  }  }
2368    /**
2369     * Update the runinfo informations (to be used to have Run infos event by event basis)
2370     * @param run Pointer to the chain/tree which contains run infos
2371     * @return true if a new run has been read, false if it is still the same run
2372     */
2373    Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev){
2374        //
2375        // check if we have already called once GetEntry, if not call it
2376        //
2377        cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) --- ATTENZIONE --- NON E` MANTENUTA!!!!!!!.... "<<endl;
2378        if(!run){
2379            cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing RunInfo tree "<<endl;
2380            return(false);  
2381        }
2382        if ( run->GetEntries() <= 0 ) return(false);
2383        //
2384      
2385        //  Int_t oldrun = irun;
2386        Long64_t oldrun = irun;
2387    
2388        // --------------------------------------
2389        // if it is a full file (not preselected)
2390        // --------------------------------------
2391        if(SELLI==0){
2392    
2393            //
2394            // the absolute time is necessary to relate the event with the run
2395            //
2396            if( !GetOrbitalInfo() && !ISGP){
2397                cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
2398                return(false);
2399            }
2400    
2401            ULong64_t abstime = 0;
2402            if( GetOrbitalInfo() )abstime=GetOrbitalInfo()->absTime;
2403    
2404    
2405            //
2406            // the first time the routine is called, set run search from the beginning
2407            //
2408            if ( irun < 0LL ){
2409                irun = 0LL;
2410                run->GetEntry(irun);
2411                runfirstentry = 0LL;
2412                runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2413                if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
2414              
2415                if( ISGP && run->GetEntries()!=1 ){
2416                    cout << "** WARNING ** simulated files are assumed to have 1 single run, not "<< run->GetEntries() << endl;
2417                    cout << "** WARNING ** run will not be updated"<<endl;
2418                }
2419    
2420            };      
2421            //
2422            if(ISGP)abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO
2423            //
2424            if ( irun == run->GetEntries()-1LL &&
2425                 !(abstime >= GetRunInfo()->RUNHEADER_TIME &&
2426                   abstime <= GetRunInfo()->RUNTRAILER_TIME)
2427                ){
2428                irun = -1LL;
2429                irunt = -1LL;
2430                runfirstentry = 0LL;
2431                runlastentry = -1LL;
2432            };
2433            // modificato il controllo sull'aggiornamento del run, per evitare problemi
2434            // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)
2435            //
2436            bool fromfirst = true;
2437            //
2438            while ( !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME) && irun < run->GetEntries()-1LL ){
2439    //      while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){
2440                irun++;
2441                run->GetEntry(irun);
2442                runfirstentry = runlastentry;
2443                if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runfirstentry += 1LL;
2444                runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2445    //        cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2446    //        cout << "runfirstentry "<<runfirstentry<<endl;
2447                //    printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));
2448                //    printf(" abstime %u trailertime %u \n",abstime,GetRunInfo()->RUNTRAILER_TIME);
2449                //    printf(" IDRUN %u \n",GetRunInfo()->ID);
2450                //
2451    //        prevshift = 0;
2452                //
2453                if ( irun == (Long64_t)(run->GetEntries()-1LL) && fromfirst && !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME)){
2454                    printf(" resetting irun  (it should NOT happen!!!)\n");
2455                    fromfirst = false;
2456                    irun = 0;
2457                    run->GetEntry(irun);
2458                    runfirstentry = 0ULL;
2459                    runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
2460                    if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
2461                };
2462                //
2463            };
2464            //
2465            if (
2466                !(abstime >= GetRunInfo()->RUNHEADER_TIME &&
2467                  abstime <= GetRunInfo()->RUNTRAILER_TIME)
2468                ) {
2469                printf(" Something very wrong here: cannot find RUN containing absolute time %llu \n",abstime);
2470                return false;
2471            }
2472            //
2473            if ( irun == oldrun || irun >= run->GetEntries() ) return(false);
2474            //
2475            //  printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);
2476            //
2477            prevshift = 0;
2478            cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2479    //      cout << "runfirstentry "<<runfirstentry<<endl;
2480            return(true);    
2481        };
2482        // ----------------------------------------------------
2483        // if it is a preselected file (there is SelectionList)
2484        // NBNB - the event tree MUST be read first
2485        // ----------------------------------------------------
2486        if(SELLI==1){      
2487            sel_tree->GetEntry(iev);
2488    //      cout << irun << " "<< irunentry << endl;
2489            if(irun != oldrun){
2490                run->GetEntry(irun);
2491                cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2492                prevshift = 0;
2493                return true;
2494            }
2495            return false;
2496        }
2497    
2498        return false;
2499        //
2500    };
2501    
2502    Bool_t PamLevel2::UpdateRunInfo(Long64_t iev){
2503    
2504        if(!run_tree){
2505            cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded"<<endl;
2506            return false;
2507        }
2508        if ( run_tree->GetEntries() <= 0 ) {
2509            cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty"<<endl;
2510            return(false);
2511        }
2512      
2513        Int_t oldrun = irun; // store current run index
2514    
2515        // -----------------------------------------------------------------------
2516        // the first time the routine is called, set run search from the beginning
2517        // -----------------------------------------------------------------------
2518    
2519        if ( irun < 0 ){
2520            irun = 0LL;
2521            irunt = 0LL;
2522            irunentry = 0;
2523            prevshift = 0;
2524            run_tree->GetEntry(irun);
2525            if( !GetOrbitalInfo() )cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated "<<endl;
2526        };      
2527        // ---------------------------------------------------------------
2528        // retrieve OBT and absolute time of the event
2529        // ---------------------------------------------------------------
2530        ULong64_t abstime = 0LL;
2531        ULong64_t obt     = 0LL;
2532        if( GetOrbitalInfo() ){
2533            abstime = GetOrbitalInfo()->absTime;
2534            obt     = GetOrbitalInfo()->OBT;
2535    //      cout << " ABS >>> "<<abstime<<" OBT >>> "<<obt<<" "<<endl;
2536        }else{
2537            abstime = GetRunInfo()->RUNHEADER_TIME;
2538            obt     = GetRunInfo()->RUNHEADER_OBT;
2539        }
2540    //    cout << ISGP << " "<<abstime<<endl;
2541        // ---------------------------------------------------------------
2542        // the absolute time is necessary to relate the event with the run
2543        // ---------------------------------------------------------------
2544    //     if( !GetOrbitalInfo() && !ISGP ){
2545    //      cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
2546    //      return(false);
2547    //     }
2548        // -----------------------------------------------------------------------
2549        // if it is simulation, assign abstime by hand (temporaneo!!!)
2550        // -----------------------------------------------------------------------
2551    //     if(ISGP){
2552    //      abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO
2553    //      obt     = GetRunInfo()->RUNHEADER_OBT; // BARBATRUCCO
2554    //     }
2555        
2556    
2557        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2558        // if it is a full file (not preselected)
2559        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2560        if(SELLI==0){
2561    
2562            // ---------------------------------------------------------------
2563            // increment dead and live-time counters
2564            // (only when reading a file not preselected)
2565            // ---------------------------------------------------------------
2566            if(SELLI==0){
2567                if( GetTrigLevel2() ){
2568                    totdltime[0]+=GetTrigLevel2()->dltime[0];
2569                    totdltime[1]+=GetTrigLevel2()->dltime[1];
2570                }
2571                totdltime[2]++;
2572            }
2573    //      cout << setw(10)<<totdltime[0]<<setw(10)<<totdltime[1]<<setw(10)<<totdltime[2]<<endl;
2574    
2575    //      // ---------------------------------------------------------------
2576    //      // retrieve OBT and absolute time of the event
2577    //      // ---------------------------------------------------------------
2578    //      ULong64_t abstime = 0;
2579    //      ULong64_t obt     = 0;
2580    //      if( GetOrbitalInfo() ){
2581    //          abstime = GetOrbitalInfo()->absTime;
2582    //          obt     = GetOrbitalInfo()->OBT;
2583    //      }
2584    //      // ---------------------------------------------------------------
2585    //      // the absolute time is necessary to relate the event with the run
2586    //      // ---------------------------------------------------------------
2587    //      if( !GetOrbitalInfo() && !ISGP ){
2588    //          cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
2589    //          return(false);
2590    //      }
2591          
2592    //      // -----------------------------------------------------------------------
2593    //      // the first time the routine is called, set run search from the beginning
2594    //      // -----------------------------------------------------------------------
2595    //      if ( irun < 0 ){
2596    //          irun = 0LL;
2597    //          irunt = 0LL;
2598    //          irunentry = 0;
2599    //          prevshift = 0;
2600    //          run_tree->GetEntry(irun);
2601    
2602    //          if( ISGP && run_tree->GetEntries()!=1 ){
2603    //              cout << "** WARNING ** simulated files are assumed to have 1 single run, not "<< run_tree->GetEntries() << endl;
2604    //              cout << "** WARNING ** run will not be updated"<<endl;
2605    //          }
2606    //      };      
2607    
2608    //      // -----------------------------------------------------------------------
2609    //      // if it is simulation, assign abstime by hand (temporaneo!!!)
2610    //      // -----------------------------------------------------------------------
2611    //      if(ISGP){
2612    //          abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO
2613    //          obt     = GetRunInfo()->RUNHEADER_OBT; // BARBATRUCCO
2614    //      }
2615            //
2616            bool fromfirst = true; // first loop over runs
2617    
2618    //      Bool_t hasfrag = false;
2619    //      if( GetRunInfo()->ID_RUN_FRAG!=0 && GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID)hasfrag=true;
2620    //      ULong64_t fragid = GetRunInfo()->ID_RUN_FRAG;
2621    
2622            // ------------------------------------------------------
2623            // loop over runs to find the one that contains the event
2624            // ------------------------------------------------------
2625            while (
2626                (
2627                    (
2628                        !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)
2629                          abstime <= GetRunInfo()->RUNTRAILER_TIME) &&
2630                        !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)
2631                          obt <= GetRunInfo()->RUNTRAILER_OBT)
2632                        )
2633                    || GetRunInfo()->NEVENTS==0
2634    //              || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift) // ERRORE!!! fa saltare i run con 1 evento
2635                    || !(irunentry <= GetRunInfo()->NEVENTS-1-prevshift)
2636                    )
2637                && irun < run_tree->GetEntries() ){
2638    
2639                // - - - - - - - - - - - - -
2640                // irunentry = position of current entry, relative to the run
2641                // prevshift = shift needed to synchronize l0 and l2 data (nested events)
2642                // - - - - - - - - - - - - -
2643    
2644                // -----------------------------------------
2645                // store dead and live-time of previous run
2646                // -----------------------------------------
2647    //          if(SELLI==0){
2648                    if(fromfirst){
2649                        if(oldrun==irun){
2650                            /// decrement counters
2651                            if( GetTrigLevel2()){
2652                                totdltime[0]-=GetTrigLevel2()->dltime[0];//live-time
2653                                totdltime[1]-=GetTrigLevel2()->dltime[1];//dead-time
2654                            }
2655                            totdltime[2]--;                              //event counter
2656                            cout << endl;
2657                            cout << "n.events     : "<<totdltime[2]<<endl;
2658                            cout << "RUN LIVE-TIME: "<<totdltime[0]*0.16<<" ms"<<endl;
2659                            cout << "RUN DEAD-TIME: "<<totdltime[1]*0.01<<" ms"<<endl;          
2660                        }else{
2661                            totdltime[0]=0;//live-time
2662                            totdltime[1]=0;//dead-time
2663                            totdltime[2]=0;                             //event counter
2664                            cout << " *** JUMP RUN *** irun "<<irun<<endl;
2665                        }
2666                        /// add an entry
2667                        if(run_tree_clone)
2668                            if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
2669                                run_tree_clone->GetBranch("DeadLiveTime")->Fill();
2670                        /// reset counters
2671                        if( GetTrigLevel2() ){
2672                            totdltime[0]=GetTrigLevel2()->dltime[0];//live-time
2673                            totdltime[1]=0;                         //dead-time
2674                        }
2675                        totdltime[2]=1;                             //event counter
2676                    }
2677    //          }
2678    
2679                irun++;        
2680                // ------------------------------------
2681                // if the end of run tree is reached...
2682                // ------------------------------------
2683                if( irun == run_tree->GetEntries() ){
2684                    if(!fromfirst){
2685                        // -----------------------------------------------------
2686                        // if it happened already once and the run was not found
2687                        // ---> exit with error
2688                        // -----------------------------------------------------
2689                        cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- event entry #"<<iev<<" does not belong to any run (should not happen)" <<endl;
2690                        return false;
2691                    }
2692                    // -----------------------------------------
2693                    // ...otherwise repeat search from beginning
2694                    // -----------------------------------------
2695                    cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- reached end of run tree. searchin again from beginning " <<endl;
2696                    fromfirst = false;
2697                    irun = 0LL;
2698                    runfirstentry = 0LL;
2699                }
2700                // -------------------------------------------------------------------
2701                // save the index of the first entry of the run, relative to pam_tree,
2702                // and read a new run
2703                // -------------------------------------------------------------------
2704                if(irun>0)runfirstentry += (GetRunInfo()->NEVENTS)-prevshift;
2705                irunentry = 0;
2706                prevshift = 0;          
2707                run_tree->GetEntry(irun);
2708                irunt = irun - irunoffset[run_tree->GetTreeNumber()];
2709                if(GetRunInfo()->RUNHEADER_OBT>GetRunInfo()->RUNTRAILER_OBT ){
2710                    cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun "<<irun<<"  has RUNHEADER_OBT>=RUNTRAILER_OBT " <<endl;
2711                    cout << "                                                            (NB!! in this case some events are assigned to a wrong run)"<<endl;
2712                }
2713    //          if(hasfrag &&  fragid != GetRunInfo()->ID){
2714    //              cout << "... where is the next fragment ??"<<endl;
2715    //          }
2716            };
2717    
2718    
2719            // --------------------------------------
2720            // if there was no need to update the run
2721            // ---> exit with FALSE
2722            // --------------------------------------
2723            if ( irun == oldrun ) return(false);
2724    
2725            // --------------------------------------
2726            // ... otherwise
2727            // --------------------------------------
2728    
2729    
2730    
2731    
2732            // --------------------------------------
2733            // ---> exit with TRUE
2734            // --------------------------------------
2735            cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2736            // ----------------------------------------------------
2737            // then check if the run has a fragment
2738            // in this case we have to switch to the next fragment
2739            // when the end of the first fragment is reached
2740            // ----------------------------------------------------
2741            if(
2742                GetRunInfo()->ID_RUN_FRAG != 0 &&
2743    //          GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID &&
2744                true ){
2745                cout << "* fragment *"<<endl;              
2746            }
2747    
2748            return(true);    
2749        };
2750        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2751        // if it is a preselected file (there is SelectionList)
2752        // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2753        // irun  = run entry relative to the chain
2754        // irunt = run entry relative to the tree
2755        if(SELLI==1){      
2756            sel_tree->GetEntry(iev);// read irunt from SelectionList
2757            irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW
2758            if(irun != oldrun ){
2759                if( irun < run_tree->GetEntries() )run_tree->GetEntry(irun);
2760                // check if the time is ok (with merged files it is not...)
2761                // if not loop over run and look for the proper entry
2762                bool SECONDO_GIRO=false;
2763                Long64_t irun_start   = irun;
2764                int      offset_start = irunoffset[sel_tree->GetTreeNumber()];
2765                while (
2766                    (
2767                        (
2768                            !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)
2769                              abstime <= GetRunInfo()->RUNTRAILER_TIME)
2770    //                      ||
2771    //                      !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)
2772    //                        obt <= GetRunInfo()->RUNTRAILER_OBT)
2773                            )
2774                        || GetRunInfo()->NEVENTS==0
2775                        )
2776    //              && irun < run_tree->GetEntries()
2777                    ){
2778    
2779                    if(DBG){
2780                        cout << " (test) ";
2781                        cout << " tree "<<sel_tree->GetTreeNumber();
2782                        cout << " irunt "<<irunt;
2783                        cout << " offset "<<irunoffset[sel_tree->GetTreeNumber()];
2784                        cout << " abs "<<abstime;
2785                        cout <<" >> "<<GetRunInfo()->RUNHEADER_TIME<<" "<<GetRunInfo()->RUNTRAILER_TIME;
2786                        cout << " obt "<<obt;
2787                        cout <<" >> "<<GetRunInfo()->RUNHEADER_OBT<<" "<<GetRunInfo()->RUNTRAILER_OBT;
2788                        cout << " *** JUMP RUN *** irun "<<irun;
2789    //              if(!SECONDO_GIRO)cout << " (don't worry)";
2790    //              else             cout << " (start worring...)";
2791                        cout << endl;
2792                    }
2793    //              irun++;
2794                    irunoffset[sel_tree->GetTreeNumber()]++;
2795                    irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW          
2796                    if(irun == run_tree->GetEntries() && SECONDO_GIRO){
2797    //              if(irun == irun_start ){
2798                        cout << " ...grrrvzzkhhhajsdkj!!!! "<<endl;
2799                        irunoffset[sel_tree->GetTreeNumber()] = offset_start;
2800                        return false;
2801                    }
2802                    if( irun >= run_tree->GetEntries() || irun < 0){
2803                        cout << "irun = "<<irun<<" >>  search from the beginning... <<"<<endl;
2804                        SECONDO_GIRO=true;
2805                        irun=0;
2806                        irunoffset[sel_tree->GetTreeNumber()]=-irunt;
2807                    }
2808                    run_tree->GetEntry(irun);
2809                }
2810    
2811    
2812                if(DBG){
2813                    cout << " (test) ";
2814                    cout << " tree "<<sel_tree->GetTreeNumber();
2815                    cout << " irunt "<<irunt;
2816                    cout << " offset "<<irunoffset[sel_tree->GetTreeNumber()];
2817                    cout << " abs "<<abstime;
2818                    cout <<" >> "<<GetRunInfo()->RUNHEADER_TIME<<" "<<GetRunInfo()->RUNTRAILER_TIME;
2819                    cout << " obt "<<obt;
2820                    cout <<" >> "<<GetRunInfo()->RUNHEADER_OBT<<" "<<GetRunInfo()->RUNTRAILER_OBT;
2821                }
2822                cout << endl;
2823                cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" (run-entry "<<irun<<")"<<endl;
2824    //          cout << endl;
2825                prevshift = 0;
2826                return true;
2827            }
2828            return false;
2829        }
2830    
2831        return false;
2832        //
2833    };
2834    /**
2835     * Update the runinfo informations (to be used to have Run infos event by event basis)
2836     * @param run Pointer to the chain/tree which contains run infos
2837     * @return true if a new run has been read, false if it is still the same run
2838     */
2839    Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev){
2840        return(UpdateRunInfo((TChain*)run,iev));
2841    };
2842    
2843  //--------------------------------------  //--------------------------------------
2844  //  //
2845  //  //
# Line 1050  TTree *PamLevel2::GetRunTree(TFile *f){ Line 2847  TTree *PamLevel2::GetRunTree(TFile *f){
2847  /**  /**
2848   * Set which trees shoul be analysed   * Set which trees shoul be analysed
2849   * @param detlist TString containing the sequence of trees required   * @param detlist TString containing the sequence of trees required
2850  */   */
2851  void PamLevel2::SetWhichTrees(TString detlist){  void PamLevel2::SetWhichTrees(TString detlist){
2852                
2853      if(detlist.Contains("+ALL", TString::kIgnoreCase)){  //    if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){
2854        if( detlist.Contains("+ALL", TString::kIgnoreCase)){
2855    
2856            cout << " ======================================================== "<<endl;
2857            cout << "                       (( WARNING ))                      "<<endl;
2858            cout << " The meaning of the option +ALL has changed!!             "<<endl;
2859            cout << " Now it includes really all (level0+level1+level2+gpamela)"<<endl;
2860            cout << " and the file is discarded if it does not contain         "<<endl;
2861            cout << " all trees or  if level0 files are not available!!        "<<endl;
2862            cout << " ======================================================== "<<endl;
2863                
2864            CAL0 = true;
2865          CAL1 = true;          CAL1 = true;
2866          CAL2 = true;          CAL2 = true;
2867          TRK2 = true;          TRK2 = true;
2868          TRK1 = false;          TRK1 = true;
2869          TRKh = false;          TRKh = true;
2870            TRK0 = true;
2871          TRG = true;          TRG = true;
2872          TOF = true;          TOF = true;
2873            TOF0 = true;
2874          S4  = true;          S4  = true;
2875          ND  = true;          ND  = true;
2876          AC  = true;          AC  = true;
2877          ORB = true;          ORB = true;
2878            GP  = true;
2879      }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){      }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){
2880            CAL0 = false;
2881          CAL1 = false;          CAL1 = false;
2882          CAL2 = false;          CAL2 = false;
2883          TRK2 = false;          TRK2 = false;
2884          TRK1 = false;          TRK1 = false;
2885          TRKh = false;          TRKh = false;
2886            TRK0 = false;
2887          TRG = false;          TRG = false;
2888          TOF = false;          TOF = false;
2889            TOF0 = false;
2890          S4  = false;          S4  = false;
2891          ND  = false;          ND  = false;
2892          AC  = false;          AC  = false;
2893          ORB = false;          ORB = false;
2894            GP  = false;
2895      };      };
2896            
2897  //  -------------------------------------------------------------------------  //  -------------------------------------------------------------------------
# Line 1084  void PamLevel2::SetWhichTrees(TString de Line 2899  void PamLevel2::SetWhichTrees(TString de
2899          if ( detlist.Contains("-CAL1", TString::kIgnoreCase) )CAL1=false;          if ( detlist.Contains("-CAL1", TString::kIgnoreCase) )CAL1=false;
2900          if ( detlist.Contains("+CAL1", TString::kIgnoreCase) )CAL1=true;          if ( detlist.Contains("+CAL1", TString::kIgnoreCase) )CAL1=true;
2901      };      };
2902    
2903        if( detlist.Contains("CAL0", TString::kIgnoreCase) ){
2904            if ( detlist.Contains("-CAL0", TString::kIgnoreCase) )CAL0=false;
2905            if ( detlist.Contains("+CAL0", TString::kIgnoreCase) )CAL0=true;
2906        };
2907                    
2908      if( detlist.Contains("CAL2", TString::kIgnoreCase)){      if( detlist.Contains("CAL2", TString::kIgnoreCase)){
2909          if ( detlist.Contains("-CAL2", TString::kIgnoreCase) )CAL2=false;          if ( detlist.Contains("-CAL2", TString::kIgnoreCase) )CAL2=false;
# Line 1096  void PamLevel2::SetWhichTrees(TString de Line 2916  void PamLevel2::SetWhichTrees(TString de
2916          CAL1=false;          CAL1=false;
2917      }      }
2918  //  -------------------------------------------------------------------------  //  -------------------------------------------------------------------------
2919        if( detlist.Contains("TRK0", TString::kIgnoreCase) ){
2920            if ( detlist.Contains("-TRK0", TString::kIgnoreCase) )TRK0=false;
2921            if ( detlist.Contains("+TRK0", TString::kIgnoreCase) )TRK0=true;
2922        };
2923    
2924      if( detlist.Contains("TRK1", TString::kIgnoreCase) ){      if( detlist.Contains("TRK1", TString::kIgnoreCase) ){
2925          if ( detlist.Contains("-TRK1", TString::kIgnoreCase) )TRK1=false;          if ( detlist.Contains("-TRK1", TString::kIgnoreCase) )TRK1=false;
2926          if ( detlist.Contains("+TRK1", TString::kIgnoreCase) )TRK1=true;          if ( detlist.Contains("+TRK1", TString::kIgnoreCase) )TRK1=true;
# Line 1118  void PamLevel2::SetWhichTrees(TString de Line 2943  void PamLevel2::SetWhichTrees(TString de
2943          TRKh=false;          TRKh=false;
2944      }      }
2945  //  -------------------------------------------------------------------------  //  -------------------------------------------------------------------------
   
   
       
 //     if( detlist.Contains("-TRK", TString::kIgnoreCase) )TRK2 = false;  
 //     else if( detlist.Contains("+TRK", TString::kIgnoreCase) )TRK2 = true;  
       
 //     if( detlist.Contains("-TRK1", TString::kIgnoreCase) )TRK1 = false;  
 //     else if( detlist.Contains("+TRK1", TString::kIgnoreCase) )TRK1 = true;  
   
 //     if( detlist.Contains("-TRKh", TString::kIgnoreCase) )TRKh = false;  
 //     else if( detlist.Contains("+TRKh", TString::kIgnoreCase) )TRKh = true;  
2946            
2947      if( detlist.Contains("-TRG", TString::kIgnoreCase) )TRG = false;      if( detlist.Contains("-TRG", TString::kIgnoreCase) )TRG = false;
2948      else if( detlist.Contains("+TRG", TString::kIgnoreCase) )TRG = true;      else if( detlist.Contains("+TRG", TString::kIgnoreCase) )TRG = true;
2949            
2950      if( detlist.Contains("-TOF", TString::kIgnoreCase) )TOF = false;      if( detlist.Contains("-TOF", TString::kIgnoreCase) )TOF = false;
2951      else if( detlist.Contains("+TOF", TString::kIgnoreCase) )TOF = true;      else if( detlist.Contains("+TOF", TString::kIgnoreCase) )TOF = true;
2952    
2953        if( detlist.Contains("-TOF0", TString::kIgnoreCase) )TOF0 = false;
2954        else if( detlist.Contains("+TOF0", TString::kIgnoreCase) )TOF0 = true;
2955            
2956      if( detlist.Contains("-S4",  TString::kIgnoreCase) )S4  = false;      if( detlist.Contains("-S4",  TString::kIgnoreCase) )S4  = false;
2957      else if( detlist.Contains("+S4",  TString::kIgnoreCase) )S4  = true;      else if( detlist.Contains("+S4",  TString::kIgnoreCase) )S4  = true;
# Line 1147  void PamLevel2::SetWhichTrees(TString de Line 2964  void PamLevel2::SetWhichTrees(TString de
2964            
2965      if( detlist.Contains("-ORB", TString::kIgnoreCase) )ORB = false;      if( detlist.Contains("-ORB", TString::kIgnoreCase) )ORB = false;
2966      else if( detlist.Contains("+ORB", TString::kIgnoreCase) )ORB = true;      else if( detlist.Contains("+ORB", TString::kIgnoreCase) )ORB = true;
2967    
2968        if( detlist.Contains("-GP", TString::kIgnoreCase) )GP = false;
2969        else if( detlist.Contains("+GP", TString::kIgnoreCase) )GP = true;
2970    
2971        cout<< "tree/branch list from input --> ";
2972        if(TRK0)cout<<"TRK0 ";
2973        if(TRK1)cout<<"TRK1 ";
2974        if(TRK2)cout<<"TRK2 ";
2975        if(TRKh)cout<<"TRKH ";
2976        if(CAL0)cout<<"CAL0 ";
2977        if(CAL1)cout<<"CAL1 ";
2978        if(CAL2)cout<<"CAL2 ";
2979        if(TOF)cout<<"TOF ";
2980        if(TRG)cout<<"TRG ";
2981        if(AC)cout<<"AC ";
2982        if(ND)cout<<"ND ";
2983        if(S4)cout<<"S4 ";
2984        if(ORB)cout<<"ORB ";
2985        if(GP)cout<<"GP ";
2986        cout << endl;
2987    //     cout<< "Set detector list --> ";
2988    //     if(TRK1)cout<<"TRK1 ";
2989    //     if(TRK2)cout<<"TRK2 ";
2990    //     if(TRKh)cout<<"TRKH ";
2991    //     if(CAL1)cout<<"CAL1 ";
2992    //     if(CAL2)cout<<"CAL2 ";
2993    //     if(TOF0)cout<<"TOF0 ";
2994    //     if(TOF)cout<<"TOF ";
2995    //     if(TRG)cout<<"TRG ";
2996    //     if(AC)cout<<"AC ";
2997    //     if(ND)cout<<"ND ";
2998    //     if(S4)cout<<"S4 ";
2999    //     if(ORB)cout<<"ORB ";
3000    //     cout << endl;
3001        
3002    };
3003    
3004    
3005    /**
3006     * Set tree/branch detector flags from the content of a tree
3007     */
3008    void  PamLevel2::GetWhichTrees(TFile* f){
3009        
3010    
3011    
3012    //    cout << "void  PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;
3013        // -----------
3014        // reset flags
3015        // -----------
3016        CAL1   = false;    
3017        CAL2   = false;    
3018        TRK2   = false;    
3019        TRK1   = false;    
3020        TRKh   = false;    
3021        TRG    = false;    
3022        TOF    = false;    
3023        S4     = false;    
3024        ND     = false;    
3025        AC     = false;    
3026        ORB    = false;    
3027        GP     = false;    
3028        
3029        RUN    = false;
3030            
3031    //    cout << "Checking file: "<<f->GetName()<<endl;
3032        if( !f || f->IsZombie() ){
3033            cout << "File: "<< f->GetName() <<" Non valid root file"<< endl;
3034            return;
3035        }
3036    
3037        TList *lk = f->GetListOfKeys();
3038        TIter next(lk);
3039        TKey *key =0;
3040    
3041        Int_t nev = 0;
3042    
3043        while( (key = (TKey*)next()) ){
3044            
3045            if( !strcmp(key->GetName(),"Run"        ) )RUN = true;
3046    
3047            //=========================================================    
3048            if( !strcmp(key->GetName(),"Trigger"    ) ){
3049                TRG = true;
3050                Int_t nevt = ((TTree*)f->Get("Trigger"))->GetEntries();
3051                if( nev && nevt!=nev){
3052                    cout << "File: "<< f->GetName() <<" Trigger tree has "<<nevt<<" events instead of "<<nev<< endl;
3053                    TRG = false;
3054                }else nev=nevt;
3055            }
3056            //=========================================================    
3057            if( !strcmp(key->GetName(),"ToF"        ) ){
3058                TOF = true;
3059                Int_t nevt = ((TTree*)f->Get("ToF"))->GetEntries();
3060                if( nev && nevt!=nev){
3061                    cout << "File: "<< f->GetName() <<"     ToF tree has "<<nevt<<" events instead of "<<nev<< endl;
3062                    TOF = false;
3063                }else nev=nevt;
3064            }
3065            //=========================================================  
3066            if( !strcmp(key->GetName(),"S4"         ) ){
3067                S4 = true;
3068                Int_t nevt = ((TTree*)f->Get("S4"))->GetEntries();
3069                if( nev && nevt!=nev){
3070                    cout << "File: "<< f->GetName() <<"      S4 tree has "<<nevt<<" events instead of "<<nev<< endl;
3071                    S4 = false;
3072                }else nev=nevt;
3073            }
3074            //=========================================================  
3075    
3076            if( !strcmp(key->GetName(),"NeutronD"   ) ){
3077                ND = true;
3078                Int_t nevt = ((TTree*)f->Get("NeutronD"))->GetEntries();
3079                if( nev && nevt!=nev){
3080                    cout << "File: "<< f->GetName() <<"NeutronD tree has "<<nevt<<" events instead of "<<nev<< endl;
3081                    ND =false;
3082                }else nev=nevt;
3083            }      
3084            //=========================================================  
3085            if( !strcmp(key->GetName(),"Anticounter") ){
3086                AC = true;
3087                Int_t nevt = ((TTree*)f->Get("Anticounter"))->GetEntries();
3088                if( nev && nevt!=nev){
3089                    cout << "File: "<< f->GetName() <<" Anticounter tree has "<<nevt<<" events instead of "<<nev<< endl;
3090                    AC =false;
3091                }else nev=nevt;
3092            }
3093            //=========================================================  
3094            if( !strcmp(key->GetName(),"OrbitalInfo") ){
3095                ORB = true;
3096                Int_t nevt = ((TTree*)f->Get("OrbitalInfo"))->GetEntries();
3097                if( nev && nevt!=nev){
3098                    cout << "File: "<< f->GetName() <<" OrbitalInfo tree has "<<nevt<<" events instead of "<<nev<< endl;
3099                    ORB = false;
3100                }else nev=nevt;
3101            }
3102            //=========================================================  
3103            if( !strcmp(key->GetName(),"Tracker"    ) ){
3104                TTree *T = (TTree*)f->Get("Tracker");
3105                for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
3106                    TString name = T->GetListOfBranches()->At(i)->GetName();
3107                    if( !name.CompareTo("TrkLevel1") )TRK1=true;
3108                    if( !name.CompareTo("TrkLevel2") )TRK2=true;
3109                    if( !name.CompareTo("TrkHough") )TRKh=true;
3110                };      
3111                Int_t nevt = T->GetEntries();
3112                if( nev && nevt!=nev){
3113                    cout << "File: "<< f->GetName() <<" Tracker tree has "<<nevt<<" events instead of "<<nev<< endl;
3114                    TRK1 = false;
3115                    TRK2 = false;
3116                    TRKh = false;
3117                }else nev=nevt;
3118    //          T->Delete();
3119            };
3120            //=========================================================  
3121            if( !strcmp(key->GetName(),"Calorimeter"    ) ){
3122                TTree *T = (TTree*)f->Get("Calorimeter");
3123                for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
3124                    TString name = T->GetListOfBranches()->At(i)->GetName();
3125                    if( !name.CompareTo("CaloLevel1") )CAL1=true;
3126                    if( !name.CompareTo("CaloLevel2") )CAL2=true;
3127                };    
3128                Int_t nevt = T->GetEntries();
3129                if( nev && nevt!=nev){
3130                    cout << "File: "<< f->GetName() <<"  Calorimeter tree has "<<nevt<<" events instead of "<<nev<< endl;
3131                    CAL1 = false;
3132                    CAL2 = false;
3133                }else nev=nevt;
3134    //          T->Delete();
3135            };      
3136            //=========================================================  
3137            if( !strcmp(key->GetName(),"h20") ){
3138                GP = true;
3139                Int_t nevt = ((TTree*)f->Get("h20"))->GetEntries();
3140                if( nev && nevt!=nev){
3141                    cout << "File: "<< f->GetName() <<" h20 tree has "<<nevt<<" events instead of "<<nev<< endl;
3142                    GP = false;
3143                }else nev=nevt;
3144            }
3145    
3146        };
3147            
3148    //    delete lk;
3149    
3150        cout<< "tree/branch list from file  --> ";
3151        if(TRK1)cout<<"TRK1 ";
3152        if(TRK2)cout<<"TRK2 ";
3153        if(TRKh)cout<<"TRKH ";
3154        if(CAL1)cout<<"CAL1 ";
3155        if(CAL2)cout<<"CAL2 ";
3156        if(TOF)cout<<"TOF ";
3157        if(TRG)cout<<"TRG ";
3158        if(AC)cout<<"AC ";
3159        if(ND)cout<<"ND ";
3160        if(S4)cout<<"S4 ";
3161        if(ORB)cout<<"ORB ";
3162        if(GP)cout<<"GP ";
3163        cout << endl;
3164          
3165        return ;
3166            
3167  };  };
3168    
3169    
3170  //--------------------------------------  //--------------------------------------
3171  //  //
3172  //  //
# Line 1171  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3189  Bool_t  PamLevel2::CheckLevel2File(TStri
3189      Bool_t ND__ok     = false;          Bool_t ND__ok     = false;    
3190      Bool_t AC__ok     = false;          Bool_t AC__ok     = false;    
3191      Bool_t ORB__ok    = false;          Bool_t ORB__ok    = false;    
3192        Bool_t GP__ok     = false;    
3193            
3194      Bool_t RUN__ok    = false;      Bool_t RUN__ok    = false;
3195            
3196            Bool_t SELLI__ok  = false;
3197    
3198      cout << "Checking file: "<<name<<endl;  //    cout << "Checking file: "<<name<<endl;
3199      TFile *f = new TFile(name.Data());      TFile *f = new TFile(name.Data());
3200      if( !f || f->IsZombie() ){      if( !f || f->IsZombie() ){
3201          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 1192  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3211  Bool_t  PamLevel2::CheckLevel2File(TStri
3211      while( (key = (TKey*)next()) ){      while( (key = (TKey*)next()) ){
3212                    
3213  //      cout << key->GetName() << endl;  //      cout << key->GetName() << endl;
3214  //      cout << " Get tree: " << f->Get(key->GetName())<<endl;  //      cout << key->GetName() << ""<<key->GetClassName()<<endl;
3215    //              cout << " Get tree: " << f->Get(key->GetName())<<endl;
3216  //      nev_previous = nev;  //      nev_previous = nev;
3217  //      cout << " n.entries  "<< nev <<endl;  //      cout << " n.entries  "<< nev <<endl;
3218  //      if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){  //      if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){
# Line 1201  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3221  Bool_t  PamLevel2::CheckLevel2File(TStri
3221  //          return false;  //          return false;
3222  //      };  //      };
3223    
3224            //=========================================================    
3225            // check if the file
3226            
3227    
3228          if( !strcmp(key->GetName(),"Run"        ) )RUN__ok = true;                if( !strcmp(key->GetName(),"Run"        ) )RUN__ok = true;      
3229    
3230          //=========================================================              //=========================================================    
3231            if( !strcmp(key->GetName(),"SelectionList"    ) ){
3232                SELLI__ok = true;
3233                if(SELLI==1){
3234                    Int_t nevt = ((TTree*)f->Get("SelectionList"))->GetEntries();
3235                    if( nev && nevt!=nev){
3236                        cout << "File: "<< f->GetName() <<" discarded ---- SelectionList tree has "<<nevt<<" events instead of "<<nev<< endl;
3237                        return false;
3238                    }
3239                    nev=nevt;
3240                }
3241            }
3242    
3243            //=========================================================    
3244          if( !strcmp(key->GetName(),"Trigger"    ) ){          if( !strcmp(key->GetName(),"Trigger"    ) ){
3245              TRG__ok = true;              TRG__ok = true;
3246              if(TRG){              if(TRG){
# Line 1313  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3350  Bool_t  PamLevel2::CheckLevel2File(TStri
3350              };                  };    
3351              T->Delete();              T->Delete();
3352          };                };      
3353            //=========================================================    
3354            if( !strcmp(key->GetName(),"h20") ){
3355                ISGP = true;    
3356                GP__ok = true;
3357                if(GP){
3358                    Int_t nevt = ((TTree*)f->Get("h20"))->GetEntries();
3359                    if( nev && nevt!=nev){
3360                        cout << "File: "<< f->GetName() <<" discarded ---- OrbitalInfo tree has "<<nevt<<" events instead of "<<nev<< endl;
3361                        return false;
3362                    }
3363                    nev=nevt;
3364                }
3365            }
3366    
3367      };      };
3368    
3369        if( SELLI==-1 )SELLI = (Int_t)SELLI__ok;
3370        if( SELLI==0 && SELLI__ok ){
3371            cout << "File: "<< f->GetName() <<" discarded ---- found SelectionList (it is not a full-event file)" << endl;
3372            return false;  
3373        }
3374        if( SELLI==1 && !SELLI__ok ){
3375            cout << "File: "<< f->GetName() <<" discarded ---- SelectionList missing" << endl;
3376            return false;
3377        }
3378            
3379    //    cout << "SELLI "<<SELLI<<endl;
3380    
3381    //     cout<< "CheckLevel2File(TString): detector list --> ";
3382    //     if(TRK1__ok)cout<<"TRK1 ";
3383    //     if(TRK2__ok)cout<<"TRK2 ";
3384    //     if(TRKh__ok)cout<<"TRKH ";
3385    //     if(CAL1__ok)cout<<"CAL1 ";
3386    //     if(CAL2__ok)cout<<"CAL2 ";
3387    //     if(TOF__ok)cout<<"TOF ";
3388    //     if(TRG__ok)cout<<"TRG ";
3389    //     if(AC__ok)cout<<"AC ";
3390    //     if(ND__ok)cout<<"ND ";
3391    //     if(S4__ok)cout<<"S4 ";
3392    //     if(ORB__ok)cout<<"ORB ";
3393    //     cout << endl;
3394    
3395    
3396        if(TRK2 && TRK1__ok)TRK1=1;
3397    // ----------------------------------------------------------------------------
3398    // NOTA
3399    // se c'e` il level1, lo devo necessarimente leggere.
3400    // infatti (non ho capito perche`) i cluster vengono letti e allocati in memoria
3401    // comunque, ma non vengono disallocati da PamLevel2::Clear()
3402    // ----------------------------------------------------------------------------
3403    
3404    
3405      if(!RUN__ok) {      if(!RUN__ok) {
3406          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;
3407  //      return false;          RUN = false;
3408      };      };
3409    
3410      if(CAL1 && !CAL1__ok){      if(CAL1 && !CAL1__ok){
# Line 1360  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3444  Bool_t  PamLevel2::CheckLevel2File(TStri
3444          return false;          return false;
3445      };      };
3446    
3447          if(ND && !ND__ok){      if(ND && !ND__ok){
3448              cout << "File: "<< f->GetName() <<" discarded ---- Missing ND tree"<< endl;          cout << "File: "<< f->GetName() <<" discarded ---- Missing ND tree"<< endl;
3449              return false;          return false;
3450          };      };
3451          if(TRG && !TRG__ok){      if(TRG && !TRG__ok){
3452              cout << "File: "<< f->GetName() <<" discarded ---- Missing Trigger tree"<< endl;          cout << "File: "<< f->GetName() <<" discarded ---- Missing Trigger tree"<< endl;
3453              return false;          return false;
3454          };      };
3455        if(GP && !GP__ok){
3456            cout << "File: "<< f->GetName() <<" discarded ---- Missing h20 tree"<< endl;
3457            return false;
3458        };
3459        
3460    
3461      lk->Delete();  //    lk->Delete();
3462    //    delete lk;
3463      f->Close();      f->Close();
3464    
3465    //     cout<< "CheckLevel2File(TString): detector list --> ";
3466    //     if(TRK1)cout<<"TRK1 ";
3467    //     if(TRK2)cout<<"TRK2 ";
3468    //     if(TRKh)cout<<"TRKH ";
3469    //     if(CAL1)cout<<"CAL1 ";
3470    //     if(CAL2)cout<<"CAL2 ";
3471    //     if(TOF)cout<<"TOF ";
3472    //     if(TRG)cout<<"TRG ";
3473    //     if(AC)cout<<"AC ";
3474    //     if(ND)cout<<"ND ";
3475    //     if(S4)cout<<"S4 ";
3476    //     if(ORB)cout<<"ORB ";
3477    //     if(GP)cout<<"GP ";
3478    //     cout << endl;
3479                
3480      return true;      return true;
3481                    
# Line 1384  Bool_t  PamLevel2::CheckLevel2File(TStri Line 3485  Bool_t  PamLevel2::CheckLevel2File(TStri
3485  /**  /**
3486   * Create clone-trees   * Create clone-trees
3487   */   */
3488  void PamLevel2::CreateCloneTrees( TChain *fChain ){  void PamLevel2::CreateCloneTrees0( TChain *fChain, TFile *ofile ){
3489    
3490      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
3491      cout << "Create clones of PAMELA trees "<<endl;      cout << "Create clones of PAMELA trees "<<endl;
3492            
3493      Int_t i=0;      Int_t i=0;
3494      tree_clone[i] = fChain->GetTree()->CloneTree(0);      pam_tree_clone[i] = fChain->GetTree()->CloneTree(0);
3495      TString name =  tree_clone[i]->GetName();      TString name =  pam_tree_clone[i]->GetName();
3496      name.Append("_clone");      name.Append("_clone");
3497  //    tree_clone[i]->SetName(name.Data());  //    pam_tree_clone[i]->SetName(name.Data());
3498      cout << tree_clone[i]->GetName() <<endl;      cout << pam_tree_clone[i]->GetName() <<endl;
3499      i++;      i++;
3500    
3501      TList *li = fChain->GetListOfFriends();      TList *li = fChain->GetListOfFriends();
3502      TIter next(li);      TIter next(li);
3503      TFriendElement* T_friend=0;      TFriendElement* T_friend=0;
3504        ofile->cd();
3505      while( (T_friend = (TFriendElement*)next()) ){      while( (T_friend = (TFriendElement*)next()) ){
3506  //      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;
3507  //      cout<<T_friend->GetTree()->GetName()<< endl;  //      cout<<T_friend->GetTree()->GetName()<< endl;
3508          tree_clone[i] = T_friend->GetTree()->CloneTree(0);                pam_tree_clone[i] = T_friend->GetTree()->CloneTree(0);
3509          name =  tree_clone[i]->GetName();          pam_tree_clone[i]->SetAutoSave(1000000);
3510            name =  pam_tree_clone[i]->GetName();
3511          name.Append("_clone");          name.Append("_clone");
3512  //      tree_clone[i]->SetName(name.Data());  //      pam_tree_clone[i]->SetName(name.Data());
3513          cout << tree_clone[i]->GetName() << endl;          cout << pam_tree_clone[i]->GetName() << endl;
3514          i++;          i++;
3515      }      }
3516        
3517        delete li;
3518    
3519      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
3520    
3521  }  }
3522    
3523  // /**  /**
3524  //  * Create clone-trees   * Create clone-trees
3525  //  */   */
3526  // void PamLevel2::CreateCloneTrees(){  void PamLevel2::CreateCloneTrees(TFile *ofile){
3527    
3528    
3529    //  if the pointer is null, create a default file
3530        if(!run_tree)return;
3531    
3532        if(!ofile){
3533            cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root "<<endl;
3534            ofile = new TFile("clone-tree.root","recreate");
3535        }
3536    
3537        ofile->cd();
3538    
3539  //     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
3540  //     cout << "Create new PAMELA trees "<<endl;      cout << "Create new PAMELA trees "<<endl;
3541                    
 //     if(TRK1||TRK2||TRKh){  
 //      T_clone = new TTree("Tracker_clone","PAMELA tracker level2 data ");      
 //      if(TRK1) {  
 //          trk1_clone = new TrkLevel1();  
 //          T_clone->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));  
 //          T_clone->BranchRef();  
 //          cout << "Tracker      : branch TrkLevel1"<<endl;  
 //      };  
 //      if(TRK2) {  
 //          trk2_clone = new TrkLevel2();  
 //          T_clone->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));  
 //          cout << "Tracker      : branch TrkLevel2"<<endl;          
 //      };  
 //      if(TRKh) {  
 //          trkh_clone = new TrkHough();  
 //          T_clone->Branch("TrkHough","TrkHough", GetPointerTo("TrkHough"));  
 //          cout << "Tracker      : branch TrkHough"<<endl;  
 //      };  
 //      cout << hex << T_clone << dec << endl;  
 //     }  
3542    
3543  //     // Calorimeter      run_tree_clone = new TTree("Run","PAMELA Level2 data from the GL_RUN table ");
3544  //     if(CAL1||CAL2){      run_tree_clone->Branch("RunInfo","GL_RUN",GetPointerTo("RunInfo"));
3545  //      C_clone = new TTree("Calorimeter_clone","PAMELA calorimeter level2 data ");          cout << "Run          : branch RunInfo"<<endl;
3546  //      if(CAL1) {      run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));
3547  //          calo1_clone = new CaloLevel1();      cout << "Run          : branch SoftInfo"<<endl;
3548  //          C_clone->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));      // ------------------
3549  //          cout << "Calorimeter  : branch CaloLevel1"<<endl;      // replicate run tree
3550  //      };      // ------------------
3551  //      if(CAL2) {  //    cout << "----------------------------------------------------"<<endl;
3552  //          calo2_clone = new CaloLevel2();  //    cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
3553  //          C_clone->Branch("CaloLevel2","CaloLevel2", GetPointerTo("CaloLevel2"));      for (Int_t i=0; i<run_tree->GetEntries(); i++){
3554  //          cout << "Calorimeter  : branch CaloLevel2"<<endl;          run_tree->GetEntry(i);
3555  //      };  //      cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
3556  //     }              run_tree_clone->Fill();
3557        }
3558        cout << "----------------------------------------------------"<<endl;
3559    
3560  //     // ToF          // ------------------------------------
3561  //     if(TOF) {      // add branch with dead and live times
3562  //      O_clone = new TTree("ToF_clone","PAMELA ToF level2 data ");          // ------------------------------------
3563  //      tof_clone = new ToFLevel2();      run_tree_clone->Branch("DeadLiveTime",totdltime,"dltime[3]/l");
3564  //      O_clone->Branch("ToFLevel2","ToFLevel2", GetPointerTo("ToFLevel2"));      cout << "Run          : branch DeadLiveTime"<<endl;
 //      cout << "ToF          : branch ToFLevel2"<<endl;  
 //     };  
 //     // Trigger  
 //     if(TRG) {  
 //      R_clone = new TTree("Trigger_clone","PAMELA trigger level2 data ");      
 //      trig_clone = new TrigLevel2();  
 //      R_clone->Branch("TrigLevel2","TrigLevel2", GetPointerTo("TrigLevel2"));  
 //      cout << "Trigger      : branch TrigLevel2"<<endl;  
 //     };  
 //     // S4  
 //     if(S4) {  
 //      S_clone = new TTree("S4_clone","PAMELA S4 level2 data ");        
 //      s4_clone = new S4Level2();  
 //      S_clone->Branch("S4Level2","S4Level2", GetPointerTo("S4Level2"));  
 //      cout << "S4           : branch S4Level2"<<endl;  
 //     };  
 //     // Neutron Detector  
 //     if(ND) {  
 //      N_clone = new TTree("NeutronD_clone","PAMELA neutron detector level2 data ");    
 //      nd_clone = new NDLevel2();  
 //      N_clone->Branch("NDLevel2","NDLevel2", GetPointerTo("NDLevel2"));  
 //      cout << "NeutronD     : branch NDLevel2"<<endl;  
 //     };  
 //     // Anticounters  
 //     if(AC) {  
 //      A_clone = new TTree("Anticounter_clone","PAMELA anticounter detector level2 data ");      
 //      ac_clone = new AcLevel2();  
 //      A_clone->Branch("AcLevel2","AcLevel2", GetPointerTo("AcLevel2"));  
 //      cout << "Anticounter  : branch AcLevel2"<<endl;  
 //     };  
 //     // OrbitalInfo  
 //     if(ORB) {  
 //      B_clone = new TTree("OrbitalInfo_clone","PAMELA oribital info  ");        
 //      orb_clone = new OrbitalInfo();  
 //      B_clone->Branch("OrbitalInfo","OrbitalInfo", GetPointerTo("OrbitalInfo"));  
 //      cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;  
 //     };  
 //     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;  
3565    
 // }  
3566    
3567  // /**      sel_tree_clone = new TTree("SelectionList","List of selected events ");
3568  //  * Create clone-trees  //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
3569  //  */      sel_tree_clone->Branch("RunEntry",&irunt,"runentry/L");//NEWNEW
3570  // void PamLevel2::CreateCloneTrees(){      sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/L");
3571        
3572    
3573  //     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      Int_t i=0;
3574  //     cout << "Create new PAMELA trees "<<endl;      if(TRK1||TRK2||TRKh){
3575                    pam_tree_clone[i] = new TTree("Tracker","PAMELA tracker level2 data ");
3576  //     if(TRK1||TRK2||TRKh){          if(TRK1) {
3577  //      T_clone = new TTree("Tracker_clone","PAMELA tracker level2 data ");                  pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));
3578  //      if(TRK1) {              pam_tree_clone[i]->BranchRef();
3579  //          trk1_clone = new TrkLevel1();              cout << "Tracker      : branch TrkLevel1"<<endl;
3580  //          T_clone->Branch("TrkLevel1","TrkLevel1", &trk1_clone);  //          cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;
3581  //          T_clone->BranchRef();          };
3582  //          cout << "Tracker      : branch TrkLevel1"<<endl;          if(TRK2) {
3583  //      };              pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));
3584  //      if(TRK2) {              cout << "Tracker      : branch TrkLevel2"<<endl;        
3585  //          trk2_clone = new TrkLevel2();          };
3586  //          T_clone->Branch("TrkLevel2", "TrkLevel2",&trk2_clone);          if(TRKh) {
3587  //          cout << "Tracker      : branch TrkLevel2"<<endl;                      pam_tree_clone[i]->Branch("TrkHough","TrkHough", GetPointerTo("TrkHough"));
3588  //      };              cout << "Tracker      : branch TrkHough"<<endl;
3589  //      if(TRKh) {          };
3590  //          trkh_clone = new TrkHough();          i++;
3591  //          T_clone->Branch("TrkHough","TrkHough", &trkh_clone);      }
 //          cout << "Tracker      : branch TrkHough"<<endl;  
 //      };  
 //      cout << hex << T_clone << dec << endl;  
 //     }  
3592    
3593  //     // Calorimeter      // Calorimeter
3594  //     if(CAL1||CAL2){      if(CAL1||CAL2){
3595  //      C_clone = new TTree("Calorimeter_clone","PAMELA calorimeter level2 data ");              pam_tree_clone[i] = new TTree("Calorimeter","PAMELA calorimeter level2 data ");
3596  //      if(CAL1) {          if(CAL1) {
3597  //          calo1_clone = new CaloLevel1();              pam_tree_clone[i]->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));
3598  //          C_clone->Branch("CaloLevel1", "CaloLevel1", &calo1_clone);              cout << "Calorimeter  : branch CaloLevel1"<<endl;
3599  //          cout << "Calorimeter  : branch CaloLevel1"<<endl;          };
3600  //      };          if(CAL2) {
3601  //      if(CAL2) {              pam_tree_clone[i]->Branch("CaloLevel2","CaloLevel2", GetPointerTo("CaloLevel2"));
3602  //          calo2_clone = new CaloLevel2();              cout << "Calorimeter  : branch CaloLevel2"<<endl;
3603  //          C_clone->Branch("CaloLevel2","CaloLevel2", &calo2_clone);          };
3604  //          cout << "Calorimeter  : branch CaloLevel2"<<endl;          i++;
3605  //      };      }    
 //     }      
3606    
3607  //     // ToF          // ToF    
3608  //     if(TOF) {      if(TOF) {
3609  //      O_clone = new TTree("ToF_clone","PAMELA ToF level2 data ");              pam_tree_clone[i] = new TTree("ToF","PAMELA ToF level2 data ");
3610  //      tof_clone = new ToFLevel2();          pam_tree_clone[i]->Branch("ToFLevel2","ToFLevel2", GetPointerTo("ToFLevel2"));
3611  //      O_clone->Branch("ToFLevel2","ToFLevel2", &tof_clone);          cout << "ToF          : branch ToFLevel2"<<endl;
3612  //      cout << "ToF          : branch ToFLevel2"<<endl;          i++;
3613  //     };      };
3614  //     // Trigger      // Trigger
3615  //     if(TRG) {      if(TRG) {
3616  //      R_clone = new TTree("Trigger_clone","PAMELA trigger level2 data ");              pam_tree_clone[i] = new TTree("Trigger","PAMELA trigger level2 data ");
3617  //      trig_clone = new TrigLevel2();          pam_tree_clone[i]->Branch("TrigLevel2","TrigLevel2", GetPointerTo("TrigLevel2"));
3618  //      R_clone->Branch("TrigLevel2","TrigLevel2", &trig_clone);          cout << "Trigger      : branch TrigLevel2"<<endl;
3619  //      cout << "Trigger      : branch TrigLevel2"<<endl;          i++;
3620  //     };      };
3621  //     // S4      // S4
3622  //     if(S4) {      if(S4) {
3623  //      S_clone = new TTree("S4_clone","PAMELA S4 level2 data ");                pam_tree_clone[i] = new TTree("S4","PAMELA S4 level2 data ");  
3624  //      s4_clone = new S4Level2();          pam_tree_clone[i]->Branch("S4Level2","S4Level2", GetPointerTo("S4Level2"));
3625  //      S_clone->Branch("S4Level2","S4Level2", &s4_clone);          cout << "S4           : branch S4Level2"<<endl;
3626  //      cout << "S4           : branch S4Level2"<<endl;          i++;
3627  //     };      };
3628  //     // Neutron Detector      // Neutron Detector
3629  //     if(ND) {      if(ND) {
3630  //      N_clone = new TTree("NeutronD_clone","PAMELA neutron detector level2 data ");            pam_tree_clone[i] = new TTree("NeutronD","PAMELA neutron detector level2 data ");      
3631  //      nd_clone = new NDLevel2();          pam_tree_clone[i]->Branch("NDLevel2","NDLevel2", GetPointerTo("NDLevel2"));
3632  //      N_clone->Branch("NDLevel2","NDLevel2", &nd_clone);          cout << "NeutronD     : branch NDLevel2"<<endl;
3633  //      cout << "NeutronD     : branch NDLevel2"<<endl;          i++;
3634  //     };      };
3635  //     // Anticounters      // Anticounters
3636  //     if(AC) {      if(AC) {
3637  //      A_clone = new TTree("Anticounter_clone","PAMELA anticounter detector level2 data ");              pam_tree_clone[i] = new TTree("Anticounter","PAMELA anticounter detector level2 data ");        
3638  //      ac_clone = new AcLevel2();          pam_tree_clone[i]->Branch("AcLevel2","AcLevel2", GetPointerTo("AcLevel2"));
3639  //      A_clone->Branch("AcLevel2","AcLevel2", &ac_clone);          cout << "Anticounter  : branch AcLevel2"<<endl;
3640  //      cout << "Anticounter  : branch AcLevel2"<<endl;          i++;
3641  //     };      };
3642  //     // OrbitalInfo      // OrbitalInfo
3643  //     if(ORB) {      if(ORB) {
3644  //      B_clone = new TTree("OrbitalInfo_clone","PAMELA oribital info  ");                pam_tree_clone[i] = new TTree("OrbitalInfo","PAMELA oribital info  ");  
3645  //      orb_clone = new OrbitalInfo();          pam_tree_clone[i]->Branch("OrbitalInfo","OrbitalInfo", GetPointerTo("OrbitalInfo"));
3646  //      B_clone->Branch("OrbitalInfo","OrbitalInfo", &orb_clone);          cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;
3647  //      cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;          i++;
3648  //     };      };
3649  //     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      // GPamela
3650        if(GP) {
3651            pam_tree_clone[i] = new TTree("h20","PAMELA oribital info  ");  
3652            pam_tree_clone[i]->Branch("GPamela","GPamela", GetPointerTo("GPamela"),32000,1);//split
3653            cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;
3654            i++;
3655        };
3656        cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
3657    
3658    }
3659    
 // }  
3660    
3661  /**  /**
3662   * Fill tree (created with CreateCloneTrees)   * Fill tree (created with CreateCloneTrees)
# Line 1601  void PamLevel2::CreateCloneTrees( TChain Line 3664  void PamLevel2::CreateCloneTrees( TChain
3664   */   */
3665  //void PamLevel2::FillNewPamTree(TTree *T){  //void PamLevel2::FillNewPamTree(TTree *T){
3666  void PamLevel2::FillCloneTrees(){  void PamLevel2::FillCloneTrees(){
3667                
3668  // //  --------------------------------------  //    cout << "PamLevel2::FillCloneTrees()" << irunentry << endl;
 // //  copy the event  
 // //  --------------------------------------  
 //     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;  
   
 //     if(T_clone)T_clone->Fill();  
 //     if(C_clone)C_clone->Fill();  
 //     if(O_clone)O_clone->Fill();  
 //     if(R_clone)R_clone->Fill();  
 //     if(S_clone)S_clone->Fill();  
 //     if(N_clone)N_clone->Fill();  
 //     if(A_clone)A_clone->Fill();  
 //     if(O_clone)O_clone->Fill();  
3669    
3670      for(Int_t i=0; i<8; i++){      for(Int_t i=0; i<NCLONES; i++){
3671          if(tree_clone[i])tree_clone[i]->Fill();          if(pam_tree_clone[i])pam_tree_clone[i]->Fill();
3672      }      }
3673        if(sel_tree_clone)sel_tree_clone->Fill();    
3674    
3675  }  }
3676    
3677    
3678  TTree* PamLevel2::GetCloneTree(TString name){  TTree* PamLevel2::GetCloneTree(TString name){
3679    
3680      for(Int_t i=0; i<8; i++){        for(Int_t i=0; i<NCLONES; i++){    
3681          if(tree_clone[i]){          if(pam_tree_clone[i]){
3682              TString na = tree_clone[i]->GetName();              TString na = pam_tree_clone[i]->GetName();
3683              if(!name.CompareTo(na))return tree_clone[i];              if(!name.CompareTo(na))return pam_tree_clone[i];
3684          };          };
3685      }      }
3686        if(run_tree_clone){
3687            TString na = run_tree_clone->GetName();
3688            if(!name.CompareTo(na))return run_tree_clone;
3689        }
3690        if(sel_tree_clone){
3691            TString na = sel_tree_clone->GetName();
3692            if(!name.CompareTo(na))return sel_tree_clone;
3693        }
3694      return NULL;      return NULL;
3695    
3696  }  }
3697  void PamLevel2::WriteCloneTrees(){  void PamLevel2::WriteCloneTrees(){
3698      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
3699      cout << "Write clones of PAMELA trees "<<endl;      cout << "Write clones of PAMELA trees "<<endl;
3700      for(Int_t i=0; i<8; i++){        cout << run_tree_clone->GetName()<<endl;  
3701          if(tree_clone[i]){      if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
3702              cout << tree_clone[i]->GetName()<<endl;          run_tree_clone->GetBranch("DeadLiveTime")->Fill();
3703              tree_clone[i]->Write();      run_tree_clone->Write();
3704        cout << sel_tree_clone->GetName()<<endl;    
3705        sel_tree_clone->Write();
3706        for(Int_t i=0; i<NCLONES; i++){    
3707            if(pam_tree_clone[i]){
3708                cout << pam_tree_clone[i]->GetName()<<endl;
3709                pam_tree_clone[i]->Write();
3710          };          };
3711      }      }
3712      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
# Line 1669  void PamLevel2::WriteCloneTrees(){ Line 3714  void PamLevel2::WriteCloneTrees(){
3714  }  }
3715    
3716  /**  /**
3717   * Create a new (empty) Pamela trees   * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.
3718     */
3719    //Int_t PamLevel2::GetEntry(Int_t iee){
3720    Int_t PamLevel2::GetEntry(Long64_t iee){
3721        
3722    //     cout << "-------------------------------------"<<endl;
3723    //     cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;
3724    
3725        if(!pam_tree){
3726            cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded"<<endl;
3727            return 0;
3728        }
3729    
3730    
3731        //
3732        // 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...
3733        //
3734        //    if(!run_tree ){
3735        //  cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loeaded"<<endl;
3736        //  return 0;
3737        //    }
3738    
3739        Long64_t ii=0;
3740        //-------------------------------
3741        ii = iee;
3742        if( !pam_tree->GetEntry(ii) ){      
3743            cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree"<<endl;
3744            return 0;
3745        }
3746    
3747        //
3748        // ... 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.
3749        // 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
3750        // a problem if you don't check the return code of getentry.
3751        //
3752        if(!RUN || !run_tree ){
3753            if ( TRK0 || CAL0 || TOF0 || RUN ) { //forse cosi` va bene per tornare 1?
3754                cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded"<<endl;
3755                return 0;
3756            }  else {
3757                return 1; //cosi` se non c'e` run esce qua...
3758            }
3759        }
3760    
3761        //-------------------------------
3762        ii = iee;
3763    //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);
3764    //    Bool_t UPDATED = UpdateRunInfo(ii);
3765        UpdateRunInfo(ii);
3766        if(SELLI==0)irunentry = iee-runfirstentry;
3767    //    if(UPDATED && run_tree_clone)run_tree_clone->Fill();
3768      
3769    //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;
3770    
3771    //     cout << "irunentry     "<<irunentry << endl;
3772    //     cout << "runfirstentry "<<runfirstentry << endl;
3773    //     cout << "nevents       "<<GetRunInfo()->NEVENTS<< endl;
3774    
3775    //     if( TRK0 || CAL0 || TOF0 ){
3776    //      if( !GetYodaEntry( ) ){
3777    //          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading level0 tree"<<endl;
3778    //          return 0;
3779    //      }
3780    //     }
3781    
3782    
3783        return 1;
3784    
3785    }
3786    
3787    TrkLevel0    *PamLevel2::GetTrkLevel0(){
3788        if( !TRK0 )return NULL;
3789        if( !GetYodaEntry( ) ){
3790            cout << " Int_t PamLevel2::GetTrkLevel0() -- ERROR -- error reading level0 tree"<<endl;
3791            return 0;
3792        }
3793        return trk0_obj;
3794    };
3795    CaloLevel0    *PamLevel2::GetCaloLevel0(){
3796        if( !CAL0 )return NULL;
3797        if( !GetYodaEntry( ) ){
3798            cout << " Int_t PamLevel2::GetCaloLevel0() -- ERROR -- error reading level0 tree"<<endl;
3799            return 0;
3800        }
3801        return calo0_obj;
3802    };
3803    
3804    
3805    /**
3806     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
3807     * Given the run ID (...), if needed it query the DB and load the proper file.
3808     * @return Pointer to the tree
3809   */   */
 // TTree* PamLevel2::GetNewPamTree(){  
3810    
 //     if(tree_clone)return tree_clone;  
3811    
3812  //     TTree *Tout = 0;  TTree* PamLevel2::GetYodaTree( ){
3813        
3814    //    cout << "TTree* PamLevel2::GetYodaTree( )"<<endl;
3815        //===================================
3816        // check if iroot has changed
3817        //===================================
3818        if( irun<0 ){
3819            cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = "<<irun<<endl;
3820    //      cout << "In order to use this method you have to first load the RunInfo tree "<<endl;
3821            return NULL;
3822        }    
3823        Int_t irootnew = run_obj->ID_ROOT_L0;
3824    //      cout << "iroot    "<<iroot<<endl;
3825    //      cout << "irootnew "<<irootnew<<endl;
3826    
3827        //===================================
3828        // load the level0 file
3829        // (if not already loaded)
3830        //===================================
3831        if( iroot != irootnew || !l0_tree){
3832            iroot = irootnew;
3833            //===================================
3834            // open the DB connection
3835            // (if not already opened)
3836            //===================================
3837            if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection();
3838            GL_ROOT glroot = GL_ROOT();
3839            if( glroot.Query_GL_ROOT(iroot,dbc) ){
3840                cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = "<<iroot<< " does not exists"<<endl;
3841                return NULL;
3842            };
3843            TString filename = glroot.PATH + glroot.NAME;
3844            if(l0_file){
3845                l0_file->Close();
3846                l0_file->Delete();
3847            }
3848            cout << "Opening LEVEL0 file: "<< filename << endl;
3849            FileStat_t t;
3850            if( gSystem->GetPathInfo(filename.Data(),t) ){
3851                cout << " PamLevel2::GetYodaTree() -- ERROR opening file "<<endl;
3852                return NULL;
3853            }
3854            l0_file = new TFile(filename);
3855            if( !l0_file )return NULL;
3856            l0_tree = (TTree*)l0_file->Get("Physics");
3857            if(!h0_obj)h0_obj = new EventHeader();
3858            l0_tree->SetBranchAddress("Header" ,&h0_obj);
3859            prevshift = 0;
3860            //---------------------------------------------------
3861            // TRACKER:
3862            if(TRK0){
3863                if(!trk0_obj){
3864                    trk0_obj = new TrkLevel0();
3865                    trk0_obj->Set();
3866                };
3867                l0_tree->SetBranchAddress("Tracker" ,trk0_obj->GetPointerToTrackerEvent());
3868            }
3869            //--------------------------------------------------
3870            // CALORIMETER:
3871            if(CAL0){
3872                if(!calo0_obj){
3873                    calo0_obj = new CaloLevel0();
3874                    calo0_obj->Set();
3875                };
3876                l0_tree->SetBranchAddress("Calorimeter" ,calo0_obj->GetPointerToCalorimeterEvent());
3877                //      cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;
3878            }
3879            //---------------------------------------------------
3880            // TOF:
3881            if(TOF0){
3882                cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented "<<endl;
3883            }
3884    
3885            dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
3886    
3887    
3888        };
3889    
3890    //     if(!dbc || (dbc && !dbc->IsConnected())){
3891    //      cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;
3892    //     }
3893    
3894        if ( TRK0 ){
3895            TrkParams::Load(6);
3896            if( !TrkParams::IsLoaded(6) ){
3897                cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- VK-mask not loaded"<<endl;
3898            };
3899            if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection();
3900            TrkParams::SetCalib(run_obj,dbc);
3901            TrkParams::LoadCalib( );
3902            if( !TrkParams::CalibIsLoaded() ){
3903                cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded"<<endl;
3904            };
3905            dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
3906        }
3907    
3908    
3909    //    cout << l0_tree << endl;
3910        return l0_tree;
3911    
3912    }
3913    
3914    /**
3915     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
3916     */
3917    Int_t PamLevel2::GetYodaEntry(){
3918    
3919    //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;
3920        if(!GetYodaTree())return 0;
3921            
3922  //     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      // patch
3923  //     cout << "Create new PAMELA trees "<<endl;      if( irunentry < 0){
3924    //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
3925            irunentry=0LL;
3926        }
3927        //  ---------------------------------
3928        //  if file is NOT a preselected file
3929        //  ---------------------------------
3930        Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM);
3931    //     cout << " irunentry "<<irunentry<<endl;
3932    //     cout << " EV_FROM "<<run_obj->EV_FROM<<endl;
3933    //     cout << " quellagiusta = irunentry + EV_FROM "<< quellagiusta << endl;
3934    
3935    //     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
3936    //     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
3937    //     cout << " time "<< abstime << endl;
3938    //     cout << " trk_calib_used "<<run_obj->TRK_CALIB_USED<< endl;
3939        
3940        ULong64_t obt     = 0;
3941        ULong64_t pktn    = 0;
3942        if( GetOrbitalInfo() ){
3943            obt     = GetOrbitalInfo()->OBT;
3944            pktn    = GetOrbitalInfo()->pkt_num;
3945        }
3946    
3947        if( !GetOrbitalInfo() && !ISGP){
3948            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo "<<endl;
3949            return 0;
3950        }
3951        if( obt==0 && pktn==0 && !ISGP ){
3952            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? "<<endl;
3953            return 0;
3954        }
3955    
3956        // ---------------------------------------------------------------------
3957        // ATTENTION!!!
3958        // If data are not pre-processed with cleaner, the level0 tree may contain
3959        // spurious nested physics packets.
3960        // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
3961        // while level2 tree DOES NOT!!
3962        // This means that "quellagiusta" in these cases is not correct.
3963        // In order to retrieve the correct level0 event, I implemented a check
3964        // of the OBT and pkt-number. In case of mismatch, the level0 entry number
3965        // is shift forward until when the packets match.
3966        // ---------------------------------------------------------------------
3967        Int_t answer = 0;
3968        Int_t shift =0;
3969        //    printf(" siamo qui %i %i \n",shift,prevshift);
3970        //    Int_t maxshift = 50; // EMILIANO
3971        do{
3972            if(shift>0){    
3973                cout << " PKTNUM  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
3974                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;
3975                cout << "         L2 <--> L0 mismatch ( irun "<<irun<<" irunentry "<<irunentry<<" shift "<<shift<<" prevshift "<<prevshift<<" )"<<endl;
3976            }
3977            answer = l0_tree->GetEntry(quellagiusta+(Long64_t)shift+(Long64_t)prevshift);
3978            shift++;
3979            if( !GetEventHeader() ){
3980                cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader "<<endl;
3981                return 0;
3982            }
3983    
3984            if(ISGP){
3985                obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
3986                pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
3987            }
3988    
3989    //      cout << "PKTNUM "<<shift<<" ==  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
3990    //      cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;
3991    //      if( (quellagiusta+shift) == l0_tree->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;
3992    //      cout << " obt "<< obt << endl;
3993    //      cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;
3994    //      cout << " pktn "<< pktn << endl;
3995    //      cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
3996    //      printf(" IDRUN %u \n",GetRunInfo()->ID);
3997    //
3998            if ( prevshift != 0 && (quellagiusta+(Long64_t)shift) == GetYodaTree()->GetEntries() ){
3999                prevshift = 0;
4000                shift = -1;
4001            };
4002    
4003        }while( ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) ||
4004                  pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())       )
4005                && (quellagiusta+(Long64_t)shift) < GetYodaTree()->GetEntries() && shift < maxshift);
4006    
4007        if ( (quellagiusta+(Long64_t)shift+(Long64_t)prevshift) > GetYodaTree()->GetEntries() || shift == maxshift ) {
4008            cout << " Big trouble here, no such event in Level0 data! " <<endl;
4009            return 0;
4010        }
4011    //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
4012    //    return GetYodaTree()->GetEntry(quellagiusta);
4013        if ( shift > 1 ) prevshift += (shift-1);
4014            
4015        return answer;
4016            
4017  //     if(TRK1||TRK2||TRKh){  }
4018  //      TTree *T = new TTree("Tracker_clone","PAMELA tracker level2 data ");      /**
4019  //      if(TRK1) {   * \Brief Set DB connection
4020  //          trk1_clone = new TrkLevel1();   */
4021  //          T->Branch("TrkLevel1","TrkLevel1", &trk1_clone);  Bool_t PamLevel2::SetDBConnection(){
 //          T->BranchRef();  
 //          cout << "Tracker      : branch TrkLevel1"<<endl;  
 //      };  
 //      if(TRK2) {  
 //          trk2_clone = new TrkLevel2();  
 //          T->Branch("TrkLevel2", "TrkLevel2",&trk2_clone);  
 //          cout << "Tracker      : branch TrkLevel2"<<endl;          
 //      };  
 //      if(TRKh) {  
 //          trkh_clone = new TrkHough();  
 //          T->Branch("TrkHough","TrkHough", &trkh_clone);  
 //          cout << "Tracker      : branch TrkHough"<<endl;  
 //      };  
 //      if(!Tout)Tout=T;  
 //      else Tout->AddFriend("Tracker_clone");  
 //     }  
4022    
4023  //     // Calorimeter      if(DBG){
4024  //     if(CAL1||CAL2){          cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
4025  //      TTree *C = new TTree("Calorimeter_clone","PAMELA calorimeter level2 data ");              cout<<"Connecting to DB"<<endl;
4026  //      if(CAL1) {          cout<<"HOST "<<host<<endl;
4027  //          calo1_clone = new CaloLevel1();          cout<<"USER "<<user<<endl;
4028  //          C->Branch("CaloLevel1", "CaloLevel1", &calo1_clone);          cout<<"PSW  "<<psw<<endl;
4029  //          cout << "Calorimeter  : branch CaloLevel1"<<endl;      }
4030  //      };      dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
4031  //      if(CAL2) {      if( !dbc )return false;
4032  //          calo2_clone = new CaloLevel2();      if( !dbc->IsConnected() )return false;    
4033  //          C->Branch("CaloLevel2","CaloLevel2", &calo2_clone);      stringstream myquery;  // EMILIANO
4034  //          cout << "Calorimeter  : branch CaloLevel2"<<endl;      myquery.str("");  // EMILIANO
4035  //      };      myquery << "SET time_zone='+0:00'";  // EMILIANO
4036  //      if(!Tout)Tout=C;      dbc->Query(myquery.str().c_str());  // EMILIANO
4037  //      else Tout->AddFriend("Calorimeter_clone");      if(DBG)cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
4038  //     }          return true;
   
 //     // ToF      
 //     if(TOF) {  
 //      TTree *O = new TTree("ToF_clone","PAMELA ToF level2 data ");      
 //      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;  
4039    
4040  //     tree_clone = Tout;  }
 //     tree_clone->SetDirectory(0);  
4041    
4042  //     return Tout;  /**
4043  // }   * \Brief Add a friend to the pamela chain.
4044  // /**   * @param cname name of the chain to be added
4045  //  * Fill a tree (created with GetNewPamTree)   */
4046  //  *  
4047  //  */  TChain* PamLevel2::AddFriend(TString cname){
4048  // //void PamLevel2::FillNewPamTree(TTree *T){  
4049  // void PamLevel2::FillNewPamTree(){      if(!GetPamTree()){
4050                    cout << " TChain* PamLevel2::AddFriend(TString cname) --- a pamela tree must be created first"<<endl;
4051            return NULL;
4052        }
4053    
4054    
4055        TChain *c = new TChain( cname.Data() );
4056    
4057        TIter next( GetPamTree()->GetListOfFiles() );
4058        Int_t nf = 0;
4059        TChainElement* element = 0;    
4060        while ((element = (TChainElement*) next())) {      
4061            c->Add(element->GetTitle());
4062            nf++;
4063        }
4064    
4065        GetPamTree()->AddFriend(cname.Data());
4066    
4067        cout << "external chain created and added to pamela friends :"<<cname<<endl;
4068        cout << "n.files "<<nf<<endl;
4069        
4070    
4071        return c;    
4072    
4073    }
4074    
 //     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  
 //     }  
4075    
 // }  

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.61

  ViewVC Help
Powered by ViewVC 1.1.23