/[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.44 by mocchiut, Mon Sep 3 09:16:48 2007 UTC revision 1.111 by pam-fi, Fri Oct 30 09:32:24 2015 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  };    orb_track = 0;
285      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    //  cout << t<<"--"<<c<<"--"<<o<<"--"<<r<<"--"<< endl;
319    
320      if (t){
321          trk_track = new TrkTrack(*t);
322          trk_ext_track = new ExtTrack(*t);//NB!! ha dimensione 6 invece che 8
323      }else{
324          trk_track = new TrkTrack();
325          trk_ext_track = new ExtTrack();
326          
327      }
328    
329  };    if (c)
330  PamTrack::PamTrack(const PamTrack& track){      calo_track = new CaloTrkVar(*c);
331      else
332        calo_track = new CaloTrkVar();
333      
334      if (o)
335        tof_track = new ToFTrkVar(*o);
336      else
337        tof_track = new ToFTrkVar();
338    
339      if (r)
340        orb_track = new OrbitalInfoTrkVar(*r);
341      else
342        orb_track = new OrbitalInfoTrkVar();
343    
344      TrkTrack   *t = track.trk_track;  //  cout << trk_track<<"--"<< calo_track <<"--"<<tof_track<<"--"<<orb_track<<"--"<< endl;
345      CaloTrkVar *c = track.calo_track;  
346      ToFTrkVar  *o = track.tof_track;    candeleteobj = 1;
347    
348    }
349    ;
350    /**
351     * Constructor
352     */
353    PamTrack::PamTrack(ExtTrack* t, CaloTrkVar* c, ToFTrkVar* o, OrbitalInfoTrkVar *r) {
354    
     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;  
355            
356    
357      trk_ext_track = 0;
358      trk_track = 0;
359      calo_track = 0;
360      tof_track = 0;
361      orb_track = 0;
362      //     if(t)trk_track  = new TrkTrack(*t);
363      //     if(c)calo_track = new CaloTrkVar(*c);
364      //     if(o)tof_track  = new ToFTrkVar(*o);
365    //   if (t)
366    //     trk_track = t;
367    //   if (c)
368    //     calo_track = c;
369    //   if (o)
370    //     tof_track = o;
371    //   if (r)
372    //     orb_track = r;
373    
374    //   candeleteobj = 0;
375    
376    //  cout << t<<"--"<<c<<"--"<<o<<"--"<<r<<"--"<< endl;
377    
378    
379      if (t){
380    ////      trk_track = new TrkTrack(*t);//in this case TrkTrack object remains null
381          trk_ext_track = new ExtTrack(*t);
382      }else{
383          trk_ext_track = new ExtTrack();
384      }
385      if (c)
386        calo_track = new CaloTrkVar(*c);
387      else
388          calo_track = new CaloTrkVar();
389    
390      if (o)
391        tof_track = new ToFTrkVar(*o);
392      else
393        tof_track = new ToFTrkVar();
394    
395      if (r)
396        orb_track = new OrbitalInfoTrkVar(*r);
397      else
398        orb_track = new OrbitalInfoTrkVar();
399    
400    //  cout << trk_track<<"--"<< calo_track <<"--"<<tof_track<<"--"<<orb_track<<"--"<< endl;
401    
402      candeleteobj = 1;
403      pscore = 0;
404      iscore = 0;
405    
406  }  }
407  void PamTrack::Clear(){  ;
408    
409  //    cout << "PamTrack::Clear() "<<candeleteobj<<endl;  PamTrack::PamTrack(const PamTrack& track) {
410      if(candeleteobj){  
411          if(trk_track)  trk_track->TrkTrack::Clear();    TrkTrack *t = track.trk_track;
412          if(calo_track) calo_track->CaloTrkVar::Clear();//???    ExtTrack *e = track.trk_ext_track;
413          if(tof_track)  tof_track->ToFTrkVar::Clear();//???    CaloTrkVar *c = track.calo_track;
414      }else{    ToFTrkVar *o = track.tof_track;
415          trk_track  = 0;    OrbitalInfoTrkVar *r = track.orb_track;
416          calo_track = 0;  
417          tof_track  = 0;    trk_ext_track = 0;
418      }    trk_track     = 0;
419  }    calo_track    = 0;
420  void PamTrack::Delete(){    tof_track     = 0;
421  //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;    orb_track     = 0;
422      if(candeleteobj){    if(e)
423          if(trk_track)  {        trk_ext_track = new ExtTrack(*e);
424              trk_track->TrkTrack::Clear();    if (t)
425              delete trk_track;      trk_track = new TrkTrack(*t);
426          }    if (c)
427          if(calo_track){      calo_track = new CaloTrkVar(*c);
428              calo_track->CaloTrkVar::Clear();//???    if (o)
429              delete calo_track;      tof_track = new ToFTrkVar(*o);
430          }    if (r)
431          if(tof_track){      orb_track = new OrbitalInfoTrkVar(*r);
432              tof_track->ToFTrkVar::Clear();//???  
433              delete tof_track;    candeleteobj = 1;
434          }    pscore = 0;
435      }else{    iscore = 0;
436          Clear();  
437    }
438    
439    void PamTrack::Copy( PamTrack& track) const {
440    
441        track.trk_track = trk_track;
442        track.trk_ext_track = trk_ext_track;
443        track.calo_track = calo_track;
444        track.tof_track = tof_track;
445        track.orb_track = orb_track;
446        
447        track.candeleteobj = candeleteobj;
448        track.pscore = pscore;
449        track.iscore = iscore;
450        
451    }
452    
453    
454    
455    void PamTrack::Clear(Option_t *option) {
456    
457    //    cout << "PamTrack::Clear( "<<option<<" ) "<<candeleteobj<<endl;
458      if (candeleteobj) {
459          
460        if (trk_ext_track)
461          trk_ext_track->ExtTrack::Clear(option);
462        if (trk_track)
463          trk_track->TrkTrack::Clear();
464        if (calo_track)
465          calo_track->CaloTrkVar::Clear();//???
466        if (tof_track)
467          tof_track->ToFTrkVar::Clear();//???
468        if (orb_track)
469          orb_track->OrbitalInfoTrkVar::Clear();//???
470      }
471      else {
472        trk_ext_track = 0;
473        trk_track = 0;
474        calo_track = 0;
475        tof_track = 0;
476        orb_track = 0;
477      }
478      pscore = 0;
479      iscore = 0;
480    
481    }
482    void PamTrack::Delete() {
483      //    cout << "PamTrack::Delete() "<<candeleteobj<<endl;
484      if (candeleteobj) {
485        if (trk_ext_track) {
486    //      trk_ext_track->ExtTrack::Clear("C");//Clear is called for all the array elements
487            trk_ext_track->ExtTrack::Clear("C+C");//Clear is called for all the array elements passing option 'C'
488          delete trk_ext_track;
489        }
490        if (trk_track) {
491          trk_track->TrkTrack::Clear();
492          delete trk_track;
493        }
494        if (calo_track) {
495          calo_track->CaloTrkVar::Clear();//???
496          delete calo_track;
497        }
498        if (tof_track) {
499          tof_track->ToFTrkVar::Clear();//???
500          delete tof_track;
501        }
502        if (orb_track) {
503          orb_track->OrbitalInfoTrkVar::Clear();//???
504          delete orb_track;
505      }      }
506      }
507      else {
508        Clear();
509      }
510  }  }
511    
512    
513    
514    
515  //--------------------------------------  //--------------------------------------
516  //  //
517  //  //
518  //--------------------------------------  //--------------------------------------
519  /**  /**
520   * Default Constructor   * Default Constructor
521   */   */
522  PamLevel2::PamLevel2(){  PamLevel2::PamLevel2() {
523    Initialize();    Initialize();
524  };  }
525    
526    
527  /**  /**
528   * Constructor with given dir, list and detectors   * Constructor
529     * @param ddir Name of directory where level2 files are stored.
530     * @param list Name of an ascii file containing the list of file names
531     * @param detlist Options to chose what to load.
532     * Possible options are:
533     *       +AUTO --> load all trees/branches in the input files
534     *       +(-)ALL --> inlcude(exclude) all trees/branches
535     *       +(-)TRK1+(-)TRK2+(-)CAL1+(-)CAL2+(-)TOF+(-)TRG+(-)ND+(-)S4+(-)ORB+(-)AC --> inlcude(exclude) trees and branches
536     *       +(-)TRK0 --> include(exclude) tracker level0 tree
537     *       +(-)GP --> include exclude GPAMELA output tree
538     * If no options are specified, the default is assumed. Default is:
539     * +TRK2+CAL2+CAL1+TOF+TRG+ND+AC+S4+ORB
540   */   */
541  PamLevel2::PamLevel2(TString ddir,TString list,TString detlist){  PamLevel2::PamLevel2(TString ddir, TString llist, TString detlist) {
542    Initialize();    Initialize();
543    GetPamTree(GetListOfLevel2Files(ddir,list),detlist);    TList* listf = GetListOfLevel2Files(ddir, llist);
544    GetRunTree(GetListOfLevel2Files(ddir,list));    if (listf)
545  };      GetPamTree(listf, detlist);
546      if (listf)
547        GetRunTree(listf);
548      SetMaxShift(-1);
549    }
550    
551    
552    PamLevel2::PamLevel2(TString ddir, TList *llist, TString detlist) {
553      Initialize();
554      GetPamTree(llist, detlist);
555      GetRunTree(llist);
556      SetMaxShift(-1);
557    }
558    
559  /**  /**
560   * Constructor with given dir and list   * Constructor
561     * @param ddir Name of directory where level2 files are stored.
562     * @param list Name of an ascii file containing the list of file names
563     * Default trees/branches are loaded. Default is:
564     * +TRK2+CAL2+CAL1+TOF+TRG+ND+AC+S4+ORB
565   */   */
566  PamLevel2::PamLevel2(TString ddir,TString list){  PamLevel2::PamLevel2(TString ddir, TString llist) {
567    Initialize();    Initialize();
568    GetPamTree(GetListOfLevel2Files(ddir,list),"");    TList* listf = GetListOfLevel2Files(ddir, llist);
569    GetRunTree(GetListOfLevel2Files(ddir,list));    cout << "GetPamTree: "<<endl;
570  };    GetPamTree(listf, "");
571      cout << "GetRunTree: "<<endl;
572      GetRunTree(listf);
573      SetMaxShift(-1);
574    }
575    
576    
577  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;  
578    
579      prevshift = 0;    h0_obj = 0;
580          trk0_obj = 0;
581      run_tree = NULL;    calo0_obj = 0;
582      run_tree_clone = NULL;  
583      sel_tree = NULL;    trk2_obj = 0;
584      sel_tree_clone = NULL;    trk1_obj = 0;
585          trkh_obj = 0;
586      irunentry = -1LL;    calo1_obj = 0;
587      pam_tree = NULL;    calo2_obj = 0;
588      for(Int_t i=0; i<NCLONES; i++ )pam_tree_clone[i]=NULL;    tof2_obj = 0;
589      trig_obj = 0;
590      host = "mysql://localhost/pamelaprod";    s4_obj = 0;
591      user = "anonymous";    nd_obj = 0;
592      psw = "";    ac_obj = 0;
593      const char *pamdbhost=gSystem->Getenv("PAM_DBHOST");    orb2_obj = 0;
594      const char *pamdbuser=gSystem->Getenv("PAM_DBUSER");    gp_obj = 0;
595      const char *pamdbpsw=gSystem->Getenv("PAM_DBPSW");  
596      if ( !pamdbhost ) pamdbhost = "";    extAlgFlag=0;
597      if ( !pamdbuser ) pamdbuser = "";  
598      if ( !pamdbpsw ) pamdbpsw = "";    trk_ext_obj     = 0;
599      if ( strcmp(pamdbhost,"") ) host = pamdbhost;    trk_ext_nuc_obj = 0;
600      if ( strcmp(pamdbuser,"") ) user = pamdbuser;    trk_nuc_obj     = 0;
601      if ( strcmp(pamdbpsw,"") ) psw = pamdbpsw;    
602      calo_ext_obj     = 0;
603      calo_ext_nuc_obj = 0;
604      calo_nuc_obj     = 0;
605      
606      tof_ext_obj     = 0;
607      tof_ext_nuc_obj = 0;
608      tof_nuc_obj     = 0;
609      
610      orb_ext_obj     = 0;
611      orb_ext_nuc_obj = 0;
612      orb_nuc_obj     = 0;
613    
614      trk2_nuc_obj  = 0;
615      calo2_nuc_obj = 0;
616      tof2_nuc_obj  = 0;
617      orb2_nuc_obj  = 0;
618    
619    
620      run_obj = 0;//new GL_RUN();
621      soft_obj = 0;// Emiliano
622      proc_obj = 0;// Emiliano
623      irun = -1LL;
624      irunt = -1LL;
625      totrunentry = 0LL;
626      totrunentrymax = 0LL;
627      totrunentrymin = 0LL;
628      runfirstentry = 0LL;
629      runlastentry = 0LL;
630      gltsync = 0; // Emiliano
631      fUpdateRunInfo = true; // Emiliano
632      fUseDBinRunInfo = true; // Emiliano
633      fDiscarded = false; //EM
634      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
635      il0entry = 0LL;
636      //  hasL0EE = true;
637    
638          l0_file = NULL;
639      l0_tree = NULL;
640      iroot = -1;
641      dbc = 0;
642    
643      prevshift = 0;
644      yprevshift = 0;
645      maxshift = 10; //EMILIANO now overridden by SetMaxShift(-1) called by constructors
646    
647  //    sorted_tracks = 0;//new TRefArray();    run_tree = NULL;
648          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;  
649    
650      SELLI = -1;    proc_tree = NULL;
651      proc_tree_clone = NULL;
652    
653      ISGP = false;    sel_tree = NULL;
654      sel_tree_clone = NULL;
655    
656      tsorted=0;    irunentry = -1LL;
657      timage=0;    pam_tree = NULL;
658          for (Int_t i = 0; i < NCLONES; i++)
659      howtosort = "+CAL+TOF";      pam_tree_clone[i] = NULL;
     //howtosort = "+TOF";  
     sortthr = 100.;  
660    
661  };    totdltime[0] = 0LL;
662      totdltime[1] = 0LL;
663      totdltime[2] = 0LL;
664    
665      host = "mysql://localhost/pamelaprod";
666      user = "anonymous";
667      psw = "";
668      const char *pamdbhost = gSystem->Getenv("PAM_DBHOST");
669      const char *pamdbuser = gSystem->Getenv("PAM_DBUSER");
670      const char *pamdbpsw = gSystem->Getenv("PAM_DBPSW");
671      if (!pamdbhost)
672        pamdbhost = "";
673      if (!pamdbuser)
674        pamdbuser = "";
675      if (!pamdbpsw)
676        pamdbpsw = "";
677      if (strcmp(pamdbhost, ""))
678        host = pamdbhost;
679      if (strcmp(pamdbuser, ""))
680        user = pamdbuser;
681      if (strcmp(pamdbpsw, ""))
682        psw = pamdbpsw;
683    
684      customString = "";
685    
686      //    sorted_tracks = 0;//new TRefArray();
687    
688      CAL0 = false;
689      CAL1 = true;
690      CAL2 = true;
691      TRK2 = true;
692      TRK1 = false;
693      TRK0 = false;
694      TRKh = false;
695      TRG = true;
696      TOF = true;
697      TOF0 = false;
698      S4 = true;
699      ND = true;
700      AC = true;
701      ORB = true;
702      PROC = true;
703      GP = false;
704    
705      EXT = false;
706      NUC = false;
707      trkAlg = "STD";//default tracking algorythm
708    
709      RUN = true;
710    
711      SELLI = -1;
712    
713      ISGP = false;
714    
715      DBG = false;
716    
717      tsorted = 0;
718      timage = 0;
719      text = 0 ;
720    
721      tsorted_nuc = 0;
722      timage_nuc = 0;
723      text_nuc = 0 ;
724    
725      howtosort = "+CAL+TOF";
726      //howtosort = "+TOF";
727      sortthr = 100.;
728    
729      issorted = false;
730      lastsorted = -1;
731      issorted_new = false;
732      lastsorted_new = -1;
733    
734    }
735    ;
736  /**  /**
737   * Delete the event (act as Dtor)   * Delete the event (act as Dtor)
738   */   */
739  void PamLevel2::Delete(){  void PamLevel2::Delete() {
           
     if(run_obj)delete run_obj;  
     if(soft_obj)delete soft_obj; //Emiliano  
740    
741  //    cout << "void PamLevel2::Clear()"<<endl;    if (run_obj)
742      if(h0_obj)   delete h0_obj;      delete run_obj;
743      if(trk0_obj) delete trk0_obj;          if (soft_obj)
744      if(calo0_obj) delete calo0_obj;          delete soft_obj; //Emiliano
745      if(trk1_obj) delete trk1_obj;          if (proc_obj)
746      if(trk2_obj) delete trk2_obj;      delete proc_obj; //Emiliano
747      if(trkh_obj) delete trkh_obj;  
748      if(calo1_obj)delete calo1_obj;    //    cout << "void PamLevel2::Clear()"<<endl;
749      if(calo2_obj)delete calo2_obj;    if (h0_obj)
750      if(tof_obj)  delete tof_obj;      delete h0_obj;
751      if(trig_obj) delete trig_obj;    if (trk0_obj)
752      if(s4_obj)   delete s4_obj;      delete trk0_obj;
753      if(nd_obj)   delete nd_obj;    if (calo0_obj)
754      if(ac_obj)   delete ac_obj;      delete calo0_obj;
755      if(orb_obj)  delete orb_obj;    if (trk1_obj)
756            delete trk1_obj;
757      if(tsorted){    if (trk2_obj)
758          tsorted->Delete();      delete trk2_obj;
759          delete tsorted;    if (trkh_obj)
760      }      delete trkh_obj;
761      if(timage){    if (calo1_obj)
762          timage->Delete();      delete calo1_obj;
763          delete timage;    if (calo2_obj)
764      }      delete calo2_obj;
765      if (tof2_obj)
766        delete tof2_obj;
767      if (trig_obj)
768        delete trig_obj;
769      if (s4_obj)
770        delete s4_obj;
771      if (nd_obj)
772        delete nd_obj;
773      if (ac_obj)
774        delete ac_obj;
775      if (orb2_obj)
776        delete orb2_obj;
777      if (gp_obj)
778        delete gp_obj;
779    
780      if(trk_nuc_obj)trk_nuc_obj->Delete();
781      if(trk_ext_obj)trk_ext_obj->Delete();
782      if(trk_ext_nuc_obj)trk_ext_nuc_obj->Delete();
783    
784      if(calo_nuc_obj)calo_nuc_obj->Delete();
785      if(calo_ext_obj)calo_ext_obj->Delete();
786      if(calo_ext_nuc_obj)calo_ext_nuc_obj->Delete();
787    
788      if(tof_nuc_obj)tof_nuc_obj->Delete();
789      if(tof_ext_obj)tof_ext_obj->Delete();
790      if(tof_ext_nuc_obj)tof_ext_nuc_obj->Delete();
791    
792      if(orb_nuc_obj)orb_nuc_obj->Delete();
793      if(orb_ext_obj)orb_ext_obj->Delete();
794      if(orb_ext_nuc_obj)orb_ext_nuc_obj->Delete();
795    
796    
797      if(trk2_nuc_obj)trk2_nuc_obj->Delete();;
798      if( calo2_nuc_obj)calo2_nuc_obj->Delete();;
799      if(tof2_nuc_obj)tof2_nuc_obj->Delete();;
800      if(orb2_nuc_obj)orb2_nuc_obj->Delete();;
801      
802    
     if(dbc){  
         dbc->Close();  
         delete dbc;  
     }  
       
     if(l0_file)l0_file->Close();  
     //    if(pam_tree)pam_tree->Delete();;  
803    
804    if ( pam_tree ){    if (tsorted) {
805        tsorted->Delete();
806        delete tsorted;
807      }
808      if (timage) {
809        timage->Delete();
810        delete timage;
811      }
812      if (text) {
813        text->Delete();
814        delete text;
815      }
816      if (tsorted_nuc) {
817        tsorted_nuc->Delete();
818        delete tsorted_nuc;
819      }
820      if (timage_nuc) {
821        timage_nuc->Delete();
822        delete timage_nuc;
823      }
824      if (text_nuc) {
825        text_nuc->Delete();
826        delete text_nuc;
827      }
828    
829    
830    
831      if (dbc) {
832        dbc->Close();
833        delete dbc;
834        dbc=0;
835      }
836    
837      if (gltsync)
838        delete gltsync;
839    
840      if (l0_file)
841        l0_file->Close();
842      //    if(pam_tree)pam_tree->Delete();;
843    
844      if (pam_tree) {
845      //      //
846      // 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...
847      //      //
848      TList *temp = pam_tree->GetListOfFriends();      TList *temp = pam_tree->GetListOfFriends();
849      TList *contents  = new TList; // create chain friend list      TList *contents = new TList; // create chain friend list
850      contents->SetOwner();      contents->SetOwner();
851      TIter next(temp);      TIter next(temp);
852      TChain *questo = 0;      TChain *questo = 0;
853      while ( (questo = (TChain*) next()) ){      while ((questo = (TChain*) next())) {
854        TString name =  questo->GetName();        TString name = questo->GetName();
855        contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list        contents->Add((TChain*) gROOT->FindObject(name.Data()));// add object to the list
856      };      };
857      //      //
858      // deleting the main chain      // deleting the main chain
# Line 259  void PamLevel2::Delete(){ Line 863  void PamLevel2::Delete(){
863      //      //
864      TIter next2(contents);      TIter next2(contents);
865      TChain *questa = 0;      TChain *questa = 0;
866      while ( questa = (TChain*)next2() ){      while ( (questa = (TChain*)next2()) ) {
867        TString name =  questa->GetName();        TString name = questa->GetName();
868        questa->Delete();              questa->Delete();
869        questa=NULL;        questa = NULL;
870      };      };
871      //      //
872    };    };
873    pam_tree = NULL;    pam_tree = NULL;
874    
875      if(run_tree)run_tree->Delete();;    if (run_tree)
876      if(sel_tree)sel_tree->Delete();;      run_tree->Delete();;
877      for(Int_t i=0; i<NCLONES; i++ )if(pam_tree_clone[i])pam_tree_clone[i]->Delete();;    if (sel_tree)
878      if(run_tree_clone)run_tree_clone->Delete();;      sel_tree->Delete();;
879      if(sel_tree_clone)sel_tree_clone->Delete();;  
880          // The following lines are commented out since they may generate a double delete error
881      // if the file containing the clone trees is closed. This is because the file owns the
882      // clone trees which are written into it, so it will delete them when it is closed; if
883      // also PamLevel2 will try to delete these trees, a double delete error will be generated
884      // when exiting from analysis program. (Nicola 28/11/2011)
885    
886      /*for (Int_t i = 0; i < NCLONES; i++)
887        if (pam_tree_clone[i])
888          pam_tree_clone[i]->Delete();;
889      if (run_tree_clone)
890        run_tree_clone->Delete();;
891      if (sel_tree_clone)
892        sel_tree_clone->Delete();;*/
893    
894  };    if (irunoffset)
895        delete[] irunoffset;
896    
897    
898      Initialize();
899    
900    }
901    ;
902    
903  /**  /**
904   * Clear the event (NB! does not deallocate objects)   * Clear the event (NB! does not deallocate objects)
905   */   */
906  void PamLevel2::Clear(){  void PamLevel2::Clear() {
           
 //    cout << "void PamLevel2::Clear()"<<endl;  
907    
908  //    //    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();  
909    
910      if(tsorted){    //
911          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
912      }    // want to load them for each event even if they are the same...
913      if(timage){    //
914          timage->Delete();    //    if(run_obj)delete run_obj;
915      }    //    if(run_obj) run_obj->Clear();  // Emiliano: Do not deallocate run_obj here, it will give segmentation fault! call clear instead
916  };    //    if(soft_obj) soft_obj->Clear();
917    
918      if (h0_obj)
919        h0_obj->Clear();
920      //    if(trk0_obj) trk0_obj->Clear();
921      if (trk1_obj)
922        trk1_obj->Clear();
923      if (trk2_obj)
924        trk2_obj->Clear();
925      if (trkh_obj)
926        trkh_obj->Clear();
927      if (calo0_obj)
928        calo0_obj->Clear();
929      if (calo1_obj)
930        calo1_obj->Clear();
931      if (calo2_obj)
932        calo2_obj->Clear();
933      if (tof2_obj)
934        tof2_obj->Clear();
935      if (trig_obj)
936        trig_obj->Clear();
937      if (s4_obj)
938        s4_obj->Clear();
939      if (nd_obj)
940        nd_obj->Clear();
941      if (ac_obj)
942        ac_obj->Clear();
943      if (orb2_obj)
944        orb2_obj->Clear();
945      if (gp_obj)
946        gp_obj->Clear();
947      if (proc_obj)
948        proc_obj->Clear();
949    
950      //    if(sorted_tracks)sorted_tracks->Clear();
951      //    sorted_tracks.Clear();
952    
953      if(trk_nuc_obj)trk_nuc_obj->Clear();
954      if(trk_ext_obj)trk_ext_obj->Clear();
955      if(trk_ext_nuc_obj)trk_ext_nuc_obj->Clear();
956    
957      if(calo_nuc_obj)calo_nuc_obj->Clear();
958      if(calo_ext_obj)calo_ext_obj->Clear();
959      if(calo_ext_nuc_obj)calo_ext_nuc_obj->Clear();
960    
961      if(tof_nuc_obj)tof_nuc_obj->Clear();
962      if(tof_ext_obj)tof_ext_obj->Clear();
963      if(tof_ext_nuc_obj)tof_ext_nuc_obj->Clear();
964    
965      if(orb_nuc_obj)orb_nuc_obj->Clear();
966      if(orb_ext_obj)orb_ext_obj->Clear();
967      if(orb_ext_nuc_obj)orb_ext_nuc_obj->Clear();
968    
969      if(trk2_nuc_obj)trk2_nuc_obj->Clear();;
970      if( calo2_nuc_obj)calo2_nuc_obj->Clear();;
971      if(tof2_nuc_obj)tof2_nuc_obj->Clear();;
972      if(orb2_nuc_obj)orb2_nuc_obj->Clear();;
973    
974      if (tsorted)tsorted->Delete();
975      if (timage)timage->Delete();
976      if (text) text->Delete();
977    
978      if (tsorted_nuc)tsorted_nuc->Delete();
979      if (timage_nuc)timage_nuc->Delete();
980      if (text_nuc) text_nuc->Delete();
981    }
982    ;
983    
984  void PamLevel2::Reset(){  void PamLevel2::Reset() {
985    //    //
986    // First of all clear everything    // First of all clear everything
987    //    //
988    Clear();    Clear();
989    //    //
990    // close and reset chains and pointers    // close and reset chains and pointers
991    //        //
992    if ( pam_tree ){    if (pam_tree) {
993      //      //
994      // 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...
995      //      //
996      TList *temp = pam_tree->GetListOfFriends();      TList *temp = pam_tree->GetListOfFriends();
997      TList *contents  = new TList; // create chain friend list      TList *contents = new TList; // create chain friend list
998      contents->SetOwner();      contents->SetOwner();
999      TIter next(temp);      TIter next(temp);
1000      TChain *questo = 0;      TChain *questo = 0;
1001      while ( (questo = (TChain*) next()) ){      while ((questo = (TChain*) next())) {
1002        TString name =  questo->GetName();        TString name = questo->GetName();
1003        contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list        contents->Add((TChain*) gROOT->FindObject(name.Data()));// add object to the list
1004      };      };
1005      //      //
1006      // deleting the main chain      // deleting the main chain
# Line 348  void PamLevel2::Reset(){ Line 1011  void PamLevel2::Reset(){
1011      //      //
1012      TIter next2(contents);      TIter next2(contents);
1013      TChain *questa = 0;      TChain *questa = 0;
1014      while ( questa = (TChain*)next2() ){      while ( (questa = (TChain*) next2()) ) {
1015        TString name =  questa->GetName();        TString name = questa->GetName();
1016        questa->Delete();              questa->Delete();
1017        questa=NULL;        questa = NULL;
1018      };      };
1019      //      //
1020    };    };
1021    pam_tree = NULL;    pam_tree = NULL;
1022    //    //
1023    if(run_tree)run_tree->Delete();;    if (run_tree)
1024        run_tree->Delete();;
1025    run_tree = NULL;    run_tree = NULL;
1026    if(sel_tree)sel_tree->Delete();;    if (sel_tree)
1027        sel_tree->Delete();;
1028    sel_tree = NULL;    sel_tree = NULL;
1029    
1030      if (proc_tree)
1031        proc_tree->Delete();
1032      proc_tree = NULL;
1033    //    //
1034    // Close file    // Close file
1035    //    //
1036    if(l0_file)l0_file->Close("R");    if (l0_file)
1037        l0_file->Close("R");
1038    l0_file = NULL;    l0_file = NULL;
1039    //    //
1040    h0_obj    = 0;    h0_obj = 0;
1041    trk0_obj  = 0;    trk0_obj = 0;
1042    calo0_obj  = 0;    calo0_obj = 0;
1043    //    //
1044    trk2_obj  = 0;    trk2_obj = 0;
1045    trk1_obj  = 0;    trk1_obj = 0;
1046    trkh_obj  = 0;    trkh_obj = 0;
1047    calo1_obj = 0;    calo1_obj = 0;
1048    calo2_obj = 0;    calo2_obj = 0;
1049    tof_obj   = 0;    tof2_obj = 0;
1050    trig_obj  = 0;    trig_obj = 0;
1051    s4_obj    = 0;    s4_obj = 0;
1052    nd_obj    = 0;    nd_obj = 0;
1053    ac_obj    = 0;    ac_obj = 0;
1054    orb_obj   = 0;    orb2_obj = 0;
1055      gp_obj = 0;
1056      proc_obj = 0;
1057    
1058      trk_ext_obj     = 0;
1059      trk_ext_nuc_obj = 0;
1060      trk_nuc_obj     = 0;
1061      
1062      calo_ext_obj     = 0;
1063      calo_ext_nuc_obj = 0;
1064      calo_nuc_obj     = 0;
1065      
1066      tof_ext_obj     = 0;
1067      tof_ext_nuc_obj = 0;
1068      tof_nuc_obj     = 0;
1069      
1070      orb_ext_obj     = 0;
1071      orb_ext_nuc_obj = 0;
1072      orb_nuc_obj     = 0;
1073    
1074      trk2_nuc_obj  = 0;
1075      calo2_nuc_obj = 0;
1076      tof2_nuc_obj  = 0;
1077      orb2_nuc_obj  = 0;
1078    
1079      trk2_nuc_obj  = 0;
1080      calo2_nuc_obj = 0;
1081      tof2_nuc_obj  = 0;
1082      orb2_nuc_obj  = 0;
1083    //    //
1084    // Reset run pointers    // Reset run pointers
1085    //    //
1086    run_obj   = 0;//new GL_RUN();    run_obj = 0;//new GL_RUN();
1087    soft_obj   = 0;// Emiliano    soft_obj = 0;// Emiliano
1088      proc_obj = 0;// Emiliano
1089    irun = -1;    irun = -1;
1090      irunt = -1;
1091      totrunentry = 0LL;
1092      totrunentrymax = 0LL;
1093      totrunentrymin = 0LL;
1094    runfirstentry = 0ULL;    runfirstentry = 0ULL;
1095    runlastentry = 0ULL;        runlastentry = 0ULL;
1096      prevabstime = 0ULL;
1097      prevpktnum = 0;
1098      abstime = 0ULL;
1099      pktnum = 0;
1100      isFragment = false;
1101    //    //
1102  };    totdltime[0] = 0LL;
1103      totdltime[1] = 0LL;
1104      totdltime[2] = 0LL;
1105      //
1106    }
1107    ;
1108    
1109  Bool_t PamLevel2::IsGood(){  Bool_t PamLevel2::IsGood(Bool_t strict) {
1110    Bool_t goodev=true;    Bool_t goodev = true;
1111    //  if(trk2_obj && trk2_obj->UnpackError() != 0 ) goodev = false;    //
1112    if(calo2_obj && calo2_obj->good != 1) goodev = false;    if (calo2_obj && !calo2_obj->IsGood(strict))
1113    if(tof_obj && tof_obj->unpackError != 0) goodev = false;        goodev = false;
1114    if(trig_obj && trig_obj->unpackError != 0) goodev = false;    //
1115    if(s4_obj && s4_obj->unpackError != 0) goodev = false;      if (strict) {
1116    if(nd_obj && nd_obj->unpackError != 0) goodev = false;        if (trk2_obj && trk2_obj->UnpackError() != 0)
1117    if(ac_obj && ac_obj->unpackError != 255) goodev = false;          goodev = false;
1118      //  if(orb_obj)        if (tof2_obj && tof2_obj->unpackError != 0)
1119    return goodev;        goodev = false;
1120  };      if (trig_obj && trig_obj->unpackError != 0)
1121          goodev = false;
1122        if (s4_obj && s4_obj->unpackError != 0)
1123          goodev = false;
1124        if (nd_obj && nd_obj->unpackError != 0)
1125          goodev = false;
1126        if (ac_obj && (ac_obj->unpackError != 0 || ((ac_obj->status[0] >> 2) & 1) || ((ac_obj->status[1] >> 2) & 1)))
1127          goodev = false;
1128        //  if(orb2_obj)
1129      }
1130      else {
1131        if (nd_obj && nd_obj->unpackError != 0)
1132          goodev = false;
1133        if (ac_obj && (ac_obj->unpackError != 0 || ((ac_obj->status[0] >> 2) & 1) || ((ac_obj->status[1] >> 2) & 1)))
1134          goodev = false;
1135      };
1136      return (goodev);
1137    }
1138    ;
1139    
1140    void PamLevel2::SkipRunInfoUpdate(){
1141      printf("\n\n ******** WARNING ******** \n Skip DB connections, DO NOT USE PamLevel2::GetRunInfo() method! \n\n");
1142      fUpdateRunInfo = false;
1143      this->SetSELLI(2);
1144      printf(" ===============> W A R N I N G <================ \n");
1145      printf(" in case PamLevel2::CreateCloneTrees() will be called \n");
1146      printf(" it will be reverted to PadmeAmidala level2 structure , i.e. NO SELECTIONLIST WILL BE CREATED IN THE NEW LEVEL2 FILE! \n\n");
1147      if ( run_tree_clone ){
1148        printf(" ===============> W A R N I N G <================ \n");
1149        printf(" PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() has been called together with PamLevel2::CreateCloneTrees() \n");
1150        printf(" TO AVOID CRASHES call PamLevel2::CreateCloneTrees() after PamLevel2::SkipRunIndoUpdate or PamLevel2::NoDBconnections() \n");    
1151      };
1152    }
1153    
1154    void PamLevel2::SetMaxShift(Int_t sh){
1155      if ( sh >=  0 ){
1156        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");
1157        maxshift = sh;
1158      } else {
1159        ULong64_t nev = GetEntries();
1160        ULong64_t runnev = 0ULL;
1161        for (Int_t r=0; r< run_tree->GetEntries();r++){
1162          run_tree->GetEntry(r);//update runinfo
1163          runnev += GetRunInfo()->NEVENTS;
1164        }
1165        maxshift = (Int_t)(runnev-nev) + 10; // +10 just to be conservative
1166        if ( (runnev-nev) == 0 ) isSync = true;
1167        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);
1168        //    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
1169      }
1170    }
1171    
1172  //--------------------------------------  //--------------------------------------
1173  //  //
1174  //  //
1175  //--------------------------------------  //--------------------------------------
1176  void *PamLevel2::GetPointerTo(const char* c ){  void *PamLevel2::GetPointerTo(const char* c) {
1177    
1178      TString objname = c;    TString objname = c;
1179    
1180      if(!objname.CompareTo("TrkLevel1"))  {    if (!objname.CompareTo("TrkLevel1")) {
1181          if(!trk1_obj){      if (!trk1_obj) {
1182              trk1_obj  = new TrkLevel1();        trk1_obj = new TrkLevel1();
1183              trk1_obj->Set();        trk1_obj->Set();
1184          }      }
1185          return &trk1_obj;      return &trk1_obj;
1186      };    };
1187      if(!objname.CompareTo("TrkLevel2"))  {    if (!objname.CompareTo("TrkLevel2")) {
1188          if(!trk2_obj){        if (!trk2_obj) {
1189              trk2_obj  = new TrkLevel2();        trk2_obj = new TrkLevel2();
1190              trk2_obj->Set();        trk2_obj->Set();
1191          }      }
1192          return &trk2_obj;      return &trk2_obj;
1193      };    };
1194      if(!objname.CompareTo("TrkHough"))   {    if (!objname.CompareTo("TrkHough")) {
1195          if(!trkh_obj)  trkh_obj  = new TrkHough();      if (!trkh_obj) {
1196          return &trkh_obj;        trkh_obj = new TrkHough();
1197      };        trkh_obj->Set();
1198      if(!objname.CompareTo("CaloLevel1")) {      }
1199          if(!calo1_obj) calo1_obj = new CaloLevel1();      return &trkh_obj;
1200          return &calo1_obj;    };
1201      };    if (!objname.CompareTo("CaloLevel1")) {
1202      if(!objname.CompareTo("CaloLevel2")) {      if (!calo1_obj)
1203          if(!calo2_obj){        calo1_obj = new CaloLevel1();
1204              calo2_obj = new CaloLevel2();      return &calo1_obj;
1205              calo2_obj->Set();    };
1206          };    if (!objname.CompareTo("CaloLevel2")) {
1207          return &calo2_obj;      if (!calo2_obj) {
1208      };        calo2_obj = new CaloLevel2();
1209      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;  
1210      };      };
1211            return &calo2_obj;
1212      if(!objname.CompareTo("RunInfo"))return &run_obj;    };
1213      if (!objname.CompareTo("ToFLevel2")) {
1214        if (!tof2_obj) {
1215          tof2_obj = new ToFLevel2();
1216          tof2_obj->Set();
1217        }
1218        return &tof2_obj;
1219      };
1220      if (!objname.CompareTo("TrigLevel2")) {
1221        if (!trig_obj)
1222          trig_obj = new TrigLevel2();
1223        return &trig_obj;
1224      };
1225      if (!objname.CompareTo("S4Level2")) {
1226        if (!s4_obj)
1227          s4_obj = new S4Level2();
1228        return &s4_obj;
1229      };
1230      if (!objname.CompareTo("NDLevel2")) {
1231        if (!nd_obj)
1232          nd_obj = new NDLevel2();
1233        return &nd_obj;
1234      };
1235      if (!objname.CompareTo("AcLevel2")) {
1236        if (!ac_obj)
1237          ac_obj = new AcLevel2();
1238        return &ac_obj;
1239      };
1240      if (!objname.CompareTo("OrbitalInfo")) {
1241        if (!orb2_obj) {
1242          orb2_obj = new OrbitalInfo();
1243          orb2_obj->Set();
1244        }
1245        return &orb2_obj;
1246      };
1247      //     if(!objname.CompareTo("OrbitalInfo")){
1248      //    if(!orb2_obj)   orb2_obj   = new OrbitalInfo();
1249      //    return &orb2_obj;
1250      //     };
1251      if (!objname.CompareTo("GPamela")) {
1252        if (!gp_obj)
1253          gp_obj = new GPamela();
1254        return &gp_obj;
1255      };
1256    
1257      if(!objname.CompareTo("SoftInfo"))return &soft_obj; // Emiliano    if (!objname.CompareTo("RunInfo"))
1258        return &run_obj;
1259    
1260      return NULL;    if (!objname.CompareTo("SoftInfo"))
1261  };      return &soft_obj; // Emiliano
1262    
1263      if (!objname.CompareTo("ProcInfo")){
1264        if (!proc_obj)
1265          proc_obj = new ProcInfo();    
1266        return &proc_obj; // Emiliano
1267      }
1268    
1269      return NULL;
1270    }
1271    ;
1272  //--------------------------------------  //--------------------------------------
1273  //  //
1274  //  //
1275  //--------------------------------------  //--------------------------------------
1276  /**  /**
1277   * Retrieves the calorimeter track matching the seqno-th tracker stored track.   * Retrieves the calorimeter track matching the seqno-th tracker stored track.
1278   * (If seqno = -1 retrieves the self-trigger calorimeter track)   * (If seqno = -1 retrieves the self-trigger calorimeter track)
1279   */   */
1280   CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno){  CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno) {
       
      if( !calo2_obj )return 0;  
1281    
1282       if( calo2_obj->CaloLevel2::ntrk()==0 ){    if (!calo2_obj)
1283           cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no Calorimeter tracks are stored"<<endl;      return 0;
1284           return NULL;  
1285       };    if (calo2_obj->CaloLevel2::ntrk() == 0) {
1286              if( seqno >=0 ){
1287       CaloTrkVar *c = 0;            cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo " << seqno;
1288       Int_t it_calo=0;            cout << " but no Calorimeter tracks are stored" << endl;
1289              }
1290       do{        return NULL;
1291           c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);    };
1292           it_calo++;  
1293       } while( c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());        CaloTrkVar *c = 0;
1294          Int_t it_calo = 0;
1295       if(!c || seqno != c->trkseqno){  
1296           c = 0;    do {
1297           if(seqno!=-1)cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match Calorimeter stored tracks"<<endl;      c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);
1298       };      it_calo++;
1299       return c;    } while (c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());
1300        
1301   };    if (!c || seqno != c->trkseqno) {
1302        c = 0;
1303        if (seqno != -1 && seqno>=0)
1304          cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo " << seqno
1305              << " does not match Calorimeter stored tracks" << endl;
1306      };
1307      return c;
1308    
1309    }
1310    ;
1311  //--------------------------------------  //--------------------------------------
1312   //  //
1313   //  //
1314  //--------------------------------------  //--------------------------------------
1315  /**  /**
1316    * Retrieves the ToF track matching the seqno-th tracker stored track.   * Retrieves the ToF track matching the seqno-th tracker stored track.
1317    * (If seqno = -1 retrieves the tracker-independent tof track)   * (If seqno = -1 retrieves the tracker-independent tof track)
1318   */   */
1319   ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno){  ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno) {
           
      if( !tof_obj )return 0;  
1320    
1321       if( tof_obj->ToFLevel2::ntrk()==0 ){    if (!tof2_obj)
1322           cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no ToF tracks are stored"<<endl;      return 0;
1323           return NULL;  
1324       };    if (tof2_obj->ToFLevel2::ntrk() == 0) {
1325            cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo " << seqno << " but no ToF tracks are stored"
1326       ToFTrkVar *c = 0;          << endl;
1327       Int_t it_tof=0;      return NULL;
1328          };
1329       do{  
1330           c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof);    ToFTrkVar *c = 0;
1331           it_tof++;    Int_t it_tof = 0;
1332       } while( c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk());          
1333          do {
1334       if(!c || seqno != c->trkseqno){      c = tof2_obj->ToFLevel2::GetToFTrkVar(it_tof);
1335           c = 0;      it_tof++;
1336           if(seqno!=-1)cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match ToF stored tracks"<<endl;    } while (c && seqno != c->trkseqno && it_tof < tof2_obj->ToFLevel2::ntrk());
1337       };  
1338       return c;    if (!c || seqno != c->trkseqno) {
1339            c = 0;
1340   };      if (seqno != -1)
1341          cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo " << seqno
1342              << " does not match ToF stored tracks" << endl;
1343      };
1344      return c;
1345    
1346    }
1347    ;
1348    
1349  //--------------------------------------  //--------------------------------------
1350   //  //
1351   //  //
1352  //--------------------------------------  //--------------------------------------
1353  /**  /**
1354   * Give the pamela track associated to a tracker track, retrieving related calorimeter and tof track information.   * Retrieves the OrbitalInfo track matching the seqno-th tracker stored track.
1355     * (If seqno = -1 retrieves the tracker-independent tof related track)
1356   */   */
1357   PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t){  OrbitalInfoTrkVar *PamLevel2::GetOrbitalInfoStoredTrack(int seqno) {
1358        
1359       cout <<"PamLevel2::GetPamTrackAlong(TrkTrack* t) **obsolete** "<<endl;    if (!orb2_obj)
1360       cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;      return 0;
1361    
1362      if (orb2_obj->OrbitalInfo::ntrk() == 0) {
1363        //       // TRICK BEGIN
1364        //       OrbitalInfoTrkVar  *r = new OrbitalInfoTrkVar(); // TEMPORARY TRICK
1365        //       Int_t nn = 0;
1366        //       TClonesArray &tor = *orb2_obj->OrbitalInfoTrk;
1367        //       for(Int_t nt=0; nt < tof2_obj->ToFLevel2::ntrk(); nt++){
1368        //  //
1369        //  ToFTrkVar *ptt = tof2_obj->ToFLevel2::GetToFTrkVar(nt);
1370        //  if ( ptt->trkseqno != -1  ){
1371        //    //
1372        //    r->trkseqno = ptt->trkseqno;
1373        //    //
1374        //    r->Eij = 0;
1375        //    //
1376        //    r->Sij = 0;
1377        //    //
1378        //    r->pitch = -1000.;
1379        //    //
1380        //    r->cutoff = -1000.;
1381        //    //
1382        //    new(tor[nn]) OrbitalInfoTrkVar(*r);
1383        //    nn++;
1384        //    //
1385        //    r->Clear();
1386        //    //
1387        //  };
1388        //       };
1389        //       delete r;
1390        //       OrbitalInfoTrkVar *c = 0;
1391        //       c = orb2_obj->OrbitalInfo::GetOrbitalInfoTrkVar(0);
1392        //       return c;
1393        //       //TRICK END
1394        cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo " << seqno
1395            << " but no OrbitalInfo tracks are stored" << endl;
1396        return NULL;
1397      };
1398    
1399      OrbitalInfoTrkVar *c = 0;
1400      Int_t it_tof = 0;
1401    
1402      do {
1403        c = orb2_obj->OrbitalInfo::GetOrbitalInfoTrkVar(it_tof);
1404        it_tof++;
1405      } while (c && seqno != c->trkseqno && it_tof < orb2_obj->OrbitalInfo::ntrk());
1406    
1407      if (!c || seqno != c->trkseqno) {
1408        c = 0;
1409        if (seqno != -1)
1410          cout << "PamLevel2::GetOrbitalInfoStoredTrack(int) : requested tracker SeqNo " << seqno
1411              << " does not match OrbitalInfo stored tracks" << endl;
1412      };
1413      return c;
1414    
1415    }
1416    ;
1417    
      CaloTrkVar *c = 0;  
      ToFTrkVar  *o = 0;  
       
      if(CAL2) c = GetCaloStoredTrack(t->GetSeqNo());  
      if(TOF) o = GetToFStoredTrack(t->GetSeqNo());  
       
 //    if(t && c && o)track = new PamTrack(t,c,o);  
      PamTrack *track = new PamTrack(t,c,o);  
       
      return track;  
   
  };  
