/[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.48 by mocchiut, Wed Sep 12 09:51:57 2007 UTC revision 1.102 by mocchiut, Fri Oct 17 16:15:06 2014 UTC
# Line 1  Line 1 
1  #include <PamLevel2.h>  #include <PamLevel2.h>
2    
3    /////////////////////////////////////////////////////////////////////////////
4    /////////////////////////////////////////////////////////////////////////////
5    /////////////////////////////////////////////////////////////////////////////
6    /////////////////////////////////////////////////////////////////////////////
7    
8    void GPamela::Clear() {
9      Irun = 0;
10      Ievnt = 0;
11      Ipa = 0;
12      X0 = 0.;
13      Y0 = 0.;
14      Z0 = 0.;
15      Theta = 0.;
16      Phi = 0.;
17      P0 = 0.;
18      Nthtof = 0;
19      Nthcat = 0;
20      Nthcas = 0;
21      Nthspe = 0;
22      Nstrpx = 0;
23      Nstrpy = 0;
24      Nthcali = 0;
25      Nthcal = 0;
26      Nthnd = 0;
27      Nthcard = 0;
28    }
29    
30    void GPamela::Delete() {
31    
32      Clear();
33      /* EM all these are in the stack not in the heap, so no need to delete by hand...
34      delete[] Ipltof;
35      delete[] Ipaddle;
36      delete[] Ipartof;
37      delete[] Xintof;
38      delete[] Yintof;
39      delete[] Zintof;
40      delete[] Xouttof;
41      delete[] Youttof;
42      delete[] Zouttof;
43      delete[] Ereltof;
44      delete[] Timetof;
45      delete[] Pathtof;
46      delete[] P0tof;
47      delete[] Iparcat;
48      delete[] Icat;
49      delete[] Xincat;
50      delete[] Yincat;
51      delete[] Zincat;
52      delete[] Xoutcat;
53      delete[] Youtcat;
54      delete[] Zoutcat;
55      delete[] Erelcat;
56      delete[] Timecat;
57      delete[] Pathcat;
58      delete[] P0cat;
59      delete[] Iparcas;
60      delete[] Icas;
61      delete[] Xincas;
62      delete[] Yincas;
63      delete[] Zincas;
64      delete[] Xoutcas;
65      delete[] Youtcas;
66      delete[] Zoutcas;
67      delete[] Erelcas;
68      delete[] Timecas;
69      delete[] Pathcas;
70      delete[] P0cas;
71      delete[] Iparspe;
72      delete[] Itrpb;
73      delete[] Itrsl;
74      delete[] Itspa;
75      delete[] Xinspe;
76      delete[] Yinspe;
77      delete[] Zinspe;
78      delete[] Xoutspe;
79      delete[] Youtspe;
80      delete[] Zoutspe;
81      delete[] Xavspe;
82      delete[] Yavspe;
83      delete[] Zavspe;
84      delete[] Erelspe;
85      delete[] Pathspe;
86      delete[] P0spe;
87      delete[] Nxmult;
88      delete[] Nymult;
89      delete[] Npstripx;
90      delete[] Ntstripx;
91      delete[] Istripx;
92      delete[] Qstripx;
93      delete[] Xstripx;
94      delete[] Npstripy;
95      delete[] Ntstripy;
96      delete[] Istripy;
97      delete[] Qstripy;
98      delete[] Ystripy;
99      delete[] Icaplane;
100      delete[] Icastrip;
101      delete[] Icamod;
102      delete[] Enestrip;
103      delete[] Icapl;
104      delete[] Icasi;
105      delete[] Icast;
106      delete[] Xincal;
107      delete[] Yincal;
108      delete[] Zincal;
109      delete[] Erelcal;
110      delete[] Itubend;
111      delete[] Iparnd;
112      delete[] Xinnd;
113      delete[] Yinnd;
114      delete[] Zinnd;
115      delete[] Xoutnd;
116      delete[] Youtnd;
117      delete[] Zoutnd;
118      delete[] Erelnd;
119      delete[] Timend;
120      delete[] Pathnd;
121      delete[] P0nd;
122      delete[] Iparcard;
123      delete[] Icard;
124      delete[] Xincard;
125      delete[] Yincard;
126      delete[] Zincard;
127      delete[] Xoutcard;
128      delete[] Youtcard;
129      delete[] Zoutcard;
130      delete[] Erelcard;
131      delete[] Timecard;
132      delete[] Pathcard;
133      delete[] P0card;
134      */
135    }
136    
137    
138    void GPamela::SetBranchAddress(TChain* fhBookTree) {
139    
140      //    cout << "fhBookTree "<<fhBookTree << endl;
141      // prepare tree
142      fhBookTree->SetBranchAddress("Irun", &Irun);
143      fhBookTree->SetBranchAddress("Ievnt", &Ievnt);
144      fhBookTree->SetBranchAddress("Ipa", &Ipa);
145      fhBookTree->SetBranchAddress("X0", &X0);
146      fhBookTree->SetBranchAddress("Y0", &Y0);
147      fhBookTree->SetBranchAddress("Z0", &Z0);
148      fhBookTree->SetBranchAddress("Theta", &Theta);
149      fhBookTree->SetBranchAddress("Phi", &Phi);
150      fhBookTree->SetBranchAddress("P0", &P0);
151      fhBookTree->SetBranchAddress("Nthtof", &Nthtof);
152      fhBookTree->SetBranchAddress("Ipltof", Ipltof);
153      fhBookTree->SetBranchAddress("Ipaddle", Ipaddle);
154      fhBookTree->SetBranchAddress("Ipartof", Ipartof);
155      fhBookTree->SetBranchAddress("Xintof", Xintof);
156      fhBookTree->SetBranchAddress("Yintof", Yintof);
157      fhBookTree->SetBranchAddress("Zintof", Zintof);
158      fhBookTree->SetBranchAddress("Xouttof", Xouttof);
159      fhBookTree->SetBranchAddress("Youttof", Youttof);
160      fhBookTree->SetBranchAddress("Zouttof", Zouttof);
161      fhBookTree->SetBranchAddress("Ereltof", Ereltof);
162      fhBookTree->SetBranchAddress("Timetof", Timetof);
163      fhBookTree->SetBranchAddress("Pathtof", Pathtof);
164      fhBookTree->SetBranchAddress("P0tof", P0tof);
165      fhBookTree->SetBranchAddress("Nthcat", &Nthcat);
166      fhBookTree->SetBranchAddress("Iparcat", Iparcat);
167      fhBookTree->SetBranchAddress("Icat", Icat);
168      fhBookTree->SetBranchAddress("Xincat", Xincat);
169      fhBookTree->SetBranchAddress("Yincat", Yincat);
170      fhBookTree->SetBranchAddress("Zincat", Zincat);
171      fhBookTree->SetBranchAddress("Xoutcat", Xoutcat);
172      fhBookTree->SetBranchAddress("Youtcat", Youtcat);
173      fhBookTree->SetBranchAddress("Zoutcat", Zoutcat);
174      fhBookTree->SetBranchAddress("Erelcat", Erelcat);
175      fhBookTree->SetBranchAddress("Timecat", Timecat);
176      fhBookTree->SetBranchAddress("Pathcat", Pathcat);
177      fhBookTree->SetBranchAddress("P0cat", P0cat);
178      fhBookTree->SetBranchAddress("Nthcas", &Nthcas);
179      fhBookTree->SetBranchAddress("Iparcas", Iparcas);
180      fhBookTree->SetBranchAddress("Icas", Icas);
181      fhBookTree->SetBranchAddress("Xincas", Xincas);
182      fhBookTree->SetBranchAddress("Yincas", Yincas);
183      fhBookTree->SetBranchAddress("Zincas", Zincas);
184      fhBookTree->SetBranchAddress("Xoutcas", Xoutcas);
185      fhBookTree->SetBranchAddress("Youtcas", Youtcas);
186      fhBookTree->SetBranchAddress("Zoutcas", Zoutcas);
187      fhBookTree->SetBranchAddress("Erelcas", Erelcas);
188      fhBookTree->SetBranchAddress("Timecas", Timecas);
189      fhBookTree->SetBranchAddress("Pathcas", Pathcas);
190      fhBookTree->SetBranchAddress("P0cas", P0cas);
191      fhBookTree->SetBranchAddress("Nthspe", &Nthspe);
192      fhBookTree->SetBranchAddress("Iparspe", Iparspe);
193      fhBookTree->SetBranchAddress("Itrpb", Itrpb);
194      fhBookTree->SetBranchAddress("Itrsl", Itrsl);
195      fhBookTree->SetBranchAddress("Itspa", Itspa);
196      fhBookTree->SetBranchAddress("Xinspe", Xinspe);
197      fhBookTree->SetBranchAddress("Yinspe", Yinspe);
198      fhBookTree->SetBranchAddress("Zinspe", Zinspe);
199      fhBookTree->SetBranchAddress("Xoutspe", Xoutspe);
200      fhBookTree->SetBranchAddress("Youtspe", Youtspe);
201      fhBookTree->SetBranchAddress("Zoutspe", Zoutspe);
202      fhBookTree->SetBranchAddress("Xavspe", Xavspe);
203      fhBookTree->SetBranchAddress("Yavspe", Yavspe);
204      fhBookTree->SetBranchAddress("Zavspe", Zavspe);
205      fhBookTree->SetBranchAddress("Erelspe", Erelspe);
206      fhBookTree->SetBranchAddress("Pathspe", Pathspe);
207      fhBookTree->SetBranchAddress("P0spe", P0spe);
208      fhBookTree->SetBranchAddress("Nxmult", Nxmult);
209      fhBookTree->SetBranchAddress("Nymult", Nymult);
210      fhBookTree->SetBranchAddress("Nstrpx", &Nstrpx);
211      fhBookTree->SetBranchAddress("Npstripx", Npstripx);
212      fhBookTree->SetBranchAddress("Ntstripx", Ntstripx);
213      fhBookTree->SetBranchAddress("Istripx", Istripx);
214      fhBookTree->SetBranchAddress("Qstripx", Qstripx);
215      fhBookTree->SetBranchAddress("Xstripx", Xstripx);
216      fhBookTree->SetBranchAddress("Nstrpy", &Nstrpy);
217      fhBookTree->SetBranchAddress("Npstripy", Npstripy);
218      fhBookTree->SetBranchAddress("Ntstripy", Ntstripy);
219      fhBookTree->SetBranchAddress("Istripy", Istripy);
220      fhBookTree->SetBranchAddress("Qstripy", Qstripy);
221      fhBookTree->SetBranchAddress("Ystripy", Ystripy);
222      fhBookTree->SetBranchAddress("Nthcali", &Nthcali);
223      fhBookTree->SetBranchAddress("Icaplane", Icaplane);
224      fhBookTree->SetBranchAddress("Icastrip", Icastrip);
225      fhBookTree->SetBranchAddress("Icamod", Icamod);
226      fhBookTree->SetBranchAddress("Enestrip", Enestrip);
227      fhBookTree->SetBranchAddress("Nthcal", &Nthcal);
228      fhBookTree->SetBranchAddress("Icapl", Icapl);
229      fhBookTree->SetBranchAddress("Icasi", Icasi);
230      fhBookTree->SetBranchAddress("Icast", Icast);
231      fhBookTree->SetBranchAddress("Xincal", Xincal);
232      fhBookTree->SetBranchAddress("Yincal", Yincal);
233      fhBookTree->SetBranchAddress("Zincal", Zincal);
234      fhBookTree->SetBranchAddress("Erelcal", Erelcal);
235      fhBookTree->SetBranchAddress("Nthnd", &Nthnd);
236      fhBookTree->SetBranchAddress("Itubend", Itubend);
237      fhBookTree->SetBranchAddress("Iparnd", Iparnd);
238      fhBookTree->SetBranchAddress("Xinnd", Xinnd);
239      fhBookTree->SetBranchAddress("Yinnd", Yinnd);
240      fhBookTree->SetBranchAddress("Zinnd", Zinnd);
241      fhBookTree->SetBranchAddress("Xoutnd", Xoutnd);
242      fhBookTree->SetBranchAddress("Youtnd", Youtnd);
243      fhBookTree->SetBranchAddress("Zoutnd", Zoutnd);
244      fhBookTree->SetBranchAddress("Erelnd", Erelnd);
245      fhBookTree->SetBranchAddress("Timend", Timend);
246      fhBookTree->SetBranchAddress("Pathnd", Pathnd);
247      fhBookTree->SetBranchAddress("P0nd", P0nd);
248      fhBookTree->SetBranchAddress("Nthcard", &Nthcard);
249      fhBookTree->SetBranchAddress("Iparcard", Iparcard);
250      fhBookTree->SetBranchAddress("Icard", Icard);
251      fhBookTree->SetBranchAddress("Xincard", Xincard);
252      fhBookTree->SetBranchAddress("Yincard", Yincard);
253      fhBookTree->SetBranchAddress("Zincard", Zincard);
254      fhBookTree->SetBranchAddress("Xoutcard", Xoutcard);
255      fhBookTree->SetBranchAddress("Youtcard", Youtcard);
256      fhBookTree->SetBranchAddress("Zoutcard", Zoutcard);
257      fhBookTree->SetBranchAddress("Erelcard", Erelcard);
258      fhBookTree->SetBranchAddress("Timecard", Timecard);
259      fhBookTree->SetBranchAddress("Pathcard", Pathcard);
260      fhBookTree->SetBranchAddress("P0card", P0card);
261    
262      //    fhBookTree->SetBranchStatus("*",0);
263    
264    }
265    
266    ClassImp( GPamela);
267    
268    /////////////////////////////////////////////////////////////////////////////
269    /////////////////////////////////////////////////////////////////////////////
270    /////////////////////////////////////////////////////////////////////////////
271    /////////////////////////////////////////////////////////////////////////////
272  //--------------------------------------  //--------------------------------------
273  //  //
274  //  //
275  //--------------------------------------  //--------------------------------------
276  /**  /**
277   * Default constructor   * Default constructor
278   */   */
279  PamTrack::PamTrack(){  PamTrack::PamTrack() {
280      trk_track  = 0;    trk_track = 0;
281      calo_track = 0;    trk_ext_track = 0;
282      tof_track  = 0;    calo_track = 0;
283      candeleteobj = 0;    tof_track = 0;
284      pscore = 0;    orb_track = 0;
285      iscore = 0;    candeleteobj = 0;
286  };    pscore = 0;
287      iscore = 0;
288    }
289    ;
290  //--------------------------------------  //--------------------------------------
291  //  //
292  //  //
293  //--------------------------------------  //--------------------------------------
294  /**  /**
295   * Constructor   * Constructor
296   */   */
297  PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o){  PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o, OrbitalInfoTrkVar *r) {
298    
299      trk_track  = 0;    trk_ext_track = 0;
300      calo_track = 0;    trk_track = 0;
301      tof_track  = 0;    calo_track = 0;
302  //     if(t)trk_track  = new TrkTrack(*t);    tof_track = 0;
303  //     if(c)calo_track = new CaloTrkVar(*c);    orb_track = 0;
304  //     if(o)tof_track  = new ToFTrkVar(*o);    //     if(t)trk_track  = new TrkTrack(*t);
305      if(t)trk_track  = t;    //     if(c)calo_track = new CaloTrkVar(*c);
306      if(c)calo_track = c;    //     if(o)tof_track  = new ToFTrkVar(*o);
307      if(o)tof_track  = o;  //   if (t)
308      candeleteobj = 0;  //     trk_track = t;
309    //   if (c)
310    //     calo_track = c;
311    //   if (o)
312    //     tof_track = o;
313    //   if (r)
314    //     orb_track = r;
315    
316    //   candeleteobj = 0;
317    
318    
319      if (t){
320          trk_track = new TrkTrack(*t);
321          trk_ext_track = new ExtTrack(*t);//NB!! ha dimensione 6 invece che 8
322      }
323      if (c)
324        calo_track = new CaloTrkVar(*c);
325      if (o)
326        tof_track = new ToFTrkVar(*o);
327      if (r)
328        orb_track = new OrbitalInfoTrkVar(*r);
329      candeleteobj = 1;
330    
331  };  }
332  PamTrack::PamTrack(const PamTrack& track){  ;
333    /**
334     * Constructor
335     */
336    PamTrack::PamTrack(ExtTrack* t, CaloTrkVar* c, ToFTrkVar* o, OrbitalInfoTrkVar *r) {
337    
338      trk_ext_track = 0;
339      trk_track = 0;
340      calo_track = 0;
341      tof_track = 0;
342      orb_track = 0;
343      //     if(t)trk_track  = new TrkTrack(*t);
344      //     if(c)calo_track = new CaloTrkVar(*c);
345      //     if(o)tof_track  = new ToFTrkVar(*o);
346    //   if (t)
347    //     trk_track = t;
348    //   if (c)
349    //     calo_track = c;
350    //   if (o)
351    //     tof_track = o;
352    //   if (r)
353    //     orb_track = r;
354    
355    //   candeleteobj = 0;
356    
357    
358      if (t){
359    ////      trk_track = new TrkTrack(*t);//in this case TrkTrack object remains null
360          trk_ext_track = new ExtTrack(*t);
361      }
362      if (c)
363        calo_track = new CaloTrkVar(*c);
364      if (o)
365        tof_track = new ToFTrkVar(*o);
366      if (r)
367        orb_track = new OrbitalInfoTrkVar(*r);
368      candeleteobj = 1;
369      pscore = 0;
370      iscore = 0;
371    
372      TrkTrack   *t = track.trk_track;  }
373      CaloTrkVar *c = track.calo_track;  ;
374      ToFTrkVar  *o = track.tof_track;  PamTrack::PamTrack(const PamTrack& track) {
375    
376      TrkTrack *t = track.trk_track;
377      ExtTrack *e = track.trk_ext_track;
378      CaloTrkVar *c = track.calo_track;
379      ToFTrkVar *o = track.tof_track;
380      OrbitalInfoTrkVar *r = track.orb_track;
381    
382      trk_ext_track = 0;
383      trk_track     = 0;
384      calo_track    = 0;
385      tof_track     = 0;
386      orb_track     = 0;
387      if(e)
388          trk_ext_track = new ExtTrack(*e);
389      if (t)
390        trk_track = new TrkTrack(*t);
391      if (c)
392        calo_track = new CaloTrkVar(*c);
393      if (o)
394        tof_track = new ToFTrkVar(*o);
395      if (r)
396        orb_track = new OrbitalInfoTrkVar(*r);
397    
398      candeleteobj = 1;
399      pscore = 0;
400      iscore = 0;
401    
     trk_track  = 0;  
     calo_track = 0;  
     tof_track  = 0;  
     if(t)trk_track  = new TrkTrack(*t);  
     if(c)calo_track = new CaloTrkVar(*c);  
     if(o)tof_track  = new ToFTrkVar(*o);  
     candeleteobj = 1;  
       
402  }  }
403  void PamTrack::Clear(){  void PamTrack::Clear(Option_t *option) {
404    
405  //    cout << "PamTrack::Clear() "<<candeleteobj<<endl;  //    cout << "PamTrack::Clear( "<<option<<" ) "<<candeleteobj<<endl;
406      if(candeleteobj){    if (candeleteobj) {
407          if(trk_track)  trk_track->TrkTrack::Clear();        
408          if(calo_track) calo_track->CaloTrkVar::Clear();//???      if (trk_ext_track)
409          if(tof_track)  tof_track->ToFTrkVar::Clear();//???        trk_ext_track->ExtTrack::Clear(option);
410      }else{      if (trk_track)
411          trk_track  = 0;        trk_track->TrkTrack::Clear();
412          calo_track = 0;      if (calo_track)
413          tof_track  = 0;        calo_track->CaloTrkVar::Clear();//???
414      }      if (tof_track)
415      pscore = 0;        tof_track->ToFTrkVar::Clear();//???
416      iscore = 0;      if (orb_track)
417          orb_track->OrbitalInfoTrkVar::Clear();//???
418  }    }
419  void PamTrack::Delete(){    else {
420  //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;      trk_ext_track = 0;
421      if(candeleteobj){      trk_track = 0;
422          if(trk_track)  {      calo_track = 0;
423              trk_track->TrkTrack::Clear();      tof_track = 0;
424              delete trk_track;      orb_track = 0;
425          }    }
426          if(calo_track){    pscore = 0;
427              calo_track->CaloTrkVar::Clear();//???    iscore = 0;
428              delete calo_track;  
429          }  }
430          if(tof_track){  void PamTrack::Delete() {
431              tof_track->ToFTrkVar::Clear();//???    //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;
432              delete tof_track;    if (candeleteobj) {
433          }      if (trk_ext_track) {
434      }else{        trk_ext_track->ExtTrack::Clear("C");
435          Clear();        delete trk_ext_track;
436        }
437        if (trk_track) {
438          trk_track->TrkTrack::Clear();
439          delete trk_track;
440        }
441        if (calo_track) {
442          calo_track->CaloTrkVar::Clear();//???
443          delete calo_track;
444        }
445        if (tof_track) {
446          tof_track->ToFTrkVar::Clear();//???
447          delete tof_track;
448        }
449        if (orb_track) {
450          orb_track->OrbitalInfoTrkVar::Clear();//???
451          delete orb_track;
452      }      }
453      }
454      else {
455        Clear();
456      }
457  }  }
458    
459    
460    
461    
462  //--------------------------------------  //--------------------------------------
463  //  //
464  //  //
465  //--------------------------------------  //--------------------------------------
466  /**  /**
467   * Default Constructor   * Default Constructor
468   */   */
469  PamLevel2::PamLevel2(){  PamLevel2::PamLevel2() {
470    Initialize();    Initialize();
471  };  }
472    
473    
474  /**  /**
475   * Constructor with given dir, list and detectors   * Constructor
476     * @param ddir Name of directory where level2 files are stored.
477     * @param list Name of an ascii file containing the list of file names
478     * @param detlist Options to chose what to load.
479     * Possible options are:
480     *       +AUTO --> load all trees/branches in the input files
481     *       +(-)ALL --> inlcude(exclude) all trees/branches
482     *       +(-)TRK1+(-)TRK2+(-)CAL1+(-)CAL2+(-)TOF+(-)TRG+(-)ND+(-)S4+(-)ORB+(-)AC --> inlcude(exclude) trees and branches
483     *       +(-)TRK0 --> include(exclude) tracker level0 tree
484     *       +(-)GP --> include exclude GPAMELA output tree
485     * If no options are specified, the default is assumed. Default is:
486     * +TRK2+CAL2+CAL1+TOF+TRG+ND+AC+S4+ORB
487   */   */
488  PamLevel2::PamLevel2(TString ddir,TString list,TString detlist){  PamLevel2::PamLevel2(TString ddir, TString llist, TString detlist) {
489    Initialize();    Initialize();
490    GetPamTree(GetListOfLevel2Files(ddir,list),detlist);    TList* listf = GetListOfLevel2Files(ddir, llist);
491    GetRunTree(GetListOfLevel2Files(ddir,list));    if (listf)
492  };      GetPamTree(listf, detlist);
493      if (listf)
494        GetRunTree(listf);
495      SetMaxShift(-1);
496    }
497    
498    
499    PamLevel2::PamLevel2(TString ddir, TList *llist, TString detlist) {
500      Initialize();
501      GetPamTree(llist, detlist);
502      GetRunTree(llist);
503      SetMaxShift(-1);
504    }
505    
506  /**  /**
507   * Constructor with given dir and list   * Constructor
508     * @param ddir Name of directory where level2 files are stored.
509     * @param list Name of an ascii file containing the list of file names
510     * Default trees/branches are loaded. Default is:
511     * +TRK2+CAL2+CAL1+TOF+TRG+ND+AC+S4+ORB
512   */   */
513  PamLevel2::PamLevel2(TString ddir,TString list){  PamLevel2::PamLevel2(TString ddir, TString llist) {
514    Initialize();    Initialize();
515    GetPamTree(GetListOfLevel2Files(ddir,list),"");    TList* listf = GetListOfLevel2Files(ddir, llist);
516    GetRunTree(GetListOfLevel2Files(ddir,list));    cout << "GetPamTree: "<<endl;
517  };    GetPamTree(listf, "");
518      cout << "GetRunTree: "<<endl;
519      GetRunTree(listf);
520      SetMaxShift(-1);
521    }
522    
523    
524  void PamLevel2::Initialize(){  void PamLevel2::Initialize() {
           
     h0_obj    = 0;  
     trk0_obj  = 0;  
     calo0_obj  = 0;  
   
     trk2_obj  = 0;  
     trk1_obj  = 0;  
     trkh_obj  = 0;  
     calo1_obj = 0;  
     calo2_obj = 0;  
     tof_obj   = 0;  
     trig_obj  = 0;  
     s4_obj    = 0;  
     nd_obj    = 0;  
     ac_obj    = 0;  
     orb_obj   = 0;  
   
     run_obj   = 0;//new GL_RUN();  
     soft_obj   = 0;// Emiliano  
     irun = -1LL;  
     runfirstentry = 0LL;  
     runlastentry = 0LL;  
   
     l0_file = NULL;  
     l0_tree = NULL;  
     iroot   = -1;  
     dbc     = 0;  
525    
526      prevshift = 0;    h0_obj = 0;
527          trk0_obj = 0;
528      run_tree = NULL;    calo0_obj = 0;
529      run_tree_clone = NULL;  
530      sel_tree = NULL;    trk2_obj = 0;
531      sel_tree_clone = NULL;    trk1_obj = 0;
532          trkh_obj = 0;
533      irunentry = -1LL;    calo1_obj = 0;
534      pam_tree = NULL;    calo2_obj = 0;
535      for(Int_t i=0; i<NCLONES; i++ )pam_tree_clone[i]=NULL;    tof2_obj = 0;
536      trig_obj = 0;
537      totdltime[0] = 0LL;    s4_obj = 0;
538      totdltime[1] = 0LL;    nd_obj = 0;
539      ac_obj = 0;
540      host = "mysql://localhost/pamelaprod";    orb2_obj = 0;
541      user = "anonymous";    gp_obj = 0;
542      psw = "";  
543      const char *pamdbhost=gSystem->Getenv("PAM_DBHOST");    extAlgFlag=0;
544      const char *pamdbuser=gSystem->Getenv("PAM_DBUSER");  
545      const char *pamdbpsw=gSystem->Getenv("PAM_DBPSW");    trk_ext_obj     = 0;
546      if ( !pamdbhost ) pamdbhost = "";    trk_ext_nuc_obj = 0;
547      if ( !pamdbuser ) pamdbuser = "";    trk_nuc_obj     = 0;
548      if ( !pamdbpsw ) pamdbpsw = "";    
549      if ( strcmp(pamdbhost,"") ) host = pamdbhost;    calo_ext_obj     = 0;
550      if ( strcmp(pamdbuser,"") ) user = pamdbuser;    calo_ext_nuc_obj = 0;
551      if ( strcmp(pamdbpsw,"") ) psw = pamdbpsw;    calo_nuc_obj     = 0;
552      
553      tof_ext_obj     = 0;
554      tof_ext_nuc_obj = 0;
555      tof_nuc_obj     = 0;
556      
557      orb_ext_obj     = 0;
558      orb_ext_nuc_obj = 0;
559      orb_nuc_obj     = 0;
560    
561      trk2_nuc_obj  = 0;
562      calo2_nuc_obj = 0;
563      tof2_nuc_obj  = 0;
564      orb2_nuc_obj  = 0;
565    
566    
567      run_obj = 0;//new GL_RUN();
568      soft_obj = 0;// Emiliano
569      proc_obj = 0;// Emiliano
570      irun = -1LL;
571      irunt = -1LL;
572      totrunentry = 0LL;
573      totrunentrymax = 0LL;
574      totrunentrymin = 0LL;
575      runfirstentry = 0LL;
576      runlastentry = 0LL;
577      gltsync = 0; // Emiliano
578      fUpdateRunInfo = true; // Emiliano
579      fUseDBinRunInfo = true; // Emiliano
580      fDiscarded = false; //EM
581      isSync = false; // by default assume that the level2 file(s) is(are) not sinchronized between L0/DB and L2, that is we miss some packets in L2 due to nested/DV-skipped events
582      il0entry = 0LL;
583      //  hasL0EE = true;
584    
585          l0_file = NULL;
586      l0_tree = NULL;
587      iroot = -1;
588      dbc = 0;
589    
590      prevshift = 0;
591      yprevshift = 0;
592      maxshift = 10; //EMILIANO now overridden by SetMaxShift(-1) called by constructors
593    
594  //    sorted_tracks = 0;//new TRefArray();    run_tree = NULL;
595          run_tree_clone = NULL;
     CAL0 = false;  
     CAL1 = true;  
     CAL2 = true;  
     TRK2 = true;  
     TRK1 = false;  
     TRKh = false;  
     TRKh = false;  
     TRG  = true;  
     TOF  = true;  
     TOF0 = false;  
     S4   = true;  
     ND   = true;  
     AC   = true;  
     ORB  = true;  
       
     RUN  = true;  
596    
597      SELLI = -1;    proc_tree = NULL;
598      proc_tree_clone = NULL;
599    
600      ISGP = false;    sel_tree = NULL;
601      sel_tree_clone = NULL;
602    
603      tsorted=0;    irunentry = -1LL;
604      timage=0;    pam_tree = NULL;
605          for (Int_t i = 0; i < NCLONES; i++)
606      howtosort = "+CAL+TOF";      pam_tree_clone[i] = NULL;
     //howtosort = "+TOF";  
     sortthr = 100.;  
607    
608  };    totdltime[0] = 0LL;
609      totdltime[1] = 0LL;
610      totdltime[2] = 0LL;
611    
612      host = "mysql://localhost/pamelaprod";
613      user = "anonymous";
614      psw = "";
615      const char *pamdbhost = gSystem->Getenv("PAM_DBHOST");
616      const char *pamdbuser = gSystem->Getenv("PAM_DBUSER");
617      const char *pamdbpsw = gSystem->Getenv("PAM_DBPSW");
618      if (!pamdbhost)
619        pamdbhost = "";
620      if (!pamdbuser)
621        pamdbuser = "";
622      if (!pamdbpsw)
623        pamdbpsw = "";
624      if (strcmp(pamdbhost, ""))
625        host = pamdbhost;
626      if (strcmp(pamdbuser, ""))
627        user = pamdbuser;
628      if (strcmp(pamdbpsw, ""))
629        psw = pamdbpsw;
630    
631      //    sorted_tracks = 0;//new TRefArray();
632    
633      CAL0 = false;
634      CAL1 = true;
635      CAL2 = true;
636      TRK2 = true;
637      TRK1 = false;
638      TRK0 = false;
639      TRKh = false;
640      TRG = true;
641      TOF = true;
642      TOF0 = false;
643      S4 = true;
644      ND = true;
645      AC = true;
646      ORB = true;
647      PROC = true;
648      GP = false;
649    
650      EXT = false;
651      NUC = false;
652      trkAlg = "STD";//default tracking algorythm
653    
654      RUN = true;
655    
656      SELLI = -1;
657    
658      ISGP = false;
659    
660      DBG = false;
661    
662      tsorted = 0;
663      timage = 0;
664      text = 0 ;
665    
666      tsorted_nuc = 0;
667      timage_nuc = 0;
668      text_nuc = 0 ;
669    
670      howtosort = "+CAL+TOF";
671      //howtosort = "+TOF";
672      sortthr = 100.;
673    
674      issorted = false;
675      lastsorted = -1;
676      issorted_new = false;
677      lastsorted_new = -1;
678    
679    }
680    ;
681  /**  /**
682   * Delete the event (act as Dtor)   * Delete the event (act as Dtor)
683   */   */
684  void PamLevel2::Delete(){  void PamLevel2::Delete() {
           
     if(run_obj)delete run_obj;  
     if(soft_obj)delete soft_obj; //Emiliano  
685    
686  //    cout << "void PamLevel2::Clear()"<<endl;    if (run_obj)
687      if(h0_obj)   delete h0_obj;      delete run_obj;
688      if(trk0_obj) delete trk0_obj;          if (soft_obj)
689      if(calo0_obj) delete calo0_obj;          delete soft_obj; //Emiliano
690      if(trk1_obj) delete trk1_obj;          if (proc_obj)
691      if(trk2_obj) delete trk2_obj;      delete proc_obj; //Emiliano
692      if(trkh_obj) delete trkh_obj;  
693      if(calo1_obj)delete calo1_obj;    //    cout << "void PamLevel2::Clear()"<<endl;
694      if(calo2_obj)delete calo2_obj;    if (h0_obj)
695      if(tof_obj)  delete tof_obj;      delete h0_obj;
696      if(trig_obj) delete trig_obj;    if (trk0_obj)
697      if(s4_obj)   delete s4_obj;      delete trk0_obj;
698      if(nd_obj)   delete nd_obj;    if (calo0_obj)
699      if(ac_obj)   delete ac_obj;      delete calo0_obj;
700      if(orb_obj)  delete orb_obj;    if (trk1_obj)
701            delete trk1_obj;
702      if(tsorted){    if (trk2_obj)
703          tsorted->Delete();      delete trk2_obj;
704          delete tsorted;    if (trkh_obj)
705      }      delete trkh_obj;
706      if(timage){    if (calo1_obj)
707          timage->Delete();      delete calo1_obj;
708          delete timage;    if (calo2_obj)
709      }      delete calo2_obj;
710      if (tof2_obj)
711        delete tof2_obj;
712      if (trig_obj)
713        delete trig_obj;
714      if (s4_obj)
715        delete s4_obj;
716      if (nd_obj)
717        delete nd_obj;
718      if (ac_obj)
719        delete ac_obj;
720      if (orb2_obj)
721        delete orb2_obj;
722      if (gp_obj)
723        delete gp_obj;
724    
725      if(trk_nuc_obj)trk_nuc_obj->Delete();
726      if(trk_ext_obj)trk_ext_obj->Delete();
727      if(trk_ext_nuc_obj)trk_ext_nuc_obj->Delete();
728    
729      if(calo_nuc_obj)calo_nuc_obj->Delete();
730      if(calo_ext_obj)calo_ext_obj->Delete();
731      if(calo_ext_nuc_obj)calo_ext_nuc_obj->Delete();
732    
733      if(tof_nuc_obj)tof_nuc_obj->Delete();
734      if(tof_ext_obj)tof_ext_obj->Delete();
735      if(tof_ext_nuc_obj)tof_ext_nuc_obj->Delete();
736    
737      if(orb_nuc_obj)orb_nuc_obj->Delete();
738      if(orb_ext_obj)orb_ext_obj->Delete();
739      if(orb_ext_nuc_obj)orb_ext_nuc_obj->Delete();
740    
741    
742      if(trk2_nuc_obj)trk2_nuc_obj->Delete();;
743      if( calo2_nuc_obj)calo2_nuc_obj->Delete();;
744      if(tof2_nuc_obj)tof2_nuc_obj->Delete();;
745      if(orb2_nuc_obj)orb2_nuc_obj->Delete();;
746      
747    
     if(dbc){  
         dbc->Close();  
         delete dbc;  
     }  
       
     if(l0_file)l0_file->Close();  
     //    if(pam_tree)pam_tree->Delete();;  
748    
749    if ( pam_tree ){    if (tsorted) {
750        tsorted->Delete();
751        delete tsorted;
752      }
753      if (timage) {
754        timage->Delete();
755        delete timage;
756      }
757      if (text) {
758        text->Delete();
759        delete text;
760      }
761      if (tsorted_nuc) {
762        tsorted_nuc->Delete();
763        delete tsorted_nuc;
764      }
765      if (timage_nuc) {
766        timage_nuc->Delete();
767        delete timage_nuc;
768      }
769      if (text_nuc) {
770        text_nuc->Delete();
771        delete text_nuc;
772      }
773    
774    
775    
776      if (dbc) {
777        dbc->Close();
778        delete dbc;
779        dbc=0;
780      }
781    
782      if (gltsync)
783        delete gltsync;
784    
785      if (l0_file)
786        l0_file->Close();
787      //    if(pam_tree)pam_tree->Delete();;
788    
789      if (pam_tree) {
790      //      //
791      // 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...      // 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...
792      //      //
793      TList *temp = pam_tree->GetListOfFriends();      TList *temp = pam_tree->GetListOfFriends();
794      TList *contents  = new TList; // create chain friend list      TList *contents = new TList; // create chain friend list
795      contents->SetOwner();      contents->SetOwner();
796      TIter next(temp);      TIter next(temp);
797      TChain *questo = 0;      TChain *questo = 0;
798      while ( (questo = (TChain*) next()) ){      while ((questo = (TChain*) next())) {
799        TString name =  questo->GetName();        TString name = questo->GetName();
800        contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list        contents->Add((TChain*) gROOT->FindObject(name.Data()));// add object to the list
801      };      };
802      //      //
803      // deleting the main chain      // deleting the main chain
# Line 267  void PamLevel2::Delete(){ Line 808  void PamLevel2::Delete(){
808      //      //
809      TIter next2(contents);      TIter next2(contents);
810      TChain *questa = 0;      TChain *questa = 0;
811      while ( questa = (TChain*)next2() ){      while ( (questa = (TChain*)next2()) ) {
812        TString name =  questa->GetName();        TString name = questa->GetName();
813        questa->Delete();              questa->Delete();
814        questa=NULL;        questa = NULL;
815      };      };
816      //      //
817    };    };
818    pam_tree = NULL;    pam_tree = NULL;
819    
820      if(run_tree)run_tree->Delete();;    if (run_tree)
821      if(sel_tree)sel_tree->Delete();;      run_tree->Delete();;
822      for(Int_t i=0; i<NCLONES; i++ )if(pam_tree_clone[i])pam_tree_clone[i]->Delete();;    if (sel_tree)
823      if(run_tree_clone)run_tree_clone->Delete();;      sel_tree->Delete();;
824      if(sel_tree_clone)sel_tree_clone->Delete();;  
825          // The following lines are commented out since they may generate a double delete error
826      // if the file containing the clone trees is closed. This is because the file owns the
827      // clone trees which are written into it, so it will delete them when it is closed; if
828      // also PamLevel2 will try to delete these trees, a double delete error will be generated
829      // when exiting from analysis program. (Nicola 28/11/2011)
830    
831      /*for (Int_t i = 0; i < NCLONES; i++)
832        if (pam_tree_clone[i])
833          pam_tree_clone[i]->Delete();;
834      if (run_tree_clone)
835        run_tree_clone->Delete();;
836      if (sel_tree_clone)
837        sel_tree_clone->Delete();;*/
838    
839  };    if (irunoffset)
840        delete[] irunoffset;
841    
842    
843      Initialize();
844    
845    }
846    ;
847    
848  /**  /**
849   * Clear the event (NB! does not deallocate objects)   * Clear the event (NB! does not deallocate objects)
850   */   */
851  void PamLevel2::Clear(){  void PamLevel2::Clear() {
           
 //    cout << "void PamLevel2::Clear()"<<endl;  
852    
853  //    //    cout << "void PamLevel2::Clear()"<<endl;
 // 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  
 // want to load them for each event even if they are the same...  
 //  
 //    if(run_obj)delete run_obj;  
 //    if(run_obj) run_obj->Clear();  // Emiliano: Do not deallocate run_obj here, it will give segmentation fault! call clear instead  
 //    if(soft_obj) soft_obj->Clear();  
   
     if(h0_obj)   h0_obj->Clear();        
 //    if(trk0_obj) trk0_obj->Clear();    
     if(trk1_obj) trk1_obj->Clear();      
     if(trk2_obj) trk2_obj->Clear();  
     if(trkh_obj) trkh_obj->Clear();  
     if(calo0_obj) calo0_obj->Clear();    
     if(calo1_obj)calo1_obj->Clear();  
     if(calo2_obj)calo2_obj->Clear();  
     if(tof_obj)  tof_obj->Clear();  
     if(trig_obj) trig_obj->Clear();  
     if(s4_obj)   s4_obj->Clear();  
     if(nd_obj)   nd_obj->Clear();  
     if(ac_obj)   ac_obj->Clear();  
     if(orb_obj)  orb_obj->Clear();  
       
 //    if(sorted_tracks)sorted_tracks->Clear();  
 //    sorted_tracks.Clear();  
854    
855      if(tsorted){    //
856          tsorted->Delete();    // 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
857      }    // want to load them for each event even if they are the same...
858      if(timage){    //
859          timage->Delete();    //    if(run_obj)delete run_obj;
860      }    //    if(run_obj) run_obj->Clear();  // Emiliano: Do not deallocate run_obj here, it will give segmentation fault! call clear instead
861  };    //    if(soft_obj) soft_obj->Clear();
862    
863      if (h0_obj)
864        h0_obj->Clear();
865      //    if(trk0_obj) trk0_obj->Clear();
866      if (trk1_obj)
867        trk1_obj->Clear();
868      if (trk2_obj)
869        trk2_obj->Clear();
870      if (trkh_obj)
871        trkh_obj->Clear();
872      if (calo0_obj)
873        calo0_obj->Clear();
874      if (calo1_obj)
875        calo1_obj->Clear();
876      if (calo2_obj)
877        calo2_obj->Clear();
878      if (tof2_obj)
879        tof2_obj->Clear();
880      if (trig_obj)
881        trig_obj->Clear();
882      if (s4_obj)
883        s4_obj->Clear();
884      if (nd_obj)
885        nd_obj->Clear();
886      if (ac_obj)
887        ac_obj->Clear();
888      if (orb2_obj)
889        orb2_obj->Clear();
890      if (gp_obj)
891        gp_obj->Clear();
892      if (proc_obj)
893        proc_obj->Clear();
894    
895      //    if(sorted_tracks)sorted_tracks->Clear();
896      //    sorted_tracks.Clear();
897    
898      if(trk_nuc_obj)trk_nuc_obj->Clear();
899      if(trk_ext_obj)trk_ext_obj->Clear();
900      if(trk_ext_nuc_obj)trk_ext_nuc_obj->Clear();
901    
902      if(calo_nuc_obj)calo_nuc_obj->Clear();
903      if(calo_ext_obj)calo_ext_obj->Clear();
904      if(calo_ext_nuc_obj)calo_ext_nuc_obj->Clear();
905    
906      if(tof_nuc_obj)tof_nuc_obj->Clear();
907      if(tof_ext_obj)tof_ext_obj->Clear();
908      if(tof_ext_nuc_obj)tof_ext_nuc_obj->Clear();
909    
910      if(orb_nuc_obj)orb_nuc_obj->Clear();
911      if(orb_ext_obj)orb_ext_obj->Clear();
912      if(orb_ext_nuc_obj)orb_ext_nuc_obj->Clear();
913    
914      if(trk2_nuc_obj)trk2_nuc_obj->Clear();;
915      if( calo2_nuc_obj)calo2_nuc_obj->Clear();;
916      if(tof2_nuc_obj)tof2_nuc_obj->Clear();;
917      if(orb2_nuc_obj)orb2_nuc_obj->Clear();;
918    
919      if (tsorted)tsorted->Delete();
920      if (timage)timage->Delete();
921      if (text) text->Delete();
922    
923      if (tsorted_nuc)tsorted_nuc->Delete();
924      if (timage_nuc)timage_nuc->Delete();
925      if (text_nuc) text_nuc->Delete();
926    }
927    ;
928    
929  void PamLevel2::Reset(){  void PamLevel2::Reset() {
930    //    //
931    // First of all clear everything    // First of all clear everything
932    //    //
933    Clear();    Clear();
934    //    //
935    // close and reset chains and pointers    // close and reset chains and pointers
936    //        //
937    if ( pam_tree ){    if (pam_tree) {
938      //      //
939      // 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...      // 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...
940      //      //
941      TList *temp = pam_tree->GetListOfFriends();      TList *temp = pam_tree->GetListOfFriends();
942      TList *contents  = new TList; // create chain friend list      TList *contents = new TList; // create chain friend list
943      contents->SetOwner();      contents->SetOwner();
944      TIter next(temp);      TIter next(temp);
945      TChain *questo = 0;      TChain *questo = 0;
946      while ( (questo = (TChain*) next()) ){      while ((questo = (TChain*) next())) {
947        TString name =  questo->GetName();        TString name = questo->GetName();
948        contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list        contents->Add((TChain*) gROOT->FindObject(name.Data()));// add object to the list
949      };      };
950      //      //
951      // deleting the main chain      // deleting the main chain
# Line 356  void PamLevel2::Reset(){ Line 956  void PamLevel2::Reset(){
956      //      //
957      TIter next2(contents);      TIter next2(contents);
958      TChain *questa = 0;      TChain *questa = 0;
959      while ( questa = (TChain*)next2() ){      while ( (questa = (TChain*) next2()) ) {
960        TString name =  questa->GetName();        TString name = questa->GetName();
961        questa->Delete();              questa->Delete();
962        questa=NULL;        questa = NULL;
963      };      };
964      //      //
965    };    };
966    pam_tree = NULL;    pam_tree = NULL;
967    //    //
968    if(run_tree)run_tree->Delete();;    if (run_tree)
969        run_tree->Delete();;
970    run_tree = NULL;    run_tree = NULL;
971    if(sel_tree)sel_tree->Delete();;    if (sel_tree)
972        sel_tree->Delete();;
973    sel_tree = NULL;    sel_tree = NULL;
974    
975      if (proc_tree)
976        proc_tree->Delete();
977      proc_tree = NULL;
978    //    //
979    // Close file    // Close file
980    //    //
981    if(l0_file)l0_file->Close("R");    if (l0_file)
982        l0_file->Close("R");
983    l0_file = NULL;    l0_file = NULL;
984    //    //
985    h0_obj    = 0;    h0_obj = 0;
986    trk0_obj  = 0;    trk0_obj = 0;
987    calo0_obj  = 0;    calo0_obj = 0;
988    //    //
989    trk2_obj  = 0;    trk2_obj = 0;
990    trk1_obj  = 0;    trk1_obj = 0;
991    trkh_obj  = 0;    trkh_obj = 0;
992    calo1_obj = 0;    calo1_obj = 0;
993    calo2_obj = 0;    calo2_obj = 0;
994    tof_obj   = 0;    tof2_obj = 0;
995    trig_obj  = 0;    trig_obj = 0;
996    s4_obj    = 0;    s4_obj = 0;
997    nd_obj    = 0;    nd_obj = 0;
998    ac_obj    = 0;    ac_obj = 0;
999    orb_obj   = 0;    orb2_obj = 0;
1000      gp_obj = 0;
1001      proc_obj = 0;
1002    
1003      trk_ext_obj     = 0;
1004      trk_ext_nuc_obj = 0;
1005      trk_nuc_obj     = 0;
1006      
1007      calo_ext_obj     = 0;
1008      calo_ext_nuc_obj = 0;
1009      calo_nuc_obj     = 0;
1010      
1011      tof_ext_obj     = 0;
1012      tof_ext_nuc_obj = 0;
1013      tof_nuc_obj     = 0;
1014      
1015      orb_ext_obj     = 0;
1016      orb_ext_nuc_obj = 0;
1017      orb_nuc_obj     = 0;
1018    
1019      trk2_nuc_obj  = 0;
1020      calo2_nuc_obj = 0;
1021      tof2_nuc_obj  = 0;
1022      orb2_nuc_obj  = 0;
1023    
1024      trk2_nuc_obj  = 0;
1025      calo2_nuc_obj = 0;
1026      tof2_nuc_obj  = 0;
1027      orb2_nuc_obj  = 0;
1028    //    //
1029    // Reset run pointers    // Reset run pointers
1030    //    //
1031    run_obj   = 0;//new GL_RUN();    run_obj = 0;//new GL_RUN();
1032    soft_obj   = 0;// Emiliano    soft_obj = 0;// Emiliano
1033      proc_obj = 0;// Emiliano
1034    irun = -1;    irun = -1;
1035      irunt = -1;
1036      totrunentry = 0LL;
1037      totrunentrymax = 0LL;
1038      totrunentrymin = 0LL;
1039    runfirstentry = 0ULL;    runfirstentry = 0ULL;
1040    runlastentry = 0ULL;        runlastentry = 0ULL;
1041      prevabstime = 0ULL;
1042      prevpktnum = 0;
1043      abstime = 0ULL;
1044      pktnum = 0;
1045      isFragment = false;
1046    //    //
1047    totdltime[0] = 0LL;    totdltime[0] = 0LL;
1048    totdltime[1] = 0LL;    totdltime[1] = 0LL;
1049      totdltime[2] = 0LL;
1050    //    //
1051  };  }
1052    ;
1053    
1054  Bool_t PamLevel2::IsGood(){  Bool_t PamLevel2::IsGood(Bool_t strict) {
1055    Bool_t goodev=true;    Bool_t goodev = true;
1056    //  if(trk2_obj && trk2_obj->UnpackError() != 0 ) goodev = false;    //
1057    if(calo2_obj && calo2_obj->good != 1) goodev = false;    if (calo2_obj && !calo2_obj->IsGood(strict))
1058    if(tof_obj && tof_obj->unpackError != 0) goodev = false;        goodev = false;
1059    if(trig_obj && trig_obj->unpackError != 0) goodev = false;    //
1060    if(s4_obj && s4_obj->unpackError != 0) goodev = false;      if (strict) {
1061    if(nd_obj && nd_obj->unpackError != 0) goodev = false;        if (trk2_obj && trk2_obj->UnpackError() != 0)
1062    if(ac_obj && ac_obj->unpackError != 255) goodev = false;          goodev = false;
1063      //  if(orb_obj)        if (tof2_obj && tof2_obj->unpackError != 0)
1064    return goodev;        goodev = false;
1065  };      if (trig_obj && trig_obj->unpackError != 0)
1066          goodev = false;
1067        if (s4_obj && s4_obj->unpackError != 0)
1068          goodev = false;
1069        if (nd_obj && nd_obj->unpackError != 0)
1070          goodev = false;
1071        if (ac_obj && (ac_obj->unpackError != 0 || ((ac_obj->status[0] >> 2) & 1) || ((ac_obj->status[1] >> 2) & 1)))
1072          goodev = false;
1073        //  if(orb2_obj)
1074      }
1075      else {
1076        if (nd_obj && nd_obj->unpackError != 0)
1077          goodev = false;
1078        if (ac_obj && (ac_obj->unpackError != 0 || ((ac_obj->status[0] >> 2) & 1) || ((ac_obj->status[1] >> 2) & 1)))
1079          goodev = false;
1080      };
1081      return (goodev);
1082    }
1083    ;
1084    
1085    void PamLevel2::SkipRunInfoUpdate(){
1086      printf("\n\n ******** WARNING ******** \n Skip DB connections, DO NOT USE PamLevel2::GetRunInfo() method! \n\n");
1087      fUpdateRunInfo = false;
1088      this->SetSELLI(2);
1089      printf(" ===============> W A R N I N G <================ \n");
1090      printf(" in case PamLevel2::CreateCloneTrees() will be called \n");
1091      printf(" it will be reverted to PadmeAmidala level2 structure , i.e. NO SELECTIONLIST WILL BE CREATED IN THE NEW LEVEL2 FILE! \n\n");
1092      if ( run_tree_clone ){
1093        printf(" ===============> W A R N I N G <================ \n");
1094        printf(" PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() has been called together with PamLevel2::CreateCloneTrees() \n");
1095        printf(" TO AVOID CRASHES call PamLevel2::CreateCloneTrees() after PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() \n");    
1096      };
1097    }
1098    
1099    void PamLevel2::SetMaxShift(Int_t sh){
1100      if ( sh >=  0 ){
1101        printf("PamLevel2::SetMaxShift(Int_t) --WARNING-- the default is optimized by checking the level2 file\n it is strongly suggested to let PamLevel2 choose the max shift!\n");
1102        maxshift = sh;
1103      } else {
1104        ULong64_t nev = GetEntries();
1105        ULong64_t runnev = 0ULL;
1106        for (Int_t r=0; r< run_tree->GetEntries();r++){
1107          run_tree->GetEntry(r);//update runinfo
1108          runnev += GetRunInfo()->NEVENTS;
1109        }
1110        maxshift = (Int_t)(runnev-nev) + 10; // +10 just to be conservative
1111        if ( (runnev-nev) == 0 ) isSync = true;
1112        if (DBG) printf("PamLevel2::SetMaxShift(Int_t_) - sh negative %i - nev is %lld runnnev is %lld so maxshift set to %i \n",sh,nev,runnev,maxshift);
1113        //    printf("PamLevel2::SetMaxShift(Int_t_) - sh negative %i - nev is %lld runnnev is %lld so maxshift set to %i \n",sh,nev,runnev,maxshift); // TOGLITOGLI
1114      }
1115    }
1116    
1117  //--------------------------------------  //--------------------------------------
1118  //  //
1119  //  //
1120  //--------------------------------------  //--------------------------------------
1121  void *PamLevel2::GetPointerTo(const char* c ){  void *PamLevel2::GetPointerTo(const char* c) {
1122    
1123      TString objname = c;    TString objname = c;
1124    
1125      if(!objname.CompareTo("TrkLevel1"))  {    if (!objname.CompareTo("TrkLevel1")) {
1126          if(!trk1_obj){      if (!trk1_obj) {
1127              trk1_obj  = new TrkLevel1();        trk1_obj = new TrkLevel1();
1128              trk1_obj->Set();        trk1_obj->Set();
1129          }      }
1130          return &trk1_obj;      return &trk1_obj;
1131      };    };
1132      if(!objname.CompareTo("TrkLevel2"))  {    if (!objname.CompareTo("TrkLevel2")) {
1133          if(!trk2_obj){        if (!trk2_obj) {
1134              trk2_obj  = new TrkLevel2();        trk2_obj = new TrkLevel2();
1135              trk2_obj->Set();        trk2_obj->Set();
1136          }      }
1137          return &trk2_obj;      return &trk2_obj;
1138      };    };
1139      if(!objname.CompareTo("TrkHough"))   {    if (!objname.CompareTo("TrkHough")) {
1140          if(!trkh_obj)  trkh_obj  = new TrkHough();      if (!trkh_obj) {
1141          return &trkh_obj;        trkh_obj = new TrkHough();
1142      };        trkh_obj->Set();
1143      if(!objname.CompareTo("CaloLevel1")) {      }
1144          if(!calo1_obj) calo1_obj = new CaloLevel1();      return &trkh_obj;
1145          return &calo1_obj;    };
1146      };    if (!objname.CompareTo("CaloLevel1")) {
1147      if(!objname.CompareTo("CaloLevel2")) {      if (!calo1_obj)
1148          if(!calo2_obj){        calo1_obj = new CaloLevel1();
1149              calo2_obj = new CaloLevel2();      return &calo1_obj;
1150              calo2_obj->Set();    };
1151          };    if (!objname.CompareTo("CaloLevel2")) {
1152          return &calo2_obj;      if (!calo2_obj) {
1153      };        calo2_obj = new CaloLevel2();
1154      if(!objname.CompareTo("ToFLevel2"))  {        calo2_obj->Set();
         if(!tof_obj){  
             tof_obj   = new ToFLevel2();  
             tof_obj->Set();  
         }  
         return &tof_obj;  
     };  
     if(!objname.CompareTo("TrigLevel2")) {  
         if(!trig_obj)  trig_obj  = new TrigLevel2();  
         return &trig_obj;  
     };  
     if(!objname.CompareTo("S4Level2"))   {  
         if(!s4_obj)    s4_obj    = new S4Level2();  
         return &s4_obj;  
     };  
     if(!objname.CompareTo("NDLevel2"))   {  
         if(!nd_obj)    nd_obj    = new NDLevel2();  
         return &nd_obj;  
     };  
     if(!objname.CompareTo("AcLevel2"))   {  
         if(!ac_obj)    ac_obj    = new AcLevel2();  
         return &ac_obj;  
     };  
     if(!objname.CompareTo("OrbitalInfo")){  
         if(!orb_obj)   orb_obj   = new OrbitalInfo();  
         return &orb_obj;  
1155      };      };
1156            return &calo2_obj;
1157      if(!objname.CompareTo("RunInfo"))return &run_obj;    };
1158      if (!objname.CompareTo("ToFLevel2")) {
1159        if (!tof2_obj) {
1160          tof2_obj = new ToFLevel2();
1161          tof2_obj->Set();
1162        }
1163        return &tof2_obj;
1164      };
1165      if (!objname.CompareTo("TrigLevel2")) {
1166        if (!trig_obj)
1167          trig_obj = new TrigLevel2();
1168        return &trig_obj;
1169      };
1170      if (!objname.CompareTo("S4Level2")) {
1171        if (!s4_obj)
1172          s4_obj = new S4Level2();
1173        return &s4_obj;
1174      };
1175      if (!objname.CompareTo("NDLevel2")) {
1176        if (!nd_obj)
1177          nd_obj = new NDLevel2();
1178        return &nd_obj;
1179      };
1180      if (!objname.CompareTo("AcLevel2")) {
1181        if (!ac_obj)
1182          ac_obj = new AcLevel2();
1183        return &ac_obj;
1184      };
1185      if (!objname.CompareTo("OrbitalInfo")) {
1186        if (!orb2_obj) {
1187          orb2_obj = new OrbitalInfo();
1188          orb2_obj->Set();
1189        }
1190        return &orb2_obj;
1191      };
1192      //     if(!objname.CompareTo("OrbitalInfo")){
1193      //    if(!orb2_obj)   orb2_obj   = new OrbitalInfo();
1194      //    return &orb2_obj;
1195      //     };
1196      if (!objname.CompareTo("GPamela")) {
1197        if (!gp_obj)
1198          gp_obj = new GPamela();
1199        return &gp_obj;
1200      };
1201    
1202      if(!objname.CompareTo("SoftInfo"))return &soft_obj; // Emiliano    if (!objname.CompareTo("RunInfo"))
1203        return &run_obj;
1204    
1205      return NULL;    if (!objname.CompareTo("SoftInfo"))
1206  };      return &soft_obj; // Emiliano
1207    
1208      if (!objname.CompareTo("ProcInfo")){
1209        if (!proc_obj)
1210          proc_obj = new ProcInfo();    
1211        return &proc_obj; // Emiliano
1212      }
1213    
1214      return NULL;
1215    }
1216    ;
1217  //--------------------------------------  //--------------------------------------
1218  //  //
1219  //  //
1220  //--------------------------------------  //--------------------------------------
1221  /**  /**
1222   * Retrieves the calorimeter track matching the seqno-th tracker stored track.   * Retrieves the calorimeter track matching the seqno-th tracker stored track.
1223   * (If seqno = -1 retrieves the self-trigger calorimeter track)   * (If seqno = -1 retrieves the self-trigger calorimeter track)
1224   */   */
1225   CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno){  CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno) {
1226        
1227       if( !calo2_obj )return 0;    if (!calo2_obj)
1228        return 0;
1229    
1230      if (calo2_obj->CaloLevel2::ntrk() == 0) {
1231        cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo " << seqno
1232            << " but no Calorimeter tracks are stored" << endl;
1233        return NULL;
1234      };
1235    
1236      CaloTrkVar *c = 0;
1237      Int_t it_calo = 0;
1238    
1239      do {
1240        c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);
1241        it_calo++;
1242      } while (c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());
1243    
1244      if (!c || seqno != c->trkseqno) {
1245        c = 0;
1246        if (seqno != -1)
1247          cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo " << seqno
1248              << " does not match Calorimeter stored tracks" << endl;
1249      };
1250      return c;
1251    
1252    }
1253    ;
1254    //--------------------------------------
1255    //
1256    //
1257    //--------------------------------------
1258    /**
1259     * Retrieves the ToF track matching the seqno-th tracker stored track.
1260     * (If seqno = -1 retrieves the tracker-independent tof track)
1261     */
1262    ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno) {
1263    
1264      if (!tof2_obj)
1265        return 0;
1266    
1267      if (tof2_obj->ToFLevel2::ntrk() == 0) {
1268        cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo " << seqno << " but no ToF tracks are stored"
1269            << endl;
1270        return NULL;
1271      };
1272    
1273      ToFTrkVar *c = 0;
1274      Int_t it_tof = 0;
1275    
1276      do {
1277        c = tof2_obj->ToFLevel2::GetToFTrkVar(it_tof);
1278        it_tof++;
1279      } while (c && seqno != c->trkseqno && it_tof < tof2_obj->ToFLevel2::ntrk());
1280    
1281      if (!c || seqno != c->trkseqno) {
1282        c = 0;
1283        if (seqno != -1)
1284          cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo " << seqno
1285              << " does not match ToF stored tracks" << endl;
1286      };
1287      return c;
1288    
1289    }
1290    ;
1291    
      if( calo2_obj->CaloLevel2::ntrk()==0 ){  
          cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no Calorimeter tracks are stored"<<endl;  
          return NULL;  
      };  
       
      CaloTrkVar *c = 0;  
      Int_t it_calo=0;  
       
      do{  
          c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);  
          it_calo++;  
      } while( c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());      
       
      if(!c || seqno != c->trkseqno){  
          c = 0;  
          if(seqno!=-1)cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match Calorimeter stored tracks"<<endl;  
      };  
      return c;  
       
  };  
1292  //--------------------------------------  //--------------------------------------
1293   //  //
1294   //  //
1295  //--------------------------------------  //--------------------------------------
1296  /**  /**
1297    * Retrieves the ToF track matching the seqno-th tracker stored track.   * Retrieves the OrbitalInfo track matching the seqno-th tracker stored track.
1298    * (If seqno = -1 retrieves the tracker-independent tof track)   * (If seqno = -1 retrieves the tracker-independent tof related track)
1299   */   */
1300   ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno){  OrbitalInfoTrkVar *PamLevel2::GetOrbitalInfoStoredTrack(int seqno) {
           
      if( !tof_obj )return 0;  
1301    
1302       if( tof_obj->ToFLevel2::ntrk()==0 ){    if (!orb2_obj)
1303           cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no ToF tracks are stored"<<endl;      return 0;
1304           return NULL;  
1305       };    if (orb2_obj->OrbitalInfo::ntrk() == 0) {
1306            //       // TRICK BEGIN
1307       ToFTrkVar *c = 0;      //       OrbitalInfoTrkVar  *r = new OrbitalInfoTrkVar(); // TEMPORARY TRICK
1308       Int_t it_tof=0;      //       Int_t nn = 0;
1309            //       TClonesArray &tor = *orb2_obj->OrbitalInfoTrk;
1310       do{      //       for(Int_t nt=0; nt < tof2_obj->ToFLevel2::ntrk(); nt++){
1311           c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof);      //  //
1312           it_tof++;      //  ToFTrkVar *ptt = tof2_obj->ToFLevel2::GetToFTrkVar(nt);
1313       } while( c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk());              //  if ( ptt->trkseqno != -1  ){
1314            //    //
1315       if(!c || seqno != c->trkseqno){      //    r->trkseqno = ptt->trkseqno;
1316           c = 0;      //    //
1317           if(seqno!=-1)cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match ToF stored tracks"<<endl;      //    r->Eij = 0;
1318       };      //    //
1319       return c;      //    r->Sij = 0;
1320            //    //
1321   };      //    r->pitch = -1000.;
1322        //    //
1323        //    r->cutoff = -1000.;
1324        //    //
1325        //    new(tor[nn]) OrbitalInfoTrkVar(*r);
1326        //    nn++;
1327        //    //
1328        //    r->Clear();
1329        //    //
1330        //  };
1331        //       };
1332        //       delete r;
1333        //       OrbitalInfoTrkVar *c = 0;
1334        //       c = orb2_obj->OrbitalInfo::GetOrbitalInfoTrkVar(0);
1335        //       return c;
1336        //       //TRICK END
1337        cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo " << seqno
1338            << " but no OrbitalInfo tracks are stored" << endl;
1339        return NULL;
1340      };
1341    
1342      OrbitalInfoTrkVar *c = 0;
1343      Int_t it_tof = 0;
1344    
1345      do {
1346        c = orb2_obj->OrbitalInfo::GetOrbitalInfoTrkVar(it_tof);
1347        it_tof++;
1348      } while (c && seqno != c->trkseqno && it_tof < orb2_obj->OrbitalInfo::ntrk());
1349    
1350      if (!c || seqno != c->trkseqno) {
1351        c = 0;
1352        if (seqno != -1)
1353          cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo " << seqno
1354              << " does not match OrbitalInfo stored tracks" << endl;
1355      };
1356      return c;
1357    
1358    }
1359    ;
1360    
1361  //--------------------------------------  //--------------------------------------
1362   //  //
1363   //  //
1364  //--------------------------------------  //--------------------------------------
1365  /**  /**
1366   * Give the pamela track associated to a tracker track, retrieving related calorimeter and tof track information.   * Give the pamela track associated to a tracker track, retrieving related calorimeter, orbitalinfo and tof track information.
1367   */   */
1368   PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t){  PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t) {
       
      cout <<"PamLevel2::GetPamTrackAlong(TrkTrack* t) **obsolete** "<<endl;  
      cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;  
1369    
1370       CaloTrkVar *c = 0;    cout << "PamLevel2::GetPamTrackAlong(TrkTrack* t) **obsolete** " << endl;
1371       ToFTrkVar  *o = 0;    cout << "(if you use it, remember to delete the PamTrack object)" << endl;
1372        
1373       if(CAL2) c = GetCaloStoredTrack(t->GetSeqNo());    CaloTrkVar *c = 0;
1374       if(TOF) o = GetToFStoredTrack(t->GetSeqNo());    ToFTrkVar *o = 0;
1375          OrbitalInfoTrkVar *r = 0;
1376  //    if(t && c && o)track = new PamTrack(t,c,o);  
1377       PamTrack *track = new PamTrack(t,c,o);    if (CAL2)
1378            c = GetCaloStoredTrack(t->GetSeqNo());
1379       return track;    if (TOF)
1380        o = GetToFStoredTrack(t->GetSeqNo());
1381   };    if (ORB)
1382        r = GetOrbitalInfoStoredTrack(t->GetSeqNo());
1383    
1384      //    if(t && c && o)track = new PamTrack(t,c,o);
1385      PamTrack *track = new PamTrack(t, c, o, r);
1386    
1387      return track;
1388    
1389    }
1390    ;
1391  //--------------------------------------  //--------------------------------------
1392  //  //
1393  //  //
1394  //--------------------------------------  //--------------------------------------
1395  /**  /**
1396   * Retrieves the it-th stored track.   * Retrieves the it-th stored track.
1397   * It override TrkLevel2::GetTrack(int it).   * It override TrkLevel2::GetTrack(int it).
1398   * @param itrk Track number, ranging from 0 to GetNTracks().   * @param itrk Track number, ranging from 0 to GetNTracks().
1399   */   */
1400    
1401  PamTrack* PamLevel2::GetStoredTrack(Int_t itrk){  PamTrack* PamLevel2::GetStoredTrack(Int_t itrk) {
1402        
1403      cout <<"PamLevel2::GetStoredTrack(Int_t itrk) **to-be-updated** "<<endl;    cout << "PamLevel2::GetStoredTrack(Int_t itrk) **to-be-updated** " << endl;
1404      cout <<"for the moment, better use separately the methods: TrkLevel2::GetStoredTrack(seqno) CaloLevel2::GetCaloTrkVar(Int_t notrack) ToFLevel2::GetToFTrkVar(Int_t notrack)"<<endl;    cout
1405      cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;        << "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)"
1406      PamTrack *track = 0;        << endl;
1407          cout << "(if you use it, remember to delete the PamTrack object)" << endl;
1408      if( itrk >=0 && itrk < trk2_obj->TrkLevel2::ntrk() ){    PamTrack *track = 0;
1409            
1410          TrkTrack *t = trk2_obj->TrkLevel2::GetStoredTrack(itrk);    if (itrk >= 0 && itrk < trk2_obj->TrkLevel2::ntrk()) {
1411          track = GetPamTrackAlong(t);  
1412                TrkTrack *t = trk2_obj->TrkLevel2::GetStoredTrack(itrk);
1413                track = GetPamTrackAlong(t);
1414    
1415      }
1416      else {
1417        cout << "PamLevel2::GetStoredTrack(int) : tracker track SeqNo " << itrk << " does not exist (GetNTracks() = "
1418            << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
1419      };
1420    
1421      return track;
1422    
     }else{  
         cout << "PamLevel2::GetStoredTrack(int) : tracker track SeqNo "<< itrk <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;  
     };  
       
     return track;  
       
1423  }  }
1424  //--------------------------------------  //--------------------------------------
1425  //  //
1426    
1427  /**  /**
1428   * Sort physical (tracker) tracks and stores them in the TRefArray (of TrkTrack objects) which pointer is  PamLevel2::sorted_tracks. Left here as backward compatibility method.   * Sort physical (tracker) tracks. Left here as backward compatibility method.
1429   **/   **/
1430  void PamLevel2::SortTracks(TString how){  void PamLevel2::SortTracks(TString how) {
1431    printf(" WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",how.Data());    printf(
1432    howtosort = how;          " WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",
1433          how.Data());
1434      howtosort = how;
1435    SortTracks();    SortTracks();
1436  };  }
1437    ;
1438    
1439  //  //
1440  //--------------------------------------  //--------------------------------------
1441  /**  /**
1442   * Sort physical (tracker) tracks and stores them in the TRefArray (of TrkTrack objects) which pointer is  PamLevel2::sorted_tracks.   * Sort physical (tracker) tracks.
1443   * @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...).
1444   * Sorting cryteria:   * Sorting cryteria:
1445   * TRK: lower chi**2   * TRK: lower chi**2
1446   * CAL: lower Y spatial residual on the first calorimeter plane   * CAL: lower Y spatial residual on the first calorimeter plane
1447   * 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).
1448     * S1: (ask Emiliano)
1449     * S2: (ask Emiliano)
1450     * S3: (ask Emiliano)
1451     * GP: more GP hits
1452   * The default sorting cryterium is "TOF+CAL".   * The default sorting cryterium is "TOF+CAL".
1453   *   *
1454   * 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).
1455   */   */
1456  void PamLevel2::SortTracks(){  void PamLevel2::SortTracks() {
1457    TString how = howtosort;  
1458      //Check if the current event has already been sorted
1459      if (issorted && lastsorted == GetReadEntry()) {
1460        return;
1461      }
1462      //Reset the sort flags, just in case something will go wrong...
1463      issorted = false;
1464      lastsorted = -1;
1465    
1466      TString how = howtosort;
1467    
1468    //  cout <<" PamLevel2::SortTracks(TString how) "<<endl;    //  cout <<" PamLevel2::SortTracks(TString how) "<<endl;
1469    if( !trk2_obj ){    if (!trk2_obj) {
1470      cout << "void PamLevel2::SortTracks():  TrkLevel2 not loaded !!!";      cout << "void PamLevel2::SortTracks():  TrkLevel2 not loaded !!!";
1471      return;      return;
1472    };    };
   //    cout << "call SortTracs() "<<endl;  
1473    //Save current Object count    //Save current Object count
1474    Int_t ObjectNumber = TProcessID::GetObjectCount();    Int_t ObjectNumber = TProcessID::GetObjectCount();
1475    
1476    //    cout << "ObjectNumber  "<<ObjectNumber <<endl;    // create TCloneArrays to store tracks and its images
1477      if (!tsorted)
1478    //     if(!sorted_tracks)sorted_tracks = new TRefArray();      tsorted = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
1479    //     sorted_tracks->Clear();    tsorted->Clear("C+C");//Delete();
   //    sorted_tracks.Clear();  
   
   if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());  
   tsorted->Delete();  
1480    TClonesArray &ttsorted = *tsorted;    TClonesArray &ttsorted = *tsorted;
1481    if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());  
1482    timage->Delete();    if (!timage)
1483        timage = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
1484      timage->Clear("C+C");//Delete();
1485    TClonesArray &ttimage = *timage;    TClonesArray &ttimage = *timage;
1486    
1487    
# Line 669  void PamLevel2::SortTracks(){ Line 1495  void PamLevel2::SortTracks(){
1495    Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase);    Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase);
1496    Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase);    Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase);
1497    Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase);    Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase);
1498        Bool_t use_GP = how.Contains("GP", TString::kIgnoreCase);
1499    if ( use_TOF ){  
1500      if (use_TOF) {
1501      use_S1 = true;      use_S1 = true;
1502      use_S2 = true;      use_S2 = true;
1503      use_S3 = true;      use_S3 = true;
1504    };    };
1505    if( !CAL2 &&  use_CAL) use_CAL = false;    if (!CAL2 && use_CAL)
1506    if( !TOF ){      use_CAL = false;
1507      if (!TOF) {
1508      use_TOF = false;      use_TOF = false;
1509      use_S1  = false;      use_S1 = false;
1510      use_S2  = false;      use_S2 = false;
1511      use_S3  = false;      use_S3 = false;
1512      }
1513      if (!GP) {
1514        use_GP = false;
1515    }    }
1516    
1517    if( !TRK2 ){    if (!TRK2) {
1518      //  cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;      cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
1519      return;      return;
1520    };    };
1521    
1522    //   cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;    //   cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;
1523      
1524    
1525    //--------------------------------------------------    //--------------------------------------------------
1526    // loop over "physical" tracks sorted by the tracker    // loop over "physical" tracks sorted by the tracker
1527    //--------------------------------------------------    //--------------------------------------------------
1528    for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){    for (Int_t i = 0; i < trk2_obj->TrkLevel2::GetNTracks(); i++) {
1529            
1530      TrkTrack   *ts = 0;      TrkTrack *ts = 0;
1531      CaloTrkVar *cs = 0;      CaloTrkVar *cs = 0;
1532      ToFTrkVar  *os = 0;      ToFTrkVar *os = 0;
1533                OrbitalInfoTrkVar *rs = 0;
1534    
1535      // get tracker tracks      // get tracker tracks
1536      TrkTrack   *tp = trk2_obj->TrkLevel2::GetTrack(i); //tracker      TrkTrack *tp = trk2_obj->TrkLevel2::GetTrack(i); //tracker
1537      CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());      CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());
1538      ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());      ToFTrkVar *op = GetToFStoredTrack(tp->GetSeqNo());
1539        OrbitalInfoTrkVar *rp = GetOrbitalInfoStoredTrack(tp->GetSeqNo());
1540    
1541      TrkTrack   *ti = 0; //tracker (image)      TrkTrack *ti = 0; //tracker (image)
1542      CaloTrkVar *ci = 0;      CaloTrkVar *ci = 0;
1543      ToFTrkVar  *oi = 0;      ToFTrkVar *oi = 0;
1544        OrbitalInfoTrkVar *ri = 0;
1545      //  cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;      //  cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
1546      // if track has an image, check image selection      // if track has an image, check image selection
1547    
1548      Int_t tp_score = 0;  //main track sorted by the tracker      Int_t tp_score = 0; //main track sorted by the tracker
1549      Int_t ti_score = 0;  //image track      Int_t ti_score = 0; //image track
1550      Int_t totp_score = 0;  //main track sorted by the tracker      Int_t totp_score = 0; //main track sorted by the tracker
1551      Int_t toti_score = 0;  //image track      Int_t toti_score = 0; //image track
1552    
1553      if(tp->HasImage()){      if (tp->HasImage()) {
1554                
1555        ti = trk2_obj->TrkLevel2::GetTrackImage(i);              //tracker (image)        ti = trk2_obj->TrkLevel2::GetTrackImage(i); //tracker (image)
1556        ci = GetCaloStoredTrack(ti->GetSeqNo());        ci = GetCaloStoredTrack(ti->GetSeqNo());
1557        oi = GetToFStoredTrack(ti->GetSeqNo());        oi = GetToFStoredTrack(ti->GetSeqNo());
1558                      ri = GetOrbitalInfoStoredTrack(ti->GetSeqNo());
1559    
1560        //            cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;        //            cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
1561    
1562        //assign starting scores        //assign starting scores
1563        tp_score = 0;  //main track sorted by the tracker        tp_score = 0; //main track sorted by the tracker
1564        ti_score = 0;  //image track        ti_score = 0; //image track
1565                
1566          // -----------------------------------------------------------------------------------------
1567          // *****************************************************************************************
1568        // -----------------------------------------------------------------------------------------        // -----------------------------------------------------------------------------------------
1569        // calorimeter check        // calorimeter check
1570        // -----------------------------------------------------------------------------------------        // -----------------------------------------------------------------------------------------
1571        // check the Y spatial residual on the first calorimeter plane        if (use_CAL && !calo2_obj) {
1572        // (cut on calorimeter variables from Emiliano)          cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but CaloLevel2 not loaded !!!";
1573        if( use_CAL && !calo2_obj ){          return;
         cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but CaloLevel2 not loaded !!!";  
         return;  
1574        };        };
1575        if( use_CAL && !cp &&  ci ){        if (use_CAL && !cp && ci) {
1576          ti_score++;          ti_score++;
1577          toti_score++;          toti_score++;
1578        };        };
1579        if( use_CAL &&  cp && !ci ){        if (use_CAL && cp && !ci) {
1580          tp_score++;          tp_score++;
1581          totp_score++;          totp_score++;
1582        };        };
1583        if(        if (use_CAL && cp && ci && true) {
          use_CAL            &&  
 //       calo2_obj->npcfit[1] > 5     &&   //no. of fit planes on Y view  
 //       calo2_obj->varcfit[1] < 1000. &&  //fit variance on Y view  
          cp && ci &&  
          true){  
   
                   
 //      Float_t resy_p = cp->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_p < 0)resy_p= - resy_p;  
 //      Float_t resy_i = ci->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_i < 0)resy_i= - resy_i;  
                   
 //      if(resy_p <= resy_i) tp_score++;  
 //      else                 ti_score++;  
           
1584    
1585          if( cp->npresh > ci->npresh){          if (cp->npresh > ci->npresh && true) {
1586            tp_score++;            tp_score++;
1587            totp_score++;            totp_score++;
1588          };          };
1589          if( cp->npresh < ci->npresh){          if (cp->npresh < ci->npresh && true) {
1590            ti_score++;            ti_score++;
1591            toti_score++;            toti_score++;
1592          };          };
1593    
1594          //      cout << "CALO "<<tp_score<<ti_score<<endl;          //      cout << "CALO "<<tp_score<<ti_score<<endl;
1595    
1596        };        };
1597        // -----------------------------------------------------------------------------------------        // -----------------------------------------------------------------------------------------
1598          // *****************************************************************************************
1599          // -----------------------------------------------------------------------------------------
1600        // TOF check        // TOF check
1601        // -----------------------------------------------------------------------------------------        // -----------------------------------------------------------------------------------------
1602        // check the number of hit pmts along the track        // check the number of hit pmts along the track
1603        // on S12 S21 and S32, where paddles are parallel to Y axis        // on S12 S21 and S32, where paddles are parallel to Y axis
1604        if( (use_TOF || use_S1 || use_S2 || use_S3 ) && !tof_obj ){        if ((use_TOF || use_S1 || use_S2 || use_S3) && !tof2_obj) {
1605          cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but ToFLevel2 not loaded !!!";          cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but ToFLevel2 not loaded !!!";
1606          return;          return;
1607        };        };
1608        //        //
1609        if( (use_TOF || use_S1 || use_S2 || use_S3 ) && !op && oi ){        if ((use_TOF || use_S1 || use_S2 || use_S3) && !op && oi) {
1610          ti_score++;          ti_score++;
1611          toti_score++;          toti_score++;
1612        };        };
1613        if( (use_TOF || use_S1 || use_S2 || use_S3 ) && op && !oi ){        if ((use_TOF || use_S1 || use_S2 || use_S3) && op && !oi) {
1614          tp_score++;          tp_score++;
1615          totp_score++;          totp_score++;
1616        };        };
1617        if( (use_TOF || use_S1 || use_S2 || use_S3 ) && op && oi ){        if ((use_TOF || use_S1 || use_S2 || use_S3) && op && oi) {
1618          //          //
1619          Float_t sen = 0.;          Float_t sen = 0.;
1620          for (Int_t ih=0; ih < op->npmtadc; ih++){          for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1621            Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );            Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1622            if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (op->dedx).At(ih);            if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
1623          };              sen += (op->dedx).At(ih);
1624          for (Int_t ih=0; ih < oi->npmtadc; ih++){          };
1625            Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );          for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1626            if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (oi->dedx).At(ih);            Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1627          };            if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
1628          //              sen += (oi->dedx).At(ih);
1629          if (  sen >= sortthr && false){ // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!)          };
1630            //printf(" IS A NUCLEUS! en = %f \n",sen);          //
1631            //          if (sen >= sortthr && false) { // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!)
1632            // is a nucleus use a different algorithm            //printf(" IS A NUCLEUS! en = %f \n",sen);
1633            //            //
1634            Int_t nz = 6; Float_t zin[6];                                          // << define TOF z-coordinates            // is a nucleus use a different algorithm
1635            for(Int_t ip=0; ip<nz; ip++)            //
1636              zin[ip] = tof_obj->ToFLevel2::GetZTOF(tof_obj->ToFLevel2::GetToFPlaneID(ip));     // << read ToF plane z-coordinates            Int_t nz = 6;
1637            Trajectory *tr = new Trajectory(nz,zin);            Float_t zin[6]; // << define TOF z-coordinates
1638            //            for (Int_t ip = 0; ip < nz; ip++)
1639            Int_t nphit_p =0;              zin[ip] = tof2_obj->ToFLevel2::GetZTOF(tof2_obj->ToFLevel2::GetToFPlaneID(ip)); // << read ToF plane z-coordinates
1640            Int_t nphit_i =0;            Trajectory *tr = new Trajectory(nz, zin);
1641            Float_t enhit_p = 0.;            //
1642            Float_t enhit_i = 0.;            Int_t nphit_p = 0;
1643            //            Int_t nphit_i = 0;
1644            for (Int_t ih=0; ih < op->npmtadc; ih++){            Float_t enhit_p = 0.;
1645              Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );            Float_t enhit_i = 0.;
1646              if(pl == 1 || pl == 2 || pl == 5){            //
1647                nphit_p++;            for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1648                enhit_p += (op->dedx).At(ih);              Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1649              };              if (pl == 1 || pl == 2 || pl == 5) {
1650            };                nphit_p++;
1651            //                enhit_p += (op->dedx).At(ih);
1652            tp->DoTrack2(tr);              };
1653            //            };
1654            if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){            //
1655              for (Int_t ih=0; ih < op->npmtadc; ih++){            tp->DoTrack2(tr);
1656                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );            //
1657                if(pl == 0){            if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
1658                  nphit_p++;              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1659                  enhit_p += (op->dedx).At(ih);                Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1660                };                if (pl == 0) {
1661              };                  nphit_p++;
1662            };                  enhit_p += (op->dedx).At(ih);
1663            if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){                };
1664              for (Int_t ih=0; ih < op->npmtadc; ih++){              };
1665                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );            };
1666                if(pl == 3){            if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
1667                  nphit_p++;              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1668                  enhit_p += (op->dedx).At(ih);                Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1669                };                if (pl == 3) {
1670              };                  nphit_p++;
1671            };                  enhit_p += (op->dedx).At(ih);
1672            if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){                };
1673              for (Int_t ih=0; ih < op->npmtadc; ih++){              };
1674                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );            };
1675                if(pl == 4){            if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
1676                  nphit_p++;              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1677                  enhit_p += (op->dedx).At(ih);                Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1678                };                if (pl == 4) {
1679              };                  nphit_p++;
1680            };                  enhit_p += (op->dedx).At(ih);
1681                  };
1682            for (Int_t ih=0; ih < oi->npmtadc; ih++){              };
1683              Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );            };
1684              if(pl == 1 || pl == 2 || pl == 5){  
1685                nphit_i++;                    for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1686                enhit_i += (op->dedx).At(ih);              Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1687              };              if (pl == 1 || pl == 2 || pl == 5) {
1688            };                nphit_i++;
1689            //                enhit_i += (op->dedx).At(ih);
1690            ti->DoTrack2(tr);              };
1691            //            };
1692            if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){            //
1693              for (Int_t ih=0; ih < oi->npmtadc; ih++){            ti->DoTrack2(tr);
1694                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );            //
1695                if(pl == 0){            if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
1696                  nphit_i++;              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1697                  enhit_i += (op->dedx).At(ih);                Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1698                };                if (pl == 0) {
1699              };                  nphit_i++;
1700            };                  enhit_i += (op->dedx).At(ih);
1701            if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){                };
1702              for (Int_t ih=0; ih < oi->npmtadc; ih++){              };
1703                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );            };
1704                if(pl == 3){            if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
1705                  nphit_i++;              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1706                  enhit_i += (op->dedx).At(ih);                Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1707                };                if (pl == 3) {
1708              };                  nphit_i++;
1709            };                  enhit_i += (op->dedx).At(ih);
1710            if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){                };
1711              for (Int_t ih=0; ih < oi->npmtadc; ih++){              };
1712                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );            };
1713                if(pl == 4){            if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
1714                  nphit_i++;              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1715                  enhit_i += (op->dedx).At(ih);                Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1716                };                if (pl == 4) {
1717              };                  nphit_i++;
1718            };                              enhit_i += (op->dedx).At(ih);
1719                  };
1720                                };
1721            if(            };
1722               (use_TOF || use_S1 || use_S2 || use_S3)            &&  
1723               (nphit_p+nphit_i) !=0 &&              if ((use_TOF || use_S1 || use_S2 || use_S3) && (nphit_p + nphit_i) != 0 && true) {
1724               true){  
1725                                    //      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);
1726              //      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);              //      printf(" score p %i score i %i \n",tp_score,ti_score);
1727              //      printf(" score p %i score i %i \n",tp_score,ti_score);              //            if( enhit_p > enhit_i ) tp_score++;
1728              //            if( enhit_p > enhit_i ) tp_score++;              //            if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
1729              //            if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;              if (nphit_p > nphit_i)
1730              if ( nphit_p > nphit_i ) tp_score++;                tp_score++;
1731              if ( nphit_p < nphit_i ) ti_score++;              if (nphit_p < nphit_i)
1732              if ( nphit_p == nphit_i ){                ti_score++;
1733                if ( enhit_p > enhit_i ) tp_score++;              if (nphit_p == nphit_i) {
1734                else ti_score++;                if (enhit_p > enhit_i)
1735              };                  tp_score++;
1736              //      printf(" dopo score p %i score i %i \n",tp_score,ti_score);                else
1737            };                  ti_score++;
1738            delete tr;              };
1739            //              //      printf(" dopo score p %i score i %i \n",tp_score,ti_score);
1740          } else {            };
1741            //            delete tr;
1742            // NOT a NUCLEUS            //
1743            //          }
1744            //printf(" NOT a NUCLEUS! en = %f \n",sen);          else {
1745              // -------------
1746            Int_t nphit_p =0;            // NOT a NUCLEUS
1747            Int_t nphit_i =0;            // -------------
1748                              //printf(" NOT a NUCLEUS! en = %f \n",sen);
1749                    
1750            /*                            cout << "track: npmtadc "<< op->npmtadc << endl;            Int_t nphit_p = 0;
1751              cout << "track: npmttdc "<< op->npmttdc << endl;            Int_t nphit_i = 0;
1752              cout << "image: npmtadc "<< oi->npmtadc << endl;  
1753              cout << "image: npmttdc "<< oi->npmttdc << endl;*/            /*                            cout << "track: npmtadc "<< op->npmtadc << endl;
1754                               cout << "track: npmttdc "<< op->npmttdc << endl;
1755  //        for (Int_t ih=0; ih < op->npmtadc; ih++){             cout << "image: npmtadc "<< oi->npmtadc << endl;
1756  //          Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );             cout << "image: npmttdc "<< oi->npmttdc << endl;*/
1757  //          if(pl == 1 || pl == 2 || pl == 5)nphit_p++;  
1758  //        };            //      for (Int_t ih=0; ih < op->npmtadc; ih++){
1759                              //        Int_t pl = tof2_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1760  //        for (Int_t ih=0; ih < oi->npmtadc; ih++){            //        if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
1761  //          Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );            //      };
1762  //          if(pl == 1 || pl == 2 || pl == 5)nphit_i++;  
1763  //        };            //      for (Int_t ih=0; ih < oi->npmtadc; ih++){
1764            // --- modified to count tdc signals (more efficient?)            //        Int_t pl = tof2_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1765            // --- and to implement check on tdcflag            //        if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
1766            for (Int_t ih=0; ih < op->npmttdc; ih++){            //      };
1767              Int_t pl = tof_obj->GetPlaneIndex( (op->pmttdc).At(ih) );            // --- modified to count tdc signals (more efficient?)
1768  //          if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;            // --- and to implement check on tdcflag
1769              if ( (use_S1 && ( pl == 0 || pl == 1 )) || (use_S2 && ( pl == 2 || pl == 3 )) || (use_S3 && ( pl == 4 || pl == 5 )) ){            for (Int_t ih = 0; ih < op->npmttdc; ih++) {
1770                if( (op->tdcflag).At(ih)==0 )nphit_p++;              Int_t pl = tof2_obj->GetPlaneIndex((op->pmttdc).At(ih));
1771              };              //      if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
1772            };              if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
1773                                    || (use_S3 && (pl == 4 || pl == 5))) {
1774            for (Int_t ih=0; ih < oi->npmttdc; ih++){                if ((op->tdcflag).At(ih) == 0)
1775              Int_t pl = tof_obj->GetPlaneIndex( (oi->pmttdc).At(ih) );                  nphit_p++;
1776  //          if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;                };
1777              if ( (use_S1 && ( pl == 0 || pl == 1 )) || (use_S2 && ( pl == 2 || pl == 3 )) || (use_S3 && ( pl == 4 || pl == 5 )) ){            };
1778                if( (oi->tdcflag).At(ih)==0 )nphit_i++;    
1779              };            for (Int_t ih = 0; ih < oi->npmttdc; ih++) {
1780            };              Int_t pl = tof2_obj->GetPlaneIndex((oi->pmttdc).At(ih));
1781                                //      if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;
1782            if(              if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
1783               (nphit_p+nphit_i) !=0 &&                    || (use_S3 && (pl == 4 || pl == 5))) {
1784               true){                if ((oi->tdcflag).At(ih) == 0)
1785                                        nphit_i++;
1786              if ( nphit_p != nphit_i ){              };
1787                totp_score += nphit_p;            };
1788                toti_score += nphit_i;  
1789                tp_score+=nphit_p;            if ((nphit_p + nphit_i) != 0 && true) {
1790                ti_score+=nphit_i;  
1791              };              if (nphit_p != nphit_i) {
1792              //      if     ( nphit_p > nphit_i) tp_score+=nphit_p;                totp_score += nphit_p;
1793              //      else if( nphit_p < nphit_i) ti_score+=nphit_i;                toti_score += nphit_i;
1794              //      else ;//niente                tp_score += nphit_p;
1795            };                ti_score += nphit_i;
1796          };              };
1797          //      cout << "TOF "<<tp_score<<ti_score<<endl;              //      if     ( nphit_p > nphit_i) tp_score+=nphit_p;
1798                //      else if( nphit_p < nphit_i) ti_score+=nphit_i;
1799                //      else ;//niente
1800              };
1801            };
1802            //      cout << "TOF "<<tp_score<<ti_score<<endl;
1803        };        };
1804    
1805          //      if(tp_score == ti_score) use_TRK = true;
 //      if(tp_score == ti_score) use_TRK = true;  
1806    
1807    
1808        // -----------------------------------------------------------------------------------------        // -----------------------------------------------------------------------------------------
1809          // *****************************************************************************************
1810          // -----------------------------------------------------------------------------------------
1811        // tracker check        // tracker check
1812        // -----------------------------------------------------------------------------------------        // -----------------------------------------------------------------------------------------
1813        // chi**2 difference is not always large enough to distinguish among        // chi**2 difference is not always large enough to distinguish among
1814        // the real track and its image.        // the real track and its image.
1815        // 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.
1816        // *** MODIFIED ON AUGUST 2007 ***        // *** MODIFIED ON AUGUST 2007 ***
1817        if(use_TRK){        if (use_TRK) {
1818  //      if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;          //      if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
1819  //      else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;          //      else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
1820    
1821            // CHECK 1 : number of points along X            // CHECK 1 : number of points along X
1822          if ( tp->GetNX() >= ti->GetNX() ){          if (tp->GetNX() >= ti->GetNX()) {
1823            tp_score++ ;            tp_score++;
1824            totp_score++ ;            totp_score++;
1825          };          };
1826          if ( tp->GetNX() <= ti->GetNX() ){          if (tp->GetNX() <= ti->GetNX()) {
1827            ti_score++ ;            ti_score++;
1828            toti_score++ ;            toti_score++;
1829          };          };
1830            // CHECK 2 : number of points along Y            // CHECK 2 : number of points along Y
1831          if ( tp->GetNY() >= ti->GetNY() ){          if (tp->GetNY() >= ti->GetNY()) {
1832            tp_score++ ;            tp_score++;
1833            totp_score++ ;            totp_score++;
1834          };          };
1835          if ( tp->GetNY() <= ti->GetNY() ){          if (tp->GetNY() <= ti->GetNY()) {
1836            ti_score++ ;            ti_score++;
1837            toti_score++ ;            toti_score++;
1838          };          };
           // CHECK 3 : chi**2 along X    
 //        if(      tp->GetChi2X() > 0 && (tp->GetChi2X() < ti->GetChi2X() || ti->GetChi2X() <=0) ) tp_score++ ;  
 //        if(      ti->GetChi2X() > 0 && (ti->GetChi2X() < tp->GetChi2X() || tp->GetChi2X() <=0) ) ti_score++ ;  
           // CHECK 4 : chi**2 along Y    
 //        if(      tp->GetChi2Y() > 0 && (tp->GetChi2Y() < ti->GetChi2Y() || ti->GetChi2Y() <=0) ) tp_score++ ;  
 //        if(      ti->GetChi2Y() > 0 && (ti->GetChi2Y() < tp->GetChi2Y() || tp->GetChi2Y() <=0) ) ti_score++ ;  
           // CHECK 5 : total chi**2    
 //        if(      tp->chi2 > 0 && (tp->chi2 < ti->chi2 || ti->chi2 <=0) ) tp_score++ ;  
 //        if(      ti->chi2 > 0 && (ti->chi2 < tp->chi2 || tp->chi2 <=0) ) ti_score++ ;  
1839    
1840          //      cout << "TRK "<<tp_score<<ti_score<<endl;          //      cout << "TRK "<<tp_score<<ti_score<<endl;
1841        };        };
               
1842    
1843          // -----------------------------------------------------------------------------------------
1844          // *****************************************************************************************
1845          // -----------------------------------------------------------------------------------------
1846          // GPamela check
1847          // -----------------------------------------------------------------------------------------
1848    
1849          //---------------------------------------------------
1850          // count the number of GP hits
1851          //---------------------------------------------------
1852          if (use_GP) {
1853            int ngphits_p = 0;
1854            int ngphits_i = 0;
1855            float toll = 0.02; //200 micron
1856            for (int ih = 0; ih < GetGPamela()->Nthspe; ih++) {
1857              int ip = (Int_t) GetGPamela()->Itrpb[ih] - 1;
1858              if (tp && tp->YGood(ip) && fabs(tp->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
1859                ngphits_p++;
1860              if (ti && ti->YGood(ip) && fabs(ti->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
1861                ngphits_i++;
1862            }
1863            if (ngphits_p > ngphits_i && true) {
1864              tp_score++;
1865              totp_score++;
1866            }
1867            if (ngphits_p < ngphits_i && true) {
1868              ti_score++;
1869              toti_score++;
1870            }
1871          }
1872    
1873        // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*        // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1874        // the winner is....        // the winner is....
1875        // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*        // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1876        if      (tp_score > ti_score) {        if (tp_score > ti_score) {
           
   
       }else if (tp_score < ti_score) {  
   
   
         ts = ti;//its image!!  
         cs = ci;  
         os = oi;  
         Int_t totis = toti_score;  
1877    
1878          ti = tp;//its image!!        }
1879          ci = cp;        else if (tp_score < ti_score) {
         oi = op;  
1880    
1881          tp = ts;//its image!!          ts = ti;//its image!!
1882          cp = cs;          cs = ci;
1883          op = os;          os = oi;
1884            rs = ri;
1885            Int_t totis = toti_score;
1886    
1887            ti = tp;//its image!!
1888            ci = cp;
1889            oi = op;
1890            ri = rp;
1891    
1892            tp = ts;//its image!!
1893            cp = cs;
1894            op = os;
1895            rp = rs;
1896    
1897          toti_score = totp_score;          toti_score = totp_score;
1898          totp_score = totis;          totp_score = totis;
1899    
1900                          }
1901        }else {        else {
1902    
1903  //      cout << "Warning - track image ambiguity not solved" << endl;          //      cout << "Warning - track image ambiguity not solved" << endl;
1904    
1905        };        };
1906                
1907      }else{      }
1908        else {
1909        totp_score = 1;        totp_score = 1;
1910        toti_score = 0;        toti_score = 0;
1911          
1912        //            ts = tp;        //            ts = tp;
1913        //            cs = cp;        //            cs = cp;
1914        //            os = op;        //            os = op;
1915      };      };
1916            
1917      //  cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;      //  cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
1918      //  sorted_tracks->Add(ts);//save the track in the sorted array      //  sorted_tracks->Add(ts);//save the track in the sorted array
1919      //  sorted_tracks.Add(ts);//save the track in the sorted array      //  sorted_tracks.Add(ts);//save the track in the sorted array
# Line 1071  void PamLevel2::SortTracks(){ Line 1923  void PamLevel2::SortTracks(){
1923      //  cout<<"o "<<cp<<endl;      //  cout<<"o "<<cp<<endl;
1924      //  cout<<"o "<<op<<endl;      //  cout<<"o "<<op<<endl;
1925    
1926      new(ttsorted[i]) PamTrack(tp,cp,op);      new (ttsorted[i]) PamTrack(tp, cp, op, rp);
1927      new(ttimage[i])  PamTrack(ti,ci,oi);      new (ttimage[i]) PamTrack(ti, ci, oi, ri);
1928    
1929      ((PamTrack*)(ttsorted[i]))->SetPScore(totp_score);      ((PamTrack*) (ttsorted[i]))->SetPScore(totp_score);
1930      ((PamTrack*)(ttsorted[i]))->SetIScore(toti_score);      ((PamTrack*) (ttsorted[i]))->SetIScore(toti_score);
1931      ((PamTrack*)(ttimage[i]))->SetPScore(totp_score);      ((PamTrack*) (ttimage[i]))->SetPScore(totp_score);
1932      ((PamTrack*)(ttimage[i]))->SetIScore(toti_score);      ((PamTrack*) (ttimage[i]))->SetIScore(toti_score);
1933    };    };
1934    
1935    if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){    if (tsorted->GetEntries() != trk2_obj->GetNTracks()) {
1936      cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() "<<tsorted->GetEntries()<<" != trk2_obj->GetNTracks() = "<<trk2_obj->GetNTracks() <<endl;      cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() " << tsorted->GetEntries()
1937      tsorted->Delete(); tsorted=0;          << " != trk2_obj->GetNTracks() = " << trk2_obj->GetNTracks() << endl;
1938      timage->Delete(); timage=0;      tsorted->Delete();
1939        tsorted = 0;
1940        timage->Delete();
1941        timage = 0;
1942    }    }
1943    
1944    //Restore Object count    //Restore Object count
1945    //To save space in the table keeping track of all referenced objects    //To save space in the table keeping track of all referenced objects
1946    //We reset the object count to what it was at the beginning of the event.    //We reset the object count to what it was at the beginning of the event.
1947    TProcessID::SetObjectCount(ObjectNumber);    TProcessID::SetObjectCount(ObjectNumber);
1948    
1949      //Everything went fine so the current event can be tagged as sorted
1950      issorted = true;
1951      lastsorted = GetReadEntry();
1952    
1953    }
1954    ;
1955    //
1956    //--------------------------------------
1957    /**
1958     * Sort physical (tracker) tracks.
1959     * @param how String to set the sorting cryterium (es: "CAL" or "TRK+CAL+TOF" ecc...).
1960     * Sorting cryteria:
1961     * TRK: lower chi**2
1962     * CAL: lower Y spatial residual on the first calorimeter plane
1963     * TOF: bigger numebr of hit PMTs along the track, on S12 S21 S32 (where paddles are along the Y axis).
1964     * S1: (ask Emiliano)
1965     * S2: (ask Emiliano)
1966     * S3: (ask Emiliano)
1967     * GP: more GP hits
1968     * The default sorting cryterium is "TOF+CAL".
1969     *
1970     * 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).
1971     *
1972     * New version, with handling of extended tracks and nuclei tracks.
1973     */
1974    void PamLevel2::SortTracksNew() {
1975    
1976      //--------------------------------------------------
1977      //Check if the current event has already been sorted
1978      //--------------------------------------------------
1979      if (issorted_new && lastsorted_new == GetReadEntry()) {
1980          return; //already done for this event
1981      }
1982    
1983    
1984    //  cout << "SORT" << endl;
1985    
1986      //Reset the sort flags, just in case something will go wrong...
1987      issorted_new = false;
1988      lastsorted_new = -1;
1989    
1990      //--------------------------------------------------
1991      // set input variables
1992      //--------------------------------------------------
1993    
1994      TString how = howtosort;
1995      //Save current Object count
1996      Int_t ObjectNumber = TProcessID::GetObjectCount();
1997    
1998    
1999    
2000    
2001      TrkLevel2  * trk2 ;
2002      CaloLevel2 * calo2;
2003      ToFLevel2  * tof2  ;
2004      OrbitalInfo  * orb2 ;
2005      
2006      TClonesArray * trkext ;
2007      TClonesArray * caloext;
2008      TClonesArray * tofext ;
2009      TClonesArray * orbext ;
2010    
2011    
2012    //   cout << "trk2_obj" << trk2_obj<< endl;
2013    //   cout << "trk2_nuc_obj" << trk2_nuc_obj<< endl;
2014    //   cout << " trk_ext_obj "<<trk_ext_obj<< endl;
2015    //   cout << " trk_ext_nuc_obj "<<trk_ext_nuc_obj<< endl;
2016    
2017      //-----------------------------------------------------------
2018      // create/reset TCloneArrays to store tracks and their images
2019      //-----------------------------------------------------------
2020    
2021    //  cout << " PamLevel2::SortTracksNew() --- Clear TClonesArray objects"<<endl;
2022      
2023      // main tracks from standard alg
2024    //   if (!tsorted)
2025    //       tsorted = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
2026    //   tsorted->Clear("C+C");//Delete();
2027    //   // track images from standard alg
2028    //   if (!timage)
2029    //       timage = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
2030    //   timage->Clear("C+C");//Delete();
2031    //   // tracks from extended algorythm
2032    //   if(EXT && !text)
2033    //       text = new TClonesArray("PamTrack",trk_ext_obj->GetEntries());
2034    //   if(text)text->Clear("C+C");//Delete();
2035    
2036      if(tsorted)delete tsorted;
2037      if(timage) delete timage;
2038      if(text)   delete text;
2039      tsorted = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
2040      timage = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
2041      text = new TClonesArray("PamTrack",trk_ext_obj->GetEntries());
2042    
2043      //-----------------------------------------------------------
2044      // create/reset TCloneArrays to store tracks and their images
2045      //-----------------------------------------------------------
2046      if(NUC){
2047    
2048          
2049          if(tsorted_nuc)delete tsorted_nuc;
2050          if(timage_nuc) delete timage_nuc;
2051          if(text_nuc)   delete text_nuc;
2052          tsorted_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks());
2053          timage_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks());
2054          text_nuc = new TClonesArray("PamTrack",trk_ext_nuc_obj->GetEntries());
2055    
2056      // main tracks from standard alg
2057    //       if (!tsorted_nuc)
2058    //        tsorted_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks());
2059    //       tsorted_nuc->Clear("C+C");//Delete();
2060    //       // track images from standard alg
2061    //       if (!timage_nuc)
2062    //        timage_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks());
2063    //       timage_nuc->Clear("C+C");//Delete();
2064    //       // tracks from extended algorythm
2065    //       if(EXT && !text_nuc)
2066    //        text_nuc = new TClonesArray("PamTrack",trk_ext_nuc_obj->GetEntries());
2067    //       if(text_nuc)text_nuc->Clear("C+C");//Delete();
2068    
2069      }
2070      //--------------------------------------------------
2071      // retrieve sorting method
2072      //--------------------------------------------------
2073      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
2074      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
2075      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
2076      Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase);
2077      Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase);
2078      Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase);
2079      Bool_t use_GP = how.Contains("GP", TString::kIgnoreCase);
2080    
2081      if (use_TOF) {
2082        use_S1 = true;
2083        use_S2 = true;
2084        use_S3 = true;
2085      };
2086      if (!CAL2 && use_CAL)
2087        use_CAL = false;
2088      if (!TOF) {
2089        use_TOF = false;
2090        use_S1 = false;
2091        use_S2 = false;
2092        use_S3 = false;
2093      }
2094      if (!GP) {
2095        use_GP = false;
2096      }
2097    
2098      if (!TRK2) {
2099        cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
2100        return;
2101      };
2102    
2103    
2104      ///////////////////////////////////////////////////////////////////////////////////
2105      //
2106      //   sort tracks and fill PamTrack arrays
2107      //
2108      ///////////////////////////////////////////////////////////////////////////////////
2109      for(int doit=0; doit<2; doit++){
2110    
2111    //       cout << "doit "<<doit<<endl;
2112    
2113    
2114          if(doit == 0){
2115    
2116              trk2  = trk2_obj;
2117              calo2 = calo2_obj;
2118              tof2  = tof2_obj;
2119              orb2  = orb2_obj;
2120          
2121              trkext = trk_ext_obj;
2122              caloext = calo_ext_obj;
2123              tofext = tof_ext_obj;
2124              orbext = orb_ext_obj;
2125            
2126          
2127          
2128          
2129    
2130          }else if (doit == 1){
2131    
2132              if(!NUC)break;
2133    
2134          
2135              trk2  = trk2_nuc_obj;
2136              calo2 = calo2_nuc_obj;
2137              tof2  = tof2_nuc_obj;
2138              orb2  = orb2_nuc_obj;
2139          
2140              trkext = trk_ext_nuc_obj;
2141              caloext = calo_ext_nuc_obj;
2142              tofext = tof_ext_nuc_obj;
2143              orbext = orb_ext_nuc_obj;
2144          
2145          
2146          
2147    
2148          }
2149    
2150    //       cout << "trk2" << trk2<<endl;
2151    //       cout << "calo2" << calo2<<endl;
2152    //       cout << "tof2" << tof2<<endl;
2153    //       cout << "orb2" << orb2<<endl;
2154    //       cout << "trkext" <<trkext <<endl;
2155    //       cout << "tofext" << tofext<<endl;
2156    //       cout << "caloext" << caloext<<endl;
2157    //       cout << "orbext" << orbext<<endl;
2158    
2159          TClonesArray &ttext    = (doit==0 ? *text : *text_nuc);
2160          TClonesArray &ttimage  = (doit==0 ? *timage : *timage_nuc);
2161          TClonesArray &ttsorted = (doit==0 ? *tsorted : *tsorted_nuc);
2162      
2163    //       cout << "tsorted + timage "<<doit<<endl;
2164    
2165          //--------------------------------------------------
2166          // loop over "physical" tracks sorted by the tracker
2167          //--------------------------------------------------
2168          for (Int_t i = 0; i < trk2->TrkLevel2::GetNTracks(); i++) {
2169    
2170              TrkTrack *ts = 0;
2171              CaloTrkVar *cs = 0;
2172              ToFTrkVar *os = 0;
2173              OrbitalInfoTrkVar *rs = 0;
2174    
2175              // get tracker tracks
2176              TrkTrack *tp = trk2->GetTrack(i); //tracker
2177              CaloTrkVar *cp = calo2->GetCaloStoredTrack(tp->GetSeqNo());
2178              ToFTrkVar *op = tof2->GetToFStoredTrack(tp->GetSeqNo());
2179              OrbitalInfoTrkVar *rp = orb2->GetOrbitalInfoStoredTrack(tp->GetSeqNo());
2180    
2181              TrkTrack *ti = 0; //tracker (image)
2182              CaloTrkVar *ci = 0;
2183              ToFTrkVar *oi = 0;
2184              OrbitalInfoTrkVar *ri = 0;
2185              //    cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
2186              // if track has an image, check image selection
2187    
2188              Int_t tp_score = 0; //main track sorted by the tracker
2189              Int_t ti_score = 0; //image track
2190              Int_t totp_score = 0; //main track sorted by the tracker
2191              Int_t toti_score = 0; //image track
2192    
2193              if (tp->HasImage()) {
2194    
2195                  ti = trk2->GetTrackImage(i); //tracker (image)
2196                  ci = calo2->GetCaloStoredTrack(ti->GetSeqNo());
2197                  oi = tof2->GetToFStoredTrack(ti->GetSeqNo());
2198                  ri = orb2->GetOrbitalInfoStoredTrack(ti->GetSeqNo());
2199    
2200                  //            cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
2201    
2202                  //assign starting scores
2203                  tp_score = 0; //main track sorted by the tracker
2204                  ti_score = 0; //image track
2205    
2206                  // -----------------------------------------------------------------------------------------
2207                  // *****************************************************************************************
2208                  // -----------------------------------------------------------------------------------------
2209                  // calorimeter check
2210                  // -----------------------------------------------------------------------------------------
2211                  if (use_CAL && !calo2) {
2212                      cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but CaloLevel2 not loaded !!!";
2213                      return;
2214                  };
2215                  if (use_CAL && !cp && ci) {
2216                      ti_score++;
2217                      toti_score++;
2218                  };
2219                  if (use_CAL && cp && !ci) {
2220                      tp_score++;
2221                      totp_score++;
2222                  };
2223                  if (use_CAL && cp && ci && true) {
2224    
2225                      if (cp->npresh > ci->npresh && true) {
2226                          tp_score++;
2227                          totp_score++;
2228                      };
2229                      if (cp->npresh < ci->npresh && true) {
2230                          ti_score++;
2231                          toti_score++;
2232                      };
2233    
2234                      //    cout << "CALO "<<tp_score<<ti_score<<endl;
2235    
2236                  };
2237                  // -----------------------------------------------------------------------------------------
2238                  // *****************************************************************************************
2239                  // -----------------------------------------------------------------------------------------
2240                  // TOF check
2241                  // -----------------------------------------------------------------------------------------
2242                  // check the number of hit pmts along the track
2243                  // on S12 S21 and S32, where paddles are parallel to Y axis
2244                  if ((use_TOF || use_S1 || use_S2 || use_S3) && !tof2) {
2245                      cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but ToFLevel2 not loaded !!!";
2246                      return;
2247                  };
2248                  //
2249                  if ((use_TOF || use_S1 || use_S2 || use_S3) && !op && oi) {
2250                      ti_score++;
2251                      toti_score++;
2252                  };
2253                  if ((use_TOF || use_S1 || use_S2 || use_S3) && op && !oi) {
2254                      tp_score++;
2255                      totp_score++;
2256                  };
2257                  if ((use_TOF || use_S1 || use_S2 || use_S3) && op && oi) {
2258                      //
2259                      Float_t sen = 0.;
2260                      for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2261                          Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2262                          if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
2263                              sen += (op->dedx).At(ih);
2264                      };
2265                      for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2266                          Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2267                          if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
2268                              sen += (oi->dedx).At(ih);
2269                      };
2270                      //
2271                      if (sen >= sortthr && false) { // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!)
2272                          //printf(" IS A NUCLEUS! en = %f \n",sen);
2273                          //
2274                          // is a nucleus use a different algorithm
2275                          //
2276                          Int_t nz = 6;
2277                          Float_t zin[6]; // << define TOF z-coordinates
2278                          for (Int_t ip = 0; ip < nz; ip++)
2279                              zin[ip] = tof2->GetZTOF(tof2->GetToFPlaneID(ip)); // << read ToF plane z-coordinates
2280                          Trajectory *tr = new Trajectory(nz, zin);
2281                          //
2282                          Int_t nphit_p = 0;
2283                          Int_t nphit_i = 0;
2284                          Float_t enhit_p = 0.;
2285                          Float_t enhit_i = 0.;
2286                          //
2287                          for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2288                              Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2289                              if (pl == 1 || pl == 2 || pl == 5) {
2290                                  nphit_p++;
2291                                  enhit_p += (op->dedx).At(ih);
2292                              };
2293                          };
2294                          //
2295                          tp->DoTrack2(tr);
2296                          //
2297                          if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
2298                              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2299                                  Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2300                                  if (pl == 0) {
2301                                      nphit_p++;
2302                                      enhit_p += (op->dedx).At(ih);
2303                                  };
2304                              };
2305                          };
2306                          if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
2307                              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2308                                  Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2309                                  if (pl == 3) {
2310                                      nphit_p++;
2311                                      enhit_p += (op->dedx).At(ih);
2312                                  };
2313                              };
2314                          };
2315                          if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
2316                              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2317                                  Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2318                                  if (pl == 4) {
2319                                      nphit_p++;
2320                                      enhit_p += (op->dedx).At(ih);
2321                                  };
2322                              };
2323                          };
2324    
2325                          for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2326                              Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2327                              if (pl == 1 || pl == 2 || pl == 5) {
2328                                  nphit_i++;
2329                                  enhit_i += (op->dedx).At(ih);
2330                              };
2331                          };
2332                          //
2333                          ti->DoTrack2(tr);
2334                          //
2335                          if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
2336                              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2337                                  Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2338                                  if (pl == 0) {
2339                                      nphit_i++;
2340                                      enhit_i += (op->dedx).At(ih);
2341                                  };
2342                              };
2343                          };
2344                          if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
2345                              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2346                                  Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2347                                  if (pl == 3) {
2348                                      nphit_i++;
2349                                      enhit_i += (op->dedx).At(ih);
2350                                  };
2351                              };
2352                          };
2353                          if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
2354                              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2355                                  Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2356                                  if (pl == 4) {
2357                                      nphit_i++;
2358                                      enhit_i += (op->dedx).At(ih);
2359                                  };
2360                              };
2361                          };
2362    
2363                          if ((use_TOF || use_S1 || use_S2 || use_S3) && (nphit_p + nphit_i) != 0 && true) {
2364    
2365                              //        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);
2366                              //        printf(" score p %i score i %i \n",tp_score,ti_score);
2367                              //              if( enhit_p > enhit_i ) tp_score++;
2368                              //              if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
2369                              if (nphit_p > nphit_i)
2370                                  tp_score++;
2371                              if (nphit_p < nphit_i)
2372                                  ti_score++;
2373                              if (nphit_p == nphit_i) {
2374                                  if (enhit_p > enhit_i)
2375                                      tp_score++;
2376                                  else
2377                                      ti_score++;
2378                              };
2379                              //        printf(" dopo score p %i score i %i \n",tp_score,ti_score);
2380                          };
2381                          delete tr;
2382                          //
2383                      }
2384                      else {
2385                          // -------------
2386                          // NOT a NUCLEUS
2387                          // -------------
2388                          //printf(" NOT a NUCLEUS! en = %f \n",sen);
2389    
2390                          Int_t nphit_p = 0;
2391                          Int_t nphit_i = 0;
2392    
2393                          /*                                cout << "track: npmtadc "<< op->npmtadc << endl;
2394                            cout << "track: npmttdc "<< op->npmttdc << endl;
2395                            cout << "image: npmtadc "<< oi->npmtadc << endl;
2396                            cout << "image: npmttdc "<< oi->npmttdc << endl;*/
2397    
2398                          //          for (Int_t ih=0; ih < op->npmtadc; ih++){
2399                          //            Int_t pl = tof2_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
2400                          //            if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
2401                          //          };
2402    
2403                          //          for (Int_t ih=0; ih < oi->npmtadc; ih++){
2404                          //            Int_t pl = tof2_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
2405                          //            if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
2406                          //          };
2407                          // --- modified to count tdc signals (more efficient?)
2408                          // --- and to implement check on tdcflag
2409                          for (Int_t ih = 0; ih < op->npmttdc; ih++) {
2410                              Int_t pl = tof2->GetPlaneIndex((op->pmttdc).At(ih));
2411                              //        if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
2412                              if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
2413                                  || (use_S3 && (pl == 4 || pl == 5))) {
2414                                  if ((op->tdcflag).At(ih) == 0)
2415                                      nphit_p++;
2416                              };
2417                          };
2418    
2419                          for (Int_t ih = 0; ih < oi->npmttdc; ih++) {
2420                              Int_t pl = tof2->GetPlaneIndex((oi->pmttdc).At(ih));
2421                              //        if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;
2422                              if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
2423                                  || (use_S3 && (pl == 4 || pl == 5))) {
2424                                  if ((oi->tdcflag).At(ih) == 0)
2425                                      nphit_i++;
2426                              };
2427                          };
2428    
2429                          if ((nphit_p + nphit_i) != 0 && true) {
2430    
2431                              if (nphit_p != nphit_i) {
2432                                  totp_score += nphit_p;
2433                                  toti_score += nphit_i;
2434                                  tp_score += nphit_p;
2435                                  ti_score += nphit_i;
2436                              };
2437                              //        if     ( nphit_p > nphit_i) tp_score+=nphit_p;
2438                              //        else if( nphit_p < nphit_i) ti_score+=nphit_i;
2439                              //        else ;//niente
2440                          };
2441                      };
2442                      //    cout << "TOF "<<tp_score<<ti_score<<endl;
2443                  };
2444    
2445                  //      if(tp_score == ti_score) use_TRK = true;
2446    
2447    
2448                  // -----------------------------------------------------------------------------------------
2449                  // *****************************************************************************************
2450                  // -----------------------------------------------------------------------------------------
2451                  // tracker check
2452                  // -----------------------------------------------------------------------------------------
2453                  // chi**2 difference is not always large enough to distinguish among
2454                  // the real track and its image.
2455                  // Tracker check will be applied always when calorimeter and tof information is ambiguous.
2456                  // *** MODIFIED ON AUGUST 2007 ***
2457                  if (use_TRK) {
2458                      //    if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
2459                      //    else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
2460    
2461                      // CHECK 1 : number of points along X
2462                      if (tp->GetNX() >= ti->GetNX()) {
2463                          tp_score++;
2464                          totp_score++;
2465                      };
2466                      if (tp->GetNX() <= ti->GetNX()) {
2467                          ti_score++;
2468                          toti_score++;
2469                      };
2470                      // CHECK 2 : number of points along Y
2471                      if (tp->GetNY() >= ti->GetNY()) {
2472                          tp_score++;
2473                          totp_score++;
2474                      };
2475                      if (tp->GetNY() <= ti->GetNY()) {
2476                          ti_score++;
2477                          toti_score++;
2478                      };
2479    
2480                      //    cout << "TRK "<<tp_score<<ti_score<<endl;
2481                  };
2482    
2483                  // -----------------------------------------------------------------------------------------
2484                  // *****************************************************************************************
2485                  // -----------------------------------------------------------------------------------------
2486                  // GPamela check
2487                  // -----------------------------------------------------------------------------------------
2488    
2489                  //---------------------------------------------------
2490                  // count the number of GP hits
2491                  //---------------------------------------------------
2492                  if (use_GP) {
2493                      int ngphits_p = 0;
2494                      int ngphits_i = 0;
2495                      float toll = 0.02; //200 micron
2496                      for (int ih = 0; ih < GetGPamela()->Nthspe; ih++) {
2497                          int ip = (Int_t) GetGPamela()->Itrpb[ih] - 1;
2498                          if (tp && tp->YGood(ip) && fabs(tp->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
2499                              ngphits_p++;
2500                          if (ti && ti->YGood(ip) && fabs(ti->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
2501                              ngphits_i++;
2502                      }
2503                      if (ngphits_p > ngphits_i && true) {
2504                          tp_score++;
2505                          totp_score++;
2506                      }
2507                      if (ngphits_p < ngphits_i && true) {
2508                          ti_score++;
2509                          toti_score++;
2510                      }
2511                  }
2512    
2513                  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
2514                  // the winner is....
2515                  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
2516                  if (tp_score > ti_score) {
2517    
2518                  }
2519                  else if (tp_score < ti_score) {
2520    
2521                      ts = ti;//its image!!
2522                      cs = ci;
2523                      os = oi;
2524                      rs = ri;
2525                      Int_t totis = toti_score;
2526    
2527                      ti = tp;//its image!!
2528                      ci = cp;
2529                      oi = op;
2530                      ri = rp;
2531    
2532                      tp = ts;//its image!!
2533                      cp = cs;
2534                      op = os;
2535                      rp = rs;
2536    
2537                      toti_score = totp_score;
2538                      totp_score = totis;
2539    
2540                  }
2541                  else {
2542    
2543                      //    cout << "Warning - track image ambiguity not solved" << endl;
2544    
2545                  };
2546    
2547              }
2548              else {
2549                  totp_score = 1;
2550                  toti_score = 0;
2551    
2552                  //            ts = tp;
2553                  //            cs = cp;
2554                  //            os = op;
2555              };
2556    
2557              //    cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
2558              //    sorted_tracks->Add(ts);//save the track in the sorted array
2559              //    sorted_tracks.Add(ts);//save the track in the sorted array
2560              //    sorted_tracks.Add(tp);//save the track in the sorted array
2561              //    cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
2562              //    cout<<"o "<<tp<<endl;
2563              //    cout<<"o "<<cp<<endl;
2564              //    cout<<"o "<<op<<endl;
2565    
2566              new (ttsorted[i]) PamTrack(tp, cp, op, rp);
2567              new (ttimage[i]) PamTrack(ti, ci, oi, ri);
2568    
2569              ((PamTrack*) (ttsorted[i]))->SetPScore(totp_score);
2570              ((PamTrack*) (ttsorted[i]))->SetIScore(toti_score);
2571              ((PamTrack*) (ttimage[i]))->SetPScore(totp_score);
2572              ((PamTrack*) (ttimage[i]))->SetIScore(toti_score);
2573          };
2574    
2575    
2576    
2577    
2578    
2579    //   if (tsorted->GetEntries() != trk2->GetNTracks()) {
2580    //     cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() " << tsorted->GetEntries()
2581    //         << " != trk2->GetNTracks() = " << trk2->GetNTracks() << endl;
2582    //     tsorted->Delete();
2583    //     tsorted = 0;
2584    //     timage->Delete();
2585    //     timage = 0;
2586    //   }
2587    
2588    
2589    //       cout << "text "<<doit<<endl;
2590    
2591    
2592          //--------------------------------------------------
2593          // fill array with extended tracks (this is easy...)
2594          //--------------------------------------------------
2595          if(EXT){
2596              for(int it=0; it<trkext->GetEntries(); it++){
2597                  
2598                  new (ttext[it]) PamTrack((ExtTrack*)(*trkext)[it], (CaloTrkVar*)(*caloext)[it], (ToFTrkVar*)(*tofext)[it], (OrbitalInfoTrkVar*)(*orbext)[it]);
2599                  
2600              }
2601          }
2602          
2603    
2604    
2605      };
2606    
2607    
2608      //Restore Object count
2609      //To save space in the table keeping track of all referenced objects
2610      //We reset the object count to what it was at the beginning of the event.
2611      TProcessID::SetObjectCount(ObjectNumber);
2612    
2613      //Everything went fine so the current event can be tagged as sorted
2614      issorted_new = true;
2615      lastsorted_new = GetReadEntry();
2616    
2617    //   cout << " tsorted "<< tsorted << " "<<(tsorted ? tsorted->GetEntries() : 0)<<endl;
2618    //   cout << " timage  "<< timage  << " "<<(timage  ? timage->GetEntries() : 0)<<endl;
2619    //   cout << " text    "<< text    << " "<<(text    ? text->GetEntries() : 0)<<endl;
2620    
2621    
2622  };  };
2623  //--------------------------------------  //--------------------------------------
2624  //  //
# Line 1100  void PamLevel2::SortTracks(){ Line 2628  void PamLevel2::SortTracks(){
2628   * 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.
2629   * 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.
2630   */   */
2631    
2632  // TRefArray *PamLevel2::GetTracks(){  // TRefArray *PamLevel2::GetTracks(){
2633    
2634  // //  *-*-*-*-*-*-*-*-*-*-*-*-*  // //  *-*-*-*-*-*-*-*-*-*-*-*-*
# Line 1108  void PamLevel2::SortTracks(){ Line 2637  void PamLevel2::SortTracks(){
2637    
2638  // //   return  sorted_tracks;  // //   return  sorted_tracks;
2639  //     return &sorted_tracks;  //     return &sorted_tracks;
2640        
2641  //  };  //  };
 TClonesArray *PamLevel2::GetTracks(){  
2642    
 //  *-*-*-*-*-*-*-*-*-*-*-*-*  
     SortTracks();  
 //  *-*-*-*-*-*-*-*-*-*-*-*-*  
2643    
     return tsorted;  
       
  };  
2644  //--------------------------------------  //--------------------------------------
2645   //  //
2646   //  //
2647  //--------------------------------------  //--------------------------------------
2648    
2649  /**  /**
2650   * Retrieves the it-th Pamela "physical" track.   * Retrieves the it-th Pamela "physical" track.
2651   * It override TrkLevel2::GetTrack(int it).   * It override TrkLevel2::GetTrack(int it).
2652   * @param it Track number, ranging from 0 to GetNTracks().   * @param it Track number, ranging from 0 to GetNTracks().
2653   */   */
2654  PamTrack *PamLevel2::GetTrack(int it){  PamTrack *PamLevel2::GetTrackOld(int it) {
2655    
2656  //    if(!trk2_obj) return 0;    PamTrack *track = NULL;
           
 // //  *-*-*-*+-*-*-*-*-*-*-*-*-*  
 //     SortTracks("+CAL+TOF");  
 // //  *-*-*-*-*-*-*-*-*-*-*-*-*  
 // //    if(!sorted_tracks)return 0;  
 //     if(sorted_tracks.IsEmpty())return 0;  
2657    
2658  //     PamTrack *track = 0;    //  *-*-*-*-*-*-*-*-*-*-*-*-*
2659          SortTracks();
2660  //     if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() && it<sorted_tracks.GetEntriesFast() ){        //  *-*-*-*-*-*-*-*-*-*-*-*-*
2661  //      TrkTrack   *t = (TrkTrack*)sorted_tracks.At(it);    if (!tsorted)
2662  //      track = GetPamTrackAlong(t);      return track;
2663  //     }else{    if (!tsorted->GetEntries())
2664  //      cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;      return track;
 //     };  
       
 //     return track;  
2665    
 //    cout << "PamLevel2::GetTrack(int it) "<<endl;  
 //  *-*-*-*-*-*-*-*-*-*-*-*-*  
     SortTracks();  
 //  *-*-*-*-*-*-*-*-*-*-*-*-*  
     if(!tsorted)return 0;  
     if(!tsorted->GetEntries())return 0;  
2666    
2667      PamTrack *track = 0;    if (it >= 0 && it < trk2_obj->TrkLevel2::GetNTracks()) {
2668            track = (PamTrack*)((*tsorted)[it]);
2669      if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks()){    }
2670  //      TrkTrack   *t = (TrkTrack*)sorted_tracks.At(it);    else {
2671  //      track = GetPamTrackAlong(t);      cout << "PamLevel2::GetTrackOld(int) : tracker track SeqNo " << it << " does not exist (GetNTracks() = "
2672          TClonesArray &t = *(tsorted);          << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
2673          track = (PamTrack*)t[it];    };
2674            
2675      }else{    return track;
2676          cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;  
     };  
       
     return track;  
       
2677  };  };
2678    
2679  //--------------------------------------  //PamTrack *PamLevel2::GetTrack(int it) { return GetTrack(it,trkAlg); };
2680  //  
 //  
 //--------------------------------------  
2681  /**  /**
2682   * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().   * Retrieves the it-th Pamela "physical" track.
2683     * It override TrkLevel2::GetTrack(int it).
2684   * @param it Track number, ranging from 0 to GetNTracks().   * @param it Track number, ranging from 0 to GetNTracks().
2685     * @param alg Algorythm, see SetTrakingAlgorythm(char *alg) for explanation.
2686   */   */
2687  PamTrack *PamLevel2::GetTrackImage(int it){  PamTrack *PamLevel2::GetTrack(int it, const char* alg) {
2688    
2689  //     if(!trk2_obj) return 0;      TString s(alg);
2690        if(!s.CompareTo("") ||!s.CompareTo("STD") )return GetTrackOld(it); //old algorythm
2691    
 // //  *-*-*-*-*-*-*-*-*-*-*-*-*  
 //     SortTracks("+CAL+TOF");  
 // //  *-*-*-*-*-*-*-*-*-*-*-*-*  
 // //    if(!sorted_tracks)return 0;  
 //     if(sorted_tracks.IsEmpty())return 0;  
       
 //     PamTrack *image = 0;  
       
 //     if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() ){  
 //      TrkTrack *temp = (TrkTrack*)sorted_tracks.At(it);  
 //      if( temp->HasImage() ){  
 //          TrkTrack   *t = trk2_obj->TrkLevel2::GetStoredTrack(temp->GetImageSeqNo());  
 //          image = GetPamTrackAlong(t);  
 //      }else{  
 //          cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;  
 //      };  
 //     }else{  
 //      cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;  
 //     };  
       
 //     return image;  
2692    
2693        SortTracksNew();
2694        // >> fill tsorted, timage and text
2695    
2696        if ( ( !s.Contains("EXTF", TString::kIgnoreCase) || !EXT )){ //not forced exteded-alg requested (or exteded-data missing)
2697    
2698            if( s.Contains("NUC")){
2699                if(
2700                    tsorted_nuc &&
2701                    it < tsorted_nuc->GetEntries() &&                             //enough tracks found
2702                    it >= 0 &&                                                //valid index
2703                    true) return (PamTrack*)((*tsorted_nuc)[it]);                 //ok return the track
2704            }else{
2705                if(
2706                    tsorted &&
2707                    it < tsorted->GetEntries() &&                             //enough tracks found
2708                    it >= 0 &&                                                //valid index
2709                    true )return (PamTrack*)((*tsorted)[it]);                 //ok return the track
2710            }
2711    
2712  //  *-*-*-*-*-*-*-*-*-*-*-*-*      }
     SortTracks();  
 //  *-*-*-*-*-*-*-*-*-*-*-*-*  
     if(!timage)return 0;  
     if(!timage->GetEntries())return 0;  
2713            
2714      PamTrack *image = 0;  
2715        /////////////////////////////////////////////////////////////////////////
2716        /// if requested get track from extended algorythm output
2717        /////////////////////////////////////////////////////////////////////////
2718            
2719      if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() ){      if(s.Contains("EXT", TString::kIgnoreCase) && EXT){//if exteded-alg requested
2720          TClonesArray &t = *(tsorted);          
2721          PamTrack *temp = (PamTrack*)t[it];          if(s.Contains("NUC")){
2722          if( temp->GetTrkTrack()->HasImage() ){              if(
2723              TClonesArray &t = *(timage);                  text_nuc &&
2724              image = (PamTrack*)t[it];                  it < text_nuc->GetEntries() &&                             //enough tracks found
2725                    it >= 0 &&                                                //valid index
2726                    true) return (PamTrack*)((*text_nuc)[it]);
2727          }else{          }else{
2728  //          cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;              if(
2729          };                  text &&
2730      }else{                  it < text->GetEntries() &&                             //enough tracks found
2731          cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;                  it >= 0 &&                                                //valid index
2732                    true) return (PamTrack*)((*text)[it]);
2733            }
2734    
2735      };      };
2736            
2737      return image;      
2738  }      cout << "PamTrack *PamLevel2::GetTrack("<<it<<","<<alg<<") -- wrong track number or unrecognised algorithm"<<endl;
2739        
2740        return NULL;
2741    
2742    }
2743    ;
2744    TClonesArray *PamLevel2::GetTracks() {
2745    
2746      //  *-*-*-*-*-*-*-*-*-*-*-*-*
2747      SortTracks();
2748      //  *-*-*-*-*-*-*-*-*-*-*-*-*
2749    
2750      return tsorted;
2751    
2752    }
2753    ;
2754    Int_t  PamLevel2::GetNTracks(const char* alg) {
2755    
2756    
2757    //    cout << " trk_nuc_obj->GetEntries() "<<trk_nuc_obj->GetEntries()<<" trk2_nuc_obj->GetNTracks() "<<trk2_nuc_obj->GetNTracks()<<endl;
2758    
2759        TString s(alg);
2760    
2761        if(!s.CompareTo("") || !s.CompareTo("STD"))return trk2_obj->TrkLevel2::GetNTracks(); //standard algorythm
2762            
2763        if(s.Contains("EXTF", TString::kIgnoreCase) && EXT){
2764            if(s.Contains("NUC", TString::kIgnoreCase) && NUC)return trk_ext_nuc_obj->GetEntries();//ok
2765            return trk_ext_obj->GetEntries();//ok
2766        }
2767        if( s.Contains("EXT", TString::kIgnoreCase) && EXT) {
2768            if(s.Contains("NUC", TString::kIgnoreCase) && NUC)
2769                return (trk2_nuc_obj->TrkLevel2::GetNTracks() ?  trk2_nuc_obj->TrkLevel2::GetNTracks() :  trk_ext_nuc_obj->GetEntries() );  
2770            return (trk2_obj->TrkLevel2::GetNTracks() ?  trk2_obj->TrkLevel2::GetNTracks() :  trk_ext_obj->GetEntries() );
2771        }
2772        if(s.Contains("NUC", TString::kIgnoreCase) && NUC )
2773            return trk2_nuc_obj->TrkLevel2::GetNTracks();
2774        
2775        cout << "Int_t  PamLevel2::GetNTracks("<<alg<<") -- unrecognised algorithm"<<endl;
2776    
2777        return 0;
2778    
2779    }
2780    
2781    
2782    //--------------------------------------
2783    //
2784    //
2785    //--------------------------------------
2786    /**
2787     * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().
2788     * @param it Track number, ranging from 0 to GetNTracks().
2789     */
2790    PamTrack *PamLevel2::GetTrackImageOld(int it) {
2791    
2792      //  *-*-*-*-*-*-*-*-*-*-*-*-*
2793      SortTracks();
2794      //  *-*-*-*-*-*-*-*-*-*-*-*-*
2795      if (!timage)
2796        return 0;
2797      if (!timage->GetEntries())
2798        return 0;
2799    
2800      PamTrack *image = 0;
2801    
2802      if (it >= 0 && it < trk2_obj->TrkLevel2::GetNTracks()) {
2803        TClonesArray &t = *(tsorted);
2804        PamTrack *temp = (PamTrack*) t[it];
2805        if (temp->GetTrkTrack()->HasImage()) {
2806          TClonesArray & t = *(timage);
2807          image = (PamTrack*) t[it];
2808        }
2809        else {
2810          //            cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;
2811        };
2812      }
2813      else {
2814        cout << "PamLevel2::GetTrackImageOld(int) : Tracker track SeqNo " << it << " does not exist (GetNTracks() = "
2815            << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
2816      };
2817    
2818      return image;
2819    }
2820    /**
2821     * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().
2822     * @param it Track number, ranging from 0 to GetNTracks().
2823     * @param alg Algorythm, see SetTrakingAlgorythm(char *alg) for explanation.
2824     */
2825    PamTrack *PamLevel2::GetTrackImage(int it, const char* alg) {
2826    
2827        TString s(alg);
2828        if(!s.CompareTo("") || !s.CompareTo("STD"))return GetTrackImageOld(it); //old algorythm
2829    
2830    
2831        SortTracksNew();
2832        // >> fill tsorted, timage and text
2833    
2834        if ( ( !s.Contains("EXTF", TString::kIgnoreCase) || !EXT )){ //not forced exteded-alg requested (or exteded-data missing)
2835    
2836            if( s.Contains("NUC")){
2837                if(
2838                    tsorted_nuc &&
2839                    it < tsorted_nuc->GetEntries() &&                             //enough tracks found
2840                    it >= 0 &&                                                //valid index
2841                    true){
2842                    TClonesArray &t = *(tsorted_nuc);
2843                    PamTrack *temp = (PamTrack*) t[it];
2844                    if (temp->GetTrkTrack()->HasImage()) {
2845                        return (PamTrack*)((*timage_nuc)[it]);                 //ok return the track
2846                    }else{
2847                        return NULL;
2848                    }
2849                    
2850                }
2851            }else{
2852                if(
2853                    tsorted &&
2854                    it < tsorted->GetEntries() &&                             //enough tracks found
2855                    it >= 0 &&                                                //valid index
2856                    true ){
2857                    TClonesArray &t = *(tsorted);
2858                    PamTrack *temp = (PamTrack*) t[it];
2859                    if (temp->GetTrkTrack()->HasImage()) {              
2860                        return (PamTrack*)((*timage)[it]);                 //ok return the track
2861                    }else{
2862                        return NULL;
2863                    }
2864                }
2865            }
2866    
2867        }
2868    
2869        cout << "PamTrack *PamLevel2::GetTrackImage("<<it<<","<<alg<<") -- wrong track number or unrecognised algorithm"<<endl;
2870          
2871        return NULL;
2872    
2873    }
2874    ;
2875    
2876  //--------------------------------------  //--------------------------------------
2877  //  //
2878  //  //
# Line 1241  PamTrack *PamLevel2::GetTrackImage(int i Line 2883  PamTrack *PamLevel2::GetTrackImage(int i
2883   * @param detlist String to select trees to be included   * @param detlist String to select trees to be included
2884   * @return Pointer to a TTree   * @return Pointer to a TTree
2885   */   */
2886  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ){  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist) {
2887    
2888      if (detlist.Contains("+AUTO", TString::kIgnoreCase)) {
2889        cout << "+AUTO" << endl;
2890        GetWhichTrees(f);
2891      };
2892      SetWhichTrees(detlist);
2893    
2894  //     if( !detlist.IsNull() ) SetWhichTrees(detlist);    if (pam_tree) {
 //     else                    GetWhichTrees(f);  
   if ( pam_tree ){  
2895      printf("WARNING: TTree *PamLevel2::GetPamTree(TFile *fl, TString detlist) -- pam_tree already exists!\n ");      printf("WARNING: TTree *PamLevel2::GetPamTree(TFile *fl, TString detlist) -- pam_tree already exists!\n ");
2896      return pam_tree;      return pam_tree;
2897    };    };
2898    //    //
2899    
2900      cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;    cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte " << endl;
2901    
2902      SetWhichTrees(detlist);    //    SetWhichTrees(detlist);
2903    
2904      TTree *Trout =0;    TTree *Trout = 0;
2905    
2906      TString fname = f->GetName();    TString fname = f->GetName();
2907      if(!CheckLevel2File(fname))return NULL;    if (!CheckLevel2File(fname))
2908        return NULL;
2909    
2910  //    UInt_t *found=0;        //    UInt_t *found=0;
2911    
2912      cout<< "GetPamTree(TFile*,TString): detector list --> ";    cout << "GetPamTree(TFile*,TString): detector list --> ";
2913      if(TRK1)cout<<"TRK1 ";    if (TRK1)
2914      if(TRK2)cout<<"TRK2 ";      cout << "TRK1 ";
2915      if(TRKh)cout<<"TRKH ";    if (TRK2)
2916      if(CAL1)cout<<"CAL1 ";      cout << "TRK2 ";
2917      if(CAL2)cout<<"CAL2 ";    if (TRKh)
2918      if(TOF)cout<<"TOF ";      cout << "TRKH ";
2919      if(TRG)cout<<"TRG ";    if (CAL1)
2920      if(AC)cout<<"AC ";      cout << "CAL1 ";
2921      if(ND)cout<<"ND ";    if (CAL2)
2922      if(S4)cout<<"S4 ";      cout << "CAL2 ";
2923      if(ORB)cout<<"ORB ";    if (TOF)
2924      cout << endl;      cout << "TOF ";
2925      if(SELLI)cout<<">>> Found selection-list <<<"<<endl;    if (TRG)
2926        cout << "TRG ";
2927      f->cd();    if (AC)
2928        cout << "AC ";
2929  // Tracker    if (ND)
2930      TTree *T = (TTree*)f->Get("Tracker");      cout << "ND ";
2931      if(T && (TRK2||TRK1||TRKh)) {    if (S4)
2932          if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));      cout << "S4 ";
2933  //      else    T->SetBranchStatus("TrkLevel2",0,found);    if (ORB)
2934          if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;      cout << "ORB ";
2935          if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));    if (GP)
2936  //      else    T->SetBranchStatus("TrkLevel1",0,found);      cout << "GP ";
2937          if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;    cout << endl;
2938          if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));    if (SELLI && SELLI != 2)
2939  //      else    T->SetBranchStatus("TrkHough",0,found);      cout << ">>> Found selection-list <<<" << endl; //EMILIANO
2940          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;  
2941          if(!Trout)Trout=T;    f->cd();
2942          else Trout->AddFriend(T);  
2943      }else{    // Tracker
2944          cout << "Tracker      : missing tree"<<endl;    TTree *T = (TTree*) f->Get("Tracker");
2945      };    if (T && (TRK2 || TRK1 || TRKh)) {
2946      // Calorimeter      if (TRK2)
2947      TTree *C = (TTree*)f->Get("Calorimeter");        T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
2948      if(C && (CAL2||CAL1)) {      //  else    T->SetBranchStatus("TrkLevel2",0,found);
2949          if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));      if (TRK2)
2950  //      else    C->SetBranchStatus("CaloLevel2",0,found);        cout << "Tracker      : set branch address TrkLevel2" << endl;
2951          if(CAL2)cout << "Calorimeter  : set branch address CaloLevel2"<<endl;      if (TRK1)
2952          if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));        T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
2953  //      else    C->SetBranchStatus("CaloLevel1",0,found);      //  else    T->SetBranchStatus("TrkLevel1",0,found);
2954          if(CAL1)cout << "Calorimeter  : set branch address CaloLevel1"<<endl;      if (TRK1)
2955          if(!Trout)Trout=C;        cout << "Tracker      : set branch address TrkLevel1" << endl;
2956          else Trout->AddFriend(C);      if (TRKh)
2957      }else{        T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
2958          cout << "Calorimeter  : missing tree"<<endl;      //  else    T->SetBranchStatus("TrkHough",0,found);
2959      };      if (TRKh)
2960          cout << "Tracker      : set branch address TrkHough" << endl;
2961      // ToF          if (!Trout)
2962      TTree *O = (TTree*)f->Get("ToF");        Trout = T;
2963      if(O && TOF) {      else
2964          O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));          Trout->AddFriend(T);
2965          cout << "ToF          : set branch address ToFLevel2"<<endl;    }
2966          if(!Trout)Trout=O;    else {
2967          else Trout->AddFriend(O);      cout << "Tracker      : missing tree" << endl;
2968      }else{    };
2969          cout << "ToF         : missing tree"<<endl;    // Calorimeter
2970      };    TTree *C = (TTree*) f->Get("Calorimeter");
2971      // Trigger    if (C && (CAL2 || CAL1)) {
2972      TTree *R = (TTree*)f->Get("Trigger");      if (CAL2)
2973      if(R && TRG) {        C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
2974          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));      //  else    C->SetBranchStatus("CaloLevel2",0,found);
2975          cout << "Trigger      : set branch address TrigLevel2"<<endl;      if (CAL2)
2976          if(!Trout)Trout=R;        cout << "Calorimeter  : set branch address CaloLevel2" << endl;
2977          else Trout->AddFriend(R);      if (CAL1)
2978      }else{        C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
2979          cout << "Trigger      : missing tree"<<endl;      //  else    C->SetBranchStatus("CaloLevel1",0,found);
2980      };      if (CAL1)
2981      // S4        cout << "Calorimeter  : set branch address CaloLevel1" << endl;
2982      TTree *S = (TTree*)f->Get("S4");      if (!Trout)
2983      if(S && S4) {        Trout = C;
2984          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));      else
2985          cout << "S4           : set branch address S4Level2"<<endl;        Trout->AddFriend(C);
2986          if(!Trout)Trout=S;    }
2987          else Trout->AddFriend(S);    else {
2988      }else{      cout << "Calorimeter  : missing tree" << endl;
2989          cout << "S4           : missing tree"<<endl;    };
2990      };  
2991      // Neutron Detector    // ToF
2992      TTree *N = (TTree*)f->Get("NeutronD");    TTree *O = (TTree*) f->Get("ToF");
2993      if(N && ND) {    if (O && TOF) {
2994          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));      O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
2995          cout << "NeutronD     : set branch address NDLevel2"<<endl;      cout << "ToF          : set branch address ToFLevel2" << endl;
2996          if(!Trout)Trout=N;      if (!Trout)
2997          else Trout->AddFriend(N);        Trout = O;
2998      }else{      else
2999          cout << "NeutronD     : missing tree"<<endl;        Trout->AddFriend(O);
3000      };    }
3001      // Anticounters    else {
3002      TTree *A = (TTree*)f->Get("Anticounter");      cout << "ToF         : missing tree" << endl;
3003      if(A && AC) {    };
3004          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));    // Trigger
3005          cout << "Anticounter  : set branch address AcLevel2"<<endl;    TTree *R = (TTree*) f->Get("Trigger");
3006          if(!Trout)Trout=A;    if (R && TRG) {
3007          else Trout->AddFriend(A);      R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
3008      }else{      cout << "Trigger      : set branch address TrigLevel2" << endl;
3009          cout << "Anticounter  : missing tree"<<endl;      if (!Trout)
3010      };        Trout = R;
3011      // Orbital Info      else
3012      TTree *B = (TTree*)f->Get("OrbitalInfo");        Trout->AddFriend(R);
3013      if(B && ORB) {    }
3014          B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));    else {
3015          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;      cout << "Trigger      : missing tree" << endl;
3016          if(!Trout)Trout=B;    };
3017          else Trout->AddFriend(B);    // S4
3018      }else{    TTree *S = (TTree*) f->Get("S4");
3019          cout << "OrbitalInfo  : missing tree"<<endl;    if (S && S4) {
3020      };      S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
3021        cout << "S4           : set branch address S4Level2" << endl;
3022      TTree *L = (TTree*)f->Get("SelectionList");      if (!Trout)
3023      if(L && SELLI==1) {        Trout = S;
3024  //      L->SetBranchAddress("RunEntry",&irun);      else
3025  //      cout << "SelectionList: set branch address RunEntry"<<endl;        Trout->AddFriend(S);
3026  //      L->SetBranchAddress("EventEntry",&irunentry);    }
3027  //      cout << "SelectionList: set branch address EventEntry"<<endl;    else {
3028  //      if(!Trout)Trout=O;      cout << "S4           : missing tree" << endl;
3029  //      else Trout->AddFriend("SelectionList");    };
3030          cout << " TTree *PamLevel2::GetPamTree(TFile, TString) >>> SelectionList not implemente!!!!"<<endl;    // Neutron Detector
3031          sel_tree = 0;    TTree *N = (TTree*) f->Get("NeutronD");
3032      }else{    if (N && ND) {
3033          cout << "SelectionList  : missing tree"<<endl;      N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
3034      };      cout << "NeutronD     : set branch address NDLevel2" << endl;
3035            if (!Trout)
3036      cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;        Trout = N;
3037        else
3038          Trout->AddFriend(N);
3039      }
3040      else {
3041        cout << "NeutronD     : missing tree" << endl;
3042      };
3043      // Anticounters
3044      TTree *A = (TTree*) f->Get("Anticounter");
3045      if (A && AC) {
3046        A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
3047        cout << "Anticounter  : set branch address AcLevel2" << endl;
3048        if (!Trout)
3049          Trout = A;
3050        else
3051          Trout->AddFriend(A);
3052      }
3053      else {
3054        cout << "Anticounter  : missing tree" << endl;
3055      };
3056      // Orbital Info
3057      TTree *B = (TTree*) f->Get("OrbitalInfo");
3058      if (B && ORB) {
3059        B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
3060        cout << "OrbitalInfo  : set branch address OrbitalInfo" << endl;
3061        if (!Trout)
3062          Trout = B;
3063        else
3064          Trout->AddFriend(B);
3065      }
3066      else {
3067        cout << "OrbitalInfo  : missing tree" << endl;
3068      };
3069    
3070      // GPamela
3071      TTree *G = (TTree*) f->Get("h20");
3072      if (G && GP) {
3073        if (!gp_obj)
3074          gp_obj = new GPamela();
3075        //      ------------------------------------
3076        //      ATTENZIONE!!!
3077        //      non so per quale arcano motivo,
3078        //      per l'albero di gpamela il branch address lo devo settare
3079        //      DOPO aver fatto friend
3080        //      FGRRRVZZZZUTSALKJMSLKJ!!!
3081        //      ------------------------------------
3082        //  gp_obj->SetBranchAddress(G); //ho dovuto fare in maniera diversa dagli altri
3083        //  cout << "h20          : set branch address GPamela "<<endl;
3084        if (!Trout)
3085          Trout = G;
3086        else
3087          Trout->AddFriend(G);
3088      }
3089      else {
3090        //  cout << "h20          : missing tree"<<endl;
3091      };
3092    
3093      TTree *L = (TTree*) f->Get("SelectionList");
3094      if (L && SELLI == 1) {
3095        cout << " TTree *PamLevel2::GetPamTree(TFile, TString) >>> SelectionList not implemented!!!!" << endl;
3096        sel_tree = 0;
3097      }
3098      else {
3099        cout << "SelectionList  : missing tree" << endl;
3100      };
3101    
3102      cout << endl << " Number of entries: " << Trout->GetEntries() << endl << endl;
3103    
3104      //      ------------------------------------
3105      //      ATTENZIONE!!!
3106      //      non so per quale arcano motivo,
3107      //      per l'albero di gpamela il branch address lo devo settare
3108      //      DOPO aver fatto friend
3109      //      FGRRRVZZZZUTSALKJMSLKJ!!!
3110      //      ------------------------------------
3111      if (G && GP) {
3112        gp_obj->SetBranchAddress(Trout); //ho dovuto fare in maniera diversa dagli altri
3113        cout << "h20          : set branch address GPamela " << endl;
3114      }
3115      else {
3116        cout << "h20          : missing tree" << endl;
3117      };
3118    
3119      pam_tree = (TChain*) Trout;
3120    
3121      return Trout;
3122    
     pam_tree = (TChain*)Trout;  
       
     return Trout;  
       
3123  }  }
3124  //--------------------------------------  //--------------------------------------
3125  //  //
# Line 1404  TTree *PamLevel2::GetPamTree(TFile *f, T Line 3132  TTree *PamLevel2::GetPamTree(TFile *f, T
3132   * @return Pointer to a TList of TSystemFiles   * @return Pointer to a TList of TSystemFiles
3133   * If no input file list is given , all the Level2 files inside the directory are processed.   * If no input file list is given , all the Level2 files inside the directory are processed.
3134   */   */
3135  TList*  PamLevel2::GetListOfLevel2Files(TString ddir, TString flisttxt = ""){  TList* PamLevel2::GetListOfLevel2Files(TString ddir, TString flisttxt = "") {
3136            
3137      TString wdir = gSystem->WorkingDirectory();    TString wdir = gSystem->WorkingDirectory();
3138        
3139      //    if(ddir=="")ddir = wdir;    if (ddir != "") {
3140  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);      cout << "Level2 data directory : " << ddir << endl;
3141      if ( ddir != ""){    }
3142        cout << "Level2 data directory : "<<  ddir << endl;    else {
3143      } else {      cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory "
3144        cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory " << endl;          << endl;
3145      };    };
3146      TList *contents  = new TList; // create output list    TList *contents = new TList; // create output list
3147      contents->SetOwner();    contents->SetOwner();
3148        
3149  //    char *fullpath;    // --------------------------------------
3150  //    const char *fullpath;    // case 1 : input file/file-list provided
3151          // --------------------------------------
3152      // if no input file list is given:      if (flisttxt != "") {
3153      if ( flisttxt != "" ){  
3154                // --------------------------------------
3155  //      if( !gSystem->IsFileInIncludePath(flisttxt,&fullpath) ){              // a list of files given as input
3156  //      if( !(fullpath = gSystem->FindFile("./",flisttxt)) ){      // --------------------------------------
3157  //          cout <<"File "<<flisttxt<<" not found"<<endl;      if (!flisttxt.EndsWith(".root")) {
3158  //          return 0;  
3159  //      }              TString dir = gSystem->DirName(flisttxt);
3160  //      flisttxt = fullpath;        //            cout << " List directory "<<dir<<endl;
3161        if ( !flisttxt.EndsWith(".root") ){        if (dir == "." || dir == "")
3162            flisttxt = gSystem->ConcatFileName(wdir.Data(), gSystem->BaseName(flisttxt));
3163          flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));        //                flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
3164    
3165          if( !gSystem->ChangeDirectory(ddir) ){        if (!gSystem->ChangeDirectory(ddir)) {
3166              cout << "Cannot change directory : "<<ddir<<endl;          cout << "Cannot change directory : " << ddir << endl;
3167              return 0;          return 0;
3168          }        }
3169            
3170          cout <<"Input file list : " << flisttxt <<endl;        cout << "Input file list : " << flisttxt << endl;
3171          ifstream in;        ifstream in;
3172          in.open(flisttxt, ios::in); //open input file list        in.open(flisttxt, ios::in); //open input file list
3173          if(!in.good()){        if (!in.good()) {
3174              cout <<" ERROR opening the file "<<endl;          cout << " TList*  PamLevel2::GetListOfLevel2Files(TString, TString) --> ERROR opening the file " << endl;
3175              gSystem->ChangeDirectory(wdir); // back to the working directory          gSystem->ChangeDirectory(wdir); // back to the working directory
3176              return 0;          return 0;
3177          }              }
3178          int line=0;        int line = 0;
3179          while (1) {        // ...........................
3180              TString file;        // loop over file-list records
3181              in >> file;        // ...........................
3182              if (!in.good()) break;        while (1) {
3183              line++;          TString file;
3184  //          cout <<"(1) " << file << endl;          in >> file;
3185              if(file.IsNull()){          if (!in.good())
3186                  cout << "-- list interrupted at line "<<line <<endl;            break;
3187                  break;          line++;
3188              }          cout << "(*) " << file << endl;
3189              if(file.Contains("#"))file = file(0,file.First("#"));          if (file.IsNull()) {
3190  //          cout <<"(2) " << file << endl;            cout << "-- list interrupted at line " << line << endl;
3191  //          if( gSystem->IsFileInIncludePath(file,&fullpath) ){            break;
3192  //          if( (fullpath = gSystem->FindFile(ddir,file)) ){          }
3193              if( file.EndsWith(".root") ){          if (file.Contains("#"))
3194                TString filedir;            file = file(0, file.First("#"));
3195                if (ddir != ""){          //
3196                  filedir = ddir; // take the input dir          // take only root files
3197                } else {          //
3198                  gSystem->ChangeDirectory(wdir); // back to the working directory          if (file.EndsWith(".root")) {
3199                  filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir            TString filedir;
3200                };            cout << ddir << endl;
3201                  char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir),gSystem->BaseName(file));            if ( ddir != "" ) {
3202                  contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list              filedir = ddir; // take the input dir
3203                  delete fullpath;            }
3204              }            else {
3205  //          }else{              gSystem->ChangeDirectory(wdir); // back to the working directory
3206  //              if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;              filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir
3207  //          };            };
3208          };            filedir.Append("/");
3209          in.close();            //          char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file));
3210        } else {            char *fullpath = gSystem->ConcatFileName(filedir.Data(), gSystem->BaseName(file));
3211            if(flisttxt.Contains("#"))flisttxt = flisttxt(0,flisttxt.First("#"));            contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));// add file to the list
3212            char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));            cout << fullpath << endl;
3213            contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list            delete fullpath;
3214            delete fullpath;          }
3215        };                  //          }else{
3216      }else{          //              if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;
3217                    //          };
3218          cout << "No input file list given."<<endl;        };
3219          cout << "Check for existing root files."<<endl;        in.close();
3220  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;        // --------------------------------------
3221          if ( ddir == "" ){        // a single root file given as input
3222            ddir = wdir;        // --------------------------------------
3223            cout << "Level2 data directory : "<<  ddir << endl;      }
3224          };      else {
3225          if (flisttxt.Contains("#"))
3226          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);          flisttxt = flisttxt(0, flisttxt.First("#"));
3227          TList *temp = datadir->GetListOfFiles();        char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt), gSystem->BaseName(flisttxt));
3228  //              temp->Print();        contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));// add file to the list
3229  //              cout << "*************************************" << endl;        delete fullpath;
3230                };
3231          TIter next(temp);      // ---------------------------------------------------------------------------------
3232          TSystemFile *questo = 0;      // case 2 : no input file/file-list provided --> read all files insede the directory
3233                // ---------------------------------------------------------------------------------
3234              }
3235          while ( (questo = (TSystemFile*) next()) ) {    else {
3236              TString name =  questo-> GetName();  
3237              if( name.EndsWith(".root") ){      cout << "No input file list given." << endl;
3238  //              const char *fullpath = gSystem->FindFile(ddir,name);      cout << "Check for existing root files." << endl;
3239  //              char *fullpath;      //          cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;
3240  //              gSystem->IsFileInIncludePath(name,&fullpath);      if (ddir == "") {
3241                  char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir),gSystem->BaseName(name));        ddir = wdir;
3242                  contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));        cout << "Level2 data directory : " << ddir << endl;
3243                  delete fullpath;      };
3244              };  
3245          }      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir), ddir);
3246          delete temp;      TList *temp = datadir->GetListOfFiles();
3247          delete datadir;      if (!temp)
3248                  return 0;
3249      };      //          temp->Print();
3250      gSystem->ChangeDirectory(wdir); // back to the working directory      //          cout << "*************************************" << endl;
3251  //      cout << endl << "Selected files:" << endl;  
3252  //      contents->Print();      TIter next(temp);
3253      cout << contents->GetEntries()<<" files \n";      TSystemFile *questo = 0;
3254  //      cout << endl;  
3255  //      cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;      while ((questo = (TSystemFile*) next())) {
3256      return contents;        TString name = questo-> GetName();
3257  };        if (name.EndsWith(".root")) {
3258            //              const char *fullpath = gSystem->FindFile(ddir,name);
3259            //              char *fullpath;
3260            //              gSystem->IsFileInIncludePath(name,&fullpath);
3261            char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir), gSystem->BaseName(name));
3262            contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));
3263            delete fullpath;
3264          };
3265        }
3266        delete temp;
3267        delete datadir;
3268    
3269      };
3270      gSystem->ChangeDirectory(wdir); // back to the working directory
3271      //    cout << endl << "Selected files:" << endl;
3272      //    contents->Print();
3273      cout << contents->GetEntries() << " files \n";
3274      //    cout << endl;
3275      //    cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;
3276      return contents;
3277    }
3278    ;
3279  //--------------------------------------  //--------------------------------------
3280  //  //
3281  //  //
3282  //--------------------------------------  //--------------------------------------
3283  /**  /**
3284   * Get the Pamela detector chains from a list of files and make them friends.   * Get the Pamela detector chains from a list of files and make them friends.
3285   * @param fl Pointer to a TList of TSystemFiles   * @param fl Pointer to a TList of TSystemFiles
3286   * @param detlist String to select trees to be included   * @param detlist String to select trees to be included
3287   * @return Pointer to a TChain   * @return Pointer to a TChain
3288   */   */
3289  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) {
3290    //    //
3291    //    //
3292    //    //
3293    if ( pam_tree ){    if (pam_tree) {
3294      printf("WARNING: TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) -- pam_tree already exists!\n ");      printf("WARNING: TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) -- pam_tree already exists!\n ");
3295      return pam_tree;      return pam_tree;
3296    };    };
3297    //    //
       
     TChain *Trout =0;  
3298    
3299  //    if( !detlist.IsNull() )SetWhichTrees(detlist);    TChain *Trout = 0;
     SetWhichTrees(detlist);  
       
     cout<< "GetPamTree(TList*,TString): input detector list --> ";  
     if(TRK1)cout<<"TRK1 ";  
     if(TRK2)cout<<"TRK2 ";  
     if(TRKh)cout<<"TRKH ";  
     if(CAL1)cout<<"CAL1 ";  
     if(CAL2)cout<<"CAL2 ";  
     if(TOF)cout<<"TOF ";  
     if(TRG)cout<<"TRG ";  
     if(AC)cout<<"AC ";  
     if(ND)cout<<"ND ";  
     if(S4)cout<<"S4 ";  
     if(ORB)cout<<"ORB ";  
     cout << endl;  
   
      TChain *T = 0;      
      TChain *C = 0;  
      TChain *O = 0;  
      TChain *R = 0;  
      TChain *S = 0;  
      TChain *N = 0;  
      TChain *A = 0;  
      TChain *B = 0;  
3300    
3301       TChain *L = 0;    // -------------------------------------------
3302          // set flags to include/exclude trees/branches
3303      if(TRK2||TRK1||TRKh) T = new TChain("Tracker");        // -------------------------------------------
3304      if(CAL2||CAL1)       C = new TChain("Calorimeter");    if (detlist.Contains("+AUTO", TString::kIgnoreCase)) {
3305      if(TOF)              O = new TChain("ToF");      if (fl->GetEntries() > 0) {
3306      if(TRG)              R = new TChain("Trigger");        cout << "+AUTO" << endl;
3307      if(S4)               S = new TChain("S4");        TFile *fprimo = new TFile(fl->At(0)->GetName());
3308      if(ND)               N = new TChain("NeutronD");        GetWhichTrees(fprimo);
3309      if(AC)               A = new TChain("Anticounter");        fprimo->Close();// AAAAARGGGGGHHHHH!!!!!!! non commentare questa riga, altrimenti si incasina il TChain
3310      if(ORB)              B = new TChain("OrbitalInfo");        fprimo->Delete();
3311       L = new TChain("SelectionList");      }
3312          };
3313      // loop over files and create chains            SetWhichTrees(detlist);
     TIter next(fl);  
     TSystemFile *questo = 0;  
     while ( (questo = (TSystemFile*) next()) ) {  
         TString name =  questo->GetName();  
         cout << "File: "<< name << endl;  
         if( CheckLevel2File(name) ){  
             if(TRK2||TRK1||TRKh) T->Add(name);  
             if(CAL1||CAL2)       C->Add(name);  
             if(TOF)              O->Add(name);  
             if(TRG)              R->Add(name);  
             if(S4)               S->Add(name);  
             if(ND)               N->Add(name);  
             if(AC)               A->Add(name);  
             if(ORB)              B->Add(name);  
             if(SELLI==1)         L->Add(name);  
         };  
     };  
       
     cout << "done chain \n";  
     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;  
3314    
3315  //    UInt_t *found=0;    // -------------------------------------------
3316  // Tracker    // build chains
3317      if(T && (TRK2||TRK1||TRKh)) {    // -------------------------------------------
3318          if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));    TChain *T = 0;
3319  //      else    T->SetBranchStatus("TrkLevel2",0,found);    TChain *C = 0;
3320          if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;    TChain *O = 0;
3321          if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));    TChain *R = 0;
3322  //      else    T->SetBranchStatus("TrkLevel1",0,found);    TChain *S = 0;
3323          if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;    TChain *N = 0;
3324          if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));    TChain *A = 0;
3325  //      else    T->SetBranchStatus("TrkHough",0,found);    TChain *B = 0;
3326          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;    TChain *G = 0;
3327          if(!Trout)Trout=T;  
3328          else Trout->AddFriend("Tracker");    TChain *L = 0;
3329      }else{    TChain *P = 0;
3330          cout << "Tracker      : missing tree"<<endl;  
3331      };    if (TRK2 || TRK1 || TRKh)
3332      // Calorimeter      T = new TChain("Tracker");
3333      if(C && (CAL2||CAL1)) {    if (CAL2 || CAL1)
3334          if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));      C = new TChain("Calorimeter");
3335  //      else    C->SetBranchStatus("CaloLevel2",0,found);    if (TOF)
3336          if(CAL2)cout << "Calorimeter  : set branch address CaloLevel2"<<endl;      O = new TChain("ToF");
3337          if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));    if (TRG)
3338  //      else    C->SetBranchStatus("CaloLevel1",0,found);      R = new TChain("Trigger");
3339          if(CAL1)cout << "Calorimeter  : set branch address CaloLevel1"<<endl;    if (S4)
3340          if(!Trout)Trout=C;      S = new TChain("S4");
3341          else Trout->AddFriend("Calorimeter");    if (ND)
3342      }else{      N = new TChain("NeutronD");
3343          cout << "Calorimeter  : missing tree"<<endl;    if (AC)
3344      };      A = new TChain("Anticounter");
3345      // ToF        if (ORB)
3346      if(O && TOF) {      B = new TChain("OrbitalInfo");
3347          O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));    if (GP)
3348          cout << "ToF          : set branch address ToFLevel2"<<endl;      G = new TChain("h20");
3349          if(!Trout)Trout=O;    if (PROC)
3350          else Trout->AddFriend("ToF");      P = new TChain("ProcessingInfo");
3351      }else{    L = new TChain("SelectionList");
3352          cout << "ToF         : missing tree"<<endl;  
3353      };    // loop over files and create chains
3354      // Trigger    TIter next(fl);
3355      if(R && TRG) {    TSystemFile *questo = 0;
3356          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));    while ((questo = (TSystemFile*) next())) {
3357          cout << "Trigger      : set branch address TrigLevel2"<<endl;      TString name = questo->GetName();
3358          if(!Trout)Trout=O;      cout << "File: " << name << endl;
3359          else Trout->AddFriend("Trigger");      if (CheckLevel2File(name)) {
3360      }else{        if (TRK2 || TRK1 || TRKh)
3361          cout << "Trigger      : missing tree"<<endl;          T->Add(name);
3362      };        if (CAL1 || CAL2)
3363      // S4          C->Add(name);
3364      if(S && S4) {        if (TOF)
3365          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));          O->Add(name);
3366          cout << "S4           : set branch address S4Level2"<<endl;        if (TRG)
3367          if(!Trout)Trout=O;          R->Add(name);
3368          else Trout->AddFriend("S4");        if (S4)
3369      }else{          S->Add(name);
3370          cout << "S4           : missing tree"<<endl;        if (ND)
3371      };          N->Add(name);
3372      // Neutron Detector        if (AC)
3373      if(N && ND) {          A->Add(name);
3374          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));        if (ORB)
3375          cout << "NeutronD     : set branch address NDLevel2"<<endl;          B->Add(name);
3376          if(!Trout)Trout=O;        if (GP)
3377          else Trout->AddFriend("NeutronD");          G->Add(name);
3378      }else{        if (P)
3379          cout << "NeutronD     : missing tree"<<endl;          P->Add(name);
3380      };        if (SELLI == 1)
3381      // Anticounters          L->Add(name);
3382      if(A && AC) {       };
3383          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));    };
         cout << "Anticounter  : set branch address AcLevel2"<<endl;  
         if(!Trout)Trout=O;  
         else Trout->AddFriend("Anticounter");  
     }else{  
         cout << "Anticounter  : missing tree"<<endl;  
     };  
     // Orbital Info  
     if(B && ORB) {  
         B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));  
         cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;  
         if(!Trout)Trout=O;  
         else Trout->AddFriend("OrbitalInfo");  
     }else{  
         cout << "OrbitalInfo  : missing tree"<<endl;  
     };  
     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;  
   
     // Selection List  
     if(L && SELLI==1) {  
         cout<<">>> Found selection-list <<<"<<endl;  
         L->SetBranchAddress("RunEntry",&irun);  
         cout << "SelectionList: set branch address RunEntry"<<endl;  
         L->SetBranchAddress("EventEntry",&irunentry);  
         cout << "SelectionList: set branch address EventEntry"<<endl;  
         sel_tree = L;  
 //      if(!Trout)Trout=O;  
 //      else Trout->AddFriend("SelectionList");  
         cout << "----------------------------------------------------" <<endl;  
     }else{  
 //      cout << "SelectionList  : missing tree"<<endl;  
         if(L)L->Delete();  
     };  
       
 //    cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;  
3384    
3385      pam_tree = Trout;    cout << "done data-tree chains  "<<  T->GetNtrees() <<" \n";
3386      cout << "----------------------------------------------------" << endl;
3387    
3388      return Trout;    // -------------------------------------------
3389  }    // make friends
3390      // -------------------------------------------
3391    
3392      // Tracker
3393      cout << "Friends: " << endl;
3394      if (T && (TRK2 || TRK1 || TRKh)) {
3395        if (!Trout)
3396          Trout = T;
3397        else
3398          Trout->AddFriend("Tracker");
3399        //  cout << "+Tacker"<<endl;
3400      };
3401      // Calorimeter
3402      if (C && (CAL2 || CAL1)) {
3403        if (!Trout)
3404          Trout = C;
3405        else
3406          Trout->AddFriend("Calorimeter");
3407        //  cout << "+Calorimeter"<<endl;
3408      };
3409      // ToF
3410      if (O && TOF) {
3411        if (!Trout)
3412          Trout = O;
3413        else
3414          Trout->AddFriend("ToF");
3415        //  cout << "+ToF"<<endl;
3416      };
3417      // Trigger
3418      if (R && TRG) {
3419        if (!Trout)
3420          Trout = O;
3421        else
3422          Trout->AddFriend("Trigger");
3423        //  cout << "+Trigger"<<endl;
3424      };
3425      // S4
3426      if (S && S4) {
3427        if (!Trout)
3428          Trout = O;
3429        else
3430          Trout->AddFriend("S4");
3431        //  cout << "+S4"<<endl;
3432      };
3433      // Neutron Detector
3434      if (N && ND) {
3435        if (!Trout)
3436          Trout = O;
3437        else
3438          Trout->AddFriend("NeutronD");
3439        //  cout << "+NeutronD"<<endl;
3440      };
3441      // Anticounters
3442      if (A && AC) {
3443        if (!Trout)
3444          Trout = O;
3445        else
3446          Trout->AddFriend("Anticounter");
3447        //  cout << "+Anticounter"<<endl;
3448      };
3449      // Orbital Info
3450      if (B && ORB) {
3451        if (!Trout)
3452          Trout = O;
3453        else
3454          Trout->AddFriend("OrbitalInfo");
3455        //  cout << "+OrbitalInfo"<<endl;
3456      };
3457      // GPamela
3458      if (G && GP) {
3459        if (!Trout)
3460          Trout = G;
3461        else
3462          Trout->AddFriend("h20");
3463        //  cout << "+h20"<<endl;
3464      };
3465    
3466      //  =====================================
3467      //  SET BRANCH-ADDRESS AFTER CHAIN+FRIEND
3468      //  =====================================
3469      if( Trout->GetNtrees() )SetBranchAddress(Trout);
3470    
3471      //  ------------------------------------
3472      //  finally handle selection trees...
3473      //  (it is not friend of pamela tree)
3474      //  ------------------------------------
3475    
3476      cout << "----------------------------------------------------" << endl;
3477    
3478      // Selection List
3479      if (L && SELLI == 1) {
3480        cout << ">>> Found selection-list <<<" << endl;
3481        //  L->SetBranchAddress("RunEntry",&irun);
3482        L->SetBranchAddress("RunEntry", &irunt);//NEWNEW
3483        cout << "SelectionList: set branch address RunEntry" << endl;
3484        L->SetBranchAddress("EventEntry", &irunentry);
3485        cout << "SelectionList: set branch address EventEntry" << endl;
3486        /*    if ( L->GetBranch("L0EventEntry") ){
3487          hasL0EE = true;
3488          L->SetBranchAddress("L0EventEntry", &il0entry);
3489          cout << "SelectionList: set branch address L0EventEntry" << endl;
3490        } else {
3491          hasL0EE = false; // backward compatibility with old preselected files...
3492          }*/
3493        sel_tree = L;
3494        //          if(!Trout)Trout=O;
3495        //          else Trout->AddFriend("SelectionList");
3496        cout << "+SelectionList" << endl;
3497        cout << "----------------------------------------------------" << endl;
3498      }
3499      else {
3500        //  cout << "SelectionList  : missing tree"<<endl;
3501        if (L)
3502          L->Delete();
3503      };
3504    
3505      //ProcessingInfo EM
3506      if ( P && P->GetEntries() ){
3507        cout << "----------------------------------------------------" << endl;
3508        cout << ">>> Found ProcessingInfo <<<" << endl;
3509        //  L->SetBranchAddress("RunEntry",&irun);
3510        P->SetBranchAddress("ProcInfo", &proc_obj);//NEWNEW
3511        proc_tree = P;
3512      }
3513      //  --------------------------------------------
3514      //  return the pamela chain with all the friends
3515      //  --------------------------------------------
3516    
3517      pam_tree = Trout;
3518      return Trout;
3519    }
3520    
3521  //--------------------------------------  //--------------------------------------
3522  //  //
# Line 1727  TChain *PamLevel2::GetPamTree(TList *fl, Line 3525  TChain *PamLevel2::GetPamTree(TList *fl,
3525  /**  /**
3526   * Set branch addresses for Pamela friend trees   * Set branch addresses for Pamela friend trees
3527   */   */
3528  void PamLevel2::SetBranchAddress(TTree *t){  void PamLevel2::SetBranchAddress(TTree *t) {
3529    
3530      TRK2    = TRK2 & t->GetBranchStatus("TrkLevel2");  //   TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");
3531      TRK1    = TRK1 & t->GetBranchStatus("TrkLevel1");  //   TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");
3532      TRKh    = TRKh & t->GetBranchStatus("TrkHough");  //   TRKh = TRKh & t->GetBranchStatus("TrkHough");
3533      CAL2    = CAL2 & t->GetBranchStatus("CaloLevel2");  //   CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");
3534      CAL1    = CAL1 & t->GetBranchStatus("CaloLevel1");  //   CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");
3535      TOF    = TOF & t->GetBranchStatus("ToFLevel2");  //   TOF = TOF & t->GetBranchStatus("ToFLevel2");
3536      TRG    = TRG & t->GetBranchStatus("TrigLevel2");  //   TRG = TRG & t->GetBranchStatus("TrigLevel2");
3537      S4     = S4  & t->GetBranchStatus("S4Level2");  //   S4 = S4 & t->GetBranchStatus("S4Level2");
3538      ND     = ND  & t->GetBranchStatus("NDLevel2");  //   ND = ND & t->GetBranchStatus("NDLevel2");
3539      AC     = AC  & t->GetBranchStatus("AcLevel2");  //   AC = AC & t->GetBranchStatus("AcLevel2");
3540      ORB    = ORB & t->GetBranchStatus("OrbitalInfo");  //   ORB = ORB & t->GetBranchStatus("OrbitalInfo");
3541    //   GP = GP & t->GetBranchStatus("h20");
3542    
3543      // Tracker  
3544      if(TRK1) {    // Tracker
3545          t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel2"));    if (TRK1) {
3546          cout << "Tracker      : set branch address TrkLevel1"<<endl;      t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel2"));
3547      };      cout << "Tracker      : set branch address TrkLevel1" << endl;
3548      if(TRK2) {    };
3549          t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));    if (TRK2) {
3550          cout << "Tracker      : set branch address TrkLevel2"<<endl;      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));
3551      };      cout << "Tracker      : set branch address TrkLevel2" << endl;
3552      if(TRKh) {      NUC = t->GetBranchStatus("TrackNuclei");
3553          t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));      EXT = t->GetBranchStatus("RecoveredTrack") && (NUC ? t->GetBranchStatus("RecoveredTrackNuclei"): true );    
3554          cout << "Tracker      : set branch address TrkHough"<<endl;    };
3555      };    if (TRKh) {
3556            t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
3557      // Calorimeter      cout << "Tracker      : set branch address TrkHough" << endl;
3558      if(CAL1) {    };
3559          t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));  
3560          cout << "Calorimeter  : set branch address CaloLevel1"<<endl;    // Calorimeter
3561      };    if (CAL1) {
3562      if(CAL2) {      t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
3563          t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));      cout << "Calorimeter  : set branch address CaloLevel1" << endl;
3564          cout << "Calorimeter  : set branch address CaloLevel2"<<endl;    };
3565      };    if (CAL2) {
3566            t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
3567      // ToF          cout << "Calorimeter  : set branch address CaloLevel2" << endl;
3568      if(TOF) {    };
3569          t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));  
3570          cout << "ToF          : set branch address ToFLevel2"<<endl;    // ToF
3571      };    if (TOF) {
3572      // Trigger      t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
3573      if(TRG) {      cout << "ToF          : set branch address ToFLevel2" << endl;
3574          t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));    };
3575          cout << "Trigger      : set branch address TrigLevel2"<<endl;    // Trigger
3576      };    if (TRG) {
3577      // S4      t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
3578      if(S4) {      cout << "Trigger      : set branch address TrigLevel2" << endl;
3579          t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));    };
3580          cout << "S4           : set branch address S4Level2"<<endl;    // S4
3581      };    if (S4) {
3582      // Neutron Detector      t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
3583      if(ND) {      cout << "S4           : set branch address S4Level2" << endl;
3584          t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));    };
3585          cout << "NeutronD     : set branch address NDLevel2"<<endl;    // Neutron Detector
3586      };    if (ND) {
3587      // Anticounters      t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
3588      if(AC) {      cout << "NeutronD     : set branch address NDLevel2" << endl;
3589          t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));    };
3590          cout << "Anticounter  : set branch address AcLevel2"<<endl;    // Anticounters
3591      };    if (AC) {
3592      // OrbitalInfo      t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
3593      if(ORB) {      cout << "Anticounter  : set branch address AcLevel2" << endl;
3594          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));    };
3595          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;    // OrbitalInfo
3596      };    if (ORB) {
3597      // SelectionList      t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
3598      if(SELLI==1) {      cout << "OrbitalInfo  : set branch address OrbitalInfo" << endl;
3599          t->SetBranchAddress("RunEntry", &irun);    };
3600          cout << "SelectionList: set branch address RunEntry"<<endl;    // GPamela
3601          t->SetBranchAddress("EventEntry", &irunentry);    if (GP) {
3602          cout << "SelectionList: set branch address EventEntry"<<endl;      //  GetPointerTo("GPamela");
3603      };      if (!gp_obj)
3604              gp_obj = new GPamela();
3605        //  gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
3606        // //       t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
3607        if (SELLI)
3608          t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
3609        else
3610          gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
3611    
3612        cout << "h20          : set branch address GPamela " << endl;
3613      };
3614      if(NUC){
3615          
3616          cout << "Found nuclei-track branches" << endl;
3617    
3618          if( !trk_nuc_obj )trk_nuc_obj = new TClonesArray("TrkTrack");
3619          if( !calo_nuc_obj)calo_nuc_obj= new TClonesArray("CaloTrkVar");
3620          if( !tof_nuc_obj)tof_nuc_obj= new TClonesArray("ToFTrkVar");
3621          if( !orb_nuc_obj)orb_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3622          if (TRK2)t->                          SetBranchAddress("TrackNuclei",&trk_nuc_obj);
3623          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("TrackNuclei",&calo_nuc_obj);
3624          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("TrackNuclei",&tof_nuc_obj);
3625          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("TrackNuclei",&orb_nuc_obj);
3626    
3627          ///copy the vector content inside a "fake" object (all aother info are missing)    
3628    
3629          if( !trk2_nuc_obj )trk2_nuc_obj =  new TrkLevel2();
3630          if( !calo2_nuc_obj )calo2_nuc_obj =  new CaloLevel2();
3631          if( !tof2_nuc_obj )tof2_nuc_obj =  new ToFLevel2();
3632          if( !orb2_nuc_obj )orb2_nuc_obj =  new OrbitalInfo();
3633    //       *(trk2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*trk_nuc_obj);
3634    //       *(calo2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*calo_nuc_obj);
3635    //       *(tof2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*tof_nuc_obj);
3636    //       *(orb2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*orb_nuc_obj);
3637    
3638          trk2_nuc_obj->SetTrackArray(  trk_nuc_obj  );
3639          calo2_nuc_obj->SetTrackArray( calo_nuc_obj );
3640          tof2_nuc_obj->SetTrackArray(  tof_nuc_obj  );
3641          orb2_nuc_obj->SetTrackArray(  orb_nuc_obj  );
3642    
3643          
3644      }    
3645    
3646      if(EXT){
3647          
3648          cout << "Found extended tracking algorythm branches" << endl;
3649    
3650          if( !trk_ext_obj )trk_ext_obj = new TClonesArray("ExtTrack");
3651          if( !calo_ext_obj)calo_ext_obj= new TClonesArray("CaloTrkVar");
3652          if( !tof_ext_obj)tof_ext_obj= new TClonesArray("ToFTrkVar");
3653          if( !orb_ext_obj)orb_ext_obj= new TClonesArray("OrbitalInfoTrkVar");
3654          
3655          if (TRK2)t->                          SetBranchAddress("RecoveredTrack",&trk_ext_obj);
3656          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrack",&calo_ext_obj);
3657          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrack",&tof_ext_obj);
3658          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrack",&orb_ext_obj);
3659    
3660        
3661          if(NUC){
3662              if( !trk_ext_nuc_obj )trk_ext_nuc_obj = new TClonesArray("ExtTrack");
3663              if( !calo_ext_nuc_obj)calo_ext_nuc_obj= new TClonesArray("CaloTrkVar");
3664              if( !tof_ext_nuc_obj)tof_ext_nuc_obj= new TClonesArray("ToFTrkVar");
3665              if( !orb_ext_nuc_obj)orb_ext_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3666              if (TRK2)t->                          SetBranchAddress("RecoveredTrackNuclei",&trk_ext_nuc_obj);
3667              if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrackNuclei",&calo_ext_nuc_obj);
3668              if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrackNuclei",&tof_ext_nuc_obj);
3669              if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrackNuclei",&orb_ext_nuc_obj);
3670          }
3671      }    
3672  }  }
3673  /**  /**
3674   * Set branch addresses for Pamela friend trees   * Set branch addresses for Pamela friend trees
3675   */   */
3676  void PamLevel2::SetBranchAddress(TChain *t){  void PamLevel2::SetBranchAddress(TChain *t) {
3677    
3678      TRK2    = TRK2 & t->GetBranchStatus("TrkLevel2");    //     TRK2    = TRK2 & t->GetBranchStatus("TrkLevel2");
3679      TRK1    = TRK1 & t->GetBranchStatus("TrkLevel1");    //     TRK1    = TRK1 & t->GetBranchStatus("TrkLevel1");
3680      TRKh    = TRKh & t->GetBranchStatus("TrkHough");    //     TRKh    = TRKh & t->GetBranchStatus("TrkHough");
3681      CAL1    = CAL1 & t->GetBranchStatus("CaloLevel1");    //     CAL1    = CAL1 & t->GetBranchStatus("CaloLevel1");
3682      CAL2    = CAL2 & t->GetBranchStatus("CaloLevel2");    //     CAL2    = CAL2 & t->GetBranchStatus("CaloLevel2");
3683      TOF    = TOF & t->GetBranchStatus("ToFLevel2");    //     TOF    = TOF & t->GetBranchStatus("ToFLevel2");
3684      TRG    = TRG & t->GetBranchStatus("TrigLevel2");    //     TRG    = TRG & t->GetBranchStatus("TrigLevel2");
3685      S4     = S4  & t->GetBranchStatus("S4Level2");    //     S4     = S4  & t->GetBranchStatus("S4Level2");
3686      ND     = ND  & t->GetBranchStatus("NDLevel2");    //     ND     = ND  & t->GetBranchStatus("NDLevel2");
3687      AC     = AC  & t->GetBranchStatus("AcLevel2");    //     AC     = AC  & t->GetBranchStatus("AcLevel2");
3688      ORB    = ORB & t->GetBranchStatus("OrbitalInfo");    //     ORB    = ORB & t->GetBranchStatus("OrbitalInfo");
3689      //    GP     = GP & t->GetBranchStatus("h20");
3690      // Tracker  
3691       if(TRK2) {    // Tracker
3692          t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));    if (TRK2) {
3693          cout << "Tracker      : set branch address TrkLevel2"<<endl;      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
3694      };      cout << "Tracker      : set branch address TrkLevel2" << endl;
3695      if(TRK1) {      NUC = t->GetBranchStatus("TrackNuclei");
3696          t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));      EXT = t->GetBranchStatus("RecoveredTrack") && (NUC ? t->GetBranchStatus("RecoveredTrackNuclei"): true );
3697          cout << "Tracker      : set branch address TrkLevel1"<<endl;    };
3698      };    if (TRK1) {
3699       if(TRKh) {      t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
3700          t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));      cout << "Tracker      : set branch address TrkLevel1" << endl;
3701          cout << "Tracker      : set branch address TrkHough"<<endl;    };
3702       };    if (TRKh) {
3703            t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
3704      // Calorimeter      cout << "Tracker      : set branch address TrkHough" << endl;
3705      if(CAL2) {    };
3706          t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));  
3707          cout << "Calorimeter  : set branch address CaloLevel2"<<endl;    // Calorimeter
3708      };      if (CAL2) {
3709      if(CAL1) {      t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
3710          t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));      cout << "Calorimeter  : set branch address CaloLevel2" << endl;    
3711          cout << "Calorimeter  : set branch address CaloLevel1"<<endl;    };
3712      };    if (CAL1) {
3713            t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
3714      // ToF          cout << "Calorimeter  : set branch address CaloLevel1" << endl;
3715      if(TOF) {    };
3716          t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));  
3717          cout << "ToF          : set branch address ToFLevel2"<<endl;    // ToF
3718      };    if (TOF) {
3719      // Trigger      t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
3720      if(TRG) {      cout << "ToF          : set branch address ToFLevel2" << endl;
3721          t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));    };
3722          cout << "Trigger      : set branch address TrigLevel2"<<endl;    // Trigger
3723      };    if (TRG) {
3724      // S4      t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
3725      if(S4) {      cout << "Trigger      : set branch address TrigLevel2" << endl;
3726          t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));    };
3727          cout << "S4           : set branch address S4Level2"<<endl;    // S4
3728      };    if (S4) {
3729      // Neutron Detector      t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
3730      if(ND) {      cout << "S4           : set branch address S4Level2" << endl;
3731          t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));    };
3732          cout << "NeutronD     : set branch address NDLevel2"<<endl;    // Neutron Detector
3733      };    if (ND) {
3734      // Anticounters      t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
3735      if(AC) {      cout << "NeutronD     : set branch address NDLevel2" << endl;
3736          t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));    };
3737          cout << "Anticounter  : set branch address AcLevel2"<<endl;    // Anticounters
3738      };    if (AC) {
3739      // OrbitalInfo      t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
3740      if(ORB) {      cout << "Anticounter  : set branch address AcLevel2" << endl;
3741          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));    };
3742          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;    // OrbitalInfo
3743      };    if (ORB) {
3744      // SelectionList      t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
3745      if(SELLI==1) {      cout << "OrbitalInfo  : set branch address OrbitalInfo" << endl;
3746          t->SetBranchAddress("RunEntry", &irun);    };
3747          cout << "SelectionList: set branch address RunEntry"<<endl;    // GPamela
3748          t->SetBranchAddress("EventEntry", &irunentry);    //    cout <<"GP "<<GP<<endl;
3749          cout << "SelectionList: set branch address EventEntry"<<endl;    if (GP) {
3750      };      //  GetPointerTo("GPamela");
3751            if (!gp_obj)
3752          gp_obj = new GPamela();
3753        if (SELLI)
3754          t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
3755        else
3756          gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
3757        //  gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
3758        cout << "h20          : set branch address GPamela " << endl;
3759      };
3760      // SelectionList
3761      //     if(SELLI==1){
3762      //    t->SetBranchAddress("RunEntry",&irunt);//NEWNEW
3763      //    cout << "SelectionList: set branch address RunEntry"<<endl;
3764      //    t->SetBranchAddress("EventEntry",&irunentry);
3765      //    cout << "SelectionList: set branch address EventEntry"<<endl;
3766    
3767      //     }
3768      if(NUC){
3769          
3770          cout << "Found nuclei-track branches" << endl;
3771    
3772          if( !trk_nuc_obj )trk_nuc_obj = new TClonesArray("TrkTrack");
3773          if( !calo_nuc_obj)calo_nuc_obj= new TClonesArray("CaloTrkVar");
3774          if( !tof_nuc_obj)tof_nuc_obj= new TClonesArray("ToFTrkVar");
3775          if( !orb_nuc_obj)orb_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3776          if (TRK2)t->                          SetBranchAddress("TrackNuclei",&trk_nuc_obj);
3777          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("TrackNuclei",&calo_nuc_obj);
3778          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("TrackNuclei",&tof_nuc_obj);
3779          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("TrackNuclei",&orb_nuc_obj);
3780          
3781          ///copy the vector content inside a "fake" object (all aother info are missing)    
3782    
3783          if( !trk2_nuc_obj )trk2_nuc_obj =  new TrkLevel2();
3784          if( !calo2_nuc_obj )calo2_nuc_obj =  new CaloLevel2();
3785          if( !tof2_nuc_obj )tof2_nuc_obj =  new ToFLevel2();
3786          if( !orb2_nuc_obj )orb2_nuc_obj =  new OrbitalInfo();
3787    
3788    //       *(trk2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*trk_nuc_obj);
3789    //       *(calo2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*calo_nuc_obj);
3790    //       *(tof2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*tof_nuc_obj);
3791    //       *(orb2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*orb_nuc_obj);
3792          trk2_nuc_obj->SetTrackArray(  trk_nuc_obj  );
3793          calo2_nuc_obj->SetTrackArray( calo_nuc_obj );
3794          tof2_nuc_obj->SetTrackArray(  tof_nuc_obj  );
3795          orb2_nuc_obj->SetTrackArray(  orb_nuc_obj  );
3796    
3797      }    
3798      if(EXT){
3799          
3800          cout << "Found extended tracking algorythm branches" << endl;
3801    
3802          t->SetBranchAddress("extAlgFlag",&extAlgFlag);
3803    
3804          if( !trk_ext_obj )trk_ext_obj = new TClonesArray("ExtTrack");
3805          if( !calo_ext_obj)calo_ext_obj= new TClonesArray("CaloTrkVar");
3806          if( !tof_ext_obj)tof_ext_obj= new TClonesArray("ToFTrkVar");
3807          if( !orb_ext_obj)orb_ext_obj= new TClonesArray("OrbitalInfoTrkVar");
3808          
3809          if (TRK2)t->                          SetBranchAddress("RecoveredTrack",&trk_ext_obj);
3810          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrack",&calo_ext_obj);
3811          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrack",&tof_ext_obj);
3812          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrack",&orb_ext_obj);
3813          
3814          if(NUC){
3815              if( !trk_ext_nuc_obj )trk_ext_nuc_obj = new TClonesArray("ExtTrack");
3816              if( !calo_ext_nuc_obj)calo_ext_nuc_obj= new TClonesArray("CaloTrkVar");
3817              if( !tof_ext_nuc_obj)tof_ext_nuc_obj= new TClonesArray("ToFTrkVar");
3818              if( !orb_ext_nuc_obj)orb_ext_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3819              if (TRK2)t->                          SetBranchAddress("RecoveredTrackNuclei",&trk_ext_nuc_obj);
3820              if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrackNuclei",&calo_ext_nuc_obj);
3821              if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrackNuclei",&tof_ext_nuc_obj);
3822              if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrackNuclei",&orb_ext_nuc_obj);
3823          }
3824      }    
3825      
3826  }  }
3827    
3828    /**
3829     * Set the tracking algorythm
3830     * @param alg String to choose the track.
3831     * "" --> take the output of the standard tracking algorythm
3832     * "STD" --> take the output of the standard tracking algorythm
3833     * "NUC" --> take the output of the standard tracking algorythm for nuclei cluster selection
3834     * "EXT" --> in case the standard tracking algorythm has not found any track, take the output of the extended one
3835     * "EXTF" --> force the extended tracking algorythm
3836     * "NUCEXT" --> as "EXT", but for nuclei
3837     * "NUCEXTF" --> as "EXTF", but for nuclei
3838     */
3839    // void PamLevel2::SetTrackingAlgorythm(const char *alg){
3840    
3841    
3842    //     TString s(alg);
3843    //     if(s.Contains("NUC", TString::kIgnoreCase) && !NUC)
3844    //      cout << "Warning! NUC algorythm requested, but branches are missing"<<endl;
3845    //     if(s.Contains("EXT", TString::kIgnoreCase) && !EXT)
3846    //      cout << "Warning! EXT algorythm requested, but branches are missing"<<endl;;
3847                
3848    //     trkAlg = alg;
3849    
3850    // };
3851    // const char* PamLevel2::GetTrackingAlgorythm(){
3852    
3853    
3854    //     cout<<endl<<" Implemented tracking algorythm: ";
3855    //     cout<<endl<<"  \"\" or \"STD\"  --> take the output of the standard tracking algorythm";
3856    //     cout<<endl<<"  \"NUC\"      --> take the output of the standard tracking algorythm for nuclei cluster selection";
3857    //     cout<<endl<<"  \"EXT\"      --> in case the standard tracking algorythm has not found any track,";
3858    //     cout<<endl<<"                 take the output of the extended one";
3859    //     cout<<endl<<"  \"EXTF\"     --> force the extended tracking algorythm";
3860    //     cout<<endl<<"  \"NUCEXT\"   --> as \"EXT\", but for nuclei ";
3861    //     cout<<endl<<"  \"NUCEXTF\"  --> as \"EXTF\", but for nuclei";
3862    
3863    //     cout<<endl;
3864    //     cout<<" <<Currently set algorythm>> "<<trkAlg<<endl;
3865    //     cout<<endl;
3866    
3867    //     return trkAlg;
3868    // };
3869    
3870    
3871    
3872  //--------------------------------------  //--------------------------------------
3873  //  //
# Line 1893  void PamLevel2::SetBranchAddress(TChain Line 3875  void PamLevel2::SetBranchAddress(TChain
3875  //--------------------------------------  //--------------------------------------
3876  /**  /**
3877   * Get the Run tree chain from a list of files.   * Get the Run tree chain from a list of files.
3878   * @param fl Pointer to a TList of TSystemFiles   * @param fl Pointer to a TList of TSystemFiles
3879   * @return Pointer to a TChain   * @return Pointer to a TChain
3880   */   */
3881  TChain *PamLevel2::GetRunTree(TList *fl){  TChain *PamLevel2::GetRunTree(TList *fl) {
3882    //    //
3883    //    //
3884    //    //
3885    if ( run_tree ){    if (run_tree) {
3886      printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");      printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");
3887      return run_tree;      return run_tree;
3888    };        };
3889    //    //
3890    
3891    
3892    TChain *R = new TChain("Run");    TChain *R = new TChain("Run");
       
     // loop over files and create chains          
     TIter next(fl);  
     TSystemFile *questo = 0;  
     while ( (questo = (TSystemFile*) next()) ) {  
         TString name =  questo->GetName();  
 //              cout << "File: "<< name << endl;  
         if( CheckLevel2File(name) ){  
             R->Add(name);  
         };  
     }  
3893    
3894      if(R->GetNtrees()){    // loop over files and create chains
3895          TIter next(fl);
3896          R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));    TSystemFile *questo = 0;
3897          cout << "Run         : set branch address RunInfo"<<endl;    while ((questo = (TSystemFile*) next())) {
3898          R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano      TString name = questo->GetName();
3899          cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano      //          cout << "File: "<< name << endl;
3900      }else{      if (CheckLevel2File(name)) {
3901          delete R;        R->Add(name);
3902          R=0;      };
3903      }
3904    
3905      cout << "done run chain  "<<  R->GetNtrees() <<" \n";
3906    
3907    
3908      if (RUN && R->GetNtrees()) {
3909    
3910        R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
3911        cout << "Run         : set branch address RunInfo" << endl;
3912        R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
3913        cout << "Software    : set branch address SoftInfo" << endl; // Emiliano
3914    
3915        irunoffset = new int[R->GetNtrees()];
3916        if (DBG) {
3917          cout << "----------------------------------------------------" << endl;
3918          cout << "irun\t | ";
3919          cout << "tree\t |";
3920          //        cout << "offset\t |";
3921          cout << "RUN\t";
3922          cout << "FRAG\t";
3923          cout << "NEVENTS\t";
3924          cout << "absolute time\t\t\t";
3925          cout << "on-board time";
3926          cout << endl;
3927        }
3928        for (Int_t ii = 0; ii < R->GetEntries(); ii++) {
3929          R->GetEntry(ii);
3930          if (DBG) {
3931            cout << ii << "\t | ";
3932            cout << R->GetTreeNumber() << "\t |";
3933            //          cout << R->GetChainOffset()<< "\t |";
3934            cout << GetRunInfo()->ID << "\t";
3935            cout << GetRunInfo()->ID_RUN_FRAG << "\t";
3936            cout << GetRunInfo()->NEVENTS << "\t";
3937            cout << GetRunInfo()->RUNHEADER_TIME << " <---> " << GetRunInfo()->RUNTRAILER_TIME << "\t";
3938            cout << GetRunInfo()->RUNHEADER_OBT << " <---> " << GetRunInfo()->RUNTRAILER_OBT << "\t";
3939            cout << endl;
3940          }
3941          irunoffset[R->GetTreeNumber()] = R->GetChainOffset();
3942      }      }
3943        cout << "N.run = " << R->GetEntries() << endl;
3944        cout << "----------------------------------------------------" << endl;
3945    
3946      run_tree = R;    }
3947    //   else {
3948    //     delete R;
3949    //     R = 0;
3950    //   }
3951    
3952      run_tree = R;
3953    
3954      return R;
3955    
     return R;  
       
3956  }  }
3957  //--------------------------------------  //--------------------------------------
3958  //  //
# Line 1940  TChain *PamLevel2::GetRunTree(TList *fl) Line 3960  TChain *PamLevel2::GetRunTree(TList *fl)
3960  //--------------------------------------  //--------------------------------------
3961  /**  /**
3962   * Get the Run tree  from a file.   * Get the Run tree  from a file.
3963   * @param f Pointer to a TFile   * @param f Pointer to a TFile
3964   * @return Pointer to a TTree   * @return Pointer to a TTree
3965   */   */
3966  TTree *PamLevel2::GetRunTree(TFile *f){  TTree *PamLevel2::GetRunTree(TFile *f) {
3967    if ( run_tree ){    if (run_tree) {
3968      printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");      printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");
3969      return run_tree;      return run_tree;
3970    };    };
           
3971    
3972      cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;    cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte " << endl;
3973    
3974      TTree *T = (TTree*)f->Get("Run");    TTree *T = (TTree*) f->Get("Run");
       
     if(T){  
         T->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));  
         cout << "Run         : set branch address RunInfo"<<endl;  
         T->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano  
         cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano  
     }  
3975    
3976      run_tree = (TChain*)T;    if (T) {
3977        T->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
3978        cout << "Run         : set branch address RunInfo" << endl;
3979        T->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
3980        cout << "Software    : set branch address SoftInfo" << endl; // Emiliano
3981    
3982      return T;    }
       
 }  
 /**  
  * Update the runinfo informations (to be used to have Run infos event by event basis)  
  * @param run Pointer to the chain/tree which contains run infos  
  * @return true if a new run has been read, false if it is still the same run  
  */  
 Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev){  
   //  
   // check if we have already called once GetEntry, if not call it  
   //  
     cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) --- NON FUNZIONA BENISSIMO.... "<<endl;  
     if(!run){  
           cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing RunInfo tree "<<endl;  
           return(false);          
     }  
     if ( run->GetEntries() <= 0 ) return(false);  
   //  
     
   //  Int_t oldrun = irun;  
   Long64_t oldrun = irun;  
3983    
3984    // --------------------------------------    run_tree = (TChain*) T;
   // if it is a full file (not preselected)  
   // --------------------------------------  
   if(SELLI==0){  
3985    
3986        //    return T;
       // the absolute time is necessary to relate the event with the run  
       //  
       if( !GetOrbitalInfo() && !ISGP){  
           cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;  
           return(false);  
       }  
3987    
3988        ULong64_t abstime = 0;  }
       if( GetOrbitalInfo() )abstime=GetOrbitalInfo()->absTime;  
3989    
3990    Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) {
3991    
3992        //    if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - inside\n");
       // the first time the routine is called, set run search from the beginning  
       //  
       if ( irun < 0LL ){  
           irun = 0LL;  
           run->GetEntry(irun);  
           runfirstentry = 0LL;  
           runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);  
           if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;  
             
           if( ISGP && run->GetEntries()!=1 ){  
               cout << "** WARNING ** simulated files are assumed to have 1 single run, not "<< run->GetEntries() << endl;  
               cout << "** WARNING ** run will not be updated"<<endl;  
           }  
3993    
3994        };          if (!run_tree) {
3995        //      cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded" << endl;
3996        if(ISGP)abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO      return false;
3997        //    }
3998        if ( irun == run->GetEntries()-1LL &&    if (run_tree->GetEntries() <= 0) {
3999             !(abstime >= GetRunInfo()->RUNHEADER_TIME &&      cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty" << endl;
4000               abstime <= GetRunInfo()->RUNTRAILER_TIME)      return (false);
             ){  
          irun = -1LL;  
          runfirstentry = 0LL;  
          runlastentry = -1LL;  
        };  
       // modificato il controllo sull'aggiornamento del run, per evitare problemi  
       // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)  
       //  
       bool fromfirst = true;  
       //  
       while ( !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME) && irun < run->GetEntries()-1LL ){  
 //      while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){  
           irun++;  
           run->GetEntry(irun);  
           runfirstentry = runlastentry;  
           if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runfirstentry += 1LL;  
           runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);  
 //        cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;  
 //        cout << "runfirstentry "<<runfirstentry<<endl;  
           //      printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));  
           //      printf(" abstime %u trailertime %u \n",abstime,GetRunInfo()->RUNTRAILER_TIME);  
           //      printf(" IDRUN %u \n",GetRunInfo()->ID);  
           //  
 //        prevshift = 0;  
           //  
           if ( irun == (Long64_t)(run->GetEntries()-1LL) && fromfirst && !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME)){  
               printf(" resetting irun  (it should NOT happen!!!)\n");  
               fromfirst = false;  
               irun = 0;  
               run->GetEntry(irun);  
               runfirstentry = 0ULL;  
               runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);  
               if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;  
           };  
           //  
       };  
       //  
       if (  
            !(abstime >= GetRunInfo()->RUNHEADER_TIME &&  
              abstime <= GetRunInfo()->RUNTRAILER_TIME)  
           ) {  
         printf(" Something very wrong here: cannot find RUN containing absolute time %llu \n",abstime);  
           return false;  
       }  
       //  
       if ( irun == oldrun || irun >= run->GetEntries() ) return(false);  
       //  
       //  printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);  
       //  
       prevshift = 0;  
       cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;  
 //      cout << "runfirstentry "<<runfirstentry<<endl;  
       return(true);      
   };  
   // ----------------------------------------------------  
   // if it is a preselected file (there is SelectionList)  
   // NBNB - the event tree MUST be read first  
   // ----------------------------------------------------  
   if(SELLI==1){        
       sel_tree->GetEntry(iev);  
 //      cout << irun << " "<< irunentry << endl;  
       if(irun != oldrun){  
           run->GetEntry(irun);  
           cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;  
           prevshift = 0;  
           return true;  
       }  
       return false;  
4001    }    }
4002    
   return false;  
   //  
 };  
4003    
4004  Bool_t PamLevel2::UpdateRunInfo(Long64_t iev){    Int_t oldrun = irun; // store current run index
4005    
4006      if(!run_tree){    // -----------------------------------------------------------------------
4007          cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded"<<endl;    // the first time the routine is called, set run search from the beginning
4008          return false;    // -----------------------------------------------------------------------
4009      }  
4010      if ( run_tree->GetEntries() <= 0 ) {    if (irun < 0) {
4011          cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty"<<endl;      irun = 0LL;
4012          return(false);      irunt = 0LL;
4013        totrunentry = 0LL;
4014        totrunentrymin = 0LL;
4015        totrunentrymax = 0LL;
4016        irunentry = 0LL;
4017        il0entry = 0LL;
4018        prevshift = 0;
4019        yprevshift = 0;
4020        prevabstime = 0;
4021        prevpktnum = 0;
4022        abstime = 0ULL;
4023        pktnum = 0;
4024        isFragment = false;
4025        run_tree->GetEntry(irun);
4026        if (!GetOrbitalInfo())
4027          cout << "** WARNING ** missing OrbitalInfo ---> run info might be not correctly updated " << endl;
4028        if ( fUseDBinRunInfo ){
4029          if (gltsync)
4030            delete gltsync; //Emiliano
4031          if (!dbc || (dbc && !dbc->IsConnected()))
4032            SetDBConnection(); //Emiliano
4033          gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); //Emiliano // the "false" means not to use level0 file (not necessary here)
4034          if (dbc){
4035            dbc->Close();// Emiliano
4036            delete dbc;
4037            dbc=0;
4038          }
4039      }      }
4040        }
4041      Int_t oldrun = irun; // store current run index    // ---------------------------------------------------------------
4042      // retrieve OBT and absolute time of the event
4043      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    // ---------------------------------------------------------------
4044      // if it is a full file (not preselected)    Long64_t obt = 0LL; // Emiliano, Long64_t GL_TIMESYNC::DBobt(UInt_t obt) since depending on the situation OBT is lowered or boosted
4045      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-    prevabstime = abstime;
4046      if(SELLI==0){    prevpktnum = pktnum;
4047      if (GetOrbitalInfo()) {
4048          // ---------------------------------------------------------------      abstime = GetOrbitalInfo()->absTime;
4049          // increment dead and live-time counters      if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano
4050          // ---------------------------------------------------------------      pktnum = GetOrbitalInfo()->pkt_num; // Emiliano
4051          if( GetTrigLevel2() ){    }
4052              totdltime[0]+=GetTrigLevel2()->dltime[0];    else {
4053              totdltime[1]+=GetTrigLevel2()->dltime[1];      abstime = GetRunInfo()->RUNHEADER_TIME;
4054          }      if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano
4055          totdltime[2]++;      pktnum = GetRunInfo()->RUNHEADER_PKT; // Emiliano
4056      }
 //      cout << setw(10)<<totdltime[0]<<setw(10)<<totdltime[1]<<setw(10)<<totdltime[2]<<endl;  
   
         // ---------------------------------------------------------------  
         // retrieve OBT and absolute time of the event  
         // ---------------------------------------------------------------  
         ULong64_t abstime = 0;  
         ULong64_t obt     = 0;  
         if( GetOrbitalInfo() ){  
             abstime = GetOrbitalInfo()->absTime;  
             obt     = GetOrbitalInfo()->OBT;  
         }  
         // ---------------------------------------------------------------  
         // the absolute time is necessary to relate the event with the run  
         // ---------------------------------------------------------------  
         if( !GetOrbitalInfo() && !ISGP ){  
             cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;  
             return(false);  
         }  
         
         // -----------------------------------------------------------------------  
         // the first time the routine is called, set run search from the beginning  
         // -----------------------------------------------------------------------  
         if ( irun < 0 ){  
             irun = 0LL;  
             irunentry = 0;  
             prevshift = 0;  
             run_tree->GetEntry(irun);  
   
             if( ISGP && run_tree->GetEntries()!=1 ){  
                 cout << "** WARNING ** simulated files are assumed to have 1 single run, not "<< run_tree->GetEntries() << endl;  
                 cout << "** WARNING ** run will not be updated"<<endl;  
             }  
         };        
4057    
4058          // -----------------------------------------------------------------------    if (DBG){
4059          // if it is simulation, assigh abstime by hand (temporaneo!!!)      printf("0abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4060          // -----------------------------------------------------------------------      printf("0        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4061          if(ISGP){      printf("0        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4062              abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO      if ( fUseDBinRunInfo ) printf("0        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4063              obt     = GetRunInfo()->RUNHEADER_OBT; // BARBATRUCCO      printf("0        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4064          }      printf("0        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
4065          //    }
         bool fromfirst = true; // first loop over runs  
4066    
4067  //      Bool_t hasfrag = false;    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4068  //      if( GetRunInfo()->ID_RUN_FRAG!=0 && GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID)hasfrag=true;    // if it is a full file (not preselected)
4069  //      ULong64_t fragid = GetRunInfo()->ID_RUN_FRAG;    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4070      if (SELLI == 0 || SELLI == 2) { // Emiliano
         // ------------------------------------------------------  
         // loop over runs to find the one that contains the event  
         // ------------------------------------------------------  
         while (  
             (  
                 (  
                     !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)  
                       abstime <= GetRunInfo()->RUNTRAILER_TIME) &&  
                     !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)  
                       obt <= GetRunInfo()->RUNTRAILER_OBT)  
                     )  
                 || GetRunInfo()->NEVENTS==0  
                 || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift)  
                 )  
                 && irun < run_tree->GetEntries() ){  
4071    
4072        // ---------------------------------------------------------------
4073        // increment dead and live-time counters
4074        // (only when reading a file not preselected)
4075        // ---------------------------------------------------------------
4076        if (SELLI == 0) {
4077          if (GetTrigLevel2()) {
4078            totdltime[0] += GetTrigLevel2()->dltime[0];
4079            totdltime[1] += GetTrigLevel2()->dltime[1];
4080          }
4081          totdltime[2]++;
4082        }
4083    
4084        //
4085        bool a = true;
4086        bool b = true;
4087        if ( fUseDBinRunInfo ){
4088          a = false;    
4089          b = false;
4090          if ( obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) a = true;
4091          if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true;
4092        }
4093        if ( iev < totrunentrymin || iev > totrunentrymax // entry is outside run limits
4094             || iev == 0 // or it is the first entry
4095             || (!isSync && (
4096                             (abstime <= GetRunInfo()->RUNHEADER_TIME && a ) // or it is outside obt limits (and abstime limits for security reasons)
4097                             || (abstime >= GetRunInfo()->RUNTRAILER_TIME && b ) ))// or it is outside obt limits (and abstime limits for security reasons)
4098             ){ // check on abstime and obt needed to handle nested+DV_skipped packets
4099          
4100          // check for a new run (ma prima il primo!)
4101          if (DBG){
4102            printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4103            printf("1        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4104            printf("1        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4105            if ( fUseDBinRunInfo ) printf("1        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4106            printf("1        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4107            printf("1        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
4108          }
4109          //        printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4110          //        printf("1        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4111          //        printf("1        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4112          //        printf("1        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4113          //        printf("1        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4114          //        printf("1        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
4115    
4116          totrunentry = 0LL;
4117          runfirstentry = 0LL;
4118          for (Int_t r=0; r< run_tree->GetEntries();r++){
4119            // -------------------------------------------------------------------
4120            // save the index of the first entry of the run, relative to pam_tree,
4121            // and read a new run
4122            // -------------------------------------------------------------------
4123            run_tree->GetEntry(r);//update runinfo
4124            if ( r > 0 ){
4125              totrunentrymin = totrunentrymax+1;
4126            } else {
4127              totrunentrymin = 0LL;
4128            }
4129            totrunentry += GetRunInfo()->NEVENTS;
4130            totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
4131            irun = r;        
4132            if ( fUseDBinRunInfo ){
4133              a = false;    
4134              b = false;
4135              if ( obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) a = true;
4136              if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true;
4137            }
4138            if ( (iev >= totrunentrymin && iev <= totrunentrymax) || // entry is inside run limits
4139                 ( !isSync &&
4140                   ( abstime >= GetRunInfo()->RUNHEADER_TIME  && a // or it is inside obt limits (and abstime limits for security reasons)
4141                     && abstime <= GetRunInfo()->RUNTRAILER_TIME && b))  // or it is inside obt limits (and abstime limits for security reasons)
4142                 ){ // check on abstime and obt needed to handle nested+DV_skipped packets
4143              if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
4144                if ( !isSync ){
4145                  if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
4146                  if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4147                  //              printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
4148                  //              printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4149                  prevshift += (totrunentrymin-iev); // add the new shift to total shift
4150                  totrunentrymin -= (totrunentrymin-iev); // shift run position min
4151                  totrunentrymax -= (totrunentrymin-iev); // shift run position max
4152                  if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4153                  //              printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4154                } else {
4155                  printf(" PamLevel2::UpdateRunInfo(Long64_t) ERROR! sync file but unconsistent totrunetrymin %lld and iev %lld!!! \n",totrunentrymin,iev);
4156                  cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4157                  cout << "\nFor bug reporting instructions, please see for example:\n";
4158                  cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4159                  cout << "  " << endl;
4160                }
4161              }
4162              runfirstentry = totrunentrymin; // first entry of the run in the level2
4163              
4164    
4165              //
4166              if ( fUseDBinRunInfo ){
4167                if (gltsync)
4168                  delete gltsync; // Emiliano
4169                if (!dbc || (dbc && !dbc->IsConnected()))
4170                  SetDBConnection(); //Emiliano
4171                gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
4172                TrkParams::Set(GetRunInfo(), dbc);
4173                if (dbc){
4174                  dbc->Close(); // Emiliano
4175                  delete dbc;
4176                  dbc=0;
4177                }          
4178                if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
4179                  cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
4180                       << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
4181                  cout
4182                    << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
4183                    << endl;
4184                }
4185              }
4186              //
4187              if (DBG) printf(" found \n");
4188              //          printf(" found \n");//TOGLITOGLI
4189              //
4190              break;
4191            }
4192          } // loop over run
4193          
4194          // --------------------------------------
4195          // if there was no need to update the run
4196          // ---> exit with FALSE
4197          // --------------------------------------
4198          if (irun == oldrun){
4199            if (DBG) printf(" no new run \n");
4200            //        printf(" no new run \n");//TOGLITOGLI
4201            return (false);
4202          }      
4203          // --------------------------------------
4204          // ... otherwise
4205          // ---> exit with TRUE
4206          // --------------------------------------
4207    
4208          if (SELLI != 2) {
4209            /// decrement counters since this event belongs to a new run
4210            if (GetTrigLevel2()) {
4211              totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time
4212              totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time
4213            }
4214            totdltime[2]--; //event counter
4215            if (DBG) {
4216              cout << endl;
4217              cout << "n.events     : " << totdltime[2] << endl;
4218              cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;
4219              cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;
4220            }
4221            // add an entry
4222            if (run_tree_clone && totdltime[2] > 0)
4223              if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
4224                run_tree_clone->GetBranch("DeadLiveTime")->Fill();
4225            // reset counters
4226            if ( totdltime[2] > 0 ){
4227              if (GetTrigLevel2()) {
4228                totdltime[0] = GetTrigLevel2()->dltime[0];//live-time
4229                totdltime[1] = 0; //dead-time
4230              }
4231              totdltime[2] = 1; //event counter
4232            }
4233          }
4234    
4235          if (DBG){
4236            cout << endl << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun
4237                 << endl;        
4238            printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4239            printf("2        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4240            printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4241            if ( fUseDBinRunInfo ) printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4242            printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4243            printf("2        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
4244          }
4245          //        printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4246          //        printf("2        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4247          //        printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4248          //        printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4249          //        printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4250          //        printf("2        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
4251    
4252          return (true);
4253        } // need for run upgrade
4254        if (DBG) printf("return false\n");
4255        return (false);
4256      }// SELLI = 0 SELLI = 2
4257      
4258      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4259      // if it is a preselected file (there is SelectionList)
4260      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4261      // irun  = run entry relative to the chain
4262      // irunt = run entry relative to the tree
4263      if (SELLI == 1) {
4264        sel_tree->GetEntry(iev);// read irunt from SelectionList
4265        irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW
4266        if (irun != oldrun) {
4267          if (irun < run_tree->GetEntries())
4268            run_tree->GetEntry(irun);
4269          // check if the time is ok (with merged files it is not...)
4270          // if not loop over run and look for the proper entry
4271          bool SECONDO_GIRO = false;
4272          //            Long64_t irun_start   = irun;
4273          int offset_start = irunoffset[sel_tree->GetTreeNumber()];
4274          while (((!(abstime >= GetRunInfo()->RUNHEADER_TIME && // check on absolute time (s)
4275              abstime <= GetRunInfo()->RUNTRAILER_TIME)
4276          //                        ||
4277          //                        !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)
4278          //                          obt <= GetRunInfo()->RUNTRAILER_OBT)
4279          ) || GetRunInfo()->NEVENTS == 0)
4280          //                && irun < run_tree->GetEntries()
4281          ) {
4282    
4283            if (DBG) {
4284              cout << " (test) ";
4285              cout << " tree " << sel_tree->GetTreeNumber();
4286              cout << " irunt " << irunt;
4287              cout << " offset " << irunoffset[sel_tree->GetTreeNumber()];
4288              cout << " abs " << abstime;
4289              cout << " >> " << GetRunInfo()->RUNHEADER_TIME << " " << GetRunInfo()->RUNTRAILER_TIME;
4290              cout << " obt " << obt;
4291              cout << " >> " << GetRunInfo()->RUNHEADER_OBT << " " << GetRunInfo()->RUNTRAILER_OBT;
4292              cout << " *** JUMP RUN *** irun " << irun;
4293              cout << endl;
4294            }
4295            //              irun++;
4296            irunoffset[sel_tree->GetTreeNumber()]++;
4297            irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW
4298            if (irun == run_tree->GetEntries() && SECONDO_GIRO) {
4299              //            if(irun == irun_start ){
4300              cout << " ...grrrvzzkhhhajsdkj!!!! " << endl;
4301              irunoffset[sel_tree->GetTreeNumber()] = offset_start;
4302              return false;
4303            }
4304            if (irun >= run_tree->GetEntries() || irun < 0) {
4305              cout << "irun = " << irun << " >>  search from the beginning... <<" << endl;
4306              SECONDO_GIRO = true;
4307              irun = 0;
4308              irunoffset[sel_tree->GetTreeNumber()] = -irunt;
4309            }
4310            run_tree->GetEntry(irun);
4311          }
4312    
4313              // -----------------------------------------        if (DBG) {
4314              // store dead and live-time of previous run          cout << " (test) ";
4315              // -----------------------------------------          cout << " tree " << sel_tree->GetTreeNumber();
4316              if(fromfirst){          cout << " irunt " << irunt;
4317                  if(oldrun==irun){          cout << " offset " << irunoffset[sel_tree->GetTreeNumber()];
4318                      /// decrement counters          cout << " abs " << abstime;
4319                      if( GetTrigLevel2()){          cout << " >> " << GetRunInfo()->RUNHEADER_TIME << " " << GetRunInfo()->RUNTRAILER_TIME;
4320                          totdltime[0]-=GetTrigLevel2()->dltime[0];//live-time          cout << " obt " << obt;
4321                          totdltime[1]-=GetTrigLevel2()->dltime[1];//dead-time          cout << " >> " << GetRunInfo()->RUNHEADER_OBT << " " << GetRunInfo()->RUNTRAILER_OBT;
4322                      }        }
4323                      totdltime[2]--;                              //event counter        if (DBG)
4324                      cout << endl;          cout << endl;
4325                      cout << "n.events     : "<<totdltime[2]<<endl;        if (DBG)
4326                      cout << "RUN LIVE-TIME: "<<totdltime[0]*0.16<<" ms"<<endl;          cout << endl << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " (run-entry "
4327                      cout << "RUN DEAD-TIME: "<<totdltime[1]*0.01<<" ms"<<endl;                    << irun << ")" << endl;
4328                  }else{        // ----------------------------------------------------
4329                      totdltime[0]=0;//live-time        // update the tracker parameters
4330                      totdltime[1]=0;//dead-time        // (non ho trovato nessun altro modo sicuro di farlo...)
4331                      totdltime[2]=0;                             //event counter        // ----------------------------------------------------
4332                      cout << " *** JUMP RUN *** irun "<<irun<<endl;        if ( fUseDBinRunInfo ){
4333                  }          if (!dbc || (dbc && !dbc->IsConnected()))
4334                  /// add an entry            SetDBConnection();
4335                  if(run_tree_clone)          TrkParams::Set(GetRunInfo(), dbc);
4336                      if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())          if (dbc){
4337                          run_tree_clone->GetBranch("DeadLiveTime")->Fill();            dbc->Close();
4338                  /// reset counters            delete dbc;
4339                  if( GetTrigLevel2() ){            dbc=0;
4340                      totdltime[0]=GetTrigLevel2()->dltime[0];//live-time          }
4341                      totdltime[1]=0;                         //dead-time        }
4342                  }        //            cout << endl;
4343                  totdltime[2]=1;                             //event counter        prevshift = 0;
4344              }        yprevshift = 0;
4345          return true;
4346        }
4347        return false;
4348      }
4349    
4350      return false;
4351      //
4352    }
4353    
4354              irun++;  /**
4355              // ------------------------------------   * Update the runinfo informations (to be used to have Run infos event by event basis)
4356              // if the end of run tree is reached...   * @param run Pointer to the chain/tree which contains run infos
4357              // ------------------------------------   * @return true if a new run has been read, false if it is still the same run
4358              if( irun == run_tree->GetEntries() ){   */
4359                  if(!fromfirst){  Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) {
4360                      // -----------------------------------------------------    return (UpdateRunInfo(iev));
4361                      // if it happened already once and the run was not found  }
                     // ---> exit with error  
                     // -----------------------------------------------------  
                     cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- event entry #"<<iev<<" does not belong to any run (should not happen)" <<endl;  
                     return false;  
                 }  
                 // -----------------------------------------  
                 // ...otherwise repeat search from beginning  
                 // -----------------------------------------  
                 cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- reached end of run tree. searchin again from beginning " <<endl;  
                 fromfirst = false;  
                 irun = 0LL;  
                 runfirstentry = 0LL;  
             }  
             // -------------------------------------------------------------------  
             // save the index of the first entry of the run, relative to pam_tree,  
             // and read a new run  
             // -------------------------------------------------------------------  
             if(irun>0)runfirstentry += (GetRunInfo()->NEVENTS)-prevshift;  
             irunentry = 0;  
             prevshift = 0;            
             run_tree->GetEntry(irun);        
             if(GetRunInfo()->RUNHEADER_OBT>GetRunInfo()->RUNTRAILER_OBT ){  
                 cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun "<<irun<<"  has RUNHEADER_OBT>=RUNTRAILER_OBT " <<endl;  
                 cout << "                                                            (NB!! in this case some events are assigned to a wrong run)"<<endl;  
             }  
 //          if(hasfrag &&  fragid != GetRunInfo()->ID){  
 //              cout << "... where is the next fragment ??"<<endl;  
 //          }  
         };  
   
   
         // --------------------------------------  
         // if there was no need to update the run  
         // ---> exit with FALSE  
         // --------------------------------------  
         if ( irun == oldrun ) return(false);  
   
         // --------------------------------------  
         // ... otherwise  
         // --------------------------------------  
   
   
   
 //      // -----------------------------------------  
 //      // store dead and live-time of previous run  
 //      // -----------------------------------------  
 //      // --------------------------------------------------------------  
 //      // if the run is empty, fill the dead-live time branch with zeros  
 //      // --------------------------------------------------------------  
 //      /// if some runs have been jumped, fill with zeros  
 //      if(irun-oldrun>1){  
 //          ULong64_t  temp[3];  
 //          temp[0]=totdltime[0];  
 //          temp[1]=totdltime[1];  
 //          temp[2]=totdltime[2];  
 //          for(int i=oldrun+1; i<irun; irun++){  
 //              totdltime[0]=0;  
 //              totdltime[1]=0;  
 //              totdltime[2]=0;  
 //              cout << " ** JUMPED RUN **"<<endl;  
 //              if(run_tree_clone)  
 //                  if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())  
 //                      run_tree_clone->GetBranch("DeadLiveTime")->Fill();  
 //          }  
 //          totdltime[0]=temp[0];  
 //          totdltime[1]=temp[1];  
 //          totdltime[2]=temp[2];  
 //      }  
 //      /// decrement counters  
 //      if( GetTrigLevel2() ){  
 //          totdltime[0]-=GetTrigLevel2()->dltime[0];//live-time  
 //          totdltime[1]-=GetTrigLevel2()->dltime[1];//dead-time  
 //      }  
 //      totdltime[2]--;                              //event counter  
 //      /// add an entry  
 //      if(irun>0 ){  
 //          cout << endl;  
 //          cout << "n.events     : "<<totdltime[2]<<endl;  
 //          cout << "RUN LIVE-TIME: "<<totdltime[0]*0.16<<" ms"<<endl;  
 //          cout << "RUN DEAD-TIME: "<<totdltime[1]*0.01<<" ms"<<endl;        
 //          if(run_tree_clone)  
 //              if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())  
 //                  run_tree_clone->GetBranch("DeadLiveTime")->Fill();  
 //      }  
 //      /// reset counters  
 //      if( GetTrigLevel2() ){  
 //          totdltime[0]=GetTrigLevel2()->dltime[0];//live-time  
 //          totdltime[1]=0;                         //dead-time  
 //      }  
 //      totdltime[2]=1;                             //event counter  
           
   
         // --------------------------------------  
         // ---> exit with TRUE  
         // --------------------------------------  
         cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;  
         // ----------------------------------------------------  
         // then check if the run has a fragment  
         // in this case we have to switch to the next fragment  
         // when the end of the first fragment is reached  
         // ----------------------------------------------------  
         if(  
             GetRunInfo()->ID_RUN_FRAG != 0 &&  
 //          GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID &&  
             true ){  
             cout << "* fragment *"<<endl;                
         }  
   
         return(true);      
     };  
     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-  
     // if it is a preselected file (there is SelectionList)  
     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-  
     if(SELLI==1){        
         sel_tree->GetEntry(iev);  
         if(irun != oldrun){  
             run_tree->GetEntry(irun);  
             cout << endl << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;  
             prevshift = 0;  
             return true;  
         }  
         return false;  
     }  
4362    
     return false;  
     //  
 };  
4363  /**  /**
4364   * Update the runinfo informations (to be used to have Run infos event by event basis)   * Update the runinfo informations (to be used to have Run infos event by event basis)
4365   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
4366   * @return true if a new run has been read, false if it is still the same run   * @return true if a new run has been read, false if it is still the same run
4367   */   */
4368  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev){  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {
4369    return(UpdateRunInfo((TChain*)run,iev));    return (UpdateRunInfo((TChain*) run, iev));
4370  };  }
4371    
4372    
4373  //--------------------------------------  //--------------------------------------
4374  //  //
# Line 2377  Bool_t PamLevel2::UpdateRunInfo(TTree *r Line 4376  Bool_t PamLevel2::UpdateRunInfo(TTree *r
4376  //--------------------------------------  //--------------------------------------
4377  /**  /**
4378   * Set which trees shoul be analysed   * Set which trees shoul be analysed
4379   * @param detlist TString containing the sequence of trees required   * @param detlist TString containing the sequence of trees required
4380  */   */
4381  void PamLevel2::SetWhichTrees(TString detlist){  void PamLevel2::SetWhichTrees(TString detlist) {
           
     if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){  
         CAL0 = false;  
         CAL1 = true;  
         CAL2 = true;  
         TRK2 = true;  
         TRK1 = false;  
         TRKh = false;  
         TRK0 = false;  
         TRG = true;  
         TOF = true;  
         TOF0 = false;  
         S4  = true;  
         ND  = true;  
         AC  = true;  
         ORB = true;  
     }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){  
         CAL0 = false;  
         CAL1 = false;  
         CAL2 = false;  
         TRK2 = false;  
         TRK1 = false;  
         TRKh = false;  
         TRK0 = false;  
         TRG = false;  
         TOF = false;  
         TOF0 = false;  
         S4  = false;  
         ND  = false;  
         AC  = false;  
         ORB = false;  
     };  
       
 //  -------------------------------------------------------------------------  
     if( detlist.Contains("CAL1", TString::kIgnoreCase) ){  
         if ( detlist.Contains("-CAL1", TString::kIgnoreCase) )CAL1=false;  
         if ( detlist.Contains("+CAL1", TString::kIgnoreCase) )CAL1=true;  
     };  
4382    
4383      if( detlist.Contains("CAL0", TString::kIgnoreCase) ){    //    if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){
4384          if ( detlist.Contains("-CAL0", TString::kIgnoreCase) )CAL0=false;    if (detlist.Contains("+ALL", TString::kIgnoreCase)) {
         if ( detlist.Contains("+CAL0", TString::kIgnoreCase) )CAL0=true;  
     };  
           
     if( detlist.Contains("CAL2", TString::kIgnoreCase)){  
         if ( detlist.Contains("-CAL2", TString::kIgnoreCase) )CAL2=false;  
         if ( detlist.Contains("+CAL2", TString::kIgnoreCase) )CAL2=true;  
     };  
           
     if( detlist.Contains("+CAL", TString::kIgnoreCase) && !CAL1 && !CAL2 )CAL2=true;  
     if( detlist.Contains("-CAL", TString::kIgnoreCase) && CAL1 && CAL2 ){  
         CAL2=false;  
         CAL1=false;  
     }  
 //  -------------------------------------------------------------------------  
     if( detlist.Contains("TRK0", TString::kIgnoreCase) ){  
         if ( detlist.Contains("-TRK0", TString::kIgnoreCase) )TRK0=false;  
         if ( detlist.Contains("+TRK0", TString::kIgnoreCase) )TRK0=true;  
     };  
4385    
4386      if( detlist.Contains("TRK1", TString::kIgnoreCase) ){      cout << " ======================================================== " << endl;
4387          if ( detlist.Contains("-TRK1", TString::kIgnoreCase) )TRK1=false;      cout << "                       (( WARNING ))                      " << endl;
4388          if ( detlist.Contains("+TRK1", TString::kIgnoreCase) )TRK1=true;      cout << " The meaning of the option +ALL has changed!!             " << endl;
4389      };      cout << " Now it includes really all (level0+level1+level2+gpamela)" << endl;
4390                cout << " and the file is discarded if it does not contain         " << endl;
4391      if( detlist.Contains("TRK2", TString::kIgnoreCase)){      cout << " all trees or  if level0 files are not available!!        " << endl;
4392          if ( detlist.Contains("-TRK2", TString::kIgnoreCase) )TRK2=false;      cout << " ======================================================== " << endl;
         if ( detlist.Contains("+TRK2", TString::kIgnoreCase) )TRK2=true;  
     };  
4393    
4394      if( detlist.Contains("TRKh", TString::kIgnoreCase)){      CAL0 = true;
4395          if ( detlist.Contains("-TRKh", TString::kIgnoreCase) )TRKh=false;      CAL1 = true;
4396          if ( detlist.Contains("+TRKh", TString::kIgnoreCase) )TRKh=true;      CAL2 = true;
4397      };      TRK2 = true;
4398                TRK1 = true;
4399      if( detlist.Contains("+TRK", TString::kIgnoreCase) && !TRK1 && !TRK2 && !TRKh )TRK2=true;      TRKh = true;
4400      if( detlist.Contains("-TRK", TString::kIgnoreCase) && TRK1 && TRK2 && TRKh){      TRK0 = true;
4401          TRK2=false;      TRG = true;
4402          TRK1=false;      TOF = true;
4403          TRKh=false;      TOF0 = true;
4404      }      S4 = true;
4405  //  -------------------------------------------------------------------------      ND = true;
4406            AC = true;
4407      if( detlist.Contains("-TRG", TString::kIgnoreCase) )TRG = false;      ORB = true;
4408      else if( detlist.Contains("+TRG", TString::kIgnoreCase) )TRG = true;      GP = true;
4409          }
4410      if( detlist.Contains("-TOF", TString::kIgnoreCase) )TOF = false;    else if (detlist.Contains("-ALL", TString::kIgnoreCase)) {
4411      else if( detlist.Contains("+TOF", TString::kIgnoreCase) )TOF = true;      CAL0 = false;
4412        CAL1 = false;
4413        CAL2 = false;
4414        TRK2 = false;
4415        TRK1 = false;
4416        TRKh = false;
4417        TRK0 = false;
4418        TRG = false;
4419        TOF = false;
4420        TOF0 = false;
4421        S4 = false;
4422        ND = false;
4423        AC = false;
4424        ORB = false;
4425        GP = false;
4426      };
4427    
4428      if( detlist.Contains("-TOF0", TString::kIgnoreCase) )TOF0 = false;    //  -------------------------------------------------------------------------
4429      else if( detlist.Contains("+TOF0", TString::kIgnoreCase) )TOF0 = true;    if (detlist.Contains("CAL1", TString::kIgnoreCase)) {
4430            if (detlist.Contains("-CAL1", TString::kIgnoreCase))
4431      if( detlist.Contains("-S4",  TString::kIgnoreCase) )S4  = false;        CAL1 = false;
4432      else if( detlist.Contains("+S4",  TString::kIgnoreCase) )S4  = true;      if (detlist.Contains("+CAL1", TString::kIgnoreCase))
4433              CAL1 = true;
4434      if( detlist.Contains("-ND",  TString::kIgnoreCase) )ND  = false;    };
     else if( detlist.Contains("+ND",  TString::kIgnoreCase) )ND  = true;  
       
     if( detlist.Contains("-AC",  TString::kIgnoreCase) )AC  = false;  
     else if( detlist.Contains("+AC",  TString::kIgnoreCase) )AC  = true;  
       
     if( detlist.Contains("-ORB", TString::kIgnoreCase) )ORB = false;  
     else if( detlist.Contains("+ORB", TString::kIgnoreCase) )ORB = true;  
4435    
4436  //     cout<< "Set detector list --> ";    if (detlist.Contains("CAL0", TString::kIgnoreCase)) {
4437  //     if(TRK1)cout<<"TRK1 ";      if (detlist.Contains("-CAL0", TString::kIgnoreCase))
4438  //     if(TRK2)cout<<"TRK2 ";        CAL0 = false;
4439  //     if(TRKh)cout<<"TRKH ";      if (detlist.Contains("+CAL0", TString::kIgnoreCase))
4440  //     if(CAL1)cout<<"CAL1 ";        CAL0 = true;
4441  //     if(CAL2)cout<<"CAL2 ";    };
4442  //     if(TOF)cout<<"TOF ";  
4443  //     if(TRG)cout<<"TRG ";    if (detlist.Contains("CAL2", TString::kIgnoreCase)) {
4444  //     if(AC)cout<<"AC ";      if (detlist.Contains("-CAL2", TString::kIgnoreCase))
4445  //     if(ND)cout<<"ND ";        CAL2 = false;
4446  //     if(S4)cout<<"S4 ";      if (detlist.Contains("+CAL2", TString::kIgnoreCase))
4447  //     if(ORB)cout<<"ORB ";        CAL2 = true;
4448  //     cout << endl;    };
4449        
4450  };    if (detlist.Contains("+CAL", TString::kIgnoreCase) && !CAL1 && !CAL2)
4451        CAL2 = true;
4452      if (detlist.Contains("-CAL", TString::kIgnoreCase) && CAL1 && CAL2) {
4453        CAL2 = false;
4454        CAL1 = false;
4455      }
4456      //  -------------------------------------------------------------------------
4457      if (detlist.Contains("TRK0", TString::kIgnoreCase)) {
4458        if (detlist.Contains("-TRK0", TString::kIgnoreCase))
4459          TRK0 = false;
4460        if (detlist.Contains("+TRK0", TString::kIgnoreCase))
4461          TRK0 = true;
4462      };
4463    
4464      if (detlist.Contains("TRK1", TString::kIgnoreCase)) {
4465        if (detlist.Contains("-TRK1", TString::kIgnoreCase))
4466          TRK1 = false;
4467        if (detlist.Contains("+TRK1", TString::kIgnoreCase))
4468          TRK1 = true;
4469      };
4470    
4471      if (detlist.Contains("TRK2", TString::kIgnoreCase)) {
4472        if (detlist.Contains("-TRK2", TString::kIgnoreCase))
4473          TRK2 = false;
4474        if (detlist.Contains("+TRK2", TString::kIgnoreCase))
4475          TRK2 = true;
4476      };
4477    
4478      if (detlist.Contains("TRKh", TString::kIgnoreCase)) {
4479        if (detlist.Contains("-TRKh", TString::kIgnoreCase))
4480          TRKh = false;
4481        if (detlist.Contains("+TRKh", TString::kIgnoreCase))
4482          TRKh = true;
4483      };
4484    
4485      if (detlist.Contains("+TRK", TString::kIgnoreCase) && !TRK1 && !TRK2 && !TRKh)
4486        TRK2 = true;
4487      if (detlist.Contains("-TRK", TString::kIgnoreCase) && TRK1 && TRK2 && TRKh) {
4488        TRK2 = false;
4489        TRK1 = false;
4490        TRKh = false;
4491      }
4492      //  -------------------------------------------------------------------------
4493    
4494      if (detlist.Contains("-TRG", TString::kIgnoreCase))
4495        TRG = false;
4496      else if (detlist.Contains("+TRG", TString::kIgnoreCase))
4497        TRG = true;
4498    
4499      if (detlist.Contains("-TOF", TString::kIgnoreCase))
4500        TOF = false;
4501      else if (detlist.Contains("+TOF", TString::kIgnoreCase))
4502        TOF = true;
4503    
4504      if (detlist.Contains("-TOF0", TString::kIgnoreCase))
4505        TOF0 = false;
4506      else if (detlist.Contains("+TOF0", TString::kIgnoreCase))
4507        TOF0 = true;
4508    
4509      if (detlist.Contains("-S4", TString::kIgnoreCase))
4510        S4 = false;
4511      else if (detlist.Contains("+S4", TString::kIgnoreCase))
4512        S4 = true;
4513    
4514      if (detlist.Contains("-ND", TString::kIgnoreCase))
4515        ND = false;
4516      else if (detlist.Contains("+ND", TString::kIgnoreCase))
4517        ND = true;
4518    
4519      if (detlist.Contains("-AC", TString::kIgnoreCase))
4520        AC = false;
4521      else if (detlist.Contains("+AC", TString::kIgnoreCase))
4522        AC = true;
4523    
4524      if (detlist.Contains("-ORB", TString::kIgnoreCase))
4525        ORB = false;
4526      else if (detlist.Contains("+ORB", TString::kIgnoreCase))
4527        ORB = true;
4528    
4529      if (detlist.Contains("-GP", TString::kIgnoreCase))
4530        GP = false;
4531      else if (detlist.Contains("+GP", TString::kIgnoreCase))
4532        GP = true;
4533    
4534      cout << "tree/branch list from input --> ";
4535      if (TRK0)
4536        cout << "TRK0 ";
4537      if (TRK1)
4538        cout << "TRK1 ";
4539      if (TRK2)
4540        cout << "TRK2 ";
4541      if (TRKh)
4542        cout << "TRKH ";
4543      if (CAL0)
4544        cout << "CAL0 ";
4545      if (CAL1)
4546        cout << "CAL1 ";
4547      if (CAL2)
4548        cout << "CAL2 ";
4549      if (TOF)
4550        cout << "TOF ";
4551      if (TRG)
4552        cout << "TRG ";
4553      if (AC)
4554        cout << "AC ";
4555      if (ND)
4556        cout << "ND ";
4557      if (S4)
4558        cout << "S4 ";
4559      if (ORB)
4560        cout << "ORB ";
4561      if (GP)
4562        cout << "GP ";
4563      cout << endl;
4564      //     cout<< "Set detector list --> ";
4565      //     if(TRK1)cout<<"TRK1 ";
4566      //     if(TRK2)cout<<"TRK2 ";
4567      //     if(TRKh)cout<<"TRKH ";
4568      //     if(CAL1)cout<<"CAL1 ";
4569      //     if(CAL2)cout<<"CAL2 ";
4570      //     if(TOF0)cout<<"TOF0 ";
4571      //     if(TOF)cout<<"TOF ";
4572      //     if(TRG)cout<<"TRG ";
4573      //     if(AC)cout<<"AC ";
4574      //     if(ND)cout<<"ND ";
4575      //     if(S4)cout<<"S4 ";
4576      //     if(ORB)cout<<"ORB ";
4577      //     cout << endl;
4578    
4579    }
4580    ;
4581    
4582  /**  /**
4583   * Set tree/branch detector flags from the content of a tree   * Set tree/branch detector flags from the content of a tree
4584   */   */
4585  void  PamLevel2::GetWhichTrees(TFile* f){  void PamLevel2::GetWhichTrees(TFile* f) {
       
4586    
4587      //    cout << "void  PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;
4588      // -----------
4589      // reset flags
4590      // -----------
4591      CAL1 = false;
4592      CAL2 = false;
4593      TRK2 = false;
4594      TRK1 = false;
4595      TRKh = false;
4596      TRG = false;
4597      TOF = false;
4598      S4 = false;
4599      ND = false;
4600      AC = false;
4601      ORB = false;
4602      GP = false;
4603    
4604      RUN = false;
4605    
4606      //    cout << "Checking file: "<<f->GetName()<<endl;
4607      if (!f || f->IsZombie()) {
4608        cout << "File: " << f->GetName() << " Non valid root file" << endl;
4609        fDiscarded = true;
4610        return;
4611      }
4612    
4613      cout << "void  PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;    TList *lk = f->GetListOfKeys();
4614      // -----------    if (!lk)
4615      // reset flags      return;
4616      // -----------    TIter next(lk);
4617      CAL1   = false;        TKey *key = 0;
     CAL2   = false;      
     TRK2   = false;      
     TRK1   = false;      
     TRKh   = false;      
     TRG    = false;      
     TOF    = false;      
     S4     = false;      
     ND     = false;      
     AC     = false;      
     ORB    = false;      
       
     RUN    = false;  
           
     cout << "Checking file: "<<f->GetName()<<endl;  
     if( !f || f->IsZombie() ){  
         cout << "File: "<< f->GetName() <<" Non valid root file"<< endl;  
         return;  
     }  
4618    
4619      TList *lk = f->GetListOfKeys();    Int_t nev = 0;
     TIter next(lk);  
     TKey *key =0;  
4620    
4621      Int_t nev = 0;    while ((key = (TKey*) next())) {
4622    
4623      while( (key = (TKey*)next()) ){      if (!strcmp(key->GetName(), "Run"))
4624                  RUN = true;
4625          if( !strcmp(key->GetName(),"Run"        ) )RUN = true;  
4626        //=========================================================
4627        if (!strcmp(key->GetName(), "Trigger")) {
4628          TRG = true;
4629          Int_t nevt = ((TTree*) f->Get("Trigger"))->GetEntries();
4630          if (nev && nevt != nev) {
4631            cout << "File: " << f->GetName() << " Trigger tree has " << nevt << " events instead of " << nev << endl;
4632            TRG = false;
4633          }
4634          else
4635            nev = nevt;
4636        }
4637        //=========================================================
4638        if (!strcmp(key->GetName(), "ToF")) {
4639          TOF = true;
4640          Int_t nevt = ((TTree*) f->Get("ToF"))->GetEntries();
4641          if (nev && nevt != nev) {
4642            cout << "File: " << f->GetName() << "     ToF tree has " << nevt << " events instead of " << nev << endl;
4643            TOF = false;
4644          }
4645          else
4646            nev = nevt;
4647        }
4648        //=========================================================
4649        if (!strcmp(key->GetName(), "S4")) {
4650          S4 = true;
4651          Int_t nevt = ((TTree*) f->Get("S4"))->GetEntries();
4652          if (nev && nevt != nev) {
4653            cout << "File: " << f->GetName() << "      S4 tree has " << nevt << " events instead of " << nev << endl;
4654            S4 = false;
4655          }
4656          else
4657            nev = nevt;
4658        }
4659        //=========================================================
4660    
4661          //=========================================================          if (!strcmp(key->GetName(), "NeutronD")) {
4662          if( !strcmp(key->GetName(),"Trigger"    ) ){        ND = true;
4663              TRG = true;        Int_t nevt = ((TTree*) f->Get("NeutronD"))->GetEntries();
4664              Int_t nevt = ((TTree*)f->Get("Trigger"))->GetEntries();        if (nev && nevt != nev) {
4665              if( nev && nevt!=nev){          cout << "File: " << f->GetName() << "NeutronD tree has " << nevt << " events instead of " << nev << endl;
4666                  cout << "File: "<< f->GetName() <<" Trigger tree has "<<nevt<<" events instead of "<<nev<< endl;          ND = false;
4667                  TRG = false;        }
4668              }else nev=nevt;        else
4669          }          nev = nevt;
4670          //=========================================================          }
4671          if( !strcmp(key->GetName(),"ToF"        ) ){      //=========================================================
4672              TOF = true;      if (!strcmp(key->GetName(), "Anticounter")) {
4673              Int_t nevt = ((TTree*)f->Get("ToF"))->GetEntries();        AC = true;
4674              if( nev && nevt!=nev){        Int_t nevt = ((TTree*) f->Get("Anticounter"))->GetEntries();
4675                  cout << "File: "<< f->GetName() <<"     ToF tree has "<<nevt<<" events instead of "<<nev<< endl;        if (nev && nevt != nev) {
4676                  TOF = false;          cout << "File: " << f->GetName() << " Anticounter tree has " << nevt << " events instead of " << nev << endl;
4677              }else nev=nevt;          AC = false;
4678          }        }
4679          //=========================================================          else
4680          if( !strcmp(key->GetName(),"S4"         ) ){          nev = nevt;
4681              S4 = true;      }
4682              Int_t nevt = ((TTree*)f->Get("S4"))->GetEntries();      //=========================================================
4683              if( nev && nevt!=nev){      if (!strcmp(key->GetName(), "OrbitalInfo")) {
4684                  cout << "File: "<< f->GetName() <<"      S4 tree has "<<nevt<<" events instead of "<<nev<< endl;        ORB = true;
4685                  S4 = false;        Int_t nevt = ((TTree*) f->Get("OrbitalInfo"))->GetEntries();
4686              }else nev=nevt;        if (nev && nevt != nev) {
4687          }          cout << "File: " << f->GetName() << " OrbitalInfo tree has " << nevt << " events instead of " << nev << endl;
4688          //=========================================================            ORB = false;
4689          }
4690          else
4691            nev = nevt;
4692        }
4693        //=========================================================
4694        if (!strcmp(key->GetName(), "Tracker")) {
4695          TTree *T = (TTree*) f->Get("Tracker");
4696          for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
4697            TString name = T->GetListOfBranches()->At(i)->GetName();
4698            if (!name.CompareTo("TrkLevel1"))
4699              TRK1 = true;
4700            if (!name.CompareTo("TrkLevel2"))
4701              TRK2 = true;
4702            if (!name.CompareTo("TrkHough"))
4703              TRKh = true;
4704          };
4705          Int_t nevt = T->GetEntries();
4706          if (nev && nevt != nev) {
4707            cout << "File: " << f->GetName() << " Tracker tree has " << nevt << " events instead of " << nev << endl;
4708            TRK1 = false;
4709            TRK2 = false;
4710            TRKh = false;
4711          }
4712          else
4713            nev = nevt;
4714          //            T->Delete();
4715        };
4716        //=========================================================
4717        if (!strcmp(key->GetName(), "Calorimeter")) {
4718          TTree *T = (TTree*) f->Get("Calorimeter");
4719          for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
4720            TString name = T->GetListOfBranches()->At(i)->GetName();
4721            if (!name.CompareTo("CaloLevel1"))
4722              CAL1 = true;
4723            if (!name.CompareTo("CaloLevel2"))
4724              CAL2 = true;
4725          };
4726          Int_t nevt = T->GetEntries();
4727          if (nev && nevt != nev) {
4728            cout << "File: " << f->GetName() << "  Calorimeter tree has " << nevt << " events instead of " << nev << endl;
4729            CAL1 = false;
4730            CAL2 = false;
4731          }
4732          else
4733            nev = nevt;
4734          //            T->Delete();
4735        };
4736        //=========================================================
4737        if (!strcmp(key->GetName(), "h20")) {
4738          GP = true;
4739          Int_t nevt = ((TTree*) f->Get("h20"))->GetEntries();
4740          if (nev && nevt != nev) {
4741            cout << "File: " << f->GetName() << " h20 tree has " << nevt << " events instead of " << nev << endl;
4742            GP = false;
4743          }
4744          else
4745            nev = nevt;
4746        }
4747    
4748          if( !strcmp(key->GetName(),"NeutronD"   ) ){    };
             ND = true;  
             Int_t nevt = ((TTree*)f->Get("NeutronD"))->GetEntries();  
             if( nev && nevt!=nev){  
                 cout << "File: "<< f->GetName() <<"NeutronD tree has "<<nevt<<" events instead of "<<nev<< endl;  
                 ND =false;  
             }else nev=nevt;  
         }        
         //=========================================================    
         if( !strcmp(key->GetName(),"Anticounter") ){  
             AC = true;  
             Int_t nevt = ((TTree*)f->Get("Anticounter"))->GetEntries();  
             if( nev && nevt!=nev){  
                 cout << "File: "<< f->GetName() <<" Anticounter tree has "<<nevt<<" events instead of "<<nev<< endl;  
                 AC =false;  
             }else nev=nevt;  
         }  
         //=========================================================    
         if( !strcmp(key->GetName(),"OrbitalInfo") ){  
             ORB = true;  
             Int_t nevt = ((TTree*)f->Get("OrbitalInfo"))->GetEntries();  
             if( nev && nevt!=nev){  
                 cout << "File: "<< f->GetName() <<" OrbitalInfo tree has "<<nevt<<" events instead of "<<nev<< endl;  
                 ORB = false;  
             }else nev=nevt;  
         }  
         //=========================================================    
         if( !strcmp(key->GetName(),"Tracker"    ) ){  
             TTree *T = (TTree*)f->Get("Tracker");  
             for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){  
                 TString name = T->GetListOfBranches()->At(i)->GetName();  
                 if( !name.CompareTo("TrkLevel1") )TRK1=true;  
                 if( !name.CompareTo("TrkLevel2") )TRK2=true;  
                 if( !name.CompareTo("TrkHough") )TRKh=true;  
             };        
             Int_t nevt = T->GetEntries();  
             if( nev && nevt!=nev){  
                 cout << "File: "<< f->GetName() <<" Tracker tree has "<<nevt<<" events instead of "<<nev<< endl;  
                 TRK1 = false;  
                 TRK2 = false;  
                 TRKh = false;  
             }else nev=nevt;  
             T->Delete();  
         };  
         //=========================================================    
         if( !strcmp(key->GetName(),"Calorimeter"    ) ){  
             TTree *T = (TTree*)f->Get("Calorimeter");  
             for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){  
                 TString name = T->GetListOfBranches()->At(i)->GetName();  
                 if( !name.CompareTo("CaloLevel1") )CAL1=true;  
                 if( !name.CompareTo("CaloLevel2") )CAL2=true;  
             };      
             Int_t nevt = T->GetEntries();  
             if( nev && nevt!=nev){  
                 cout << "File: "<< f->GetName() <<"  Calorimeter tree has "<<nevt<<" events instead of "<<nev<< endl;  
                 CAL1 = false;  
                 CAL2 = false;  
             }else nev=nevt;  
             T->Delete();  
         };        
4749    
4750      };    //    delete lk;
       
     delete lk;  
4751    
4752  //     cout<< "Get detector list from input file --> ";    cout << "tree/branch list from file  --> ";
4753  //     if(TRK1)cout<<"TRK1 ";    if (TRK1)
4754  //     if(TRK2)cout<<"TRK2 ";      cout << "TRK1 ";
4755  //     if(TRKh)cout<<"TRKH ";    if (TRK2)
4756  //     if(CAL1)cout<<"CAL1 ";      cout << "TRK2 ";
4757  //     if(CAL2)cout<<"CAL2 ";    if (TRKh)
4758  //     if(TOF)cout<<"TOF ";      cout << "TRKH ";
4759  //     if(TRG)cout<<"TRG ";    if (CAL1)
4760  //     if(AC)cout<<"AC ";      cout << "CAL1 ";
4761  //     if(ND)cout<<"ND ";    if (CAL2)
4762  //     if(S4)cout<<"S4 ";      cout << "CAL2 ";
4763  //     if(ORB)cout<<"ORB ";    if (TOF)
4764  //     cout << endl;      cout << "TOF ";
4765            if (TRG)
4766      return ;      cout << "TRG ";
4767              if (AC)
4768  };      cout << "AC ";
4769      if (ND)
4770        cout << "ND ";
4771      if (S4)
4772        cout << "S4 ";
4773      if (ORB)
4774        cout << "ORB ";
4775      if (GP)
4776        cout << "GP ";
4777      cout << endl;
4778    
4779      return;
4780    
4781    }
4782    ;
4783    
4784  //--------------------------------------  //--------------------------------------
4785  //  //
# Line 2664  void  PamLevel2::GetWhichTrees(TFile* f) Line 4790  void  PamLevel2::GetWhichTrees(TFile* f)
4790   * @param name File name   * @param name File name
4791   * @return true if the file is ok.   * @return true if the file is ok.
4792   */   */
4793  Bool_t  PamLevel2::CheckLevel2File(TString name){  Bool_t PamLevel2::CheckLevel2File(TString name) {
4794            
4795      Bool_t CAL1__ok   = false;        Bool_t CAL1__ok = false;
4796      Bool_t CAL2__ok   = false;        Bool_t CAL2__ok = false;
4797      Bool_t TRK2__ok   = false;        Bool_t TRK2__ok = false;
4798      Bool_t TRK1__ok   = false;        Bool_t TRK1__ok = false;
4799      Bool_t TRKh__ok   = false;        Bool_t TRKh__ok = false;
4800      Bool_t TRG__ok    = false;        Bool_t TRG__ok = false;
4801      Bool_t TOF__ok    = false;        Bool_t TOF__ok = false;
4802      Bool_t S4__ok     = false;        Bool_t S4__ok = false;
4803      Bool_t ND__ok     = false;        Bool_t ND__ok = false;
4804      Bool_t AC__ok     = false;        Bool_t AC__ok = false;
4805      Bool_t ORB__ok    = false;        Bool_t ORB__ok = false;
4806          Bool_t GP__ok = false;
4807      Bool_t RUN__ok    = false;  
4808          Bool_t RUN__ok = false;
4809      Bool_t SELLI__ok  = false;  
4810      Bool_t SELLI__ok = false;
4811    
4812      //    cout << "Checking file: "<<name<<endl;
4813      TFile *f = new TFile(name.Data());
4814      if (!f || f->IsZombie()) {
4815        cout << "File: " << f->GetName() << " discarded ---- Non valid root file" << endl;
4816        fDiscarded = true;
4817        return false;
4818      }
4819      //    cout << "Get list of keys: "<<f<<endl;
4820      TList *lk = f->GetListOfKeys();
4821      //    lk->Print();
4822      TIter next(lk);
4823      TKey *key = 0;
4824    
4825      Int_t nev = 0;
4826    
4827      while ((key = (TKey*) next())) {
4828    
4829        //  cout << key->GetName() << endl;
4830        //  cout << key->GetName() << ""<<key->GetClassName()<<endl;
4831        //          cout << " Get tree: " << f->Get(key->GetName())<<endl;
4832        //  nev_previous = nev;
4833        //  cout << " n.entries  "<< nev <<endl;
4834        //  if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){
4835        //      nev = ((TTree*)f->Get(key->GetName()))->GetEntries();
4836        //      cout << "File: "<< f->GetName() <<" discarded ---- "<< key->GetName() << " tree: n.entries does not match "<<nev<<" "<<nev_previous<< endl;
4837        //      return false;
4838        //  };
4839    
4840        //=========================================================
4841        // check if the file
4842    
4843    
4844        if (!strcmp(key->GetName(), "Run"))
4845          RUN__ok = true;
4846    
4847        //=========================================================
4848        if (!strcmp(key->GetName(), "SelectionList")) {
4849          SELLI__ok = true;
4850          if (SELLI == 1) {
4851            Int_t nevt = ((TTree*) f->Get("SelectionList"))->GetEntries();
4852            if (nev && nevt != nev) {
4853              cout << "File: " << f->GetName() << " discarded ---- SelectionList tree has " << nevt
4854                  << " events instead of " << nev << endl;
4855              fDiscarded = true;
4856              return false;
4857            }
4858            nev = nevt;
4859          }
4860        }
4861    
4862        //=========================================================
4863        if (!strcmp(key->GetName(), "Trigger")) {
4864          TRG__ok = true;
4865          if (TRG) {
4866            Int_t nevt = ((TTree*) f->Get("Trigger"))->GetEntries();
4867            if (nev && nevt != nev) {
4868              cout << "File: " << f->GetName() << " discarded ---- Trigger tree has " << nevt << " events instead of "
4869                  << nev << endl;
4870              fDiscarded = true;
4871              return false;
4872            }
4873            nev = nevt;
4874          }
4875        }
4876        //=========================================================
4877        if (!strcmp(key->GetName(), "ToF")) {
4878          TOF__ok = true;
4879          if (TOF) {
4880            Int_t nevt = ((TTree*) f->Get("ToF"))->GetEntries();
4881            if (nev && nevt != nev) {
4882              cout << "File: " << f->GetName() << " discarded ---- ToF tree has " << nevt << " events instead of " << nev
4883                  << endl;
4884              fDiscarded = true;
4885              return false;
4886            }
4887            nev = nevt;
4888          }
4889        }
4890        //=========================================================
4891        if (!strcmp(key->GetName(), "S4")) {
4892          S4__ok = true;
4893          if (S4) {
4894            Int_t nevt = ((TTree*) f->Get("S4"))->GetEntries();
4895            if (nev && nevt != nev) {
4896              cout << "File: " << f->GetName() << " discarded ---- S4 tree has " << nevt << " events instead of " << nev
4897                  << endl;
4898              fDiscarded = true;
4899              return false;
4900            }
4901            nev = nevt;
4902          }
4903        }
4904        //=========================================================
4905    
4906      cout << "Checking file: "<<name<<endl;      if (!strcmp(key->GetName(), "NeutronD")) {
4907      TFile *f = new TFile(name.Data());        ND__ok = true;
4908      if( !f || f->IsZombie() ){        if (ND) {
4909          cout << "File: "<< f->GetName() <<" discarded ---- Non valid root file"<< endl; return false;          Int_t nevt = ((TTree*) f->Get("NeutronD"))->GetEntries();
4910      }          if (nev && nevt != nev) {
4911  //    cout << "Get list of keys: "<<f<<endl;            cout << "File: " << f->GetName() << " discarded ---- NeutronD tree has " << nevt << " events instead of "
4912      TList *lk = f->GetListOfKeys();                << nev << endl;
4913  //    lk->Print();            fDiscarded = true;
4914      TIter next(lk);            return false;
4915      TKey *key =0;          }
4916            nev = nevt;
4917          }
4918        }
4919        //=========================================================
4920        if (!strcmp(key->GetName(), "Anticounter")) {
4921          AC__ok = true;
4922          if (AC) {
4923            Int_t nevt = ((TTree*) f->Get("Anticounter"))->GetEntries();
4924            if (nev && nevt != nev) {
4925              cout << "File: " << f->GetName() << " discarded ---- Anticounter tree has " << nevt << " events instead of "
4926                  << nev << endl;
4927              fDiscarded = true;
4928              return false;
4929            }
4930            nev = nevt;
4931          }
4932        }
4933        //=========================================================
4934        if (!strcmp(key->GetName(), "OrbitalInfo")) {
4935          ORB__ok = true;
4936          if (ORB) {
4937            Int_t nevt = ((TTree*) f->Get("OrbitalInfo"))->GetEntries();
4938            if (nev && nevt != nev) {
4939              cout << "File: " << f->GetName() << " discarded ---- OrbitalInfo tree has " << nevt << " events instead of "
4940                  << nev << endl;
4941              fDiscarded = true;
4942              return false;
4943            }
4944            nev = nevt;
4945          }
4946        }
4947        //=========================================================
4948        if (!strcmp(key->GetName(), "Tracker")) {
4949          TTree *T = (TTree*) f->Get("Tracker");
4950          if (TRK1 || TRK2 || TRKh) {
4951            Int_t nevt = T->GetEntries();
4952            if (nev && nevt != nev) {
4953              cout << "File: " << f->GetName() << " discarded ---- Tracker tree has " << nevt << " events instead of "
4954                  << nev << endl;
4955              fDiscarded = true;
4956              return false;
4957            }
4958            nev = nevt;
4959          }
4960          for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
4961            TString name = T->GetListOfBranches()->At(i)->GetName();
4962            if (!name.CompareTo("TrkLevel1"))
4963              TRK1__ok = true;
4964            if (!name.CompareTo("TrkLevel2"))
4965              TRK2__ok = true;
4966            if (!name.CompareTo("TrkHough"))
4967              TRKh__ok = true;
4968          };
4969          T->Delete();
4970        };
4971        //=========================================================
4972        if (!strcmp(key->GetName(), "Calorimeter")) {
4973          TTree *T = (TTree*) f->Get("Calorimeter");
4974          if (CAL1 || CAL2) {
4975            Int_t nevt = T->GetEntries();
4976            if (nev && nevt != nev) {
4977              cout << "File: " << f->GetName() << " discarded ---- Calorimeter tree has " << nevt << " events instead of "
4978                  << nev << endl;
4979              fDiscarded = true;
4980              return false;
4981            }
4982            nev = nevt;
4983          }
4984          for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
4985            TString name = T->GetListOfBranches()->At(i)->GetName();
4986            if (!name.CompareTo("CaloLevel1"))
4987              CAL1__ok = true;
4988            if (!name.CompareTo("CaloLevel2"))
4989              CAL2__ok = true;
4990          };
4991          T->Delete();
4992        };
4993        //=========================================================
4994        if (!strcmp(key->GetName(), "h20")) {
4995          ISGP = true;
4996          GP__ok = true;
4997          if (GP) {
4998            Int_t nevt = ((TTree*) f->Get("h20"))->GetEntries();
4999            if (nev && nevt != nev) {
5000              cout << "File: " << f->GetName() << " discarded ---- h20 tree has " << nevt << " events instead of " << nev
5001                  << endl;
5002              fDiscarded = true;
5003              return false;
5004            }
5005            nev = nevt;
5006          }
5007        }
5008    
5009      Int_t nev = 0;    };
5010    
5011      while( (key = (TKey*)next()) ){    if (SELLI == -1)
5012                SELLI = (Int_t) SELLI__ok;
5013  //      cout << key->GetName() << endl;    if (SELLI == 0 && SELLI__ok) {
5014  //      cout << key->GetName() << ""<<key->GetClassName()<<endl;      cout << "File: " << f->GetName() << " discarded ---- found SelectionList (it is not a full-event file)" << endl;
5015  //              cout << " Get tree: " << f->Get(key->GetName())<<endl;      fDiscarded = true;
5016  //      nev_previous = nev;      return false;
5017  //      cout << " n.entries  "<< nev <<endl;    }
5018  //      if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){    if (SELLI == 1 && !SELLI__ok) {
5019  //          nev = ((TTree*)f->Get(key->GetName()))->GetEntries();      cout << "File: " << f->GetName() << " discarded ---- SelectionList missing" << endl;
5020  //          cout << "File: "<< f->GetName() <<" discarded ---- "<< key->GetName() << " tree: n.entries does not match "<<nev<<" "<<nev_previous<< endl;      fDiscarded = true;
5021  //          return false;      return false;
5022  //      };    }
   
   
         if( !strcmp(key->GetName(),"Run"        ) )RUN__ok = true;        
   
         //=========================================================      
         if( !strcmp(key->GetName(),"SelectionList"    ) ){  
             SELLI__ok = true;  
             if(SELLI==1){  
                 Int_t nevt = ((TTree*)f->Get("SelectionList"))->GetEntries();  
                 if( nev && nevt!=nev){  
                     cout << "File: "<< f->GetName() <<" discarded ---- SelectionList tree has "<<nevt<<" events instead of "<<nev<< endl;  
                     return false;  
                 }  
                 nev=nevt;  
             }  
         }  
5023    
5024          //=========================================================        //    cout << "SELLI "<<SELLI<<endl;
         if( !strcmp(key->GetName(),"Trigger"    ) ){  
             TRG__ok = true;  
             if(TRG){  
                 Int_t nevt = ((TTree*)f->Get("Trigger"))->GetEntries();  
                 if( nev && nevt!=nev){  
                     cout << "File: "<< f->GetName() <<" discarded ---- Trigger tree has "<<nevt<<" events instead of "<<nev<< endl;  
                     return false;  
                 }  
                 nev=nevt;  
             }  
         }  
         //=========================================================      
         if( !strcmp(key->GetName(),"ToF"        ) ){  
             TOF__ok = true;  
             if(TOF){  
                 Int_t nevt = ((TTree*)f->Get("ToF"))->GetEntries();  
                 if( nev && nevt!=nev){  
                     cout << "File: "<< f->GetName() <<" discarded ---- ToF tree has "<<nevt<<" events instead of "<<nev<< endl;  
                     return false;  
                 }  
                 nev=nevt;  
             }  
         }  
         //=========================================================    
         if( !strcmp(key->GetName(),"S4"         ) ){  
             S4__ok = true;  
             if(S4){  
                 Int_t nevt = ((TTree*)f->Get("S4"))->GetEntries();  
                 if( nev && nevt!=nev){  
                     cout << "File: "<< f->GetName() <<" discarded ---- S4 tree has "<<nevt<<" events instead of "<<nev<< endl;  
                     return false;  
                 }  
                 nev=nevt;  
             }  
         }  
         //=========================================================    
5025    
5026          if( !strcmp(key->GetName(),"NeutronD"   ) ){    //     cout<< "CheckLevel2File(TString): detector list --> ";
5027              ND__ok = true;    //     if(TRK1__ok)cout<<"TRK1 ";
5028              if(ND){    //     if(TRK2__ok)cout<<"TRK2 ";
5029                  Int_t nevt = ((TTree*)f->Get("NeutronD"))->GetEntries();    //     if(TRKh__ok)cout<<"TRKH ";
5030                  if( nev && nevt!=nev){    //     if(CAL1__ok)cout<<"CAL1 ";
5031                      cout << "File: "<< f->GetName() <<" discarded ---- NeutronD tree has "<<nevt<<" events instead of "<<nev<< endl;    //     if(CAL2__ok)cout<<"CAL2 ";
5032                      return false;    //     if(TOF__ok)cout<<"TOF ";
5033                  }    //     if(TRG__ok)cout<<"TRG ";
5034                  nev=nevt;    //     if(AC__ok)cout<<"AC ";
5035              }    //     if(ND__ok)cout<<"ND ";
5036          }          //     if(S4__ok)cout<<"S4 ";
5037          //=========================================================      //     if(ORB__ok)cout<<"ORB ";
5038          if( !strcmp(key->GetName(),"Anticounter") ){    //     cout << endl;
5039              AC__ok = true;  
5040              if(AC){  
5041                  Int_t nevt = ((TTree*)f->Get("Anticounter"))->GetEntries();    if (TRK2 && TRK1__ok)
5042                  if( nev && nevt!=nev){      TRK1 = 1;
5043                      cout << "File: "<< f->GetName() <<" discarded ---- Anticounter tree has "<<nevt<<" events instead of "<<nev<< endl;    // ----------------------------------------------------------------------------
5044                      return false;    // NOTA
5045                  }    // se c'e` il level1, lo devo necessarimente leggere.
5046                  nev=nevt;    // infatti (non ho capito perche`) i cluster vengono letti e allocati in memoria
5047              }    // comunque, ma non vengono disallocati da PamLevel2::Clear()
5048          }    // ----------------------------------------------------------------------------
5049          //=========================================================    
5050          if( !strcmp(key->GetName(),"OrbitalInfo") ){  
5051              ORB__ok = true;    if (!RUN__ok) {
5052              if(ORB){      cout << "File: " << f->GetName() << " *WARNING* ---- Missing RunInfo tree (NB: RUN infos will not be updated)"
5053                  Int_t nevt = ((TTree*)f->Get("OrbitalInfo"))->GetEntries();          << endl;
5054                  if( nev && nevt!=nev){      RUN = false;
5055                      cout << "File: "<< f->GetName() <<" discarded ---- OrbitalInfo tree has "<<nevt<<" events instead of "<<nev<< endl;    };
                     return false;  
                 }  
                 nev=nevt;  
             }  
         }  
         //=========================================================    
         if( !strcmp(key->GetName(),"Tracker"    ) ){  
             TTree *T = (TTree*)f->Get("Tracker");  
             if(TRK1||TRK2||TRKh){  
                 Int_t nevt = T->GetEntries();  
                 if( nev && nevt!=nev){  
                     cout << "File: "<< f->GetName() <<" discarded ---- Tracker tree has "<<nevt<<" events instead of "<<nev<< endl;  
                     return false;  
                 }  
                 nev=nevt;  
             }  
             for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){  
                 TString name = T->GetListOfBranches()->At(i)->GetName();  
                 if( !name.CompareTo("TrkLevel1") )TRK1__ok=true;  
                 if( !name.CompareTo("TrkLevel2") )TRK2__ok=true;  
                 if( !name.CompareTo("TrkHough") )TRKh__ok=true;  
             };        
             T->Delete();  
         };  
         //=========================================================    
         if( !strcmp(key->GetName(),"Calorimeter"    ) ){  
             TTree *T = (TTree*)f->Get("Calorimeter");  
             if(CAL1||CAL2){  
                 Int_t nevt = T->GetEntries();  
                 if( nev && nevt!=nev){  
                     cout << "File: "<< f->GetName() <<" discarded ---- Calorimeter tree has "<<nevt<<" events instead of "<<nev<< endl;  
                     return false;  
                 }  
                 nev=nevt;  
             }  
             for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){  
                 TString name = T->GetListOfBranches()->At(i)->GetName();  
                 if( !name.CompareTo("CaloLevel1") )CAL1__ok=true;  
                 if( !name.CompareTo("CaloLevel2") )CAL2__ok=true;  
             };      
             T->Delete();  
         };        
5056    
5057      };    if (CAL1 && !CAL1__ok) {
5058        cout << "File: " << f->GetName() << " discarded ---- Missing CaloLevel1 branch" << endl;
5059        fDiscarded = true;
5060        return false;
5061      };
5062      if (CAL2 && !CAL2__ok) {
5063        cout << "File: " << f->GetName() << " discarded ---- Missing CaloLevel2 branch" << endl;
5064        fDiscarded = true;
5065        return false;
5066      };
5067      if (TRK2 && !TRK2__ok) {
5068        cout << "File: " << f->GetName() << " discarded ---- Missing TrkLevel2 branch" << endl;
5069        fDiscarded = true;
5070        return false;
5071      };
5072      if (TRK1 && !TRK1__ok) {
5073        cout << "File: " << f->GetName() << " discarded ---- Missing TrkLevel1 branch" << endl;
5074        fDiscarded = true;
5075        return false;
5076      };
5077      if (TRKh && !TRKh__ok) {
5078        cout << "File: " << f->GetName() << " discarded ---- Missing TrkHough branch" << endl;
5079        fDiscarded = true;
5080        return false;
5081      };
5082      if (ORB && !ORB__ok) {
5083        cout << "File: " << f->GetName() << " discarded ---- Missing ORB tree" << endl;
5084        fDiscarded = true;
5085        return false;
5086      };
5087      if (AC && !AC__ok) {
5088        cout << "File: " << f->GetName() << " discarded ---- Missing AC tree" << endl;
5089        fDiscarded = true;
5090        return false;
5091      };
5092      if (S4 && !S4__ok) {
5093        cout << "File: " << f->GetName() << " discarded ---- Missing S4 tree" << endl;
5094        fDiscarded = true;
5095        return false;
5096      };
5097      if (TOF && !TOF__ok) {
5098        cout << "File: " << f->GetName() << " discarded ---- Missing ToF tree" << endl;
5099        fDiscarded = true;
5100        return false;
5101      };
5102    
5103      if( SELLI==-1 )SELLI = (Int_t)SELLI__ok;    if (ND && !ND__ok) {
5104      if( SELLI==0 && SELLI__ok ){      cout << "File: " << f->GetName() << " discarded ---- Missing ND tree" << endl;
5105          cout << "File: "<< f->GetName() <<" discarded ---- found SelectionList (it is not a full-event file)" << endl;      fDiscarded = true;
5106          return false;        return false;
5107      }    };
5108      if( SELLI==1 && !SELLI__ok ){    if (TRG && !TRG__ok) {
5109          cout << "File: "<< f->GetName() <<" discarded ---- SelectionList missing" << endl;      cout << "File: " << f->GetName() << " discarded ---- Missing Trigger tree" << endl;
5110          return false;      fDiscarded = true;
5111      }      return false;
5112          };
5113  //    cout << "SELLI "<<SELLI<<endl;    if (GP && !GP__ok) {
5114        cout << "File: " << f->GetName() << " discarded ---- Missing h20 tree" << endl;
5115        fDiscarded = true;
5116        return false;
5117      };
5118    
5119  //     cout<< "CheckLevel2File(TString): detector list --> ";    //    lk->Delete();
5120  //     if(TRK1__ok)cout<<"TRK1 ";    //    delete lk;
5121  //     if(TRK2__ok)cout<<"TRK2 ";    f->Close();
5122  //     if(TRKh__ok)cout<<"TRKH ";  
5123  //     if(CAL1__ok)cout<<"CAL1 ";    //     cout<< "CheckLevel2File(TString): detector list --> ";
5124  //     if(CAL2__ok)cout<<"CAL2 ";    //     if(TRK1)cout<<"TRK1 ";
5125  //     if(TOF__ok)cout<<"TOF ";    //     if(TRK2)cout<<"TRK2 ";
5126  //     if(TRG__ok)cout<<"TRG ";    //     if(TRKh)cout<<"TRKH ";
5127  //     if(AC__ok)cout<<"AC ";    //     if(CAL1)cout<<"CAL1 ";
5128  //     if(ND__ok)cout<<"ND ";    //     if(CAL2)cout<<"CAL2 ";
5129  //     if(S4__ok)cout<<"S4 ";    //     if(TOF)cout<<"TOF ";
5130  //     if(ORB__ok)cout<<"ORB ";    //     if(TRG)cout<<"TRG ";
5131  //     cout << endl;    //     if(AC)cout<<"AC ";
5132      //     if(ND)cout<<"ND ";
5133      //     if(S4)cout<<"S4 ";
5134      if(TRK2 && TRK1__ok)TRK1=1;    //     if(ORB)cout<<"ORB ";
5135  // ----------------------------------------------------------------------------    //     if(GP)cout<<"GP ";
5136  // NOTA    //     cout << endl;
 // se c'e` il level1, lo devo necessarimente leggere.  
 // infatti (non ho capito perche`) i cluster vengono letti e allocati in memoria  
 // comunque, ma non vengono disallocati da PamLevel2::Clear()  
 // ----------------------------------------------------------------------------  
   
   
     if(!RUN__ok) {  
         cout << "File: "<< f->GetName() <<" *WARNING* ---- Missing RunInfo tree"<< endl;  
 //      return false;  
     };  
   
     if(CAL1 && !CAL1__ok){  
         cout << "File: "<< f->GetName() <<" discarded ---- Missing CaloLevel1 branch"<< endl;  
         return false;  
     };  
     if(CAL2 && !CAL2__ok){  
         cout << "File: "<< f->GetName() <<" discarded ---- Missing CaloLevel2 branch"<< endl;  
         return false;  
     };  
     if(TRK2 && !TRK2__ok){  
         cout << "File: "<< f->GetName() <<" discarded ---- Missing TrkLevel2 branch"<< endl;  
         return false;  
     };  
     if(TRK1 && !TRK1__ok){  
         cout << "File: "<< f->GetName() <<" discarded ---- Missing TrkLevel1 branch"<< endl;  
         return false;  
     };  
     if(TRKh && !TRKh__ok){  
         cout << "File: "<< f->GetName() <<" discarded ---- Missing TrkHough branch"<< endl;  
         return false;  
     };  
     if(ORB && !ORB__ok){  
         cout << "File: "<< f->GetName() <<" discarded ---- Missing ORB tree"<< endl;  
         return false;  
     };  
     if(AC && !AC__ok){  
         cout << "File: "<< f->GetName() <<" discarded ---- Missing AC tree"<< endl;  
         return false;  
     };  
     if(S4 && !S4__ok){  
         cout << "File: "<< f->GetName() <<" discarded ---- Missing S4 tree"<< endl;  
         return false;  
     };  
     if(TOF && !TOF__ok){  
         cout << "File: "<< f->GetName() <<" discarded ---- Missing ToF tree"<< endl;  
         return false;  
     };  
   
     if(ND && !ND__ok){  
         cout << "File: "<< f->GetName() <<" discarded ---- Missing ND tree"<< endl;  
         return false;  
     };  
     if(TRG && !TRG__ok){  
         cout << "File: "<< f->GetName() <<" discarded ---- Missing Trigger tree"<< endl;  
         return false;  
     };  
       
5137    
5138  //    lk->Delete();    return true;
 //    delete lk;  
     f->Close();  
   
 //     cout<< "CheckLevel2File(TString): detector list --> ";  
 //     if(TRK1)cout<<"TRK1 ";  
 //     if(TRK2)cout<<"TRK2 ";  
 //     if(TRKh)cout<<"TRKH ";  
 //     if(CAL1)cout<<"CAL1 ";  
 //     if(CAL2)cout<<"CAL2 ";  
 //     if(TOF)cout<<"TOF ";  
 //     if(TRG)cout<<"TRG ";  
 //     if(AC)cout<<"AC ";  
 //     if(ND)cout<<"ND ";  
 //     if(S4)cout<<"S4 ";  
 //     if(ORB)cout<<"ORB ";  
 //     cout << endl;  
         
     return true;  
           
 };  
5139    
5140    }
5141    ;
5142    
5143  /**  /**
5144   * Create clone-trees   * Create clone-trees
5145   */   */
5146  void PamLevel2::CreateCloneTrees0( TChain *fChain, TFile *ofile ){  void PamLevel2::CreateCloneTrees0(TChain *fChain, TFile *ofile) {
5147    
5148      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5149      cout << "Create clones of PAMELA trees "<<endl;    cout << "Create clones of PAMELA trees " << endl;
5150        
5151      Int_t i=0;    Int_t i = 0;
5152      pam_tree_clone[i] = fChain->GetTree()->CloneTree(0);    pam_tree_clone[i] = fChain->GetTree()->CloneTree(0);
5153      TString name =  pam_tree_clone[i]->GetName();    TString name = pam_tree_clone[i]->GetName();
5154      name.Append("_clone");
5155      //    pam_tree_clone[i]->SetName(name.Data());
5156      cout << pam_tree_clone[i]->GetName() << endl;
5157      i++;
5158    
5159      TList *li = fChain->GetListOfFriends();
5160      TIter next(li);
5161      TFriendElement* T_friend = 0;
5162      ofile->cd();
5163      while ((T_friend = (TFriendElement*) next())) {
5164        //  cout<<T_friend->IsA()->GetName()<<" "<<T_friend->GetName()<<hex << T_friend->GetTree() << dec<<endl;
5165        //  cout<<T_friend->GetTree()->GetName()<< endl;
5166        pam_tree_clone[i] = T_friend->GetTree()->CloneTree(0);
5167        pam_tree_clone[i]->SetAutoSave(1000000);
5168        name = pam_tree_clone[i]->GetName();
5169      name.Append("_clone");      name.Append("_clone");
5170  //    pam_tree_clone[i]->SetName(name.Data());      //  pam_tree_clone[i]->SetName(name.Data());
5171      cout << pam_tree_clone[i]->GetName() <<endl;      cout << pam_tree_clone[i]->GetName() << endl;
5172      i++;      i++;
5173      }
5174    
5175      TList *li = fChain->GetListOfFriends();    delete li;
     TIter next(li);  
     TFriendElement* T_friend=0;  
     ofile->cd();  
     while( (T_friend = (TFriendElement*)next()) ){  
 //      cout<<T_friend->IsA()->GetName()<<" "<<T_friend->GetName()<<hex << T_friend->GetTree() << dec<<endl;  
 //      cout<<T_friend->GetTree()->GetName()<< endl;  
         pam_tree_clone[i] = T_friend->GetTree()->CloneTree(0);  
         pam_tree_clone[i]->SetAutoSave(1000000);  
         name =  pam_tree_clone[i]->GetName();  
         name.Append("_clone");  
 //      pam_tree_clone[i]->SetName(name.Data());  
         cout << pam_tree_clone[i]->GetName() << endl;  
         i++;  
     }  
       
     delete li;  
5176    
5177      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5178    
5179  }  }
5180    
5181  /**  /**
5182   * Create clone-trees   * Create clone-trees
5183   */   */
5184  void PamLevel2::CreateCloneTrees(TFile *ofile){  void PamLevel2::CreateCloneTrees(TFile *ofile) {
   
5185    
5186  //  if the pointer is null, create a default file    //  if the pointer is null, create a default file
5187      if (!run_tree)
5188        return;
5189    
5190      if(!ofile){    if (!ofile) {
5191          cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root "<<endl;      cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root " << endl;
5192          ofile = new TFile("clone-tree.root","recreate");      ofile = new TFile("clone-tree.root", "recreate");
5193      }    }
5194    
5195      ofile->cd();    ofile->cd();
5196    
5197      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5198      cout << "Create new PAMELA trees "<<endl;    cout << "Create new PAMELA trees " << endl;
           
5199    
5200      run_tree_clone = new TTree("Run","PAMELA Level2 data from the GL_RUN table ");    run_tree_clone = new TTree("Run", "PAMELA Level2 data from the GL_RUN table ");
5201      run_tree_clone->Branch("RunInfo","GL_RUN",GetPointerTo("RunInfo"));    run_tree_clone->Branch("RunInfo", "GL_RUN", GetPointerTo("RunInfo"));
5202      cout << "Run          : branch RunInfo"<<endl;    cout << "Run          : branch RunInfo" << endl;
5203      run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));    run_tree_clone->Branch("SoftInfo", "SoftInfo", GetPointerTo("SoftInfo"));
5204      cout << "Run          : branch SoftInfo"<<endl;    cout << "Run          : branch SoftInfo" << endl;
5205      // ------------------
5206      // replicate run tree
5207      // ------------------
5208      //    cout << "----------------------------------------------------"<<endl;
5209      //    cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
5210      for (Int_t i = 0; i < run_tree->GetEntries(); i++) {
5211        run_tree->GetEntry(i);
5212        //  cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
5213        run_tree_clone->Fill();
5214      }
5215      if ( PROC ){
5216        proc_tree_clone = new TTree("ProcessingInfo","Log of data processing");
5217        proc_tree_clone->Branch("ProcInfo", "ProcInfo", GetPointerTo("ProcInfo"));
5218        cout << "ProcessingInfo: branch ProcessingInfo" << endl;  
5219      // ------------------      // ------------------
5220      // replicate run tree      // replicate processinginfo tree
5221      // ------------------      // ------------------
5222      cout << "----------------------------------------------------"<<endl;      //    cout << "----------------------------------------------------"<<endl;
5223      cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;      //    cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
5224      for (Int_t i=0; i<run_tree->GetEntries(); i++){      for (Int_t i = 0; i < proc_tree->GetEntries(); i++) {
5225          run_tree->GetEntry(i);        proc_tree->GetEntry(i);
5226          cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;        //        cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
5227          run_tree_clone->Fill();        proc_tree_clone->Fill();
5228      }      }
5229      cout << "----------------------------------------------------"<<endl;      if ( SELLI != 2 || true ){      
5230          proc_obj->runID = 0;
5231      // ------------------------------------        TTimeStamp *dt = new TTimeStamp();
5232      // add branch with dead and live times        proc_obj->date = dt->AsString();
5233      // ------------------------------------        delete dt;
5234      run_tree_clone->Branch("DeadLiveTime",totdltime,"dltime[3]/l");        proc_obj->commandLine = Form("PamelaLevel2 was called");
5235      cout << "Run          : branch DeadLiveTime"<<endl;        proc_obj->outputFilename = "";
5236          proc_obj->localDir = gSystem->WorkingDirectory();
5237          proc_obj->uname = gSystem->GetFromPipe("uname -a");
5238      sel_tree_clone = new TTree("SelectionList","List of selected events ");        proc_obj->DB = Form("mysql://%s/%s",dbc->GetHost(),dbc->GetDB());
5239      sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");        proc_tree_clone->Fill();
5240      sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/L");      }
5241            cout << "----------------------------------------------------" << endl;
5242      }
5243      // ------------------------------------
5244      // add branch with dead and live times
5245      // ------------------------------------
5246      if (SELLI != 2) { // EMILIANO
5247        run_tree_clone->Branch("DeadLiveTime", totdltime, "dltime[3]/l");
5248        cout << "Run          : branch DeadLiveTime" << endl;
5249    
5250        sel_tree_clone = new TTree("SelectionList", "List of selected events ");
5251        //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
5252        sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW
5253        sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");
5254        //    if ( hasL0EE ) sel_tree_clone->Branch("L0EventEntry", &il0entry, "l0eventry/L");
5255      };
5256    
5257      Int_t i=0;    Int_t i = 0;
5258      if(TRK1||TRK2||TRKh){    if (TRK1 || TRK2 || TRKh) {
5259          pam_tree_clone[i] = new TTree("Tracker","PAMELA tracker level2 data ");      pam_tree_clone[i] = new TTree("Tracker", "PAMELA tracker level2 data ");
5260          if(TRK1) {      if (TRK1) {
5261              pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));        pam_tree_clone[i]->Branch("TrkLevel1", "TrkLevel1", GetPointerTo("TrkLevel1"));
5262              pam_tree_clone[i]->BranchRef();        pam_tree_clone[i]->BranchRef();
5263              cout << "Tracker      : branch TrkLevel1"<<endl;        cout << "Tracker      : branch TrkLevel1" << endl;
5264  //          cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;        //            cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;
5265          };      };
5266          if(TRK2) {      if (TRK2) {
5267              pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));        pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2", GetPointerTo("TrkLevel2"));
5268              cout << "Tracker      : branch TrkLevel2"<<endl;                cout << "Tracker      : branch TrkLevel2" << endl;
5269          };      };
5270          if(TRKh) {      if (TRKh) {
5271              pam_tree_clone[i]->Branch("TrkHough","TrkHough", GetPointerTo("TrkHough"));        pam_tree_clone[i]->Branch("TrkHough", "TrkHough", GetPointerTo("TrkHough"));
5272              cout << "Tracker      : branch TrkHough"<<endl;        cout << "Tracker      : branch TrkHough" << endl;
5273          };      };
5274          i++;      if(NUC){
5275      }          pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&trk_nuc_obj);
5276            cout << "Tracker      : branch TrackNuclei" << endl;    
5277      // Calorimeter      }
5278      if(CAL1||CAL2){      if(EXT){
5279          pam_tree_clone[i] = new TTree("Calorimeter","PAMELA calorimeter level2 data ");          pam_tree_clone[i]->Branch("extAlgFlag",&extAlgFlag,"extAlgFlag/I");
5280          if(CAL1) {          pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&trk_ext_obj);
5281              pam_tree_clone[i]->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));          cout << "Tracker      : branch RecoveredTrack" << endl;
5282              cout << "Calorimeter  : branch CaloLevel1"<<endl;          if(NUC){
5283          };              pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&trk_ext_nuc_obj);
5284          if(CAL2) {              cout << "Tracker      : branch RecoveredTrackNuclei" << endl;                  
5285              pam_tree_clone[i]->Branch("CaloLevel2","CaloLevel2", GetPointerTo("CaloLevel2"));          }
5286              cout << "Calorimeter  : branch CaloLevel2"<<endl;      }
5287          };  
5288          i++;      i++;
5289      }        }
5290    
5291      // ToF        // Calorimeter
5292      if(TOF) {    if (CAL1 || CAL2) {
5293          pam_tree_clone[i] = new TTree("ToF","PAMELA ToF level2 data ");      pam_tree_clone[i] = new TTree("Calorimeter", "PAMELA calorimeter level2 data ");
5294          pam_tree_clone[i]->Branch("ToFLevel2","ToFLevel2", GetPointerTo("ToFLevel2"));      if (CAL1) {
5295          cout << "ToF          : branch ToFLevel2"<<endl;        pam_tree_clone[i]->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));
5296          i++;        cout << "Calorimeter  : branch CaloLevel1" << endl;
5297      };      };
5298      // Trigger      if (CAL2) {
5299      if(TRG) {        pam_tree_clone[i]->Branch("CaloLevel2", "CaloLevel2", GetPointerTo("CaloLevel2"));
5300          pam_tree_clone[i] = new TTree("Trigger","PAMELA trigger level2 data ");        cout << "Calorimeter  : branch CaloLevel2" << endl;
5301          pam_tree_clone[i]->Branch("TrigLevel2","TrigLevel2", GetPointerTo("TrigLevel2"));      };
5302          cout << "Trigger      : branch TrigLevel2"<<endl;      if(NUC){
5303          i++;          pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&calo_nuc_obj);
5304      };          cout << "Calorimeter  : branch TrackNuclei" << endl;    
5305      // S4      }
5306      if(S4) {      if(EXT){
5307          pam_tree_clone[i] = new TTree("S4","PAMELA S4 level2 data ");            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&calo_ext_obj);
5308          pam_tree_clone[i]->Branch("S4Level2","S4Level2", GetPointerTo("S4Level2"));          cout << "Calorimeter  : branch RecoveredTrack" << endl;
5309          cout << "S4           : branch S4Level2"<<endl;          if(NUC){
5310          i++;              pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&calo_ext_nuc_obj);
5311      };              cout << "Calorimeter  : branch RecoveredTrackNuclei" << endl;                  
5312      // Neutron Detector          }
5313      if(ND) {      }
5314          pam_tree_clone[i] = new TTree("NeutronD","PAMELA neutron detector level2 data ");            i++;
5315          pam_tree_clone[i]->Branch("NDLevel2","NDLevel2", GetPointerTo("NDLevel2"));    }
5316          cout << "NeutronD     : branch NDLevel2"<<endl;  
5317          i++;    // ToF
5318      };    if (TOF) {
5319      // Anticounters      pam_tree_clone[i] = new TTree("ToF", "PAMELA ToF level2 data ");
5320      if(AC) {      pam_tree_clone[i]->Branch("ToFLevel2", "ToFLevel2", GetPointerTo("ToFLevel2"));
5321          pam_tree_clone[i] = new TTree("Anticounter","PAMELA anticounter detector level2 data ");              cout << "ToF          : branch ToFLevel2" << endl;
5322          pam_tree_clone[i]->Branch("AcLevel2","AcLevel2", GetPointerTo("AcLevel2"));      if(NUC){
5323          cout << "Anticounter  : branch AcLevel2"<<endl;          pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&tof_nuc_obj);
5324          i++;          cout << "ToF          : branch TrackNuclei" << endl;    
5325      };      }
5326      // OrbitalInfo      if(EXT){
5327      if(ORB) {          pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&tof_ext_obj);
5328          pam_tree_clone[i] = new TTree("OrbitalInfo","PAMELA oribital info  ");            cout << "ToF          : branch RecoveredTrack" << endl;
5329          pam_tree_clone[i]->Branch("OrbitalInfo","OrbitalInfo", GetPointerTo("OrbitalInfo"));          if(NUC){
5330          cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;              pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&tof_ext_nuc_obj);
5331          i++;              cout << "ToF          : branch RecoveredTrackNuclei" << endl;                  
5332      };          }
5333      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      }
5334        i++;
5335      };
5336      // Trigger
5337      if (TRG) {
5338        pam_tree_clone[i] = new TTree("Trigger", "PAMELA trigger level2 data ");
5339        pam_tree_clone[i]->Branch("TrigLevel2", "TrigLevel2", GetPointerTo("TrigLevel2"));
5340        cout << "Trigger      : branch TrigLevel2" << endl;
5341        i++;
5342      };
5343      // S4
5344      if (S4) {
5345        pam_tree_clone[i] = new TTree("S4", "PAMELA S4 level2 data ");
5346        pam_tree_clone[i]->Branch("S4Level2", "S4Level2", GetPointerTo("S4Level2"));
5347        cout << "S4           : branch S4Level2" << endl;
5348        i++;
5349      };
5350      // Neutron Detector
5351      if (ND) {
5352        pam_tree_clone[i] = new TTree("NeutronD", "PAMELA neutron detector level2 data ");
5353        pam_tree_clone[i]->Branch("NDLevel2", "NDLevel2", GetPointerTo("NDLevel2"));
5354        cout << "NeutronD     : branch NDLevel2" << endl;
5355        i++;
5356      };
5357      // Anticounters
5358      if (AC) {
5359        pam_tree_clone[i] = new TTree("Anticounter", "PAMELA anticounter detector level2 data ");
5360        pam_tree_clone[i]->Branch("AcLevel2", "AcLevel2", GetPointerTo("AcLevel2"));
5361        cout << "Anticounter  : branch AcLevel2" << endl;
5362        i++;
5363      };
5364      // OrbitalInfo
5365      if (ORB) {
5366        pam_tree_clone[i] = new TTree("OrbitalInfo", "PAMELA orbital info  ");
5367        pam_tree_clone[i]->Branch("OrbitalInfo", "OrbitalInfo", GetPointerTo("OrbitalInfo"));
5368        cout << "OrbitalInfo  : branch OrbitalInfo" << endl;
5369        if(NUC){
5370            pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&orb_nuc_obj);
5371            cout << "OrbitalInfo  : branch TrackNuclei" << endl;    
5372        }
5373        if(EXT){
5374            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&orb_ext_obj);
5375            cout << "OrbitalInfo  : branch RecoveredTrack" << endl;
5376            if(NUC){
5377                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&orb_ext_nuc_obj);
5378                cout << "OrbitalInfo  : branch RecoveredTrackNuclei" << endl;                  
5379            }
5380        }
5381        i++;
5382      };
5383      // GPamela
5384      if (GP) {
5385        pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");
5386        pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split
5387        cout << "GPamela  : branch GPamela" << endl;
5388        i++;
5389      };
5390    
 }  
5391    
5392      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5393    
5394    }
5395    
5396  /**  /**
5397   * Fill tree (created with CreateCloneTrees)   * Fill tree (created with CreateCloneTrees)
5398   *   *
5399   */   */
5400  //void PamLevel2::FillNewPamTree(TTree *T){  //void PamLevel2::FillNewPamTree(TTree *T){
5401  void PamLevel2::FillCloneTrees(){  void PamLevel2::FillCloneTrees() {
       
 //    cout << "PamLevel2::FillCloneTrees()" << irunentry << endl;  
5402    
5403      for(Int_t i=0; i<NCLONES; i++){    //    cout << "PamLevel2::FillCloneTrees()" << irunentry << endl;
         if(pam_tree_clone[i])pam_tree_clone[i]->Fill();  
     }  
     if(sel_tree_clone)sel_tree_clone->Fill();      
5404    
5405  }    for (Int_t i = 0; i < NCLONES; i++) {
5406        if (pam_tree_clone[i])
5407          pam_tree_clone[i]->Fill();
5408      }
5409      if (sel_tree_clone)
5410        sel_tree_clone->Fill();
5411    
5412    }
5413    
5414  TTree* PamLevel2::GetCloneTree(TString name){  TTree* PamLevel2::GetCloneTree(TString name) {
5415    
5416      for(Int_t i=0; i<NCLONES; i++){        for (Int_t i = 0; i < NCLONES; i++) {
5417          if(pam_tree_clone[i]){      if (pam_tree_clone[i]) {
5418              TString na = pam_tree_clone[i]->GetName();        TString na = pam_tree_clone[i]->GetName();
5419              if(!name.CompareTo(na))return pam_tree_clone[i];        if (!name.CompareTo(na))
5420          };          return pam_tree_clone[i];
5421      }      };
5422      if(run_tree_clone){    }
5423          TString na = run_tree_clone->GetName();    if (run_tree_clone) {
5424          if(!name.CompareTo(na))return run_tree_clone;      TString na = run_tree_clone->GetName();
5425      }      if (!name.CompareTo(na))
5426      if(sel_tree_clone){        return run_tree_clone;
5427          TString na = sel_tree_clone->GetName();    }
5428          if(!name.CompareTo(na))return sel_tree_clone;    if (sel_tree_clone) {
5429      }      TString na = sel_tree_clone->GetName();
5430      return NULL;      if (!name.CompareTo(na))
5431          return sel_tree_clone;
5432      }
5433      if (proc_tree_clone && PROC) {
5434        TString na = proc_tree_clone->GetName();
5435        if (!name.CompareTo(na))
5436          return proc_tree_clone;
5437      }
5438      return NULL;
5439    
5440  }  }
5441  void PamLevel2::WriteCloneTrees(){  void PamLevel2::WriteCloneTrees() {
5442      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5443      cout << "Write clones of PAMELA trees "<<endl;    cout << "Write clones of PAMELA trees " << endl;
5444      cout << run_tree_clone->GetName()<<endl;      cout << run_tree_clone->GetName() << endl;
5445      if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())    if (SELLI != 2) {// Emiliano
5446          run_tree_clone->GetBranch("DeadLiveTime")->Fill();      if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
5447      run_tree_clone->Write();        run_tree_clone->GetBranch("DeadLiveTime")->Fill();
5448      cout << sel_tree_clone->GetName()<<endl;        };
5449      run_tree_clone->Write();
5450      if (SELLI != 2) { //Emiliano
5451        cout << sel_tree_clone->GetName() << endl;
5452      sel_tree_clone->Write();      sel_tree_clone->Write();
5453      for(Int_t i=0; i<NCLONES; i++){        };
5454          if(pam_tree_clone[i]){    for (Int_t i = 0; i < NCLONES; i++) {
5455              cout << pam_tree_clone[i]->GetName()<<endl;      if (pam_tree_clone[i]) {
5456              pam_tree_clone[i]->Write();        cout << pam_tree_clone[i]->GetName() << endl;
5457          };        pam_tree_clone[i]->Write(pam_tree_clone[i]->GetName(),TObject::kOverwrite);
5458      }      };
5459      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;    }
5460      
5461      if ( PROC ){
5462        proc_tree_clone->Write("ProcessingInfo",TObject::kOverwrite);
5463      }
5464      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5465    
5466  }  }
5467    
5468  /**  /**
5469   * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.   * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.
5470   */   */
5471  //Int_t PamLevel2::GetEntry(Int_t iee){  Int_t PamLevel2::GetEntry(Long64_t iee) {
 Int_t PamLevel2::GetEntry(Long64_t iee){  
       
 //     cout << "-------------------------------------"<<endl;  
 //     cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;  
   
     if(!pam_tree){  
         cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded"<<endl;  
         return 0;  
     }  
5472    
5473      if (!pam_tree) {
5474        cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded" << endl;
5475        return 0;
5476      }
5477    
5478      //    //
5479      // 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...    // 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...
5480      //    //
5481      //    if(!run_tree ){    //    if(!run_tree ){
5482      //  cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loeaded"<<endl;    //    cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loeaded"<<endl;
5483      //  return 0;    //    return 0;
5484      //    }    //    }
5485    
5486      Long64_t ii=0;    //-------------------------------
5487      //-------------------------------    if (!pam_tree->GetEntry(iee)) {
5488      ii = iee;      cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree" << endl;
5489      if( !pam_tree->GetEntry(ii) ){            return 0;
5490          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree"<<endl;    }
5491          return 0;    //
5492      // ... 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.
5493      // 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
5494      // a problem if you don't check the return code of getentry.
5495      //
5496      if (!RUN || !run_tree) {
5497        if (TRK0 || CAL0 || TOF0 || RUN) { //forse cosi` va bene per tornare 1?
5498          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded" << endl;
5499          return 0;
5500      }      }
5501      //      else {
5502      // ... 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.        return 1; //cosi` se non c'e` run esce qua...
     // 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  
     // a problem if you don't check the return code of getentry.  
     //  
     if(!run_tree ){  
         if ( TRK0 || CAL0 || TOF0 || RUN ) { //forse cosi` va bene per tornare 1?  
             cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded"<<endl;  
             return 0;  
         }  else {  
             return 1; //cosi` se non c'e` run esce qua...  
         }  
5503      }      }
5504      }
5505    
5506      //-------------------------------    //-------------------------------
5507      ii = iee;    //
5508  //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);    if ( fUpdateRunInfo ) UpdateRunInfo(iee); // Emiliano
5509  //    Bool_t UPDATED = UpdateRunInfo(ii);    if (SELLI == 0 || SELLI == 2) irunentry = iee - runfirstentry;
     UpdateRunInfo(ii);  
     if(SELLI==0)irunentry = iee-runfirstentry;  
 //    if(UPDATED && run_tree_clone)run_tree_clone->Fill();  
     
 //    cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;  
   
 //     cout << "irunentry     "<<irunentry << endl;  
 //     cout << "runfirstentry "<<runfirstentry << endl;  
 //     cout << "nevents       "<<GetRunInfo()->NEVENTS<< endl;  
   
 //     if( TRK0 || CAL0 || TOF0 ){  
 //      if( !GetYodaEntry( ) ){  
 //          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading level0 tree"<<endl;  
 //          return 0;  
 //      }  
 //     }  
   
   
     return 1;  
5510    
5511  }    return 1;
5512    
5513  TrkLevel0    *PamLevel2::GetTrkLevel0(){  }
     if( !TRK0 )return NULL;  
     if( !GetYodaEntry( ) ){  
         cout << " Int_t PamLevel2::GetTrkLevel0() -- ERROR -- error reading level0 tree"<<endl;  
         return 0;  
     }  
     return trk0_obj;  
 };  
 CaloLevel0    *PamLevel2::GetCaloLevel0(){  
     if( !CAL0 )return NULL;  
     if( !GetYodaEntry( ) ){  
         cout << " Int_t PamLevel2::GetCaloLevel0() -- ERROR -- error reading level0 tree"<<endl;  
         return 0;  
     }  
     return calo0_obj;  
 };  
5514    
5515    TrkLevel0 *PamLevel2::GetTrkLevel0() {
5516      if (!TRK0)
5517        return NULL;
5518      if (!GetYodaEntry()) {
5519        cout << " Int_t PamLevel2::GetTrkLevel0() -- ERROR -- error reading level0 tree" << endl;
5520        return 0;
5521      }
5522      return trk0_obj;
5523    }
5524    ;
5525    CaloLevel0 *PamLevel2::GetCaloLevel0() {
5526      if (!CAL0)
5527        return NULL;
5528      if (!GetYodaEntry()) {
5529        cout << " Int_t PamLevel2::GetCaloLevel0() -- ERROR -- error reading level0 tree" << endl;
5530        return 0;
5531      }
5532      return calo0_obj;
5533    }
5534    ;
5535    
5536  /**  /**
5537   * Method to retrieve the level0 tree (YODA tree) that contains the current event.   * Method to retrieve the level0 tree (YODA tree) that contains the current event.
# Line 3264  CaloLevel0    *PamLevel2::GetCaloLevel0( Line 5539  CaloLevel0    *PamLevel2::GetCaloLevel0(
5539   * @return Pointer to the tree   * @return Pointer to the tree
5540   */   */
5541    
5542    TTree* PamLevel2::GetYodaTree() {
5543    
5544  TTree* PamLevel2::GetYodaTree( ){    //    cout << "TTree* PamLevel2::GetYodaTree( )"<<endl;
5545          //===================================
5546  //    cout << "TTree* PamLevel2::GetYodaTree( )"<<endl;    // check if iroot has changed
5547      //===================================    //===================================
5548      // check if iroot has changed    if (irun < 0) {
5549      //===================================      cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;
5550      if( irun<0 ){      if (DBG) cout << "In order to use this method you have to first load the RunInfo tree "<<endl;
5551          cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = "<<irun<<endl;      return NULL;
5552  //      cout << "In order to use this method you have to first load the RunInfo tree "<<endl;    }
5553          return NULL;    Int_t irootnew = GetRunInfo()->ID_ROOT_L0;
5554      }        if (DBG){
5555      Int_t irootnew = run_obj->ID_ROOT_L0;      cout << "iroot    "<<iroot<<endl;
5556  //      cout << "iroot    "<<iroot<<endl;      cout << "irootnew "<<irootnew<<endl;
5557  //      cout << "irootnew "<<irootnew<<endl;    }
5558    
5559      //===================================
5560      // load the level0 file
5561      // (if not already loaded)
5562      //===================================
5563      if (iroot != irootnew || !l0_tree) {
5564        iroot = irootnew;
5565      //===================================      //===================================
5566      // load the level0 file      // open the DB connection
5567      // (if not already loaded)      // (if not already opened)
5568      //===================================      //===================================
5569      if( iroot != irootnew || !l0_tree){      if (!dbc || (dbc && !dbc->IsConnected()))
5570          iroot = irootnew;        SetDBConnection();
5571          //===================================      GL_ROOT glroot = GL_ROOT();
5572          // open the DB connection      if (glroot.Query_GL_ROOT(iroot, dbc)) {
5573          // (if not already opened)        cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = " << iroot << " does not exists"
5574          //===================================            << endl;
5575          if(!dbc || (dbc && !dbc->IsConnected())){        return NULL;
5576              cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;      };
5577              cout<<"Connecting to DB"<<endl;      TString filename = glroot.PATH + glroot.NAME;
5578              cout<<"HOST "<<host<<endl;      if (l0_file) {
5579              cout<<"USER "<<user<<endl;        l0_file->Close();
5580              cout<<"PSW  "<<psw<<endl;        l0_file->Delete();
5581              dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());      }
5582              if( !dbc )return NULL;      cout << "Opening LEVEL0 file: " << filename << endl;
5583              if( !dbc->IsConnected() )return NULL;          FileStat_t t;
5584  //          cout<<"...done"<<endl;      if (gSystem->GetPathInfo(filename.Data(), t)) {
5585              cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;        cout << " PamLevel2::GetYodaTree() -- ERROR opening file " << endl;
5586          }else{        return NULL;
5587  //          cout<<"DB already connected"<<endl;      }
5588          }      l0_file = new TFile(filename);
5589          GL_ROOT glroot = GL_ROOT();      if (!l0_file)
5590          if( glroot.Query_GL_ROOT(iroot,dbc) ){        return NULL;
5591              cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = "<<iroot<< " does not exists"<<endl;      l0_tree = (TTree*) l0_file->Get("Physics");
5592              return NULL;      if (!h0_obj)
5593          };        h0_obj = new EventHeader();
5594          TString filename = glroot.PATH + glroot.NAME;      l0_tree->SetBranchAddress("Header", &h0_obj);
5595          if(l0_file){      yprevshift = 0; // yes, yprevshift is the shift in the level0, prevshift is the shift in the level2
5596              l0_file->Close();      //---------------------------------------------------
5597              l0_file->Delete();      // TRACKER:
5598          }      if (TRK0) {
5599          cout << "Opening LEVEL0 file: "<< filename << endl;        if (!trk0_obj) {
5600          FileStat_t t;          trk0_obj = new TrkLevel0();
5601          if( gSystem->GetPathInfo(filename.Data(),t) ){          trk0_obj->Set();
5602              cout << " PamLevel2::GetYodaTree() -- ERROR opening file "<<endl;        };
5603              return NULL;        l0_tree->SetBranchAddress("Tracker", trk0_obj->GetPointerToTrackerEvent());
         }  
         l0_file = new TFile(filename);  
         if( !l0_file )return NULL;  
         l0_tree = (TTree*)l0_file->Get("Physics");  
         if(!h0_obj)h0_obj = new EventHeader();  
         l0_tree->SetBranchAddress("Header" ,&h0_obj);  
         prevshift = 0;  
         //---------------------------------------------------  
         // TRACKER:  
         if(TRK0){  
             if(!trk0_obj){  
                 trk0_obj = new TrkLevel0();  
                 trk0_obj->Set();  
             };  
             l0_tree->SetBranchAddress("Tracker" ,trk0_obj->GetPointerToTrackerEvent());  
         }  
         //--------------------------------------------------  
         // CALORIMETER:  
         if(CAL0){  
             if(!calo0_obj){  
                 calo0_obj = new CaloLevel0();  
                 calo0_obj->Set();  
             };  
             l0_tree->SetBranchAddress("Calorimeter" ,calo0_obj->GetPointerToCalorimeterEvent());  
             //      cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;  
         }  
         //---------------------------------------------------  
         // TOF:  
         if(TOF0){  
             cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented "<<endl;  
         }  
   
     };  
   
     if(!dbc || (dbc && !dbc->IsConnected())){  
         cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;  
5604      }      }
5605        //--------------------------------------------------
5606      if ( TRK0 ){      // CALORIMETER:
5607          TrkParams::Load(6);      if (CAL0) {
5608          if( !TrkParams::IsLoaded(6) ){        if (!calo0_obj) {
5609              cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- VK-mask not loaded"<<endl;          calo0_obj = new CaloLevel0();
5610          };          calo0_obj->Set();
5611          TrkParams::SetCalib(run_obj,dbc);        };
5612          TrkParams::LoadCalib( );        l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());
5613          if( !TrkParams::CalibIsLoaded() ){      }
5614              cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded"<<endl;      //---------------------------------------------------
5615          };      // TOF:
5616        if (TOF0) {
5617          cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented " << endl;
5618      }      }
5619    
5620  //    cout << l0_tree << endl;      dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
5621            delete dbc;
5622      return l0_tree;      dbc=0;
5623    
5624      };
5625    
5626      if (TRK0) {
5627        if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection();
5628        TrkParams::SetCalib(run_obj, dbc);
5629        TrkParams::LoadCalib();
5630        if (!TrkParams::CalibIsLoaded()) {
5631          cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded" << endl;
5632        };
5633        if(dbc){
5634          dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
5635          delete dbc;
5636          dbc=0;
5637        };
5638      }
5639      return l0_tree;
5640  }  }
5641    
5642  /**  /**
5643   * Method to retrieve the level0 tree (YODA tree) that contains the current event.   * Method to retrieve the level0 tree (YODA tree) that contains the current event.
5644   */   */
5645  Int_t PamLevel2::GetYodaEntry(){  Int_t PamLevel2::GetYodaEntry() {
5646    
5647  //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;    Long64_t iev = this->GetReadEntry();
5648      if(!GetYodaTree())return 0;  
5649          // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
5650      // patch    // if it is a full file (not preselected)
5651      if( irunentry < 0){    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
5652  //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;    //  if (SELLI == 0 || SELLI == 2 || !hasL0EE) {
5653          irunentry=0LL;  
5654        if (!GetYodaTree()){
5655          printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
5656          return 0;
5657        }
5658    
5659        if (irunentry < 0) {
5660          if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
5661          //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
5662          irunentry = 0LL;
5663      }      }
5664      //  ---------------------------------      //  ---------------------------------
5665      //  if file is NOT a preselected file      //  if file is NOT a preselected file
5666      //  ---------------------------------      //  ---------------------------------
5667      Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM);      Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
5668  //     cout << " irunentry "<<irunentry<<endl;  
5669  //     cout << " EV_FROM "<<run_obj->EV_FROM<<endl;      if (DBG){
5670  //     cout << " quellagiusta = irunentry + EV_FROM "<< quellagiusta << endl;        cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
5671          cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
5672  //     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;        cout << " time "<< abstime << endl;
5673  //     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;      }
5674  //     cout << " time "<< abstime << endl;  
5675  //     cout << " trk_calib_used "<<run_obj->TRK_CALIB_USED<< endl;      ULong64_t obt = 0;
5676            ULong64_t pktn = 0;
5677      ULong64_t obt     = 0;      if (GetOrbitalInfo()) {
5678      ULong64_t pktn    = 0;        obt = GetOrbitalInfo()->OBT;
5679      if( GetOrbitalInfo() ){        pktn = GetOrbitalInfo()->pkt_num;
         obt     = GetOrbitalInfo()->OBT;  
         pktn    = GetOrbitalInfo()->pkt_num;  
5680      }      }
5681    
5682      if( !GetOrbitalInfo() && !ISGP){      if (!GetOrbitalInfo() && !ISGP) {
5683          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo "<<endl;        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;
5684          return 0;        return 0;
5685      }      }
5686      if( obt==0 && pktn==0 && !ISGP ){      if (obt == 0 && pktn == 0 && !ISGP) {
5687          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? "<<endl;        cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;
5688          return 0;        return 0;
5689      }      }
5690    
5691      // ---------------------------------------------------------------------      // ---------------------------------------------------------------------
# Line 3428  Int_t PamLevel2::GetYodaEntry(){ Line 5699  Int_t PamLevel2::GetYodaEntry(){
5699      // of the OBT and pkt-number. In case of mismatch, the level0 entry number      // of the OBT and pkt-number. In case of mismatch, the level0 entry number
5700      // is shift forward until when the packets match.      // is shift forward until when the packets match.
5701      // ---------------------------------------------------------------------      // ---------------------------------------------------------------------
5702      Int_t answer = 0;      Long64_t shift = 0LL;
5703      Int_t shift =0;      Long64_t answer = quellagiusta + shift + yprevshift;
5704      //    printf(" siamo qui %i %i \n",shift,prevshift);      Int_t readl0 = 0;
5705      Int_t maxshift = 10;      readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
     do{  
         if(shift>0){      
             cout << " PKTNUM  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;  
             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;  
             cout << "         L2 <--> L0 mismatch ( irun "<<irun<<" irunentry "<<irunentry<<" shift "<<shift<<" prevshift "<<prevshift<<" )"<<endl;  
         }  
         answer = l0_tree->GetEntry(quellagiusta+(Long64_t)shift+(Long64_t)prevshift);  
         shift++;  
         if( !GetEventHeader() ){  
             cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader "<<endl;  
             return 0;  
         }  
5706    
5707          if(ISGP){      if (DBG){
5708              obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO        printf(" siamo qui shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5709              pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO      }
         }  
5710    
5711  //      cout << "PKTNUM "<<shift<<" ==  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;  
5712  //      cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;      if (ISGP) {
5713  //      if( (quellagiusta+shift) == l0_tree->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;        obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
5714  //      cout << " obt "<< obt << endl;        pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
5715  //      cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;      }
5716  //      cout << " pktn "<< pktn << endl;  
5717  //      cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;      while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){
5718  //      printf(" IDRUN %u \n",GetRunInfo()->ID);        if ( isSync && shift == 0LL ){
5719  //          printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found in place!!! \n");
5720          if ( prevshift != 0 && (quellagiusta+(Long64_t)shift) == GetYodaTree()->GetEntries() ){          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5721            prevshift = 0;          cout << "\nFor bug reporting instructions, please see for example:\n";
5722            shift = -1;          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5723          };          cout << "  " << endl;
5724          }
5725      }while( ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) ||        if (shift > 0) {
5726                pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())       )          if (DBG) cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
5727              && (quellagiusta+(Long64_t)shift) < GetYodaTree()->GetEntries() && shift < maxshift);          if (DBG)
5728              cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
5729      if ( (quellagiusta+(Long64_t)shift+(Long64_t)prevshift) > GetYodaTree()->GetEntries() || shift == maxshift ) {                 << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
5730          cout << " Big trouble here, no such event in Level0 data! " <<endl;          if (DBG)
5731          return 0;            cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
5732      }                 << " prevshift " << prevshift << " )" << endl;
5733  //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;        }
5734  //    return GetYodaTree()->GetEntry(quellagiusta);        answer = quellagiusta +  shift+ yprevshift;
5735      if ( shift > 1 ) prevshift += (shift-1);        readl0 = l0_tree->GetEntry(answer);
5736              //      printf(" inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5737      return answer;  
5738          if (!GetEventHeader()) {
5739            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
5740            return 0;
5741          }
5742    
5743          //
5744          if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
5745            if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5746            //        printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5747            yprevshift = 0LL;
5748            shift = -1LL;
5749          };
5750            
5751          shift++;
5752        }
5753                                      
5754    
5755        if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
5756          if ( isSync ){
5757            printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found AT ALL!!! \n");
5758            cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5759            cout << "\nFor bug reporting instructions, please see for example:\n";
5760            cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5761            cout << "  " << endl;
5762          }
5763          cout << "Int_t PamLevel2::GetYodaEntry() -- WARNING -- " << endl;
5764          cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;    
5765          cout << " Nested and/or DarthVader skipped packets in fragmented run? checking and trying to fix " <<endl;
5766          // query the DB for runs containing the event, loop over LEVEL0 files which could contain the level0 event and try to find it
5767          // ma nel mezzo del cammin per ogni run che pesco devo vedere la posizione relativa di iev rispetto al runheader nel livello2 per andare a cercare nel posto giusto
5768          // connect to db
5769          if (!dbc || (dbc && !dbc->IsConnected())) SetDBConnection(); //Emiliano
5770          //
5771          if (GetOrbitalInfo()){
5772            abstime = GetOrbitalInfo()->absTime;
5773          } else {
5774            printf(" PamLevel2::GetYodaEntry() ERROR! no OrbitalInfo, cannot get the absolute time for event \n");
5775            return 0;
5776          }
5777          // query DB looking for runs containing the processed event
5778          TSQLResult *pResult;
5779          TSQLRow *Row = NULL;
5780          TString myquery = Form("select ID,NEVENTS from GL_RUN where RUNHEADER_TIME<=%lld and RUNTRAILER_TIME>=%lld;",abstime,abstime);
5781          if ( DBG ) printf(" query is %s \n",myquery.Data());
5782          //      printf(" query is %s \n",myquery.Data());// TOGLITOGLI
5783          pResult = dbc->Query(myquery.Data());    
5784          if (!pResult->GetRowCount()){
5785            printf(" PamLevel2::GetYodaEntry() ERROR! event is not included in any run!!! \n");
5786            cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5787            cout << "\nFor bug reporting instructions, please see for example:\n";
5788            cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5789            cout << "  " << endl;
5790            return 0;
5791          }
5792          if ( pResult->GetRowCount() == 1 ){
5793            if (DBG) printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");
5794            //        printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");//TOGLITOGLI
5795          }
5796          for( Int_t ru=0; ru < pResult->GetRowCount(); ru++){ // loop over runs containing the event
5797            if (Row) delete Row;
5798            Row = pResult->Next();  
5799            if( Row == NULL ) break;
5800            UInt_t idrun = (UInt_t)atoll(Row->GetField(0));
5801            UInt_t nev = (UInt_t)atoll(Row->GetField(1));
5802            if (DBG) printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);
5803            //        printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);//TOGLITOGLI
5804    
5805            // now look for this run in the level2 file, it must be present! code is taken from updateruninfo of course
5806            Bool_t rfound = false;
5807            totrunentry = 0LL;
5808            runfirstentry = 0LL;
5809            for (Int_t r=0; r< run_tree->GetEntries();r++){
5810              run_tree->GetEntry(r);//update runinfo
5811              if ( r > 0 ){
5812                totrunentrymin = totrunentrymax+1;
5813              } else {
5814                totrunentrymin = 0LL;
5815              }
5816              totrunentry += GetRunInfo()->NEVENTS;
5817              totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
5818              irun = r;        
5819    
5820              if (idrun == GetRunInfo()->ID){
5821                if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
5822                  if (DBG) printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
5823                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
5824                  //              printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
5825                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
5826                  prevshift += (totrunentrymin-iev); // add the new shift to total shift
5827                  totrunentrymin -= (totrunentrymin-iev); // shift run position min
5828                  totrunentrymax -= (totrunentrymin-iev); // shift run position max
5829                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
5830                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
5831                }
5832                runfirstentry = totrunentrymin; // first entry of the run in the level2
5833              
5834    
5835                //
5836                if (gltsync)
5837                  delete gltsync; // Emiliano
5838                if (!dbc || (dbc && !dbc->IsConnected()))
5839                  SetDBConnection(); //Emiliano
5840                gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
5841                if (dbc){
5842                  dbc->Close(); // Emiliano
5843                  delete dbc;
5844                  dbc=0;
5845                }          
5846                if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
5847                  cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
5848                       << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
5849                  cout
5850                    << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
5851                    << endl;
5852                }
5853                //
5854                if (DBG) printf(" found \n");
5855                //            printf(" found \n");//TOGLITOGLI
5856                rfound = true;
5857                //
5858                break;
5859              }
5860            } // loop over run
5861            if ( !rfound ){
5862              printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level2 file!!! \n");
5863              cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5864              cout << "\nFor bug reporting instructions, please see for example:\n";
5865              cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5866              cout << "  " << endl;        
5867              return 0;
5868            }
5869    
5870            // here we got the first run and we can check if it contains the level0 event
5871            if (!GetYodaTree()){
5872              printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
5873              return 0;
5874            }      
5875    
5876            // get the current run entry
5877            irunentry = iev - runfirstentry;
5878            if (irunentry < 0) {
5879              if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
5880              //          cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
5881              irunentry = 0LL;
5882            }
5883            //  ---------------------------------
5884            //  if file is NOT a preselected file
5885            //  ---------------------------------
5886            quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
5887          
5888            if (DBG){
5889              cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
5890              cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
5891              cout << " time "<< abstime << endl;
5892            }
5893            //        cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
5894            //        cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
5895            //        cout << " time "<< abstime << endl; // TOGLITOGLI
5896          
5897            shift = 0;
5898            answer = quellagiusta + shift + yprevshift; // prevshift already included in irunentry
5899            readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
5900    
5901            if (DBG){
5902              printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5903            }
5904            //        printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5905          
5906            while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){
5907              if (shift > 0) {
5908                if (DBG) cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
5909                if (DBG)
5910                  cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
5911                       << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
5912                if (DBG)
5913                  cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
5914                       << " prevshift " << prevshift << " )" << endl;
5915              }
5916              answer = quellagiusta +  shift+ yprevshift;
5917              readl0 = l0_tree->GetEntry(answer);
5918              //          printf(" inside inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5919            
5920              if (!GetEventHeader()) {
5921                cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
5922                return 0;
5923              }
5924              //
5925              if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
5926                if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5927                //            printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5928                yprevshift = 0;
5929                shift = -1;
5930              };
5931            
5932              shift++;
5933            }
5934          
5935            if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
5936              //still not the good run... continue with the nex one!
5937              printf("still not the good run... continue with the nex one!\n");
5938            } else {
5939              if (DBG) cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
5940              //          cout << "LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;//TOGLITOGLI
5941              if (shift > 1) yprevshift = (shift - 1);
5942              if (Row) delete Row;
5943              delete pResult;  
5944              if (dbc){
5945                dbc->Close(); // Emiliano
5946                delete dbc;
5947                dbc=0;
5948              }    
5949              il0entry = answer;
5950              return readl0;
5951            }
5952            // perhaps it is all
5953          }// loop over runs containing the event
5954          if (Row) delete Row;
5955          delete pResult;  
5956          if (dbc){
5957            dbc->Close(); // Emiliano
5958            delete dbc;
5959            dbc=0;
5960          }          
5961          // arriving here it means no run found, cannot be! error!
5962          printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level0 files!!! \n");
5963          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5964          cout << "\nFor bug reporting instructions, please see for example:\n";
5965          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5966          cout << "  " << endl;        
5967          return 0;
5968        } else {
5969          if (DBG) cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
5970          //    cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
5971          //    printf("obt %lld (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) %i  pktn %lld (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter() %i \n",obt,(UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()), pktn, (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) );
5972          if (shift > 1) yprevshift = (shift - 1);
5973          il0entry = answer;
5974          return readl0;
5975        }
5976    
5977        /*  } // if selli 0 || 2
5978      if ( SELLI == 1 && hasL0EE ){
5979        sel_tree->GetEntry(iev);  
5980        Long64_t answer = il0entry;
5981        Int_t readl0 = 0;
5982        readl0 = l0_tree->GetEntry(answer);
5983        return readl0;
5984        }*/
5985      printf(" PamLevel2::GetYodaEntry() ERROR! \n");
5986      cout << " Entry not found! OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5987      cout << "\nFor bug reporting instructions, please see for example:\n";
5988      cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5989      cout << "  " << endl;
5990      return 0;
5991  }  }
5992    
5993  /**  /**
5994   * \Brief Set DB connection   * \Brief Set DB connection
5995   */   */
5996  Bool_t PamLevel2::SetDBConnection(){  Bool_t PamLevel2::SetDBConnection() {
5997    
5998      cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;    //    cout << "PamLevel2::SetDBConnection()" << endl;
5999      cout<<"Connecting to DB"<<endl;    if (DBG) {
6000      cout<<"HOST "<<host<<endl;      cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
6001      cout<<"USER "<<user<<endl;      cout << "Connecting to DB" << endl;
6002      cout<<"PSW  "<<psw<<endl;      cout << "HOST " << host << endl;
6003      dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());      cout << "USER " << user << endl;
6004      if( !dbc )return false;      cout << "PSW  " << psw << endl;
6005      if( !dbc->IsConnected() )return false;        }
6006      cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;    Bool_t notconn = true;
6007      return true;    Int_t trials = 0;
6008      while ( notconn && trials < 10 ){
6009        //    gSystem->Sleep(500);
6010        dbc = TSQLServer::Connect(host.Data(), user.Data(), psw.Data());
6011        //dbc->Connect(host.Data(), user.Data(), psw.Data());
6012        if ( dbc ) notconn = false;
6013        if (DBG) printf("<%i> test connection...\n ",trials);
6014        if (!dbc){
6015          if (DBG) printf(" :( failed, no pointer \n");
6016          notconn = true;
6017          //      return false;
6018        };
6019        if (dbc && !dbc->IsConnected()){
6020          if (DBG) printf(" :( failed, no connection \n");
6021          notconn = true;
6022          //      return false;
6023        };
6024        trials++;
6025      };
6026      if ( notconn ) return false;
6027      //
6028      if (DBG) printf("=connected!\n");
6029      stringstream myquery; // EMILIANO
6030      myquery.str(""); // EMILIANO
6031      myquery << "SET time_zone='+0:00'"; // EMILIANO
6032      dbc->Query(myquery.str().c_str()); // EMILIANO
6033      if ( DBG ) printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
6034      return true;
6035    
6036  }  }
6037    
# Line 3502  Bool_t PamLevel2::SetDBConnection(){ Line 6040  Bool_t PamLevel2::SetDBConnection(){
6040   * @param cname name of the chain to be added   * @param cname name of the chain to be added
6041   */   */
6042    
6043  TChain* PamLevel2::AddFriend(TString cname){  TChain* PamLevel2::AddFriend(TString cname) {
6044    
6045      if(!GetPamTree()){    if (!GetPamTree()) {
6046          cout << " TChain* PamLevel2::AddFriend(TString cname) --- a pamela tree must be created first"<<endl;      cout << " TChain* PamLevel2::AddFriend(TString cname) --- a pamela tree must be created first" << endl;
6047          return NULL;      return NULL;
6048      }    }
6049    
6050      TChain *c = new TChain(cname.Data());
6051    
6052      TChain *c = new TChain( cname.Data() );    TIter next(GetPamTree()->GetListOfFiles());
6053      Int_t nf = 0;
6054      TChainElement* element = 0;
6055      while ((element = (TChainElement*) next())) {
6056        c->Add(element->GetTitle());
6057        nf++;
6058      }
6059    
6060      TIter next( GetPamTree()->GetListOfFiles() );    GetPamTree()->AddFriend(cname.Data());
     Int_t nf = 0;  
     TChainElement* element = 0;      
     while ((element = (TChainElement*) next())) {        
         c->Add(element->GetTitle());  
         nf++;  
     }  
6061    
6062      GetPamTree()->AddFriend(cname.Data());    cout << "external chain created and added to pamela friends :" << cname << endl;
6063      cout << "n.files " << nf << endl;
6064    
6065      cout << "external chain created and added to pamela friends :"<<cname<<endl;    return c;
     cout << "n.files "<<nf<<endl;  
       
6066    
6067      return c;      }
6068    
6069    /**
6070     * Returns the current read entry. This method simply returns the result of the call to
6071     * pam_tree->GetReadEntry(), so it is entirely handled by ROOT.
6072     */
6073    Long64_t PamLevel2::GetReadEntry() {
6074      return pam_tree->GetReadEntry();
6075    }
6076    
6077    /**
6078     * Sets the sorting method. If the new method is different from the previous, the issorted
6079     * flag is set to false, forcing a new sort the next time GetTrack is called.
6080     * @see GetTrack
6081     */
6082    void PamLevel2::SetSortingMethod(TString how) {
6083      if (howtosort != how) {
6084        issorted = false;
6085        issorted_new = false;
6086      }
6087      howtosort = how;
6088  }  }

Legend:
Removed from v.1.48  
changed lines
  Added in v.1.102

  ViewVC Help
Powered by ViewVC 1.1.23