/[PAMELA software]/PamelaLevel2/src/PamLevel2.cpp
ViewVC logotype

Annotation of /PamelaLevel2/src/PamLevel2.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.114 - (hide annotations) (download)
Tue Jan 17 09:49:51 2017 UTC (8 years, 1 month ago) by pamela
Branch: MAIN
CVS Tags: HEAD
Changes since 1.113: +52 -0 lines
Added new tracking algorythm "HE"

1 pam-fi 1.1 #include <PamLevel2.h>
2 pam-fi 1.52
3     /////////////////////////////////////////////////////////////////////////////
4     /////////////////////////////////////////////////////////////////////////////
5     /////////////////////////////////////////////////////////////////////////////
6     /////////////////////////////////////////////////////////////////////////////
7    
8 pam-fi 1.81 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 pam-fi 1.52 }
29    
30 pam-fi 1.81 void GPamela::Delete() {
31    
32     Clear();
33 mocchiut 1.93 /* EM all these are in the stack not in the heap, so no need to delete by hand...
34 pam-fi 1.81 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 mocchiut 1.93 */
135     }
136 pam-fi 1.52
137 pam-fi 1.81
138     void GPamela::SetBranchAddress(TChain* fhBookTree) {
139 pam-fi 1.52
140 pam-fi 1.81 // 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 pam-fi 1.52
262 pam-fi 1.81 // fhBookTree->SetBranchStatus("*",0);
263 pam-fi 1.52
264     }
265    
266 pam-fi 1.81 ClassImp( GPamela);
267 pam-fi 1.52
268     /////////////////////////////////////////////////////////////////////////////
269     /////////////////////////////////////////////////////////////////////////////
270     /////////////////////////////////////////////////////////////////////////////
271     /////////////////////////////////////////////////////////////////////////////
272 pam-fi 1.1 //--------------------------------------
273     //
274     //
275     //--------------------------------------
276     /**
277 pam-fi 1.81 * Default constructor
278 pam-fi 1.1 */
279 pam-fi 1.81 PamTrack::PamTrack() {
280     trk_track = 0;
281 pam-ts 1.95 trk_ext_track = 0;
282 pam-fi 1.81 calo_track = 0;
283     tof_track = 0;
284     orb_track = 0;
285     candeleteobj = 0;
286     pscore = 0;
287     iscore = 0;
288     }
289     ;
290 pam-fi 1.1 //--------------------------------------
291     //
292     //
293     //--------------------------------------
294     /**
295 pam-fi 1.81 * Constructor
296 pam-fi 1.1 */
297 pam-fi 1.81 PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o, OrbitalInfoTrkVar *r) {
298    
299 pam-ts 1.95 trk_ext_track = 0;
300 pam-fi 1.81 trk_track = 0;
301     calo_track = 0;
302     tof_track = 0;
303     orb_track = 0;
304     // if(t)trk_track = new TrkTrack(*t);
305     // if(c)calo_track = new CaloTrkVar(*c);
306     // if(o)tof_track = new ToFTrkVar(*o);
307 pam-ts 1.95 // if (t)
308     // 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 pam-fi 1.111 // cout << t<<"--"<<c<<"--"<<o<<"--"<<r<<"--"<< endl;
319 pam-ts 1.95
320     if (t){
321     trk_track = new TrkTrack(*t);
322     trk_ext_track = new ExtTrack(*t);//NB!! ha dimensione 6 invece che 8
323 pam-fi 1.111 }else{
324     trk_track = new TrkTrack();
325     trk_ext_track = new ExtTrack();
326    
327 pam-ts 1.95 }
328 pam-fi 1.111
329 pam-fi 1.81 if (c)
330 pam-ts 1.95 calo_track = new CaloTrkVar(*c);
331 pam-fi 1.111 else
332     calo_track = new CaloTrkVar();
333    
334 pam-fi 1.81 if (o)
335 pam-ts 1.95 tof_track = new ToFTrkVar(*o);
336 pam-fi 1.111 else
337     tof_track = new ToFTrkVar();
338    
339 pam-fi 1.81 if (r)
340 pam-ts 1.95 orb_track = new OrbitalInfoTrkVar(*r);
341 pam-fi 1.111 else
342     orb_track = new OrbitalInfoTrkVar();
343    
344     // cout << trk_track<<"--"<< calo_track <<"--"<<tof_track<<"--"<<orb_track<<"--"<< endl;
345    
346 pam-ts 1.95 candeleteobj = 1;
347    
348     }
349     ;
350     /**
351     * Constructor
352     */
353     PamTrack::PamTrack(ExtTrack* t, CaloTrkVar* c, ToFTrkVar* o, OrbitalInfoTrkVar *r) {
354    
355 pam-fi 1.111
356    
357 pam-ts 1.95 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 pam-fi 1.111 // cout << t<<"--"<<c<<"--"<<o<<"--"<<r<<"--"<< endl;
377    
378 pam-fi 1.81
379 pam-ts 1.95 if (t){
380     //// trk_track = new TrkTrack(*t);//in this case TrkTrack object remains null
381     trk_ext_track = new ExtTrack(*t);
382 pam-fi 1.111 }else{
383     trk_ext_track = new ExtTrack();
384 pam-ts 1.95 }
385     if (c)
386     calo_track = new CaloTrkVar(*c);
387 pam-fi 1.111 else
388     calo_track = new CaloTrkVar();
389    
390 pam-ts 1.95 if (o)
391     tof_track = new ToFTrkVar(*o);
392 pam-fi 1.111 else
393     tof_track = new ToFTrkVar();
394    
395 pam-ts 1.95 if (r)
396     orb_track = new OrbitalInfoTrkVar(*r);
397 pam-fi 1.111 else
398     orb_track = new OrbitalInfoTrkVar();
399    
400     // cout << trk_track<<"--"<< calo_track <<"--"<<tof_track<<"--"<<orb_track<<"--"<< endl;
401    
402 pam-ts 1.95 candeleteobj = 1;
403     pscore = 0;
404     iscore = 0;
405 pam-fi 1.81
406     }
407     ;
408 pam-fi 1.110
409 pam-fi 1.81 PamTrack::PamTrack(const PamTrack& track) {
410    
411     TrkTrack *t = track.trk_track;
412 pam-fi 1.97 ExtTrack *e = track.trk_ext_track;
413 pam-fi 1.81 CaloTrkVar *c = track.calo_track;
414     ToFTrkVar *o = track.tof_track;
415     OrbitalInfoTrkVar *r = track.orb_track;
416    
417 pam-fi 1.97 trk_ext_track = 0;
418     trk_track = 0;
419     calo_track = 0;
420     tof_track = 0;
421     orb_track = 0;
422     if(e)
423     trk_ext_track = new ExtTrack(*e);
424 pam-fi 1.81 if (t)
425     trk_track = new TrkTrack(*t);
426     if (c)
427     calo_track = new CaloTrkVar(*c);
428     if (o)
429     tof_track = new ToFTrkVar(*o);
430     if (r)
431     orb_track = new OrbitalInfoTrkVar(*r);
432 pam-fi 1.97
433 pam-fi 1.81 candeleteobj = 1;
434 pam-ts 1.95 pscore = 0;
435     iscore = 0;
436 pam-fi 1.81
437     }
438 pam-fi 1.110
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 pam-fi 1.97 void PamTrack::Clear(Option_t *option) {
456 pam-fi 1.13
457 pam-fi 1.97 // cout << "PamTrack::Clear( "<<option<<" ) "<<candeleteobj<<endl;
458 pam-fi 1.81 if (candeleteobj) {
459 pam-ts 1.95
460     if (trk_ext_track)
461 pam-fi 1.97 trk_ext_track->ExtTrack::Clear(option);
462 pam-fi 1.81 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 pam-ts 1.95 trk_ext_track = 0;
473 pam-fi 1.81 trk_track = 0;
474 pam-fi 1.13 calo_track = 0;
475 pam-fi 1.81 tof_track = 0;
476     orb_track = 0;
477     }
478     pscore = 0;
479     iscore = 0;
480 pam-fi 1.13
481 pam-fi 1.81 }
482     void PamTrack::Delete() {
483     // cout << "PamTrack::Delete() "<<candeleteobj<<endl;
484     if (candeleteobj) {
485 pam-ts 1.95 if (trk_ext_track) {
486 pam-fi 1.110 // 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 pam-ts 1.95 delete trk_ext_track;
489     }
490 pam-fi 1.81 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 pam-fi 1.13 }
506 pam-fi 1.81 }
507     else {
508     Clear();
509     }
510 pam-fi 1.13 }
511 pam-ts 1.95
512    
513    
514    
515 pam-fi 1.1 //--------------------------------------
516     //
517     //
518     //--------------------------------------
519     /**
520 pam-fi 1.81 * Default Constructor
521 pam-fi 1.1 */
522 pam-fi 1.81 PamLevel2::PamLevel2() {
523     Initialize();
524     }
525 mocchiut 1.90
526 mocchiut 1.35
527     /**
528 pam-fi 1.52 * 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 pam-fi 1.81 * Possible options are:
533 pam-fi 1.52 * +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 pam-fi 1.81 * +(-)TRK0 --> include(exclude) tracker level0 tree
537 pam-fi 1.52 * +(-)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 mocchiut 1.35 */
541 pam-fi 1.81 PamLevel2::PamLevel2(TString ddir, TString llist, TString detlist) {
542     Initialize();
543     TList* listf = GetListOfLevel2Files(ddir, llist);
544     if (listf)
545     GetPamTree(listf, detlist);
546     if (listf)
547     GetRunTree(listf);
548 pam-ts 1.95 SetMaxShift(-1);
549 pam-fi 1.81 }
550 mocchiut 1.90
551 pam-fi 1.81
552     PamLevel2::PamLevel2(TString ddir, TList *llist, TString detlist) {
553     Initialize();
554     GetPamTree(llist, detlist);
555     GetRunTree(llist);
556 pam-ts 1.95 SetMaxShift(-1);
557 pam-fi 1.81 }
558 mocchiut 1.90
559 mocchiut 1.35 /**
560 pam-fi 1.81 * Constructor
561 pam-fi 1.52 * @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 mocchiut 1.35 */
566 pam-fi 1.81 PamLevel2::PamLevel2(TString ddir, TString llist) {
567     Initialize();
568 pam-ts 1.95 TList* listf = GetListOfLevel2Files(ddir, llist);
569     cout << "GetPamTree: "<<endl;
570 pam-fi 1.81 GetPamTree(listf, "");
571 pam-ts 1.95 cout << "GetRunTree: "<<endl;
572 pam-fi 1.81 GetRunTree(listf);
573 pam-ts 1.95 SetMaxShift(-1);
574 pam-fi 1.81 }
575 mocchiut 1.90
576 pam-fi 1.81
577     void PamLevel2::Initialize() {
578 mocchiut 1.35
579 pam-fi 1.81 h0_obj = 0;
580     trk0_obj = 0;
581     calo0_obj = 0;
582    
583     trk2_obj = 0;
584     trk1_obj = 0;
585     trkh_obj = 0;
586     calo1_obj = 0;
587     calo2_obj = 0;
588 pam-ts 1.95 tof2_obj = 0;
589 pam-fi 1.81 trig_obj = 0;
590     s4_obj = 0;
591     nd_obj = 0;
592     ac_obj = 0;
593 pam-ts 1.95 orb2_obj = 0;
594 pam-fi 1.81 gp_obj = 0;
595    
596 pam-ts 1.95 extAlgFlag=0;
597    
598     trk_ext_obj = 0;
599     trk_ext_nuc_obj = 0;
600     trk_nuc_obj = 0;
601    
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 pam-fi 1.81 run_obj = 0;//new GL_RUN();
621     soft_obj = 0;// Emiliano
622 mocchiut 1.100 proc_obj = 0;// Emiliano
623 pam-fi 1.81 irun = -1LL;
624     irunt = -1LL;
625 mocchiut 1.89 totrunentry = 0LL;
626     totrunentrymax = 0LL;
627     totrunentrymin = 0LL;
628 pam-fi 1.81 runfirstentry = 0LL;
629     runlastentry = 0LL;
630     gltsync = 0; // Emiliano
631 mocchiut 1.85 fUpdateRunInfo = true; // Emiliano
632 mocchiut 1.94 fUseDBinRunInfo = true; // Emiliano
633 mocchiut 1.98 fDiscarded = false; //EM
634 mocchiut 1.90 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 mocchiut 1.91 // hasL0EE = true;
637 pam-fi 1.81
638     l0_file = NULL;
639     l0_tree = NULL;
640     iroot = -1;
641     dbc = 0;
642    
643     prevshift = 0;
644 mocchiut 1.90 yprevshift = 0;
645     maxshift = 10; //EMILIANO now overridden by SetMaxShift(-1) called by constructors
646 pam-fi 1.81
647     run_tree = NULL;
648     run_tree_clone = NULL;
649 mocchiut 1.100
650     proc_tree = NULL;
651     proc_tree_clone = NULL;
652    
653 pam-fi 1.81 sel_tree = NULL;
654     sel_tree_clone = NULL;
655    
656     irunentry = -1LL;
657     pam_tree = NULL;
658     for (Int_t i = 0; i < NCLONES; i++)
659     pam_tree_clone[i] = NULL;
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 emocchiutti 1.108 customString = "";
685    
686 pam-fi 1.81 // 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 mocchiut 1.100 PROC = true;
703 pam-fi 1.81 GP = false;
704    
705 pam-ts 1.95 EXT = false;
706     NUC = false;
707 pam-ts 1.96 trkAlg = "STD";//default tracking algorythm
708 pam-ts 1.95
709 pam-fi 1.81 RUN = true;
710    
711     SELLI = -1;
712    
713     ISGP = false;
714    
715     DBG = false;
716    
717     tsorted = 0;
718     timage = 0;
719 pam-ts 1.95 text = 0 ;
720    
721     tsorted_nuc = 0;
722     timage_nuc = 0;
723     text_nuc = 0 ;
724 pam-fi 1.81
725     howtosort = "+CAL+TOF";
726     //howtosort = "+TOF";
727     sortthr = 100.;
728 mocchiut 1.35
729 pam-fi 1.81 issorted = false;
730     lastsorted = -1;
731 pam-ts 1.95 issorted_new = false;
732     lastsorted_new = -1;
733 mocchiut 1.30
734 pam-fi 1.81 }
735     ;
736     /**
737     * Delete the event (act as Dtor)
738     */
739     void PamLevel2::Delete() {
740 pam-fi 1.20
741 pam-fi 1.81 if (run_obj)
742     delete run_obj;
743     if (soft_obj)
744     delete soft_obj; //Emiliano
745 mocchiut 1.100 if (proc_obj)
746     delete proc_obj; //Emiliano
747 pam-fi 1.81
748     // cout << "void PamLevel2::Clear()"<<endl;
749     if (h0_obj)
750     delete h0_obj;
751     if (trk0_obj)
752     delete trk0_obj;
753     if (calo0_obj)
754     delete calo0_obj;
755     if (trk1_obj)
756     delete trk1_obj;
757     if (trk2_obj)
758     delete trk2_obj;
759     if (trkh_obj)
760     delete trkh_obj;
761     if (calo1_obj)
762     delete calo1_obj;
763     if (calo2_obj)
764     delete calo2_obj;
765 pam-ts 1.95 if (tof2_obj)
766     delete tof2_obj;
767 pam-fi 1.81 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 pam-ts 1.95 if (orb2_obj)
776     delete orb2_obj;
777 pam-fi 1.81 if (gp_obj)
778     delete gp_obj;
779 pam-fi 1.9
780 pam-ts 1.95 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    
803    
804 pam-fi 1.81 if (tsorted) {
805     tsorted->Delete();
806     delete tsorted;
807     }
808     if (timage) {
809     timage->Delete();
810     delete timage;
811     }
812 pam-ts 1.95 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 pam-fi 1.52
831 pam-fi 1.81 if (dbc) {
832     dbc->Close();
833     delete dbc;
834 mocchiut 1.82 dbc=0;
835 pam-fi 1.81 }
836    
837     if (gltsync)
838     delete gltsync;
839    
840     if (l0_file)
841     l0_file->Close();
842     // if(pam_tree)pam_tree->Delete();;
843 pam-fi 1.20
844 pam-fi 1.81 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...
847     //
848     TList *temp = pam_tree->GetListOfFriends();
849     TList *contents = new TList; // create chain friend list
850     contents->SetOwner();
851     TIter next(temp);
852     TChain *questo = 0;
853     while ((questo = (TChain*) next())) {
854     TString name = questo->GetName();
855     contents->Add((TChain*) gROOT->FindObject(name.Data()));// add object to the list
856     };
857     //
858     // deleting the main chain
859     //
860     pam_tree->Delete();
861     //
862     // deleting the friends...
863     //
864     TIter next2(contents);
865     TChain *questa = 0;
866 mocchiut 1.83 while ( (questa = (TChain*)next2()) ) {
867 pam-fi 1.81 TString name = questa->GetName();
868     questa->Delete();
869     questa = NULL;
870     };
871     //
872     };
873     pam_tree = NULL;
874 pam-fi 1.8
875 pam-fi 1.81 if (run_tree)
876     run_tree->Delete();;
877     if (sel_tree)
878     sel_tree->Delete();;
879 pam-fi 1.88
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 pam-fi 1.81 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 pam-fi 1.88 sel_tree_clone->Delete();;*/
893 pam-fi 1.43
894 pam-fi 1.81 if (irunoffset)
895     delete[] irunoffset;
896 pam-fi 1.59
897 pam-ts 1.95
898     Initialize();
899    
900 pam-fi 1.81 }
901     ;
902 pam-fi 1.13
903 pam-fi 1.2 /**
904 pam-fi 1.81 * Clear the event (NB! does not deallocate objects)
905 pam-fi 1.2 */
906 pam-fi 1.81 void PamLevel2::Clear() {
907    
908     // cout << "void PamLevel2::Clear()"<<endl;
909 mocchiut 1.35
910 pam-fi 1.81 //
911     // 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     //
914     // 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 pam-ts 1.95 if (tof2_obj)
934     tof2_obj->Clear();
935 pam-fi 1.81 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 pam-ts 1.95 if (orb2_obj)
944     orb2_obj->Clear();
945 pam-fi 1.81 if (gp_obj)
946     gp_obj->Clear();
947 mocchiut 1.100 if (proc_obj)
948     proc_obj->Clear();
949 pam-fi 1.20
950 pam-fi 1.81 // if(sorted_tracks)sorted_tracks->Clear();
951     // sorted_tracks.Clear();
952 pam-fi 1.13
953 pam-ts 1.95 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 pam-fi 1.81 }
982     ;
983 mocchiut 1.35
984 pam-fi 1.81 void PamLevel2::Reset() {
985     //
986     // First of all clear everything
987     //
988     Clear();
989     //
990     // close and reset chains and pointers
991     //
992     if (pam_tree) {
993 mocchiut 1.35 //
994 pam-fi 1.81 // 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 pam-fi 1.55 //
996 pam-fi 1.81 TList *temp = pam_tree->GetListOfFriends();
997     TList *contents = new TList; // create chain friend list
998     contents->SetOwner();
999     TIter next(temp);
1000     TChain *questo = 0;
1001     while ((questo = (TChain*) next())) {
1002     TString name = questo->GetName();
1003     contents->Add((TChain*) gROOT->FindObject(name.Data()));// add object to the list
1004 pam-fi 1.55 };
1005     //
1006 pam-fi 1.81 // deleting the main chain
1007 pam-fi 1.55 //
1008 pam-fi 1.81 pam_tree->Delete();
1009 pam-fi 1.55 //
1010 pam-fi 1.81 // deleting the friends...
1011 pam-fi 1.55 //
1012 pam-fi 1.81 TIter next2(contents);
1013     TChain *questa = 0;
1014 mocchiut 1.83 while ( (questa = (TChain*) next2()) ) {
1015 pam-fi 1.81 TString name = questa->GetName();
1016     questa->Delete();
1017     questa = NULL;
1018     };
1019 pam-fi 1.55 //
1020 pam-fi 1.81 };
1021     pam_tree = NULL;
1022     //
1023     if (run_tree)
1024     run_tree->Delete();;
1025     run_tree = NULL;
1026     if (sel_tree)
1027     sel_tree->Delete();;
1028     sel_tree = NULL;
1029 mocchiut 1.102
1030     if (proc_tree)
1031     proc_tree->Delete();
1032     proc_tree = NULL;
1033 pam-fi 1.81 //
1034     // Close file
1035     //
1036     if (l0_file)
1037     l0_file->Close("R");
1038     l0_file = NULL;
1039     //
1040     h0_obj = 0;
1041     trk0_obj = 0;
1042     calo0_obj = 0;
1043     //
1044     trk2_obj = 0;
1045     trk1_obj = 0;
1046     trkh_obj = 0;
1047     calo1_obj = 0;
1048     calo2_obj = 0;
1049 pam-ts 1.95 tof2_obj = 0;
1050 pam-fi 1.81 trig_obj = 0;
1051     s4_obj = 0;
1052     nd_obj = 0;
1053     ac_obj = 0;
1054 pam-ts 1.95 orb2_obj = 0;
1055 pam-fi 1.81 gp_obj = 0;
1056 mocchiut 1.100 proc_obj = 0;
1057 pam-ts 1.95
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 pam-fi 1.81 //
1084     // Reset run pointers
1085     //
1086     run_obj = 0;//new GL_RUN();
1087     soft_obj = 0;// Emiliano
1088 mocchiut 1.100 proc_obj = 0;// Emiliano
1089 pam-fi 1.81 irun = -1;
1090     irunt = -1;
1091 mocchiut 1.89 totrunentry = 0LL;
1092     totrunentrymax = 0LL;
1093     totrunentrymin = 0LL;
1094 pam-fi 1.81 runfirstentry = 0ULL;
1095     runlastentry = 0ULL;
1096 mocchiut 1.89 prevabstime = 0ULL;
1097     prevpktnum = 0;
1098     abstime = 0ULL;
1099     pktnum = 0;
1100     isFragment = false;
1101 pam-fi 1.81 //
1102     totdltime[0] = 0LL;
1103     totdltime[1] = 0LL;
1104     totdltime[2] = 0LL;
1105     //
1106     }
1107     ;
1108 pam-fi 1.2
1109 pam-fi 1.81 Bool_t PamLevel2::IsGood(Bool_t strict) {
1110     Bool_t goodev = true;
1111 mocchiut 1.69 //
1112 pam-fi 1.81 if (calo2_obj && !calo2_obj->IsGood(strict))
1113     goodev = false;
1114 mocchiut 1.69 //
1115 pam-fi 1.81 if (strict) {
1116     if (trk2_obj && trk2_obj->UnpackError() != 0)
1117     goodev = false;
1118 pam-ts 1.95 if (tof2_obj && tof2_obj->unpackError != 0)
1119 pam-fi 1.81 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 pam-ts 1.95 // if(orb2_obj)
1129 pam-fi 1.81 }
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 mocchiut 1.69 };
1136 pam-fi 1.81 return (goodev);
1137     }
1138     ;
1139 pam-fi 1.2
1140 mocchiut 1.86 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 mocchiut 1.90 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 pam-ts 1.95 ULong64_t nev = GetEntries();
1160 mocchiut 1.90 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 pam-fi 1.1 //--------------------------------------
1173     //
1174     //
1175     //--------------------------------------
1176 pam-fi 1.81 void *PamLevel2::GetPointerTo(const char* c) {
1177 pam-fi 1.9
1178 pam-fi 1.81 TString objname = c;
1179 pam-fi 1.13
1180 pam-fi 1.81 if (!objname.CompareTo("TrkLevel1")) {
1181     if (!trk1_obj) {
1182     trk1_obj = new TrkLevel1();
1183     trk1_obj->Set();
1184     }
1185     return &trk1_obj;
1186     };
1187     if (!objname.CompareTo("TrkLevel2")) {
1188     if (!trk2_obj) {
1189     trk2_obj = new TrkLevel2();
1190     trk2_obj->Set();
1191     }
1192     return &trk2_obj;
1193     };
1194     if (!objname.CompareTo("TrkHough")) {
1195     if (!trkh_obj) {
1196     trkh_obj = new TrkHough();
1197     trkh_obj->Set();
1198     }
1199     return &trkh_obj;
1200     };
1201     if (!objname.CompareTo("CaloLevel1")) {
1202     if (!calo1_obj)
1203     calo1_obj = new CaloLevel1();
1204     return &calo1_obj;
1205     };
1206     if (!objname.CompareTo("CaloLevel2")) {
1207     if (!calo2_obj) {
1208     calo2_obj = new CaloLevel2();
1209     calo2_obj->Set();
1210 pam-fi 1.13 };
1211 pam-fi 1.81 return &calo2_obj;
1212     };
1213     if (!objname.CompareTo("ToFLevel2")) {
1214 pam-ts 1.95 if (!tof2_obj) {
1215     tof2_obj = new ToFLevel2();
1216     tof2_obj->Set();
1217 pam-fi 1.81 }
1218 pam-ts 1.95 return &tof2_obj;
1219 pam-fi 1.81 };
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 pam-ts 1.95 if (!orb2_obj) {
1242     orb2_obj = new OrbitalInfo();
1243     orb2_obj->Set();
1244 pam-fi 1.81 }
1245 pam-ts 1.95 return &orb2_obj;
1246 pam-fi 1.81 };
1247     // if(!objname.CompareTo("OrbitalInfo")){
1248 pam-ts 1.95 // if(!orb2_obj) orb2_obj = new OrbitalInfo();
1249     // return &orb2_obj;
1250 pam-fi 1.81 // };
1251     if (!objname.CompareTo("GPamela")) {
1252     if (!gp_obj)
1253     gp_obj = new GPamela();
1254     return &gp_obj;
1255     };
1256    
1257     if (!objname.CompareTo("RunInfo"))
1258     return &run_obj;
1259 mocchiut 1.15
1260 pam-fi 1.81 if (!objname.CompareTo("SoftInfo"))
1261     return &soft_obj; // Emiliano
1262 mocchiut 1.15
1263 mocchiut 1.101 if (!objname.CompareTo("ProcInfo")){
1264 mocchiut 1.100 if (!proc_obj)
1265     proc_obj = new ProcInfo();
1266     return &proc_obj; // Emiliano
1267     }
1268    
1269 pam-fi 1.81 return NULL;
1270     }
1271     ;
1272 pam-fi 1.9 //--------------------------------------
1273     //
1274     //
1275     //--------------------------------------
1276 pam-fi 1.1 /**
1277 pam-fi 1.81 * Retrieves the calorimeter track matching the seqno-th tracker stored track.
1278 pam-fi 1.3 * (If seqno = -1 retrieves the self-trigger calorimeter track)
1279     */
1280 pam-fi 1.81 CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno) {
1281    
1282     if (!calo2_obj)
1283     return 0;
1284    
1285     if (calo2_obj->CaloLevel2::ntrk() == 0) {
1286 pam-fi 1.110 if( seqno >=0 ){
1287     cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo " << seqno;
1288     cout << " but no Calorimeter tracks are stored" << endl;
1289     }
1290     return NULL;
1291 pam-fi 1.81 };
1292    
1293     CaloTrkVar *c = 0;
1294     Int_t it_calo = 0;
1295    
1296     do {
1297     c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);
1298     it_calo++;
1299     } while (c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());
1300    
1301     if (!c || seqno != c->trkseqno) {
1302     c = 0;
1303 pam-fi 1.110 if (seqno != -1 && seqno>=0)
1304 pam-fi 1.81 cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo " << seqno
1305     << " does not match Calorimeter stored tracks" << endl;
1306     };
1307     return c;
1308    
1309     }
1310     ;
1311 pam-fi 1.3 //--------------------------------------
1312 pam-fi 1.55 //
1313     //
1314 pam-fi 1.3 //--------------------------------------
1315     /**
1316 pam-fi 1.81 * Retrieves the ToF track matching the seqno-th tracker stored track.
1317 pam-fi 1.55 * (If seqno = -1 retrieves the tracker-independent tof track)
1318 pam-fi 1.3 */
1319 pam-fi 1.81 ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno) {
1320    
1321 pam-ts 1.95 if (!tof2_obj)
1322 pam-fi 1.81 return 0;
1323    
1324 pam-ts 1.95 if (tof2_obj->ToFLevel2::ntrk() == 0) {
1325 pam-fi 1.81 cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo " << seqno << " but no ToF tracks are stored"
1326     << endl;
1327     return NULL;
1328     };
1329    
1330     ToFTrkVar *c = 0;
1331     Int_t it_tof = 0;
1332    
1333     do {
1334 pam-ts 1.95 c = tof2_obj->ToFLevel2::GetToFTrkVar(it_tof);
1335 pam-fi 1.81 it_tof++;
1336 pam-ts 1.95 } while (c && seqno != c->trkseqno && it_tof < tof2_obj->ToFLevel2::ntrk());
1337 pam-fi 1.81
1338     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 pam-fi 1.3
1349     //--------------------------------------
1350 pam-fi 1.55 //
1351     //
1352 pam-fi 1.3 //--------------------------------------
1353     /**
1354 pam-fi 1.81 * Retrieves the OrbitalInfo track matching the seqno-th tracker stored track.
1355 mocchiut 1.64 * (If seqno = -1 retrieves the tracker-independent tof related track)
1356     */
1357 pam-fi 1.81 OrbitalInfoTrkVar *PamLevel2::GetOrbitalInfoStoredTrack(int seqno) {
1358    
1359 pam-ts 1.95 if (!orb2_obj)
1360 pam-fi 1.81 return 0;
1361    
1362 pam-ts 1.95 if (orb2_obj->OrbitalInfo::ntrk() == 0) {
1363 pam-fi 1.81 // // TRICK BEGIN
1364     // OrbitalInfoTrkVar *r = new OrbitalInfoTrkVar(); // TEMPORARY TRICK
1365     // Int_t nn = 0;
1366 pam-ts 1.95 // TClonesArray &tor = *orb2_obj->OrbitalInfoTrk;
1367     // for(Int_t nt=0; nt < tof2_obj->ToFLevel2::ntrk(); nt++){
1368 pam-fi 1.81 // //
1369 pam-ts 1.95 // ToFTrkVar *ptt = tof2_obj->ToFLevel2::GetToFTrkVar(nt);
1370 pam-fi 1.81 // 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 pam-ts 1.95 // c = orb2_obj->OrbitalInfo::GetOrbitalInfoTrkVar(0);
1392 pam-fi 1.81 // 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 pam-ts 1.95 c = orb2_obj->OrbitalInfo::GetOrbitalInfoTrkVar(it_tof);
1404 pam-fi 1.81 it_tof++;
1405 pam-ts 1.95 } while (c && seqno != c->trkseqno && it_tof < orb2_obj->OrbitalInfo::ntrk());
1406 pam-fi 1.81
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 mocchiut 1.64
1418     //--------------------------------------
1419     //
1420     //
1421     //--------------------------------------
1422 pam-fi 1.110 // /**
1423     // * Give the pamela track associated to a tracker track, retrieving related calorimeter, orbitalinfo and tof track information.
1424     // */
1425     // 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 pam-fi 1.81
1441 pam-fi 1.110 // // if(t && c && o)track = new PamTrack(t,c,o);
1442     // PamTrack *track = new PamTrack(t, c, o, r);
1443 pam-fi 1.81
1444 pam-fi 1.110 // return track;
1445 pam-fi 1.81
1446 pam-fi 1.110 // }
1447     // ;
1448 pam-fi 1.3 //--------------------------------------
1449     //
1450     //
1451     //--------------------------------------
1452 pam-fi 1.110 // /**
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 pam-fi 1.1
1467 pam-fi 1.110 // if (itrk >= 0 && itrk < trk2_obj->TrkLevel2::ntrk()) {
1468 pam-fi 1.81
1469 pam-fi 1.110 // TrkTrack *t = trk2_obj->TrkLevel2::GetStoredTrack(itrk);
1470     // track = GetPamTrackAlong(t);
1471 pam-fi 1.81
1472 pam-fi 1.110 // }
1473     // else {
1474     // cout << "PamLevel2::GetStoredTrack(int) : tracker track SeqNo " << itrk << " does not exist (GetNTracks() = "
1475     // << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
1476     // };
1477 pam-fi 1.81
1478 pam-fi 1.110 // return track;
1479 pam-fi 1.81
1480 pam-fi 1.110 // }
1481 pam-fi 1.1 //--------------------------------------
1482     //
1483 mocchiut 1.37
1484     /**
1485 pam-fi 1.55 * Sort physical (tracker) tracks. Left here as backward compatibility method.
1486 mocchiut 1.37 **/
1487 pam-fi 1.81 void PamLevel2::SortTracks(TString how) {
1488     printf(
1489     " WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",
1490     how.Data());
1491     howtosort = how;
1492     SortTracks();
1493     }
1494     ;
1495 mocchiut 1.37
1496 pam-fi 1.1 //
1497     //--------------------------------------
1498     /**
1499 pam-fi 1.55 * Sort physical (tracker) tracks.
1500 pam-fi 1.3 * @param how String to set the sorting cryterium (es: "CAL" or "TRK+CAL+TOF" ecc...).
1501     * Sorting cryteria:
1502 pam-fi 1.81 * TRK: lower chi**2
1503 pam-fi 1.3 * 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).
1505 pam-fi 1.55 * S1: (ask Emiliano)
1506     * S2: (ask Emiliano)
1507     * S3: (ask Emiliano)
1508 pam-fi 1.81 * GP: more GP hits
1509 pam-fi 1.3 * The default sorting cryterium is "TOF+CAL".
1510 pam-fi 1.81 *
1511 pam-fi 1.3 * 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 pam-fi 1.1 */
1513 pam-fi 1.81 void PamLevel2::SortTracks() {
1514 pam-fi 1.55
1515 pam-fi 1.81 //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;
1524    
1525     // cout <<" PamLevel2::SortTracks(TString how) "<<endl;
1526     if (!trk2_obj) {
1527     cout << "void PamLevel2::SortTracks(): TrkLevel2 not loaded !!!";
1528     return;
1529     };
1530     //Save current Object count
1531     Int_t ObjectNumber = TProcessID::GetObjectCount();
1532 pam-fi 1.1
1533 pam-fi 1.81 // create TCloneArrays to store tracks and its images
1534 pam-fi 1.110 // 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    
1546    
1547 pam-fi 1.81 TClonesArray &ttsorted = *tsorted;
1548     TClonesArray &ttimage = *timage;
1549    
1550 pam-ts 1.95
1551    
1552 pam-fi 1.81 //--------------------------------------------------
1553     // retrieve sorting method
1554     //--------------------------------------------------
1555     Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
1556     Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
1557     Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
1558     Bool_t use_S1 = how.Contains("S1", TString::kIgnoreCase);
1559     Bool_t use_S2 = how.Contains("S2", TString::kIgnoreCase);
1560     Bool_t use_S3 = how.Contains("S3", TString::kIgnoreCase);
1561     Bool_t use_GP = how.Contains("GP", TString::kIgnoreCase);
1562    
1563     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;
1583     };
1584 pam-fi 1.55
1585 pam-fi 1.81 // cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;
1586 pam-fi 1.13
1587    
1588 pam-fi 1.81 //--------------------------------------------------
1589     // loop over "physical" tracks sorted by the tracker
1590     //--------------------------------------------------
1591     for (Int_t i = 0; i < trk2_obj->TrkLevel2::GetNTracks(); i++) {
1592    
1593     TrkTrack *ts = 0;
1594     CaloTrkVar *cs = 0;
1595     ToFTrkVar *os = 0;
1596     OrbitalInfoTrkVar *rs = 0;
1597    
1598     // get tracker tracks
1599     TrkTrack *tp = trk2_obj->TrkLevel2::GetTrack(i); //tracker
1600     CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());
1601     ToFTrkVar *op = GetToFStoredTrack(tp->GetSeqNo());
1602     OrbitalInfoTrkVar *rp = GetOrbitalInfoStoredTrack(tp->GetSeqNo());
1603    
1604     TrkTrack *ti = 0; //tracker (image)
1605     CaloTrkVar *ci = 0;
1606     ToFTrkVar *oi = 0;
1607     OrbitalInfoTrkVar *ri = 0;
1608     // cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
1609     // if track has an image, check image selection
1610    
1611     Int_t tp_score = 0; //main track sorted by the tracker
1612     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());
1620     oi = GetToFStoredTrack(ti->GetSeqNo());
1621     ri = GetOrbitalInfoStoredTrack(ti->GetSeqNo());
1622    
1623     // cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
1624    
1625     //assign starting scores
1626     tp_score = 0; //main track sorted by the tracker
1627     ti_score = 0; //image track
1628    
1629     // -----------------------------------------------------------------------------------------
1630     // *****************************************************************************************
1631     // -----------------------------------------------------------------------------------------
1632     // calorimeter check
1633     // -----------------------------------------------------------------------------------------
1634     if (use_CAL && !calo2_obj) {
1635     cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but CaloLevel2 not loaded !!!";
1636     return;
1637     };
1638     if (use_CAL && !cp && ci) {
1639     ti_score++;
1640     toti_score++;
1641     };
1642     if (use_CAL && cp && !ci) {
1643     tp_score++;
1644     totp_score++;
1645     };
1646     if (use_CAL && cp && ci && true) {
1647    
1648     if (cp->npresh > ci->npresh && true) {
1649     tp_score++;
1650     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;
1658    
1659     };
1660     // -----------------------------------------------------------------------------------------
1661     // *****************************************************************************************
1662     // -----------------------------------------------------------------------------------------
1663     // TOF check
1664     // -----------------------------------------------------------------------------------------
1665     // check the number of hit pmts along the track
1666     // on S12 S21 and S32, where paddles are parallel to Y axis
1667 pam-ts 1.95 if ((use_TOF || use_S1 || use_S2 || use_S3) && !tof2_obj) {
1668 pam-fi 1.81 cout << "void PamLevel2::SortTracks(): howtosort= " << how << " but ToFLevel2 not loaded !!!";
1669     return;
1670     };
1671     //
1672     if ((use_TOF || use_S1 || use_S2 || use_S3) && !op && oi) {
1673     ti_score++;
1674     toti_score++;
1675     };
1676     if ((use_TOF || use_S1 || use_S2 || use_S3) && op && !oi) {
1677     tp_score++;
1678     totp_score++;
1679     };
1680     if ((use_TOF || use_S1 || use_S2 || use_S3) && op && oi) {
1681     //
1682     Float_t sen = 0.;
1683     for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1684 pam-ts 1.95 Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1685 pam-fi 1.81 if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
1686     sen += (op->dedx).At(ih);
1687     };
1688     for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1689 pam-ts 1.95 Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1690 pam-fi 1.81 if (pl == 2 || pl == 3 || pl == 4 || pl == 5)
1691     sen += (oi->dedx).At(ih);
1692     };
1693     //
1694     if (sen >= sortthr && false) { // temporary disabled NUCLEI special algorithm since the new one should work for every particle (to be checked!)
1695     //printf(" IS A NUCLEUS! en = %f \n",sen);
1696     //
1697     // is a nucleus use a different algorithm
1698     //
1699     Int_t nz = 6;
1700     Float_t zin[6]; // << define TOF z-coordinates
1701     for (Int_t ip = 0; ip < nz; ip++)
1702 pam-ts 1.95 zin[ip] = tof2_obj->ToFLevel2::GetZTOF(tof2_obj->ToFLevel2::GetToFPlaneID(ip)); // << read ToF plane z-coordinates
1703 pam-fi 1.81 Trajectory *tr = new Trajectory(nz, zin);
1704     //
1705     Int_t nphit_p = 0;
1706     Int_t nphit_i = 0;
1707     Float_t enhit_p = 0.;
1708     Float_t enhit_i = 0.;
1709     //
1710     for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1711 pam-ts 1.95 Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1712 pam-fi 1.81 if (pl == 1 || pl == 2 || pl == 5) {
1713     nphit_p++;
1714     enhit_p += (op->dedx).At(ih);
1715     };
1716     };
1717     //
1718     tp->DoTrack2(tr);
1719     //
1720     if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
1721     for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1722 pam-ts 1.95 Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1723 pam-fi 1.81 if (pl == 0) {
1724     nphit_p++;
1725     enhit_p += (op->dedx).At(ih);
1726     };
1727     };
1728     };
1729     if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
1730     for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1731 pam-ts 1.95 Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1732 pam-fi 1.81 if (pl == 3) {
1733     nphit_p++;
1734     enhit_p += (op->dedx).At(ih);
1735     };
1736     };
1737     };
1738     if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
1739     for (Int_t ih = 0; ih < op->npmtadc; ih++) {
1740 pam-ts 1.95 Int_t pl = tof2_obj->GetPlaneIndex((op->pmtadc).At(ih));
1741 pam-fi 1.81 if (pl == 4) {
1742     nphit_p++;
1743     enhit_p += (op->dedx).At(ih);
1744     };
1745     };
1746     };
1747    
1748     for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1749 pam-ts 1.95 Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1750 pam-fi 1.81 if (pl == 1 || pl == 2 || pl == 5) {
1751     nphit_i++;
1752     enhit_i += (op->dedx).At(ih);
1753     };
1754     };
1755     //
1756     ti->DoTrack2(tr);
1757     //
1758     if (fabs(tr->y[0] - oi->ytofpos[0]) < 2.) {
1759     for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1760 pam-ts 1.95 Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1761 pam-fi 1.81 if (pl == 0) {
1762     nphit_i++;
1763     enhit_i += (op->dedx).At(ih);
1764     };
1765     };
1766     };
1767     if (fabs(tr->y[3] - oi->ytofpos[1]) < 2.) {
1768     for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1769 pam-ts 1.95 Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1770 pam-fi 1.81 if (pl == 3) {
1771     nphit_i++;
1772     enhit_i += (op->dedx).At(ih);
1773     };
1774     };
1775     };
1776     if (fabs(tr->y[4] - oi->ytofpos[2]) < 2.) {
1777     for (Int_t ih = 0; ih < oi->npmtadc; ih++) {
1778 pam-ts 1.95 Int_t pl = tof2_obj->GetPlaneIndex((oi->pmtadc).At(ih));
1779 pam-fi 1.81 if (pl == 4) {
1780     nphit_i++;
1781     enhit_i += (op->dedx).At(ih);
1782     };
1783     };
1784     };
1785    
1786     if ((use_TOF || use_S1 || use_S2 || use_S3) && (nphit_p + nphit_i) != 0 && true) {
1787    
1788     // 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     // printf(" score p %i score i %i \n",tp_score,ti_score);
1790     // if( enhit_p > enhit_i ) tp_score++;
1791     // if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
1792     if (nphit_p > nphit_i)
1793     tp_score++;
1794     if (nphit_p < nphit_i)
1795     ti_score++;
1796     if (nphit_p == nphit_i) {
1797     if (enhit_p > enhit_i)
1798     tp_score++;
1799     else
1800     ti_score++;
1801     };
1802     // printf(" dopo score p %i score i %i \n",tp_score,ti_score);
1803     };
1804     delete tr;
1805     //
1806     }
1807     else {
1808     // -------------
1809     // NOT a NUCLEUS
1810     // -------------
1811     //printf(" NOT a NUCLEUS! en = %f \n",sen);
1812    
1813     Int_t nphit_p = 0;
1814     Int_t nphit_i = 0;
1815    
1816     /* cout << "track: npmtadc "<< op->npmtadc << endl;
1817     cout << "track: npmttdc "<< op->npmttdc << endl;
1818     cout << "image: npmtadc "<< oi->npmtadc << endl;
1819     cout << "image: npmttdc "<< oi->npmttdc << endl;*/
1820    
1821     // for (Int_t ih=0; ih < op->npmtadc; ih++){
1822 pam-ts 1.95 // Int_t pl = tof2_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
1823 pam-fi 1.81 // if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
1824     // };
1825    
1826     // for (Int_t ih=0; ih < oi->npmtadc; ih++){
1827 pam-ts 1.95 // Int_t pl = tof2_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
1828 pam-fi 1.81 // if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
1829     // };
1830     // --- 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 pam-ts 1.95 Int_t pl = tof2_obj->GetPlaneIndex((op->pmttdc).At(ih));
1834 pam-fi 1.81 // if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
1835     if ((use_S1 && (pl == 0 || pl == 1)) || (use_S2 && (pl == 2 || pl == 3))
1836     || (use_S3 && (pl == 4 || pl == 5))) {
1837     if ((op->tdcflag).At(ih) == 0)
1838     nphit_p++;
1839     };
1840     };
1841    
1842     for (Int_t ih = 0; ih < oi->npmttdc; ih++) {
1843 pam-ts 1.95 Int_t pl = tof2_obj->GetPlaneIndex((oi->pmttdc).At(ih));
1844 pam-fi 1.81 // 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;
1869    
1870    
1871     // -----------------------------------------------------------------------------------------
1872     // *****************************************************************************************
1873     // -----------------------------------------------------------------------------------------
1874     // tracker check
1875     // -----------------------------------------------------------------------------------------
1876     // chi**2 difference is not always large enough to distinguish among
1877     // the real track and its image.
1878     // Tracker check will be applied always when calorimeter and tof information is ambiguous.
1879     // *** MODIFIED ON AUGUST 2007 ***
1880     if (use_TRK) {
1881     // if( tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
1882     // else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
1883    
1884     // CHECK 1 : number of points along X
1885     if (tp->GetNX() >= ti->GetNX()) {
1886     tp_score++;
1887     totp_score++;
1888     };
1889     if (tp->GetNX() <= ti->GetNX()) {
1890     ti_score++;
1891     toti_score++;
1892     };
1893     // CHECK 2 : number of points along Y
1894     if (tp->GetNY() >= ti->GetNY()) {
1895     tp_score++;
1896     totp_score++;
1897     };
1898     if (tp->GetNY() <= ti->GetNY()) {
1899     ti_score++;
1900     toti_score++;
1901     };
1902    
1903     // 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....
1938     // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
1939     if (tp_score > ti_score) {
1940    
1941     }
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     toti_score = totp_score;
1961     totp_score = totis;
1962    
1963     }
1964     else {
1965    
1966     // cout << "Warning - track image ambiguity not solved" << endl;
1967    
1968     };
1969    
1970     }
1971     else {
1972     totp_score = 1;
1973     toti_score = 0;
1974    
1975     // ts = tp;
1976     // cs = cp;
1977     // os = op;
1978     };
1979    
1980     // cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
1981     // sorted_tracks->Add(ts);//save the track in the sorted array
1982     // sorted_tracks.Add(ts);//save the track in the sorted array
1983     // sorted_tracks.Add(tp);//save the track in the sorted array
1984     // cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
1985     // cout<<"o "<<tp<<endl;
1986     // cout<<"o "<<cp<<endl;
1987     // cout<<"o "<<op<<endl;
1988    
1989 pam-fi 1.111 // cout <<"old p:"<< tp<<"--"<<cp<<"--"<<op<<"--"<<rp<<"--"<< endl;
1990     // cout <<"old i:"<< ti<<"--"<<ci<<"--"<<oi<<"--"<<ri<<"--"<< endl;
1991    
1992 pam-fi 1.81 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 pam-fi 1.41
2001 pam-fi 1.81 if (tsorted->GetEntries() != trk2_obj->GetNTracks()) {
2002     cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() " << tsorted->GetEntries()
2003     << " != trk2_obj->GetNTracks() = " << trk2_obj->GetNTracks() << endl;
2004     tsorted->Delete();
2005     tsorted = 0;
2006     timage->Delete();
2007     timage = 0;
2008     }
2009 pam-fi 1.55
2010 pam-fi 1.81 //Restore Object count
2011     //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.
2013     TProcessID::SetObjectCount(ObjectNumber);
2014    
2015     //Everything went fine so the current event can be tagged as sorted
2016     issorted = true;
2017     lastsorted = GetReadEntry();
2018 pam-fi 1.9
2019 pam-fi 1.111 // cout <<" SortTracks() -- end"<<endl;
2020    
2021 pam-fi 1.81 }
2022     ;
2023 pam-ts 1.95 //
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 pam-fi 1.97
2089     // cout << " PamLevel2::SortTracksNew() --- Clear TClonesArray objects"<<endl;
2090 pam-ts 1.95
2091     // main tracks from standard alg
2092 pam-fi 1.97 // 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 pam-ts 1.95
2111     //-----------------------------------------------------------
2112     // create/reset TCloneArrays to store tracks and their images
2113     //-----------------------------------------------------------
2114     if(NUC){
2115 pam-fi 1.97
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 pam-ts 1.95 // main tracks from standard alg
2125 pam-fi 1.97 // 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 pam-ts 1.95 }
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 pam-fi 1.111 trk2 = (TRK2 ? trk2_obj: NULL);;
2185     calo2 = (CAL2 ? calo2_obj: NULL);;
2186     tof2 = (TOF ?tof2_obj: NULL);;
2187     orb2 = (ORB ? orb2_obj: NULL);;
2188 pam-ts 1.95
2189 pam-fi 1.111 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 pam-ts 1.95
2194     }else if (doit == 1){
2195    
2196     if(!NUC)break;
2197    
2198    
2199 pam-fi 1.111 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 pam-ts 1.95
2204 pam-fi 1.111 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 pam-ts 1.95
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 pam-fi 1.111 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 pam-fi 1.112 // cout << "ORB="<<ORB<<" rp="<<rp<<endl;
2246 pam-ts 1.95
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 pam-fi 1.111 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 pam-ts 1.95
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 pam-fi 1.111 // cout <<"p:"<< tp<<"--"<<cp<<"--"<<op<<"--"<<rp<<"--"<< endl;
2633     // cout <<"i:"<< ti<<"--"<<ci<<"--"<<oi<<"--"<<ri<<"--"<< endl;
2634    
2635    
2636    
2637 pam-ts 1.95 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 pam-fi 1.111 // cout << "text "<<orbext<<endl;
2661 pam-ts 1.95
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 pam-fi 1.111 // 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 pam-ts 1.95
2681 pam-fi 1.111 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 pam-ts 1.95 }
2690     }
2691    
2692 pam-fi 1.111 // cout <<" SortTracksNew() -- end"<<endl;
2693 pam-ts 1.95
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 pam-fi 1.1 //--------------------------------------
2714     //
2715     //
2716     //--------------------------------------
2717     /**
2718 pam-fi 1.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.
2720 pam-ts 1.96 */
2721 pam-ts 1.95
2722 pam-fi 1.13 // TRefArray *PamLevel2::GetTracks(){
2723    
2724     // // *-*-*-*-*-*-*-*-*-*-*-*-*
2725     // SortTracks("+CAL+TOF");
2726     // // *-*-*-*-*-*-*-*-*-*-*-*-*
2727    
2728     // // return sorted_tracks;
2729     // return &sorted_tracks;
2730 pam-fi 1.81
2731 pam-fi 1.13 // };
2732 pam-fi 1.81
2733 pam-fi 1.8
2734 pam-fi 1.2 //--------------------------------------
2735 pam-fi 1.55 //
2736     //
2737 pam-fi 1.2 //--------------------------------------
2738 pam-ts 1.96
2739 pam-fi 1.2 /**
2740 pam-fi 1.81 * Retrieves the it-th Pamela "physical" track.
2741     * It override TrkLevel2::GetTrack(int it).
2742 pam-fi 1.1 * @param it Track number, ranging from 0 to GetNTracks().
2743     */
2744 pam-ts 1.96 PamTrack *PamLevel2::GetTrackOld(int it) {
2745 pam-fi 1.81
2746 pam-ts 1.95 PamTrack *track = NULL;
2747 pam-fi 1.81
2748     // *-*-*-*-*-*-*-*-*-*-*-*-*
2749     SortTracks();
2750     // *-*-*-*-*-*-*-*-*-*-*-*-*
2751     if (!tsorted)
2752 pam-ts 1.95 return track;
2753 pam-fi 1.110 // if (!tsorted->GetEntries())
2754     // return track;
2755     if (tsorted->GetEntries()==0)
2756 pam-ts 1.95 return track;
2757 pam-fi 1.81
2758    
2759 pam-fi 1.110 if (
2760     tsorted &&
2761     it >= 0 &&
2762     it < trk2_obj->TrkLevel2::GetNTracks() &&
2763     it < tsorted->GetEntries() &&
2764     true) {
2765     track = (PamTrack*)((*tsorted)[it]);
2766 pam-fi 1.81 }
2767     else {
2768 pam-ts 1.96 cout << "PamLevel2::GetTrackOld(int) : tracker track SeqNo " << it << " does not exist (GetNTracks() = "
2769 pam-fi 1.81 << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
2770     };
2771 pam-fi 1.13
2772 pam-fi 1.81 return track;
2773 pam-fi 1.13
2774 pam-ts 1.95 };
2775    
2776     //PamTrack *PamLevel2::GetTrack(int it) { return GetTrack(it,trkAlg); };
2777    
2778     /**
2779     * Retrieves the it-th Pamela "physical" track.
2780     * It override TrkLevel2::GetTrack(int it).
2781     * @param it Track number, ranging from 0 to GetNTracks().
2782     * @param alg Algorythm, see SetTrakingAlgorythm(char *alg) for explanation.
2783     */
2784 pam-ts 1.96 PamTrack *PamLevel2::GetTrack(int it, const char* alg) {
2785 pam-ts 1.95
2786     TString s(alg);
2787 pam-ts 1.96 if(!s.CompareTo("") ||!s.CompareTo("STD") )return GetTrackOld(it); //old algorythm
2788 pam-ts 1.95
2789 pam-fi 1.113
2790 pam-ts 1.95
2791     SortTracksNew();
2792     // >> fill tsorted, timage and text
2793    
2794     if ( ( !s.Contains("EXTF", TString::kIgnoreCase) || !EXT )){ //not forced exteded-alg requested (or exteded-data missing)
2795    
2796     if( s.Contains("NUC")){
2797     if(
2798     tsorted_nuc &&
2799     it < tsorted_nuc->GetEntries() && //enough tracks found
2800     it >= 0 && //valid index
2801     true) return (PamTrack*)((*tsorted_nuc)[it]); //ok return the track
2802 pamela 1.114 }else if( s.Contains("HE" ) ){
2803    
2804     //-------------------------------- mixed NUC+STD algorythm
2805     PamTrack* trnuc = NULL;
2806     PamTrack* trstd = NULL;
2807     if( tsorted_nuc &&
2808     it < tsorted_nuc->GetEntries() && //enough NUC tracks found
2809     it >= 0 &&
2810     true)trnuc = (PamTrack*)((*tsorted_nuc)[it]);
2811     if( tsorted &&
2812     it < tsorted->GetEntries() && //enough NUC tracks found
2813     it >= 0 &&
2814     true)trstd = (PamTrack*)((*tsorted)[it]);
2815    
2816     if(!trnuc && trstd)return trstd;
2817     if( trnuc && !trstd)return trnuc;
2818     if( trnuc && trnuc->GetExtTrack()->chi2 < trstd->GetExtTrack()->chi2 )return trnuc;
2819     return trstd;
2820    
2821 pam-ts 1.95 }else{
2822     if(
2823     tsorted &&
2824     it < tsorted->GetEntries() && //enough tracks found
2825     it >= 0 && //valid index
2826     true )return (PamTrack*)((*tsorted)[it]); //ok return the track
2827     }
2828    
2829     }
2830    
2831    
2832     /////////////////////////////////////////////////////////////////////////
2833     /// if requested get track from extended algorythm output
2834     /////////////////////////////////////////////////////////////////////////
2835    
2836     if(s.Contains("EXT", TString::kIgnoreCase) && EXT){//if exteded-alg requested
2837    
2838     if(s.Contains("NUC")){
2839     if(
2840     text_nuc &&
2841     it < text_nuc->GetEntries() && //enough tracks found
2842     it >= 0 && //valid index
2843     true) return (PamTrack*)((*text_nuc)[it]);
2844 pamela 1.114
2845     }else if( s.Contains("HE" ) ){
2846    
2847     //-------------------------------- mixed NUC+STD algorythm
2848     PamTrack* trnuc = NULL;
2849     PamTrack* trstd = NULL;
2850     if( text_nuc &&
2851     it < text_nuc->GetEntries() && //enough NUC tracks found
2852     it >= 0 &&
2853     true)trnuc = (PamTrack*)((*text_nuc)[it]);
2854     if( text &&
2855     it < text->GetEntries() && //enough NUC tracks found
2856     it >= 0 &&
2857     true)trstd = (PamTrack*)((*text)[it]);
2858    
2859     if(!trnuc && trstd)return trstd;
2860     if( trnuc && !trstd)return trnuc;
2861     if( trnuc && trnuc->GetExtTrack()->chi2 < trstd->GetExtTrack()->chi2 )return trnuc;
2862     return trstd;
2863    
2864 pam-ts 1.95 }else{
2865     if(
2866     text &&
2867     it < text->GetEntries() && //enough tracks found
2868     it >= 0 && //valid index
2869     true) return (PamTrack*)((*text)[it]);
2870     }
2871    
2872     };
2873    
2874    
2875 pam-ts 1.96 cout << "PamTrack *PamLevel2::GetTrack("<<it<<","<<alg<<") -- wrong track number or unrecognised algorithm"<<endl;
2876 pam-ts 1.95
2877     return NULL;
2878    
2879     }
2880     ;
2881     TClonesArray *PamLevel2::GetTracks() {
2882    
2883     // *-*-*-*-*-*-*-*-*-*-*-*-*
2884     SortTracks();
2885     // *-*-*-*-*-*-*-*-*-*-*-*-*
2886    
2887     return tsorted;
2888    
2889 pam-fi 1.81 }
2890     ;
2891 pam-ts 1.96 Int_t PamLevel2::GetNTracks(const char* alg) {
2892 pam-ts 1.95
2893    
2894     // cout << " trk_nuc_obj->GetEntries() "<<trk_nuc_obj->GetEntries()<<" trk2_nuc_obj->GetNTracks() "<<trk2_nuc_obj->GetNTracks()<<endl;
2895    
2896     TString s(alg);
2897    
2898     if(!s.CompareTo("") || !s.CompareTo("STD"))return trk2_obj->TrkLevel2::GetNTracks(); //standard algorythm
2899    
2900     if(s.Contains("EXTF", TString::kIgnoreCase) && EXT){
2901     if(s.Contains("NUC", TString::kIgnoreCase) && NUC)return trk_ext_nuc_obj->GetEntries();//ok
2902     return trk_ext_obj->GetEntries();//ok
2903     }
2904     if( s.Contains("EXT", TString::kIgnoreCase) && EXT) {
2905 pamela 1.114
2906     if(s.Contains("HE", TString::kIgnoreCase) && NUC){
2907     int nnuc = (trk2_nuc_obj->TrkLevel2::GetNTracks() ? trk2_nuc_obj->TrkLevel2::GetNTracks() : trk_ext_nuc_obj->GetEntries() );
2908     int next = (trk2_obj->TrkLevel2::GetNTracks() ? trk2_obj->TrkLevel2::GetNTracks() : trk_ext_obj->GetEntries() );
2909     return max(nnuc,next);
2910     }
2911 pam-ts 1.95 if(s.Contains("NUC", TString::kIgnoreCase) && NUC)
2912     return (trk2_nuc_obj->TrkLevel2::GetNTracks() ? trk2_nuc_obj->TrkLevel2::GetNTracks() : trk_ext_nuc_obj->GetEntries() );
2913 pamela 1.114
2914    
2915 pam-ts 1.95 return (trk2_obj->TrkLevel2::GetNTracks() ? trk2_obj->TrkLevel2::GetNTracks() : trk_ext_obj->GetEntries() );
2916     }
2917 pamela 1.114
2918     if(s.Contains("HE", TString::kIgnoreCase) && NUC )
2919     return max(trk2_obj->TrkLevel2::GetNTracks(), trk2_nuc_obj->TrkLevel2::GetNTracks());
2920    
2921 pam-ts 1.95 if(s.Contains("NUC", TString::kIgnoreCase) && NUC )
2922     return trk2_nuc_obj->TrkLevel2::GetNTracks();
2923 pamela 1.114
2924 pam-ts 1.95
2925 pam-ts 1.96 cout << "Int_t PamLevel2::GetNTracks("<<alg<<") -- unrecognised algorithm"<<endl;
2926    
2927 pam-ts 1.95 return 0;
2928    
2929     }
2930    
2931 pam-fi 1.3
2932 pam-fi 1.1 //--------------------------------------
2933     //
2934     //
2935     //--------------------------------------
2936     /**
2937 pam-fi 1.81 * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().
2938 pam-fi 1.1 * @param it Track number, ranging from 0 to GetNTracks().
2939     */
2940 pam-ts 1.96 PamTrack *PamLevel2::GetTrackImageOld(int it) {
2941 pam-fi 1.1
2942 pam-fi 1.81 // *-*-*-*-*-*-*-*-*-*-*-*-*
2943     SortTracks();
2944     // *-*-*-*-*-*-*-*-*-*-*-*-*
2945     if (!timage)
2946     return 0;
2947     if (!timage->GetEntries())
2948     return 0;
2949    
2950     PamTrack *image = 0;
2951    
2952     if (it >= 0 && it < trk2_obj->TrkLevel2::GetNTracks()) {
2953     TClonesArray &t = *(tsorted);
2954     PamTrack *temp = (PamTrack*) t[it];
2955     if (temp->GetTrkTrack()->HasImage()) {
2956     TClonesArray & t = *(timage);
2957     image = (PamTrack*) t[it];
2958     }
2959     else {
2960     // cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;
2961     };
2962     }
2963     else {
2964 pam-ts 1.96 cout << "PamLevel2::GetTrackImageOld(int) : Tracker track SeqNo " << it << " does not exist (GetNTracks() = "
2965 pam-fi 1.81 << trk2_obj->TrkLevel2::GetNTracks() << ")" << endl;
2966     };
2967    
2968     return image;
2969 pam-fi 1.1 }
2970 pam-ts 1.95 /**
2971     * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().
2972     * @param it Track number, ranging from 0 to GetNTracks().
2973     * @param alg Algorythm, see SetTrakingAlgorythm(char *alg) for explanation.
2974     */
2975 pam-ts 1.96 PamTrack *PamLevel2::GetTrackImage(int it, const char* alg) {
2976 pam-ts 1.95
2977     TString s(alg);
2978 pam-ts 1.96 if(!s.CompareTo("") || !s.CompareTo("STD"))return GetTrackImageOld(it); //old algorythm
2979 pam-ts 1.95
2980    
2981     SortTracksNew();
2982     // >> fill tsorted, timage and text
2983    
2984     if ( ( !s.Contains("EXTF", TString::kIgnoreCase) || !EXT )){ //not forced exteded-alg requested (or exteded-data missing)
2985    
2986     if( s.Contains("NUC")){
2987     if(
2988     tsorted_nuc &&
2989     it < tsorted_nuc->GetEntries() && //enough tracks found
2990     it >= 0 && //valid index
2991     true){
2992     TClonesArray &t = *(tsorted_nuc);
2993     PamTrack *temp = (PamTrack*) t[it];
2994     if (temp->GetTrkTrack()->HasImage()) {
2995     return (PamTrack*)((*timage_nuc)[it]); //ok return the track
2996 pam-ts 1.96 }else{
2997     return NULL;
2998 pam-ts 1.95 }
2999    
3000     }
3001     }else{
3002     if(
3003     tsorted &&
3004     it < tsorted->GetEntries() && //enough tracks found
3005     it >= 0 && //valid index
3006     true ){
3007     TClonesArray &t = *(tsorted);
3008     PamTrack *temp = (PamTrack*) t[it];
3009     if (temp->GetTrkTrack()->HasImage()) {
3010     return (PamTrack*)((*timage)[it]); //ok return the track
3011 pam-ts 1.96 }else{
3012     return NULL;
3013 pam-ts 1.95 }
3014     }
3015     }
3016    
3017     }
3018 pam-ts 1.96
3019 pam-fi 1.110 // cout << "PamTrack *PamLevel2::GetTrackImage("<<it<<","<<alg<<") -- wrong track number or unrecognised algorithm"<<endl;
3020 pam-ts 1.95
3021     return NULL;
3022    
3023     }
3024     ;
3025 pam-fi 1.1
3026     //--------------------------------------
3027     //
3028     //
3029     //--------------------------------------
3030     /**
3031 pam-fi 1.2 * Get the Pamela detector trees in a single file and make them friends.
3032     * @param f TFile pointer
3033     * @param detlist String to select trees to be included
3034     * @return Pointer to a TTree
3035 pam-fi 1.1 */
3036 pam-fi 1.81 TTree *PamLevel2::GetPamTree(TFile *f, TString detlist) {
3037    
3038     if (detlist.Contains("+AUTO", TString::kIgnoreCase)) {
3039     cout << "+AUTO" << endl;
3040     GetWhichTrees(f);
3041     };
3042     SetWhichTrees(detlist);
3043    
3044     if (pam_tree) {
3045     printf("WARNING: TTree *PamLevel2::GetPamTree(TFile *fl, TString detlist) -- pam_tree already exists!\n ");
3046     return pam_tree;
3047     };
3048     //
3049 pam-fi 1.4
3050 pam-fi 1.81 cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte " << endl;
3051    
3052     // SetWhichTrees(detlist);
3053 pam-fi 1.52
3054 pam-fi 1.81 TTree *Trout = 0;
3055 mocchiut 1.35
3056 pam-fi 1.81 TString fname = f->GetName();
3057     if (!CheckLevel2File(fname))
3058     return NULL;
3059 pam-fi 1.20
3060 pam-fi 1.81 // UInt_t *found=0;
3061 pam-fi 1.9
3062 pam-fi 1.81 cout << "GetPamTree(TFile*,TString): detector list --> ";
3063     if (TRK1)
3064     cout << "TRK1 ";
3065     if (TRK2)
3066     cout << "TRK2 ";
3067     if (TRKh)
3068     cout << "TRKH ";
3069     if (CAL1)
3070     cout << "CAL1 ";
3071     if (CAL2)
3072     cout << "CAL2 ";
3073     if (TOF)
3074     cout << "TOF ";
3075     if (TRG)
3076     cout << "TRG ";
3077     if (AC)
3078     cout << "AC ";
3079     if (ND)
3080     cout << "ND ";
3081     if (S4)
3082     cout << "S4 ";
3083     if (ORB)
3084     cout << "ORB ";
3085     if (GP)
3086     cout << "GP ";
3087     cout << endl;
3088     if (SELLI && SELLI != 2)
3089     cout << ">>> Found selection-list <<<" << endl; //EMILIANO
3090    
3091     f->cd();
3092    
3093     // Tracker
3094     TTree *T = (TTree*) f->Get("Tracker");
3095     if (T && (TRK2 || TRK1 || TRKh)) {
3096     if (TRK2)
3097     T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
3098     // else T->SetBranchStatus("TrkLevel2",0,found);
3099     if (TRK2)
3100     cout << "Tracker : set branch address TrkLevel2" << endl;
3101     if (TRK1)
3102     T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
3103     // else T->SetBranchStatus("TrkLevel1",0,found);
3104     if (TRK1)
3105     cout << "Tracker : set branch address TrkLevel1" << endl;
3106     if (TRKh)
3107     T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
3108     // else T->SetBranchStatus("TrkHough",0,found);
3109     if (TRKh)
3110     cout << "Tracker : set branch address TrkHough" << endl;
3111     if (!Trout)
3112     Trout = T;
3113     else
3114 pam-ts 1.95 Trout->AddFriend(T);
3115 pam-fi 1.81 }
3116     else {
3117     cout << "Tracker : missing tree" << endl;
3118     };
3119     // Calorimeter
3120     TTree *C = (TTree*) f->Get("Calorimeter");
3121     if (C && (CAL2 || CAL1)) {
3122     if (CAL2)
3123     C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
3124     // else C->SetBranchStatus("CaloLevel2",0,found);
3125     if (CAL2)
3126     cout << "Calorimeter : set branch address CaloLevel2" << endl;
3127     if (CAL1)
3128     C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
3129     // else C->SetBranchStatus("CaloLevel1",0,found);
3130     if (CAL1)
3131     cout << "Calorimeter : set branch address CaloLevel1" << endl;
3132     if (!Trout)
3133     Trout = C;
3134     else
3135     Trout->AddFriend(C);
3136     }
3137     else {
3138     cout << "Calorimeter : missing tree" << endl;
3139     };
3140 pam-fi 1.9
3141 pam-fi 1.81 // ToF
3142     TTree *O = (TTree*) f->Get("ToF");
3143     if (O && TOF) {
3144     O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
3145     cout << "ToF : set branch address ToFLevel2" << endl;
3146     if (!Trout)
3147     Trout = O;
3148     else
3149     Trout->AddFriend(O);
3150     }
3151     else {
3152     cout << "ToF : missing tree" << endl;
3153     };
3154     // Trigger
3155     TTree *R = (TTree*) f->Get("Trigger");
3156     if (R && TRG) {
3157     R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
3158     cout << "Trigger : set branch address TrigLevel2" << endl;
3159     if (!Trout)
3160     Trout = R;
3161     else
3162     Trout->AddFriend(R);
3163     }
3164     else {
3165     cout << "Trigger : missing tree" << endl;
3166     };
3167     // S4
3168     TTree *S = (TTree*) f->Get("S4");
3169     if (S && S4) {
3170     S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
3171     cout << "S4 : set branch address S4Level2" << endl;
3172     if (!Trout)
3173     Trout = S;
3174     else
3175     Trout->AddFriend(S);
3176     }
3177     else {
3178     cout << "S4 : missing tree" << endl;
3179     };
3180     // Neutron Detector
3181     TTree *N = (TTree*) f->Get("NeutronD");
3182     if (N && ND) {
3183     N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
3184     cout << "NeutronD : set branch address NDLevel2" << endl;
3185     if (!Trout)
3186     Trout = N;
3187     else
3188     Trout->AddFriend(N);
3189     }
3190     else {
3191     cout << "NeutronD : missing tree" << endl;
3192     };
3193     // Anticounters
3194     TTree *A = (TTree*) f->Get("Anticounter");
3195     if (A && AC) {
3196     A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
3197     cout << "Anticounter : set branch address AcLevel2" << endl;
3198     if (!Trout)
3199     Trout = A;
3200     else
3201     Trout->AddFriend(A);
3202     }
3203     else {
3204     cout << "Anticounter : missing tree" << endl;
3205     };
3206     // Orbital Info
3207     TTree *B = (TTree*) f->Get("OrbitalInfo");
3208     if (B && ORB) {
3209     B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
3210     cout << "OrbitalInfo : set branch address OrbitalInfo" << endl;
3211     if (!Trout)
3212     Trout = B;
3213     else
3214     Trout->AddFriend(B);
3215     }
3216     else {
3217     cout << "OrbitalInfo : missing tree" << endl;
3218     };
3219 pam-fi 1.9
3220 pam-fi 1.81 // GPamela
3221     TTree *G = (TTree*) f->Get("h20");
3222     if (G && GP) {
3223     if (!gp_obj)
3224     gp_obj = new GPamela();
3225     // ------------------------------------
3226     // ATTENZIONE!!!
3227     // non so per quale arcano motivo,
3228     // per l'albero di gpamela il branch address lo devo settare
3229     // DOPO aver fatto friend
3230     // FGRRRVZZZZUTSALKJMSLKJ!!!
3231     // ------------------------------------
3232     // gp_obj->SetBranchAddress(G); //ho dovuto fare in maniera diversa dagli altri
3233     // cout << "h20 : set branch address GPamela "<<endl;
3234     if (!Trout)
3235     Trout = G;
3236     else
3237     Trout->AddFriend(G);
3238     }
3239     else {
3240     // cout << "h20 : missing tree"<<endl;
3241     };
3242 pam-fi 1.12
3243 pam-fi 1.81 TTree *L = (TTree*) f->Get("SelectionList");
3244     if (L && SELLI == 1) {
3245     cout << " TTree *PamLevel2::GetPamTree(TFile, TString) >>> SelectionList not implemented!!!!" << endl;
3246     sel_tree = 0;
3247     }
3248     else {
3249     cout << "SelectionList : missing tree" << endl;
3250     };
3251 pam-fi 1.20
3252 pam-fi 1.81 cout << endl << " Number of entries: " << Trout->GetEntries() << endl << endl;
3253 pam-fi 1.52
3254 pam-fi 1.81 // ------------------------------------
3255     // ATTENZIONE!!!
3256     // non so per quale arcano motivo,
3257     // per l'albero di gpamela il branch address lo devo settare
3258     // DOPO aver fatto friend
3259     // FGRRRVZZZZUTSALKJMSLKJ!!!
3260     // ------------------------------------
3261     if (G && GP) {
3262     gp_obj->SetBranchAddress(Trout); //ho dovuto fare in maniera diversa dagli altri
3263     cout << "h20 : set branch address GPamela " << endl;
3264     }
3265     else {
3266     cout << "h20 : missing tree" << endl;
3267     };
3268 pam-fi 1.52
3269 pam-fi 1.81 pam_tree = (TChain*) Trout;
3270 mocchiut 1.28
3271 pam-fi 1.81 return Trout;
3272 pam-fi 1.52
3273 pam-fi 1.1 }
3274 pam-fi 1.2 //--------------------------------------
3275     //
3276     //
3277     //--------------------------------------
3278     /**
3279     * Get list of Level2 files.
3280     * @param ddir Level2 data directory.
3281     * @param flisttxt Name of txt file containing file list.
3282     * @return Pointer to a TList of TSystemFiles
3283     * If no input file list is given , all the Level2 files inside the directory are processed.
3284     */
3285 pam-fi 1.81 TList* PamLevel2::GetListOfLevel2Files(TString ddir, TString flisttxt = "") {
3286    
3287     TString wdir = gSystem->WorkingDirectory();
3288    
3289     if (ddir != "") {
3290     cout << "Level2 data directory : " << ddir << endl;
3291     }
3292     else {
3293     cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory "
3294     << endl;
3295     };
3296     TList *contents = new TList; // create output list
3297     contents->SetOwner();
3298    
3299     // --------------------------------------
3300     // case 1 : input file/file-list provided
3301     // --------------------------------------
3302     if (flisttxt != "") {
3303    
3304 pam-fi 1.79 // --------------------------------------
3305 pam-fi 1.81 // a list of files given as input
3306 pam-fi 1.79 // --------------------------------------
3307 pam-fi 1.81 if (!flisttxt.EndsWith(".root")) {
3308    
3309     TString dir = gSystem->DirName(flisttxt);
3310     // cout << " List directory "<<dir<<endl;
3311     if (dir == "." || dir == "")
3312     flisttxt = gSystem->ConcatFileName(wdir.Data(), gSystem->BaseName(flisttxt));
3313     // flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
3314    
3315     if (!gSystem->ChangeDirectory(ddir)) {
3316     cout << "Cannot change directory : " << ddir << endl;
3317     return 0;
3318     }
3319    
3320     cout << "Input file list : " << flisttxt << endl;
3321     ifstream in;
3322     in.open(flisttxt, ios::in); //open input file list
3323     if (!in.good()) {
3324     cout << " TList* PamLevel2::GetListOfLevel2Files(TString, TString) --> ERROR opening the file " << endl;
3325     gSystem->ChangeDirectory(wdir); // back to the working directory
3326     return 0;
3327     }
3328     int line = 0;
3329     // ...........................
3330     // loop over file-list records
3331     // ...........................
3332     while (1) {
3333     TString file;
3334     in >> file;
3335     if (!in.good())
3336     break;
3337     line++;
3338     cout << "(*) " << file << endl;
3339     if (file.IsNull()) {
3340     cout << "-- list interrupted at line " << line << endl;
3341     break;
3342     }
3343     if (file.Contains("#"))
3344     file = file(0, file.First("#"));
3345     //
3346     // take only root files
3347     //
3348     if (file.EndsWith(".root")) {
3349     TString filedir;
3350     cout << ddir << endl;
3351 mocchiut 1.84 if ( ddir != "" ) {
3352 pam-fi 1.81 filedir = ddir; // take the input dir
3353     }
3354     else {
3355     gSystem->ChangeDirectory(wdir); // back to the working directory
3356     filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir
3357     };
3358     filedir.Append("/");
3359 mocchiut 1.84 // char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir), gSystem->BaseName(file));
3360     char *fullpath = gSystem->ConcatFileName(filedir.Data(), gSystem->BaseName(file));
3361 pam-fi 1.81 contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));// add file to the list
3362     cout << fullpath << endl;
3363     delete fullpath;
3364     }
3365     // }else{
3366     // if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;
3367     // };
3368     };
3369     in.close();
3370     // --------------------------------------
3371     // a single root file given as input
3372     // --------------------------------------
3373     }
3374     else {
3375     if (flisttxt.Contains("#"))
3376     flisttxt = flisttxt(0, flisttxt.First("#"));
3377     char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt), gSystem->BaseName(flisttxt));
3378     contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));// add file to the list
3379     delete fullpath;
3380     };
3381 pam-fi 1.79 // ---------------------------------------------------------------------------------
3382     // case 2 : no input file/file-list provided --> read all files insede the directory
3383     // ---------------------------------------------------------------------------------
3384 pam-fi 1.81 }
3385     else {
3386    
3387     cout << "No input file list given." << endl;
3388     cout << "Check for existing root files." << endl;
3389     // cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;
3390     if (ddir == "") {
3391     ddir = wdir;
3392     cout << "Level2 data directory : " << ddir << endl;
3393 pam-fi 1.6 };
3394 pam-fi 1.81
3395     TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir), ddir);
3396     TList *temp = datadir->GetListOfFiles();
3397     if (!temp)
3398     return 0;
3399     // temp->Print();
3400     // cout << "*************************************" << endl;
3401    
3402     TIter next(temp);
3403     TSystemFile *questo = 0;
3404    
3405     while ((questo = (TSystemFile*) next())) {
3406     TString name = questo-> GetName();
3407     if (name.EndsWith(".root")) {
3408     // const char *fullpath = gSystem->FindFile(ddir,name);
3409     // char *fullpath;
3410     // gSystem->IsFileInIncludePath(name,&fullpath);
3411     char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir), gSystem->BaseName(name));
3412     contents->Add(new TSystemFile(fullpath, gSystem->DirName(fullpath)));
3413     delete fullpath;
3414     };
3415     }
3416     delete temp;
3417     delete datadir;
3418    
3419     };
3420     gSystem->ChangeDirectory(wdir); // back to the working directory
3421     // cout << endl << "Selected files:" << endl;
3422     // contents->Print();
3423     cout << contents->GetEntries() << " files \n";
3424     // cout << endl;
3425     // cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;
3426     return contents;
3427     }
3428     ;
3429 pam-fi 1.2 //--------------------------------------
3430     //
3431     //
3432     //--------------------------------------
3433     /**
3434     * Get the Pamela detector chains from a list of files and make them friends.
3435 pam-fi 1.81 * @param fl Pointer to a TList of TSystemFiles
3436 pam-fi 1.2 * @param detlist String to select trees to be included
3437     * @return Pointer to a TChain
3438     */
3439 pam-fi 1.81 TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) {
3440     //
3441     //
3442     //
3443     if (pam_tree) {
3444     printf("WARNING: TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) -- pam_tree already exists!\n ");
3445     return pam_tree;
3446     };
3447     //
3448 pam-fi 1.2
3449 pam-fi 1.81 TChain *Trout = 0;
3450 pam-fi 1.52
3451 pam-fi 1.81 // -------------------------------------------
3452     // set flags to include/exclude trees/branches
3453     // -------------------------------------------
3454     if (detlist.Contains("+AUTO", TString::kIgnoreCase)) {
3455     if (fl->GetEntries() > 0) {
3456     cout << "+AUTO" << endl;
3457     TFile *fprimo = new TFile(fl->At(0)->GetName());
3458     GetWhichTrees(fprimo);
3459     fprimo->Close();// AAAAARGGGGGHHHHH!!!!!!! non commentare questa riga, altrimenti si incasina il TChain
3460     fprimo->Delete();
3461     }
3462     };
3463     SetWhichTrees(detlist);
3464 pam-fi 1.52
3465 pam-fi 1.81 // -------------------------------------------
3466     // build chains
3467     // -------------------------------------------
3468     TChain *T = 0;
3469     TChain *C = 0;
3470     TChain *O = 0;
3471     TChain *R = 0;
3472     TChain *S = 0;
3473     TChain *N = 0;
3474     TChain *A = 0;
3475     TChain *B = 0;
3476     TChain *G = 0;
3477    
3478     TChain *L = 0;
3479 mocchiut 1.100 TChain *P = 0;
3480 pam-fi 1.81
3481     if (TRK2 || TRK1 || TRKh)
3482     T = new TChain("Tracker");
3483     if (CAL2 || CAL1)
3484     C = new TChain("Calorimeter");
3485     if (TOF)
3486     O = new TChain("ToF");
3487     if (TRG)
3488     R = new TChain("Trigger");
3489     if (S4)
3490     S = new TChain("S4");
3491     if (ND)
3492     N = new TChain("NeutronD");
3493     if (AC)
3494     A = new TChain("Anticounter");
3495     if (ORB)
3496     B = new TChain("OrbitalInfo");
3497     if (GP)
3498     G = new TChain("h20");
3499 mocchiut 1.100 if (PROC)
3500     P = new TChain("ProcessingInfo");
3501 pam-fi 1.81 L = new TChain("SelectionList");
3502    
3503     // loop over files and create chains
3504     TIter next(fl);
3505     TSystemFile *questo = 0;
3506     while ((questo = (TSystemFile*) next())) {
3507     TString name = questo->GetName();
3508     cout << "File: " << name << endl;
3509     if (CheckLevel2File(name)) {
3510     if (TRK2 || TRK1 || TRKh)
3511     T->Add(name);
3512     if (CAL1 || CAL2)
3513     C->Add(name);
3514     if (TOF)
3515     O->Add(name);
3516     if (TRG)
3517     R->Add(name);
3518     if (S4)
3519     S->Add(name);
3520     if (ND)
3521     N->Add(name);
3522     if (AC)
3523     A->Add(name);
3524     if (ORB)
3525     B->Add(name);
3526     if (GP)
3527     G->Add(name);
3528 mocchiut 1.100 if (P)
3529     P->Add(name);
3530 pam-fi 1.81 if (SELLI == 1)
3531     L->Add(name);
3532 pam-ts 1.95 };
3533 pam-fi 1.81 };
3534 pam-fi 1.11
3535 pam-ts 1.95 cout << "done data-tree chains "<< T->GetNtrees() <<" \n";
3536     cout << "----------------------------------------------------" << endl;
3537 pam-fi 1.56
3538 pam-fi 1.81 // -------------------------------------------
3539     // make friends
3540     // -------------------------------------------
3541    
3542     // Tracker
3543     cout << "Friends: " << endl;
3544     if (T && (TRK2 || TRK1 || TRKh)) {
3545     if (!Trout)
3546     Trout = T;
3547     else
3548     Trout->AddFriend("Tracker");
3549     // cout << "+Tacker"<<endl;
3550     };
3551     // Calorimeter
3552     if (C && (CAL2 || CAL1)) {
3553     if (!Trout)
3554     Trout = C;
3555     else
3556     Trout->AddFriend("Calorimeter");
3557     // cout << "+Calorimeter"<<endl;
3558     };
3559     // ToF
3560     if (O && TOF) {
3561     if (!Trout)
3562     Trout = O;
3563     else
3564     Trout->AddFriend("ToF");
3565     // cout << "+ToF"<<endl;
3566     };
3567     // Trigger
3568     if (R && TRG) {
3569     if (!Trout)
3570     Trout = O;
3571     else
3572     Trout->AddFriend("Trigger");
3573     // cout << "+Trigger"<<endl;
3574     };
3575     // S4
3576     if (S && S4) {
3577     if (!Trout)
3578     Trout = O;
3579     else
3580     Trout->AddFriend("S4");
3581     // cout << "+S4"<<endl;
3582     };
3583     // Neutron Detector
3584     if (N && ND) {
3585     if (!Trout)
3586     Trout = O;
3587     else
3588     Trout->AddFriend("NeutronD");
3589     // cout << "+NeutronD"<<endl;
3590     };
3591     // Anticounters
3592     if (A && AC) {
3593     if (!Trout)
3594     Trout = O;
3595     else
3596     Trout->AddFriend("Anticounter");
3597     // cout << "+Anticounter"<<endl;
3598     };
3599     // Orbital Info
3600     if (B && ORB) {
3601     if (!Trout)
3602     Trout = O;
3603     else
3604     Trout->AddFriend("OrbitalInfo");
3605     // cout << "+OrbitalInfo"<<endl;
3606     };
3607     // GPamela
3608     if (G && GP) {
3609     if (!Trout)
3610     Trout = G;
3611     else
3612     Trout->AddFriend("h20");
3613     // cout << "+h20"<<endl;
3614     };
3615 pam-fi 1.52
3616 pam-fi 1.81 // =====================================
3617     // SET BRANCH-ADDRESS AFTER CHAIN+FRIEND
3618     // =====================================
3619 pam-ts 1.95 if( Trout->GetNtrees() )SetBranchAddress(Trout);
3620 pam-fi 1.81
3621     // ------------------------------------
3622     // finally handle selection trees...
3623     // (it is not friend of pamela tree)
3624     // ------------------------------------
3625    
3626 pam-ts 1.95 cout << "----------------------------------------------------" << endl;
3627 pam-fi 1.81
3628     // Selection List
3629     if (L && SELLI == 1) {
3630     cout << ">>> Found selection-list <<<" << endl;
3631     // L->SetBranchAddress("RunEntry",&irun);
3632     L->SetBranchAddress("RunEntry", &irunt);//NEWNEW
3633     cout << "SelectionList: set branch address RunEntry" << endl;
3634     L->SetBranchAddress("EventEntry", &irunentry);
3635     cout << "SelectionList: set branch address EventEntry" << endl;
3636 mocchiut 1.91 /* if ( L->GetBranch("L0EventEntry") ){
3637 mocchiut 1.90 hasL0EE = true;
3638     L->SetBranchAddress("L0EventEntry", &il0entry);
3639     cout << "SelectionList: set branch address L0EventEntry" << endl;
3640     } else {
3641     hasL0EE = false; // backward compatibility with old preselected files...
3642 mocchiut 1.91 }*/
3643 pam-fi 1.81 sel_tree = L;
3644     // if(!Trout)Trout=O;
3645     // else Trout->AddFriend("SelectionList");
3646     cout << "+SelectionList" << endl;
3647 pam-ts 1.95 cout << "----------------------------------------------------" << endl;
3648 pam-fi 1.81 }
3649     else {
3650     // cout << "SelectionList : missing tree"<<endl;
3651     if (L)
3652     L->Delete();
3653     };
3654 pam-fi 1.52
3655 mocchiut 1.100 //ProcessingInfo EM
3656     if ( P && P->GetEntries() ){
3657     cout << "----------------------------------------------------" << endl;
3658     cout << ">>> Found ProcessingInfo <<<" << endl;
3659     // L->SetBranchAddress("RunEntry",&irun);
3660     P->SetBranchAddress("ProcInfo", &proc_obj);//NEWNEW
3661 mocchiut 1.102 proc_tree = P;
3662 mocchiut 1.109 } else {
3663     // proc_tree = new TChain("ProcessingInfo","Log of data processing");
3664     // proc_tree->Branch("ProcInfo", "ProcInfo", &proc_obj);
3665     cout << "----------------------------------------------------" << endl;
3666     cout << ">>> ProcessingInfo not found, PROC set to false and continue (not 10RED files?)<<<" << endl;
3667     PROC = false;
3668 mocchiut 1.100 }
3669 pam-fi 1.81 // --------------------------------------------
3670     // return the pamela chain with all the friends
3671     // --------------------------------------------
3672 pam-fi 1.8
3673 pam-fi 1.81 pam_tree = Trout;
3674     return Trout;
3675     }
3676 pam-fi 1.8
3677 pam-fi 1.4 //--------------------------------------
3678     //
3679     //
3680     //--------------------------------------
3681     /**
3682     * Set branch addresses for Pamela friend trees
3683     */
3684 pam-fi 1.81 void PamLevel2::SetBranchAddress(TTree *t) {
3685    
3686 pam-ts 1.95 // TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");
3687     // TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");
3688     // TRKh = TRKh & t->GetBranchStatus("TrkHough");
3689     // CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");
3690     // CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");
3691     // TOF = TOF & t->GetBranchStatus("ToFLevel2");
3692     // TRG = TRG & t->GetBranchStatus("TrigLevel2");
3693     // S4 = S4 & t->GetBranchStatus("S4Level2");
3694     // ND = ND & t->GetBranchStatus("NDLevel2");
3695     // AC = AC & t->GetBranchStatus("AcLevel2");
3696     // ORB = ORB & t->GetBranchStatus("OrbitalInfo");
3697     // GP = GP & t->GetBranchStatus("h20");
3698    
3699 pam-fi 1.81
3700     // Tracker
3701     if (TRK1) {
3702     t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel2"));
3703     cout << "Tracker : set branch address TrkLevel1" << endl;
3704     };
3705     if (TRK2) {
3706     t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));
3707     cout << "Tracker : set branch address TrkLevel2" << endl;
3708 pam-ts 1.95 NUC = t->GetBranchStatus("TrackNuclei");
3709     EXT = t->GetBranchStatus("RecoveredTrack") && (NUC ? t->GetBranchStatus("RecoveredTrackNuclei"): true );
3710 pam-fi 1.81 };
3711     if (TRKh) {
3712     t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
3713     cout << "Tracker : set branch address TrkHough" << endl;
3714     };
3715    
3716     // Calorimeter
3717     if (CAL1) {
3718     t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
3719     cout << "Calorimeter : set branch address CaloLevel1" << endl;
3720     };
3721     if (CAL2) {
3722     t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
3723     cout << "Calorimeter : set branch address CaloLevel2" << endl;
3724     };
3725 pam-fi 1.8
3726 pam-fi 1.81 // ToF
3727     if (TOF) {
3728     t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
3729     cout << "ToF : set branch address ToFLevel2" << endl;
3730     };
3731     // Trigger
3732     if (TRG) {
3733     t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
3734     cout << "Trigger : set branch address TrigLevel2" << endl;
3735     };
3736     // S4
3737     if (S4) {
3738     t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
3739     cout << "S4 : set branch address S4Level2" << endl;
3740     };
3741     // Neutron Detector
3742     if (ND) {
3743     t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
3744     cout << "NeutronD : set branch address NDLevel2" << endl;
3745     };
3746     // Anticounters
3747     if (AC) {
3748     t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
3749     cout << "Anticounter : set branch address AcLevel2" << endl;
3750     };
3751     // OrbitalInfo
3752     if (ORB) {
3753     t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
3754     cout << "OrbitalInfo : set branch address OrbitalInfo" << endl;
3755     };
3756     // GPamela
3757     if (GP) {
3758     // GetPointerTo("GPamela");
3759     if (!gp_obj)
3760     gp_obj = new GPamela();
3761     // gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
3762     // // t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
3763     if (SELLI)
3764     t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
3765     else
3766     gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
3767    
3768     cout << "h20 : set branch address GPamela " << endl;
3769     };
3770 pam-ts 1.95 if(NUC){
3771    
3772     cout << "Found nuclei-track branches" << endl;
3773    
3774     if( !trk_nuc_obj )trk_nuc_obj = new TClonesArray("TrkTrack");
3775     if( !calo_nuc_obj)calo_nuc_obj= new TClonesArray("CaloTrkVar");
3776     if( !tof_nuc_obj)tof_nuc_obj= new TClonesArray("ToFTrkVar");
3777     if( !orb_nuc_obj)orb_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3778     if (TRK2)t-> SetBranchAddress("TrackNuclei",&trk_nuc_obj);
3779     if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("TrackNuclei",&calo_nuc_obj);
3780     if (TOF )t->GetFriend("ToF")-> SetBranchAddress("TrackNuclei",&tof_nuc_obj);
3781     if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("TrackNuclei",&orb_nuc_obj);
3782    
3783     ///copy the vector content inside a "fake" object (all aother info are missing)
3784    
3785     if( !trk2_nuc_obj )trk2_nuc_obj = new TrkLevel2();
3786     if( !calo2_nuc_obj )calo2_nuc_obj = new CaloLevel2();
3787     if( !tof2_nuc_obj )tof2_nuc_obj = new ToFLevel2();
3788     if( !orb2_nuc_obj )orb2_nuc_obj = new OrbitalInfo();
3789     // *(trk2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*trk_nuc_obj);
3790     // *(calo2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*calo_nuc_obj);
3791     // *(tof2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*tof_nuc_obj);
3792     // *(orb2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*orb_nuc_obj);
3793    
3794     trk2_nuc_obj->SetTrackArray( trk_nuc_obj );
3795     calo2_nuc_obj->SetTrackArray( calo_nuc_obj );
3796     tof2_nuc_obj->SetTrackArray( tof_nuc_obj );
3797     orb2_nuc_obj->SetTrackArray( orb_nuc_obj );
3798    
3799    
3800     }
3801 pam-fi 1.61
3802 pam-ts 1.95 if(EXT){
3803    
3804     cout << "Found extended tracking algorythm branches" << endl;
3805    
3806     if( !trk_ext_obj )trk_ext_obj = new TClonesArray("ExtTrack");
3807     if( !calo_ext_obj)calo_ext_obj= new TClonesArray("CaloTrkVar");
3808     if( !tof_ext_obj)tof_ext_obj= new TClonesArray("ToFTrkVar");
3809     if( !orb_ext_obj)orb_ext_obj= new TClonesArray("OrbitalInfoTrkVar");
3810    
3811     if (TRK2)t-> SetBranchAddress("RecoveredTrack",&trk_ext_obj);
3812     if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrack",&calo_ext_obj);
3813     if (TOF )t->GetFriend("ToF")-> SetBranchAddress("RecoveredTrack",&tof_ext_obj);
3814     if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrack",&orb_ext_obj);
3815    
3816    
3817     if(NUC){
3818     if( !trk_ext_nuc_obj )trk_ext_nuc_obj = new TClonesArray("ExtTrack");
3819     if( !calo_ext_nuc_obj)calo_ext_nuc_obj= new TClonesArray("CaloTrkVar");
3820     if( !tof_ext_nuc_obj)tof_ext_nuc_obj= new TClonesArray("ToFTrkVar");
3821     if( !orb_ext_nuc_obj)orb_ext_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3822     if (TRK2)t-> SetBranchAddress("RecoveredTrackNuclei",&trk_ext_nuc_obj);
3823     if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrackNuclei",&calo_ext_nuc_obj);
3824     if (TOF )t->GetFriend("ToF")-> SetBranchAddress("RecoveredTrackNuclei",&tof_ext_nuc_obj);
3825     if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrackNuclei",&orb_ext_nuc_obj);
3826     }
3827     }
3828 pam-fi 1.4 }
3829 pam-fi 1.8 /**
3830     * Set branch addresses for Pamela friend trees
3831     */
3832 pam-fi 1.81 void PamLevel2::SetBranchAddress(TChain *t) {
3833    
3834     // TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");
3835     // TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");
3836     // TRKh = TRKh & t->GetBranchStatus("TrkHough");
3837     // CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");
3838     // CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");
3839     // TOF = TOF & t->GetBranchStatus("ToFLevel2");
3840     // TRG = TRG & t->GetBranchStatus("TrigLevel2");
3841     // S4 = S4 & t->GetBranchStatus("S4Level2");
3842     // ND = ND & t->GetBranchStatus("NDLevel2");
3843     // AC = AC & t->GetBranchStatus("AcLevel2");
3844     // ORB = ORB & t->GetBranchStatus("OrbitalInfo");
3845     // GP = GP & t->GetBranchStatus("h20");
3846    
3847     // Tracker
3848     if (TRK2) {
3849     t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
3850     cout << "Tracker : set branch address TrkLevel2" << endl;
3851 pam-ts 1.95 NUC = t->GetBranchStatus("TrackNuclei");
3852     EXT = t->GetBranchStatus("RecoveredTrack") && (NUC ? t->GetBranchStatus("RecoveredTrackNuclei"): true );
3853 pam-fi 1.81 };
3854     if (TRK1) {
3855     t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
3856     cout << "Tracker : set branch address TrkLevel1" << endl;
3857     };
3858     if (TRKh) {
3859     t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
3860     cout << "Tracker : set branch address TrkHough" << endl;
3861     };
3862    
3863     // Calorimeter
3864     if (CAL2) {
3865     t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
3866 pam-ts 1.95 cout << "Calorimeter : set branch address CaloLevel2" << endl;
3867 pam-fi 1.81 };
3868     if (CAL1) {
3869     t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
3870     cout << "Calorimeter : set branch address CaloLevel1" << endl;
3871     };
3872    
3873     // ToF
3874     if (TOF) {
3875     t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
3876     cout << "ToF : set branch address ToFLevel2" << endl;
3877     };
3878     // Trigger
3879     if (TRG) {
3880     t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
3881     cout << "Trigger : set branch address TrigLevel2" << endl;
3882     };
3883     // S4
3884     if (S4) {
3885     t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
3886     cout << "S4 : set branch address S4Level2" << endl;
3887     };
3888     // Neutron Detector
3889     if (ND) {
3890     t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
3891     cout << "NeutronD : set branch address NDLevel2" << endl;
3892     };
3893     // Anticounters
3894     if (AC) {
3895     t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
3896     cout << "Anticounter : set branch address AcLevel2" << endl;
3897     };
3898     // OrbitalInfo
3899     if (ORB) {
3900     t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
3901     cout << "OrbitalInfo : set branch address OrbitalInfo" << endl;
3902     };
3903     // GPamela
3904     // cout <<"GP "<<GP<<endl;
3905     if (GP) {
3906     // GetPointerTo("GPamela");
3907     if (!gp_obj)
3908     gp_obj = new GPamela();
3909     if (SELLI)
3910     t->SetBranchAddress("GPamela", GetPointerTo("GPamela"));
3911     else
3912     gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
3913     // gp_obj->SetBranchAddress(t); //ho dovuto fare in maniera diversa dagli altri
3914     cout << "h20 : set branch address GPamela " << endl;
3915     };
3916     // SelectionList
3917     // if(SELLI==1){
3918     // t->SetBranchAddress("RunEntry",&irunt);//NEWNEW
3919     // cout << "SelectionList: set branch address RunEntry"<<endl;
3920     // t->SetBranchAddress("EventEntry",&irunentry);
3921     // cout << "SelectionList: set branch address EventEntry"<<endl;
3922 pam-fi 1.4
3923 pam-fi 1.81 // }
3924 pam-ts 1.95 if(NUC){
3925    
3926     cout << "Found nuclei-track branches" << endl;
3927    
3928     if( !trk_nuc_obj )trk_nuc_obj = new TClonesArray("TrkTrack");
3929     if( !calo_nuc_obj)calo_nuc_obj= new TClonesArray("CaloTrkVar");
3930     if( !tof_nuc_obj)tof_nuc_obj= new TClonesArray("ToFTrkVar");
3931     if( !orb_nuc_obj)orb_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3932     if (TRK2)t-> SetBranchAddress("TrackNuclei",&trk_nuc_obj);
3933     if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("TrackNuclei",&calo_nuc_obj);
3934     if (TOF )t->GetFriend("ToF")-> SetBranchAddress("TrackNuclei",&tof_nuc_obj);
3935     if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("TrackNuclei",&orb_nuc_obj);
3936    
3937     ///copy the vector content inside a "fake" object (all aother info are missing)
3938    
3939     if( !trk2_nuc_obj )trk2_nuc_obj = new TrkLevel2();
3940     if( !calo2_nuc_obj )calo2_nuc_obj = new CaloLevel2();
3941     if( !tof2_nuc_obj )tof2_nuc_obj = new ToFLevel2();
3942     if( !orb2_nuc_obj )orb2_nuc_obj = new OrbitalInfo();
3943    
3944     // *(trk2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*trk_nuc_obj);
3945     // *(calo2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*calo_nuc_obj);
3946     // *(tof2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*tof_nuc_obj);
3947     // *(orb2_nuc_obj->GetPointerToTrackArray()) = new TClonesArray(*orb_nuc_obj);
3948     trk2_nuc_obj->SetTrackArray( trk_nuc_obj );
3949     calo2_nuc_obj->SetTrackArray( calo_nuc_obj );
3950     tof2_nuc_obj->SetTrackArray( tof_nuc_obj );
3951     orb2_nuc_obj->SetTrackArray( orb_nuc_obj );
3952    
3953     }
3954     if(EXT){
3955    
3956     cout << "Found extended tracking algorythm branches" << endl;
3957    
3958     t->SetBranchAddress("extAlgFlag",&extAlgFlag);
3959 pam-fi 1.56
3960 pam-ts 1.95 if( !trk_ext_obj )trk_ext_obj = new TClonesArray("ExtTrack");
3961     if( !calo_ext_obj)calo_ext_obj= new TClonesArray("CaloTrkVar");
3962     if( !tof_ext_obj)tof_ext_obj= new TClonesArray("ToFTrkVar");
3963     if( !orb_ext_obj)orb_ext_obj= new TClonesArray("OrbitalInfoTrkVar");
3964    
3965     if (TRK2)t-> SetBranchAddress("RecoveredTrack",&trk_ext_obj);
3966     if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrack",&calo_ext_obj);
3967     if (TOF )t->GetFriend("ToF")-> SetBranchAddress("RecoveredTrack",&tof_ext_obj);
3968     if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrack",&orb_ext_obj);
3969    
3970     if(NUC){
3971     if( !trk_ext_nuc_obj )trk_ext_nuc_obj = new TClonesArray("ExtTrack");
3972     if( !calo_ext_nuc_obj)calo_ext_nuc_obj= new TClonesArray("CaloTrkVar");
3973     if( !tof_ext_nuc_obj)tof_ext_nuc_obj= new TClonesArray("ToFTrkVar");
3974     if( !orb_ext_nuc_obj)orb_ext_nuc_obj= new TClonesArray("OrbitalInfoTrkVar");
3975     if (TRK2)t-> SetBranchAddress("RecoveredTrackNuclei",&trk_ext_nuc_obj);
3976     if (CAL2)t->GetFriend("Calorimeter")->SetBranchAddress("RecoveredTrackNuclei",&calo_ext_nuc_obj);
3977     if (TOF )t->GetFriend("ToF")-> SetBranchAddress("RecoveredTrackNuclei",&tof_ext_nuc_obj);
3978     if (ORB )t->GetFriend("OrbitalInfo")->SetBranchAddress("RecoveredTrackNuclei",&orb_ext_nuc_obj);
3979     }
3980     }
3981    
3982 pam-fi 1.8 }
3983 pam-fi 1.9
3984 pam-ts 1.95 /**
3985     * Set the tracking algorythm
3986     * @param alg String to choose the track.
3987     * "" --> take the output of the standard tracking algorythm
3988     * "STD" --> take the output of the standard tracking algorythm
3989     * "NUC" --> take the output of the standard tracking algorythm for nuclei cluster selection
3990     * "EXT" --> in case the standard tracking algorythm has not found any track, take the output of the extended one
3991     * "EXTF" --> force the extended tracking algorythm
3992     * "NUCEXT" --> as "EXT", but for nuclei
3993     * "NUCEXTF" --> as "EXTF", but for nuclei
3994     */
3995 pam-ts 1.96 // void PamLevel2::SetTrackingAlgorythm(const char *alg){
3996 pam-ts 1.95
3997    
3998 pam-ts 1.96 // TString s(alg);
3999     // if(s.Contains("NUC", TString::kIgnoreCase) && !NUC)
4000     // cout << "Warning! NUC algorythm requested, but branches are missing"<<endl;
4001     // if(s.Contains("EXT", TString::kIgnoreCase) && !EXT)
4002     // cout << "Warning! EXT algorythm requested, but branches are missing"<<endl;;
4003 pam-ts 1.95
4004 pam-ts 1.96 // trkAlg = alg;
4005 pam-ts 1.95
4006 pam-ts 1.96 // };
4007     // const char* PamLevel2::GetTrackingAlgorythm(){
4008 pam-ts 1.95
4009    
4010 pam-ts 1.96 // cout<<endl<<" Implemented tracking algorythm: ";
4011     // cout<<endl<<" \"\" or \"STD\" --> take the output of the standard tracking algorythm";
4012     // cout<<endl<<" \"NUC\" --> take the output of the standard tracking algorythm for nuclei cluster selection";
4013     // cout<<endl<<" \"EXT\" --> in case the standard tracking algorythm has not found any track,";
4014     // cout<<endl<<" take the output of the extended one";
4015     // cout<<endl<<" \"EXTF\" --> force the extended tracking algorythm";
4016     // cout<<endl<<" \"NUCEXT\" --> as \"EXT\", but for nuclei ";
4017     // cout<<endl<<" \"NUCEXTF\" --> as \"EXTF\", but for nuclei";
4018    
4019     // cout<<endl;
4020     // cout<<" <<Currently set algorythm>> "<<trkAlg<<endl;
4021     // cout<<endl;
4022 pam-ts 1.95
4023 pam-ts 1.96 // return trkAlg;
4024     // };
4025 pam-ts 1.95
4026    
4027    
4028 pam-fi 1.4 //--------------------------------------
4029     //
4030     //
4031     //--------------------------------------
4032     /**
4033     * Get the Run tree chain from a list of files.
4034 pam-fi 1.81 * @param fl Pointer to a TList of TSystemFiles
4035 pam-fi 1.4 * @return Pointer to a TChain
4036     */
4037 pam-fi 1.81 TChain *PamLevel2::GetRunTree(TList *fl) {
4038     //
4039     //
4040     //
4041     if (run_tree) {
4042     printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");
4043     return run_tree;
4044     };
4045     //
4046    
4047    
4048     TChain *R = new TChain("Run");
4049 pam-fi 1.60
4050 pam-fi 1.81 // loop over files and create chains
4051     TIter next(fl);
4052     TSystemFile *questo = 0;
4053     while ((questo = (TSystemFile*) next())) {
4054     TString name = questo->GetName();
4055     // cout << "File: "<< name << endl;
4056     if (CheckLevel2File(name)) {
4057     R->Add(name);
4058     };
4059     }
4060    
4061 pam-ts 1.95 cout << "done run chain "<< R->GetNtrees() <<" \n";
4062    
4063    
4064 pam-fi 1.81 if (RUN && R->GetNtrees()) {
4065    
4066     R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
4067     cout << "Run : set branch address RunInfo" << endl;
4068     R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
4069     cout << "Software : set branch address SoftInfo" << endl; // Emiliano
4070    
4071     irunoffset = new int[R->GetNtrees()];
4072     if (DBG) {
4073     cout << "----------------------------------------------------" << endl;
4074     cout << "irun\t | ";
4075     cout << "tree\t |";
4076     // cout << "offset\t |";
4077     cout << "RUN\t";
4078     cout << "FRAG\t";
4079     cout << "NEVENTS\t";
4080     cout << "absolute time\t\t\t";
4081     cout << "on-board time";
4082     cout << endl;
4083     }
4084     for (Int_t ii = 0; ii < R->GetEntries(); ii++) {
4085     R->GetEntry(ii);
4086     if (DBG) {
4087     cout << ii << "\t | ";
4088     cout << R->GetTreeNumber() << "\t |";
4089     // cout << R->GetChainOffset()<< "\t |";
4090     cout << GetRunInfo()->ID << "\t";
4091     cout << GetRunInfo()->ID_RUN_FRAG << "\t";
4092     cout << GetRunInfo()->NEVENTS << "\t";
4093     cout << GetRunInfo()->RUNHEADER_TIME << " <---> " << GetRunInfo()->RUNTRAILER_TIME << "\t";
4094     cout << GetRunInfo()->RUNHEADER_OBT << " <---> " << GetRunInfo()->RUNTRAILER_OBT << "\t";
4095     cout << endl;
4096     }
4097     irunoffset[R->GetTreeNumber()] = R->GetChainOffset();
4098     }
4099     cout << "N.run = " << R->GetEntries() << endl;
4100     cout << "----------------------------------------------------" << endl;
4101    
4102     }
4103 pam-ts 1.95 // else {
4104     // delete R;
4105     // R = 0;
4106     // }
4107 pam-fi 1.60
4108 pam-fi 1.81 run_tree = R;
4109 pam-fi 1.20
4110 pam-fi 1.81 return R;
4111 pam-fi 1.20
4112 pam-fi 1.4 }
4113     //--------------------------------------
4114     //
4115     //
4116     //--------------------------------------
4117     /**
4118     * Get the Run tree from a file.
4119 pam-fi 1.81 * @param f Pointer to a TFile
4120 pam-fi 1.4 * @return Pointer to a TTree
4121     */
4122 pam-fi 1.81 TTree *PamLevel2::GetRunTree(TFile *f) {
4123     if (run_tree) {
4124     printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");
4125     return run_tree;
4126     };
4127    
4128     cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte " << endl;
4129    
4130     TTree *T = (TTree*) f->Get("Run");
4131 pam-fi 1.4
4132 pam-fi 1.81 if (T) {
4133     T->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
4134     cout << "Run : set branch address RunInfo" << endl;
4135     T->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
4136     cout << "Software : set branch address SoftInfo" << endl; // Emiliano
4137 pam-fi 1.4
4138 pam-fi 1.81 }
4139 pam-fi 1.56
4140 pam-fi 1.81 run_tree = (TChain*) T;
4141 mocchiut 1.28
4142 pam-fi 1.81 return T;
4143 mocchiut 1.28
4144 pam-fi 1.2 }
4145 mocchiut 1.89
4146 mocchiut 1.90 Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) {
4147 pam-fi 1.36
4148 mocchiut 1.90 if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - inside\n");
4149 pam-fi 1.36
4150 pam-fi 1.81 if (!run_tree) {
4151     cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded" << endl;
4152     return false;
4153     }
4154     if (run_tree->GetEntries() <= 0) {
4155     cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty" << endl;
4156     return (false);
4157     }
4158    
4159 mocchiut 1.89
4160     Int_t oldrun = irun; // store current run index
4161    
4162     // -----------------------------------------------------------------------
4163     // the first time the routine is called, set run search from the beginning
4164     // -----------------------------------------------------------------------
4165    
4166     if (irun < 0) {
4167     irun = 0LL;
4168     irunt = 0LL;
4169     totrunentry = 0LL;
4170     totrunentrymin = 0LL;
4171     totrunentrymax = 0LL;
4172 mocchiut 1.90 irunentry = 0LL;
4173     il0entry = 0LL;
4174 mocchiut 1.89 prevshift = 0;
4175 mocchiut 1.90 yprevshift = 0;
4176 mocchiut 1.89 prevabstime = 0;
4177     prevpktnum = 0;
4178     abstime = 0ULL;
4179     pktnum = 0;
4180     isFragment = false;
4181     run_tree->GetEntry(irun);
4182     if (!GetOrbitalInfo())
4183 pam-fi 1.111 cout << "PamLevel2::UpdateRunInfo(Long64_t "<<iev<<") ** WARNING ** missing OrbitalInfo ORB="<<ORB << endl;
4184 mocchiut 1.94 if ( fUseDBinRunInfo ){
4185     if (gltsync)
4186     delete gltsync; //Emiliano
4187     if (!dbc || (dbc && !dbc->IsConnected()))
4188     SetDBConnection(); //Emiliano
4189     gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); //Emiliano // the "false" means not to use level0 file (not necessary here)
4190     if (dbc){
4191     dbc->Close();// Emiliano
4192     delete dbc;
4193     dbc=0;
4194     }
4195     }
4196     }
4197 mocchiut 1.89 // ---------------------------------------------------------------
4198     // retrieve OBT and absolute time of the event
4199     // ---------------------------------------------------------------
4200     Long64_t obt = 0LL; // Emiliano, Long64_t GL_TIMESYNC::DBobt(UInt_t obt) since depending on the situation OBT is lowered or boosted
4201     prevabstime = abstime;
4202     prevpktnum = pktnum;
4203     if (GetOrbitalInfo()) {
4204     abstime = GetOrbitalInfo()->absTime;
4205 mocchiut 1.94 if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetOrbitalInfo()->OBT); // Emiliano
4206 mocchiut 1.89 pktnum = GetOrbitalInfo()->pkt_num; // Emiliano
4207     }
4208     else {
4209     abstime = GetRunInfo()->RUNHEADER_TIME;
4210 mocchiut 1.94 if ( fUseDBinRunInfo ) obt = gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT); // Emiliano
4211 mocchiut 1.89 pktnum = GetRunInfo()->RUNHEADER_PKT; // Emiliano
4212     }
4213    
4214     if (DBG){
4215     printf("0abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4216     printf("0 rth %d %d nevents %d \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4217     printf("0 rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4218 mocchiut 1.94 if ( fUseDBinRunInfo ) printf("0 rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4219 mocchiut 1.90 printf("0 bo irunentry %lld prevshift %lld irun %lld \n",irunentry,prevshift,irun);
4220 mocchiut 1.89 printf("0 min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
4221     }
4222    
4223     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4224     // if it is a full file (not preselected)
4225     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4226     if (SELLI == 0 || SELLI == 2) { // Emiliano
4227    
4228     // ---------------------------------------------------------------
4229     // increment dead and live-time counters
4230     // (only when reading a file not preselected)
4231     // ---------------------------------------------------------------
4232     if (SELLI == 0) {
4233     if (GetTrigLevel2()) {
4234     totdltime[0] += GetTrigLevel2()->dltime[0];
4235     totdltime[1] += GetTrigLevel2()->dltime[1];
4236     }
4237     totdltime[2]++;
4238     }
4239    
4240     //
4241 mocchiut 1.94 bool a = true;
4242     bool b = true;
4243     if ( fUseDBinRunInfo ){
4244     a = false;
4245     b = false;
4246     if ( obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) a = true;
4247     if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true;
4248     }
4249 mocchiut 1.89 if ( iev < totrunentrymin || iev > totrunentrymax // entry is outside run limits
4250     || iev == 0 // or it is the first entry
4251 mocchiut 1.90 || (!isSync && (
4252 mocchiut 1.94 (abstime <= GetRunInfo()->RUNHEADER_TIME && a ) // or it is outside obt limits (and abstime limits for security reasons)
4253     || (abstime >= GetRunInfo()->RUNTRAILER_TIME && b ) ))// or it is outside obt limits (and abstime limits for security reasons)
4254 mocchiut 1.90 ){ // check on abstime and obt needed to handle nested+DV_skipped packets
4255 mocchiut 1.89
4256     // check for a new run (ma prima il primo!)
4257     if (DBG){
4258     printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4259     printf("1 rth %d %d nevents %d \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4260     printf("1 rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4261 mocchiut 1.94 if ( fUseDBinRunInfo ) printf("1 rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4262 mocchiut 1.90 printf("1 bo irunentry %lld prevshift %lld irun %lld \n",irunentry,prevshift,irun);
4263 mocchiut 1.89 printf("1 min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
4264     }
4265 mocchiut 1.90 // printf("1abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4266     // printf("1 rth %d %d nevents %d \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4267     // printf("1 rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4268     // printf("1 rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4269     // printf("1 bo irunentry %lld prevshift %lld irun %lld \n",irunentry,prevshift,irun);
4270     // printf("1 min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
4271 mocchiut 1.89
4272     totrunentry = 0LL;
4273 mocchiut 1.90 runfirstentry = 0LL;
4274 mocchiut 1.89 for (Int_t r=0; r< run_tree->GetEntries();r++){
4275     // -------------------------------------------------------------------
4276     // save the index of the first entry of the run, relative to pam_tree,
4277     // and read a new run
4278     // -------------------------------------------------------------------
4279     run_tree->GetEntry(r);//update runinfo
4280     if ( r > 0 ){
4281     totrunentrymin = totrunentrymax+1;
4282     } else {
4283     totrunentrymin = 0LL;
4284     }
4285     totrunentry += GetRunInfo()->NEVENTS;
4286 mocchiut 1.90 totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
4287 mocchiut 1.89 irun = r;
4288 mocchiut 1.94 if ( fUseDBinRunInfo ){
4289     a = false;
4290     b = false;
4291     if ( obt < gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) ) a = true;
4292     if ( obt > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT) ) b = true;
4293     }
4294 mocchiut 1.90 if ( (iev >= totrunentrymin && iev <= totrunentrymax) || // entry is inside run limits
4295     ( !isSync &&
4296 mocchiut 1.94 ( abstime >= GetRunInfo()->RUNHEADER_TIME && a // or it is inside obt limits (and abstime limits for security reasons)
4297     && abstime <= GetRunInfo()->RUNTRAILER_TIME && b)) // or it is inside obt limits (and abstime limits for security reasons)
4298 mocchiut 1.90 ){ // check on abstime and obt needed to handle nested+DV_skipped packets
4299     if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
4300     if ( !isSync ){
4301     if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
4302     if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4303     // printf("PamLevel2::UpdateRunInfo(Long64_t) - unconsistent iev - nevents, probable DBL0-L2 async\n");
4304     // printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4305     prevshift += (totrunentrymin-iev); // add the new shift to total shift
4306     totrunentrymin -= (totrunentrymin-iev); // shift run position min
4307     totrunentrymax -= (totrunentrymin-iev); // shift run position max
4308     if (DBG) printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
4309     // printf("PamLevel2::UpdateRunInfo(Long64_t) - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
4310     } else {
4311     printf(" PamLevel2::UpdateRunInfo(Long64_t) ERROR! sync file but unconsistent totrunetrymin %lld and iev %lld!!! \n",totrunentrymin,iev);
4312     cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
4313     cout << "\nFor bug reporting instructions, please see for example:\n";
4314     cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
4315     cout << " " << endl;
4316     }
4317     }
4318     runfirstentry = totrunentrymin; // first entry of the run in the level2
4319    
4320 mocchiut 1.89
4321     //
4322 mocchiut 1.94 if ( fUseDBinRunInfo ){
4323     if (gltsync)
4324     delete gltsync; // Emiliano
4325     if (!dbc || (dbc && !dbc->IsConnected()))
4326     SetDBConnection(); //Emiliano
4327     gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
4328     TrkParams::Set(GetRunInfo(), dbc);
4329     if (dbc){
4330     dbc->Close(); // Emiliano
4331     delete dbc;
4332     dbc=0;
4333     }
4334     if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
4335     cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
4336     << " has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
4337     cout
4338     << " (NB!! in this case some events could be assigned to a wrong run)"
4339     << endl;
4340     }
4341 mocchiut 1.89 }
4342     //
4343     if (DBG) printf(" found \n");
4344 mocchiut 1.90 // printf(" found \n");//TOGLITOGLI
4345 mocchiut 1.89 //
4346     break;
4347     }
4348     } // loop over run
4349    
4350     // --------------------------------------
4351     // if there was no need to update the run
4352     // ---> exit with FALSE
4353     // --------------------------------------
4354 mocchiut 1.90 if (irun == oldrun){
4355     if (DBG) printf(" no new run \n");
4356     // printf(" no new run \n");//TOGLITOGLI
4357 mocchiut 1.89 return (false);
4358 mocchiut 1.90 }
4359 mocchiut 1.89 // --------------------------------------
4360     // ... otherwise
4361     // ---> exit with TRUE
4362     // --------------------------------------
4363    
4364     if (SELLI != 2) {
4365     /// decrement counters since this event belongs to a new run
4366     if (GetTrigLevel2()) {
4367     totdltime[0] -= GetTrigLevel2()->dltime[0];//live-time
4368     totdltime[1] -= GetTrigLevel2()->dltime[1];//dead-time
4369     }
4370     totdltime[2]--; //event counter
4371     if (DBG) {
4372     cout << endl;
4373     cout << "n.events : " << totdltime[2] << endl;
4374     cout << "RUN LIVE-TIME: " << totdltime[0] * 0.16 << " ms" << endl;
4375     cout << "RUN DEAD-TIME: " << totdltime[1] * 0.01 << " ms" << endl;
4376     }
4377     // add an entry
4378     if (run_tree_clone && totdltime[2] > 0)
4379     if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
4380     run_tree_clone->GetBranch("DeadLiveTime")->Fill();
4381     // reset counters
4382     if ( totdltime[2] > 0 ){
4383     if (GetTrigLevel2()) {
4384     totdltime[0] = GetTrigLevel2()->dltime[0];//live-time
4385     totdltime[1] = 0; //dead-time
4386     }
4387     totdltime[2] = 1; //event counter
4388     }
4389     }
4390    
4391     if (DBG){
4392     cout << endl << " ))))) UPDATE RUN INFO ((((( @iev " << iev << " run " << GetRunInfo()->ID << " irun " << irun
4393 mocchiut 1.90 << endl;
4394 mocchiut 1.89 printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4395     printf("2 rth %d %d nevents %d \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4396     printf("2 rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4397 mocchiut 1.94 if ( fUseDBinRunInfo ) printf("2 rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4398 mocchiut 1.90 printf("2 bo irunentry %lld prevshift %lld irun %lld \n",irunentry,prevshift,irun);
4399 mocchiut 1.89 printf("2 min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);
4400     }
4401 mocchiut 1.90 // printf("2abstime %lld %lld pktnum %d %d obt %lld \n",abstime,prevabstime,pktnum,prevpktnum,obt);
4402     // printf("2 rth %d %d nevents %d \n",GetRunInfo()->RUNHEADER_TIME,GetRunInfo()->RUNTRAILER_TIME,GetRunInfo()->NEVENTS);
4403     // printf("2 rto %d %d \n",GetRunInfo()->RUNHEADER_OBT,GetRunInfo()->RUNTRAILER_OBT);
4404     // printf("2 rto2 %lld %lld \n",gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT),gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT));
4405     // printf("2 bo irunentry %lld prevshift %lld irun %lld \n",irunentry,prevshift,irun);
4406     // printf("2 min %lld iev %lld max %lld tot %lld \n",totrunentrymin,iev,totrunentrymax,totrunentry);//TOGLITOGLI
4407 mocchiut 1.89
4408     return (true);
4409     } // need for run upgrade
4410     if (DBG) printf("return false\n");
4411     return (false);
4412     }// SELLI = 0 SELLI = 2
4413    
4414     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4415     // if it is a preselected file (there is SelectionList)
4416     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
4417     // irun = run entry relative to the chain
4418     // irunt = run entry relative to the tree
4419     if (SELLI == 1) {
4420     sel_tree->GetEntry(iev);// read irunt from SelectionList
4421     irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW
4422     if (irun != oldrun) {
4423     if (irun < run_tree->GetEntries())
4424     run_tree->GetEntry(irun);
4425     // check if the time is ok (with merged files it is not...)
4426     // if not loop over run and look for the proper entry
4427     bool SECONDO_GIRO = false;
4428     // Long64_t irun_start = irun;
4429     int offset_start = irunoffset[sel_tree->GetTreeNumber()];
4430     while (((!(abstime >= GetRunInfo()->RUNHEADER_TIME && // check on absolute time (s)
4431     abstime <= GetRunInfo()->RUNTRAILER_TIME)
4432     // ||
4433     // !(obt >= GetRunInfo()->RUNHEADER_OBT && // additional check on OBT (ms)
4434     // obt <= GetRunInfo()->RUNTRAILER_OBT)
4435     ) || GetRunInfo()->NEVENTS == 0)
4436     // && irun < run_tree->GetEntries()
4437     ) {
4438    
4439     if (DBG) {
4440     cout << " (test) ";
4441     cout << " tree " << sel_tree->GetTreeNumber();
4442     cout << " irunt " << irunt;
4443     cout << " offset " << irunoffset[sel_tree->GetTreeNumber()];
4444     cout << " abs " << abstime;
4445     cout << " >> " << GetRunInfo()->RUNHEADER_TIME << " " << GetRunInfo()->RUNTRAILER_TIME;
4446     cout << " obt " << obt;
4447     cout << " >> " << GetRunInfo()->RUNHEADER_OBT << " " << GetRunInfo()->RUNTRAILER_OBT;
4448     cout << " *** JUMP RUN *** irun " << irun;
4449     cout << endl;
4450     }
4451     // irun++;
4452     irunoffset[sel_tree->GetTreeNumber()]++;
4453     irun = irunt + irunoffset[sel_tree->GetTreeNumber()];//NEWNEW
4454     if (irun == run_tree->GetEntries() && SECONDO_GIRO) {
4455     // if(irun == irun_start ){
4456     cout << " ...grrrvzzkhhhajsdkj!!!! " << endl;
4457     irunoffset[sel_tree->GetTreeNumber()] = offset_start;
4458     return false;
4459     }
4460     if (irun >= run_tree->GetEntries() || irun < 0) {
4461     cout << "irun = " << irun << " >> search from the beginning... <<" << endl;
4462     SECONDO_GIRO = true;
4463     irun = 0;
4464     irunoffset[sel_tree->GetTreeNumber()] = -irunt;
4465     }
4466     run_tree->GetEntry(irun);
4467     }
4468    
4469     if (DBG) {
4470     cout << " (test) ";
4471     cout << " tree " << sel_tree->GetTreeNumber();
4472     cout << " irunt " << irunt;
4473     cout << " offset " << irunoffset[sel_tree->GetTreeNumber()];
4474     cout << " abs " << abstime;
4475     cout << " >> " << GetRunInfo()->RUNHEADER_TIME << " " << GetRunInfo()->RUNTRAILER_TIME;
4476     cout << " obt " << obt;
4477     cout << " >> " << GetRunInfo()->RUNHEADER_OBT << " " << GetRunInfo()->RUNTRAILER_OBT;
4478     }
4479     if (DBG)
4480     cout << endl;
4481     if (DBG)
4482     cout << endl << " ))))) UPDATE RUN INFO ((((( @iev " << iev << " run " << GetRunInfo()->ID << " (run-entry "
4483     << irun << ")" << endl;
4484     // ----------------------------------------------------
4485     // update the tracker parameters
4486     // (non ho trovato nessun altro modo sicuro di farlo...)
4487     // ----------------------------------------------------
4488 mocchiut 1.94 if ( fUseDBinRunInfo ){
4489     if (!dbc || (dbc && !dbc->IsConnected()))
4490     SetDBConnection();
4491     TrkParams::Set(GetRunInfo(), dbc);
4492     if (dbc){
4493     dbc->Close();
4494     delete dbc;
4495     dbc=0;
4496     }
4497     }
4498 mocchiut 1.89 // cout << endl;
4499     prevshift = 0;
4500 mocchiut 1.90 yprevshift = 0;
4501 mocchiut 1.89 return true;
4502     }
4503     return false;
4504     }
4505    
4506     return false;
4507     //
4508     }
4509    
4510 mocchiut 1.90 /**
4511     * Update the runinfo informations (to be used to have Run infos event by event basis)
4512     * @param run Pointer to the chain/tree which contains run infos
4513     * @return true if a new run has been read, false if it is still the same run
4514     */
4515     Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) {
4516     return (UpdateRunInfo(iev));
4517 pam-fi 1.81 }
4518 mocchiut 1.89
4519 mocchiut 1.17 /**
4520     * Update the runinfo informations (to be used to have Run infos event by event basis)
4521 pam-fi 1.81 * @param run Pointer to the chain/tree which contains run infos
4522 mocchiut 1.17 * @return true if a new run has been read, false if it is still the same run
4523     */
4524 pam-fi 1.81 Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev) {
4525     return (UpdateRunInfo((TChain*) run, iev));
4526     }
4527 mocchiut 1.89
4528 mocchiut 1.17
4529 pam-fi 1.2 //--------------------------------------
4530     //
4531     //
4532     //--------------------------------------
4533     /**
4534 pam-fi 1.8 * Set which trees shoul be analysed
4535 pam-fi 1.81 * @param detlist TString containing the sequence of trees required
4536 pam-fi 1.55 */
4537 pam-fi 1.81 void PamLevel2::SetWhichTrees(TString detlist) {
4538    
4539     // if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){
4540     if (detlist.Contains("+ALL", TString::kIgnoreCase)) {
4541    
4542     cout << " ======================================================== " << endl;
4543     cout << " (( WARNING )) " << endl;
4544     cout << " The meaning of the option +ALL has changed!! " << endl;
4545     cout << " Now it includes really all (level0+level1+level2+gpamela)" << endl;
4546     cout << " and the file is discarded if it does not contain " << endl;
4547     cout << " all trees or if level0 files are not available!! " << endl;
4548     cout << " ======================================================== " << endl;
4549    
4550     CAL0 = true;
4551     CAL1 = true;
4552     CAL2 = true;
4553     TRK2 = true;
4554     TRK1 = true;
4555     TRKh = true;
4556     TRK0 = true;
4557     TRG = true;
4558     TOF = true;
4559     TOF0 = true;
4560     S4 = true;
4561     ND = true;
4562     AC = true;
4563     ORB = true;
4564     GP = true;
4565     }
4566     else if (detlist.Contains("-ALL", TString::kIgnoreCase)) {
4567     CAL0 = false;
4568     CAL1 = false;
4569     CAL2 = false;
4570     TRK2 = false;
4571     TRK1 = false;
4572     TRKh = false;
4573     TRK0 = false;
4574     TRG = false;
4575     TOF = false;
4576     TOF0 = false;
4577     S4 = false;
4578     ND = false;
4579     AC = false;
4580     ORB = false;
4581     GP = false;
4582     };
4583    
4584     // -------------------------------------------------------------------------
4585     if (detlist.Contains("CAL1", TString::kIgnoreCase)) {
4586     if (detlist.Contains("-CAL1", TString::kIgnoreCase))
4587     CAL1 = false;
4588     if (detlist.Contains("+CAL1", TString::kIgnoreCase))
4589     CAL1 = true;
4590     };
4591    
4592     if (detlist.Contains("CAL0", TString::kIgnoreCase)) {
4593     if (detlist.Contains("-CAL0", TString::kIgnoreCase))
4594     CAL0 = false;
4595     if (detlist.Contains("+CAL0", TString::kIgnoreCase))
4596     CAL0 = true;
4597     };
4598    
4599     if (detlist.Contains("CAL2", TString::kIgnoreCase)) {
4600     if (detlist.Contains("-CAL2", TString::kIgnoreCase))
4601     CAL2 = false;
4602     if (detlist.Contains("+CAL2", TString::kIgnoreCase))
4603     CAL2 = true;
4604     };
4605    
4606     if (detlist.Contains("+CAL", TString::kIgnoreCase) && !CAL1 && !CAL2)
4607     CAL2 = true;
4608     if (detlist.Contains("-CAL", TString::kIgnoreCase) && CAL1 && CAL2) {
4609     CAL2 = false;
4610     CAL1 = false;
4611     }
4612     // -------------------------------------------------------------------------
4613     if (detlist.Contains("TRK0", TString::kIgnoreCase)) {
4614     if (detlist.Contains("-TRK0", TString::kIgnoreCase))
4615     TRK0 = false;
4616     if (detlist.Contains("+TRK0", TString::kIgnoreCase))
4617     TRK0 = true;
4618     };
4619    
4620     if (detlist.Contains("TRK1", TString::kIgnoreCase)) {
4621     if (detlist.Contains("-TRK1", TString::kIgnoreCase))
4622     TRK1 = false;
4623     if (detlist.Contains("+TRK1", TString::kIgnoreCase))
4624     TRK1 = true;
4625     };
4626    
4627     if (detlist.Contains("TRK2", TString::kIgnoreCase)) {
4628     if (detlist.Contains("-TRK2", TString::kIgnoreCase))
4629     TRK2 = false;
4630     if (detlist.Contains("+TRK2", TString::kIgnoreCase))
4631     TRK2 = true;
4632     };
4633    
4634     if (detlist.Contains("TRKh", TString::kIgnoreCase)) {
4635     if (detlist.Contains("-TRKh", TString::kIgnoreCase))
4636     TRKh = false;
4637     if (detlist.Contains("+TRKh", TString::kIgnoreCase))
4638     TRKh = true;
4639     };
4640    
4641     if (detlist.Contains("+TRK", TString::kIgnoreCase) && !TRK1 && !TRK2 && !TRKh)
4642     TRK2 = true;
4643     if (detlist.Contains("-TRK", TString::kIgnoreCase) && TRK1 && TRK2 && TRKh) {
4644     TRK2 = false;
4645     TRK1 = false;
4646     TRKh = false;
4647     }
4648     // -------------------------------------------------------------------------
4649 pam-fi 1.20
4650 pam-fi 1.81 if (detlist.Contains("-TRG", TString::kIgnoreCase))
4651     TRG = false;
4652     else if (detlist.Contains("+TRG", TString::kIgnoreCase))
4653     TRG = true;
4654    
4655     if (detlist.Contains("-TOF", TString::kIgnoreCase))
4656     TOF = false;
4657     else if (detlist.Contains("+TOF", TString::kIgnoreCase))
4658     TOF = true;
4659 pam-fi 1.20
4660 pam-fi 1.81 if (detlist.Contains("-TOF0", TString::kIgnoreCase))
4661     TOF0 = false;
4662     else if (detlist.Contains("+TOF0", TString::kIgnoreCase))
4663     TOF0 = true;
4664 pam-fi 1.8
4665 pam-fi 1.81 if (detlist.Contains("-S4", TString::kIgnoreCase))
4666     S4 = false;
4667     else if (detlist.Contains("+S4", TString::kIgnoreCase))
4668     S4 = true;
4669    
4670     if (detlist.Contains("-ND", TString::kIgnoreCase))
4671     ND = false;
4672     else if (detlist.Contains("+ND", TString::kIgnoreCase))
4673     ND = true;
4674    
4675     if (detlist.Contains("-AC", TString::kIgnoreCase))
4676     AC = false;
4677     else if (detlist.Contains("+AC", TString::kIgnoreCase))
4678     AC = true;
4679    
4680     if (detlist.Contains("-ORB", TString::kIgnoreCase))
4681     ORB = false;
4682     else if (detlist.Contains("+ORB", TString::kIgnoreCase))
4683     ORB = true;
4684    
4685     if (detlist.Contains("-GP", TString::kIgnoreCase))
4686     GP = false;
4687     else if (detlist.Contains("+GP", TString::kIgnoreCase))
4688     GP = true;
4689    
4690     cout << "tree/branch list from input --> ";
4691     if (TRK0)
4692     cout << "TRK0 ";
4693     if (TRK1)
4694     cout << "TRK1 ";
4695     if (TRK2)
4696     cout << "TRK2 ";
4697     if (TRKh)
4698     cout << "TRKH ";
4699     if (CAL0)
4700     cout << "CAL0 ";
4701     if (CAL1)
4702     cout << "CAL1 ";
4703     if (CAL2)
4704     cout << "CAL2 ";
4705     if (TOF)
4706     cout << "TOF ";
4707     if (TRG)
4708     cout << "TRG ";
4709     if (AC)
4710     cout << "AC ";
4711     if (ND)
4712     cout << "ND ";
4713     if (S4)
4714     cout << "S4 ";
4715     if (ORB)
4716     cout << "ORB ";
4717     if (GP)
4718     cout << "GP ";
4719     cout << endl;
4720     // cout<< "Set detector list --> ";
4721     // if(TRK1)cout<<"TRK1 ";
4722     // if(TRK2)cout<<"TRK2 ";
4723     // if(TRKh)cout<<"TRKH ";
4724     // if(CAL1)cout<<"CAL1 ";
4725     // if(CAL2)cout<<"CAL2 ";
4726     // if(TOF0)cout<<"TOF0 ";
4727     // if(TOF)cout<<"TOF ";
4728     // if(TRG)cout<<"TRG ";
4729     // if(AC)cout<<"AC ";
4730     // if(ND)cout<<"ND ";
4731     // if(S4)cout<<"S4 ";
4732     // if(ORB)cout<<"ORB ";
4733     // cout << endl;
4734 pam-fi 1.11
4735 pam-fi 1.81 }
4736     ;
4737 pam-fi 1.11
4738     /**
4739     * Set tree/branch detector flags from the content of a tree
4740     */
4741 pam-fi 1.81 void PamLevel2::GetWhichTrees(TFile* f) {
4742 pam-fi 1.20
4743 pam-fi 1.81 // cout << "void PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;
4744     // -----------
4745     // reset flags
4746     // -----------
4747     CAL1 = false;
4748     CAL2 = false;
4749     TRK2 = false;
4750     TRK1 = false;
4751     TRKh = false;
4752     TRG = false;
4753     TOF = false;
4754     S4 = false;
4755     ND = false;
4756     AC = false;
4757     ORB = false;
4758     GP = false;
4759    
4760     RUN = false;
4761    
4762     // cout << "Checking file: "<<f->GetName()<<endl;
4763     if (!f || f->IsZombie()) {
4764     cout << "File: " << f->GetName() << " Non valid root file" << endl;
4765 mocchiut 1.99 fDiscarded = true;
4766 pam-fi 1.81 return;
4767     }
4768    
4769     TList *lk = f->GetListOfKeys();
4770     if (!lk)
4771     return;
4772     TIter next(lk);
4773     TKey *key = 0;
4774    
4775     Int_t nev = 0;
4776    
4777     while ((key = (TKey*) next())) {
4778    
4779     if (!strcmp(key->GetName(), "Run"))
4780     RUN = true;
4781    
4782     //=========================================================
4783     if (!strcmp(key->GetName(), "Trigger")) {
4784     TRG = true;
4785     Int_t nevt = ((TTree*) f->Get("Trigger"))->GetEntries();
4786     if (nev && nevt != nev) {
4787     cout << "File: " << f->GetName() << " Trigger tree has " << nevt << " events instead of " << nev << endl;
4788     TRG = false;
4789     }
4790     else
4791     nev = nevt;
4792     }
4793     //=========================================================
4794     if (!strcmp(key->GetName(), "ToF")) {
4795     TOF = true;
4796     Int_t nevt = ((TTree*) f->Get("ToF"))->GetEntries();
4797     if (nev && nevt != nev) {
4798     cout << "File: " << f->GetName() << " ToF tree has " << nevt << " events instead of " << nev << endl;
4799     TOF = false;
4800     }
4801     else
4802     nev = nevt;
4803     }
4804     //=========================================================
4805     if (!strcmp(key->GetName(), "S4")) {
4806     S4 = true;
4807     Int_t nevt = ((TTree*) f->Get("S4"))->GetEntries();
4808     if (nev && nevt != nev) {
4809     cout << "File: " << f->GetName() << " S4 tree has " << nevt << " events instead of " << nev << endl;
4810     S4 = false;
4811     }
4812     else
4813     nev = nevt;
4814     }
4815     //=========================================================
4816    
4817     if (!strcmp(key->GetName(), "NeutronD")) {
4818     ND = true;
4819     Int_t nevt = ((TTree*) f->Get("NeutronD"))->GetEntries();
4820     if (nev && nevt != nev) {
4821     cout << "File: " << f->GetName() << "NeutronD tree has " << nevt << " events instead of " << nev << endl;
4822     ND = false;
4823     }
4824     else
4825     nev = nevt;
4826     }
4827     //=========================================================
4828     if (!strcmp(key->GetName(), "Anticounter")) {
4829     AC = true;
4830     Int_t nevt = ((TTree*) f->Get("Anticounter"))->GetEntries();
4831     if (nev && nevt != nev) {
4832     cout << "File: " << f->GetName() << " Anticounter tree has " << nevt << " events instead of " << nev << endl;
4833     AC = false;
4834     }
4835     else
4836     nev = nevt;
4837     }
4838     //=========================================================
4839     if (!strcmp(key->GetName(), "OrbitalInfo")) {
4840     ORB = true;
4841     Int_t nevt = ((TTree*) f->Get("OrbitalInfo"))->GetEntries();
4842     if (nev && nevt != nev) {
4843     cout << "File: " << f->GetName() << " OrbitalInfo tree has " << nevt << " events instead of " << nev << endl;
4844     ORB = false;
4845     }
4846     else
4847     nev = nevt;
4848     }
4849     //=========================================================
4850     if (!strcmp(key->GetName(), "Tracker")) {
4851     TTree *T = (TTree*) f->Get("Tracker");
4852     for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
4853     TString name = T->GetListOfBranches()->At(i)->GetName();
4854     if (!name.CompareTo("TrkLevel1"))
4855     TRK1 = true;
4856     if (!name.CompareTo("TrkLevel2"))
4857     TRK2 = true;
4858     if (!name.CompareTo("TrkHough"))
4859     TRKh = true;
4860     };
4861     Int_t nevt = T->GetEntries();
4862     if (nev && nevt != nev) {
4863     cout << "File: " << f->GetName() << " Tracker tree has " << nevt << " events instead of " << nev << endl;
4864     TRK1 = false;
4865     TRK2 = false;
4866     TRKh = false;
4867     }
4868     else
4869     nev = nevt;
4870     // T->Delete();
4871     };
4872     //=========================================================
4873     if (!strcmp(key->GetName(), "Calorimeter")) {
4874     TTree *T = (TTree*) f->Get("Calorimeter");
4875     for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
4876     TString name = T->GetListOfBranches()->At(i)->GetName();
4877     if (!name.CompareTo("CaloLevel1"))
4878     CAL1 = true;
4879     if (!name.CompareTo("CaloLevel2"))
4880     CAL2 = true;
4881     };
4882     Int_t nevt = T->GetEntries();
4883     if (nev && nevt != nev) {
4884     cout << "File: " << f->GetName() << " Calorimeter tree has " << nevt << " events instead of " << nev << endl;
4885     CAL1 = false;
4886     CAL2 = false;
4887     }
4888     else
4889     nev = nevt;
4890     // T->Delete();
4891     };
4892     //=========================================================
4893     if (!strcmp(key->GetName(), "h20")) {
4894     GP = true;
4895     Int_t nevt = ((TTree*) f->Get("h20"))->GetEntries();
4896     if (nev && nevt != nev) {
4897     cout << "File: " << f->GetName() << " h20 tree has " << nevt << " events instead of " << nev << endl;
4898     GP = false;
4899     }
4900     else
4901     nev = nevt;
4902     }
4903 pam-fi 1.20
4904 pam-fi 1.81 };
4905 pam-fi 1.11
4906 pam-fi 1.81 // delete lk;
4907    
4908     cout << "tree/branch list from file --> ";
4909     if (TRK1)
4910     cout << "TRK1 ";
4911     if (TRK2)
4912     cout << "TRK2 ";
4913     if (TRKh)
4914     cout << "TRKH ";
4915     if (CAL1)
4916     cout << "CAL1 ";
4917     if (CAL2)
4918     cout << "CAL2 ";
4919     if (TOF)
4920     cout << "TOF ";
4921     if (TRG)
4922     cout << "TRG ";
4923     if (AC)
4924     cout << "AC ";
4925     if (ND)
4926     cout << "ND ";
4927     if (S4)
4928     cout << "S4 ";
4929     if (ORB)
4930     cout << "ORB ";
4931     if (GP)
4932     cout << "GP ";
4933     cout << endl;
4934 pam-fi 1.12
4935 pam-fi 1.81 return;
4936 pam-fi 1.11
4937 pam-fi 1.81 }
4938     ;
4939 pam-fi 1.11
4940 pam-fi 1.2 //--------------------------------------
4941     //
4942     //
4943     //--------------------------------------
4944     /**
4945 pam-fi 1.3 * Check if a file contains selected Pamela Level2 trees.
4946 pam-fi 1.2 * @param name File name
4947 pam-fi 1.4 * @return true if the file is ok.
4948 pam-fi 1.2 */
4949 pam-fi 1.81 Bool_t PamLevel2::CheckLevel2File(TString name) {
4950    
4951     Bool_t CAL1__ok = false;
4952     Bool_t CAL2__ok = false;
4953     Bool_t TRK2__ok = false;
4954     Bool_t TRK1__ok = false;
4955     Bool_t TRKh__ok = false;
4956     Bool_t TRG__ok = false;
4957     Bool_t TOF__ok = false;
4958     Bool_t S4__ok = false;
4959     Bool_t ND__ok = false;
4960     Bool_t AC__ok = false;
4961     Bool_t ORB__ok = false;
4962     Bool_t GP__ok = false;
4963    
4964     Bool_t RUN__ok = false;
4965    
4966     Bool_t SELLI__ok = false;
4967    
4968     // cout << "Checking file: "<<name<<endl;
4969     TFile *f = new TFile(name.Data());
4970     if (!f || f->IsZombie()) {
4971     cout << "File: " << f->GetName() << " discarded ---- Non valid root file" << endl;
4972 mocchiut 1.98 fDiscarded = true;
4973 pam-fi 1.81 return false;
4974     }
4975     // cout << "Get list of keys: "<<f<<endl;
4976     TList *lk = f->GetListOfKeys();
4977     // lk->Print();
4978     TIter next(lk);
4979     TKey *key = 0;
4980    
4981     Int_t nev = 0;
4982    
4983     while ((key = (TKey*) next())) {
4984    
4985     // cout << key->GetName() << endl;
4986     // cout << key->GetName() << ""<<key->GetClassName()<<endl;
4987     // cout << " Get tree: " << f->Get(key->GetName())<<endl;
4988     // nev_previous = nev;
4989     // cout << " n.entries "<< nev <<endl;
4990     // if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){
4991     // nev = ((TTree*)f->Get(key->GetName()))->GetEntries();
4992     // cout << "File: "<< f->GetName() <<" discarded ---- "<< key->GetName() << " tree: n.entries does not match "<<nev<<" "<<nev_previous<< endl;
4993     // return false;
4994     // };
4995    
4996     //=========================================================
4997     // check if the file
4998    
4999    
5000     if (!strcmp(key->GetName(), "Run"))
5001     RUN__ok = true;
5002    
5003     //=========================================================
5004     if (!strcmp(key->GetName(), "SelectionList")) {
5005     SELLI__ok = true;
5006     if (SELLI == 1) {
5007     Int_t nevt = ((TTree*) f->Get("SelectionList"))->GetEntries();
5008     if (nev && nevt != nev) {
5009     cout << "File: " << f->GetName() << " discarded ---- SelectionList tree has " << nevt
5010     << " events instead of " << nev << endl;
5011 mocchiut 1.98 fDiscarded = true;
5012 pam-fi 1.81 return false;
5013     }
5014     nev = nevt;
5015     }
5016     }
5017    
5018     //=========================================================
5019     if (!strcmp(key->GetName(), "Trigger")) {
5020     TRG__ok = true;
5021     if (TRG) {
5022     Int_t nevt = ((TTree*) f->Get("Trigger"))->GetEntries();
5023     if (nev && nevt != nev) {
5024     cout << "File: " << f->GetName() << " discarded ---- Trigger tree has " << nevt << " events instead of "
5025     << nev << endl;
5026 mocchiut 1.98 fDiscarded = true;
5027 pam-fi 1.81 return false;
5028     }
5029     nev = nevt;
5030     }
5031     }
5032     //=========================================================
5033     if (!strcmp(key->GetName(), "ToF")) {
5034     TOF__ok = true;
5035     if (TOF) {
5036     Int_t nevt = ((TTree*) f->Get("ToF"))->GetEntries();
5037     if (nev && nevt != nev) {
5038     cout << "File: " << f->GetName() << " discarded ---- ToF tree has " << nevt << " events instead of " << nev
5039     << endl;
5040 mocchiut 1.98 fDiscarded = true;
5041 pam-fi 1.81 return false;
5042     }
5043     nev = nevt;
5044     }
5045     }
5046     //=========================================================
5047     if (!strcmp(key->GetName(), "S4")) {
5048     S4__ok = true;
5049     if (S4) {
5050     Int_t nevt = ((TTree*) f->Get("S4"))->GetEntries();
5051     if (nev && nevt != nev) {
5052     cout << "File: " << f->GetName() << " discarded ---- S4 tree has " << nevt << " events instead of " << nev
5053     << endl;
5054 mocchiut 1.98 fDiscarded = true;
5055 pam-fi 1.81 return false;
5056     }
5057     nev = nevt;
5058     }
5059     }
5060     //=========================================================
5061    
5062     if (!strcmp(key->GetName(), "NeutronD")) {
5063     ND__ok = true;
5064     if (ND) {
5065     Int_t nevt = ((TTree*) f->Get("NeutronD"))->GetEntries();
5066     if (nev && nevt != nev) {
5067     cout << "File: " << f->GetName() << " discarded ---- NeutronD tree has " << nevt << " events instead of "
5068     << nev << endl;
5069 mocchiut 1.98 fDiscarded = true;
5070 pam-fi 1.81 return false;
5071     }
5072     nev = nevt;
5073     }
5074     }
5075     //=========================================================
5076     if (!strcmp(key->GetName(), "Anticounter")) {
5077     AC__ok = true;
5078     if (AC) {
5079     Int_t nevt = ((TTree*) f->Get("Anticounter"))->GetEntries();
5080     if (nev && nevt != nev) {
5081     cout << "File: " << f->GetName() << " discarded ---- Anticounter tree has " << nevt << " events instead of "
5082     << nev << endl;
5083 mocchiut 1.98 fDiscarded = true;
5084 pam-fi 1.81 return false;
5085     }
5086     nev = nevt;
5087     }
5088     }
5089     //=========================================================
5090     if (!strcmp(key->GetName(), "OrbitalInfo")) {
5091     ORB__ok = true;
5092     if (ORB) {
5093     Int_t nevt = ((TTree*) f->Get("OrbitalInfo"))->GetEntries();
5094     if (nev && nevt != nev) {
5095     cout << "File: " << f->GetName() << " discarded ---- OrbitalInfo tree has " << nevt << " events instead of "
5096     << nev << endl;
5097 mocchiut 1.98 fDiscarded = true;
5098 pam-fi 1.81 return false;
5099     }
5100     nev = nevt;
5101     }
5102     }
5103     //=========================================================
5104     if (!strcmp(key->GetName(), "Tracker")) {
5105     TTree *T = (TTree*) f->Get("Tracker");
5106     if (TRK1 || TRK2 || TRKh) {
5107     Int_t nevt = T->GetEntries();
5108     if (nev && nevt != nev) {
5109     cout << "File: " << f->GetName() << " discarded ---- Tracker tree has " << nevt << " events instead of "
5110     << nev << endl;
5111 mocchiut 1.98 fDiscarded = true;
5112 pam-fi 1.81 return false;
5113     }
5114     nev = nevt;
5115     }
5116     for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
5117     TString name = T->GetListOfBranches()->At(i)->GetName();
5118     if (!name.CompareTo("TrkLevel1"))
5119     TRK1__ok = true;
5120     if (!name.CompareTo("TrkLevel2"))
5121     TRK2__ok = true;
5122     if (!name.CompareTo("TrkHough"))
5123     TRKh__ok = true;
5124     };
5125     T->Delete();
5126     };
5127     //=========================================================
5128     if (!strcmp(key->GetName(), "Calorimeter")) {
5129     TTree *T = (TTree*) f->Get("Calorimeter");
5130     if (CAL1 || CAL2) {
5131     Int_t nevt = T->GetEntries();
5132     if (nev && nevt != nev) {
5133     cout << "File: " << f->GetName() << " discarded ---- Calorimeter tree has " << nevt << " events instead of "
5134     << nev << endl;
5135 mocchiut 1.98 fDiscarded = true;
5136 pam-fi 1.81 return false;
5137     }
5138     nev = nevt;
5139     }
5140     for (Int_t i = 0; i < T->GetListOfBranches()->GetEntries(); i++) {
5141     TString name = T->GetListOfBranches()->At(i)->GetName();
5142     if (!name.CompareTo("CaloLevel1"))
5143     CAL1__ok = true;
5144     if (!name.CompareTo("CaloLevel2"))
5145     CAL2__ok = true;
5146     };
5147     T->Delete();
5148     };
5149     //=========================================================
5150     if (!strcmp(key->GetName(), "h20")) {
5151     ISGP = true;
5152     GP__ok = true;
5153     if (GP) {
5154     Int_t nevt = ((TTree*) f->Get("h20"))->GetEntries();
5155     if (nev && nevt != nev) {
5156     cout << "File: " << f->GetName() << " discarded ---- h20 tree has " << nevt << " events instead of " << nev
5157     << endl;
5158 mocchiut 1.98 fDiscarded = true;
5159 pam-fi 1.81 return false;
5160     }
5161     nev = nevt;
5162     }
5163     }
5164    
5165     };
5166    
5167     if (SELLI == -1)
5168     SELLI = (Int_t) SELLI__ok;
5169     if (SELLI == 0 && SELLI__ok) {
5170     cout << "File: " << f->GetName() << " discarded ---- found SelectionList (it is not a full-event file)" << endl;
5171 mocchiut 1.98 fDiscarded = true;
5172 pam-fi 1.81 return false;
5173     }
5174     if (SELLI == 1 && !SELLI__ok) {
5175     cout << "File: " << f->GetName() << " discarded ---- SelectionList missing" << endl;
5176 mocchiut 1.98 fDiscarded = true;
5177 pam-fi 1.81 return false;
5178     }
5179 pam-fi 1.8
5180 pam-fi 1.81 // cout << "SELLI "<<SELLI<<endl;
5181    
5182     // cout<< "CheckLevel2File(TString): detector list --> ";
5183     // if(TRK1__ok)cout<<"TRK1 ";
5184     // if(TRK2__ok)cout<<"TRK2 ";
5185     // if(TRKh__ok)cout<<"TRKH ";
5186     // if(CAL1__ok)cout<<"CAL1 ";
5187     // if(CAL2__ok)cout<<"CAL2 ";
5188     // if(TOF__ok)cout<<"TOF ";
5189     // if(TRG__ok)cout<<"TRG ";
5190     // if(AC__ok)cout<<"AC ";
5191     // if(ND__ok)cout<<"ND ";
5192     // if(S4__ok)cout<<"S4 ";
5193     // if(ORB__ok)cout<<"ORB ";
5194     // cout << endl;
5195    
5196    
5197     if (TRK2 && TRK1__ok)
5198     TRK1 = 1;
5199     // ----------------------------------------------------------------------------
5200     // NOTA
5201     // se c'e` il level1, lo devo necessarimente leggere.
5202     // infatti (non ho capito perche`) i cluster vengono letti e allocati in memoria
5203     // comunque, ma non vengono disallocati da PamLevel2::Clear()
5204     // ----------------------------------------------------------------------------
5205    
5206    
5207     if (!RUN__ok) {
5208     cout << "File: " << f->GetName() << " *WARNING* ---- Missing RunInfo tree (NB: RUN infos will not be updated)"
5209     << endl;
5210     RUN = false;
5211     };
5212 pam-fi 1.20
5213 pam-fi 1.81 if (CAL1 && !CAL1__ok) {
5214     cout << "File: " << f->GetName() << " discarded ---- Missing CaloLevel1 branch" << endl;
5215 mocchiut 1.98 fDiscarded = true;
5216 pam-fi 1.81 return false;
5217     };
5218     if (CAL2 && !CAL2__ok) {
5219     cout << "File: " << f->GetName() << " discarded ---- Missing CaloLevel2 branch" << endl;
5220 mocchiut 1.98 fDiscarded = true;
5221 pam-fi 1.81 return false;
5222     };
5223     if (TRK2 && !TRK2__ok) {
5224     cout << "File: " << f->GetName() << " discarded ---- Missing TrkLevel2 branch" << endl;
5225 mocchiut 1.98 fDiscarded = true;
5226 pam-fi 1.81 return false;
5227     };
5228     if (TRK1 && !TRK1__ok) {
5229     cout << "File: " << f->GetName() << " discarded ---- Missing TrkLevel1 branch" << endl;
5230 mocchiut 1.98 fDiscarded = true;
5231 pam-fi 1.81 return false;
5232     };
5233     if (TRKh && !TRKh__ok) {
5234     cout << "File: " << f->GetName() << " discarded ---- Missing TrkHough branch" << endl;
5235 mocchiut 1.98 fDiscarded = true;
5236 pam-fi 1.81 return false;
5237     };
5238     if (ORB && !ORB__ok) {
5239     cout << "File: " << f->GetName() << " discarded ---- Missing ORB tree" << endl;
5240 mocchiut 1.98 fDiscarded = true;
5241 pam-fi 1.81 return false;
5242     };
5243     if (AC && !AC__ok) {
5244     cout << "File: " << f->GetName() << " discarded ---- Missing AC tree" << endl;
5245 mocchiut 1.98 fDiscarded = true;
5246 pam-fi 1.81 return false;
5247     };
5248     if (S4 && !S4__ok) {
5249     cout << "File: " << f->GetName() << " discarded ---- Missing S4 tree" << endl;
5250 mocchiut 1.98 fDiscarded = true;
5251 pam-fi 1.81 return false;
5252     };
5253     if (TOF && !TOF__ok) {
5254     cout << "File: " << f->GetName() << " discarded ---- Missing ToF tree" << endl;
5255 mocchiut 1.98 fDiscarded = true;
5256 pam-fi 1.81 return false;
5257     };
5258 pam-fi 1.8
5259 pam-fi 1.81 if (ND && !ND__ok) {
5260     cout << "File: " << f->GetName() << " discarded ---- Missing ND tree" << endl;
5261 mocchiut 1.98 fDiscarded = true;
5262 pam-fi 1.81 return false;
5263     };
5264     if (TRG && !TRG__ok) {
5265     cout << "File: " << f->GetName() << " discarded ---- Missing Trigger tree" << endl;
5266 mocchiut 1.98 fDiscarded = true;
5267 pam-fi 1.81 return false;
5268     };
5269     if (GP && !GP__ok) {
5270     cout << "File: " << f->GetName() << " discarded ---- Missing h20 tree" << endl;
5271 mocchiut 1.98 fDiscarded = true;
5272 pam-fi 1.81 return false;
5273     };
5274 pam-fi 1.8
5275 pam-fi 1.81 // lk->Delete();
5276     // delete lk;
5277     f->Close();
5278    
5279     // cout<< "CheckLevel2File(TString): detector list --> ";
5280     // if(TRK1)cout<<"TRK1 ";
5281     // if(TRK2)cout<<"TRK2 ";
5282     // if(TRKh)cout<<"TRKH ";
5283     // if(CAL1)cout<<"CAL1 ";
5284     // if(CAL2)cout<<"CAL2 ";
5285     // if(TOF)cout<<"TOF ";
5286     // if(TRG)cout<<"TRG ";
5287     // if(AC)cout<<"AC ";
5288     // if(ND)cout<<"ND ";
5289     // if(S4)cout<<"S4 ";
5290     // if(ORB)cout<<"ORB ";
5291     // if(GP)cout<<"GP ";
5292     // cout << endl;
5293 pam-fi 1.8
5294 pam-fi 1.81 return true;
5295 pam-fi 1.2
5296 pam-fi 1.81 }
5297     ;
5298 pam-fi 1.2
5299 pam-fi 1.9 /**
5300     * Create clone-trees
5301     */
5302 pam-fi 1.81 void PamLevel2::CreateCloneTrees0(TChain *fChain, TFile *ofile) {
5303    
5304     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5305     cout << "Create clones of PAMELA trees " << endl;
5306 pam-fi 1.9
5307 pam-fi 1.81 Int_t i = 0;
5308     pam_tree_clone[i] = fChain->GetTree()->CloneTree(0);
5309     TString name = pam_tree_clone[i]->GetName();
5310     name.Append("_clone");
5311     // pam_tree_clone[i]->SetName(name.Data());
5312     cout << pam_tree_clone[i]->GetName() << endl;
5313     i++;
5314    
5315     TList *li = fChain->GetListOfFriends();
5316     TIter next(li);
5317     TFriendElement* T_friend = 0;
5318     ofile->cd();
5319     while ((T_friend = (TFriendElement*) next())) {
5320     // cout<<T_friend->IsA()->GetName()<<" "<<T_friend->GetName()<<hex << T_friend->GetTree() << dec<<endl;
5321     // cout<<T_friend->GetTree()->GetName()<< endl;
5322     pam_tree_clone[i] = T_friend->GetTree()->CloneTree(0);
5323     pam_tree_clone[i]->SetAutoSave(1000000);
5324     name = pam_tree_clone[i]->GetName();
5325 pam-fi 1.9 name.Append("_clone");
5326 pam-fi 1.81 // pam_tree_clone[i]->SetName(name.Data());
5327     cout << pam_tree_clone[i]->GetName() << endl;
5328 pam-fi 1.9 i++;
5329 pam-fi 1.81 }
5330 pam-fi 1.9
5331 pam-fi 1.81 delete li;
5332 pam-fi 1.9
5333 pam-fi 1.81 cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5334 pam-fi 1.9
5335     }
5336    
5337 pam-fi 1.11 /**
5338     * Create clone-trees
5339     */
5340 pam-fi 1.81 void PamLevel2::CreateCloneTrees(TFile *ofile) {
5341 pam-fi 1.11
5342 pam-fi 1.81 // if the pointer is null, create a default file
5343     if (!run_tree)
5344     return;
5345    
5346     if (!ofile) {
5347     cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root " << endl;
5348     ofile = new TFile("clone-tree.root", "recreate");
5349     }
5350    
5351     ofile->cd();
5352    
5353     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5354     cout << "Create new PAMELA trees " << endl;
5355    
5356     run_tree_clone = new TTree("Run", "PAMELA Level2 data from the GL_RUN table ");
5357     run_tree_clone->Branch("RunInfo", "GL_RUN", GetPointerTo("RunInfo"));
5358     cout << "Run : branch RunInfo" << endl;
5359     run_tree_clone->Branch("SoftInfo", "SoftInfo", GetPointerTo("SoftInfo"));
5360     cout << "Run : branch SoftInfo" << endl;
5361     // ------------------
5362     // replicate run tree
5363     // ------------------
5364     // cout << "----------------------------------------------------"<<endl;
5365     // cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
5366     for (Int_t i = 0; i < run_tree->GetEntries(); i++) {
5367     run_tree->GetEntry(i);
5368     // cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
5369     run_tree_clone->Fill();
5370     }
5371 mocchiut 1.104 //
5372     // replicate processinginfo tree
5373     //
5374 mocchiut 1.107 if ( PROC ){ // EMEMEM
5375 mocchiut 1.101 proc_tree_clone = new TTree("ProcessingInfo","Log of data processing");
5376     proc_tree_clone->Branch("ProcInfo", "ProcInfo", GetPointerTo("ProcInfo"));
5377 mocchiut 1.100 cout << "ProcessingInfo: branch ProcessingInfo" << endl;
5378     // ------------------
5379     // replicate processinginfo tree
5380     // ------------------
5381     // cout << "----------------------------------------------------"<<endl;
5382     // cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
5383     for (Int_t i = 0; i < proc_tree->GetEntries(); i++) {
5384     proc_tree->GetEntry(i);
5385 mocchiut 1.107 // cout << i<< "\t | "<<endl;
5386 mocchiut 1.100 proc_tree_clone->Fill();
5387     }
5388 mocchiut 1.107 if ( SELLI != 2 ){
5389 mocchiut 1.100 proc_obj->runID = 0;
5390     TTimeStamp *dt = new TTimeStamp();
5391     proc_obj->date = dt->AsString();
5392     delete dt;
5393 emocchiutti 1.108 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());
5394     proc_obj->outputFilename = ofile->GetName();
5395 mocchiut 1.100 proc_obj->localDir = gSystem->WorkingDirectory();
5396     proc_obj->uname = gSystem->GetFromPipe("uname -a");
5397 emocchiutti 1.108 if (!dbc || (dbc && !dbc->IsConnected())) SetDBConnection();
5398     proc_obj->DB = Form("mysql://%s/%s",dbc->GetHost(),dbc->GetDB());
5399     dbc->Close();
5400 mocchiut 1.100 proc_tree_clone->Fill();
5401     }
5402     cout << "----------------------------------------------------" << endl;
5403     }
5404 pam-fi 1.81 // ------------------------------------
5405     // add branch with dead and live times
5406     // ------------------------------------
5407     if (SELLI != 2) { // EMILIANO
5408     run_tree_clone->Branch("DeadLiveTime", totdltime, "dltime[3]/l");
5409     cout << "Run : branch DeadLiveTime" << endl;
5410    
5411     sel_tree_clone = new TTree("SelectionList", "List of selected events ");
5412     // sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
5413     sel_tree_clone->Branch("RunEntry", &irunt, "runentry/L");//NEWNEW
5414     sel_tree_clone->Branch("EventEntry", &irunentry, "eventry/L");
5415 mocchiut 1.91 // if ( hasL0EE ) sel_tree_clone->Branch("L0EventEntry", &il0entry, "l0eventry/L");
5416 pam-fi 1.81 };
5417 pam-fi 1.39
5418 pam-fi 1.81 Int_t i = 0;
5419     if (TRK1 || TRK2 || TRKh) {
5420     pam_tree_clone[i] = new TTree("Tracker", "PAMELA tracker level2 data ");
5421     if (TRK1) {
5422     pam_tree_clone[i]->Branch("TrkLevel1", "TrkLevel1", GetPointerTo("TrkLevel1"));
5423     pam_tree_clone[i]->BranchRef();
5424     cout << "Tracker : branch TrkLevel1" << endl;
5425     // cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;
5426     };
5427     if (TRK2) {
5428     pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2", GetPointerTo("TrkLevel2"));
5429     cout << "Tracker : branch TrkLevel2" << endl;
5430     };
5431     if (TRKh) {
5432     pam_tree_clone[i]->Branch("TrkHough", "TrkHough", GetPointerTo("TrkHough"));
5433     cout << "Tracker : branch TrkHough" << endl;
5434     };
5435 pam-ts 1.95 if(NUC){
5436     pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&trk_nuc_obj);
5437     cout << "Tracker : branch TrackNuclei" << endl;
5438     }
5439     if(EXT){
5440     pam_tree_clone[i]->Branch("extAlgFlag",&extAlgFlag,"extAlgFlag/I");
5441     pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&trk_ext_obj);
5442     cout << "Tracker : branch RecoveredTrack" << endl;
5443     if(NUC){
5444     pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&trk_ext_nuc_obj);
5445     cout << "Tracker : branch RecoveredTrackNuclei" << endl;
5446     }
5447     }
5448    
5449 pam-fi 1.81 i++;
5450     }
5451 pam-fi 1.39
5452 pam-fi 1.81 // Calorimeter
5453     if (CAL1 || CAL2) {
5454     pam_tree_clone[i] = new TTree("Calorimeter", "PAMELA calorimeter level2 data ");
5455     if (CAL1) {
5456     pam_tree_clone[i]->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));
5457     cout << "Calorimeter : branch CaloLevel1" << endl;
5458     };
5459     if (CAL2) {
5460     pam_tree_clone[i]->Branch("CaloLevel2", "CaloLevel2", GetPointerTo("CaloLevel2"));
5461     cout << "Calorimeter : branch CaloLevel2" << endl;
5462 mocchiut 1.72 };
5463 pam-ts 1.95 if(NUC){
5464     pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&calo_nuc_obj);
5465     cout << "Calorimeter : branch TrackNuclei" << endl;
5466     }
5467     if(EXT){
5468     pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&calo_ext_obj);
5469     cout << "Calorimeter : branch RecoveredTrack" << endl;
5470     if(NUC){
5471     pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&calo_ext_nuc_obj);
5472     cout << "Calorimeter : branch RecoveredTrackNuclei" << endl;
5473     }
5474     }
5475 pam-fi 1.81 i++;
5476     }
5477 pam-fi 1.20
5478 pam-fi 1.81 // ToF
5479     if (TOF) {
5480     pam_tree_clone[i] = new TTree("ToF", "PAMELA ToF level2 data ");
5481     pam_tree_clone[i]->Branch("ToFLevel2", "ToFLevel2", GetPointerTo("ToFLevel2"));
5482     cout << "ToF : branch ToFLevel2" << endl;
5483 pam-ts 1.95 if(NUC){
5484     pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&tof_nuc_obj);
5485     cout << "ToF : branch TrackNuclei" << endl;
5486     }
5487     if(EXT){
5488     pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&tof_ext_obj);
5489     cout << "ToF : branch RecoveredTrack" << endl;
5490     if(NUC){
5491     pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&tof_ext_nuc_obj);
5492     cout << "ToF : branch RecoveredTrackNuclei" << endl;
5493     }
5494     }
5495 pam-fi 1.81 i++;
5496     };
5497     // Trigger
5498     if (TRG) {
5499     pam_tree_clone[i] = new TTree("Trigger", "PAMELA trigger level2 data ");
5500     pam_tree_clone[i]->Branch("TrigLevel2", "TrigLevel2", GetPointerTo("TrigLevel2"));
5501     cout << "Trigger : branch TrigLevel2" << endl;
5502     i++;
5503     };
5504     // S4
5505     if (S4) {
5506     pam_tree_clone[i] = new TTree("S4", "PAMELA S4 level2 data ");
5507     pam_tree_clone[i]->Branch("S4Level2", "S4Level2", GetPointerTo("S4Level2"));
5508     cout << "S4 : branch S4Level2" << endl;
5509     i++;
5510     };
5511     // Neutron Detector
5512     if (ND) {
5513     pam_tree_clone[i] = new TTree("NeutronD", "PAMELA neutron detector level2 data ");
5514     pam_tree_clone[i]->Branch("NDLevel2", "NDLevel2", GetPointerTo("NDLevel2"));
5515     cout << "NeutronD : branch NDLevel2" << endl;
5516     i++;
5517     };
5518     // Anticounters
5519     if (AC) {
5520     pam_tree_clone[i] = new TTree("Anticounter", "PAMELA anticounter detector level2 data ");
5521     pam_tree_clone[i]->Branch("AcLevel2", "AcLevel2", GetPointerTo("AcLevel2"));
5522     cout << "Anticounter : branch AcLevel2" << endl;
5523     i++;
5524     };
5525     // OrbitalInfo
5526     if (ORB) {
5527     pam_tree_clone[i] = new TTree("OrbitalInfo", "PAMELA orbital info ");
5528     pam_tree_clone[i]->Branch("OrbitalInfo", "OrbitalInfo", GetPointerTo("OrbitalInfo"));
5529     cout << "OrbitalInfo : branch OrbitalInfo" << endl;
5530 pam-ts 1.95 if(NUC){
5531     pam_tree_clone[i]->Branch("TrackNuclei","TClonesArray",&orb_nuc_obj);
5532     cout << "OrbitalInfo : branch TrackNuclei" << endl;
5533     }
5534     if(EXT){
5535     pam_tree_clone[i]->Branch("RecoveredTrack","TClonesArray",&orb_ext_obj);
5536     cout << "OrbitalInfo : branch RecoveredTrack" << endl;
5537     if(NUC){
5538     pam_tree_clone[i]->Branch("RecoveredTrackNuclei","TClonesArray",&orb_ext_nuc_obj);
5539     cout << "OrbitalInfo : branch RecoveredTrackNuclei" << endl;
5540     }
5541     }
5542 pam-fi 1.81 i++;
5543     };
5544     // GPamela
5545     if (GP) {
5546     pam_tree_clone[i] = new TTree("h20", "GPAMELA info ");
5547     pam_tree_clone[i]->Branch("GPamela", "GPamela", GetPointerTo("GPamela"), 32000, 1);//split
5548 pam-fi 1.88 cout << "GPamela : branch GPamela" << endl;
5549 pam-fi 1.81 i++;
5550     };
5551 pam-ts 1.95
5552    
5553 pam-fi 1.81 cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5554 pam-fi 1.9
5555 pam-fi 1.11 }
5556 pam-fi 1.9
5557     /**
5558     * Fill tree (created with CreateCloneTrees)
5559     *
5560     */
5561     //void PamLevel2::FillNewPamTree(TTree *T){
5562 pam-fi 1.81 void PamLevel2::FillCloneTrees() {
5563    
5564     // cout << "PamLevel2::FillCloneTrees()" << irunentry << endl;
5565 pam-fi 1.24
5566 pam-fi 1.81 for (Int_t i = 0; i < NCLONES; i++) {
5567     if (pam_tree_clone[i])
5568     pam_tree_clone[i]->Fill();
5569     }
5570     if (sel_tree_clone)
5571     sel_tree_clone->Fill();
5572 pam-fi 1.21
5573 pam-fi 1.9 }
5574    
5575 pam-fi 1.81 TTree* PamLevel2::GetCloneTree(TString name) {
5576 pam-fi 1.9
5577 pam-fi 1.81 for (Int_t i = 0; i < NCLONES; i++) {
5578     if (pam_tree_clone[i]) {
5579     TString na = pam_tree_clone[i]->GetName();
5580     if (!name.CompareTo(na))
5581     return pam_tree_clone[i];
5582     };
5583     }
5584     if (run_tree_clone) {
5585     TString na = run_tree_clone->GetName();
5586     if (!name.CompareTo(na))
5587     return run_tree_clone;
5588     }
5589     if (sel_tree_clone) {
5590     TString na = sel_tree_clone->GetName();
5591     if (!name.CompareTo(na))
5592     return sel_tree_clone;
5593     }
5594 mocchiut 1.100 if (proc_tree_clone && PROC) {
5595     TString na = proc_tree_clone->GetName();
5596     if (!name.CompareTo(na))
5597     return proc_tree_clone;
5598     }
5599 pam-fi 1.81 return NULL;
5600 pam-fi 1.9
5601     }
5602 pam-fi 1.81 void PamLevel2::WriteCloneTrees() {
5603     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5604     cout << "Write clones of PAMELA trees " << endl;
5605     cout << run_tree_clone->GetName() << endl;
5606     if (SELLI != 2) {// Emiliano
5607     if (run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
5608     run_tree_clone->GetBranch("DeadLiveTime")->Fill();
5609     };
5610     run_tree_clone->Write();
5611     if (SELLI != 2) { //Emiliano
5612     cout << sel_tree_clone->GetName() << endl;
5613     sel_tree_clone->Write();
5614     };
5615     for (Int_t i = 0; i < NCLONES; i++) {
5616     if (pam_tree_clone[i]) {
5617     cout << pam_tree_clone[i]->GetName() << endl;
5618 mocchiut 1.98 pam_tree_clone[i]->Write(pam_tree_clone[i]->GetName(),TObject::kOverwrite);
5619 mocchiut 1.72 };
5620 pam-fi 1.81 }
5621 mocchiut 1.100
5622 mocchiut 1.106 if ( PROC ){//EMEMEMEM
5623 mocchiut 1.100 proc_tree_clone->Write("ProcessingInfo",TObject::kOverwrite);
5624     }
5625 pam-fi 1.81 cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" << endl;
5626 pam-fi 1.9
5627     }
5628    
5629     /**
5630 pam-fi 1.24 * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.
5631 pam-fi 1.9 */
5632 pam-fi 1.81 Int_t PamLevel2::GetEntry(Long64_t iee) {
5633    
5634     if (!pam_tree) {
5635     cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded" << endl;
5636     return 0;
5637     }
5638 mocchiut 1.28
5639 pam-fi 1.81 //
5640     // 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...
5641     //
5642     // if(!run_tree ){
5643     // cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loeaded"<<endl;
5644     // return 0;
5645     // }
5646    
5647     //-------------------------------
5648 mocchiut 1.90 if (!pam_tree->GetEntry(iee)) {
5649 pam-fi 1.81 cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree" << endl;
5650     return 0;
5651     }
5652     //
5653     // ... 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.
5654     // 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
5655     // a problem if you don't check the return code of getentry.
5656     //
5657     if (!RUN || !run_tree) {
5658     if (TRK0 || CAL0 || TOF0 || RUN) { //forse cosi` va bene per tornare 1?
5659     cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded" << endl;
5660 mocchiut 1.77 return 0;
5661 pam-fi 1.33 }
5662 pam-fi 1.81 else {
5663     return 1; //cosi` se non c'e` run esce qua...
5664     }
5665     }
5666    
5667     //-------------------------------
5668 mocchiut 1.90 //
5669     if ( fUpdateRunInfo ) UpdateRunInfo(iee); // Emiliano
5670     if (SELLI == 0 || SELLI == 2) irunentry = iee - runfirstentry;
5671 pam-fi 1.81
5672     return 1;
5673 pam-fi 1.20
5674     }
5675    
5676 pam-fi 1.81 TrkLevel0 *PamLevel2::GetTrkLevel0() {
5677     if (!TRK0)
5678     return NULL;
5679     if (!GetYodaEntry()) {
5680     cout << " Int_t PamLevel2::GetTrkLevel0() -- ERROR -- error reading level0 tree" << endl;
5681     return 0;
5682     }
5683     return trk0_obj;
5684     }
5685     ;
5686     CaloLevel0 *PamLevel2::GetCaloLevel0() {
5687     if (!CAL0)
5688     return NULL;
5689     if (!GetYodaEntry()) {
5690     cout << " Int_t PamLevel2::GetCaloLevel0() -- ERROR -- error reading level0 tree" << endl;
5691     return 0;
5692     }
5693     return calo0_obj;
5694     }
5695     ;
5696 pam-fi 1.40
5697 pam-fi 1.20 /**
5698     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
5699     * Given the run ID (...), if needed it query the DB and load the proper file.
5700     * @return Pointer to the tree
5701     */
5702    
5703 pam-fi 1.81 TTree* PamLevel2::GetYodaTree() {
5704 pam-fi 1.20
5705 pam-fi 1.81 // cout << "TTree* PamLevel2::GetYodaTree( )"<<endl;
5706     //===================================
5707     // check if iroot has changed
5708     //===================================
5709     if (irun < 0) {
5710     cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = " << irun << endl;
5711 mocchiut 1.90 if (DBG) cout << "In order to use this method you have to first load the RunInfo tree "<<endl;
5712 pam-fi 1.81 return NULL;
5713     }
5714 mocchiut 1.90 Int_t irootnew = GetRunInfo()->ID_ROOT_L0;
5715     if (DBG){
5716     cout << "iroot "<<iroot<<endl;
5717     cout << "irootnew "<<irootnew<<endl;
5718     }
5719 pam-fi 1.81
5720     //===================================
5721     // load the level0 file
5722     // (if not already loaded)
5723     //===================================
5724     if (iroot != irootnew || !l0_tree) {
5725     iroot = irootnew;
5726 pam-fi 1.20 //===================================
5727 pam-fi 1.81 // open the DB connection
5728     // (if not already opened)
5729 pam-fi 1.20 //===================================
5730 pam-fi 1.81 if (!dbc || (dbc && !dbc->IsConnected()))
5731     SetDBConnection();
5732     GL_ROOT glroot = GL_ROOT();
5733     if (glroot.Query_GL_ROOT(iroot, dbc)) {
5734     cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = " << iroot << " does not exists"
5735     << endl;
5736     return NULL;
5737     };
5738     TString filename = glroot.PATH + glroot.NAME;
5739     if (l0_file) {
5740     l0_file->Close();
5741     l0_file->Delete();
5742     }
5743     cout << "Opening LEVEL0 file: " << filename << endl;
5744     FileStat_t t;
5745     if (gSystem->GetPathInfo(filename.Data(), t)) {
5746     cout << " PamLevel2::GetYodaTree() -- ERROR opening file " << endl;
5747     return NULL;
5748     }
5749     l0_file = new TFile(filename);
5750     if (!l0_file)
5751     return NULL;
5752     l0_tree = (TTree*) l0_file->Get("Physics");
5753     if (!h0_obj)
5754     h0_obj = new EventHeader();
5755     l0_tree->SetBranchAddress("Header", &h0_obj);
5756 mocchiut 1.90 yprevshift = 0; // yes, yprevshift is the shift in the level0, prevshift is the shift in the level2
5757 pam-fi 1.81 //---------------------------------------------------
5758     // TRACKER:
5759     if (TRK0) {
5760     if (!trk0_obj) {
5761     trk0_obj = new TrkLevel0();
5762     trk0_obj->Set();
5763     };
5764     l0_tree->SetBranchAddress("Tracker", trk0_obj->GetPointerToTrackerEvent());
5765     }
5766     //--------------------------------------------------
5767     // CALORIMETER:
5768     if (CAL0) {
5769     if (!calo0_obj) {
5770     calo0_obj = new CaloLevel0();
5771     calo0_obj->Set();
5772     };
5773     l0_tree->SetBranchAddress("Calorimeter", calo0_obj->GetPointerToCalorimeterEvent());
5774     }
5775     //---------------------------------------------------
5776     // TOF:
5777     if (TOF0) {
5778     cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented " << endl;
5779     }
5780 pam-fi 1.20
5781 pam-fi 1.81 dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
5782 pam-fi 1.87 delete dbc;
5783 mocchiut 1.82 dbc=0;
5784 mocchiut 1.51
5785 pam-fi 1.81 };
5786 mocchiut 1.51
5787 pam-fi 1.81 if (TRK0) {
5788 mocchiut 1.90 if(!dbc || (dbc && !dbc->IsConnected()))SetDBConnection();
5789 pam-fi 1.81 TrkParams::SetCalib(run_obj, dbc);
5790     TrkParams::LoadCalib();
5791     if (!TrkParams::CalibIsLoaded()) {
5792     cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded" << endl;
5793 pam-fi 1.20 };
5794 mocchiut 1.82 if(dbc){
5795     dbc->Close(); // EMILIANO, do not leave open connections, open only when needed
5796 pam-fi 1.87 delete dbc;
5797 mocchiut 1.82 dbc=0;
5798     };
5799 pam-fi 1.81 }
5800     return l0_tree;
5801 pam-fi 1.20 }
5802 pam-fi 1.9
5803 pam-fi 1.20 /**
5804     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
5805     */
5806 pam-fi 1.81 Int_t PamLevel2::GetYodaEntry() {
5807    
5808 mocchiut 1.90 Long64_t iev = this->GetReadEntry();
5809 pam-fi 1.81
5810 mocchiut 1.90 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
5811     // if it is a full file (not preselected)
5812     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
5813 mocchiut 1.91 // if (SELLI == 0 || SELLI == 2 || !hasL0EE) {
5814 pam-fi 1.81
5815 mocchiut 1.90 if (!GetYodaTree()){
5816     printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
5817     return 0;
5818     }
5819 pam-fi 1.81
5820 mocchiut 1.90 if (irunentry < 0) {
5821     if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
5822     // cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
5823     irunentry = 0LL;
5824     }
5825     // ---------------------------------
5826     // if file is NOT a preselected file
5827     // ---------------------------------
5828     Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
5829    
5830     if (DBG){
5831     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
5832     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
5833     cout << " time "<< abstime << endl;
5834     }
5835    
5836     ULong64_t obt = 0;
5837     ULong64_t pktn = 0;
5838     if (GetOrbitalInfo()) {
5839     obt = GetOrbitalInfo()->OBT;
5840     pktn = GetOrbitalInfo()->pkt_num;
5841     }
5842 pam-fi 1.81
5843 mocchiut 1.90 if (!GetOrbitalInfo() && !ISGP) {
5844     cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo " << endl;
5845     return 0;
5846 pam-fi 1.81 }
5847 mocchiut 1.90 if (obt == 0 && pktn == 0 && !ISGP) {
5848     cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? " << endl;
5849 pam-fi 1.81 return 0;
5850     }
5851    
5852 mocchiut 1.90 // ---------------------------------------------------------------------
5853     // ATTENTION!!!
5854     // If data are not pre-processed with cleaner, the level0 tree may contain
5855     // spurious nested physics packets.
5856     // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
5857     // while level2 tree DOES NOT!!
5858     // This means that "quellagiusta" in these cases is not correct.
5859     // In order to retrieve the correct level0 event, I implemented a check
5860     // of the OBT and pkt-number. In case of mismatch, the level0 entry number
5861     // is shift forward until when the packets match.
5862     // ---------------------------------------------------------------------
5863     Long64_t shift = 0LL;
5864     Long64_t answer = quellagiusta + shift + yprevshift;
5865     Int_t readl0 = 0;
5866     readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
5867    
5868     if (DBG){
5869     printf(" siamo qui shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5870     }
5871    
5872    
5873 pam-fi 1.81 if (ISGP) {
5874     obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
5875     pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
5876     }
5877    
5878 mocchiut 1.90 while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){
5879     if ( isSync && shift == 0LL ){
5880     printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found in place!!! \n");
5881     cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5882     cout << "\nFor bug reporting instructions, please see for example:\n";
5883     cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5884     cout << " " << endl;
5885     }
5886     if (shift > 0) {
5887 mocchiut 1.93 if (DBG) cout << " PKTNUM L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
5888 mocchiut 1.90 if (DBG)
5889     cout << " RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
5890     << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
5891     if (DBG)
5892     cout << " L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
5893     << " prevshift " << prevshift << " )" << endl;
5894     }
5895     answer = quellagiusta + shift+ yprevshift;
5896     readl0 = l0_tree->GetEntry(answer);
5897     // printf(" inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5898    
5899     if (!GetEventHeader()) {
5900     cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
5901     return 0;
5902     }
5903 pam-fi 1.81
5904 mocchiut 1.90 //
5905     if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
5906     if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
5907     // printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
5908     yprevshift = 0LL;
5909     shift = -1LL;
5910     };
5911    
5912     shift++;
5913     }
5914    
5915 pam-fi 1.81
5916 mocchiut 1.90 if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
5917     if ( isSync ){
5918     printf(" PamLevel2::GetYodaEntry() ERROR! sync file but the level0 entry not found AT ALL!!! \n");
5919     cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5920     cout << "\nFor bug reporting instructions, please see for example:\n";
5921     cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5922     cout << " " << endl;
5923     }
5924     cout << "Int_t PamLevel2::GetYodaEntry() -- WARNING -- " << endl;
5925     cout << " Big trouble here, no such event in Level0 data! (NB maxshift set to " << maxshift << " )" << endl;
5926     cout << " Nested and/or DarthVader skipped packets in fragmented run? checking and trying to fix " <<endl;
5927     // query the DB for runs containing the event, loop over LEVEL0 files which could contain the level0 event and try to find it
5928     // 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
5929     // connect to db
5930     if (!dbc || (dbc && !dbc->IsConnected())) SetDBConnection(); //Emiliano
5931     //
5932     if (GetOrbitalInfo()){
5933     abstime = GetOrbitalInfo()->absTime;
5934     } else {
5935     printf(" PamLevel2::GetYodaEntry() ERROR! no OrbitalInfo, cannot get the absolute time for event \n");
5936     return 0;
5937     }
5938     // query DB looking for runs containing the processed event
5939     TSQLResult *pResult;
5940     TSQLRow *Row = NULL;
5941     TString myquery = Form("select ID,NEVENTS from GL_RUN where RUNHEADER_TIME<=%lld and RUNTRAILER_TIME>=%lld;",abstime,abstime);
5942     if ( DBG ) printf(" query is %s \n",myquery.Data());
5943     // printf(" query is %s \n",myquery.Data());// TOGLITOGLI
5944     pResult = dbc->Query(myquery.Data());
5945     if (!pResult->GetRowCount()){
5946     printf(" PamLevel2::GetYodaEntry() ERROR! event is not included in any run!!! \n");
5947     cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
5948     cout << "\nFor bug reporting instructions, please see for example:\n";
5949     cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
5950     cout << " " << endl;
5951     return 0;
5952     }
5953     if ( pResult->GetRowCount() == 1 ){
5954     if (DBG) printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");
5955     // printf(" PamLevel2::GetYodaEntry() - WARNING - YodaEntry not found but only one run containing the event, it should not happen \n");//TOGLITOGLI
5956     }
5957     for( Int_t ru=0; ru < pResult->GetRowCount(); ru++){ // loop over runs containing the event
5958     if (Row) delete Row;
5959     Row = pResult->Next();
5960     if( Row == NULL ) break;
5961     UInt_t idrun = (UInt_t)atoll(Row->GetField(0));
5962     UInt_t nev = (UInt_t)atoll(Row->GetField(1));
5963     if (DBG) printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);
5964     // printf(" inside loop over runs: ru %i idrun %i nev %i \n",ru,idrun,nev);//TOGLITOGLI
5965    
5966     // now look for this run in the level2 file, it must be present! code is taken from updateruninfo of course
5967     Bool_t rfound = false;
5968     totrunentry = 0LL;
5969     runfirstentry = 0LL;
5970     for (Int_t r=0; r< run_tree->GetEntries();r++){
5971     run_tree->GetEntry(r);//update runinfo
5972     if ( r > 0 ){
5973     totrunentrymin = totrunentrymax+1;
5974     } else {
5975     totrunentrymin = 0LL;
5976     }
5977     totrunentry += GetRunInfo()->NEVENTS;
5978     totrunentrymax = totrunentry - 1 - prevshift; // prevshift is needed to handle nested+DV_skipped packets
5979     irun = r;
5980    
5981     if (idrun == GetRunInfo()->ID){
5982     if ( totrunentrymin > iev ){ // there is a shift (nested+DV_skipped packets)
5983     if (DBG) printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
5984     if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
5985     // printf("PamLevel2::GetYodaEntry - unconsistent iev - nevents, probable DBL0-L2 async\n");
5986     // printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
5987     prevshift += (totrunentrymin-iev); // add the new shift to total shift
5988     totrunentrymin -= (totrunentrymin-iev); // shift run position min
5989     totrunentrymax -= (totrunentrymin-iev); // shift run position max
5990     if (DBG) printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);
5991     // printf("PamLevel2::GetYodaEntry - totrunentrymin %lld iev %lld prevshift %lld totrunentrymax %lld \n",totrunentrymin,iev,prevshift,totrunentrymax);//TOGLITOGLI
5992     }
5993     runfirstentry = totrunentrymin; // first entry of the run in the level2
5994    
5995 pam-fi 1.81
5996 mocchiut 1.90 //
5997     if (gltsync)
5998     delete gltsync; // Emiliano
5999     if (!dbc || (dbc && !dbc->IsConnected()))
6000     SetDBConnection(); //Emiliano
6001     gltsync = new GL_TIMESYNC(GetRunInfo()->ID_ROOT_L0, "ID", dbc, false); // Emiliano
6002     if (dbc){
6003     dbc->Close(); // Emiliano
6004     delete dbc;
6005     dbc=0;
6006     }
6007     if (gltsync->DBobt(GetRunInfo()->RUNHEADER_OBT) > gltsync->DBobt(GetRunInfo()->RUNTRAILER_OBT)) { // Emiliano
6008     cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun " << irun
6009     << " has RUNHEADER_OBT>=RUNTRAILER_OBT " << endl;
6010     cout
6011     << " (NB!! in this case some events could be assigned to a wrong run)"
6012     << endl;
6013     }
6014     //
6015     if (DBG) printf(" found \n");
6016     // printf(" found \n");//TOGLITOGLI
6017     rfound = true;
6018     //
6019     break;
6020     }
6021     } // loop over run
6022     if ( !rfound ){
6023     printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level2 file!!! \n");
6024     cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
6025     cout << "\nFor bug reporting instructions, please see for example:\n";
6026     cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
6027     cout << " " << endl;
6028     return 0;
6029     }
6030 pam-fi 1.9
6031 mocchiut 1.90 // here we got the first run and we can check if it contains the level0 event
6032     if (!GetYodaTree()){
6033     printf(" PamLevel2::GetYodaEntry() : ERROR no level0 file loaded!\n");
6034     return 0;
6035     }
6036    
6037     // get the current run entry
6038     irunentry = iev - runfirstentry;
6039     if (irunentry < 0) {
6040     if (DBG) cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
6041     // cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl; // TOGLITOGLI
6042     irunentry = 0LL;
6043     }
6044     // ---------------------------------
6045     // if file is NOT a preselected file
6046     // ---------------------------------
6047     quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM); // prevshift already included in irunentry
6048    
6049     if (DBG){
6050     cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
6051     cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
6052     cout << " time "<< abstime << endl;
6053     }
6054     // cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
6055     // cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
6056     // cout << " time "<< abstime << endl; // TOGLITOGLI
6057    
6058     shift = 0;
6059     answer = quellagiusta + shift + yprevshift; // prevshift already included in irunentry
6060     readl0 = l0_tree->GetEntry(answer); // prevshift already included in irunentry
6061    
6062     if (DBG){
6063     printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
6064     }
6065     // printf(" siamo qua shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
6066    
6067     while ( (obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter())) && (quellagiusta + (Long64_t) shift) < GetYodaTree()->GetEntries() && shift < maxshift ){
6068     if (shift > 0) {
6069 mocchiut 1.93 if (DBG) cout << " PKTNUM L2 --- " << pktn << " --- L0 --- " << GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
6070 mocchiut 1.90 if (DBG)
6071     cout << " RUN: ID " << GetRunInfo()->ID << " ID_ROOT_L0 " << run_obj->ID_ROOT_L0 << " ID_RUN_FRAG "
6072     << GetRunInfo()->ID_RUN_FRAG << " EV_FROM " << GetRunInfo()->EV_FROM << endl;
6073     if (DBG)
6074     cout << " L2 <--> L0 mismatch ( irun " << irun << " irunentry " << irunentry << " shift " << shift
6075     << " prevshift " << prevshift << " )" << endl;
6076     }
6077     answer = quellagiusta + shift+ yprevshift;
6078     readl0 = l0_tree->GetEntry(answer);
6079     // printf(" inside inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
6080    
6081     if (!GetEventHeader()) {
6082     cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader " << endl;
6083     return 0;
6084     }
6085     //
6086     if (yprevshift != 0 && (quellagiusta + (Long64_t) shift) == GetYodaTree()->GetEntries()) {
6087     if (DBG) printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);
6088     // printf(" reset inside while shift %lld yprevshift %lld answer %lld \n",shift,yprevshift,answer);//TOGLITOGLI
6089     yprevshift = 0;
6090     shift = -1;
6091     };
6092    
6093     shift++;
6094     }
6095    
6096     if ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) || pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) ){
6097     //still not the good run... continue with the nex one!
6098     printf("still not the good run... continue with the nex one!\n");
6099     } else {
6100     if (DBG) cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
6101     // cout << "LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;//TOGLITOGLI
6102     if (shift > 1) yprevshift = (shift - 1);
6103     if (Row) delete Row;
6104     delete pResult;
6105     if (dbc){
6106     dbc->Close(); // Emiliano
6107     delete dbc;
6108     dbc=0;
6109     }
6110     il0entry = answer;
6111     return readl0;
6112     }
6113     // perhaps it is all
6114     }// loop over runs containing the event
6115     if (Row) delete Row;
6116     delete pResult;
6117     if (dbc){
6118     dbc->Close(); // Emiliano
6119     delete dbc;
6120     dbc=0;
6121     }
6122     // arriving here it means no run found, cannot be! error!
6123     printf(" PamLevel2::GetYodaEntry() ERROR! run is not present in the level0 files!!! \n");
6124     cout << " OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
6125     cout << "\nFor bug reporting instructions, please see for example:\n";
6126     cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
6127     cout << " " << endl;
6128     return 0;
6129     } else {
6130     if (DBG) cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
6131     // cout << "=> LA ENTRY GIUSTA E`: "<<answer<<" (spero...)"<<endl;
6132     // 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()) );
6133     if (shift > 1) yprevshift = (shift - 1);
6134     il0entry = answer;
6135     return readl0;
6136     }
6137    
6138 mocchiut 1.91 /* } // if selli 0 || 2
6139 mocchiut 1.90 if ( SELLI == 1 && hasL0EE ){
6140     sel_tree->GetEntry(iev);
6141     Long64_t answer = il0entry;
6142     Int_t readl0 = 0;
6143     readl0 = l0_tree->GetEntry(answer);
6144     return readl0;
6145 mocchiut 1.91 }*/
6146 mocchiut 1.90 printf(" PamLevel2::GetYodaEntry() ERROR! \n");
6147     cout << " Entry not found! OK this is a bug, write to Emiliano, Emiliano.Mocchiutti@ts.infn.it " << endl;
6148     cout << "\nFor bug reporting instructions, please see for example:\n";
6149     cout << " <http://www.ts.infn.it/~mocchiut/bugs/bugs.html>.\n";
6150     cout << " " << endl;
6151     return 0;
6152 pam-fi 1.20 }
6153 mocchiut 1.90
6154 pam-fi 1.40 /**
6155     * \Brief Set DB connection
6156     */
6157 pam-fi 1.81 Bool_t PamLevel2::SetDBConnection() {
6158 mocchiut 1.30
6159 pam-fi 1.81 // cout << "PamLevel2::SetDBConnection()" << endl;
6160     if (DBG) {
6161     cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
6162     cout << "Connecting to DB" << endl;
6163     cout << "HOST " << host << endl;
6164     cout << "USER " << user << endl;
6165     cout << "PSW " << psw << endl;
6166     }
6167 mocchiut 1.82 Bool_t notconn = true;
6168     Int_t trials = 0;
6169     while ( notconn && trials < 10 ){
6170     // gSystem->Sleep(500);
6171     dbc = TSQLServer::Connect(host.Data(), user.Data(), psw.Data());
6172     //dbc->Connect(host.Data(), user.Data(), psw.Data());
6173     if ( dbc ) notconn = false;
6174     if (DBG) printf("<%i> test connection...\n ",trials);
6175     if (!dbc){
6176     if (DBG) printf(" :( failed, no pointer \n");
6177     notconn = true;
6178     // return false;
6179     };
6180     if (dbc && !dbc->IsConnected()){
6181     if (DBG) printf(" :( failed, no connection \n");
6182     notconn = true;
6183     // return false;
6184     };
6185     trials++;
6186     };
6187     if ( notconn ) return false;
6188     //
6189     if (DBG) printf("=connected!\n");
6190 pam-fi 1.81 stringstream myquery; // EMILIANO
6191     myquery.str(""); // EMILIANO
6192     myquery << "SET time_zone='+0:00'"; // EMILIANO
6193     dbc->Query(myquery.str().c_str()); // EMILIANO
6194 mocchiut 1.82 if ( DBG ) printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
6195 pam-fi 1.81 return true;
6196 mocchiut 1.30
6197 pam-fi 1.40 }
6198 pam-fi 1.45
6199     /**
6200     * \Brief Add a friend to the pamela chain.
6201     * @param cname name of the chain to be added
6202     */
6203    
6204 pam-fi 1.81 TChain* PamLevel2::AddFriend(TString cname) {
6205 pam-fi 1.45
6206 pam-fi 1.81 if (!GetPamTree()) {
6207     cout << " TChain* PamLevel2::AddFriend(TString cname) --- a pamela tree must be created first" << endl;
6208     return NULL;
6209     }
6210 pam-fi 1.45
6211 pam-fi 1.81 TChain *c = new TChain(cname.Data());
6212 pam-fi 1.45
6213 pam-fi 1.81 TIter next(GetPamTree()->GetListOfFiles());
6214     Int_t nf = 0;
6215     TChainElement* element = 0;
6216     while ((element = (TChainElement*) next())) {
6217     c->Add(element->GetTitle());
6218     nf++;
6219     }
6220 pam-fi 1.45
6221 pam-fi 1.81 GetPamTree()->AddFriend(cname.Data());
6222 pam-fi 1.45
6223 pam-fi 1.81 cout << "external chain created and added to pamela friends :" << cname << endl;
6224     cout << "n.files " << nf << endl;
6225 pam-fi 1.45
6226 pam-fi 1.81 return c;
6227 pam-fi 1.45
6228 pam-fi 1.81 }
6229 pam-fi 1.45
6230 pam-fi 1.81 /**
6231     * Returns the current read entry. This method simply returns the result of the call to
6232     * pam_tree->GetReadEntry(), so it is entirely handled by ROOT.
6233     */
6234     Long64_t PamLevel2::GetReadEntry() {
6235     return pam_tree->GetReadEntry();
6236 pam-fi 1.45 }
6237 pam-fi 1.52
6238 pam-fi 1.81 /**
6239     * Sets the sorting method. If the new method is different from the previous, the issorted
6240     * flag is set to false, forcing a new sort the next time GetTrack is called.
6241     * @see GetTrack
6242     */
6243     void PamLevel2::SetSortingMethod(TString how) {
6244     if (howtosort != how) {
6245     issorted = false;
6246 pam-ts 1.95 issorted_new = false;
6247 pam-fi 1.81 }
6248     howtosort = how;
6249     }

  ViewVC Help
Powered by ViewVC 1.1.23