1418  //--------------------------------------  //--------------------------------------
1419  //  //
1420  //  //
1421  //--------------------------------------  //--------------------------------------
1422  /**  // /**
1423   * Retrieves the it-th stored track.  //  * Give the pamela track associated to a tracker track, retrieving related calorimeter, orbitalinfo and tof track information.
1424   * It override TrkLevel2::GetTrack(int it).  //  */
1425   * @param itrk Track number, ranging from 0 to GetNTracks().  // PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t) {
1426   */  
1427    //   cout << "PamLevel2::GetPamTrackAlong(TrkTrack* t) **obsolete** " << endl;
1428    //   cout << "(if you use it, remember to delete the PamTrack object)" << endl;
1429    
1430    //   CaloTrkVar *c = 0;
1431    //   ToFTrkVar *o = 0;
1432    //   OrbitalInfoTrkVar *r = 0;
1433    
1434    //   if (CAL2)
1435    //     c = GetCaloStoredTrack(t->GetSeqNo());
1436    //   if (TOF)
1437    //     o = GetToFStoredTrack(t->GetSeqNo());
1438    //   if (ORB)
1439    //     r = GetOrbitalInfoStoredTrack(t->GetSeqNo());
1440    
1441  PamTrack* PamLevel2::GetStoredTrack(Int_t itrk){  //   //    if(t && c && o)track = new PamTrack(t,c,o);
1442        //   PamTrack *track = new PamTrack(t, c, o, r);
     cout <<"PamLevel2::GetStoredTrack(Int_t itrk) **to-be-updated** "<<endl;  
     cout <<"for the moment, better use separately the methods: TrkLevel2::GetStoredTrack(seqno) CaloLevel2::GetCaloTrkVar(Int_t notrack) ToFLevel2::GetToFTrkVar(Int_t notrack)"<<endl;  
     cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;  
     PamTrack *track = 0;  
       
     if( itrk >=0 && itrk < trk2_obj->TrkLevel2::ntrk() ){  
           
         TrkTrack *t = trk2_obj->TrkLevel2::GetStoredTrack(itrk);  
         track = GetPamTrackAlong(t);  
           
           
1443    
1444      }else{  //   return track;
1445          cout << "PamLevel2::GetStoredTrack(int) : tracker track SeqNo "<< itrk <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;  
1446      };  // }
1447        // ;
1448      return track;  //--------------------------------------
1449        //
1450  }  //
1451    //--------------------------------------
1452    // /**
1453    //  * Retrieves the it-th stored track.
1454    //  * It override TrkLevel2::GetTrack(int it).
1455    //  * @param itrk Track number, ranging from 0 to GetNTracks().
1456    //  */
1457    
1458    // PamTrack* PamLevel2::GetStoredTrack(Int_t itrk) {
1459    
1460    //   cout << "PamLevel2::GetStoredTrack(Int_t itrk) **to-be-updated** " << endl;
1461    //   cout
1462    //       << "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)"
1463    //       << endl;
1464    //   cout << "(if you use it, remember to delete the PamTrack object)" << endl;
1465    //   PamTrack *track = 0;
1466    
1467    //   if (itrk >= 0 && itrk < trk2_obj->TrkLevel2::ntrk()) {
1468    
1469    //     TrkTrack *t = trk2_obj->TrkLevel2::GetStoredTrack(itrk);
1470    //     track = GetPamTrackAlong(t);
1471    
1472    //   }
1473    //   else {
1474    //     cout << "PamLevel2::GetStoredTrack(int) : tracker track SeqNo " << itrk << " does not exist (GetNTracks() = "
1475    //         << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
1476    //   };
1477    
1478    //   return track;
1479    
1480    // }
1481  //--------------------------------------  //--------------------------------------
1482  //  //
1483    
1484  /**  /**
1485   * 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.
1486   **/   **/
1487  void PamLevel2::SortTracks(TString how){  void PamLevel2::SortTracks(TString how) {
1488    printf(" WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",how.Data());    printf(
1489    howtosort = how;          " WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",
1490          how.Data());
1491      howtosort = how;
1492    SortTracks();    SortTracks();
1493  };  }
1494    ;
1495    
1496  //  //
1497  //--------------------------------------  //--------------------------------------
1498  /**  /**
1499   * Sort physical (tracker) tracks and stores them in the TRefArray (of TrkTrack objects) which pointer is  PamLevel2::sorted_tracks.   * Sort physical (tracker) tracks.
1500   * @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...).
1501   * Sorting cryteria:   * Sorting cryteria:
1502   * TRK: lower chi**2   * TRK: lower chi**2
1503   * CAL: lower Y spatial residual on the first calorimeter plane   * CAL: lower Y spatial residual on the first calorimeter plane
1504   * 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).
1505     * S1: (ask Emiliano)
1506     * S2: (ask Emiliano)
1507     * S3: (ask Emiliano)
1508     * GP: more GP hits
1509   * The default sorting cryterium is "TOF+CAL".   * The default sorting cryterium is "TOF+CAL".
1510   *   *
1511   * 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).
1512   */   */
1513  void PamLevel2::SortTracks(){  void PamLevel2::SortTracks() {
1514    
1515      //Check if the current event has already been sorted
1516      if (issorted && lastsorted == GetReadEntry()) {
1517        return;
1518      }
1519      //Reset the sort flags, just in case something will go wrong...
1520      issorted = false;
1521      lastsorted = -1;
1522    
1523    TString how = howtosort;    TString how = howtosort;
1524    
1525    //    cout <<" PamLevel2::SortTracks(TString how) "<<endl;    //  cout <<" PamLevel2::SortTracks(TString how) "<<endl;
1526    if( !trk2_obj ){    if (!trk2_obj) {
1527      cout << "void PamLevel2::SortTracks():  TrkLevel2 not loaded !!!";      cout << "void PamLevel2::SortTracks():  TrkLevel2 not loaded !!!";
1528      return;      return;
1529    };    };
   //    cout << "call SortTracs() "<<endl;  
1530    //Save current Object count    //Save current Object count
1531    Int_t ObjectNumber = TProcessID::GetObjectCount();    Int_t ObjectNumber = TProcessID::GetObjectCount();
1532    
1533    //    cout << "ObjectNumber  "<<ObjectNumber <<endl;    // create TCloneArrays to store tracks and its images
1534    //   if (!tsorted)
1535    //     tsorted = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
1536    //   tsorted->Clear("C+C");//Delete();
1537    //   if (!timage)
1538    //     timage = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
1539    //   timage->Clear("C+C");//Delete();
1540    
1541      if(tsorted)delete tsorted;
1542      if(timage) delete timage;
1543      tsorted = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
1544      timage = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
1545    
   //     if(!sorted_tracks)sorted_tracks = new TRefArray();  
   //     sorted_tracks->Clear();  
   //    sorted_tracks.Clear();  
1546    
   if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());  
   tsorted->Delete();  
1547    TClonesArray &ttsorted = *tsorted;    TClonesArray &ttsorted = *tsorted;
   if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());  
   timage->Delete();  
1548    TClonesArray &ttimage = *timage;    TClonesArray &ttimage = *timage;
1549    
1550    
# Line 655  void PamLevel2::SortTracks(){ Line 1555  void PamLevel2::SortTracks(){
1555    Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);    Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
1556    Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);    Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
1557    Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);    Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
1558          Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase);
1559    if( !CAL2 &&  use_CAL) use_CAL = false;    Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase);
1560    if( !TOF &&  use_TOF) use_TOF = false;    Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase);
1561          Bool_t use_GP = how.Contains("GP", TString::kIgnoreCase);
1562    if( !TRK2 ){  
1563      //  cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;    if (use_TOF) {
1564        use_S1 = true;
1565        use_S2 = true;
1566        use_S3 = true;
1567      };
1568      if (!CAL2 && use_CAL)
1569        use_CAL = false;
1570      if (!TOF) {
1571        use_TOF = false;
1572        use_S1 = false;
1573        use_S2 = false;
1574        use_S3 = false;
1575      }
1576      if (!GP) {
1577        use_GP = false;
1578      }
1579    
1580      if (!TRK2) {
1581        cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
1582      return;      return;
1583    };    };
1584    
1585    //   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;
1586      
1587    
1588    //--------------------------------------------------    //--------------------------------------------------
1589    // loop over "physical" tracks sorted by the tracker    // loop over "physical" tracks sorted by the tracker
1590    //--------------------------------------------------    //--------------------------------------------------
1591    for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){    for (Int_t i = 0; i < trk2_obj->TrkLevel2::GetNTracks(); i++) {
1592            
1593      TrkTrack   *ts = 0;      TrkTrack *ts = 0;
1594      CaloTrkVar *cs = 0;      CaloTrkVar *cs = 0;
1595      ToFTrkVar  *os = 0;      ToFTrkVar *os = 0;
1596                OrbitalInfoTrkVar *rs = 0;
1597    
1598      // get tracker tracks      // get tracker tracks
1599      TrkTrack   *tp = trk2_obj->TrkLevel2::GetTrack(i); //tracker      TrkTrack *tp = trk2_obj->TrkLevel2::GetTrack(i); //tracker
1600      CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());      CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());
1601      ToFTrkVar  *op = GetToFStoredTrack(tp->GetSeqNo());      ToFTrkVar *op = GetToFStoredTrack(tp->GetSeqNo());
1602        OrbitalInfoTrkVar *rp = GetOrbitalInfoStoredTrack(tp->GetSeqNo());
1603    
1604      TrkTrack   *ti = 0; //tracker (image)      TrkTrack *ti = 0; //tracker (image)
1605      CaloTrkVar *ci = 0;      CaloTrkVar *ci = 0;
1606      ToFTrkVar  *oi = 0;      ToFTrkVar *oi = 0;
1607        OrbitalInfoTrkVar *ri = 0;
1608      //  cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;      //  cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
1609      // if track has an image, check image selection      // if track has an image, check image selection
1610      if(tp->HasImage()){  
1611                    Int_t tp_score = 0; //main track sorted by the tracker
1612        ti = trk2_obj->TrkLevel2::GetTrackImage(i);              //tracker (image)      Int_t ti_score = 0; //image track
1613        Int_t totp_score = 0; //main track sorted by the tracker
1614        Int_t toti_score = 0; //image track
1615    
1616        if (tp->HasImage()) {
1617    
1618          ti = trk2_obj->TrkLevel2::GetTrackImage(i); //tracker (image)
1619        ci = GetCaloStoredTrack(ti->GetSeqNo());        ci = GetCaloStoredTrack(ti->GetSeqNo());
1620        oi = GetToFStoredTrack(ti->GetSeqNo());        oi = GetToFStoredTrack(ti->GetSeqNo());
1621                      ri = GetOrbitalInfoStoredTrack(ti->GetSeqNo());
1622    
1623        //            cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;        //            cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
1624    
1625        //assign starting scores        //assign starting scores
1626        Int_t tp_score = 0;  //main track sorted by the tracker        tp_score = 0; //main track sorted by the tracker
1627        Int_t ti_score = 0;  //image track        ti_score = 0; //image track
1628                
1629          // -----------------------------------------------------------------------------------------
1630          // *****************************************************************************************
1631        // -----------------------------------------------------------------------------------------        // -----------------------------------------------------------------------------------------
1632        // calorimeter check        // calorimeter check
1633        // -----------------------------------------------------------------------------------------        // -----------------------------------------------------------------------------------------
1634        // check the Y spatial residual on the first calorimeter plane        if (use_CAL && !calo2_obj) {
1635        // (cut on calorimeter variables from Emiliano)          cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but CaloLevel2 not loaded !!!";
1636        if( use_CAL && !calo2_obj ){          return;
         cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but CaloLevel2 not loaded !!!";  
         return;  
1637        };        };
1638        if(        if (use_CAL && !cp && ci) {
1639           use_CAL            &&          ti_score++;
1640  //       calo2_obj->npcfit[1] > 5     &&   //no. of fit planes on Y view          toti_score++;
1641  //       calo2_obj->varcfit[1] < 1000. &&  //fit variance on Y view        };
1642           cp && ci &&        if (use_CAL && cp && !ci) {
1643           true){          tp_score++;
1644            totp_score++;
1645                          };
1646  //      Float_t resy_p = cp->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_p < 0)resy_p= - resy_p;        if (use_CAL && cp && ci && true) {
 //      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++;  
           
1647    
1648            if( cp->npresh > ci->npresh) tp_score++;          if (cp->npresh > ci->npresh && true) {
1649            if( cp->npresh < ci->npresh) ti_score++;            tp_score++;
1650            else ;//niente            totp_score++;
1651            };
1652            if (cp->npresh < ci->npresh && true) {
1653              ti_score++;
1654              toti_score++;
1655            };
1656    
1657          //      cout << "CALO "<<tp_score<<ti_score<<endl;          //      cout << "CALO "<<tp_score<<ti_score<<endl;
1658    
1659        };        };
1660        // -----------------------------------------------------------------------------------------        // -----------------------------------------------------------------------------------------
1661          // *****************************************************************************************
1662          // -----------------------------------------------------------------------------------------
1663        // TOF check        // TOF check
1664        // -----------------------------------------------------------------------------------------        // -----------------------------------------------------------------------------------------
1665        // check the number of hit pmts along the track        // check the number of hit pmts along the track
1666        // on S12 S21 and S32, where paddles are parallel to Y axis        // on S12 S21 and S32, where paddles are parallel to Y axis
1667        if( use_TOF && !tof_obj ){        if ((use_TOF || use_S1 || use_S2 || use_S3) && !tof2_obj) {
1668          cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but ToFLevel2 not loaded !!!";          cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but ToFLevel2 not loaded !!!";
1669          return;          return;
1670        };        };
1671        if( use_TOF && op && oi ){        //
1672                          if ((use_TOF || use_S1 || use_S2 || use_S3) && !op && oi) {
1673          //          ti_score++;
1674          Float_t sen = 0.;          toti_score++;
1675          for (Int_t ih=0; ih < op->npmtadc; ih++){        };
1676            Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );        if ((use_TOF || use_S1 || use_S2 || use_S3) && op && !oi) {
1677            if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (op->dedx).At(ih);          tp_score++;
1678          };          totp_score++;
1679          for (Int_t ih=0; ih < oi->npmtadc; ih++){        };
1680            Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );        if ((use_TOF || use_S1 || use_S2 || use_S3) && op && oi) {
1681            if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (oi->dedx).At(ih);          //
1682          };          Float_t sen = 0.;
1683          //          for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1684          if (  sen >= sortthr ){            Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1685            //printf(" IS A NUCLEUS! en = %f \n",sen);            if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
1686            //              sen += (op->dedx).At(ih);
1687            // is a nucleus use a different algorithm          };
1688            //          for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1689            Int_t nz = 6; Float_t zin[6];                                          // << define TOF z-coordinates            Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1690            for(Int_t ip=0; ip<nz; ip++)            if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
1691              zin[ip] = tof_obj->ToFLevel2::GetZTOF(tof_obj->ToFLevel2::GetToFPlaneID(ip));     // << read ToF plane z-coordinates              sen += (oi->dedx).At(ih);
1692            Trajectory *tr = new Trajectory(nz,zin);          };
1693            //          //
1694            Int_t nphit_p =0;          if (sen >= sortthr && false) { // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!)
1695            Int_t nphit_i =0;            //printf(" IS A NUCLEUS! en = %f \n",sen);
1696            Float_t enhit_p = 0.;            //
1697            Float_t enhit_i = 0.;            // is a nucleus use a different algorithm
1698            //            //
1699            for (Int_t ih=0; ih < op->npmtadc; ih++){            Int_t nz = 6;
1700              Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );            Float_t zin[6]; // << define TOF z-coordinates
1701              if(pl == 1 || pl == 2 || pl == 5){            for (Int_t ip = 0; ip < nz; ip++)
1702                nphit_p++;              zin[ip] = tof2_obj->ToFLevel2::GetZTOF(tof2_obj->ToFLevel2::GetToFPlaneID(ip)); // << read ToF plane z-coordinates
1703                enhit_p += (op->dedx).At(ih);            Trajectory *tr = new Trajectory(nz, zin);
1704              };            //
1705            };            Int_t nphit_p = 0;
1706            //            Int_t nphit_i = 0;
1707            tp->DoTrack2(tr);            Float_t enhit_p = 0.;
1708            //            Float_t enhit_i = 0.;
1709            if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){            //
1710              for (Int_t ih=0; ih < op->npmtadc; ih++){            for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1711                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );              Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1712                if(pl == 0){              if (pl == 1 || pl == 2 || pl == 5) {
1713                  nphit_p++;                nphit_p++;
1714                  enhit_p += (op->dedx).At(ih);                enhit_p += (op->dedx).At(ih);
1715                };              };
1716              };            };
1717            };            //
1718            if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){            tp->DoTrack2(tr);
1719              for (Int_t ih=0; ih < op->npmtadc; ih++){            //
1720                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );            if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
1721                if(pl == 3){              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1722                  nphit_p++;                Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1723                  enhit_p += (op->dedx).At(ih);                if (pl == 0) {
1724                };                  nphit_p++;
1725              };                  enhit_p += (op->dedx).At(ih);
1726            };                };
1727            if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){              };
1728              for (Int_t ih=0; ih < op->npmtadc; ih++){            };
1729                Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );            if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
1730                if(pl == 4){              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1731                  nphit_p++;                Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1732                  enhit_p += (op->dedx).At(ih);                if (pl == 3) {
1733                };                  nphit_p++;
1734              };                  enhit_p += (op->dedx).At(ih);
1735            };                };
1736                };
1737            for (Int_t ih=0; ih < oi->npmtadc; ih++){            };
1738              Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );            if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
1739              if(pl == 1 || pl == 2 || pl == 5){              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1740                nphit_i++;                        Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1741                enhit_i += (op->dedx).At(ih);                if (pl == 4) {
1742              };                  nphit_p++;
1743            };                  enhit_p += (op->dedx).At(ih);
1744            //                };
1745            ti->DoTrack2(tr);              };
1746            //            };
1747            if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){  
1748              for (Int_t ih=0; ih < oi->npmtadc; ih++){            for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1749                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );              Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1750                if(pl == 0){              if (pl == 1 || pl == 2 || pl == 5) {
1751                  nphit_i++;                nphit_i++;
1752                  enhit_i += (op->dedx).At(ih);                enhit_i += (op->dedx).At(ih);
1753                };              };
1754              };            };
1755            };            //
1756            if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){            ti->DoTrack2(tr);
1757              for (Int_t ih=0; ih < oi->npmtadc; ih++){            //
1758                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );            if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
1759                if(pl == 3){              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1760                  nphit_i++;                Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1761                  enhit_i += (op->dedx).At(ih);                if (pl == 0) {
1762                };                  nphit_i++;
1763              };                  enhit_i += (op->dedx).At(ih);
1764            };                };
1765            if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){              };
1766              for (Int_t ih=0; ih < oi->npmtadc; ih++){            };
1767                Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );            if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
1768                if(pl == 4){              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1769                  nphit_i++;                Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1770                  enhit_i += (op->dedx).At(ih);                if (pl == 3) {
1771                };                  nphit_i++;
1772              };                  enhit_i += (op->dedx).At(ih);
1773            };                            };
1774                };
1775                              };
1776            if(            if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
1777               use_TOF            &&              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1778               (nphit_p+nphit_i) !=0 &&                  Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1779               true){                if (pl == 4) {
1780                                        nphit_i++;
1781              //      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);                  enhit_i += (op->dedx).At(ih);
1782              //      printf(" score p %i score i %i \n",tp_score,ti_score);                };
1783              //            if( enhit_p > enhit_i ) tp_score++;              };
1784              //            if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;            };
1785              if ( nphit_p > nphit_i ) tp_score++;  
1786              if ( nphit_p < nphit_i ) ti_score++;            if ((use_TOF || use_S1 || use_S2 || use_S3) && (nphit_p + nphit_i) != 0 && true) {
1787              if ( nphit_p == nphit_i ){  
1788                if ( enhit_p > enhit_i ) tp_score++;              //      printf(" seqno %i nphit_p %i nphit_i %i enhit_p %f enhit_i %f \n",trk2_obj->TrkLevel2::GetSeqNo(i),nphit_p,nphit_i,enhit_p,enhit_i);
1789                else ti_score++;              //      printf(" score p %i score i %i \n",tp_score,ti_score);
1790              };              //            if( enhit_p > enhit_i ) tp_score++;
1791              //      printf(" dopo score p %i score i %i \n",tp_score,ti_score);              //            if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
1792            };              if (nphit_p > nphit_i)
1793            delete tr;                tp_score++;
1794            //              if (nphit_p < nphit_i)
1795          } else {                ti_score++;
1796            //              if (nphit_p == nphit_i) {
1797            // NOT a NUCLEUS                if (enhit_p > enhit_i)
1798            //                  tp_score++;
1799            //printf(" NOT a NUCLEUS! en = %f \n",sen);                else
1800                    ti_score++;
1801            Int_t nphit_p =0;              };
1802            Int_t nphit_i =0;              //      printf(" dopo score p %i score i %i \n",tp_score,ti_score);
1803                              };
1804                              delete tr;
1805            /*                            cout << "track: npmtadc "<< op->npmtadc << endl;            //
1806              cout << "track: npmttdc "<< op->npmttdc << endl;          }
1807              cout << "image: npmtadc "<< oi->npmtadc << endl;          else {
1808              cout << "image: npmttdc "<< oi->npmttdc << endl;*/            // -------------
1809                              // NOT a NUCLEUS
1810  //        for (Int_t ih=0; ih < op->npmtadc; ih++){            // -------------
1811  //          Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );            //printf(" NOT a NUCLEUS! en = %f \n",sen);
1812  //          if(pl == 1 || pl == 2 || pl == 5)nphit_p++;  
1813  //        };            Int_t nphit_p = 0;
1814                              Int_t nphit_i = 0;
1815  //        for (Int_t ih=0; ih < oi->npmtadc; ih++){  
1816  //          Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );            /*                            cout << "track: npmtadc "<< op->npmtadc << endl;
1817  //          if(pl == 1 || pl == 2 || pl == 5)nphit_i++;             cout << "track: npmttdc "<< op->npmttdc << endl;
1818  //        };             cout << "image: npmtadc "<< oi->npmtadc << endl;
1819            // --- modified to count tdc signals (more efficient?)             cout << "image: npmttdc "<< oi->npmttdc << endl;*/
1820            // --- and to implement check on tdcflag  
1821            for (Int_t ih=0; ih < op->npmttdc; ih++){            //      for (Int_t ih=0; ih < op->npmtadc; ih++){
1822              //      Int_t pl = tof_obj->GetPlaneIndex( (op->pmttdc).At(ih) );            //        Int_t pl = tof2_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1823  //          if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;            //        if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
1824              if( (op->tdcflag).At(ih)==0 )nphit_p++;            //      };
1825            };  
1826                              //      for (Int_t ih=0; ih < oi->npmtadc; ih++){
1827            for (Int_t ih=0; ih < oi->npmttdc; ih++){            //        Int_t pl = tof2_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1828              //      Int_t pl = tof_obj->GetPlaneIndex( (oi->pmttdc).At(ih) );            //        if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
1829  //          if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;              //      };
1830              if( (oi->tdcflag).At(ih)==0 )nphit_i++;                // --- modified to count tdc signals (more efficient?)
1831            };            // --- and to implement check on tdcflag
1832                              for (Int_t ih = 0; ih < op->npmttdc; ih++) {
1833            if(              Int_t pl = tof2_obj->GetPlaneIndex((op->pmttdc).At(ih));
1834               (nphit_p+nphit_i) !=0 &&                //      if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
1835               true){              if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
1836                                        || (use_S3 && (pl == 4 || pl == 5))) {
1837              if     ( nphit_p > nphit_i) tp_score++;                if ((op->tdcflag).At(ih) == 0)
1838              else if( nphit_p < nphit_i) ti_score++;                  nphit_p++;
1839              else ;//niente              };
1840            };            };
1841          };  
1842          //      cout << "TOF "<<tp_score<<ti_score<<endl;            for (Int_t ih = 0; ih < oi->npmttdc; ih++) {
1843                Int_t pl = tof2_obj->GetPlaneIndex((oi->pmttdc).At(ih));
1844                //      if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;
1845                if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
1846                    || (use_S3 && (pl == 4 || pl == 5))) {
1847                  if ((oi->tdcflag).At(ih) == 0)
1848                    nphit_i++;
1849                };
1850              };
1851    
1852              if ((nphit_p + nphit_i) != 0 && true) {
1853    
1854                if (nphit_p != nphit_i) {
1855                  totp_score += nphit_p;
1856                  toti_score += nphit_i;
1857                  tp_score += nphit_p;
1858                  ti_score += nphit_i;
1859                };
1860                //      if     ( nphit_p > nphit_i) tp_score+=nphit_p;
1861                //      else if( nphit_p < nphit_i) ti_score+=nphit_i;
1862                //      else ;//niente
1863              };
1864            };
1865            //      cout << "TOF "<<tp_score<<ti_score<<endl;
1866        };        };
1867    
1868          //      if(tp_score == ti_score) use_TRK = true;
 //      if(tp_score == ti_score) use_TRK = true;  
