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

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.68

  ViewVC Help
Powered by ViewVC 1.1.23