1869    
1870    
1871        // -----------------------------------------------------------------------------------------        // -----------------------------------------------------------------------------------------
1872          // *****************************************************************************************
1873          // -----------------------------------------------------------------------------------------
1874        // tracker check        // tracker check
1875        // -----------------------------------------------------------------------------------------        // -----------------------------------------------------------------------------------------
1876        // chi**2 difference is not always large enough to distinguish among        // chi**2 difference is not always large enough to distinguish among
1877        // the real track and its image.        // the real track and its image.
1878        // 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.
1879        // *** MODIFIED ON AUGUST 2007 ***        // *** MODIFIED ON AUGUST 2007 ***
1880        if(use_TRK){        if (use_TRK) {
1881  //      if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;          //      if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
1882  //      else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;          //      else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
1883    
1884            // CHECK 1 : number of points along X            // CHECK 1 : number of points along X
1885            if ( tp->GetNX() >= ti->GetNX() )tp_score++ ;          if (tp->GetNX() >= ti->GetNX()) {
1886            if ( tp->GetNX() <= ti->GetNX() )ti_score++ ;            tp_score++;
1887            // CHECK 2 : number of points along Y              totp_score++;
1888            if ( tp->GetNY() >= ti->GetNY() )tp_score++ ;          };
1889            if ( tp->GetNY() <= ti->GetNY() )ti_score++ ;          if (tp->GetNX() <= ti->GetNX()) {
1890            // CHECK 3 : chi**2 along X              ti_score++;
1891  //        if(      tp->GetChi2X() > 0 && (tp->GetChi2X() < ti->GetChi2X() || ti->GetChi2X() <=0) ) tp_score++ ;            toti_score++;
1892  //        if(      ti->GetChi2X() > 0 && (ti->GetChi2X() < tp->GetChi2X() || tp->GetChi2X() <=0) ) ti_score++ ;          };
1893            // CHECK 4 : chi**2 along Y            // CHECK 2 : number of points along Y
1894  //        if(      tp->GetChi2Y() > 0 && (tp->GetChi2Y() < ti->GetChi2Y() || ti->GetChi2Y() <=0) ) tp_score++ ;          if (tp->GetNY() >= ti->GetNY()) {
1895  //        if(      ti->GetChi2Y() > 0 && (ti->GetChi2Y() < tp->GetChi2Y() || tp->GetChi2Y() <=0) ) ti_score++ ;            tp_score++;
1896            // CHECK 5 : total chi**2              totp_score++;
1897  //        if(      tp->chi2 > 0 && (tp->chi2 < ti->chi2 || ti->chi2 <=0) ) tp_score++ ;          };
1898  //        if(      ti->chi2 > 0 && (ti->chi2 < tp->chi2 || tp->chi2 <=0) ) ti_score++ ;          if (tp->GetNY() <= ti->GetNY()) {
1899              ti_score++;
1900              toti_score++;
1901            };
1902    
1903          //      cout << "TRK "<<tp_score<<ti_score<<endl;          //      cout << "TRK "<<tp_score<<ti_score<<endl;
1904        };        };
               
1905    
1906          // -----------------------------------------------------------------------------------------
1907          // *****************************************************************************************
1908          // -----------------------------------------------------------------------------------------
1909          // GPamela check
1910          // -----------------------------------------------------------------------------------------
1911    
1912          //---------------------------------------------------
1913          // count the number of GP hits
1914          //---------------------------------------------------
1915          if (use_GP) {
1916            int ngphits_p = 0;
1917            int ngphits_i = 0;
1918            float toll = 0.02; //200 micron
1919            for (int ih = 0; ih < GetGPamela()->Nthspe; ih++) {
1920              int ip = (Int_t) GetGPamela()->Itrpb[ih] - 1;
1921              if (tp && tp->YGood(ip) && fabs(tp->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
1922                ngphits_p++;
1923              if (ti && ti->YGood(ip) && fabs(ti->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
1924                ngphits_i++;
1925            }
1926            if (ngphits_p > ngphits_i && true) {
1927              tp_score++;
1928              totp_score++;
1929            }
1930            if (ngphits_p < ngphits_i && true) {
1931              ti_score++;
1932              toti_score++;
1933            }
1934          }
1935    
1936        // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*        // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1937        // the winner is....        // the winner is....
1938        // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*        // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1939        if      (tp_score > ti_score) {        if (tp_score > ti_score) {
           
1940    
1941        }else if (tp_score < ti_score) {        }
1942          else if (tp_score < ti_score) {
1943    
1944            ts = ti;//its image!!
1945            cs = ci;
1946            os = oi;
1947            rs = ri;
1948            Int_t totis = toti_score;
1949    
1950            ti = tp;//its image!!
1951            ci = cp;
1952            oi = op;
1953            ri = rp;
1954    
1955            tp = ts;//its image!!
1956            cp = cs;
1957            op = os;
1958            rp = rs;
1959    
1960          ts = ti;//its image!!          toti_score = totp_score;
1961          cs = ci;          totp_score = totis;
         os = oi;  
1962    
1963          ti = tp;//its image!!        }
1964          ci = cp;        else {
         oi = op;  
1965    
1966          tp = ts;//its image!!          //      cout << "Warning - track image ambiguity not solved" << endl;
         cp = cs;  
         op = os;  
1967    
1968                          };
       }else {  
1969    
1970  //      cout << "Warning - track image ambiguity not solved" << endl;      }
1971        else {
1972          totp_score = 1;
1973          toti_score = 0;
1974    
       };  
               
     }else{  
1975        //            ts = tp;        //            ts = tp;
1976        //            cs = cp;        //            cs = cp;
1977        //            os = op;        //            os = op;
1978      };      };
1979            
1980      //  cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;      //  cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
1981      //  sorted_tracks->Add(ts);//save the track in the sorted array      //  sorted_tracks->Add(ts);//save the track in the sorted array
1982      //  sorted_tracks.Add(ts);//save the track in the sorted array      //  sorted_tracks.Add(ts);//save the track in the sorted array
# Line 990  void PamLevel2::SortTracks(){ Line 1985  void PamLevel2::SortTracks(){
1985      //  cout<<"o "<<tp<<endl;      //  cout<<"o "<<tp<<endl;
1986      //  cout<<"o "<<cp<<endl;      //  cout<<"o "<<cp<<endl;
1987      //  cout<<"o "<<op<<endl;      //  cout<<"o "<<op<<endl;
1988      new(ttsorted[i]) PamTrack(tp,cp,op);  
1989      new(ttimage[i])  PamTrack(ti,ci,oi);  //     cout <<"old p:"<< tp<<"--"<<cp<<"--"<<op<<"--"<<rp<<"--"<< endl;
1990    //     cout <<"old i:"<< ti<<"--"<<ci<<"--"<<oi<<"--"<<ri<<"--"<< endl;
1991    
1992        new (ttsorted[i]) PamTrack(tp, cp, op, rp);
1993        new (ttimage[i]) PamTrack(ti, ci, oi, ri);
1994    
1995        ((PamTrack*) (ttsorted[i]))->SetPScore(totp_score);
1996        ((PamTrack*) (ttsorted[i]))->SetIScore(toti_score);
1997        ((PamTrack*) (ttimage[i]))->SetPScore(totp_score);
1998        ((PamTrack*) (ttimage[i]))->SetIScore(toti_score);
1999    };    };
2000    
2001    if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){    if (tsorted->GetEntries() != trk2_obj->GetNTracks()) {
2002      cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() "<<tsorted->GetEntries()<<" != trk2_obj->GetNTracks() = "<<trk2_obj->GetNTracks() <<endl;      cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() " << tsorted->GetEntries()
2003      tsorted->Delete(); tsorted=0;          << " != trk2_obj->GetNTracks() = " << trk2_obj->GetNTracks() << endl;
2004      timage->Delete(); timage=0;      tsorted->Delete();
2005        tsorted = 0;
2006        timage->Delete();
2007        timage = 0;
2008    }    }
2009    
2010    //Restore Object count    //Restore Object count
2011    //To save space in the table keeping track of all referenced objects    //To save space in the table keeping track of all referenced objects
2012    //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.
2013    TProcessID::SetObjectCount(ObjectNumber);    TProcessID::SetObjectCount(ObjectNumber);
2014        
2015      //Everything went fine so the current event can be tagged as sorted
2016      issorted = true;
2017      lastsorted = GetReadEntry();
2018    
2019    //  cout <<" SortTracks() -- end"<<endl;
2020    
2021    }
2022    ;
2023    //
2024    //--------------------------------------
2025    /**
2026     * Sort physical (tracker) tracks.
2027     * @param how String to set the sorting cryterium (es: "CAL" or "TRK+CAL+TOF" ecc...).
2028     * Sorting cryteria:
2029     * TRK: lower chi**2
2030     * CAL: lower Y spatial residual on the first calorimeter plane
2031     * TOF: bigger numebr of hit PMTs along the track, on S12 S21 S32 (where paddles are along the Y axis).
2032     * S1: (ask Emiliano)
2033     * S2: (ask Emiliano)
2034     * S3: (ask Emiliano)
2035     * GP: more GP hits
2036     * The default sorting cryterium is "TOF+CAL".
2037     *
2038     * 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).
2039     *
2040     * New version, with handling of extended tracks and nuclei tracks.
2041     */
2042    void PamLevel2::SortTracksNew() {
2043    
2044      //--------------------------------------------------
2045      //Check if the current event has already been sorted
2046      //--------------------------------------------------
2047      if (issorted_new && lastsorted_new == GetReadEntry()) {
2048          return; //already done for this event
2049      }
2050    
2051    
2052    //  cout << "SORT" << endl;
2053    
2054      //Reset the sort flags, just in case something will go wrong...
2055      issorted_new = false;
2056      lastsorted_new = -1;
2057    
2058      //--------------------------------------------------
2059      // set input variables
2060      //--------------------------------------------------
2061    
2062      TString how = howtosort;
2063      //Save current Object count
2064      Int_t ObjectNumber = TProcessID::GetObjectCount();
2065    
2066    
2067    
2068    
2069      TrkLevel2  * trk2 ;
2070      CaloLevel2 * calo2;
2071      ToFLevel2  * tof2  ;
2072      OrbitalInfo  * orb2 ;
2073      
2074      TClonesArray * trkext ;
2075      TClonesArray * caloext;
2076      TClonesArray * tofext ;
2077      TClonesArray * orbext ;
2078    
2079    
2080    //   cout << "trk2_obj" << trk2_obj<< endl;
2081    //   cout << "trk2_nuc_obj" << trk2_nuc_obj<< endl;
2082    //   cout << " trk_ext_obj "<<trk_ext_obj<< endl;
2083    //   cout << " trk_ext_nuc_obj "<<trk_ext_nuc_obj<< endl;
2084    
2085      //-----------------------------------------------------------
2086      // create/reset TCloneArrays to store tracks and their images
2087      //-----------------------------------------------------------
2088    
2089    //  cout << " PamLevel2::SortTracksNew() --- Clear TClonesArray objects"<<endl;
2090      
2091      // main tracks from standard alg
2092    //   if (!tsorted)
2093    //       tsorted = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
2094    //   tsorted->Clear("C+C");//Delete();
2095    //   // track images from standard alg
2096    //   if (!timage)
2097    //       timage = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
2098    //   timage->Clear("C+C");//Delete();
2099    //   // tracks from extended algorythm
2100    //   if(EXT && !text)
2101    //       text = new TClonesArray("PamTrack",trk_ext_obj->GetEntries());
2102    //   if(text)text->Clear("C+C");//Delete();
2103    
2104      if(tsorted)delete tsorted;
2105      if(timage) delete timage;
2106      if(text)   delete text;
2107      tsorted = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
2108      timage = new TClonesArray("PamTrack", trk2_obj->GetNTracks());
2109      text = new TClonesArray("PamTrack",trk_ext_obj->GetEntries());
2110    
2111      //-----------------------------------------------------------
2112      // create/reset TCloneArrays to store tracks and their images
2113      //-----------------------------------------------------------
2114      if(NUC){
2115    
2116          
2117          if(tsorted_nuc)delete tsorted_nuc;
2118          if(timage_nuc) delete timage_nuc;
2119          if(text_nuc)   delete text_nuc;
2120          tsorted_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks());
2121          timage_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks());
2122          text_nuc = new TClonesArray("PamTrack",trk_ext_nuc_obj->GetEntries());
2123    
2124      // main tracks from standard alg
2125    //       if (!tsorted_nuc)
2126    //        tsorted_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks());
2127    //       tsorted_nuc->Clear("C+C");//Delete();
2128    //       // track images from standard alg
2129    //       if (!timage_nuc)
2130    //        timage_nuc = new TClonesArray("PamTrack", trk2_nuc_obj->GetNTracks());
2131    //       timage_nuc->Clear("C+C");//Delete();
2132    //       // tracks from extended algorythm
2133    //       if(EXT && !text_nuc)
2134    //        text_nuc = new TClonesArray("PamTrack",trk_ext_nuc_obj->GetEntries());
2135    //       if(text_nuc)text_nuc->Clear("C+C");//Delete();
2136    
2137      }
2138      //--------------------------------------------------
2139      // retrieve sorting method
2140      //--------------------------------------------------
2141      Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
2142      Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
2143      Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
2144      Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase);
2145      Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase);
2146      Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase);
2147      Bool_t use_GP = how.Contains("GP", TString::kIgnoreCase);
2148    
2149      if (use_TOF) {
2150        use_S1 = true;
2151        use_S2 = true;
2152        use_S3 = true;
2153      };
2154      if (!CAL2 && use_CAL)
2155        use_CAL = false;
2156      if (!TOF) {
2157        use_TOF = false;
2158        use_S1 = false;
2159        use_S2 = false;
2160        use_S3 = false;
2161      }
2162      if (!GP) {
2163        use_GP = false;
2164      }
2165    
2166      if (!TRK2) {
2167        cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
2168        return;
2169      };
2170    
2171    
2172      ///////////////////////////////////////////////////////////////////////////////////
2173      //
2174      //   sort tracks and fill PamTrack arrays
2175      //
2176      ///////////////////////////////////////////////////////////////////////////////////
2177      for(int doit=0; doit<2; doit++){
2178    
2179    //       cout << "doit "<<doit<<endl;
2180    
2181    
2182          if(doit == 0){
2183    
2184              trk2  = (TRK2 ? trk2_obj: NULL);;
2185              calo2 = (CAL2 ? calo2_obj: NULL);;
2186              tof2  = (TOF  ?tof2_obj: NULL);;
2187              orb2  = (ORB ? orb2_obj: NULL);;
2188          
2189              trkext = (TRK2 ? trk_ext_obj: NULL);;
2190              caloext = (CAL2 ? calo_ext_obj: NULL);;
2191              tofext = (TOF  ?tof_ext_obj: NULL);;
2192              orbext = (ORB ? orb_ext_obj: NULL);;
2193    
2194          }else if (doit == 1){
2195    
2196              if(!NUC)break;
2197    
2198          
2199              trk2  = (TRK2 ?trk2_nuc_obj: NULL);;
2200              calo2 = (CAL2 ? calo2_nuc_obj: NULL);;
2201              tof2  = (TOF  ?tof2_nuc_obj: NULL);;
2202              orb2  = (ORB ? orb2_nuc_obj: NULL);;
2203          
2204              trkext = (TRK2 ?trk_ext_nuc_obj: NULL);;
2205              caloext = (CAL2 ? calo_ext_nuc_obj: NULL);;
2206              tofext = (TOF  ?tof_ext_nuc_obj: NULL);;
2207              orbext = (ORB ? orb_ext_nuc_obj: NULL);;
2208          
2209          
2210          
2211    
2212          }
2213    
2214    //       cout << "trk2" << trk2<<endl;
2215    //       cout << "calo2" << calo2<<endl;
2216    //       cout << "tof2" << tof2<<endl;
2217    //       cout << "orb2" << orb2<<endl;
2218    //       cout << "trkext" <<trkext <<endl;
2219    //       cout << "tofext" << tofext<<endl;
2220    //       cout << "caloext" << caloext<<endl;
2221    //       cout << "orbext" << orbext<<endl;
2222    
2223          TClonesArray &ttext    = (doit==0 ? *text : *text_nuc);
2224          TClonesArray &ttimage  = (doit==0 ? *timage : *timage_nuc);
2225          TClonesArray &ttsorted = (doit==0 ? *tsorted : *tsorted_nuc);
2226      
2227    //       cout << "tsorted + timage "<<doit<<endl;
2228    
2229          //--------------------------------------------------
2230          // loop over "physical" tracks sorted by the tracker
2231          //--------------------------------------------------
2232          for (Int_t i = 0; i < trk2->TrkLevel2::GetNTracks(); i++) {
2233    
2234              TrkTrack *ts = 0;
2235              CaloTrkVar *cs = 0;
2236              ToFTrkVar *os = 0;
2237              OrbitalInfoTrkVar *rs = 0;
2238    
2239              // get tracker tracks
2240              TrkTrack *tp          = (TRK2 ? trk2->GetTrack(i): NULL); //tracker
2241              CaloTrkVar *cp        = (CAL2 ? calo2->GetCaloStoredTrack(tp->GetSeqNo()) : NULL);
2242              ToFTrkVar *op         = (TOF  ? tof2->GetToFStoredTrack(tp->GetSeqNo())   : NULL);
2243              OrbitalInfoTrkVar *rp = (ORB ? orb2->GetOrbitalInfoStoredTrack(tp->GetSeqNo()) : NULL);
2244    
2245              cout << "ORB="<<ORB<<" rp="<<rp<<endl;
2246    
2247              TrkTrack *ti = 0; //tracker (image)
2248              CaloTrkVar *ci = 0;
2249              ToFTrkVar *oi = 0;
2250              OrbitalInfoTrkVar *ri = 0;
2251              //    cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
2252              // if track has an image, check image selection
2253    
2254              Int_t tp_score = 0; //main track sorted by the tracker
2255              Int_t ti_score = 0; //image track
2256              Int_t totp_score = 0; //main track sorted by the tracker
2257              Int_t toti_score = 0; //image track
2258    
2259              if (tp->HasImage()) {
2260    
2261                  ti = (TRK2 ? trk2->GetTrackImage(i) : NULL); //tracker (image)
2262                  ci = (CAL2 ? calo2->GetCaloStoredTrack(ti->GetSeqNo()): NULL);
2263                  oi = (TOF  ? tof2->GetToFStoredTrack(ti->GetSeqNo()): NULL);
2264                  ri = (ORB ? orb2->GetOrbitalInfoStoredTrack(ti->GetSeqNo()): NULL);
2265    
2266                  //            cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
2267    
2268                  //assign starting scores
2269                  tp_score = 0; //main track sorted by the tracker
2270                  ti_score = 0; //image track
2271    
2272                  // -----------------------------------------------------------------------------------------
2273                  // *****************************************************************************************
2274                  // -----------------------------------------------------------------------------------------
2275                  // calorimeter check
2276                  // -----------------------------------------------------------------------------------------
2277                  if (use_CAL && !calo2) {
2278                      cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but CaloLevel2 not loaded !!!";
2279                      return;
2280                  };
2281                  if (use_CAL && !cp && ci) {
2282                      ti_score++;
2283                      toti_score++;
2284                  };
2285                  if (use_CAL && cp && !ci) {
2286                      tp_score++;
2287                      totp_score++;
2288                  };
2289                  if (use_CAL && cp && ci && true) {
2290    
2291                      if (cp->npresh > ci->npresh && true) {
2292                          tp_score++;
2293                          totp_score++;
2294                      };
2295                      if (cp->npresh < ci->npresh && true) {
2296                          ti_score++;
2297                          toti_score++;
2298                      };
2299    
2300                      //    cout << "CALO "<<tp_score<<ti_score<<endl;
2301    
2302                  };
2303                  // -----------------------------------------------------------------------------------------
2304                  // *****************************************************************************************
2305                  // -----------------------------------------------------------------------------------------
2306                  // TOF check
2307                  // -----------------------------------------------------------------------------------------
2308                  // check the number of hit pmts along the track
2309                  // on S12 S21 and S32, where paddles are parallel to Y axis
2310                  if ((use_TOF || use_S1 || use_S2 || use_S3) && !tof2) {
2311                      cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but ToFLevel2 not loaded !!!";
2312                      return;
2313                  };
2314                  //
2315                  if ((use_TOF || use_S1 || use_S2 || use_S3) && !op && oi) {
2316                      ti_score++;
2317                      toti_score++;
2318                  };
2319                  if ((use_TOF || use_S1 || use_S2 || use_S3) && op && !oi) {
2320                      tp_score++;
2321                      totp_score++;
2322                  };
2323                  if ((use_TOF || use_S1 || use_S2 || use_S3) && op && oi) {
2324                      //
2325                      Float_t sen = 0.;
2326                      for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2327                          Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2328                          if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
2329                              sen += (op->dedx).At(ih);
2330                      };
2331                      for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2332                          Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2333                          if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
2334                              sen += (oi->dedx).At(ih);
2335                      };
2336                      //
2337                      if (sen >= sortthr && false) { // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!)
2338                          //printf(" IS A NUCLEUS! en = %f \n",sen);
2339                          //
2340                          // is a nucleus use a different algorithm
2341                          //
2342                          Int_t nz = 6;
2343                          Float_t zin[6]; // << define TOF z-coordinates
2344                          for (Int_t ip = 0; ip < nz; ip++)
2345                              zin[ip] = tof2->GetZTOF(tof2->GetToFPlaneID(ip)); // << read ToF plane z-coordinates
2346                          Trajectory *tr = new Trajectory(nz, zin);
2347                          //
2348                          Int_t nphit_p = 0;
2349                          Int_t nphit_i = 0;
2350                          Float_t enhit_p = 0.;
2351                          Float_t enhit_i = 0.;
2352                          //
2353                          for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2354                              Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2355                              if (pl == 1 || pl == 2 || pl == 5) {
2356                                  nphit_p++;
2357                                  enhit_p += (op->dedx).At(ih);
2358                              };
2359                          };
2360                          //
2361                          tp->DoTrack2(tr);
2362                          //
2363                          if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
2364                              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2365                                  Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2366                                  if (pl == 0) {
2367                                      nphit_p++;
2368                                      enhit_p += (op->dedx).At(ih);
2369                                  };
2370                              };
2371                          };
2372                          if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
2373                              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2374                                  Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2375                                  if (pl == 3) {
2376                                      nphit_p++;
2377                                      enhit_p += (op->dedx).At(ih);
2378                                  };
2379                              };
2380                          };
2381                          if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
2382                              for (Int_t ih = 0; ih < op->npmtadc; ih++) {
2383                                  Int_t pl = tof2->GetPlaneIndex((op->pmtadc).At(ih));
2384                                  if (pl == 4) {
2385                                      nphit_p++;
2386                                      enhit_p += (op->dedx).At(ih);
2387                                  };
2388                              };
2389                          };
2390    
2391                          for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2392                              Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2393                              if (pl == 1 || pl == 2 || pl == 5) {
2394                                  nphit_i++;
2395                                  enhit_i += (op->dedx).At(ih);
2396                              };
2397                          };
2398                          //
2399                          ti->DoTrack2(tr);
2400                          //
2401                          if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
2402                              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2403                                  Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2404                                  if (pl == 0) {
2405                                      nphit_i++;
2406                                      enhit_i += (op->dedx).At(ih);
2407                                  };
2408                              };
2409                          };
2410                          if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
2411                              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2412                                  Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2413                                  if (pl == 3) {
2414                                      nphit_i++;
2415                                      enhit_i += (op->dedx).At(ih);
2416                                  };
2417                              };
2418                          };
2419                          if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
2420                              for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
2421                                  Int_t pl = tof2->GetPlaneIndex((oi->pmtadc).At(ih));
2422                                  if (pl == 4) {
2423                                      nphit_i++;
2424                                      enhit_i += (op->dedx).At(ih);
2425                                  };
2426                              };
2427                          };
2428    
2429                          if ((use_TOF || use_S1 || use_S2 || use_S3) && (nphit_p + nphit_i) != 0 && true) {
2430    
2431                              //        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);
2432                              //        printf(" score p %i score i %i \n",tp_score,ti_score);
2433                              //              if( enhit_p > enhit_i ) tp_score++;
2434                              //              if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
2435                              if (nphit_p > nphit_i)
2436                                  tp_score++;
2437                              if (nphit_p < nphit_i)
2438                                  ti_score++;
2439                              if (nphit_p == nphit_i) {
2440                                  if (enhit_p > enhit_i)
2441                                      tp_score++;
2442                                  else
2443                                      ti_score++;
2444                              };
2445                              //        printf(" dopo score p %i score i %i \n",tp_score,ti_score);
2446                          };
2447                          delete tr;
2448                          //
2449                      }
2450                      else {
2451                          // -------------
2452                          // NOT a NUCLEUS
2453                          // -------------
2454                          //printf(" NOT a NUCLEUS! en = %f \n",sen);
2455    
2456                          Int_t nphit_p = 0;
2457                          Int_t nphit_i = 0;
2458    
2459                          /*                                cout << "track: npmtadc "<< op->npmtadc << endl;
2460                            cout << "track: npmttdc "<< op->npmttdc << endl;
2461                            cout << "image: npmtadc "<< oi->npmtadc << endl;
2462                            cout << "image: npmttdc "<< oi->npmttdc << endl;*/
2463    
2464                          //          for (Int_t ih=0; ih < op->npmtadc; ih++){
2465                          //            Int_t pl = tof2_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
2466                          //            if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
2467                          //          };
2468    
2469                          //          for (Int_t ih=0; ih < oi->npmtadc; ih++){
2470                          //            Int_t pl = tof2_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
2471                          //            if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
2472                          //          };
2473                          // --- modified to count tdc signals (more efficient?)
2474                          // --- and to implement check on tdcflag
2475                          for (Int_t ih = 0; ih < op->npmttdc; ih++) {
2476                              Int_t pl = tof2->GetPlaneIndex((op->pmttdc).At(ih));
2477                              //        if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
2478                              if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
2479                                  || (use_S3 && (pl == 4 || pl == 5))) {
2480                                  if ((op->tdcflag).At(ih) == 0)
2481                                      nphit_p++;
2482                              };
2483                          };
2484    
2485                          for (Int_t ih = 0; ih < oi->npmttdc; ih++) {
2486                              Int_t pl = tof2->GetPlaneIndex((oi->pmttdc).At(ih));
2487                              //        if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;
2488                              if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
2489                                  || (use_S3 && (pl == 4 || pl == 5))) {
2490                                  if ((oi->tdcflag).At(ih) == 0)
2491                                      nphit_i++;
2492                              };
2493                          };
2494    
2495                          if ((nphit_p + nphit_i) != 0 && true) {
2496    
2497                              if (nphit_p != nphit_i) {
2498                                  totp_score += nphit_p;
2499                                  toti_score += nphit_i;
2500                                  tp_score += nphit_p;
2501                                  ti_score += nphit_i;
2502                              };
2503                              //        if     ( nphit_p > nphit_i) tp_score+=nphit_p;
2504                              //        else if( nphit_p < nphit_i) ti_score+=nphit_i;
2505                              //        else ;//niente
2506                          };
2507                      };
2508                      //    cout << "TOF "<<tp_score<<ti_score<<endl;
2509                  };
2510    
2511                  //      if(tp_score == ti_score) use_TRK = true;
2512    
2513    
2514                  // -----------------------------------------------------------------------------------------
2515                  // *****************************************************************************************
2516                  // -----------------------------------------------------------------------------------------
2517                  // tracker check
2518                  // -----------------------------------------------------------------------------------------
2519                  // chi**2 difference is not always large enough to distinguish among
2520                  // the real track and its image.
2521                  // Tracker check will be applied always when calorimeter and tof information is ambiguous.
2522                  // *** MODIFIED ON AUGUST 2007 ***
2523                  if (use_TRK) {
2524                      //    if(      tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
2525                      //    else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
2526    
2527                      // CHECK 1 : number of points along X
2528                      if (tp->GetNX() >= ti->GetNX()) {
2529                          tp_score++;
2530                          totp_score++;
2531                      };
2532                      if (tp->GetNX() <= ti->GetNX()) {
2533                          ti_score++;
2534                          toti_score++;
2535                      };
2536                      // CHECK 2 : number of points along Y
2537                      if (tp->GetNY() >= ti->GetNY()) {
2538                          tp_score++;
2539                          totp_score++;
2540                      };
2541                      if (tp->GetNY() <= ti->GetNY()) {
2542                          ti_score++;
2543                          toti_score++;
2544                      };
2545    
2546                      //    cout << "TRK "<<tp_score<<ti_score<<endl;
2547                  };
2548    
2549                  // -----------------------------------------------------------------------------------------
2550                  // *****************************************************************************************
2551                  // -----------------------------------------------------------------------------------------
2552                  // GPamela check
2553                  // -----------------------------------------------------------------------------------------
2554    
2555                  //---------------------------------------------------
2556                  // count the number of GP hits
2557                  //---------------------------------------------------
2558                  if (use_GP) {
2559                      int ngphits_p = 0;
2560                      int ngphits_i = 0;
2561                      float toll = 0.02; //200 micron
2562                      for (int ih = 0; ih < GetGPamela()->Nthspe; ih++) {
2563                          int ip = (Int_t) GetGPamela()->Itrpb[ih] - 1;
2564                          if (tp && tp->YGood(ip) && fabs(tp->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
2565                              ngphits_p++;
2566                          if (ti && ti->YGood(ip) && fabs(ti->ym[ip] - GetGPamela()->Yavspe[ih]) < toll && true)
2567                              ngphits_i++;
2568                      }
2569                      if (ngphits_p > ngphits_i && true) {
2570                          tp_score++;
2571                          totp_score++;
2572                      }
2573                      if (ngphits_p < ngphits_i && true) {
2574                          ti_score++;
2575                          toti_score++;
2576                      }
2577                  }
2578    
2579                  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
2580                  // the winner is....
2581                  // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
2582                  if (tp_score > ti_score) {
2583    
2584                  }
2585                  else if (tp_score < ti_score) {
2586    
2587                      ts = ti;//its image!!
2588                      cs = ci;
2589                      os = oi;
2590                      rs = ri;
2591                      Int_t totis = toti_score;
2592    
2593                      ti = tp;//its image!!
2594                      ci = cp;
2595                      oi = op;
2596                      ri = rp;
2597    
2598                      tp = ts;//its image!!
2599                      cp = cs;
2600                      op = os;
2601                      rp = rs;
2602    
2603                      toti_score = totp_score;
2604                      totp_score = totis;
2605    
2606                  }
2607                  else {
2608    
2609                      //    cout << "Warning - track image ambiguity not solved" << endl;
2610    
2611                  };
2612    
2613              }
2614              else {
2615                  totp_score = 1;
2616                  toti_score = 0;
2617    
2618                  //            ts = tp;
2619                  //            cs = cp;
2620                  //            os = op;
2621              };
2622    
2623              //    cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
2624              //    sorted_tracks->Add(ts);//save the track in the sorted array
2625              //    sorted_tracks.Add(ts);//save the track in the sorted array
2626              //    sorted_tracks.Add(tp);//save the track in the sorted array
2627              //    cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
2628              //    cout<<"o "<<tp<<endl;
2629              //    cout<<"o "<<cp<<endl;
2630              //    cout<<"o "<<op<<endl;
2631    
2632    //        cout <<"p:"<< tp<<"--"<<cp<<"--"<<op<<"--"<<rp<<"--"<< endl;
2633    //        cout <<"i:"<< ti<<"--"<<ci<<"--"<<oi<<"--"<<ri<<"--"<< endl;
2634    
2635    
2636    
2637              new (ttsorted[i]) PamTrack(tp, cp, op, rp);
2638              new (ttimage[i]) PamTrack(ti, ci, oi, ri);
2639    
2640              ((PamTrack*) (ttsorted[i]))->SetPScore(totp_score);
2641              ((PamTrack*) (ttsorted[i]))->SetIScore(toti_score);
2642              ((PamTrack*) (ttimage[i]))->SetPScore(totp_score);
2643              ((PamTrack*) (ttimage[i]))->SetIScore(toti_score);
2644          };
2645    
2646    
2647    
2648    
2649    
2650    //   if (tsorted->GetEntries() != trk2->GetNTracks()) {
2651    //     cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() " << tsorted->GetEntries()
2652    //         << " != trk2->GetNTracks() = " << trk2->GetNTracks() << endl;
2653    //     tsorted->Delete();
2654    //     tsorted = 0;
2655    //     timage->Delete();
2656    //     timage = 0;
2657    //   }
2658    
2659    
2660          //      cout << "text "<<orbext<<endl;
2661    
2662    
2663          //--------------------------------------------------
2664          // fill array with extended tracks (this is easy...)
2665          //--------------------------------------------------
2666          if(EXT){
2667              for(int it=0; it<trkext->GetEntries(); it++){
2668                  
2669    //            if(
2670    //                it < caloext->GetEntries() &&
2671    //                it < tofext->GetEntries() &&
2672    //                it < orbext->GetEntries() &&
2673    //                true){
2674                  
2675                  ExtTrack *t = (trkext ?(ExtTrack*)(*trkext)[it]:NULL );
2676                  CaloTrkVar *c =(caloext ?(CaloTrkVar*)(*caloext)[it]:NULL );
2677                  ToFTrkVar *o = (tofext ?(ToFTrkVar*)(*tofext)[it]:NULL );
2678                  OrbitalInfoTrkVar *r =(orbext ?(OrbitalInfoTrkVar*)(*orbext)[it]:NULL );
2679    //            cout <<"ext:"<< t<<"--"<<c<<"--"<<o<<"--"<<r<<"--"<< endl;
2680                  
2681                  new (ttext[it]) PamTrack(t, c, o, r);
2682    //            }else{
2683    //                cout << " PamLevel2::SortTracksNew() --> ORRORE E RACCAPRICCIO!!!"<<endl;
2684    //                cout << " trk  ext-tracks = "<<trkext->GetEntries()<<endl;
2685    //                cout << " calo ext-tracks = "<<caloext->GetEntries()<<endl;
2686    //                cout << " tof  ext-tracks = "<<tofext->GetEntries()<<endl;
2687    //                cout << " orb  ext-tracks = "<<orbext->GetEntries()<<endl;
2688    //            }
2689              }
2690          }
2691          
2692    //  cout <<" SortTracksNew() -- end"<<endl;
2693    
2694    
2695      };
2696    
2697    
2698      //Restore Object count
2699      //To save space in the table keeping track of all referenced objects
2700      //We reset the object count to what it was at the beginning of the event.
2701      TProcessID::SetObjectCount(ObjectNumber);
2702    
2703      //Everything went fine so the current event can be tagged as sorted
2704      issorted_new = true;
2705      lastsorted_new = GetReadEntry();
2706    
2707    //   cout << " tsorted "<< tsorted << " "<<(tsorted ? tsorted->GetEntries() : 0)<<endl;
2708    //   cout << " timage  "<< timage  << " "<<(timage  ? timage->GetEntries() : 0)<<endl;
2709    //   cout << " text    "<< text    << " "<<(text    ? text->GetEntries() : 0)<<endl;
2710    
2711    
2712  };  };
2713  //--------------------------------------  //--------------------------------------
2714  //  //
# Line 1014  void PamLevel2::SortTracks(){ Line 2718  void PamLevel2::SortTracks(){
2718   * 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.
2719   * 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.
2720   */   */
2721    
2722  // TRefArray *PamLevel2::GetTracks(){  // TRefArray *PamLevel2::GetTracks(){
2723    
2724  // //  *-*-*-*-*-*-*-*-*-*-*-*-*  // //  *-*-*-*-*-*-*-*-*-*-*-*-*
# Line 1022  void PamLevel2::SortTracks(){ Line 2727  void PamLevel2::SortTracks(){
2727    
2728  // //   return  sorted_tracks;  // //   return  sorted_tracks;
2729  //     return &sorted_tracks;  //     return &sorted_tracks;
2730        
2731  //  };  //  };
 TClonesArray *PamLevel2::GetTracks(){  
2732    
 //  *-*-*-*-*-*-*-*-*-*-*-*-*  
     SortTracks();  
 //  *-*-*-*-*-*-*-*-*-*-*-*-*  
2733    
     return tsorted;  
       
  };  
2734  //--------------------------------------  //--------------------------------------
2735   //  //
2736   //  //
2737  //--------------------------------------  //--------------------------------------
2738    
2739  /**  /**
2740   * Retrieves the it-th Pamela "physical" track.   * Retrieves the it-th Pamela "physical" track.
2741   * It override TrkLevel2::GetTrack(int it).   * It override TrkLevel2::GetTrack(int it).
2742   * @param it Track number, ranging from 0 to GetNTracks().   * @param it Track number, ranging from 0 to GetNTracks().
2743   */   */
2744  PamTrack *PamLevel2::GetTrack(int it){  PamTrack *PamLevel2::GetTrackOld(int it) {
2745    
2746  //    if(!trk2_obj) return 0;    PamTrack *track = NULL;
           
 // //  *-*-*-*+-*-*-*-*-*-*-*-*-*  
 //     SortTracks("+CAL+TOF");  
 // //  *-*-*-*-*-*-*-*-*-*-*-*-*  
 // //    if(!sorted_tracks)return 0;  
 //     if(sorted_tracks.IsEmpty())return 0;  
2747    
2748  //     PamTrack *track = 0;    //  *-*-*-*-*-*-*-*-*-*-*-*-*
2749          SortTracks();
2750  //     if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() && it<sorted_tracks.GetEntriesFast() ){        //  *-*-*-*-*-*-*-*-*-*-*-*-*
2751  //      TrkTrack   *t = (TrkTrack*)sorted_tracks.At(it);    if (!tsorted)
2752  //      track = GetPamTrackAlong(t);      return track;
2753  //     }else{  //   if (!tsorted->GetEntries())
 //      cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;  
 //     };  
       
2754  //     return track;  //     return track;
2755      if (tsorted->GetEntries()==0)
2756        return track;
2757    
 //    cout << "PamLevel2::GetTrack(int it) "<<endl;  
 //  *-*-*-*-*-*-*-*-*-*-*-*-*  
     SortTracks();  
 //  *-*-*-*-*-*-*-*-*-*-*-*-*  
     if(!tsorted)return 0;  
     if(!tsorted->GetEntries())return 0;  
2758    
2759      PamTrack *track = 0;    if (
2760              tsorted &&
2761      if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks()){        it >= 0 &&
2762  //      TrkTrack   *t = (TrkTrack*)sorted_tracks.At(it);        it < trk2_obj->TrkLevel2::GetNTracks() &&
2763  //      track = GetPamTrackAlong(t);        it < tsorted->GetEntries() &&
2764          TClonesArray &t = *(tsorted);        true) {
2765          track = (PamTrack*)t[it];        track = (PamTrack*)((*tsorted)[it]);
2766              }
2767      }else{    else {
2768          cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;      cout << "PamLevel2::GetTrackOld(int) : tracker track SeqNo " << it << " does not exist (GetNTracks() = "
2769      };          << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
2770          };
2771      return track;  
2772          return track;
2773    
2774  };  };
2775    
2776  //--------------------------------------  //PamTrack *PamLevel2::GetTrack(int it) { return GetTrack(it,trkAlg); };
2777  //  
 //  
 //--------------------------------------  
2778  /**  /**
2779   * 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.
2780     * It override TrkLevel2::GetTrack(int it).
2781   * @param it Track number, ranging from 0 to GetNTracks().   * @param it Track number, ranging from 0 to GetNTracks().
2782     * @param alg Algorythm, see SetTrakingAlgorythm(char *alg) for explanation.
2783   */   */
2784  PamTrack *PamLevel2::GetTrackImage(int it){  PamTrack *PamLevel2::GetTrack(int it, const char* alg) {
2785    
2786  //     if(!trk2_obj) return 0;      TString s(alg);
2787        if(!s.CompareTo("") ||!s.CompareTo("STD") )return GetTrackOld(it); //old algorythm
2788    
 // //  *-*-*-*-*-*-*-*-*-*-*-*-*  
 //     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;  
2789    
2790        SortTracksNew();
2791        // >> fill tsorted, timage and text
2792    
2793        if ( ( !s.Contains("EXTF", TString::kIgnoreCase) || !EXT )){ //not forced exteded-alg requested (or exteded-data missing)
2794    
2795            if( s.Contains("NUC")){
2796                if(
2797                    tsorted_nuc &&
2798                    it < tsorted_nuc->GetEntries() &&                             //enough tracks found
2799                    it >= 0 &&                                                //valid index
2800                    true) return (PamTrack*)((*tsorted_nuc)[it]);                 //ok return the track
2801            }else{
2802                if(
2803                    tsorted &&
2804                    it < tsorted->GetEntries() &&                             //enough tracks found
2805                    it >= 0 &&                                                //valid index
2806                    true )return (PamTrack*)((*tsorted)[it]);                 //ok return the track
2807            }
2808    
2809  //  *-*-*-*-*-*-*-*-*-*-*-*-*      }
     SortTracks();  
 //  *-*-*-*-*-*-*-*-*-*-*-*-*  
     if(!timage)return 0;  
     if(!timage->GetEntries())return 0;  
2810            
2811      PamTrack *image = 0;  
2812        /////////////////////////////////////////////////////////////////////////
2813        /// if requested get track from extended algorythm output
2814        /////////////////////////////////////////////////////////////////////////
2815            
2816      if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() ){      if(s.Contains("EXT", TString::kIgnoreCase) && EXT){//if exteded-alg requested
2817          TClonesArray &t = *(tsorted);          
2818          PamTrack *temp = (PamTrack*)t[it];          if(s.Contains("NUC")){
2819          if( temp->GetTrkTrack()->HasImage() ){              if(
2820              TClonesArray &t = *(timage);                  text_nuc &&
2821              image = (PamTrack*)t[it];                  it < text_nuc->GetEntries() &&                             //enough tracks found
2822                    it >= 0 &&                                                //valid index
2823                    true) return (PamTrack*)((*text_nuc)[it]);
2824          }else{          }else{
2825  //          cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;              if(
2826          };                  text &&
2827      }else{                  it < text->GetEntries() &&                             //enough tracks found
2828          cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;                  it >= 0 &&                                                //valid index
2829                    true) return (PamTrack*)((*text)[it]);
2830            }
2831    
2832      };      };
2833            
2834      return image;      
2835  }      cout << "PamTrack *PamLevel2::GetTrack("<<it<<","<<alg<<") -- wrong track number or unrecognised algorithm"<<endl;
2836        
2837        return NULL;
2838    
2839    }
2840    ;
2841    TClonesArray *PamLevel2::GetTracks() {
2842    
2843      //  *-*-*-*-*-*-*-*-*-*-*-*-*
2844      SortTracks();
2845      //  *-*-*-*-*-*-*-*-*-*-*-*-*
2846    
2847      return tsorted;
2848    
2849    }
2850    ;
2851    Int_t  PamLevel2::GetNTracks(const char* alg) {
2852    
2853    
2854    //    cout << " trk_nuc_obj->GetEntries() "<<trk_nuc_obj->GetEntries()<<" trk2_nuc_obj->GetNTracks() "<<trk2_nuc_obj->GetNTracks()<<endl;
2855    
2856        TString s(alg);
2857    
2858        if(!s.CompareTo("") || !s.CompareTo("STD"))return trk2_obj->TrkLevel2::GetNTracks(); //standard algorythm
2859            
2860        if(s.Contains("EXTF", TString::kIgnoreCase) && EXT){
2861            if(s.Contains("NUC", TString::kIgnoreCase) && NUC)return trk_ext_nuc_obj->GetEntries();//ok
2862            return trk_ext_obj->GetEntries();//ok
2863        }
2864        if( s.Contains("EXT", TString::kIgnoreCase) && EXT) {
2865            if(s.Contains("NUC", TString::kIgnoreCase) && NUC)
2866                return (trk2_nuc_obj->TrkLevel2::GetNTracks() ?  trk2_nuc_obj->TrkLevel2::GetNTracks() :  trk_ext_nuc_obj->GetEntries() );  
2867            return (trk2_obj->TrkLevel2::GetNTracks() ?  trk2_obj->TrkLevel2::GetNTracks() :  trk_ext_obj->GetEntries() );
2868        }
2869        if(s.Contains("NUC", TString::kIgnoreCase) && NUC )
2870            return trk2_nuc_obj->TrkLevel2::GetNTracks();
2871        
2872        cout << "Int_t  PamLevel2::GetNTracks("<<alg<<") -- unrecognised algorithm"<<endl;
2873    
2874        return 0;
2875    
2876    }
2877    
2878    
2879    //--------------------------------------
2880    //
2881    //
2882    //--------------------------------------
2883    /**
2884     * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().
2885     * @param it Track number, ranging from 0 to GetNTracks().
2886     */
2887    PamTrack *PamLevel2::GetTrackImageOld(int it) {
2888    
2889      //  *-*-*-*-*-*-*-*-*-*-*-*-*
2890      SortTracks();
2891      //  *-*-*-*-*-*-*-*-*-*-*-*-*
2892      if (!timage)
2893        return 0;
2894      if (!timage->GetEntries())
2895        return 0;
2896    
2897      PamTrack *image = 0;
2898    
2899      if (it >= 0 && it < trk2_obj->TrkLevel2::GetNTracks()) {
2900        TClonesArray &t = *(tsorted);
2901        PamTrack *temp = (PamTrack*) t[it];
2902        if (temp->GetTrkTrack()->HasImage()) {
2903          TClonesArray & t = *(timage);
2904          image = (PamTrack*) t[it];
2905        }
2906        else {
2907          //            cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;
2908        };
2909      }
2910      else {
2911        cout << "PamLevel2::GetTrackImageOld(int) : Tracker track SeqNo " << it << " does not exist (GetNTracks() = "
2912            << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
2913      };
2914    
2915      return image;
2916    }
2917    /**
2918     * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().
2919     * @param it Track number, ranging from 0 to GetNTracks().
2920     * @param alg Algorythm, see SetTrakingAlgorythm(char *alg) for explanation.
2921     */
2922    PamTrack *PamLevel2::GetTrackImage(int it, const char* alg) {
2923    
2924        TString s(alg);
2925        if(!s.CompareTo("") || !s.CompareTo("STD"))return GetTrackImageOld(it); //old algorythm
2926    
2927    
2928        SortTracksNew();
2929        // >> fill tsorted, timage and text
2930    
2931        if ( ( !s.Contains("EXTF", TString::kIgnoreCase) || !EXT )){ //not forced exteded-alg requested (or exteded-data missing)
2932    
2933            if( s.Contains("NUC")){
2934                if(
2935                    tsorted_nuc &&
2936                    it < tsorted_nuc->GetEntries() &&                             //enough tracks found
2937                    it >= 0 &&                                                //valid index
2938                    true){
2939                    TClonesArray &t = *(tsorted_nuc);
2940                    PamTrack *temp = (PamTrack*) t[it];
2941                    if (temp->GetTrkTrack()->HasImage()) {
2942                        return (PamTrack*)((*timage_nuc)[it]);                 //ok return the track
2943                    }else{
2944                        return NULL;
2945                    }
2946                    
2947                }
2948            }else{
2949                if(
2950                    tsorted &&
2951                    it < tsorted->GetEntries() &&                             //enough tracks found
2952                    it >= 0 &&                                                //valid index
2953                    true ){
2954                    TClonesArray &t = *(tsorted);
2955                    PamTrack *temp = (PamTrack*) t[it];
2956                    if (temp->GetTrkTrack()->HasImage()) {              
2957                        return (PamTrack*)((*timage)[it]);                 //ok return the track
2958                    }else{
2959                        return NULL;
2960                    }
2961                }
2962            }
2963    
2964        }
2965    
2966    //    cout << "PamTrack *PamLevel2::GetTrackImage("<<it<<","<<alg<<") -- wrong track number or unrecognised algorithm"<<endl;
2967          
2968        return NULL;
2969    
2970    }
2971    ;
2972    
2973  //--------------------------------------  //--------------------------------------
2974  //  //
# Line 1155  PamTrack *PamLevel2::GetTrackImage(int i Line 2980  PamTrack *PamLevel2::GetTrackImage(int i
2980   * @param detlist String to select trees to be included   * @param detlist String to select trees to be included
2981   * @return Pointer to a TTree   * @return Pointer to a TTree
2982   */   */
2983  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ){  TTree *PamLevel2::GetPamTree(TFile *f, TString detlist) {
2984    
2985      if (detlist.Contains("+AUTO", TString::kIgnoreCase)) {
2986        cout << "+AUTO" << endl;
2987        GetWhichTrees(f);
2988      };
2989      SetWhichTrees(detlist);
2990    
2991  //     if( !detlist.IsNull() ) SetWhichTrees(detlist);    if (pam_tree) {
 //     else                    GetWhichTrees(f);  
   if ( pam_tree ){  
2992      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 ");
2993      return pam_tree;      return pam_tree;
2994    };    };
2995    //    //
2996    
2997      cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;    cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte " << endl;
2998    
2999      SetWhichTrees(detlist);    //    SetWhichTrees(detlist);
3000    
3001      TTree *Trout =0;    TTree *Trout = 0;
3002    
3003      TString fname = f->GetName();    TString fname = f->GetName();
3004      if(!CheckLevel2File(fname))return NULL;    if (!CheckLevel2File(fname))
3005        return NULL;
3006    
3007  //    UInt_t *found=0;        //    UInt_t *found=0;
3008    
3009      cout<< "GetPamTree(TFile*,TString): detector list --> ";    cout << "GetPamTree(TFile*,TString): detector list --> ";
3010      if(TRK1)cout<<"TRK1 ";    if (TRK1)
3011      if(TRK2)cout<<"TRK2 ";      cout << "TRK1 ";
3012      if(TRKh)cout<<"TRKH ";    if (TRK2)
3013      if(CAL1)cout<<"CAL1 ";      cout << "TRK2 ";
3014      if(CAL2)cout<<"CAL2 ";    if (TRKh)
3015      if(TOF)cout<<"TOF ";      cout << "TRKH ";
3016      if(TRG)cout<<"TRG ";    if (CAL1)
3017      if(AC)cout<<"AC ";      cout << "CAL1 ";
3018      if(ND)cout<<"ND ";    if (CAL2)
3019      if(S4)cout<<"S4 ";      cout << "CAL2 ";
3020      if(ORB)cout<<"ORB ";    if (TOF)
3021      cout << endl;      cout << "TOF ";
3022      if(SELLI)cout<<">>> Found selection-list <<<"<<endl;    if (TRG)
3023        cout << "TRG ";
3024      f->cd();    if (AC)
3025        cout << "AC ";
3026  // Tracker    if (ND)
3027      TTree *T = (TTree*)f->Get("Tracker");      cout << "ND ";
3028      if(T && (TRK2||TRK1||TRKh)) {    if (S4)
3029          if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));      cout << "S4 ";
3030  //      else    T->SetBranchStatus("TrkLevel2",0,found);    if (ORB)
3031          if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;      cout << "ORB ";
3032          if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));    if (GP)
3033  //      else    T->SetBranchStatus("TrkLevel1",0,found);      cout << "GP ";
3034          if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;    cout << endl;
3035          if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));    if (SELLI && SELLI != 2)
3036  //      else    T->SetBranchStatus("TrkHough",0,found);      cout << ">>> Found selection-list <<<" << endl; //EMILIANO
3037          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;  
3038          if(!Trout)Trout=T;    f->cd();
3039          else Trout->AddFriend(T);  
3040      }else{    // Tracker
3041          cout << "Tracker      : missing tree"<<endl;    TTree *T = (TTree*) f->Get("Tracker");
3042      };    if (T && (TRK2 || TRK1 || TRKh)) {
3043      // Calorimeter      if (TRK2)
3044      TTree *C = (TTree*)f->Get("Calorimeter");        T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
3045      if(C && (CAL2||CAL1)) {      //  else    T->SetBranchStatus("TrkLevel2",0,found);
3046          if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));      if (TRK2)
3047  //      else    C->SetBranchStatus("CaloLevel2",0,found);        cout << "Tracker      : set branch address TrkLevel2" << endl;
3048          if(CAL2)cout << "Calorimeter  : set branch address CaloLevel2"<<endl;      if (TRK1)
3049          if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));        T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
3050  //      else    C->SetBranchStatus("CaloLevel1",0,found);      //  else    T->SetBranchStatus("TrkLevel1",0,found);
3051          if(CAL1)cout << "Calorimeter  : set branch address CaloLevel1"<<endl;      if (TRK1)
3052          if(!Trout)Trout=C;        cout << "Tracker      : set branch address TrkLevel1" << endl;
3053          else Trout->AddFriend(C);      if (TRKh)
3054      }else{        T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
3055          cout << "Calorimeter  : missing tree"<<endl;      //  else    T->SetBranchStatus("TrkHough",0,found);
3056      };      if (TRKh)
3057          cout << "Tracker      : set branch address TrkHough" << endl;
3058      // ToF          if (!Trout)
3059      TTree *O = (TTree*)f->Get("ToF");        Trout = T;
3060      if(O && TOF) {      else
3061          O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));          Trout->AddFriend(T);
3062          cout << "ToF          : set branch address ToFLevel2"<<endl;    }
3063          if(!Trout)Trout=O;    else {
3064          else Trout->AddFriend(O);      cout << "Tracker      : missing tree" << endl;
3065      }else{    };
3066          cout << "ToF         : missing tree"<<endl;    // Calorimeter
3067      };    TTree *C = (TTree*) f->Get("Calorimeter");
3068      // Trigger    if (C && (CAL2 || CAL1)) {
3069      TTree *R = (TTree*)f->Get("Trigger");      if (CAL2)
3070      if(R && TRG) {        C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
3071          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));      //  else    C->SetBranchStatus("CaloLevel2",0,found);
3072          cout << "Trigger      : set branch address TrigLevel2"<<endl;      if (CAL2)
3073          if(!Trout)Trout=R;        cout << "Calorimeter  : set branch address CaloLevel2" << endl;
3074          else Trout->AddFriend(R);      if (CAL1)
3075      }else{        C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
3076          cout << "Trigger      : missing tree"<<endl;      //  else    C->SetBranchStatus("CaloLevel1",0,found);
3077      };      if (CAL1)
3078      // S4        cout << "Calorimeter  : set branch address CaloLevel1" << endl;
3079      TTree *S = (TTree*)f->Get("S4");      if (!Trout)
3080      if(S && S4) {        Trout = C;
3081          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));      else
3082          cout << "S4           : set branch address S4Level2"<<endl;        Trout->AddFriend(C);
3083          if(!Trout)Trout=S;    }
3084          else Trout->AddFriend(S);    else {
3085      }else{      cout << "Calorimeter  : missing tree" << endl;
3086          cout << "S4           : missing tree"<<endl;    };
3087      };  
3088      // Neutron Detector    // ToF
3089      TTree *N = (TTree*)f->Get("NeutronD");    TTree *O = (TTree*) f->Get("ToF");
3090      if(N && ND) {    if (O && TOF) {
3091          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));      O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
3092          cout << "NeutronD     : set branch address NDLevel2"<<endl;      cout << "ToF          : set branch address ToFLevel2" << endl;
3093          if(!Trout)Trout=N;      if (!Trout)
3094          else Trout->AddFriend(N);        Trout = O;
3095      }else{      else
3096          cout << "NeutronD     : missing tree"<<endl;        Trout->AddFriend(O);
3097      };    }
3098      // Anticounters    else {
3099      TTree *A = (TTree*)f->Get("Anticounter");      cout << "ToF         : missing tree" << endl;
3100      if(A && AC) {    };
3101          A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));    // Trigger
3102          cout << "Anticounter  : set branch address AcLevel2"<<endl;    TTree *R = (TTree*) f->Get("Trigger");
3103          if(!Trout)Trout=A;    if (R && TRG) {
3104          else Trout->AddFriend(A);      R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
3105      }else{      cout << "Trigger      : set branch address TrigLevel2" << endl;
3106          cout << "Anticounter  : missing tree"<<endl;      if (!Trout)
3107      };        Trout = R;
3108      // Orbital Info      else
3109      TTree *B = (TTree*)f->Get("OrbitalInfo");        Trout->AddFriend(R);
3110      if(B && ORB) {    }
3111          B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));    else {
3112          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;      cout << "Trigger      : missing tree" << endl;
3113          if(!Trout)Trout=B;    };
3114          else Trout->AddFriend(B);    // S4
3115      }else{    TTree *S = (TTree*) f->Get("S4");
3116          cout << "OrbitalInfo  : missing tree"<<endl;    if (S && S4) {
3117      };      S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
3118        cout << "S4           : set branch address S4Level2" << endl;
3119      TTree *L = (TTree*)f->Get("SelectionList");      if (!Trout)
3120      if(L && SELLI==1) {        Trout = S;
3121  //      L->SetBranchAddress("RunEntry",&irun);      else
3122  //      cout << "SelectionList: set branch address RunEntry"<<endl;        Trout->AddFriend(S);
3123  //      L->SetBranchAddress("EventEntry",&irunentry);    }
3124  //      cout << "SelectionList: set branch address EventEntry"<<endl;    else {
3125  //      if(!Trout)Trout=O;      cout << "S4           : missing tree" << endl;
3126  //      else Trout->AddFriend("SelectionList");    };
3127          cout << " TTree *PamLevel2::GetPamTree(TFile, TString) >>> SelectionList not implemente!!!!"<<endl;    // Neutron Detector
3128          sel_tree = 0;    TTree *N = (TTree*) f->Get("NeutronD");
3129      }else{    if (N && ND) {
3130          cout << "SelectionList  : missing tree"<<endl;      N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
3131      };      cout << "NeutronD     : set branch address NDLevel2" << endl;
3132            if (!Trout)
3133      cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;        Trout = N;
3134        else
3135          Trout->AddFriend(N);
3136      }
3137      else {
3138        cout << "NeutronD     : missing tree" << endl;
3139      };
3140      // Anticounters
3141      TTree *A = (TTree*) f->Get("Anticounter");
3142      if (A && AC) {
3143        A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
3144        cout << "Anticounter  : set branch address AcLevel2" << endl;
3145        if (!Trout)
3146          Trout = A;
3147        else
3148          Trout->AddFriend(A);
3149      }
3150      else {
3151        cout << "Anticounter  : missing tree" << endl;
3152      };
3153      // Orbital Info
3154      TTree *B = (TTree*) f->Get("OrbitalInfo");
3155      if (B && ORB) {
3156        B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
3157        cout << "OrbitalInfo  : set branch address OrbitalInfo" << endl;
3158        if (!Trout)
3159          Trout = B;
3160        else
3161          Trout->AddFriend(B);
3162      }
3163      else {
3164        cout << "OrbitalInfo  : missing tree" << endl;
3165      };
3166    
3167      // GPamela
3168      TTree *G = (TTree*) f->Get("h20");
3169      if (G && GP) {
3170        if (!gp_obj)
3171          gp_obj = new GPamela();
3172        //      ------------------------------------
3173        //      ATTENZIONE!!!
3174        //      non so per quale arcano motivo,
3175        //      per l'albero di gpamela il branch address lo devo settare
3176        //      DOPO aver fatto friend
3177        //      FGRRRVZZZZUTSALKJMSLKJ!!!
3178        //      ------------------------------------
3179        //  gp_obj->SetBranchAddress(G); //ho dovuto fare in maniera diversa dagli altri
3180        //  cout << "h20          : set branch address GPamela "<<endl;
3181        if (!Trout)
3182          Trout = G;
3183        else
3184          Trout->AddFriend(G);
3185      }
3186      else {
3187        //  cout << "h20          : missing tree"<<endl;
3188      };
3189    
3190      TTree *L = (TTree*) f->Get("SelectionList");
3191      if (L && SELLI == 1) {
3192        cout << " TTree *PamLevel2::GetPamTree(TFile, TString) >>> SelectionList not implemented!!!!" << endl;
3193        sel_tree = 0;
3194      }
3195      else {
3196        cout << "SelectionList  : missing tree" << endl;
3197      };
3198    
3199      cout << endl << " Number of entries: " << Trout->GetEntries() << endl << endl;
3200    
3201      //      ------------------------------------
3202      //      ATTENZIONE!!!
3203      //      non so per quale arcano motivo,
3204      //      per l'albero di gpamela il branch address lo devo settare
3205      //      DOPO aver fatto friend
3206      //      FGRRRVZZZZUTSALKJMSLKJ!!!
3207      //      ------------------------------------
3208      if (G && GP) {
3209        gp_obj->SetBranchAddress(Trout); //ho dovuto fare in maniera diversa dagli altri
3210        cout << "h20          : set branch address GPamela " << endl;
3211      }
3212      else {
3213        cout << "h20          : missing tree" << endl;
3214      };
3215    
3216      pam_tree = (TChain*) Trout;
3217    
3218      return Trout;
3219    
     pam_tree = (TChain*)Trout;  
       
     return Trout;  
       
3220  }  }
3221  //--------------------------------------  //--------------------------------------
3222  //  //
# Line 1318  TTree *PamLevel2::GetPamTree(TFile *f, T Line 3229  TTree *PamLevel2::GetPamTree(TFile *f, T
3229   * @return Pointer to a TList of TSystemFiles   * @return Pointer to a TList of TSystemFiles
3230   * 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.
3231   */   */
3232  TList*  PamLevel2::GetListOfLevel2Files(TString ddir, TString flisttxt = ""){  TList* PamLevel2::GetListOfLevel2Files(TString ddir, TString flisttxt = "") {
3233            
3234      TString wdir = gSystem->WorkingDirectory();    TString wdir = gSystem->WorkingDirectory();
3235        
3236      //    if(ddir=="")ddir = wdir;    if (ddir != "") {
3237  //      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);      cout << "Level2 data directory : " << ddir << endl;
3238      if ( ddir != ""){    }
3239        cout << "Level2 data directory : "<<  ddir << endl;    else {
3240      } else {      cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory "
3241        cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory " << endl;          << endl;
3242      };    };
3243      TList *contents  = new TList; // create output list    TList *contents = new TList; // create output list
3244      contents->SetOwner();    contents->SetOwner();
3245        
3246  //    char *fullpath;    // --------------------------------------
3247  //    const char *fullpath;    // case 1 : input file/file-list provided
3248          // --------------------------------------
3249      // if no input file list is given:      if (flisttxt != "") {
3250      if ( flisttxt != "" ){  
3251                // --------------------------------------
3252  //      if( !gSystem->IsFileInIncludePath(flisttxt,&fullpath) ){              // a list of files given as input
3253  //      if( !(fullpath = gSystem->FindFile("./",flisttxt)) ){      // --------------------------------------
3254  //          cout <<"File "<<flisttxt<<" not found"<<endl;      if (!flisttxt.EndsWith(".root")) {
3255  //          return 0;  
3256  //      }              TString dir = gSystem->DirName(flisttxt);
3257  //      flisttxt = fullpath;        //            cout << " List directory "<<dir<<endl;
3258        if ( !flisttxt.EndsWith(".root") ){        if (dir == "." || dir == "")
3259            flisttxt = gSystem->ConcatFileName(wdir.Data(), gSystem->BaseName(flisttxt));
3260          flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));        //                flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
3261    
3262          if( !gSystem->ChangeDirectory(ddir) ){        if (!gSystem->ChangeDirectory(ddir)) {
3263              cout << "Cannot change directory : "<<ddir<<endl;          cout << "Cannot change directory : " << ddir << endl;
3264              return 0;          return 0;
3265          }        }
3266            
3267          cout <<"Input file list : " << flisttxt <<endl;        cout << "Input file list : " << flisttxt << endl;
3268          ifstream in;        ifstream in;
3269          in.open(flisttxt, ios::in); //open input file list        in.open(flisttxt, ios::in); //open input file list
3270          if(!in.good()){        if (!in.good()) {
3271              cout <<" ERROR opening the file "<<endl;          cout << " TList*  PamLevel2::GetListOfLevel2Files(TString, TString) --> ERROR opening the file " << endl;
3272              gSystem->ChangeDirectory(wdir); // back to the working directory          gSystem->ChangeDirectory(wdir); // back to the working directory
3273              return 0;          return 0;
3274          }              }
3275          int line=0;        int line = 0;
3276          while (1) {        // ...........................
3277              TString file;        // loop over file-list records
3278              in >> file;        // ...........................
3279              if (!in.good()) break;        while (1) {
3280              line++;          TString file;
3281  //          cout <<"(1) " << file << endl;          in >> file;
3282              if(file.IsNull()){          if (!in.good())
3283                  cout << "-- list interrupted at line "<<line <<endl;            break;
3284                  break;          line++;
3285              }          cout << "(*) " << file << endl;
3286              if(file.Contains("#"))file = file(0,file.First("#"));          if (file.IsNull()) {
3287  //          cout <<"(2) " << file << endl;            cout << "-- list interrupted at line " << line << endl;
3288  //          if( gSystem->IsFileInIncludePath(file,&fullpath) ){            break;
3289  //          if( (fullpath = gSystem->FindFile(ddir,file)) ){          }
3290              if( file.EndsWith(".root") ){          if (file.Contains("#"))
3291                TString filedir;            file = file(0, file.First("#"));
3292                if (ddir != ""){          //
3293                  filedir = ddir; // take the input dir          // take only root files
3294                } else {          //
3295                  gSystem->ChangeDirectory(wdir); // back to the working directory          if (file.EndsWith(".root")) {
3296                  filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir            TString filedir;
3297                };            cout << ddir << endl;
3298                  char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir),gSystem->BaseName(file));            if ( ddir != "" ) {
3299                  contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list              filedir = ddir; // take the input dir
3300                  delete fullpath;            }
3301              }            else {
3302  //          }else{              gSystem->ChangeDirectory(wdir); // back to the working directory
3303  //              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
3304  //          };            };
3305          };            filedir.Append("/");
3306          in.close();            //          char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file));
3307        } else {            char *fullpath = gSystem->ConcatFileName(filedir.Data(), gSystem->BaseName(file));
3308            if(flisttxt.Contains("#"))flisttxt = flisttxt(0,flisttxt.First("#"));            contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));// add file to the list
3309            char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));            cout << fullpath << endl;
3310            contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list            delete fullpath;
3311            delete fullpath;          }
3312        };                  //          }else{
3313      }else{          //              if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;
3314                    //          };
3315          cout << "No input file list given."<<endl;        };
3316          cout << "Check for existing root files."<<endl;        in.close();
3317  //              cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;        // --------------------------------------
3318          if ( ddir == "" ){        // a single root file given as input
3319            ddir = wdir;        // --------------------------------------
3320            cout << "Level2 data directory : "<<  ddir << endl;      }
3321          };      else {
3322          if (flisttxt.Contains("#"))
3323          TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);          flisttxt = flisttxt(0, flisttxt.First("#"));
3324          TList *temp = datadir->GetListOfFiles();        char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt), gSystem->BaseName(flisttxt));
3325  //              temp->Print();        contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));// add file to the list
3326  //              cout << "*************************************" << endl;        delete fullpath;
3327                };
3328          TIter next(temp);      // ---------------------------------------------------------------------------------
3329          TSystemFile *questo = 0;      // case 2 : no input file/file-list provided --> read all files insede the directory
3330                // ---------------------------------------------------------------------------------
3331              }
3332          while ( (questo = (TSystemFile*) next()) ) {    else {
3333              TString name =  questo-> GetName();  
3334              if( name.EndsWith(".root") ){      cout << "No input file list given." << endl;
3335  //              const char *fullpath = gSystem->FindFile(ddir,name);      cout << "Check for existing root files." << endl;
3336  //              char *fullpath;      //          cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;
3337  //              gSystem->IsFileInIncludePath(name,&fullpath);      if (ddir == "") {
3338                  char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir),gSystem->BaseName(name));        ddir = wdir;
3339                  contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));        cout << "Level2 data directory : " << ddir << endl;
3340                  delete fullpath;      };
3341              };  
3342          }      TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir), ddir);
3343          delete temp;      TList *temp = datadir->GetListOfFiles();
3344          delete datadir;      if (!temp)
3345                  return 0;
3346      };      //          temp->Print();
3347      gSystem->ChangeDirectory(wdir); // back to the working directory      //          cout << "*************************************" << endl;
3348  //      cout << endl << "Selected files:" << endl;  
3349  //      contents->Print();      TIter next(temp);
3350      cout << contents->GetEntries()<<" files \n";      TSystemFile *questo = 0;
3351  //      cout << endl;  
3352  //      cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;      while ((questo = (TSystemFile*) next())) {
3353      return contents;        TString name = questo-> GetName();
3354  };        if (name.EndsWith(".root")) {
3355            //              const char *fullpath = gSystem->FindFile(ddir,name);
3356            //              char *fullpath;
3357            //              gSystem->IsFileInIncludePath(name,&fullpath);
3358            char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir), gSystem->BaseName(name));
3359            contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));
3360            delete fullpath;
3361          };
3362        }
3363        delete temp;
3364        delete datadir;
3365    
3366      };
3367      gSystem->ChangeDirectory(wdir); // back to the working directory
3368      //    cout << endl << "Selected files:" << endl;
3369      //    contents->Print();
3370      cout << contents->GetEntries() << " files \n";
3371      //    cout << endl;
3372      //    cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;
3373      return contents;
3374    }
3375    ;
3376  //--------------------------------------  //--------------------------------------
3377  //  //
3378  //  //
3379  //--------------------------------------  //--------------------------------------
3380  /**  /**
3381   * 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.
3382   * @param fl Pointer to a TList of TSystemFiles   * @param fl Pointer to a TList of TSystemFiles
3383   * @param detlist String to select trees to be included   * @param detlist String to select trees to be included
3384   * @return Pointer to a TChain   * @return Pointer to a TChain
3385   */   */
3386  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){  TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) {
3387    //    //
3388    //    //
3389    //    //
3390    if ( pam_tree ){    if (pam_tree) {
3391      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 ");
3392      return pam_tree;      return pam_tree;
3393    };    };
3394    //    //
       
     TChain *Trout =0;  
3395    
3396  //    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;  
3397    
3398       TChain *L = 0;    // -------------------------------------------
3399          // set flags to include/exclude trees/branches
3400      if(TRK2||TRK1||TRKh) T = new TChain("Tracker");        // -------------------------------------------
3401      if(CAL2||CAL1)       C = new TChain("Calorimeter");    if (detlist.Contains("+AUTO", TString::kIgnoreCase)) {
3402      if(TOF)              O = new TChain("ToF");      if (fl->GetEntries() > 0) {
3403      if(TRG)              R = new TChain("Trigger");        cout << "+AUTO" << endl;
3404      if(S4)               S = new TChain("S4");        TFile *fprimo = new TFile(fl->At(0)->GetName());
3405      if(ND)               N = new TChain("NeutronD");        GetWhichTrees(fprimo);
3406      if(AC)               A = new TChain("Anticounter");        fprimo->Close();// AAAAARGGGGGHHHHH!!!!!!! non commentare questa riga, altrimenti si incasina il TChain
3407      if(ORB)              B = new TChain("OrbitalInfo");        fprimo->Delete();
3408       L = new TChain("SelectionList");      }
3409          };
3410      // 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;  
3411    
3412  //    UInt_t *found=0;    // -------------------------------------------
3413  // Tracker    // build chains
3414      if(T && (TRK2||TRK1||TRKh)) {    // -------------------------------------------
3415          if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));    TChain *T = 0;
3416  //      else    T->SetBranchStatus("TrkLevel2",0,found);    TChain *C = 0;
3417          if(TRK2)cout << "Tracker      : set branch address TrkLevel2"<<endl;    TChain *O = 0;
3418          if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));    TChain *R = 0;
3419  //      else    T->SetBranchStatus("TrkLevel1",0,found);    TChain *S = 0;
3420          if(TRK1)cout << "Tracker      : set branch address TrkLevel1"<<endl;    TChain *N = 0;
3421          if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));    TChain *A = 0;
3422  //      else    T->SetBranchStatus("TrkHough",0,found);    TChain *B = 0;
3423          if(TRKh)cout << "Tracker      : set branch address TrkHough"<<endl;    TChain *G = 0;
3424          if(!Trout)Trout=T;  
3425          else Trout->AddFriend("Tracker");    TChain *L = 0;
3426      }else{    TChain *P = 0;
3427          cout << "Tracker      : missing tree"<<endl;  
3428      };    if (TRK2 || TRK1 || TRKh)
3429      // Calorimeter      T = new TChain("Tracker");
3430      if(C && (CAL2||CAL1)) {    if (CAL2 || CAL1)
3431          if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));      C = new TChain("Calorimeter");
3432  //      else    C->SetBranchStatus("CaloLevel2",0,found);    if (TOF)
3433          if(CAL2)cout << "Calorimeter  : set branch address CaloLevel2"<<endl;      O = new TChain("ToF");
3434          if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));    if (TRG)
3435  //      else    C->SetBranchStatus("CaloLevel1",0,found);      R = new TChain("Trigger");
3436          if(CAL1)cout << "Calorimeter  : set branch address CaloLevel1"<<endl;    if (S4)
3437          if(!Trout)Trout=C;      S = new TChain("S4");
3438          else Trout->AddFriend("Calorimeter");    if (ND)
3439      }else{      N = new TChain("NeutronD");
3440          cout << "Calorimeter  : missing tree"<<endl;    if (AC)
3441      };      A = new TChain("Anticounter");
3442      // ToF        if (ORB)
3443      if(O && TOF) {      B = new TChain("OrbitalInfo");
3444          O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));    if (GP)
3445          cout << "ToF          : set branch address ToFLevel2"<<endl;      G = new TChain("h20");
3446          if(!Trout)Trout=O;    if (PROC)
3447          else Trout->AddFriend("ToF");      P = new TChain("ProcessingInfo");
3448      }else{    L = new TChain("SelectionList");
3449          cout << "ToF         : missing tree"<<endl;  
3450      };    // loop over files and create chains
3451      // Trigger    TIter next(fl);
3452      if(R && TRG) {    TSystemFile *questo = 0;
3453          R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));    while ((questo = (TSystemFile*) next())) {
3454          cout << "Trigger      : set branch address TrigLevel2"<<endl;      TString name = questo->GetName();
3455          if(!Trout)Trout=O;      cout << "File: " << name << endl;
3456          else Trout->AddFriend("Trigger");      if (CheckLevel2File(name)) {
3457      }else{        if (TRK2 || TRK1 || TRKh)
3458          cout << "Trigger      : missing tree"<<endl;          T->Add(name);
3459      };        if (CAL1 || CAL2)
3460      // S4          C->Add(name);
3461      if(S && S4) {        if (TOF)
3462          S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));          O->Add(name);
3463          cout << "S4           : set branch address S4Level2"<<endl;        if (TRG)
3464          if(!Trout)Trout=O;          R->Add(name);
3465          else Trout->AddFriend("S4");        if (S4)
3466      }else{          S->Add(name);
3467          cout << "S4           : missing tree"<<endl;        if (ND)
3468      };          N->Add(name);
3469      // Neutron Detector        if (AC)
3470      if(N && ND) {          A->Add(name);
3471          N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));        if (ORB)
3472          cout << "NeutronD     : set branch address NDLevel2"<<endl;          B->Add(name);
3473          if(!Trout)Trout=O;        if (GP)
3474          else Trout->AddFriend("NeutronD");          G->Add(name);
3475      }else{        if (P)
3476          cout << "NeutronD     : missing tree"<<endl;          P->Add(name);
3477      };        if (SELLI == 1)
3478      // Anticounters          L->Add(name);
3479      if(A && AC) {       };
3480          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;  
3481    
3482      pam_tree = Trout;    cout << "done data-tree chains  "<<  T->GetNtrees() <<" \n";
3483      cout << "----------------------------------------------------" << endl;
3484    
3485      return Trout;    // -------------------------------------------
3486  }    // make friends
3487      // -------------------------------------------
3488    
3489      // Tracker
3490      cout << "Friends: " << endl;
3491      if (T && (TRK2 || TRK1 || TRKh)) {
3492        if (!Trout)
3493          Trout = T;
3494        else
3495          Trout->AddFriend("Tracker");
3496        //  cout << "+Tacker"<<endl;
3497      };
3498      // Calorimeter
3499      if (C && (CAL2 || CAL1)) {
3500        if (!Trout)
3501          Trout = C;
3502        else
3503          Trout->AddFriend("Calorimeter");
3504        //  cout << "+Calorimeter"<<endl;
3505      };
3506      // ToF
3507      if (O && TOF) {
3508        if (!Trout)
3509          Trout = O;
3510        else
3511          Trout->AddFriend("ToF");
3512        //  cout << "+ToF"<<endl;
3513      };
3514      // Trigger
3515      if (R && TRG) {
3516        if (!Trout)
3517          Trout = O;
3518        else
3519          Trout->AddFriend("Trigger");
3520        //  cout << "+Trigger"<<endl;
3521      };
3522      // S4
3523      if (S && S4) {
3524        if (!Trout)
3525          Trout = O;
3526        else
3527          Trout->AddFriend("S4");
3528        //  cout << "+S4"<<endl;
3529      };
3530      // Neutron Detector
3531      if (N && ND) {
3532        if (!Trout)
3533          Trout = O;
3534        else
3535          Trout->AddFriend("NeutronD");
3536        //  cout << "+NeutronD"<<endl;
3537      };
3538      // Anticounters
3539      if (A && AC) {
3540        if (!Trout)
3541          Trout = O;
3542        else
3543          Trout->AddFriend("Anticounter");
3544        //  cout << "+Anticounter"<<endl;
3545      };
3546      // Orbital Info
3547      if (B && ORB) {
3548        if (!Trout)
3549          Trout = O;
3550        else
3551          Trout->AddFriend("OrbitalInfo");
3552        //  cout << "+OrbitalInfo"<<endl;
3553      };
3554      // GPamela
3555      if (G && GP) {
3556        if (!Trout)
3557          Trout = G;
3558        else
3559          Trout->AddFriend("h20");
3560        //  cout << "+h20"<<endl;
3561      };
3562    
3563      //  =====================================
3564      //  SET BRANCH-ADDRESS AFTER CHAIN+FRIEND
3565      //  =====================================
3566      if( Trout->GetNtrees() )SetBranchAddress(Trout);
3567    
3568      //  ------------------------------------
3569      //  finally handle selection trees...
3570      //  (it is not friend of pamela tree)
3571      //  ------------------------------------
3572    
3573      cout << "----------------------------------------------------" << endl;
3574    
3575      // Selection List
3576      if (L && SELLI == 1) {
3577        cout << ">>> Found selection-list <<<" << endl;
3578        //  L->SetBranchAddress("RunEntry",&irun);
3579        L->SetBranchAddress("RunEntry", &irunt);//NEWNEW
3580        cout << "SelectionList: set branch address RunEntry" << endl;
3581        L->SetBranchAddress("EventEntry", &irunentry);
3582        cout << "SelectionList: set branch address EventEntry" << endl;
3583        /*    if ( L->GetBranch("L0EventEntry") ){
3584          hasL0EE = true;
3585          L->SetBranchAddress("L0EventEntry", &il0entry);
3586          cout << "SelectionList: set branch address L0EventEntry" << endl;
3587        } else {
3588          hasL0EE = false; // backward compatibility with old preselected files...
3589          }*/
3590        sel_tree = L;
3591        //          if(!Trout)Trout=O;
3592        //          else Trout->AddFriend("SelectionList");
3593        cout << "+SelectionList" << endl;
3594        cout << "----------------------------------------------------" << endl;
3595      }
3596      else {
3597        //  cout << "SelectionList  : missing tree"<<endl;
3598        if (L)
3599          L->Delete();
3600      };
3601    
3602      //ProcessingInfo EM
3603      if ( P && P->GetEntries() ){
3604        cout << "----------------------------------------------------" << endl;
3605        cout << ">>> Found ProcessingInfo <<<" << endl;
3606        //  L->SetBranchAddress("RunEntry",&irun);
3607        P->SetBranchAddress("ProcInfo", &proc_obj);//NEWNEW
3608        proc_tree = P;
3609      } else {
3610        //    proc_tree = new TChain("ProcessingInfo","Log of data processing");
3611        //    proc_tree->Branch("ProcInfo", "ProcInfo", &proc_obj);
3612        cout << "----------------------------------------------------" << endl;
3613        cout << ">>> ProcessingInfo not found, PROC set to false and continue (not 10RED files?)<<<" << endl;
3614        PROC = false;
3615      }
3616      //  --------------------------------------------
3617      //  return the pamela chain with all the friends
3618      //  --------------------------------------------
3619    
3620      pam_tree = Trout;
3621      return Trout;
3622    }
3623    
3624  //--------------------------------------  //--------------------------------------
3625  //  //
# Line 1641  TChain *PamLevel2::GetPamTree(TList *fl, Line 3628  TChain *PamLevel2::GetPamTree(TList *fl,
3628  /**  /**
3629   * Set branch addresses for Pamela friend trees   * Set branch addresses for Pamela friend trees
3630   */   */
3631  void PamLevel2::SetBranchAddress(TTree *t){  void PamLevel2::SetBranchAddress(TTree *t) {
3632    
3633      TRK2    = TRK2 & t->GetBranchStatus("TrkLevel2");  //   TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");
3634      TRK1    = TRK1 & t->GetBranchStatus("TrkLevel1");  //   TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");
3635      TRKh    = TRKh & t->GetBranchStatus("TrkHough");  //   TRKh = TRKh & t->GetBranchStatus("TrkHough");
3636      CAL2    = CAL2 & t->GetBranchStatus("CaloLevel2");  //   CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");
3637      CAL1    = CAL1 & t->GetBranchStatus("CaloLevel1");  //   CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");
3638      TOF    = TOF & t->GetBranchStatus("ToFLevel2");  //   TOF = TOF & t->GetBranchStatus("ToFLevel2");
3639      TRG    = TRG & t->GetBranchStatus("TrigLevel2");  //   TRG = TRG & t->GetBranchStatus("TrigLevel2");
3640      S4     = S4  & t->GetBranchStatus("S4Level2");  //   S4 = S4 & t->GetBranchStatus("S4Level2");
3641      ND     = ND  & t->GetBranchStatus("NDLevel2");  //   ND = ND & t->GetBranchStatus("NDLevel2");
3642      AC     = AC  & t->GetBranchStatus("AcLevel2");  //   AC = AC & t->GetBranchStatus("AcLevel2");
3643      ORB    = ORB & t->GetBranchStatus("OrbitalInfo");  //   ORB = ORB & t->GetBranchStatus("OrbitalInfo");
3644    //   GP = GP & t->GetBranchStatus("h20");
3645    
3646      // Tracker  
3647      if(TRK1) {    // Tracker
3648          t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel2"));    if (TRK1) {
3649          cout << "Tracker      : set branch address TrkLevel1"<<endl;      t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel2"));
3650      };      cout << "Tracker      : set branch address TrkLevel1" << endl;
3651      if(TRK2) {    };
3652          t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));    if (TRK2) {
3653          cout << "Tracker      : set branch address TrkLevel2"<<endl;      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));
3654      };      cout << "Tracker      : set branch address TrkLevel2" << endl;
3655      if(TRKh) {      NUC = t->GetBranchStatus("TrackNuclei");
3656          t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));      EXT = t->GetBranchStatus("RecoveredTrack") && (NUC ? t->GetBranchStatus("RecoveredTrackNuclei"): true );    
3657          cout << "Tracker      : set branch address TrkHough"<<endl;    };
3658      };    if (TRKh) {
3659            t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
3660      // Calorimeter      cout << "Tracker      : set branch address TrkHough" << endl;
3661      if(CAL1) {    };
3662          t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));  
3663          cout << "Calorimeter  : set branch address CaloLevel1"<<endl;    // Calorimeter
3664      };    if (CAL1) {
3665      if(CAL2) {      t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
3666          t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));      cout << "Calorimeter  : set branch address CaloLevel1" << endl;
3667          cout << "Calorimeter  : set branch address CaloLevel2"<<endl;    };
3668      };    if (CAL2) {
3669            t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
3670      // ToF          cout << "Calorimeter  : set branch address CaloLevel2" << endl;
3671      if(TOF) {    };
3672          t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));  
3673          cout << "ToF          : set branch address ToFLevel2"<<endl;    // ToF
3674      };    if (TOF) {
3675      // Trigger      t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
3676      if(TRG) {      cout << "ToF          : set branch address ToFLevel2" << endl;
3677          t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));    };
3678          cout << "Trigger      : set branch address TrigLevel2"<<endl;    // Trigger
3679      };    if (TRG) {
3680      // S4      t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
3681      if(S4) {      cout << "Trigger      : set branch address TrigLevel2" << endl;
3682          t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));    };
3683          cout << "S4           : set branch address S4Level2"<<endl;    // S4
3684      };    if (S4) {
3685      // Neutron Detector      t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
3686      if(ND) {      cout << "S4           : set branch address S4Level2" << endl;
3687          t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));    };
3688          cout << "NeutronD     : set branch address NDLevel2"<<endl;    // Neutron Detector
3689      };    if (ND) {
3690      // Anticounters      t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
3691      if(AC) {      cout << "NeutronD     : set branch address NDLevel2" << endl;
3692          t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));    };
3693          cout << "Anticounter  : set branch address AcLevel2"<<endl;    // Anticounters
3694      };    if (AC) {
3695      // OrbitalInfo      t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
3696      if(ORB) {      cout << "Anticounter  : set branch address AcLevel2" << endl;
3697          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));    };
3698          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;    // OrbitalInfo
3699      };    if (ORB) {
3700      // SelectionList      t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
3701      if(SELLI==1) {      cout << "OrbitalInfo  : set branch address OrbitalInfo" << endl;
3702          t->SetBranchAddress("RunEntry", &irun);    };
3703          cout << "SelectionList: set branch address RunEntry"<<endl;    // GPamela
3704          t->SetBranchAddress("EventEntry", &irunentry);    if (GP) {
3705          cout << "SelectionList: set branch address EventEntry"<<endl;      //  GetPointerTo("GPamela");
3706      };      if (!gp_obj)
3707              gp_obj = new GPamela();
3708        //  gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
3709        // //       t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
3710        if (SELLI)
3711          t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
3712        else
3713          gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
3714    
3715        cout << "h20          : set branch address GPamela " << endl;
3716      };
3717      if(NUC){
3718          
3719          cout << "Found nuclei-track branches" << endl;
3720    
3721          if( !trk_nuc_obj )trk_nuc_obj = new TClonesArray("TrkTrack");
3722          if( !calo_nuc_obj)calo_nuc_obj= new TClonesArray("CaloTrkVar");
3723          if( !tof_nuc_obj)tof_nuc_obj= new TClonesArray("ToFTrkVar");
3724          if( !orb_nuc_obj)orb_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3725          if (TRK2)t->                          SetBranchAddress("TrackNuclei",&trk_nuc_obj);
3726          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("TrackNuclei",&calo_nuc_obj);
3727          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("TrackNuclei",&tof_nuc_obj);
3728          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("TrackNuclei",&orb_nuc_obj);
3729    
3730          ///copy the vector content inside a "fake" object (all aother info are missing)    
3731    
3732          if( !trk2_nuc_obj )trk2_nuc_obj =  new TrkLevel2();
3733          if( !calo2_nuc_obj )calo2_nuc_obj =  new CaloLevel2();
3734          if( !tof2_nuc_obj )tof2_nuc_obj =  new ToFLevel2();
3735          if( !orb2_nuc_obj )orb2_nuc_obj =  new OrbitalInfo();
3736    //       *(trk2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*trk_nuc_obj);
3737    //       *(calo2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*calo_nuc_obj);
3738    //       *(tof2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*tof_nuc_obj);
3739    //       *(orb2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*orb_nuc_obj);
3740    
3741          trk2_nuc_obj->SetTrackArray(  trk_nuc_obj  );
3742          calo2_nuc_obj->SetTrackArray( calo_nuc_obj );
3743          tof2_nuc_obj->SetTrackArray(  tof_nuc_obj  );
3744          orb2_nuc_obj->SetTrackArray(  orb_nuc_obj  );
3745    
3746          
3747      }    
3748    
3749      if(EXT){
3750          
3751          cout << "Found extended tracking algorythm branches" << endl;
3752    
3753          if( !trk_ext_obj )trk_ext_obj = new TClonesArray("ExtTrack");
3754          if( !calo_ext_obj)calo_ext_obj= new TClonesArray("CaloTrkVar");
3755          if( !tof_ext_obj)tof_ext_obj= new TClonesArray("ToFTrkVar");
3756          if( !orb_ext_obj)orb_ext_obj= new TClonesArray("OrbitalInfoTrkVar");
3757          
3758          if (TRK2)t->                          SetBranchAddress("RecoveredTrack",&trk_ext_obj);
3759          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrack",&calo_ext_obj);
3760          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrack",&tof_ext_obj);
3761          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrack",&orb_ext_obj);
3762    
3763        
3764          if(NUC){
3765              if( !trk_ext_nuc_obj )trk_ext_nuc_obj = new TClonesArray("ExtTrack");
3766              if( !calo_ext_nuc_obj)calo_ext_nuc_obj= new TClonesArray("CaloTrkVar");
3767              if( !tof_ext_nuc_obj)tof_ext_nuc_obj= new TClonesArray("ToFTrkVar");
3768              if( !orb_ext_nuc_obj)orb_ext_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3769              if (TRK2)t->                          SetBranchAddress("RecoveredTrackNuclei",&trk_ext_nuc_obj);
3770              if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrackNuclei",&calo_ext_nuc_obj);
3771              if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrackNuclei",&tof_ext_nuc_obj);
3772              if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrackNuclei",&orb_ext_nuc_obj);
3773          }
3774      }    
3775  }  }
3776  /**  /**
3777   * Set branch addresses for Pamela friend trees   * Set branch addresses for Pamela friend trees
3778   */   */
3779  void PamLevel2::SetBranchAddress(TChain *t){  void PamLevel2::SetBranchAddress(TChain *t) {
3780    
3781      TRK2    = TRK2 & t->GetBranchStatus("TrkLevel2");    //     TRK2    = TRK2 & t->GetBranchStatus("TrkLevel2");
3782      TRK1    = TRK1 & t->GetBranchStatus("TrkLevel1");    //     TRK1    = TRK1 & t->GetBranchStatus("TrkLevel1");
3783      TRKh    = TRKh & t->GetBranchStatus("TrkHough");    //     TRKh    = TRKh & t->GetBranchStatus("TrkHough");
3784      CAL1    = CAL1 & t->GetBranchStatus("CaloLevel1");    //     CAL1    = CAL1 & t->GetBranchStatus("CaloLevel1");
3785      CAL2    = CAL2 & t->GetBranchStatus("CaloLevel2");    //     CAL2    = CAL2 & t->GetBranchStatus("CaloLevel2");
3786      TOF    = TOF & t->GetBranchStatus("ToFLevel2");    //     TOF    = TOF & t->GetBranchStatus("ToFLevel2");
3787      TRG    = TRG & t->GetBranchStatus("TrigLevel2");    //     TRG    = TRG & t->GetBranchStatus("TrigLevel2");
3788      S4     = S4  & t->GetBranchStatus("S4Level2");    //     S4     = S4  & t->GetBranchStatus("S4Level2");
3789      ND     = ND  & t->GetBranchStatus("NDLevel2");    //     ND     = ND  & t->GetBranchStatus("NDLevel2");
3790      AC     = AC  & t->GetBranchStatus("AcLevel2");    //     AC     = AC  & t->GetBranchStatus("AcLevel2");
3791      ORB    = ORB & t->GetBranchStatus("OrbitalInfo");    //     ORB    = ORB & t->GetBranchStatus("OrbitalInfo");
3792      //    GP     = GP & t->GetBranchStatus("h20");
3793      // Tracker  
3794       if(TRK2) {    // Tracker
3795          t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));    if (TRK2) {
3796          cout << "Tracker      : set branch address TrkLevel2"<<endl;      t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
3797      };      cout << "Tracker      : set branch address TrkLevel2" << endl;
3798      if(TRK1) {      NUC = t->GetBranchStatus("TrackNuclei");
3799          t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));      EXT = t->GetBranchStatus("RecoveredTrack") && (NUC ? t->GetBranchStatus("RecoveredTrackNuclei"): true );
3800          cout << "Tracker      : set branch address TrkLevel1"<<endl;    };
3801      };    if (TRK1) {
3802       if(TRKh) {      t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
3803          t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));      cout << "Tracker      : set branch address TrkLevel1" << endl;
3804          cout << "Tracker      : set branch address TrkHough"<<endl;    };
3805       };    if (TRKh) {
3806            t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
3807      // Calorimeter      cout << "Tracker      : set branch address TrkHough" << endl;
3808      if(CAL2) {    };
3809          t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));  
3810          cout << "Calorimeter  : set branch address CaloLevel2"<<endl;    // Calorimeter
3811      };      if (CAL2) {
3812      if(CAL1) {      t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
3813          t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));      cout << "Calorimeter  : set branch address CaloLevel2" << endl;    
3814          cout << "Calorimeter  : set branch address CaloLevel1"<<endl;    };
3815      };    if (CAL1) {
3816            t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
3817      // ToF          cout << "Calorimeter  : set branch address CaloLevel1" << endl;
3818      if(TOF) {    };
3819          t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));  
3820          cout << "ToF          : set branch address ToFLevel2"<<endl;    // ToF
3821      };    if (TOF) {
3822      // Trigger      t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
3823      if(TRG) {      cout << "ToF          : set branch address ToFLevel2" << endl;
3824          t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));    };
3825          cout << "Trigger      : set branch address TrigLevel2"<<endl;    // Trigger
3826      };    if (TRG) {
3827      // S4      t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
3828      if(S4) {      cout << "Trigger      : set branch address TrigLevel2" << endl;
3829          t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));    };
3830          cout << "S4           : set branch address S4Level2"<<endl;    // S4
3831      };    if (S4) {
3832      // Neutron Detector      t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
3833      if(ND) {      cout << "S4           : set branch address S4Level2" << endl;
3834          t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));    };
3835          cout << "NeutronD     : set branch address NDLevel2"<<endl;    // Neutron Detector
3836      };    if (ND) {
3837      // Anticounters      t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
3838      if(AC) {      cout << "NeutronD     : set branch address NDLevel2" << endl;
3839          t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));    };
3840          cout << "Anticounter  : set branch address AcLevel2"<<endl;    // Anticounters
3841      };    if (AC) {
3842      // OrbitalInfo      t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
3843      if(ORB) {      cout << "Anticounter  : set branch address AcLevel2" << endl;
3844          t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));    };
3845          cout << "OrbitalInfo  : set branch address OrbitalInfo"<<endl;    // OrbitalInfo
3846      };    if (ORB) {
3847      // SelectionList      t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
3848      if(SELLI==1) {      cout << "OrbitalInfo  : set branch address OrbitalInfo" << endl;
3849          t->SetBranchAddress("RunEntry", &irun);    };
3850          cout << "SelectionList: set branch address RunEntry"<<endl;    // GPamela
3851          t->SetBranchAddress("EventEntry", &irunentry);    //    cout <<"GP "<<GP<<endl;
3852          cout << "SelectionList: set branch address EventEntry"<<endl;    if (GP) {
3853      };      //  GetPointerTo("GPamela");
3854            if (!gp_obj)
3855          gp_obj = new GPamela();
3856        if (SELLI)
3857          t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
3858        else
3859          gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
3860        //  gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
3861        cout << "h20          : set branch address GPamela " << endl;
3862      };
3863      // SelectionList
3864      //     if(SELLI==1){
3865      //    t->SetBranchAddress("RunEntry",&irunt);//NEWNEW
3866      //    cout << "SelectionList: set branch address RunEntry"<<endl;
3867      //    t->SetBranchAddress("EventEntry",&irunentry);
3868      //    cout << "SelectionList: set branch address EventEntry"<<endl;
3869    
3870      //     }
3871      if(NUC){
3872          
3873          cout << "Found nuclei-track branches" << endl;
3874    
3875          if( !trk_nuc_obj )trk_nuc_obj = new TClonesArray("TrkTrack");
3876          if( !calo_nuc_obj)calo_nuc_obj= new TClonesArray("CaloTrkVar");
3877          if( !tof_nuc_obj)tof_nuc_obj= new TClonesArray("ToFTrkVar");
3878          if( !orb_nuc_obj)orb_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3879          if (TRK2)t->                          SetBranchAddress("TrackNuclei",&trk_nuc_obj);
3880          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("TrackNuclei",&calo_nuc_obj);
3881          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("TrackNuclei",&tof_nuc_obj);
3882          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("TrackNuclei",&orb_nuc_obj);
3883          
3884          ///copy the vector content inside a "fake" object (all aother info are missing)    
3885    
3886          if( !trk2_nuc_obj )trk2_nuc_obj =  new TrkLevel2();
3887          if( !calo2_nuc_obj )calo2_nuc_obj =  new CaloLevel2();
3888          if( !tof2_nuc_obj )tof2_nuc_obj =  new ToFLevel2();
3889          if( !orb2_nuc_obj )orb2_nuc_obj =  new OrbitalInfo();
3890    
3891    //       *(trk2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*trk_nuc_obj);
3892    //       *(calo2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*calo_nuc_obj);
3893    //       *(tof2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*tof_nuc_obj);
3894    //       *(orb2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*orb_nuc_obj);
3895          trk2_nuc_obj->SetTrackArray(  trk_nuc_obj  );
3896          calo2_nuc_obj->SetTrackArray( calo_nuc_obj );
3897          tof2_nuc_obj->SetTrackArray(  tof_nuc_obj  );
3898          orb2_nuc_obj->SetTrackArray(  orb_nuc_obj  );
3899    
3900      }    
3901      if(EXT){
3902          
3903          cout << "Found extended tracking algorythm branches" << endl;
3904    
3905          t->SetBranchAddress("extAlgFlag",&extAlgFlag);
3906    
3907          if( !trk_ext_obj )trk_ext_obj = new TClonesArray("ExtTrack");
3908          if( !calo_ext_obj)calo_ext_obj= new TClonesArray("CaloTrkVar");
3909          if( !tof_ext_obj)tof_ext_obj= new TClonesArray("ToFTrkVar");
3910          if( !orb_ext_obj)orb_ext_obj= new TClonesArray("OrbitalInfoTrkVar");
3911          
3912          if (TRK2)t->                          SetBranchAddress("RecoveredTrack",&trk_ext_obj);
3913          if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrack",&calo_ext_obj);
3914          if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrack",&tof_ext_obj);
3915          if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrack",&orb_ext_obj);
3916          
3917          if(NUC){
3918              if( !trk_ext_nuc_obj )trk_ext_nuc_obj = new TClonesArray("ExtTrack");
3919              if( !calo_ext_nuc_obj)calo_ext_nuc_obj= new TClonesArray("CaloTrkVar");
3920              if( !tof_ext_nuc_obj)tof_ext_nuc_obj= new TClonesArray("ToFTrkVar");
3921              if( !orb_ext_nuc_obj)orb_ext_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3922              if (TRK2)t->                          SetBranchAddress("RecoveredTrackNuclei",&trk_ext_nuc_obj);
3923              if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrackNuclei",&calo_ext_nuc_obj);
3924              if (TOF )t->GetFriend("ToF")->        SetBranchAddress("RecoveredTrackNuclei",&tof_ext_nuc_obj);
3925              if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrackNuclei",&orb_ext_nuc_obj);
3926          }
3927      }    
3928      
3929  }  }
3930    
3931    /**
3932     * Set the tracking algorythm
3933     * @param alg String to choose the track.
3934     * "" --> take the output of the standard tracking algorythm
3935     * "STD" --> take the output of the standard tracking algorythm
3936     * "NUC" --> take the output of the standard tracking algorythm for nuclei cluster selection
3937     * "EXT" --> in case the standard tracking algorythm has not found any track, take the output of the extended one
3938     * "EXTF" --> force the extended tracking algorythm
3939     * "NUCEXT" --> as "EXT", but for nuclei
3940     * "NUCEXTF" --> as "EXTF", but for nuclei
3941     */
3942    // void PamLevel2::SetTrackingAlgorythm(const char *alg){
3943    
3944    
3945    //     TString s(alg);
3946    //     if(s.Contains("NUC", TString::kIgnoreCase) && !NUC)
3947    //      cout << "Warning! NUC algorythm requested, but branches are missing"<<endl;
3948    //     if(s.Contains("EXT", TString::kIgnoreCase) && !EXT)
3949    //      cout << "Warning! EXT algorythm requested, but branches are missing"<<endl;;
3950                
3951    //     trkAlg = alg;
3952    
3953    // };
3954    // const char* PamLevel2::GetTrackingAlgorythm(){
3955    
3956    
3957    //     cout<<endl<<" Implemented tracking algorythm: ";
3958    //     cout<<endl<<"  \"\" or \"STD\"  --> take the output of the standard tracking algorythm";
3959    //     cout<<endl<<"  \"NUC\"      --> take the output of the standard tracking algorythm for nuclei cluster selection";
3960    //     cout<<endl<<"  \"EXT\"      --> in case the standard tracking algorythm has not found any track,";
3961    //     cout<<endl<<"                 take the output of the extended one";
3962    //     cout<<endl<<"  \"EXTF\"     --> force the extended tracking algorythm";
3963    //     cout<<endl<<"  \"NUCEXT\"   --> as \"EXT\", but for nuclei ";
3964    //     cout<<endl<<"  \"NUCEXTF\"  --> as \"EXTF\", but for nuclei";
3965    
3966    //     cout<<endl;
3967    //     cout<<" <<Currently set algorythm>> "<<trkAlg<<endl;
3968    //     cout<<endl;
3969    
3970    //     return trkAlg;
3971    // };
3972    
3973    
3974    
3975  //--------------------------------------  //--------------------------------------
3976  //  //
# Line 1807  void PamLevel2::SetBranchAddress(TChain Line 3978  void PamLevel2::SetBranchAddress(TChain
3978  //--------------------------------------  //--------------------------------------
3979  /**  /**
3980   * Get the Run tree chain from a list of files.   * Get the Run tree chain from a list of files.
3981   * @param fl Pointer to a TList of TSystemFiles   * @param fl Pointer to a TList of TSystemFiles
3982   * @return Pointer to a TChain   * @return Pointer to a TChain
3983   */   */
3984  TChain *PamLevel2::GetRunTree(TList *fl){  TChain *PamLevel2::GetRunTree(TList *fl) {
3985    //    //
3986    //    //
3987    //    //
3988    if ( run_tree ){    if (run_tree) {
3989      printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");      printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");
3990      return run_tree;      return run_tree;
3991    };        };
3992    //    //
3993    
3994    
3995    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);  
         };  
     }  
3996    
3997      if(R->GetNtrees()){    // loop over files and create chains
3998          TIter next(fl);
3999          R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));    TSystemFile *questo = 0;
4000          cout << "Run         : set branch address RunInfo"<<endl;    while ((questo = (TSystemFile*) next())) {
4001          R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano      TString name = questo->GetName();
4002          cout << "Software    : set branch address SoftInfo"<<endl; // Emiliano      //          cout << "File: "<< name << endl;
4003      }else{      if (CheckLevel2File(name)) {
4004          delete R;        R->Add(name);
4005          R=0;      };
4006      }
4007    
4008      cout << "done run chain  "<<  R->GetNtrees() <<" \n";
4009    
4010    
4011      if (RUN && R->GetNtrees()) {
4012    
4013        R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
4014        cout << "Run         : set branch address RunInfo" << endl;
4015        R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
4016        cout << "Software    : set branch address SoftInfo" << endl; // Emiliano
4017    
4018        irunoffset = new int[R->GetNtrees()];
4019        if (DBG) {
4020          cout << "----------------------------------------------------" << endl;
4021          cout << "irun\t | ";
4022          cout << "tree\t |";
4023          //        cout << "offset\t |";
4024          cout << "RUN\t";
4025          cout << "FRAG\t";
4026          cout << "NEVENTS\t";
4027          cout << "absolute time\t\t\t";
4028          cout << "on-board time";
4029          cout << endl;
4030        }
4031        for (Int_t ii = 0; ii < R->GetEntries(); ii++) {
4032          R->GetEntry(ii);
4033          if (DBG) {
4034            cout << ii << "\t | ";
4035            cout << R->GetTreeNumber() << "\t |";
4036            //          cout << R->GetChainOffset()<< "\t |";
4037            cout << GetRunInfo()->ID << "\t";
4038            cout << GetRunInfo()->ID_RUN_FRAG << "\t";
4039            cout << GetRunInfo()->NEVENTS << "\t";
4040            cout << GetRunInfo()->RUNHEADER_TIME << " <---> " << GetRunInfo()->RUNTRAILER_TIME << "\t";
4041            cout << GetRunInfo()->RUNHEADER_OBT << " <---> " << GetRunInfo()->RUNTRAILER_OBT << "\t";
4042            cout << endl;
4043          }
4044          irunoffset[R->GetTreeNumber()] = R->GetChainOffset();
4045      }      }
4046        cout << "N.run = " << R->GetEntries() << endl;
4047        cout << "----------------------------------------------------" << endl;
4048    
4049      }
4050    //   else {
4051    //     delete R;
4052    //     R = 0;
4053    //   }
4054    
4055      run_tree = R;    run_tree = R;
4056    
4057      return R;
4058    
     return R;  
       
4059  }  }
4060  //--------------------------------------  //--------------------------------------
4061  //  //
# Line 1854  TChain *PamLevel2::GetRunTree(TList *fl) Line 4063  TChain *PamLevel2::GetRunTree(TList *fl)
4063  //--------------------------------------  //--------------------------------------
4064  /**  /**
4065   * Get the Run tree  from a file.   * Get the Run tree  from a file.
4066   * @param f Pointer to a TFile   * @param f Pointer to a TFile
4067   * @return Pointer to a TTree   * @return Pointer to a TTree
4068   */   */
4069  TTree *PamLevel2::GetRunTree(TFile *f){  TTree *PamLevel2::GetRunTree(TFile *f) {
4070    if ( run_tree ){    if (run_tree) {
4071      printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");      printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");
4072      return run_tree;      return run_tree;
4073    };    };
           
4074    
4075      cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;    cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte " << endl;
4076    
4077      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  
     }  
4078    
4079      run_tree = (TChain*)T;    if (T) {
4080        T->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
4081        cout << "Run         : set branch address RunInfo" << endl;
4082        T->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
4083        cout << "Software    : set branch address SoftInfo" << endl; // Emiliano
4084    
4085      }
4086    
4087      run_tree = (TChain*) T;
4088    
4089      return T;
4090    
     return T;  
       
4091  }  }
 /**  
  * 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;  
4092    
4093    // --------------------------------------  Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) {
   // if it is a full file (not preselected)  
   // --------------------------------------  
   if(SELLI==0){  
4094    
4095        //    if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - inside\n");
       // 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);  
       }  
4096    
4097        ULong64_t abstime = 0;    if (!run_tree) {
4098        if( GetOrbitalInfo() )abstime=GetOrbitalInfo()->absTime;      cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded" << endl;
4099        return false;
4100      }
4101      if (run_tree->GetEntries() <= 0) {
4102        cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty" << endl;
4103        return (false);
4104      }
4105    
4106    
4107        //    Int_t oldrun = irun; // store current run index
       // 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;  
           }  
4108    
4109        };          // -----------------------------------------------------------------------
4110        //    // the first time the routine is called, set run search from the beginning
4111        if(ISGP)abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO    // -----------------------------------------------------------------------
4112        //  
4113        if ( irun == run->GetEntries()-1LL &&    if (irun < 0) {
4114             !(abstime >= GetRunInfo()->RUNHEADER_TIME &&      irun = 0LL;
4115               abstime <= GetRunInfo()->RUNTRAILER_TIME)      irunt = 0LL;
4116              ){      totrunentry = 0LL;
4117           irun = -1LL;      totrunentrymin = 0LL;
4118           runfirstentry = 0LL;      totrunentrymax = 0LL;
4119           runlastentry = -1LL;      irunentry = 0LL;
4120         };      il0entry = 0LL;
4121        // modificato il controllo sull'aggiornamento del run, per evitare problemi      prevshift = 0;
4122        // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)      yprevshift = 0;
4123        //      prevabstime = 0;
4124        bool fromfirst = true;      prevpktnum = 0;
4125        //      abstime = 0ULL;
4126        while ( !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME) && irun < run->GetEntries()-1LL ){      pktnum = 0;
4127  //      while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){      isFragment = false;
4128            irun++;      run_tree->GetEntry(irun);
4129            run->GetEntry(irun);      if (!GetOrbitalInfo())
4130            runfirstentry = runlastentry;          cout << "PamLevel2::UpdateRunInfo(Long64_t "<<iev<<") ** WARNING ** missing OrbitalInfo    ORB="<<ORB << endl;
4131            if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runfirstentry += 1LL;      if ( fUseDBinRunInfo ){
4132            runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);        if (gltsync)
4133  //        cout << " ))))) UPDATE RUN INFO (((((  @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;          delete gltsync; //Emiliano
4134  //        cout << "runfirstentry "<<runfirstentry<<endl;        if (!dbc || (dbc && !dbc->IsConnected()))
4135            //      printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));          SetDBConnection(); //Emiliano
4136            //      printf(" abstime %u trailertime %u \n",abstime,GetRunInfo()->RUNTRAILER_TIME);        gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); //Emiliano // the "false" means not to use level0 file (not necessary here)
4137            //      printf(" IDRUN %u \n",GetRunInfo()->ID);        if (dbc){
4138            //          dbc->Close();// Emiliano
4139  //        prevshift = 0;          delete dbc;
4140            //          dbc=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;  
4141        }        }
4142        return false;      }
4143      }
4144      // ---------------------------------------------------------------
4145      // retrieve OBT and absolute time of the event
4146      // ---------------------------------------------------------------
4147      Long64_t obt = 0LL; // Emiliano, Long64_t GL_TIMESYNC::DBobt(UInt_t obt) since depending on the situation OBT is lowered or boosted
4148      prevabstime = abstime;
4149      prevpktnum = pktnum;
4150      if (GetOrbitalInfo()) {
4151        abstime = GetOrbitalInfo()->absTime;
4152        if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano
4153        pktnum = GetOrbitalInfo()->pkt_num; // Emiliano
4154      }
4155      else {
4156        abstime = GetRunInfo()->RUNHEADER_TIME;
4157        if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano
4158        pktnum = GetRunInfo()->RUNHEADER_PKT; // Emiliano
4159    }    }
4160    
4161    return false;    if (DBG){
4162    //      printf("0abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4163  };      printf("0        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4164        printf("0        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4165        if ( fUseDBinRunInfo ) printf("0        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4166        printf("0        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4167        printf("0        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
4168      }
4169    
4170  Bool_t PamLevel2::UpdateRunInfo(Long64_t iev){    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4171      // if it is a full file (not preselected)
4172      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4173      if (SELLI == 0 || SELLI == 2) { // Emiliano
4174    
4175      if(!run_tree){      // ---------------------------------------------------------------
4176          cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded"<<endl;      // increment dead and live-time counters
4177          return false;      // (only when reading a file not preselected)
4178      }      // ---------------------------------------------------------------
4179      if ( run_tree->GetEntries() <= 0 ) {      if (SELLI == 0) {
4180          cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty"<<endl;        if (GetTrigLevel2()) {
4181          return(false);          totdltime[0] += GetTrigLevel2()->dltime[0];
4182            totdltime[1] += GetTrigLevel2()->dltime[1];
4183          }
4184          totdltime[2]++;
4185      }      }
     
     Int_t oldrun = irun; // store current run index  
4186    
4187      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-      //
4188      // if it is a full file (not preselected)      bool a = true;
4189      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-      bool b = true;
4190      if(SELLI==0){      if ( fUseDBinRunInfo ){
4191          a = false;    
4192          ULong64_t abstime = 0;        b = false;
4193          ULong64_t obt     = 0;        if ( obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) a = true;
4194          if( GetOrbitalInfo() ){        if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true;
4195              abstime = GetOrbitalInfo()->absTime;      }
4196              obt     = GetOrbitalInfo()->OBT;      if ( iev < totrunentrymin || iev > totrunentrymax // entry is outside run limits
4197          }           || iev == 0 // or it is the first entry
4198          // ---------------------------------------------------------------           || (!isSync && (
4199          // the absolute time is necessary to relate the event with the run                           (abstime <= GetRunInfo()->RUNHEADER_TIME && a ) // or it is outside obt limits (and abstime limits for security reasons)
4200          // ---------------------------------------------------------------                           || (abstime >= GetRunInfo()->RUNTRAILER_TIME && b ) ))// or it is outside obt limits (and abstime limits for security reasons)
4201          if( !GetOrbitalInfo() && !ISGP ){           ){ // check on abstime and obt needed to handle nested+DV_skipped packets
             cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;  
             return(false);  
         }  
4202                
4203          // -----------------------------------------------------------------------        // check for a new run (ma prima il primo!)
4204          // the first time the routine is called, set run search from the beginning        if (DBG){
4205          // -----------------------------------------------------------------------          printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4206          if ( irun < 0 ){          printf("1        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4207              irun = 0LL;          printf("1        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4208              irunentry = 0;          if ( fUseDBinRunInfo ) printf("1        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4209              prevshift = 0;          printf("1        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4210              run_tree->GetEntry(irun);          printf("1        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
4211          }
4212              if( ISGP && run_tree->GetEntries()!=1 ){        //        printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4213                  cout << "** WARNING ** simulated files are assumed to have 1 single run, not "<< run_tree->GetEntries() << endl;        //        printf("1        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4214                  cout << "** WARNING ** run will not be updated"<<endl;        //        printf("1        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4215              }        //        printf("1        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4216          };              //        printf("1        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4217          //        printf("1        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
4218    
4219          totrunentry = 0LL;
4220          runfirstentry = 0LL;
4221          for (Int_t r=0; r< run_tree->GetEntries();r++){
4222            // -------------------------------------------------------------------
4223            // save the index of the first entry of the run, relative to pam_tree,
4224            // and read a new run
4225            // -------------------------------------------------------------------
4226            run_tree->GetEntry(r);//update runinfo
4227            if ( r > 0 ){
4228              totrunentrymin = totrunentrymax+1;
4229            } else {
4230              totrunentrymin = 0LL;
4231            }
4232            totrunentry += GetRunInfo()->NEVENTS;
4233            totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
4234            irun = r;        
4235            if ( fUseDBinRunInfo ){
4236              a = false;    
4237              b = false;
4238              if ( obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) a = true;
4239              if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true;
4240            }
4241            if ( (iev >= totrunentrymin && iev <= totrunentrymax) || // entry is inside run limits
4242                 ( !isSync &&
4243                   ( abstime >= GetRunInfo()->RUNHEADER_TIME  && a // or it is inside obt limits (and abstime limits for security reasons)
4244                     && abstime <= GetRunInfo()->RUNTRAILER_TIME && b))  // or it is inside obt limits (and abstime limits for security reasons)
4245                 ){ // check on abstime and obt needed to handle nested+DV_skipped packets
4246              if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
4247                if ( !isSync ){
4248                  if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
4249                  if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4250                  //              printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
4251                  //              printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4252                  prevshift += (totrunentrymin-iev); // add the new shift to total shift
4253                  totrunentrymin -= (totrunentrymin-iev); // shift run position min
4254                  totrunentrymax -= (totrunentrymin-iev); // shift run position max
4255                  if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4256                  //              printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4257                } else {
4258                  printf(" PamLevel2::UpdateRunInfo(Long64_t) ERROR! sync file but unconsistent totrunetrymin %lld and iev %lld!!! \n",totrunentrymin,iev);
4259                  cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4260                  cout << "\nFor bug reporting instructions, please see for example:\n";
4261                  cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4262                  cout << "  " << endl;
4263                }
4264              }
4265              runfirstentry = totrunentrymin; // first entry of the run in the level2
4266              
4267    
4268              //
4269              if ( fUseDBinRunInfo ){
4270                if (gltsync)
4271                  delete gltsync; // Emiliano
4272                if (!dbc || (dbc && !dbc->IsConnected()))
4273                  SetDBConnection(); //Emiliano
4274                gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
4275                TrkParams::Set(GetRunInfo(), dbc);
4276                if (dbc){
4277                  dbc->Close(); // Emiliano
4278                  delete dbc;
4279                  dbc=0;
4280                }          
4281                if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
4282                  cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
4283                       << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
4284                  cout
4285                    << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
4286                    << endl;
4287                }
4288              }
4289              //
4290              if (DBG) printf(" found \n");
4291              //          printf(" found \n");//TOGLITOGLI
4292              //
4293              break;
4294            }
4295          } // loop over run
4296          
4297          // --------------------------------------
4298          // if there was no need to update the run
4299          // ---> exit with FALSE
4300          // --------------------------------------
4301          if (irun == oldrun){
4302            if (DBG) printf(" no new run \n");
4303            //        printf(" no new run \n");//TOGLITOGLI
4304            return (false);
4305          }      
4306          // --------------------------------------
4307          // ... otherwise
4308          // ---> exit with TRUE
4309          // --------------------------------------
4310    
4311          if (SELLI != 2) {
4312            /// decrement counters since this event belongs to a new run
4313            if (GetTrigLevel2()) {
4314              totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time
4315              totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time
4316            }
4317            totdltime[2]--; //event counter
4318            if (DBG) {
4319              cout << endl;
4320              cout << "n.events     : " << totdltime[2] << endl;
4321              cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;
4322              cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;
4323            }
4324            // add an entry
4325            if (run_tree_clone && totdltime[2] > 0)
4326              if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
4327                run_tree_clone->GetBranch("DeadLiveTime")->Fill();
4328            // reset counters
4329            if ( totdltime[2] > 0 ){
4330              if (GetTrigLevel2()) {
4331                totdltime[0] = GetTrigLevel2()->dltime[0];//live-time
4332                totdltime[1] = 0; //dead-time
4333              }
4334              totdltime[2] = 1; //event counter
4335            }
4336          }
4337    
4338          // -----------------------------------------------------------------------        if (DBG){
4339          // if it is simulation, assigh abstime by end (temporaneo!!!)          cout << endl << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun
4340          // -----------------------------------------------------------------------               << endl;        
4341          if(ISGP){          printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4342              abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO          printf("2        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4343              obt     = GetRunInfo()->RUNHEADER_OBT; // BARBATRUCCO          printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4344          }          if ( fUseDBinRunInfo ) printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4345          //          printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4346          bool fromfirst = true; // first loop over runs          printf("2        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
4347          }
4348          //        printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4349          //        printf("2        rth %d %d nevents %d  \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4350          //        printf("2        rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4351          //        printf("2        rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4352          //        printf("2        bo irunentry %lld prevshift %lld irun %lld  \n",irunentry,prevshift,irun);
4353          //        printf("2        min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
4354    
4355          return (true);
4356        } // need for run upgrade
4357        if (DBG) printf("return false\n");
4358        return (false);
4359      }// SELLI = 0 SELLI = 2
4360      
4361      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4362      // if it is a preselected file (there is SelectionList)
4363      // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4364      // irun  = run entry relative to the chain
4365      // irunt = run entry relative to the tree
4366      if (SELLI == 1) {
4367        sel_tree->GetEntry(iev);// read irunt from SelectionList
4368        irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW
4369        if (irun != oldrun) {
4370          if (irun < run_tree->GetEntries())
4371            run_tree->GetEntry(irun);
4372          // check if the time is ok (with merged files it is not...)
4373          // if not loop over run and look for the proper entry
4374          bool SECONDO_GIRO = false;
4375          //            Long64_t irun_start   = irun;
4376          int offset_start = irunoffset[sel_tree->GetTreeNumber()];
4377          while (((!(abstime >= GetRunInfo()->RUNHEADER_TIME && // check on absolute time (s)
4378              abstime <= GetRunInfo()->RUNTRAILER_TIME)
4379          //                        ||
4380          //                        !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)
4381          //                          obt <= GetRunInfo()->RUNTRAILER_OBT)
4382          ) || GetRunInfo()->NEVENTS == 0)
4383          //                && irun < run_tree->GetEntries()
4384          ) {
4385    
4386            if (DBG) {
4387              cout << " (test) ";
4388              cout << " tree " << sel_tree->GetTreeNumber();
4389              cout << " irunt " << irunt;
4390              cout << " offset " << irunoffset[sel_tree->GetTreeNumber()];
4391              cout << " abs " << abstime;
4392              cout << " >> " << GetRunInfo()->RUNHEADER_TIME << " " << GetRunInfo()->RUNTRAILER_TIME;
4393              cout << " obt " << obt;
4394              cout << " >> " << GetRunInfo()->RUNHEADER_OBT << " " << GetRunInfo()->RUNTRAILER_OBT;
4395              cout << " *** JUMP RUN *** irun " << irun;
4396              cout << endl;
4397            }
4398            //              irun++;
4399            irunoffset[sel_tree->GetTreeNumber()]++;
4400            irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW
4401            if (irun == run_tree->GetEntries() && SECONDO_GIRO) {
4402              //            if(irun == irun_start ){
4403              cout << " ...grrrvzzkhhhajsdkj!!!! " << endl;
4404              irunoffset[sel_tree->GetTreeNumber()] = offset_start;
4405              return false;
4406            }
4407            if (irun >= run_tree->GetEntries() || irun < 0) {
4408              cout << "irun = " << irun << " >>  search from the beginning... <<" << endl;
4409              SECONDO_GIRO = true;
4410              irun = 0;
4411              irunoffset[sel_tree->GetTreeNumber()] = -irunt;
4412            }
4413            run_tree->GetEntry(irun);
4414          }
4415    
4416  //      Bool_t hasfrag = false;        if (DBG) {
4417  //      if( GetRunInfo()->ID_RUN_FRAG!=0 && GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID)hasfrag=true;          cout << " (test) ";
4418  //      ULong64_t fragid = GetRunInfo()->ID_RUN_FRAG;          cout << " tree " << sel_tree->GetTreeNumber();
4419            cout << " irunt " << irunt;
4420          // ------------------------------------------------------          cout << " offset " << irunoffset[sel_tree->GetTreeNumber()];
4421          // loop over runs to find the one that contains the event          cout << " abs " << abstime;
4422          // ------------------------------------------------------          cout << " >> " << GetRunInfo()->RUNHEADER_TIME << " " << GetRunInfo()->RUNTRAILER_TIME;
4423          while (          cout << " obt " << obt;
4424              (          cout << " >> " << GetRunInfo()->RUNHEADER_OBT << " " << GetRunInfo()->RUNTRAILER_OBT;
4425                  (        }
4426                      !(abstime >= GetRunInfo()->RUNHEADER_TIME &&    // check on absolute time (s)        if (DBG)
4427                        abstime <= GetRunInfo()->RUNTRAILER_TIME) &&          cout << endl;
4428                      !(obt >= GetRunInfo()->RUNHEADER_OBT &&         // additional check on OBT (ms)        if (DBG)
4429                        obt <= GetRunInfo()->RUNTRAILER_OBT)          cout << endl << " ))))) UPDATE RUN INFO (((((  @iev " << iev << " run " << GetRunInfo()->ID << " (run-entry "
4430                      )              << irun << ")" << endl;
4431                  || GetRunInfo()->NEVENTS==0        // ----------------------------------------------------
4432                  || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift)        // update the tracker parameters
4433                  )        // (non ho trovato nessun altro modo sicuro di farlo...)
4434                  && irun < run_tree->GetEntries() ){        // ----------------------------------------------------
4435          if ( fUseDBinRunInfo ){
4436              irun++;          if (!dbc || (dbc && !dbc->IsConnected()))
4437              // ------------------------------------            SetDBConnection();
4438              // if the end of run tree is reached...          TrkParams::Set(GetRunInfo(), dbc);
4439              // ------------------------------------          if (dbc){
4440              if( irun == run_tree->GetEntries() ){            dbc->Close();
4441                  if(!fromfirst){            delete dbc;
4442                      // -----------------------------------------------------            dbc=0;
4443                      // if it happened already once and the run was not found          }
4444                      // ---> exit with error        }
4445                      // -----------------------------------------------------        //            cout << endl;
4446                      cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- event entry #"<<iev<<" does not belong to any run (should not happen)" <<endl;        prevshift = 0;
4447                      return false;        yprevshift = 0;
4448                  }        return true;
                 // -----------------------------------------  
                 // ...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  
         // ---> 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;  
4449      }      }
   
4450      return false;      return false;
4451      //    }
4452  };  
4453      return false;
4454      //
4455    }
4456    
4457  /**  /**
4458   * 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)
4459   * @param run Pointer to the chain/tree which contains run infos   * @param run Pointer to the chain/tree which contains run infos
4460   * @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
4461   */   */
4462  Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev){  Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) {
4463    return(UpdateRunInfo((TChain*)run,iev));    return (UpdateRunInfo(iev));
4464  };  }
4465    
4466    /**
4467     * Update the runinfo informations (to be used to have Run infos event by event basis)
4468     * @param run Pointer to the chain/tree which contains run infos
4469     * @return true if a new run has been read, false if it is still the same run
4470     */
4471    Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {
4472      return (UpdateRunInfo((TChain*) run, iev));
4473    }
4474    
4475    
4476  //--------------------------------------  //--------------------------------------
4477  //  //
# Line 2186  Bool_t PamLevel2::UpdateRunInfo(TTree *r Line 4479  Bool_t PamLevel2::UpdateRunInfo(TTree *r
4479  //--------------------------------------  //--------------------------------------
4480  /**  /**
4481   * Set which trees shoul be analysed   * Set which trees shoul be analysed
4482   * @param detlist TString containing the sequence of trees required   * @param detlist TString containing the sequence of trees required
4483  */   */
4484  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;  
     };  
4485    
4486      if( detlist.Contains("CAL0", TString::kIgnoreCase) ){    //    if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){
4487          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;  
     };  
4488    
4489      if( detlist.Contains("TRK1", TString::kIgnoreCase) ){      cout << " ======================================================== " << endl;
4490          if ( detlist.Contains("-TRK1", TString::kIgnoreCase) )TRK1=false;      cout << "                       (( WARNING ))                      " << endl;
4491          if ( detlist.Contains("+TRK1", TString::kIgnoreCase) )TRK1=true;      cout << " The meaning of the option +ALL has changed!!             " << endl;
4492      };      cout << " Now it includes really all (level0+level1+level2+gpamela)" << endl;
4493                cout << " and the file is discarded if it does not contain         " << endl;
4494      if( detlist.Contains("TRK2", TString::kIgnoreCase)){      cout << " all trees or  if level0 files are not available!!        " << endl;
4495          if ( detlist.Contains("-TRK2", TString::kIgnoreCase) )TRK2=false;      cout << " ======================================================== " << endl;
         if ( detlist.Contains("+TRK2", TString::kIgnoreCase) )TRK2=true;  
     };  
4496    
4497      if( detlist.Contains("TRKh", TString::kIgnoreCase)){      CAL0 = true;
4498          if ( detlist.Contains("-TRKh", TString::kIgnoreCase) )TRKh=false;      CAL1 = true;
4499          if ( detlist.Contains("+TRKh", TString::kIgnoreCase) )TRKh=true;      CAL2 = true;
4500      };      TRK2 = true;
4501                TRK1 = true;
4502      if( detlist.Contains("+TRK", TString::kIgnoreCase) && !TRK1 && !TRK2 && !TRKh )TRK2=true;      TRKh = true;
4503      if( detlist.Contains("-TRK", TString::kIgnoreCase) && TRK1 && TRK2 && TRKh){      TRK0 = true;
4504          TRK2=false;      TRG = true;
4505          TRK1=false;      TOF = true;
4506          TRKh=false;      TOF0 = true;
4507      }      S4 = true;
4508  //  -------------------------------------------------------------------------      ND = true;
4509            AC = true;
4510      if( detlist.Contains("-TRG", TString::kIgnoreCase) )TRG = false;      ORB = true;
4511      else if( detlist.Contains("+TRG", TString::kIgnoreCase) )TRG = true;      GP = true;
4512          }
4513      if( detlist.Contains("-TOF", TString::kIgnoreCase) )TOF = false;    else if (detlist.Contains("-ALL", TString::kIgnoreCase)) {
4514      else if( detlist.Contains("+TOF", TString::kIgnoreCase) )TOF = true;      CAL0 = false;
4515        CAL1 = false;
4516        CAL2 = false;
4517        TRK2 = false;
4518        TRK1 = false;
4519        TRKh = false;
4520        TRK0 = false;
4521        TRG = false;
4522        TOF = false;
4523        TOF0 = false;
4524        S4 = false;
4525        ND = false;
4526        AC = false;
4527        ORB = false;
4528        GP = false;
4529      };
4530    
4531      if( detlist.Contains("-TOF0", TString::kIgnoreCase) )TOF0 = false;    //  -------------------------------------------------------------------------
4532      else if( detlist.Contains("+TOF0", TString::kIgnoreCase) )TOF0 = true;    if (detlist.Contains("CAL1", TString::kIgnoreCase)) {
4533            if (detlist.Contains("-CAL1", TString::kIgnoreCase))
4534      if( detlist.Contains("-S4",  TString::kIgnoreCase) )S4  = false;        CAL1 = false;
4535      else if( detlist.Contains("+S4",  TString::kIgnoreCase) )S4  = true;      if (detlist.Contains("+CAL1", TString::kIgnoreCase))
4536              CAL1 = true;
4537      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;  
4538    
4539  //     cout<< "Set detector list --> ";    if (detlist.Contains("CAL0", TString::kIgnoreCase)) {
4540  //     if(TRK1)cout<<"TRK1 ";      if (detlist.Contains("-CAL0", TString::kIgnoreCase))
4541  //     if(TRK2)cout<<"TRK2 ";        CAL0 = false;
4542  //     if(TRKh)cout<<"TRKH ";      if (detlist.Contains("+CAL0", TString::kIgnoreCase))
4543  //     if(CAL1)cout<<"CAL1 ";        CAL0 = true;
4544  //     if(CAL2)cout<<"CAL2 ";    };
4545  //     if(TOF)cout<<"TOF ";  
4546  //     if(TRG)cout<<"TRG ";    if (detlist.Contains("CAL2", TString::kIgnoreCase)) {
4547  //     if(AC)cout<<"AC ";      if (detlist.Contains("-CAL2", TString::kIgnoreCase))
4548  //     if(ND)cout<<"ND ";        CAL2 = false;
4549  //     if(S4)cout<<"S4 ";      if (detlist.Contains("+CAL2", TString::kIgnoreCase))
4550  //     if(ORB)cout<<"ORB ";        CAL2 = true;
4551  //     cout << endl;    };
4552        
4553  };    if (detlist.Contains("+CAL", TString::kIgnoreCase) && !CAL1 && !CAL2)
4554        CAL2 = true;
4555      if (detlist.Contains("-CAL", TString::kIgnoreCase) && CAL1 && CAL2) {
4556        CAL2 = false;
4557        CAL1 = false;
4558      }
4559      //  -------------------------------------------------------------------------
4560      if (detlist.Contains("TRK0", TString::kIgnoreCase)) {
4561        if (detlist.Contains("-TRK0", TString::kIgnoreCase))
4562          TRK0 = false;
4563        if (detlist.Contains("+TRK0", TString::kIgnoreCase))
4564          TRK0 = true;
4565      };
4566    
4567      if (detlist.Contains("TRK1", TString::kIgnoreCase)) {
4568        if (detlist.Contains("-TRK1", TString::kIgnoreCase))
4569          TRK1 = false;
4570        if (detlist.Contains("+TRK1", TString::kIgnoreCase))
4571          TRK1 = true;
4572      };
4573    
4574      if (detlist.Contains("TRK2", TString::kIgnoreCase)) {
4575        if (detlist.Contains("-TRK2", TString::kIgnoreCase))
4576          TRK2 = false;
4577        if (detlist.Contains("+TRK2", TString::kIgnoreCase))
4578          TRK2 = true;
4579      };
4580    
4581      if (detlist.Contains("TRKh", TString::kIgnoreCase)) {
4582        if (detlist.Contains("-TRKh", TString::kIgnoreCase))
4583          TRKh = false;
4584        if (detlist.Contains("+TRKh", TString::kIgnoreCase))
4585          TRKh = true;
4586      };
4587    
4588      if (detlist.Contains("+TRK", TString::kIgnoreCase) && !TRK1 && !TRK2 && !TRKh)
4589        TRK2 = true;
4590      if (detlist.Contains("-TRK", TString::kIgnoreCase) && TRK1 && TRK2 && TRKh) {
4591        TRK2 = false;
4592        TRK1 = false;
4593        TRKh = false;
4594      }
4595      //  -------------------------------------------------------------------------
4596    
4597      if (detlist.Contains("-TRG", TString::kIgnoreCase))
4598        TRG = false;
4599      else if (detlist.Contains("+TRG", TString::kIgnoreCase))
4600        TRG = true;
4601    
4602      if (detlist.Contains("-TOF", TString::kIgnoreCase))
4603        TOF = false;
4604      else if (detlist.Contains("+TOF", TString::kIgnoreCase))
4605        TOF = true;
4606    
4607      if (detlist.Contains("-TOF0", TString::kIgnoreCase))
4608        TOF0 = false;
4609      else if (detlist.Contains("+TOF0", TString::kIgnoreCase))
4610        TOF0 = true;
4611    
4612      if (detlist.Contains("-S4", TString::kIgnoreCase))
4613        S4 = false;
4614      else if (detlist.Contains("+S4", TString::kIgnoreCase))
4615        S4 = true;
4616    
4617      if (detlist.Contains("-ND", TString::kIgnoreCase))
4618        ND = false;
4619      else if (detlist.Contains("+ND", TString::kIgnoreCase))
4620        ND = true;
4621    
4622      if (detlist.Contains("-AC", TString::kIgnoreCase))
4623        AC = false;
4624      else if (detlist.Contains("+AC", TString::kIgnoreCase))
4625        AC = true;
4626    
4627      if (detlist.Contains("-ORB", TString::kIgnoreCase))
4628        ORB = false;
4629      else if (detlist.Contains("+ORB", TString::kIgnoreCase))
4630        ORB = true;
4631    
4632      if (detlist.Contains("-GP", TString::kIgnoreCase))
4633        GP = false;
4634      else if (detlist.Contains("+GP", TString::kIgnoreCase))
4635        GP = true;
4636    
4637      cout << "tree/branch list from input --> ";
4638      if (TRK0)
4639        cout << "TRK0 ";
4640      if (TRK1)
4641        cout << "TRK1 ";
4642      if (TRK2)
4643        cout << "TRK2 ";
4644      if (TRKh)
4645        cout << "TRKH ";
4646      if (CAL0)
4647        cout << "CAL0 ";
4648      if (CAL1)
4649        cout << "CAL1 ";
4650      if (CAL2)
4651        cout << "CAL2 ";
4652      if (TOF)
4653        cout << "TOF ";
4654      if (TRG)
4655        cout << "TRG ";
4656      if (AC)
4657        cout << "AC ";
4658      if (ND)
4659        cout << "ND ";
4660      if (S4)
4661        cout << "S4 ";
4662      if (ORB)
4663        cout << "ORB ";
4664      if (GP)
4665        cout << "GP ";
4666      cout << endl;
4667      //     cout<< "Set detector list --> ";
4668      //     if(TRK1)cout<<"TRK1 ";
4669      //     if(TRK2)cout<<"TRK2 ";
4670      //     if(TRKh)cout<<"TRKH ";
4671      //     if(CAL1)cout<<"CAL1 ";
4672      //     if(CAL2)cout<<"CAL2 ";
4673      //     if(TOF0)cout<<"TOF0 ";
4674      //     if(TOF)cout<<"TOF ";
4675      //     if(TRG)cout<<"TRG ";
4676      //     if(AC)cout<<"AC ";
4677      //     if(ND)cout<<"ND ";
4678      //     if(S4)cout<<"S4 ";
4679      //     if(ORB)cout<<"ORB ";
4680      //     cout << endl;
4681    
4682    }
4683    ;
4684    
4685  /**  /**
4686   * Set tree/branch detector flags from the content of a tree   * Set tree/branch detector flags from the content of a tree
4687   */   */
4688  void  PamLevel2::GetWhichTrees(TFile* f){  void PamLevel2::GetWhichTrees(TFile* f) {
       
4689    
4690      //    cout << "void  PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;
4691      // -----------
4692      // reset flags
4693      // -----------
4694      CAL1 = false;
4695      CAL2 = false;
4696      TRK2 = false;
4697      TRK1 = false;
4698      TRKh = false;
4699      TRG = false;
4700      TOF = false;
4701      S4 = false;
4702      ND = false;
4703      AC = false;
4704      ORB = false;
4705      GP = false;
4706    
4707      RUN = false;
4708    
4709      //    cout << "Checking file: "<<f->GetName()<<endl;
4710      if (!f || f->IsZombie()) {
4711        cout << "File: " << f->GetName() << " Non valid root file" << endl;
4712        fDiscarded = true;
4713        return;
4714      }
4715    
4716      cout << "void  PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;    TList *lk = f->GetListOfKeys();
4717      // -----------    if (!lk)
4718      // reset flags      return;
4719      // -----------    TIter next(lk);
4720      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;  
     }  
4721    
4722      TList *lk = f->GetListOfKeys();    Int_t nev = 0;
     TIter next(lk);  
     TKey *key =0;  
4723    
4724      Int_t nev = 0;    while ((key = (TKey*) next())) {
4725    
4726      while( (key = (TKey*)next()) ){      if (!strcmp(key->GetName(), "Run"))
4727                  RUN = true;
4728          if( !strcmp(key->GetName(),"Run"        ) )RUN = true;  
4729        //=========================================================
4730        if (!strcmp(key->GetName(), "Trigger")) {
4731          TRG = true;
4732          Int_t nevt = ((TTree*) f->Get("Trigger"))->GetEntries();
4733          if (nev && nevt != nev) {
4734            cout << "File: " << f->GetName() << " Trigger tree has " << nevt << " events instead of " << nev << endl;
4735            TRG = false;
4736          }
4737          else
4738            nev = nevt;
4739        }
4740        //=========================================================
4741        if (!strcmp(key->GetName(), "ToF")) {
4742          TOF = true;
4743          Int_t nevt = ((TTree*) f->Get("ToF"))->GetEntries();
4744          if (nev && nevt != nev) {
4745            cout << "File: " << f->GetName() << "     ToF tree has " << nevt << " events instead of " << nev << endl;
4746            TOF = false;
4747          }
4748          else
4749            nev = nevt;
4750        }
4751        //=========================================================
4752        if (!strcmp(key->GetName(), "S4")) {
4753          S4 = true;
4754          Int_t nevt = ((TTree*) f->Get("S4"))->GetEntries();
4755          if (nev && nevt != nev) {
4756            cout << "File: " << f->GetName() << "      S4 tree has " << nevt << " events instead of " << nev << endl;
4757            S4 = false;
4758          }
4759          else
4760            nev = nevt;
4761        }
4762        //=========================================================
4763    
4764          //=========================================================          if (!strcmp(key->GetName(), "NeutronD")) {
4765          if( !strcmp(key->GetName(),"Trigger"    ) ){        ND = true;
4766              TRG = true;        Int_t nevt = ((TTree*) f->Get("NeutronD"))->GetEntries();
4767              Int_t nevt = ((TTree*)f->Get("Trigger"))->GetEntries();        if (nev && nevt != nev) {
4768              if( nev && nevt!=nev){          cout << "File: " << f->GetName() << "NeutronD tree has " << nevt << " events instead of " << nev << endl;
4769                  cout << "File: "<< f->GetName() <<" Trigger tree has "<<nevt<<" events instead of "<<nev<< endl;          ND = false;
4770                  TRG = false;        }
4771              }else nev=nevt;        else
4772          }          nev = nevt;
4773          //=========================================================          }
4774          if( !strcmp(key->GetName(),"ToF"        ) ){      //=========================================================
4775              TOF = true;      if (!strcmp(key->GetName(), "Anticounter")) {
4776              Int_t nevt = ((TTree*)f->Get("ToF"))->GetEntries();        AC = true;
4777              if( nev && nevt!=nev){        Int_t nevt = ((TTree*) f->Get("Anticounter"))->GetEntries();
4778                  cout << "File: "<< f->GetName() <<"     ToF tree has "<<nevt<<" events instead of "<<nev<< endl;        if (nev && nevt != nev) {
4779                  TOF = false;          cout << "File: " << f->GetName() << " Anticounter tree has " << nevt << " events instead of " << nev << endl;
4780              }else nev=nevt;          AC = false;
4781          }        }
4782          //=========================================================          else
4783          if( !strcmp(key->GetName(),"S4"         ) ){          nev = nevt;
4784              S4 = true;      }
4785              Int_t nevt = ((TTree*)f->Get("S4"))->GetEntries();      //=========================================================
4786              if( nev && nevt!=nev){      if (!strcmp(key->GetName(), "OrbitalInfo")) {
4787                  cout << "File: "<< f->GetName() <<"      S4 tree has "<<nevt<<" events instead of "<<nev<< endl;        ORB = true;
4788                  S4 = false;        Int_t nevt = ((TTree*) f->Get("OrbitalInfo"))->GetEntries();
4789              }else nev=nevt;        if (nev && nevt != nev) {
4790          }          cout << "File: " << f->GetName() << " OrbitalInfo tree has " << nevt << " events instead of " << nev << endl;
4791          //=========================================================            ORB = false;
4792          }
4793          else
4794            nev = nevt;
4795        }
4796        //=========================================================
4797        if (!strcmp(key->GetName(), "Tracker")) {
4798          TTree *T = (TTree*) f->Get("Tracker");
4799          for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
4800            TString name = T->GetListOfBranches()->At(i)->GetName();
4801            if (!name.CompareTo("TrkLevel1"))
4802              TRK1 = true;
4803            if (!name.CompareTo("TrkLevel2"))
4804              TRK2 = true;
4805            if (!name.CompareTo("TrkHough"))
4806              TRKh = true;
4807          };
4808          Int_t nevt = T->GetEntries();
4809          if (nev && nevt != nev) {
4810            cout << "File: " << f->GetName() << " Tracker tree has " << nevt << " events instead of " << nev << endl;
4811            TRK1 = false;
4812            TRK2 = false;
4813            TRKh = false;
4814          }
4815          else
4816            nev = nevt;
4817          //            T->Delete();
4818        };
4819        //=========================================================
4820        if (!strcmp(key->GetName(), "Calorimeter")) {
4821          TTree *T = (TTree*) f->Get("Calorimeter");
4822          for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
4823            TString name = T->GetListOfBranches()->At(i)->GetName();
4824            if (!name.CompareTo("CaloLevel1"))
4825              CAL1 = true;
4826            if (!name.CompareTo("CaloLevel2"))
4827              CAL2 = true;
4828          };
4829          Int_t nevt = T->GetEntries();
4830          if (nev && nevt != nev) {
4831            cout << "File: " << f->GetName() << "  Calorimeter tree has " << nevt << " events instead of " << nev << endl;
4832            CAL1 = false;
4833            CAL2 = false;
4834          }
4835          else
4836            nev = nevt;
4837          //            T->Delete();
4838        };
4839        //=========================================================
4840        if (!strcmp(key->GetName(), "h20")) {
4841          GP = true;
4842          Int_t nevt = ((TTree*) f->Get("h20"))->GetEntries();
4843          if (nev && nevt != nev) {
4844            cout << "File: " << f->GetName() << " h20 tree has " << nevt << " events instead of " << nev << endl;
4845            GP = false;
4846          }
4847          else
4848            nev = nevt;
4849        }
4850    
4851          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();  
         };        
4852    
4853      };    //    delete lk;
       
     delete lk;  
4854    
4855  //     cout<< "Get detector list from input file --> ";    cout << "tree/branch list from file  --> ";
4856  //     if(TRK1)cout<<"TRK1 ";    if (TRK1)
4857  //     if(TRK2)cout<<"TRK2 ";      cout << "TRK1 ";
4858  //     if(TRKh)cout<<"TRKH ";    if (TRK2)
4859  //     if(CAL1)cout<<"CAL1 ";      cout << "TRK2 ";
4860  //     if(CAL2)cout<<"CAL2 ";    if (TRKh)
4861  //     if(TOF)cout<<"TOF ";      cout << "TRKH ";
4862  //     if(TRG)cout<<"TRG ";    if (CAL1)
4863  //     if(AC)cout<<"AC ";      cout << "CAL1 ";
4864  //     if(ND)cout<<"ND ";    if (CAL2)
4865  //     if(S4)cout<<"S4 ";      cout << "CAL2 ";
4866  //     if(ORB)cout<<"ORB ";    if (TOF)
4867  //     cout << endl;      cout << "TOF ";
4868            if (TRG)
4869      return ;      cout << "TRG ";
4870              if (AC)
4871  };      cout << "AC ";
4872      if (ND)
4873        cout << "ND ";
4874      if (S4)
4875        cout << "S4 ";
4876      if (ORB)
4877        cout << "ORB ";
4878      if (GP)
4879        cout << "GP ";
4880      cout << endl;
4881    
4882      return;
4883    
4884    }
4885    ;
4886    
4887  //--------------------------------------  //--------------------------------------
4888  //  //
# Line 2473  void  PamLevel2::GetWhichTrees(TFile* f) Line 4893  void  PamLevel2::GetWhichTrees(TFile* f)
4893   * @param name File name   * @param name File name
4894   * @return true if the file is ok.   * @return true if the file is ok.
4895   */   */
4896  Bool_t  PamLevel2::CheckLevel2File(TString name){  Bool_t PamLevel2::CheckLevel2File(TString name) {
           
     Bool_t CAL1__ok   = false;      
     Bool_t CAL2__ok   = false;      
     Bool_t TRK2__ok   = false;      
     Bool_t TRK1__ok   = false;      
     Bool_t TRKh__ok   = false;      
     Bool_t TRG__ok    = false;      
     Bool_t TOF__ok    = false;      
     Bool_t S4__ok     = false;      
     Bool_t ND__ok     = false;      
     Bool_t AC__ok     = false;      
     Bool_t ORB__ok    = false;      
       
     Bool_t RUN__ok    = false;  
       
     Bool_t SELLI__ok  = false;  
4897    
4898      cout << "Checking file: "<<name<<endl;    Bool_t CAL1__ok = false;
4899      TFile *f = new TFile(name.Data());    Bool_t CAL2__ok = false;
4900      if( !f || f->IsZombie() ){    Bool_t TRK2__ok = false;
4901          cout << "File: "<< f->GetName() <<" discarded ---- Non valid root file"<< endl; return false;    Bool_t TRK1__ok = false;
4902      }    Bool_t TRKh__ok = false;
4903  //    cout << "Get list of keys: "<<f<<endl;    Bool_t TRG__ok = false;
4904      TList *lk = f->GetListOfKeys();    Bool_t TOF__ok = false;
4905  //    lk->Print();    Bool_t S4__ok = false;
4906      TIter next(lk);    Bool_t ND__ok = false;
4907      TKey *key =0;    Bool_t AC__ok = false;
4908      Bool_t ORB__ok = false;
4909      Bool_t GP__ok = false;
4910    
4911      Bool_t RUN__ok = false;
4912    
4913      Bool_t SELLI__ok = false;
4914    
4915      //    cout << "Checking file: "<<name<<endl;
4916      TFile *f = new TFile(name.Data());
4917      if (!f || f->IsZombie()) {
4918        cout << "File: " << f->GetName() << " discarded ---- Non valid root file" << endl;
4919        fDiscarded = true;
4920        return false;
4921      }
4922      //    cout << "Get list of keys: "<<f<<endl;
4923      TList *lk = f->GetListOfKeys();
4924      //    lk->Print();
4925      TIter next(lk);
4926      TKey *key = 0;
4927    
4928      Int_t nev = 0;
4929    
4930      while ((key = (TKey*) next())) {
4931    
4932        //  cout << key->GetName() << endl;
4933        //  cout << key->GetName() << ""<<key->GetClassName()<<endl;
4934        //          cout << " Get tree: " << f->Get(key->GetName())<<endl;
4935        //  nev_previous = nev;
4936        //  cout << " n.entries  "<< nev <<endl;
4937        //  if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){
4938        //      nev = ((TTree*)f->Get(key->GetName()))->GetEntries();
4939        //      cout << "File: "<< f->GetName() <<" discarded ---- "<< key->GetName() << " tree: n.entries does not match "<<nev<<" "<<nev_previous<< endl;
4940        //      return false;
4941        //  };
4942    
4943        //=========================================================
4944        // check if the file
4945    
4946    
4947        if (!strcmp(key->GetName(), "Run"))
4948          RUN__ok = true;
4949    
4950        //=========================================================
4951        if (!strcmp(key->GetName(), "SelectionList")) {
4952          SELLI__ok = true;
4953          if (SELLI == 1) {
4954            Int_t nevt = ((TTree*) f->Get("SelectionList"))->GetEntries();
4955            if (nev && nevt != nev) {
4956              cout << "File: " << f->GetName() << " discarded ---- SelectionList tree has " << nevt
4957                  << " events instead of " << nev << endl;
4958              fDiscarded = true;
4959              return false;
4960            }
4961            nev = nevt;
4962          }
4963        }
4964    
4965        //=========================================================
4966        if (!strcmp(key->GetName(), "Trigger")) {
4967          TRG__ok = true;
4968          if (TRG) {
4969            Int_t nevt = ((TTree*) f->Get("Trigger"))->GetEntries();
4970            if (nev && nevt != nev) {
4971              cout << "File: " << f->GetName() << " discarded ---- Trigger tree has " << nevt << " events instead of "
4972                  << nev << endl;
4973              fDiscarded = true;
4974              return false;
4975            }
4976            nev = nevt;
4977          }
4978        }
4979        //=========================================================
4980        if (!strcmp(key->GetName(), "ToF")) {
4981          TOF__ok = true;
4982          if (TOF) {
4983            Int_t nevt = ((TTree*) f->Get("ToF"))->GetEntries();
4984            if (nev && nevt != nev) {
4985              cout << "File: " << f->GetName() << " discarded ---- ToF tree has " << nevt << " events instead of " << nev
4986                  << endl;
4987              fDiscarded = true;
4988              return false;
4989            }
4990            nev = nevt;
4991          }
4992        }
4993        //=========================================================
4994        if (!strcmp(key->GetName(), "S4")) {
4995          S4__ok = true;
4996          if (S4) {
4997            Int_t nevt = ((TTree*) f->Get("S4"))->GetEntries();
4998            if (nev && nevt != nev) {
4999              cout << "File: " << f->GetName() << " discarded ---- S4 tree has " << nevt << " events instead of " << nev
5000                  << endl;
5001              fDiscarded = true;
5002              return false;
5003            }
5004            nev = nevt;
5005          }
5006        }
5007        //=========================================================
5008    
5009        if (!strcmp(key->GetName(), "NeutronD")) {
5010          ND__ok = true;
5011          if (ND) {
5012            Int_t nevt = ((TTree*) f->Get("NeutronD"))->GetEntries();
5013            if (nev && nevt != nev) {
5014              cout << "File: " << f->GetName() << " discarded ---- NeutronD tree has " << nevt << " events instead of "
5015                  << nev << endl;
5016              fDiscarded = true;
5017              return false;
5018            }
5019            nev = nevt;
5020          }
5021        }
5022        //=========================================================
5023        if (!strcmp(key->GetName(), "Anticounter")) {
5024          AC__ok = true;
5025          if (AC) {
5026            Int_t nevt = ((TTree*) f->Get("Anticounter"))->GetEntries();
5027            if (nev && nevt != nev) {
5028              cout << "File: " << f->GetName() << " discarded ---- Anticounter tree has " << nevt << " events instead of "
5029                  << nev << endl;
5030              fDiscarded = true;
5031              return false;
5032            }
5033            nev = nevt;
5034          }
5035        }
5036        //=========================================================
5037        if (!strcmp(key->GetName(), "OrbitalInfo")) {
5038          ORB__ok = true;
5039          if (ORB) {
5040            Int_t nevt = ((TTree*) f->Get("OrbitalInfo"))->GetEntries();
5041            if (nev && nevt != nev) {
5042              cout << "File: " << f->GetName() << " discarded ---- OrbitalInfo tree has " << nevt << " events instead of "
5043                  << nev << endl;
5044              fDiscarded = true;
5045              return false;
5046            }
5047            nev = nevt;
5048          }
5049        }
5050        //=========================================================
5051        if (!strcmp(key->GetName(), "Tracker")) {
5052          TTree *T = (TTree*) f->Get("Tracker");
5053          if (TRK1 || TRK2 || TRKh) {
5054            Int_t nevt = T->GetEntries();
5055            if (nev && nevt != nev) {
5056              cout << "File: " << f->GetName() << " discarded ---- Tracker tree has " << nevt << " events instead of "
5057                  << nev << endl;
5058              fDiscarded = true;
5059              return false;
5060            }
5061            nev = nevt;
5062          }
5063          for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
5064            TString name = T->GetListOfBranches()->At(i)->GetName();
5065            if (!name.CompareTo("TrkLevel1"))
5066              TRK1__ok = true;
5067            if (!name.CompareTo("TrkLevel2"))
5068              TRK2__ok = true;
5069            if (!name.CompareTo("TrkHough"))
5070              TRKh__ok = true;
5071          };
5072          T->Delete();
5073        };
5074        //=========================================================
5075        if (!strcmp(key->GetName(), "Calorimeter")) {
5076          TTree *T = (TTree*) f->Get("Calorimeter");
5077          if (CAL1 || CAL2) {
5078            Int_t nevt = T->GetEntries();
5079            if (nev && nevt != nev) {
5080              cout << "File: " << f->GetName() << " discarded ---- Calorimeter tree has " << nevt << " events instead of "
5081                  << nev << endl;
5082              fDiscarded = true;
5083              return false;
5084            }
5085            nev = nevt;
5086          }
5087          for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
5088            TString name = T->GetListOfBranches()->At(i)->GetName();
5089            if (!name.CompareTo("CaloLevel1"))
5090              CAL1__ok = true;
5091            if (!name.CompareTo("CaloLevel2"))
5092              CAL2__ok = true;
5093          };
5094          T->Delete();
5095        };
5096        //=========================================================
5097        if (!strcmp(key->GetName(), "h20")) {
5098          ISGP = true;
5099          GP__ok = true;
5100          if (GP) {
5101            Int_t nevt = ((TTree*) f->Get("h20"))->GetEntries();
5102            if (nev && nevt != nev) {
5103              cout << "File: " << f->GetName() << " discarded ---- h20 tree has " << nevt << " events instead of " << nev
5104                  << endl;
5105              fDiscarded = true;
5106              return false;
5107            }
5108            nev = nevt;
5109          }
5110        }
5111    
5112      Int_t nev = 0;    };
5113    
5114      while( (key = (TKey*)next()) ){    if (SELLI == -1)
5115                SELLI = (Int_t) SELLI__ok;
5116  //      cout << key->GetName() << endl;    if (SELLI == 0 && SELLI__ok) {
5117  //      cout << key->GetName() << ""<<key->GetClassName()<<endl;      cout << "File: " << f->GetName() << " discarded ---- found SelectionList (it is not a full-event file)" << endl;
5118  //              cout << " Get tree: " << f->Get(key->GetName())<<endl;      fDiscarded = true;
5119  //      nev_previous = nev;      return false;
5120  //      cout << " n.entries  "<< nev <<endl;    }
5121  //      if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){    if (SELLI == 1 && !SELLI__ok) {
5122  //          nev = ((TTree*)f->Get(key->GetName()))->GetEntries();      cout << "File: " << f->GetName() << " discarded ---- SelectionList missing" << endl;
5123  //          cout << "File: "<< f->GetName() <<" discarded ---- "<< key->GetName() << " tree: n.entries does not match "<<nev<<" "<<nev_previous<< endl;      fDiscarded = true;
5124  //          return false;      return false;
5125  //      };    }
   
   
         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;  
             }  
         }  
5126    
5127          //=========================================================        //    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;  
             }  
         }  
         //=========================================================    
5128    
5129          if( !strcmp(key->GetName(),"NeutronD"   ) ){    //     cout<< "CheckLevel2File(TString): detector list --> ";
5130              ND__ok = true;    //     if(TRK1__ok)cout<<"TRK1 ";
5131              if(ND){    //     if(TRK2__ok)cout<<"TRK2 ";
5132                  Int_t nevt = ((TTree*)f->Get("NeutronD"))->GetEntries();    //     if(TRKh__ok)cout<<"TRKH ";
5133                  if( nev && nevt!=nev){    //     if(CAL1__ok)cout<<"CAL1 ";
5134                      cout << "File: "<< f->GetName() <<" discarded ---- NeutronD tree has "<<nevt<<" events instead of "<<nev<< endl;    //     if(CAL2__ok)cout<<"CAL2 ";
5135                      return false;    //     if(TOF__ok)cout<<"TOF ";
5136                  }    //     if(TRG__ok)cout<<"TRG ";
5137                  nev=nevt;    //     if(AC__ok)cout<<"AC ";
5138              }    //     if(ND__ok)cout<<"ND ";
5139          }          //     if(S4__ok)cout<<"S4 ";
5140          //=========================================================      //     if(ORB__ok)cout<<"ORB ";
5141          if( !strcmp(key->GetName(),"Anticounter") ){    //     cout << endl;
5142              AC__ok = true;  
5143              if(AC){  
5144                  Int_t nevt = ((TTree*)f->Get("Anticounter"))->GetEntries();    if (TRK2 && TRK1__ok)
5145                  if( nev && nevt!=nev){      TRK1 = 1;
5146                      cout << "File: "<< f->GetName() <<" discarded ---- Anticounter tree has "<<nevt<<" events instead of "<<nev<< endl;    // ----------------------------------------------------------------------------
5147                      return false;    // NOTA
5148                  }    // se c'e` il level1, lo devo necessarimente leggere.
5149                  nev=nevt;    // infatti (non ho capito perche`) i cluster vengono letti e allocati in memoria
5150              }    // comunque, ma non vengono disallocati da PamLevel2::Clear()
5151          }    // ----------------------------------------------------------------------------
5152          //=========================================================    
5153          if( !strcmp(key->GetName(),"OrbitalInfo") ){  
5154              ORB__ok = true;    if (!RUN__ok) {
5155              if(ORB){      cout << "File: " << f->GetName() << " *WARNING* ---- Missing RunInfo tree (NB: RUN infos will not be updated)"
5156                  Int_t nevt = ((TTree*)f->Get("OrbitalInfo"))->GetEntries();          << endl;
5157                  if( nev && nevt!=nev){      RUN = false;
5158                      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();  
         };        
5159    
5160      };    if (CAL1 && !CAL1__ok) {
5161        cout << "File: " << f->GetName() << " discarded ---- Missing CaloLevel1 branch" << endl;
5162        fDiscarded = true;
5163        return false;
5164      };
5165      if (CAL2 && !CAL2__ok) {
5166        cout << "File: " << f->GetName() << " discarded ---- Missing CaloLevel2 branch" << endl;
5167        fDiscarded = true;
5168        return false;
5169      };
5170      if (TRK2 && !TRK2__ok) {
5171        cout << "File: " << f->GetName() << " discarded ---- Missing TrkLevel2 branch" << endl;
5172        fDiscarded = true;
5173        return false;
5174      };
5175      if (TRK1 && !TRK1__ok) {
5176        cout << "File: " << f->GetName() << " discarded ---- Missing TrkLevel1 branch" << endl;
5177        fDiscarded = true;
5178        return false;
5179      };
5180      if (TRKh && !TRKh__ok) {
5181        cout << "File: " << f->GetName() << " discarded ---- Missing TrkHough branch" << endl;
5182        fDiscarded = true;
5183        return false;
5184      };
5185      if (ORB && !ORB__ok) {
5186        cout << "File: " << f->GetName() << " discarded ---- Missing ORB tree" << endl;
5187        fDiscarded = true;
5188        return false;
5189      };
5190      if (AC && !AC__ok) {
5191        cout << "File: " << f->GetName() << " discarded ---- Missing AC tree" << endl;
5192        fDiscarded = true;
5193        return false;
5194      };
5195      if (S4 && !S4__ok) {
5196        cout << "File: " << f->GetName() << " discarded ---- Missing S4 tree" << endl;
5197        fDiscarded = true;
5198        return false;
5199      };
5200      if (TOF && !TOF__ok) {
5201        cout << "File: " << f->GetName() << " discarded ---- Missing ToF tree" << endl;
5202        fDiscarded = true;
5203        return false;
5204      };
5205    
5206      if( SELLI==-1 )SELLI = (Int_t)SELLI__ok;    if (ND && !ND__ok) {
5207      if( SELLI==0 && SELLI__ok ){      cout << "File: " << f->GetName() << " discarded ---- Missing ND tree" << endl;
5208          cout << "File: "<< f->GetName() <<" discarded ---- found SelectionList (it is not a full-event file)" << endl;      fDiscarded = true;
5209          return false;        return false;
5210      }    };
5211      if( SELLI==1 && !SELLI__ok ){    if (TRG && !TRG__ok) {
5212          cout << "File: "<< f->GetName() <<" discarded ---- SelectionList missing" << endl;      cout << "File: " << f->GetName() << " discarded ---- Missing Trigger tree" << endl;
5213          return false;      fDiscarded = true;
5214      }      return false;
5215          };
5216  //    cout << "SELLI "<<SELLI<<endl;    if (GP && !GP__ok) {
5217        cout << "File: " << f->GetName() << " discarded ---- Missing h20 tree" << endl;
5218        fDiscarded = true;
5219        return false;
5220      };
5221    
5222  //     cout<< "CheckLevel2File(TString): detector list --> ";    //    lk->Delete();
5223  //     if(TRK1__ok)cout<<"TRK1 ";    //    delete lk;
5224  //     if(TRK2__ok)cout<<"TRK2 ";    f->Close();
5225  //     if(TRKh__ok)cout<<"TRKH ";  
5226  //     if(CAL1__ok)cout<<"CAL1 ";    //     cout<< "CheckLevel2File(TString): detector list --> ";
5227  //     if(CAL2__ok)cout<<"CAL2 ";    //     if(TRK1)cout<<"TRK1 ";
5228  //     if(TOF__ok)cout<<"TOF ";    //     if(TRK2)cout<<"TRK2 ";
5229  //     if(TRG__ok)cout<<"TRG ";    //     if(TRKh)cout<<"TRKH ";
5230  //     if(AC__ok)cout<<"AC ";    //     if(CAL1)cout<<"CAL1 ";
5231  //     if(ND__ok)cout<<"ND ";    //     if(CAL2)cout<<"CAL2 ";
5232  //     if(S4__ok)cout<<"S4 ";    //     if(TOF)cout<<"TOF ";
5233  //     if(ORB__ok)cout<<"ORB ";    //     if(TRG)cout<<"TRG ";
5234  //     cout << endl;    //     if(AC)cout<<"AC ";
5235      //     if(ND)cout<<"ND ";
5236      //     if(S4)cout<<"S4 ";
5237      if(TRK2 && TRK1__ok)TRK1=1;    //     if(ORB)cout<<"ORB ";
5238  // ----------------------------------------------------------------------------    //     if(GP)cout<<"GP ";
5239  // 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;  
     };  
       
5240    
5241  //    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;  
           
 };  
5242    
5243    }
5244    ;
5245    
5246  /**  /**
5247   * Create clone-trees   * Create clone-trees
5248   */   */
5249  void PamLevel2::CreateCloneTrees0( TChain *fChain, TFile *ofile ){  void PamLevel2::CreateCloneTrees0(TChain *fChain, TFile *ofile) {
5250    
5251      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5252      cout << "Create clones of PAMELA trees "<<endl;    cout << "Create clones of PAMELA trees " << endl;
5253        
5254      Int_t i=0;    Int_t i = 0;
5255      pam_tree_clone[i] = fChain->GetTree()->CloneTree(0);    pam_tree_clone[i] = fChain->GetTree()->CloneTree(0);
5256      TString name =  pam_tree_clone[i]->GetName();    TString name = pam_tree_clone[i]->GetName();
5257      name.Append("_clone");
5258      //    pam_tree_clone[i]->SetName(name.Data());
5259      cout << pam_tree_clone[i]->GetName() << endl;
5260      i++;
5261    
5262      TList *li = fChain->GetListOfFriends();
5263      TIter next(li);
5264      TFriendElement* T_friend = 0;
5265      ofile->cd();
5266      while ((T_friend = (TFriendElement*) next())) {
5267        //  cout<<T_friend->IsA()->GetName()<<" "<<T_friend->GetName()<<hex << T_friend->GetTree() << dec<<endl;
5268        //  cout<<T_friend->GetTree()->GetName()<< endl;
5269        pam_tree_clone[i] = T_friend->GetTree()->CloneTree(0);
5270        pam_tree_clone[i]->SetAutoSave(1000000);
5271        name = pam_tree_clone[i]->GetName();
5272      name.Append("_clone");      name.Append("_clone");
5273  //    pam_tree_clone[i]->SetName(name.Data());      //  pam_tree_clone[i]->SetName(name.Data());
5274      cout << pam_tree_clone[i]->GetName() <<endl;      cout << pam_tree_clone[i]->GetName() << endl;
5275      i++;      i++;
5276      }
5277    
5278      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;  
5279    
5280      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5281    
5282  }  }
5283    
5284  /**  /**
5285   * Create clone-trees   * Create clone-trees
5286   */   */
5287  void PamLevel2::CreateCloneTrees(TFile *ofile){  void PamLevel2::CreateCloneTrees(TFile *ofile) {
   
5288    
5289  //  if the pointer is null, create a default file    //  if the pointer is null, create a default file
5290      if (!run_tree)
5291        return;
5292    
5293      if(!ofile){    if (!ofile) {
5294          cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root "<<endl;      cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root " << endl;
5295          ofile = new TFile("clone-tree.root","recreate");      ofile = new TFile("clone-tree.root", "recreate");
5296      }    }
5297    
5298      ofile->cd();    ofile->cd();
5299    
5300      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5301      cout << "Create new PAMELA trees "<<endl;    cout << "Create new PAMELA trees " << endl;
           
5302    
5303      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 ");
5304      run_tree_clone->Branch("RunInfo","GL_RUN",GetPointerTo("RunInfo"));    run_tree_clone->Branch("RunInfo", "GL_RUN", GetPointerTo("RunInfo"));
5305      cout << "Run          : branch RunInfo"<<endl;    cout << "Run          : branch RunInfo" << endl;
5306      run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));    run_tree_clone->Branch("SoftInfo", "SoftInfo", GetPointerTo("SoftInfo"));
5307      cout << "Run          : branch SoftInfo"<<endl;    cout << "Run          : branch SoftInfo" << endl;
5308      // ------------------
5309      // replicate run tree
5310      // ------------------
5311      //    cout << "----------------------------------------------------"<<endl;
5312      //    cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
5313      for (Int_t i = 0; i < run_tree->GetEntries(); i++) {
5314        run_tree->GetEntry(i);
5315        //  cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
5316        run_tree_clone->Fill();
5317      }
5318      //
5319      // replicate processinginfo tree
5320      //
5321      if ( PROC ){ // EMEMEM
5322        proc_tree_clone = new TTree("ProcessingInfo","Log of data processing");
5323        proc_tree_clone->Branch("ProcInfo", "ProcInfo", GetPointerTo("ProcInfo"));
5324        cout << "ProcessingInfo: branch ProcessingInfo" << endl;  
5325      // ------------------      // ------------------
5326      // replicate run tree      // replicate processinginfo tree
5327      // ------------------      // ------------------
5328      cout << "----------------------------------------------------"<<endl;      //    cout << "----------------------------------------------------"<<endl;
5329      cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;      //    cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
5330      for (Int_t i=0; i<run_tree->GetEntries(); i++){      for (Int_t i = 0; i < proc_tree->GetEntries(); i++) {
5331          run_tree->GetEntry(i);        proc_tree->GetEntry(i);
5332          cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;        //      cout << i<< "\t | "<<endl;
5333          run_tree_clone->Fill();        proc_tree_clone->Fill();
5334      }      }
5335      cout << "----------------------------------------------------"<<endl;      if ( SELLI != 2 ){      
5336          proc_obj->runID = 0;
5337          TTimeStamp *dt = new TTimeStamp();
5338          proc_obj->date = dt->AsString();
5339          delete dt;
5340          proc_obj->commandLine = Form("PamelaLevel2 was called: CAL2 %i CAL1 %i CAL0 %i TRK2 %i TRK1 %i TRKh %i TRK0 %i TOF %i TOF0 %i TRG %i \n                                            S4 %i ND %i AC %i ORB %i GP %i EXT %i NUC %i RUN %i ISGP %i SELLI %i \n Custom string   = %s",CAL2,CAL1,CAL0,TRK2,TRK1,TRKh,TRK0,TOF,TOF0,TRG,S4,ND,AC,ORB,GP,EXT,NUC,RUN,ISGP,SELLI,customString.Data());
5341          proc_obj->outputFilename = ofile->GetName();
5342          proc_obj->localDir = gSystem->WorkingDirectory();
5343          proc_obj->uname = gSystem->GetFromPipe("uname -a");
5344          if (!dbc || (dbc && !dbc->IsConnected())) SetDBConnection();
5345          proc_obj->DB = Form("mysql://%s/%s",dbc->GetHost(),dbc->GetDB());
5346          dbc->Close();
5347          proc_tree_clone->Fill();
5348        }
5349        cout << "----------------------------------------------------" << endl;
5350      }
5351      // ------------------------------------
5352      // add branch with dead and live times
5353      // ------------------------------------
5354      if (SELLI != 2) { // EMILIANO
5355        run_tree_clone->Branch("DeadLiveTime", totdltime, "dltime[3]/l");
5356        cout << "Run          : branch DeadLiveTime" << endl;
5357    
5358        sel_tree_clone = new TTree("SelectionList", "List of selected events ");
5359        //    sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
5360        sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW
5361        sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");
5362        //    if ( hasL0EE ) sel_tree_clone->Branch("L0EventEntry", &il0entry, "l0eventry/L");
5363      };
5364    
5365      Int_t i = 0;
5366      if (TRK1 || TRK2 || TRKh) {
5367        pam_tree_clone[i] = new TTree("Tracker", "PAMELA tracker level2 data ");
5368        if (TRK1) {
5369          pam_tree_clone[i]->Branch("TrkLevel1", "TrkLevel1", GetPointerTo("TrkLevel1"));
5370          pam_tree_clone[i]->BranchRef();
5371          cout << "Tracker      : branch TrkLevel1" << endl;
5372          //            cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;
5373        };
5374        if (TRK2) {
5375          pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2", GetPointerTo("TrkLevel2"));
5376          cout << "Tracker      : branch TrkLevel2" << endl;
5377        };
5378        if (TRKh) {
5379          pam_tree_clone[i]->Branch("TrkHough", "TrkHough", GetPointerTo("TrkHough"));
5380          cout << "Tracker      : branch TrkHough" << endl;
5381        };
5382        if(NUC){
5383            pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&trk_nuc_obj);
5384            cout << "Tracker      : branch TrackNuclei" << endl;    
5385        }
5386        if(EXT){
5387            pam_tree_clone[i]->Branch("extAlgFlag",&extAlgFlag,"extAlgFlag/I");
5388            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&trk_ext_obj);
5389            cout << "Tracker      : branch RecoveredTrack" << endl;
5390            if(NUC){
5391                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&trk_ext_nuc_obj);
5392                cout << "Tracker      : branch RecoveredTrackNuclei" << endl;                  
5393            }
5394        }
5395    
5396      sel_tree_clone = new TTree("SelectionList","List of selected events ");      i++;
5397      sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");    }
     sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/L");  
       
5398    
5399      Int_t i=0;    // Calorimeter
5400      if(TRK1||TRK2||TRKh){    if (CAL1 || CAL2) {
5401          pam_tree_clone[i] = new TTree("Tracker","PAMELA tracker level2 data ");      pam_tree_clone[i] = new TTree("Calorimeter", "PAMELA calorimeter level2 data ");
5402          if(TRK1) {      if (CAL1) {
5403              pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));        pam_tree_clone[i]->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));
5404              pam_tree_clone[i]->BranchRef();        cout << "Calorimeter  : branch CaloLevel1" << endl;
5405              cout << "Tracker      : branch TrkLevel1"<<endl;      };
5406  //          cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;      if (CAL2) {
5407          };        pam_tree_clone[i]->Branch("CaloLevel2", "CaloLevel2", GetPointerTo("CaloLevel2"));
5408          if(TRK2) {        cout << "Calorimeter  : branch CaloLevel2" << endl;
5409              pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));      };
5410              cout << "Tracker      : branch TrkLevel2"<<endl;              if(NUC){
5411          };          pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&calo_nuc_obj);
5412          if(TRKh) {          cout << "Calorimeter  : branch TrackNuclei" << endl;    
5413              pam_tree_clone[i]->Branch("TrkHough","TrkHough", GetPointerTo("TrkHough"));      }
5414              cout << "Tracker      : branch TrkHough"<<endl;      if(EXT){
5415          };          pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&calo_ext_obj);
5416          i++;          cout << "Calorimeter  : branch RecoveredTrack" << endl;
5417      }          if(NUC){
5418                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&calo_ext_nuc_obj);
5419      // Calorimeter              cout << "Calorimeter  : branch RecoveredTrackNuclei" << endl;                  
5420      if(CAL1||CAL2){          }
5421          pam_tree_clone[i] = new TTree("Calorimeter","PAMELA calorimeter level2 data ");      }
5422          if(CAL1) {      i++;
5423              pam_tree_clone[i]->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));    }
5424              cout << "Calorimeter  : branch CaloLevel1"<<endl;  
5425          };    // ToF
5426          if(CAL2) {    if (TOF) {
5427              pam_tree_clone[i]->Branch("CaloLevel2","CaloLevel2", GetPointerTo("CaloLevel2"));      pam_tree_clone[i] = new TTree("ToF", "PAMELA ToF level2 data ");
5428              cout << "Calorimeter  : branch CaloLevel2"<<endl;      pam_tree_clone[i]->Branch("ToFLevel2", "ToFLevel2", GetPointerTo("ToFLevel2"));
5429          };      cout << "ToF          : branch ToFLevel2" << endl;
5430          i++;      if(NUC){
5431      }              pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&tof_nuc_obj);
5432            cout << "ToF          : branch TrackNuclei" << endl;    
5433      // ToF          }
5434      if(TOF) {      if(EXT){
5435          pam_tree_clone[i] = new TTree("ToF","PAMELA ToF level2 data ");          pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&tof_ext_obj);
5436          pam_tree_clone[i]->Branch("ToFLevel2","ToFLevel2", GetPointerTo("ToFLevel2"));          cout << "ToF          : branch RecoveredTrack" << endl;
5437          cout << "ToF          : branch ToFLevel2"<<endl;          if(NUC){
5438          i++;              pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&tof_ext_nuc_obj);
5439      };              cout << "ToF          : branch RecoveredTrackNuclei" << endl;                  
5440      // Trigger          }
5441      if(TRG) {      }
5442          pam_tree_clone[i] = new TTree("Trigger","PAMELA trigger level2 data ");      i++;
5443          pam_tree_clone[i]->Branch("TrigLevel2","TrigLevel2", GetPointerTo("TrigLevel2"));    };
5444          cout << "Trigger      : branch TrigLevel2"<<endl;    // Trigger
5445          i++;    if (TRG) {
5446      };      pam_tree_clone[i] = new TTree("Trigger", "PAMELA trigger level2 data ");
5447      // S4      pam_tree_clone[i]->Branch("TrigLevel2", "TrigLevel2", GetPointerTo("TrigLevel2"));
5448      if(S4) {      cout << "Trigger      : branch TrigLevel2" << endl;
5449          pam_tree_clone[i] = new TTree("S4","PAMELA S4 level2 data ");        i++;
5450          pam_tree_clone[i]->Branch("S4Level2","S4Level2", GetPointerTo("S4Level2"));    };
5451          cout << "S4           : branch S4Level2"<<endl;    // S4
5452          i++;    if (S4) {
5453      };      pam_tree_clone[i] = new TTree("S4", "PAMELA S4 level2 data ");
5454      // Neutron Detector      pam_tree_clone[i]->Branch("S4Level2", "S4Level2", GetPointerTo("S4Level2"));
5455      if(ND) {      cout << "S4           : branch S4Level2" << endl;
5456          pam_tree_clone[i] = new TTree("NeutronD","PAMELA neutron detector level2 data ");            i++;
5457          pam_tree_clone[i]->Branch("NDLevel2","NDLevel2", GetPointerTo("NDLevel2"));    };
5458          cout << "NeutronD     : branch NDLevel2"<<endl;    // Neutron Detector
5459          i++;    if (ND) {
5460      };      pam_tree_clone[i] = new TTree("NeutronD", "PAMELA neutron detector level2 data ");
5461      // Anticounters      pam_tree_clone[i]->Branch("NDLevel2", "NDLevel2", GetPointerTo("NDLevel2"));
5462      if(AC) {      cout << "NeutronD     : branch NDLevel2" << endl;
5463          pam_tree_clone[i] = new TTree("Anticounter","PAMELA anticounter detector level2 data ");              i++;
5464          pam_tree_clone[i]->Branch("AcLevel2","AcLevel2", GetPointerTo("AcLevel2"));    };
5465          cout << "Anticounter  : branch AcLevel2"<<endl;    // Anticounters
5466          i++;    if (AC) {
5467      };      pam_tree_clone[i] = new TTree("Anticounter", "PAMELA anticounter detector level2 data ");
5468      // OrbitalInfo      pam_tree_clone[i]->Branch("AcLevel2", "AcLevel2", GetPointerTo("AcLevel2"));
5469      if(ORB) {      cout << "Anticounter  : branch AcLevel2" << endl;
5470          pam_tree_clone[i] = new TTree("OrbitalInfo","PAMELA oribital info  ");        i++;
5471          pam_tree_clone[i]->Branch("OrbitalInfo","OrbitalInfo", GetPointerTo("OrbitalInfo"));    };
5472          cout << "OrbitalInfo  : branch OrbitalInfo"<<endl;    // OrbitalInfo
5473          i++;    if (ORB) {
5474      };      pam_tree_clone[i] = new TTree("OrbitalInfo", "PAMELA orbital info  ");
5475      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;      pam_tree_clone[i]->Branch("OrbitalInfo", "OrbitalInfo", GetPointerTo("OrbitalInfo"));
5476        cout << "OrbitalInfo  : branch OrbitalInfo" << endl;
5477        if(NUC){
5478            pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&orb_nuc_obj);
5479            cout << "OrbitalInfo  : branch TrackNuclei" << endl;    
5480        }
5481        if(EXT){
5482            pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&orb_ext_obj);
5483            cout << "OrbitalInfo  : branch RecoveredTrack" << endl;
5484            if(NUC){
5485                pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&orb_ext_nuc_obj);
5486                cout << "OrbitalInfo  : branch RecoveredTrackNuclei" << endl;                  
5487            }
5488        }
5489        i++;
5490      };
5491      // GPamela
5492      if (GP) {
5493        pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");
5494        pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split
5495        cout << "GPamela  : branch GPamela" << endl;
5496        i++;
5497      };
5498    
 }  
5499    
5500      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5501    
5502    }
5503    
5504  /**  /**
5505   * Fill tree (created with CreateCloneTrees)   * Fill tree (created with CreateCloneTrees)
5506   *   *
5507   */   */
5508  //void PamLevel2::FillNewPamTree(TTree *T){  //void PamLevel2::FillNewPamTree(TTree *T){
5509  void PamLevel2::FillCloneTrees(){  void PamLevel2::FillCloneTrees() {
       
 //    cout << "PamLevel2::FillCloneTrees()" << irunentry << endl;  
5510    
5511      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();      
5512    
5513  }    for (Int_t i = 0; i < NCLONES; i++) {
5514        if (pam_tree_clone[i])
5515          pam_tree_clone[i]->Fill();
5516      }
5517      if (sel_tree_clone)
5518        sel_tree_clone->Fill();
5519    
5520    }
5521    
5522  TTree* PamLevel2::GetCloneTree(TString name){  TTree* PamLevel2::GetCloneTree(TString name) {
5523    
5524      for(Int_t i=0; i<NCLONES; i++){        for (Int_t i = 0; i < NCLONES; i++) {
5525          if(pam_tree_clone[i]){      if (pam_tree_clone[i]) {
5526              TString na = pam_tree_clone[i]->GetName();        TString na = pam_tree_clone[i]->GetName();
5527              if(!name.CompareTo(na))return pam_tree_clone[i];        if (!name.CompareTo(na))
5528          };          return pam_tree_clone[i];
5529      }      };
5530      if(run_tree_clone){    }
5531          TString na = run_tree_clone->GetName();    if (run_tree_clone) {
5532          if(!name.CompareTo(na))return run_tree_clone;      TString na = run_tree_clone->GetName();
5533      }      if (!name.CompareTo(na))
5534      if(sel_tree_clone){        return run_tree_clone;
5535          TString na = sel_tree_clone->GetName();    }
5536          if(!name.CompareTo(na))return sel_tree_clone;    if (sel_tree_clone) {
5537      }      TString na = sel_tree_clone->GetName();
5538      return NULL;      if (!name.CompareTo(na))
5539          return sel_tree_clone;
5540      }
5541      if (proc_tree_clone && PROC) {
5542        TString na = proc_tree_clone->GetName();
5543        if (!name.CompareTo(na))
5544          return proc_tree_clone;
5545      }
5546      return NULL;
5547    
5548  }  }
5549  void PamLevel2::WriteCloneTrees(){  void PamLevel2::WriteCloneTrees() {
5550      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;    cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5551      cout << "Write clones of PAMELA trees "<<endl;    cout << "Write clones of PAMELA trees " << endl;
5552      cout << run_tree_clone->GetName()<<endl;        cout << run_tree_clone->GetName() << endl;
5553      run_tree_clone->Write();    if (SELLI != 2) {// Emiliano
5554      cout << sel_tree_clone->GetName()<<endl;          if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
5555          run_tree_clone->GetBranch("DeadLiveTime")->Fill();
5556      };
5557      run_tree_clone->Write();
5558      if (SELLI != 2) { //Emiliano
5559        cout << sel_tree_clone->GetName() << endl;
5560      sel_tree_clone->Write();      sel_tree_clone->Write();
5561      for(Int_t i=0; i<NCLONES; i++){        };
5562          if(pam_tree_clone[i]){    for (Int_t i = 0; i < NCLONES; i++) {
5563              cout << pam_tree_clone[i]->GetName()<<endl;      if (pam_tree_clone[i]) {
5564              pam_tree_clone[i]->Write();        cout << pam_tree_clone[i]->GetName() << endl;
5565          };        pam_tree_clone[i]->Write(pam_tree_clone[i]->GetName(),TObject::kOverwrite);
5566      }      };
5567      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;    }
5568      
5569      if ( PROC ){//EMEMEMEM
5570        proc_tree_clone->Write("ProcessingInfo",TObject::kOverwrite);
5571      }
5572      cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5573    
5574  }  }
5575    
5576  /**  /**
5577   * 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.
5578   */   */
5579  //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;  
     }  
5580    
5581      if (!pam_tree) {
5582        cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded" << endl;
5583        return 0;
5584      }
5585    
5586      //    //
5587      // 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...
5588      //    //
5589      //    if(!run_tree ){    //    if(!run_tree ){
5590      //  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;
5591      //  return 0;    //    return 0;
5592      //    }    //    }
5593    
5594      Long64_t ii=0;    //-------------------------------
5595      //-------------------------------    if (!pam_tree->GetEntry(iee)) {
5596      ii = iee;      cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree" << endl;
5597      if( !pam_tree->GetEntry(ii) ){            return 0;
5598          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree"<<endl;    }
5599          return 0;    //
5600      // ... 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.
5601      // 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
5602      // a problem if you don't check the return code of getentry.
5603      //
5604      if (!RUN || !run_tree) {
5605        if (TRK0 || CAL0 || TOF0 || RUN) { //forse cosi` va bene per tornare 1?
5606          cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded" << endl;
5607          return 0;
5608      }      }
5609      //      else {
5610      // ... 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...  
         }  
5611      }      }
5612      }
5613    
5614      //-------------------------------    //-------------------------------
5615      ii = iee;    //
5616  //    Bool_t UPDATED = UpdateRunInfo(run_tree,ii);    if ( fUpdateRunInfo ) UpdateRunInfo(iee); // Emiliano
5617  //    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;  
5618    
5619  }    return 1;
5620    
5621  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;  
 };  
5622    
5623    TrkLevel0 *PamLevel2::GetTrkLevel0() {
5624      if (!TRK0)
5625        return NULL;
5626      if (!GetYodaEntry()) {
5627        cout << " Int_t PamLevel2::GetTrkLevel0() -- ERROR -- error reading level0 tree" << endl;
5628        return 0;
5629      }
5630      return trk0_obj;
5631    }
5632    ;
5633    CaloLevel0 *PamLevel2::GetCaloLevel0() {
5634      if (!CAL0)
5635        return NULL;
5636      if (!GetYodaEntry()) {
5637        cout << " Int_t PamLevel2::GetCaloLevel0() -- ERROR -- error reading level0 tree" << endl;
5638        return 0;
5639      }
5640      return calo0_obj;
5641    }
5642    ;
5643    
5644  /**  /**
5645   * 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 3065  CaloLevel0    *PamLevel2::GetCaloLevel0( Line 5647  CaloLevel0    *PamLevel2::GetCaloLevel0(
5647   * @return Pointer to the tree   * @return Pointer to the tree
5648   */   */
5649    
5650    TTree* PamLevel2::GetYodaTree() {
5651    
5652  TTree* PamLevel2::GetYodaTree( ){    //    cout << "TTree* PamLevel2::GetYodaTree( )"<<endl;
5653          //===================================
5654  //    cout << "TTree* PamLevel2::GetYodaTree( )"<<endl;    // check if iroot has changed
5655      //===================================    //===================================
5656      // check if iroot has changed    if (irun < 0) {
5657      //===================================      cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;
5658      if( irun<0 ){      if (DBG) cout << "In order to use this method you have to first load the RunInfo tree "<<endl;
5659          cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = "<<irun<<endl;      return NULL;
5660  //      cout << "In order to use this method you have to first load the RunInfo tree "<<endl;    }
5661          return NULL;    Int_t irootnew = GetRunInfo()->ID_ROOT_L0;
5662      }        if (DBG){
5663      Int_t irootnew = run_obj->ID_ROOT_L0;      cout << "iroot    "<<iroot<<endl;
5664  //      cout << "iroot    "<<iroot<<endl;      cout << "irootnew "<<irootnew<<endl;
5665  //      cout << "irootnew "<<irootnew<<endl;    }
5666    
5667      //===================================
5668      // load the level0 file
5669      // (if not already loaded)
5670      //===================================
5671      if (iroot != irootnew || !l0_tree) {
5672        iroot = irootnew;
5673      //===================================      //===================================
5674      // load the level0 file      // open the DB connection
5675      // (if not already loaded)      // (if not already opened)
5676      //===================================      //===================================
5677      if( iroot != irootnew || !l0_tree){      if (!dbc || (dbc && !dbc->IsConnected()))
5678          iroot = irootnew;        SetDBConnection();
5679          //===================================      GL_ROOT glroot = GL_ROOT();
5680          // open the DB connection      if (glroot.Query_GL_ROOT(iroot, dbc)) {
5681          // (if not already opened)        cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = " << iroot << " does not exists"
5682          //===================================            << endl;
5683          if(!dbc || (dbc && !dbc->IsConnected())){        return NULL;
5684              cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;      };
5685              cout<<"Connecting to DB"<<endl;      TString filename = glroot.PATH + glroot.NAME;
5686              cout<<"HOST "<<host<<endl;      if (l0_file) {
5687              cout<<"USER "<<user<<endl;        l0_file->Close();
5688              cout<<"PSW  "<<psw<<endl;        l0_file->Delete();
5689              dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());      }
5690              if( !dbc )return NULL;      cout << "Opening LEVEL0 file: " << filename << endl;
5691              if( !dbc->IsConnected() )return NULL;          FileStat_t t;
5692  //          cout<<"...done"<<endl;      if (gSystem->GetPathInfo(filename.Data(), t)) {
5693              cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;        cout << " PamLevel2::GetYodaTree() -- ERROR opening file " << endl;
5694          }else{        return NULL;
5695  //          cout<<"DB already connected"<<endl;      }
5696          }      l0_file = new TFile(filename);
5697          GL_ROOT glroot = GL_ROOT();      if (!l0_file)
5698          if( glroot.Query_GL_ROOT(iroot,dbc) ){        return NULL;
5699              cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = "<<iroot<< " does not exists"<<endl;      l0_tree = (TTree*) l0_file->Get("Physics");
5700              return NULL;      if (!h0_obj)
5701          };        h0_obj = new EventHeader();
5702          TString filename = glroot.PATH + glroot.NAME;      l0_tree->SetBranchAddress("Header", &h0_obj);
5703          if(l0_file){      yprevshift = 0; // yes, yprevshift is the shift in the level0, prevshift is the shift in the level2
5704              l0_file->Close();      //---------------------------------------------------
5705              l0_file->Delete();      // TRACKER:
5706          }      if (TRK0) {
5707          cout << "Opening LEVEL0 file: "<< filename << endl;        if (!trk0_obj) {
5708          FileStat_t t;          trk0_obj = new TrkLevel0();
5709          if( gSystem->GetPathInfo(filename.Data(),t) ){          trk0_obj->Set();
5710              cout << " PamLevel2::GetYodaTree() -- ERROR opening file "<<endl;        };
5711              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;  
5712      }      }
5713        //--------------------------------------------------
5714      if ( TRK0 ){      // CALORIMETER:
5715          TrkParams::Load(6);      if (CAL0) {
5716          if( !TrkParams::IsLoaded(6) ){        if (!calo0_obj) {
5717              cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- VK-mask not loaded"<<endl;          calo0_obj = new CaloLevel0();
5718          };          calo0_obj->Set();
5719          TrkParams::SetCalib(run_obj,dbc);        };
5720          TrkParams::LoadCalib( );        l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());
5721          if( !TrkParams::CalibIsLoaded() ){      }
5722              cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded"<<endl;      //---------------------------------------------------
5723          };      // TOF:
5724        if (TOF0) {
5725          cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented " << endl;
5726      }      }
5727    
5728  //    cout << l0_tree << endl;      dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
5729            delete dbc;
5730      return l0_tree;      dbc=0;
5731    
5732      };
5733    
5734      if (TRK0) {
5735        if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection();
5736        TrkParams::SetCalib(run_obj, dbc);
5737        TrkParams::LoadCalib();
5738        if (!TrkParams::CalibIsLoaded()) {
5739          cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded" << endl;
5740        };
5741        if(dbc){
5742          dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
5743          delete dbc;
5744          dbc=0;
5745        };
5746      }
5747      return l0_tree;
5748  }  }
5749    
5750  /**  /**
5751   * 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.
5752   */   */
5753  Int_t PamLevel2::GetYodaEntry(){  Int_t PamLevel2::GetYodaEntry() {
5754    
5755  //    cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;    Long64_t iev = this->GetReadEntry();
5756      if(!GetYodaTree())return 0;  
5757          // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
5758      // patch    // if it is a full file (not preselected)
5759      if( irunentry < 0){    // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
5760  //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;    //  if (SELLI == 0 || SELLI == 2 || !hasL0EE) {
5761          irunentry=0LL;  
5762        if (!GetYodaTree()){
5763          printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
5764          return 0;
5765        }
5766    
5767        if (irunentry < 0) {
5768          if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
5769          //      cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
5770          irunentry = 0LL;
5771      }      }
5772      //  ---------------------------------      //  ---------------------------------
5773      //  if file is NOT a preselected file      //  if file is NOT a preselected file
5774      //  ---------------------------------      //  ---------------------------------
5775      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
5776  //     cout << " irunentry "<<irunentry<<endl;  
5777  //     cout << " EV_FROM "<<run_obj->EV_FROM<<endl;      if (DBG){
5778  //     cout << " quellagiusta = irunentry + EV_FROM "<< quellagiusta << endl;        cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
5779          cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
5780  //     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;        cout << " time "<< abstime << endl;
 //     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;  
 //     cout << " time "<< abstime << endl;  
 //     cout << " trk_calib_used "<<run_obj->TRK_CALIB_USED<< endl;  
       
     ULong64_t obt     = 0;  
     ULong64_t pktn    = 0;  
     if( GetOrbitalInfo() ){  
         obt     = GetOrbitalInfo()->OBT;  
         pktn    = GetOrbitalInfo()->pkt_num;  
5781      }      }
5782    
5783      if( !GetOrbitalInfo() && !ISGP){      ULong64_t obt = 0;
5784          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo "<<endl;      ULong64_t pktn = 0;
5785          return 0;      if (GetOrbitalInfo()) {
5786      }        obt = GetOrbitalInfo()->OBT;
5787      if( obt==0 && pktn==0 && !ISGP ){        pktn = GetOrbitalInfo()->pkt_num;
5788          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? "<<endl;      }
5789          return 0;  
5790        if (!GetOrbitalInfo() && !ISGP) {
5791          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;
5792          return 0;
5793        }
5794        if (obt == 0 && pktn == 0 && !ISGP) {
5795          cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;
5796          return 0;
5797      }      }
5798    
5799      // ---------------------------------------------------------------------      // ---------------------------------------------------------------------
# Line 3229  Int_t PamLevel2::GetYodaEntry(){ Line 5807  Int_t PamLevel2::GetYodaEntry(){
5807      // 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
5808      // is shift forward until when the packets match.      // is shift forward until when the packets match.
5809      // ---------------------------------------------------------------------      // ---------------------------------------------------------------------
5810      Int_t answer = 0;      Long64_t shift = 0LL;
5811      Int_t shift =0;      Long64_t answer = quellagiusta + shift + yprevshift;
5812      //    printf(" siamo qui %i %i \n",shift,prevshift);      Int_t readl0 = 0;
5813      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;  
         }  
5814    
5815          if(ISGP){      if (DBG){
5816              obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO        printf(" siamo qui shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5817              pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO      }
         }  
5818    
5819  //      cout << "PKTNUM "<<shift<<" ==  L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;  
5820  //      cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;      if (ISGP) {
5821  //      if( (quellagiusta+shift) == l0_tree->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;        obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
5822  //      cout << " obt "<< obt << endl;        pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
5823  //      cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;      }
5824  //      cout << " pktn "<< pktn << endl;  
5825  //      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 ){
5826  //      printf(" IDRUN %u \n",GetRunInfo()->ID);        if ( isSync && shift == 0LL ){
5827  //          printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found in place!!! \n");
5828          if ( prevshift != 0 && (quellagiusta+(Long64_t)shift) == GetYodaTree()->GetEntries() ){          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5829            prevshift = 0;          cout << "\nFor bug reporting instructions, please see for example:\n";
5830            shift = -1;          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5831          };          cout << "  " << endl;
5832          }
5833      }while( ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) ||        if (shift > 0) {
5834                pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())       )          if (DBG) cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
5835              && (quellagiusta+(Long64_t)shift) < GetYodaTree()->GetEntries() && shift < maxshift);          if (DBG)
5836              cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
5837      if ( (quellagiusta+(Long64_t)shift+(Long64_t)prevshift) > GetYodaTree()->GetEntries() || shift == maxshift ) {                 << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
5838          cout << " Big trouble here, no such event in Level0 data! " <<endl;          if (DBG)
5839          return 0;            cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
5840      }                 << " prevshift " << prevshift << " )" << endl;
5841  //    cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;        }
5842  //    return GetYodaTree()->GetEntry(quellagiusta);        answer = quellagiusta +  shift+ yprevshift;
5843      if ( shift > 1 ) prevshift += (shift-1);        readl0 = l0_tree->GetEntry(answer);
5844              //      printf(" inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5845      return answer;  
5846          if (!GetEventHeader()) {
5847            cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
5848            return 0;
5849          }
5850    
5851          //
5852          if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
5853            if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5854            //        printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5855            yprevshift = 0LL;
5856            shift = -1LL;
5857          };
5858            
5859          shift++;
5860        }
5861                                      
5862    
5863        if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
5864          if ( isSync ){
5865            printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found AT ALL!!! \n");
5866            cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5867            cout << "\nFor bug reporting instructions, please see for example:\n";
5868            cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5869            cout << "  " << endl;
5870          }
5871          cout << "Int_t PamLevel2::GetYodaEntry() -- WARNING -- " << endl;
5872          cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;    
5873          cout << " Nested and/or DarthVader skipped packets in fragmented run? checking and trying to fix " <<endl;
5874          // query the DB for runs containing the event, loop over LEVEL0 files which could contain the level0 event and try to find it
5875          // 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
5876          // connect to db
5877          if (!dbc || (dbc && !dbc->IsConnected())) SetDBConnection(); //Emiliano
5878          //
5879          if (GetOrbitalInfo()){
5880            abstime = GetOrbitalInfo()->absTime;
5881          } else {
5882            printf(" PamLevel2::GetYodaEntry() ERROR! no OrbitalInfo, cannot get the absolute time for event \n");
5883            return 0;
5884          }
5885          // query DB looking for runs containing the processed event
5886          TSQLResult *pResult;
5887          TSQLRow *Row = NULL;
5888          TString myquery = Form("select ID,NEVENTS from GL_RUN where RUNHEADER_TIME<=%lld and RUNTRAILER_TIME>=%lld;",abstime,abstime);
5889          if ( DBG ) printf(" query is %s \n",myquery.Data());
5890          //      printf(" query is %s \n",myquery.Data());// TOGLITOGLI
5891          pResult = dbc->Query(myquery.Data());    
5892          if (!pResult->GetRowCount()){
5893            printf(" PamLevel2::GetYodaEntry() ERROR! event is not included in any run!!! \n");
5894            cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5895            cout << "\nFor bug reporting instructions, please see for example:\n";
5896            cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5897            cout << "  " << endl;
5898            return 0;
5899          }
5900          if ( pResult->GetRowCount() == 1 ){
5901            if (DBG) printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");
5902            //        printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");//TOGLITOGLI
5903          }
5904          for( Int_t ru=0; ru < pResult->GetRowCount(); ru++){ // loop over runs containing the event
5905            if (Row) delete Row;
5906            Row = pResult->Next();  
5907            if( Row == NULL ) break;
5908            UInt_t idrun = (UInt_t)atoll(Row->GetField(0));
5909            UInt_t nev = (UInt_t)atoll(Row->GetField(1));
5910            if (DBG) printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);
5911            //        printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);//TOGLITOGLI
5912    
5913            // now look for this run in the level2 file, it must be present! code is taken from updateruninfo of course
5914            Bool_t rfound = false;
5915            totrunentry = 0LL;
5916            runfirstentry = 0LL;
5917            for (Int_t r=0; r< run_tree->GetEntries();r++){
5918              run_tree->GetEntry(r);//update runinfo
5919              if ( r > 0 ){
5920                totrunentrymin = totrunentrymax+1;
5921              } else {
5922                totrunentrymin = 0LL;
5923              }
5924              totrunentry += GetRunInfo()->NEVENTS;
5925              totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
5926              irun = r;        
5927    
5928              if (idrun == GetRunInfo()->ID){
5929                if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
5930                  if (DBG) printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
5931                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
5932                  //              printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
5933                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
5934                  prevshift += (totrunentrymin-iev); // add the new shift to total shift
5935                  totrunentrymin -= (totrunentrymin-iev); // shift run position min
5936                  totrunentrymax -= (totrunentrymin-iev); // shift run position max
5937                  if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
5938                  //              printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
5939                }
5940                runfirstentry = totrunentrymin; // first entry of the run in the level2
5941              
5942    
5943                //
5944                if (gltsync)
5945                  delete gltsync; // Emiliano
5946                if (!dbc || (dbc && !dbc->IsConnected()))
5947                  SetDBConnection(); //Emiliano
5948                gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
5949                if (dbc){
5950                  dbc->Close(); // Emiliano
5951                  delete dbc;
5952                  dbc=0;
5953                }          
5954                if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
5955                  cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
5956                       << "  has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
5957                  cout
5958                    << "                                                            (NB!! in this case some events could be assigned to a wrong run)"
5959                    << endl;
5960                }
5961                //
5962                if (DBG) printf(" found \n");
5963                //            printf(" found \n");//TOGLITOGLI
5964                rfound = true;
5965                //
5966                break;
5967              }
5968            } // loop over run
5969            if ( !rfound ){
5970              printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level2 file!!! \n");
5971              cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5972              cout << "\nFor bug reporting instructions, please see for example:\n";
5973              cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5974              cout << "  " << endl;        
5975              return 0;
5976            }
5977    
5978            // here we got the first run and we can check if it contains the level0 event
5979            if (!GetYodaTree()){
5980              printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
5981              return 0;
5982            }      
5983    
5984            // get the current run entry
5985            irunentry = iev - runfirstentry;
5986            if (irunentry < 0) {
5987              if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
5988              //          cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
5989              irunentry = 0LL;
5990            }
5991            //  ---------------------------------
5992            //  if file is NOT a preselected file
5993            //  ---------------------------------
5994            quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
5995          
5996            if (DBG){
5997              cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
5998              cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
5999              cout << " time "<< abstime << endl;
6000            }
6001            //        cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
6002            //        cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
6003            //        cout << " time "<< abstime << endl; // TOGLITOGLI
6004          
6005            shift = 0;
6006            answer = quellagiusta + shift + yprevshift; // prevshift already included in irunentry
6007            readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
6008    
6009            if (DBG){
6010              printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
6011            }
6012            //        printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
6013          
6014            while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){
6015              if (shift > 0) {
6016                if (DBG) cout << " PKTNUM  L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
6017                if (DBG)
6018                  cout << "         RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
6019                       << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
6020                if (DBG)
6021                  cout << "         L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
6022                       << " prevshift " << prevshift << " )" << endl;
6023              }
6024              answer = quellagiusta +  shift+ yprevshift;
6025              readl0 = l0_tree->GetEntry(answer);
6026              //          printf(" inside inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
6027            
6028              if (!GetEventHeader()) {
6029                cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
6030                return 0;
6031              }
6032              //
6033              if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
6034                if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
6035                //            printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
6036                yprevshift = 0;
6037                shift = -1;
6038              };
6039            
6040              shift++;
6041            }
6042          
6043            if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
6044              //still not the good run... continue with the nex one!
6045              printf("still not the good run... continue with the nex one!\n");
6046            } else {
6047              if (DBG) cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
6048              //          cout << "LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;//TOGLITOGLI
6049              if (shift > 1) yprevshift = (shift - 1);
6050              if (Row) delete Row;
6051              delete pResult;  
6052              if (dbc){
6053                dbc->Close(); // Emiliano
6054                delete dbc;
6055                dbc=0;
6056              }    
6057              il0entry = answer;
6058              return readl0;
6059            }
6060            // perhaps it is all
6061          }// loop over runs containing the event
6062          if (Row) delete Row;
6063          delete pResult;  
6064          if (dbc){
6065            dbc->Close(); // Emiliano
6066            delete dbc;
6067            dbc=0;
6068          }          
6069          // arriving here it means no run found, cannot be! error!
6070          printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level0 files!!! \n");
6071          cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
6072          cout << "\nFor bug reporting instructions, please see for example:\n";
6073          cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
6074          cout << "  " << endl;        
6075          return 0;
6076        } else {
6077          if (DBG) cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
6078          //    cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
6079          //    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()) );
6080          if (shift > 1) yprevshift = (shift - 1);
6081          il0entry = answer;
6082          return readl0;
6083        }
6084    
6085        /*  } // if selli 0 || 2
6086      if ( SELLI == 1 && hasL0EE ){
6087        sel_tree->GetEntry(iev);  
6088        Long64_t answer = il0entry;
6089        Int_t readl0 = 0;
6090        readl0 = l0_tree->GetEntry(answer);
6091        return readl0;
6092        }*/
6093      printf(" PamLevel2::GetYodaEntry() ERROR! \n");
6094      cout << " Entry not found! OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
6095      cout << "\nFor bug reporting instructions, please see for example:\n";
6096      cout << "     <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
6097      cout << "  " << endl;
6098      return 0;
6099  }  }
6100    
6101  /**  /**
6102   * \Brief Set DB connection   * \Brief Set DB connection
6103   */   */
6104  Bool_t PamLevel2::SetDBConnection(){  Bool_t PamLevel2::SetDBConnection() {
6105    
6106      //    cout << "PamLevel2::SetDBConnection()" << endl;
6107      if (DBG) {
6108        cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
6109        cout << "Connecting to DB" << endl;
6110        cout << "HOST " << host << endl;
6111        cout << "USER " << user << endl;
6112        cout << "PSW  " << psw << endl;
6113      }
6114      Bool_t notconn = true;
6115      Int_t trials = 0;
6116      while ( notconn && trials < 10 ){
6117        //    gSystem->Sleep(500);
6118        dbc = TSQLServer::Connect(host.Data(), user.Data(), psw.Data());
6119        //dbc->Connect(host.Data(), user.Data(), psw.Data());
6120        if ( dbc ) notconn = false;
6121        if (DBG) printf("<%i> test connection...\n ",trials);
6122        if (!dbc){
6123          if (DBG) printf(" :( failed, no pointer \n");
6124          notconn = true;
6125          //      return false;
6126        };
6127        if (dbc && !dbc->IsConnected()){
6128          if (DBG) printf(" :( failed, no connection \n");
6129          notconn = true;
6130          //      return false;
6131        };
6132        trials++;
6133      };
6134      if ( notconn ) return false;
6135      //
6136      if (DBG) printf("=connected!\n");
6137      stringstream myquery; // EMILIANO
6138      myquery.str(""); // EMILIANO
6139      myquery << "SET time_zone='+0:00'"; // EMILIANO
6140      dbc->Query(myquery.str().c_str()); // EMILIANO
6141      if ( DBG ) printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
6142      return true;
6143    
6144    }
6145    
6146    /**
6147     * \Brief Add a friend to the pamela chain.
6148     * @param cname name of the chain to be added
6149     */
6150    
6151    TChain* PamLevel2::AddFriend(TString cname) {
6152    
6153      if (!GetPamTree()) {
6154        cout << " TChain* PamLevel2::AddFriend(TString cname) --- a pamela tree must be created first" << endl;
6155        return NULL;
6156      }
6157    
6158      TChain *c = new TChain(cname.Data());
6159    
6160      TIter next(GetPamTree()->GetListOfFiles());
6161      Int_t nf = 0;
6162      TChainElement* element = 0;
6163      while ((element = (TChainElement*) next())) {
6164        c->Add(element->GetTitle());
6165        nf++;
6166      }
6167    
6168      GetPamTree()->AddFriend(cname.Data());
6169    
6170      cout << "external chain created and added to pamela friends :" << cname << endl;
6171      cout << "n.files " << nf << endl;
6172    
6173      return c;
6174    
6175      cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;  }
6176      cout<<"Connecting to DB"<<endl;  
6177      cout<<"HOST "<<host<<endl;  /**
6178      cout<<"USER "<<user<<endl;   * Returns the current read entry. This method simply returns the result of the call to
6179      cout<<"PSW  "<<psw<<endl;   * pam_tree->GetReadEntry(), so it is entirely handled by ROOT.
6180      dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());   */
6181      if( !dbc )return false;  Long64_t PamLevel2::GetReadEntry() {
6182      if( !dbc->IsConnected() )return false;        return pam_tree->GetReadEntry();
6183      cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;  }
     return true;  
6184    
6185    /**
6186     * Sets the sorting method. If the new method is different from the previous, the issorted
6187     * flag is set to false, forcing a new sort the next time GetTrack is called.
6188     * @see GetTrack
6189     */
6190    void PamLevel2::SetSortingMethod(TString how) {
6191      if (howtosort != how) {
6192        issorted = false;
6193        issorted_new = false;
6194      }
6195      howtosort = how;
6196  }  }

Legend:
Removed from v.1.44  
changed lines
  Added in v.1.111

  ViewVC Help
Powered by ViewVC 1.1.23