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

Annotation of /PamelaLevel2/src/PamLevel2.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.45 - (hide annotations) (download)
Wed Sep 12 08:14:00 2007 UTC (17 years, 2 months ago) by pam-fi
Branch: MAIN
Changes since 1.44: +160 -4 lines
fixed bug in live/dead time evaluation

1 pam-fi 1.1 #include <PamLevel2.h>
2     //--------------------------------------
3     //
4     //
5     //--------------------------------------
6     /**
7     * Default constructor
8     */
9     PamTrack::PamTrack(){
10 pam-fi 1.13 trk_track = 0;
11     calo_track = 0;
12     tof_track = 0;
13     candeleteobj = 0;
14 pam-fi 1.1 };
15     //--------------------------------------
16     //
17     //
18     //--------------------------------------
19     /**
20     * Constructor
21     */
22     PamTrack::PamTrack(TrkTrack* t, CaloTrkVar* c, ToFTrkVar* o){
23 pam-fi 1.13
24     trk_track = 0;
25     calo_track = 0;
26     tof_track = 0;
27     // if(t)trk_track = new TrkTrack(*t);
28     // if(c)calo_track = new CaloTrkVar(*c);
29     // if(o)tof_track = new ToFTrkVar(*o);
30     if(t)trk_track = t;
31     if(c)calo_track = c;
32     if(o)tof_track = o;
33     candeleteobj = 0;
34    
35 pam-fi 1.1 };
36 pam-fi 1.13 PamTrack::PamTrack(const PamTrack& track){
37    
38     TrkTrack *t = track.trk_track;
39     CaloTrkVar *c = track.calo_track;
40     ToFTrkVar *o = track.tof_track;
41    
42     trk_track = 0;
43     calo_track = 0;
44     tof_track = 0;
45     if(t)trk_track = new TrkTrack(*t);
46     if(c)calo_track = new CaloTrkVar(*c);
47     if(o)tof_track = new ToFTrkVar(*o);
48     candeleteobj = 1;
49    
50     }
51     void PamTrack::Clear(){
52 pam-fi 1.1
53 pam-fi 1.13 // cout << "PamTrack::Clear() "<<candeleteobj<<endl;
54     if(candeleteobj){
55     if(trk_track) trk_track->TrkTrack::Clear();
56     if(calo_track) calo_track->CaloTrkVar::Clear();//???
57     if(tof_track) tof_track->ToFTrkVar::Clear();//???
58     }else{
59     trk_track = 0;
60     calo_track = 0;
61     tof_track = 0;
62     }
63     }
64     void PamTrack::Delete(){
65     // cout << "PamTrack::Delete() "<<candeleteobj<<endl;
66     if(candeleteobj){
67     if(trk_track) {
68     trk_track->TrkTrack::Clear();
69     delete trk_track;
70     }
71     if(calo_track){
72     calo_track->CaloTrkVar::Clear();//???
73     delete calo_track;
74     }
75     if(tof_track){
76     tof_track->ToFTrkVar::Clear();//???
77     delete tof_track;
78     }
79     }else{
80     Clear();
81     }
82     }
83 pam-fi 1.1 //--------------------------------------
84     //
85     //
86     //--------------------------------------
87     /**
88 mocchiut 1.35 * Default Constructor
89 pam-fi 1.1 */
90     PamLevel2::PamLevel2(){
91 mocchiut 1.35 Initialize();
92     };
93    
94     /**
95     * Constructor with given dir, list and detectors
96     */
97     PamLevel2::PamLevel2(TString ddir,TString list,TString detlist){
98     Initialize();
99     GetPamTree(GetListOfLevel2Files(ddir,list),detlist);
100     GetRunTree(GetListOfLevel2Files(ddir,list));
101     };
102    
103     /**
104     * Constructor with given dir and list
105     */
106     PamLevel2::PamLevel2(TString ddir,TString list){
107     Initialize();
108     GetPamTree(GetListOfLevel2Files(ddir,list),"");
109     GetRunTree(GetListOfLevel2Files(ddir,list));
110     };
111    
112    
113     void PamLevel2::Initialize(){
114 pam-fi 1.2
115 pam-fi 1.20 h0_obj = 0;
116     trk0_obj = 0;
117 mocchiut 1.30 calo0_obj = 0;
118 pam-fi 1.20
119 pam-fi 1.13 trk2_obj = 0;
120     trk1_obj = 0;
121     trkh_obj = 0;
122     calo1_obj = 0;
123     calo2_obj = 0;
124     tof_obj = 0;
125     trig_obj = 0;
126     s4_obj = 0;
127     nd_obj = 0;
128     ac_obj = 0;
129     orb_obj = 0;
130    
131     run_obj = 0;//new GL_RUN();
132 mocchiut 1.15 soft_obj = 0;// Emiliano
133 pam-fi 1.33 irun = -1LL;
134     runfirstentry = 0LL;
135     runlastentry = 0LL;
136 pam-fi 1.9
137 pam-fi 1.20 l0_file = NULL;
138     l0_tree = NULL;
139     iroot = -1;
140     dbc = 0;
141 mocchiut 1.30
142     prevshift = 0;
143 pam-fi 1.20
144     run_tree = NULL;
145     run_tree_clone = NULL;
146     sel_tree = NULL;
147     sel_tree_clone = NULL;
148    
149 pam-fi 1.33 irunentry = -1LL;
150 pam-fi 1.20 pam_tree = NULL;
151     for(Int_t i=0; i<NCLONES; i++ )pam_tree_clone[i]=NULL;
152    
153 pam-fi 1.45 totdltime[0] = 0LL;
154     totdltime[1] = 0LL;
155    
156 pam-fi 1.20 host = "mysql://localhost/pamelaprod";
157     user = "anonymous";
158     psw = "";
159     const char *pamdbhost=gSystem->Getenv("PAM_DBHOST");
160     const char *pamdbuser=gSystem->Getenv("PAM_DBUSER");
161     const char *pamdbpsw=gSystem->Getenv("PAM_DBPSW");
162     if ( !pamdbhost ) pamdbhost = "";
163     if ( !pamdbuser ) pamdbuser = "";
164     if ( !pamdbpsw ) pamdbpsw = "";
165     if ( strcmp(pamdbhost,"") ) host = pamdbhost;
166     if ( strcmp(pamdbuser,"") ) user = pamdbuser;
167     if ( strcmp(pamdbpsw,"") ) psw = pamdbpsw;
168    
169    
170 pam-fi 1.9
171 pam-fi 1.13 // sorted_tracks = 0;//new TRefArray();
172 pam-fi 1.6
173 pam-fi 1.20 CAL0 = false;
174 pam-fi 1.9 CAL1 = true;
175 pam-fi 1.8 CAL2 = true;
176     TRK2 = true;
177     TRK1 = false;
178     TRKh = false;
179 pam-fi 1.20 TRKh = false;
180     TRG = true;
181     TOF = true;
182     TOF0 = false;
183     S4 = true;
184     ND = true;
185     AC = true;
186     ORB = true;
187 pam-fi 1.6
188 pam-fi 1.20 RUN = true;
189    
190     SELLI = -1;
191 pam-fi 1.8
192 pam-fi 1.43 ISGP = false;
193    
194 pam-fi 1.13 tsorted=0;
195     timage=0;
196 mocchiut 1.37
197     howtosort = "+CAL+TOF";
198     //howtosort = "+TOF";
199     sortthr = 100.;
200 pam-fi 1.13
201 pam-fi 1.2 };
202     /**
203 pam-fi 1.13 * Delete the event (act as Dtor)
204 pam-fi 1.2 */
205 pam-fi 1.13 void PamLevel2::Delete(){
206    
207     if(run_obj)delete run_obj;
208 mocchiut 1.15 if(soft_obj)delete soft_obj; //Emiliano
209 pam-fi 1.13
210     // cout << "void PamLevel2::Clear()"<<endl;
211 pam-fi 1.20 if(h0_obj) delete h0_obj;
212     if(trk0_obj) delete trk0_obj;
213 mocchiut 1.30 if(calo0_obj) delete calo0_obj;
214 pam-fi 1.13 if(trk1_obj) delete trk1_obj;
215     if(trk2_obj) delete trk2_obj;
216     if(trkh_obj) delete trkh_obj;
217     if(calo1_obj)delete calo1_obj;
218     if(calo2_obj)delete calo2_obj;
219     if(tof_obj) delete tof_obj;
220     if(trig_obj) delete trig_obj;
221     if(s4_obj) delete s4_obj;
222     if(nd_obj) delete nd_obj;
223     if(ac_obj) delete ac_obj;
224     if(orb_obj) delete orb_obj;
225    
226     if(tsorted){
227     tsorted->Delete();
228     delete tsorted;
229     }
230     if(timage){
231     timage->Delete();
232     delete timage;
233     }
234 pam-fi 1.20
235     if(dbc){
236     dbc->Close();
237     delete dbc;
238     }
239    
240     if(l0_file)l0_file->Close();
241 mocchiut 1.35 // if(pam_tree)pam_tree->Delete();;
242    
243     if ( pam_tree ){
244     //
245     // 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...
246     //
247     TList *temp = pam_tree->GetListOfFriends();
248     TList *contents = new TList; // create chain friend list
249     contents->SetOwner();
250     TIter next(temp);
251     TChain *questo = 0;
252     while ( (questo = (TChain*) next()) ){
253     TString name = questo->GetName();
254     contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
255     };
256     //
257     // deleting the main chain
258     //
259     pam_tree->Delete();
260     //
261     // deleting the friends...
262     //
263     TIter next2(contents);
264     TChain *questa = 0;
265     while ( questa = (TChain*)next2() ){
266     TString name = questa->GetName();
267     questa->Delete();
268     questa=NULL;
269     };
270     //
271     };
272     pam_tree = NULL;
273    
274 pam-fi 1.20 if(run_tree)run_tree->Delete();;
275     if(sel_tree)sel_tree->Delete();;
276     for(Int_t i=0; i<NCLONES; i++ )if(pam_tree_clone[i])pam_tree_clone[i]->Delete();;
277     if(run_tree_clone)run_tree_clone->Delete();;
278     if(sel_tree_clone)sel_tree_clone->Delete();;
279    
280    
281 pam-fi 1.2 };
282 pam-fi 1.13
283 pam-fi 1.2 /**
284 pam-fi 1.13 * Clear the event (NB! does not deallocate objects)
285 pam-fi 1.2 */
286     void PamLevel2::Clear(){
287 pam-fi 1.3
288 pam-fi 1.13 // cout << "void PamLevel2::Clear()"<<endl;
289 mocchiut 1.15
290     //
291 mocchiut 1.17 // 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
292     // want to load them for each event even if they are the same...
293     //
294 mocchiut 1.15 // if(run_obj)delete run_obj;
295 mocchiut 1.17 // if(run_obj) run_obj->Clear(); // Emiliano: Do not deallocate run_obj here, it will give segmentation fault! call clear instead
296     // if(soft_obj) soft_obj->Clear();
297 pam-fi 1.13
298 pam-fi 1.20 if(h0_obj) h0_obj->Clear();
299 pam-fi 1.33 // if(trk0_obj) trk0_obj->Clear();
300 pam-fi 1.13 if(trk1_obj) trk1_obj->Clear();
301 mocchiut 1.35 if(trk2_obj) trk2_obj->Clear();
302 pam-fi 1.13 if(trkh_obj) trkh_obj->Clear();
303 pam-fi 1.40 if(calo0_obj) calo0_obj->Clear();
304 pam-fi 1.13 if(calo1_obj)calo1_obj->Clear();
305     if(calo2_obj)calo2_obj->Clear();
306     if(tof_obj) tof_obj->Clear();
307     if(trig_obj) trig_obj->Clear();
308     if(s4_obj) s4_obj->Clear();
309     if(nd_obj) nd_obj->Clear();
310     if(ac_obj) ac_obj->Clear();
311     if(orb_obj) orb_obj->Clear();
312 pam-fi 1.6
313 pam-fi 1.13 // if(sorted_tracks)sorted_tracks->Clear();
314     // sorted_tracks.Clear();
315    
316     if(tsorted){
317     tsorted->Delete();
318     }
319     if(timage){
320     timage->Delete();
321     }
322 mocchiut 1.35 };
323    
324     void PamLevel2::Reset(){
325     //
326     // First of all clear everything
327     //
328     Clear();
329     //
330     // close and reset chains and pointers
331     //
332     if ( pam_tree ){
333     //
334     // 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...
335     //
336     TList *temp = pam_tree->GetListOfFriends();
337     TList *contents = new TList; // create chain friend list
338     contents->SetOwner();
339     TIter next(temp);
340     TChain *questo = 0;
341     while ( (questo = (TChain*) next()) ){
342     TString name = questo->GetName();
343     contents->Add((TChain*)gROOT->FindObject(name.Data()));// add object to the list
344     };
345     //
346     // deleting the main chain
347     //
348     pam_tree->Delete();
349     //
350     // deleting the friends...
351     //
352     TIter next2(contents);
353     TChain *questa = 0;
354     while ( questa = (TChain*)next2() ){
355     TString name = questa->GetName();
356     questa->Delete();
357     questa=NULL;
358     };
359     //
360     };
361     pam_tree = NULL;
362     //
363     if(run_tree)run_tree->Delete();;
364     run_tree = NULL;
365     if(sel_tree)sel_tree->Delete();;
366     sel_tree = NULL;
367     //
368     // Close file
369     //
370     if(l0_file)l0_file->Close("R");
371     l0_file = NULL;
372     //
373     h0_obj = 0;
374     trk0_obj = 0;
375     calo0_obj = 0;
376     //
377     trk2_obj = 0;
378     trk1_obj = 0;
379     trkh_obj = 0;
380     calo1_obj = 0;
381     calo2_obj = 0;
382     tof_obj = 0;
383     trig_obj = 0;
384     s4_obj = 0;
385     nd_obj = 0;
386     ac_obj = 0;
387     orb_obj = 0;
388     //
389     // Reset run pointers
390     //
391     run_obj = 0;//new GL_RUN();
392     soft_obj = 0;// Emiliano
393     irun = -1;
394     runfirstentry = 0ULL;
395     runlastentry = 0ULL;
396     //
397 pam-fi 1.45 totdltime[0] = 0LL;
398     totdltime[1] = 0LL;
399     //
400 pam-fi 1.1 };
401 pam-fi 1.2
402 mocchiut 1.38 Bool_t PamLevel2::IsGood(){
403     Bool_t goodev=true;
404     // if(trk2_obj && trk2_obj->UnpackError() != 0 ) goodev = false;
405     if(calo2_obj && calo2_obj->good != 1) goodev = false;
406     if(tof_obj && tof_obj->unpackError != 0) goodev = false;
407     if(trig_obj && trig_obj->unpackError != 0) goodev = false;
408     if(s4_obj && s4_obj->unpackError != 0) goodev = false;
409     if(nd_obj && nd_obj->unpackError != 0) goodev = false;
410     if(ac_obj && ac_obj->unpackError != 255) goodev = false;
411     // if(orb_obj)
412     return goodev;
413     };
414 pam-fi 1.2
415 pam-fi 1.1 //--------------------------------------
416     //
417     //
418     //--------------------------------------
419 pam-fi 1.9 void *PamLevel2::GetPointerTo(const char* c ){
420    
421     TString objname = c;
422 pam-fi 1.13
423     if(!objname.CompareTo("TrkLevel1")) {
424 pam-fi 1.14 if(!trk1_obj){
425     trk1_obj = new TrkLevel1();
426     trk1_obj->Set();
427     }
428 pam-fi 1.13 return &trk1_obj;
429     };
430     if(!objname.CompareTo("TrkLevel2")) {
431 pam-fi 1.14 if(!trk2_obj){
432     trk2_obj = new TrkLevel2();
433     trk2_obj->Set();
434     }
435 pam-fi 1.13 return &trk2_obj;
436     };
437     if(!objname.CompareTo("TrkHough")) {
438     if(!trkh_obj) trkh_obj = new TrkHough();
439     return &trkh_obj;
440     };
441     if(!objname.CompareTo("CaloLevel1")) {
442     if(!calo1_obj) calo1_obj = new CaloLevel1();
443     return &calo1_obj;
444     };
445     if(!objname.CompareTo("CaloLevel2")) {
446 pam-fi 1.14 if(!calo2_obj){
447     calo2_obj = new CaloLevel2();
448     calo2_obj->Set();
449     };
450 pam-fi 1.13 return &calo2_obj;
451     };
452     if(!objname.CompareTo("ToFLevel2")) {
453 pam-fi 1.14 if(!tof_obj){
454     tof_obj = new ToFLevel2();
455     tof_obj->Set();
456     }
457 pam-fi 1.13 return &tof_obj;
458     };
459     if(!objname.CompareTo("TrigLevel2")) {
460     if(!trig_obj) trig_obj = new TrigLevel2();
461     return &trig_obj;
462     };
463     if(!objname.CompareTo("S4Level2")) {
464     if(!s4_obj) s4_obj = new S4Level2();
465     return &s4_obj;
466     };
467     if(!objname.CompareTo("NDLevel2")) {
468     if(!nd_obj) nd_obj = new NDLevel2();
469     return &nd_obj;
470     };
471     if(!objname.CompareTo("AcLevel2")) {
472     if(!ac_obj) ac_obj = new AcLevel2();
473     return &ac_obj;
474     };
475     if(!objname.CompareTo("OrbitalInfo")){
476     if(!orb_obj) orb_obj = new OrbitalInfo();
477     return &orb_obj;
478     };
479    
480 pam-fi 1.9 if(!objname.CompareTo("RunInfo"))return &run_obj;
481 mocchiut 1.15
482     if(!objname.CompareTo("SoftInfo"))return &soft_obj; // Emiliano
483    
484 pam-fi 1.9 return NULL;
485     };
486     //--------------------------------------
487     //
488     //
489     //--------------------------------------
490 pam-fi 1.1 /**
491 pam-fi 1.3 * Retrieves the calorimeter track matching the seqno-th tracker stored track.
492     * (If seqno = -1 retrieves the self-trigger calorimeter track)
493     */
494     CaloTrkVar *PamLevel2::GetCaloStoredTrack(int seqno){
495 pam-fi 1.13
496     if( !calo2_obj )return 0;
497    
498     if( calo2_obj->CaloLevel2::ntrk()==0 ){
499 pam-fi 1.6 cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no Calorimeter tracks are stored"<<endl;
500     return NULL;
501     };
502    
503     CaloTrkVar *c = 0;
504     Int_t it_calo=0;
505    
506     do{
507 pam-fi 1.13 c = calo2_obj->CaloLevel2::GetCaloTrkVar(it_calo);
508 pam-fi 1.6 it_calo++;
509 pam-fi 1.13 } while( c && seqno != c->trkseqno && it_calo < calo2_obj->CaloLevel2::ntrk());
510 pam-fi 1.6
511     if(!c || seqno != c->trkseqno){
512     c = 0;
513     if(seqno!=-1)cout << "PamLevel2::GetCaloStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match Calorimeter stored tracks"<<endl;
514     };
515     return c;
516    
517 pam-fi 1.3 };
518     //--------------------------------------
519     //
520     //
521     //--------------------------------------
522     /**
523     * Retrieves the ToF track matching the seqno-th tracker stored track.
524     * (If seqno = -1 retrieves the tracker-independent tof track)
525     */
526     ToFTrkVar *PamLevel2::GetToFStoredTrack(int seqno){
527    
528 pam-fi 1.13 if( !tof_obj )return 0;
529    
530     if( tof_obj->ToFLevel2::ntrk()==0 ){
531 pam-fi 1.6 cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" but no ToF tracks are stored"<<endl;
532     return NULL;
533     };
534    
535     ToFTrkVar *c = 0;
536     Int_t it_tof=0;
537    
538     do{
539 pam-fi 1.13 c = tof_obj->ToFLevel2::GetToFTrkVar(it_tof);
540 pam-fi 1.6 it_tof++;
541 pam-fi 1.13 } while( c && seqno != c->trkseqno && it_tof < tof_obj->ToFLevel2::ntrk());
542 pam-fi 1.6
543     if(!c || seqno != c->trkseqno){
544     c = 0;
545     if(seqno!=-1)cout << "PamLevel2::GetToFStoredTrack(int) : requested tracker SeqNo "<< seqno <<" does not match ToF stored tracks"<<endl;
546     };
547     return c;
548    
549 pam-fi 1.3 };
550    
551     //--------------------------------------
552     //
553     //
554     //--------------------------------------
555     /**
556     * Give the pamela track associated to a tracker track, retrieving related calorimeter and tof track information.
557     */
558     PamTrack* PamLevel2::GetPamTrackAlong(TrkTrack* t){
559 pam-fi 1.13
560     cout <<"PamLevel2::GetPamTrackAlong(TrkTrack* t) **obsolete** "<<endl;
561     cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;
562    
563 pam-fi 1.6 CaloTrkVar *c = 0;
564     ToFTrkVar *o = 0;
565    
566 pam-fi 1.8 if(CAL2) c = GetCaloStoredTrack(t->GetSeqNo());
567 pam-fi 1.6 if(TOF) o = GetToFStoredTrack(t->GetSeqNo());
568    
569 pam-fi 1.3 // if(t && c && o)track = new PamTrack(t,c,o);
570 pam-fi 1.6 PamTrack *track = new PamTrack(t,c,o);
571    
572     return track;
573 pam-fi 1.3
574     };
575     //--------------------------------------
576     //
577     //
578     //--------------------------------------
579     /**
580 pam-fi 1.1 * Retrieves the it-th stored track.
581     * It override TrkLevel2::GetTrack(int it).
582     * @param itrk Track number, ranging from 0 to GetNTracks().
583     */
584    
585     PamTrack* PamLevel2::GetStoredTrack(Int_t itrk){
586    
587 pam-fi 1.13 cout <<"PamLevel2::GetStoredTrack(Int_t itrk) **to-be-updated** "<<endl;
588     cout <<"for the moment, better use separately the methods: TrkLevel2::GetStoredTrack(seqno) CaloLevel2::GetCaloTrkVar(Int_t notrack) ToFLevel2::GetToFTrkVar(Int_t notrack)"<<endl;
589     cout <<"(if you use it, remember to delete the PamTrack object)"<<endl;
590 pam-fi 1.6 PamTrack *track = 0;
591    
592 pam-fi 1.13 if( itrk >=0 && itrk < trk2_obj->TrkLevel2::ntrk() ){
593 pam-fi 1.3
594 pam-fi 1.13 TrkTrack *t = trk2_obj->TrkLevel2::GetStoredTrack(itrk);
595 pam-fi 1.6 track = GetPamTrackAlong(t);
596 pam-fi 1.2
597 pam-fi 1.13
598    
599 pam-fi 1.6 }else{
600 pam-fi 1.13 cout << "PamLevel2::GetStoredTrack(int) : tracker track SeqNo "<< itrk <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
601 pam-fi 1.6 };
602    
603     return track;
604    
605 pam-fi 1.1 }
606     //--------------------------------------
607     //
608 mocchiut 1.37
609     /**
610     * Sort physical (tracker) tracks and stores them in the TRefArray (of TrkTrack objects) which pointer is PamLevel2::sorted_tracks. Left here as backward compatibility method.
611     **/
612     void PamLevel2::SortTracks(TString how){
613     printf(" WARNING! obsolete, use SortTracks() and SetSortingMethod(TString) instead! \n Setting sorting method to %s \n",how.Data());
614     howtosort = how;
615     SortTracks();
616     };
617    
618 pam-fi 1.1 //
619     //--------------------------------------
620     /**
621 pam-fi 1.3 * Sort physical (tracker) tracks and stores them in the TRefArray (of TrkTrack objects) which pointer is PamLevel2::sorted_tracks.
622     * @param how String to set the sorting cryterium (es: "CAL" or "TRK+CAL+TOF" ecc...).
623     * Sorting cryteria:
624 pam-fi 1.8 * TRK: lower chi**2
625 pam-fi 1.3 * CAL: lower Y spatial residual on the first calorimeter plane
626     * TOF: bigger numebr of hit PMTs along the track, on S12 S21 S32 (where paddles are along the Y axis).
627     * The default sorting cryterium is "TOF+CAL".
628     *
629     * 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).
630 pam-fi 1.1 */
631 mocchiut 1.37 void PamLevel2::SortTracks(){
632     TString how = howtosort;
633 pam-fi 1.1
634 mocchiut 1.37 // cout <<" PamLevel2::SortTracks(TString how) "<<endl;
635     if( !trk2_obj ){
636     cout << "void PamLevel2::SortTracks(): TrkLevel2 not loaded !!!";
637     return;
638     };
639     // cout << "call SortTracs() "<<endl;
640 pam-fi 1.4 //Save current Object count
641 mocchiut 1.37 Int_t ObjectNumber = TProcessID::GetObjectCount();
642 pam-fi 1.9
643 mocchiut 1.37 // cout << "ObjectNumber "<<ObjectNumber <<endl;
644 pam-fi 1.13
645 mocchiut 1.37 // if(!sorted_tracks)sorted_tracks = new TRefArray();
646     // sorted_tracks->Clear();
647     // sorted_tracks.Clear();
648 pam-fi 1.13
649 mocchiut 1.37 if(!tsorted)tsorted = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
650     tsorted->Delete();
651     TClonesArray &ttsorted = *tsorted;
652     if(!timage)timage = new TClonesArray("PamTrack",trk2_obj->GetNTracks());
653     timage->Delete();
654     TClonesArray &ttimage = *timage;
655 pam-fi 1.13
656    
657 pam-fi 1.41
658     //--------------------------------------------------
659     // retrieve sorting method
660     //--------------------------------------------------
661 mocchiut 1.37 Bool_t use_TRK = how.Contains("TRK", TString::kIgnoreCase);
662     Bool_t use_CAL = how.Contains("CAL", TString::kIgnoreCase);
663     Bool_t use_TOF = how.Contains("TOF", TString::kIgnoreCase);
664 pam-fi 1.6
665 mocchiut 1.37 if( !CAL2 && use_CAL) use_CAL = false;
666     if( !TOF && use_TOF) use_TOF = false;
667 pam-fi 1.6
668 mocchiut 1.37 if( !TRK2 ){
669     // cout << "SortTracks() : without tracker does not work!!! (not yet)" << endl;
670     return;
671     };
672 pam-fi 1.9
673 mocchiut 1.37 // cout << "use_CAL "<<use_CAL<<" use_TOF "<<use_TOF<<" use_TRK "<<use_TRK <<endl;
674 pam-fi 1.41
675    
676     //--------------------------------------------------
677     // loop over "physical" tracks sorted by the tracker
678     //--------------------------------------------------
679 mocchiut 1.37 for(Int_t i=0; i < trk2_obj->TrkLevel2::GetNTracks(); i++){
680 pam-fi 1.4
681 mocchiut 1.37 TrkTrack *ts = 0;
682     CaloTrkVar *cs = 0;
683     ToFTrkVar *os = 0;
684    
685     // get tracker tracks
686 pam-fi 1.41 TrkTrack *tp = trk2_obj->TrkLevel2::GetTrack(i); //tracker
687 mocchiut 1.37 CaloTrkVar *cp = GetCaloStoredTrack(tp->GetSeqNo());
688     ToFTrkVar *op = GetToFStoredTrack(tp->GetSeqNo());
689    
690 pam-fi 1.41 TrkTrack *ti = 0; //tracker (image)
691 mocchiut 1.37 CaloTrkVar *ci = 0;
692     ToFTrkVar *oi = 0;
693     // cout << "trk track n. "<<i << " "<<hex<< tp <<dec<< endl;
694     // if track has an image, check image selection
695     if(tp->HasImage()){
696 pam-fi 1.6
697 mocchiut 1.37 ti = trk2_obj->TrkLevel2::GetTrackImage(i); //tracker (image)
698     ci = GetCaloStoredTrack(ti->GetSeqNo());
699     oi = GetToFStoredTrack(ti->GetSeqNo());
700 pam-fi 1.6
701 mocchiut 1.37 // cout << "its image "<<i << " "<<hex<< ti <<dec<< endl;
702 pam-fi 1.9
703 mocchiut 1.37 //assign starting scores
704     Int_t tp_score = 0; //main track sorted by the tracker
705     Int_t ti_score = 0; //image track
706 pam-fi 1.6
707 pam-fi 1.41 // -----------------------------------------------------------------------------------------
708 mocchiut 1.37 // calorimeter check
709 pam-fi 1.41 // -----------------------------------------------------------------------------------------
710 mocchiut 1.37 // check the Y spatial residual on the first calorimeter plane
711     // (cut on calorimeter variables from Emiliano)
712     if( use_CAL && !calo2_obj ){
713     cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but CaloLevel2 not loaded !!!";
714     return;
715     };
716 pam-fi 1.45 if( use_CAL && !cp && ci )ti_score++;
717     if( use_CAL && cp && !ci )tp_score++;
718 mocchiut 1.37 if(
719     use_CAL &&
720 pam-fi 1.41 // calo2_obj->npcfit[1] > 5 && //no. of fit planes on Y view
721     // calo2_obj->varcfit[1] < 1000. && //fit variance on Y view
722 mocchiut 1.37 cp && ci &&
723     true){
724 pam-fi 1.9
725 pam-fi 1.6
726 pam-fi 1.41 // Float_t resy_p = cp->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_p < 0)resy_p= - resy_p;
727     // Float_t resy_i = ci->tbar[0][1] - calo2_obj->cbar[0][1]; if(resy_i < 0)resy_i= - resy_i;
728 pam-fi 1.6
729 pam-fi 1.41 // if(resy_p <= resy_i) tp_score++;
730     // else ti_score++;
731 mocchiut 1.37
732 pam-fi 1.41
733 pam-fi 1.42 if( cp->npresh > ci->npresh) tp_score++;
734     if( cp->npresh < ci->npresh) ti_score++;
735     else ;//niente
736 pam-fi 1.41
737 mocchiut 1.37 // cout << "CALO "<<tp_score<<ti_score<<endl;
738 pam-fi 1.9
739 mocchiut 1.37 };
740 pam-fi 1.41 // -----------------------------------------------------------------------------------------
741 mocchiut 1.37 // TOF check
742 pam-fi 1.41 // -----------------------------------------------------------------------------------------
743 mocchiut 1.37 // check the number of hit pmts along the track
744     // on S12 S21 and S32, where paddles are parallel to Y axis
745     if( use_TOF && !tof_obj ){
746     cout << "void PamLevel2::SortTracks(): howtosort= "<<how<<" but ToFLevel2 not loaded !!!";
747     return;
748     };
749 pam-fi 1.45 if( use_TOF && !op && oi )ti_score++;
750     if( use_TOF && op && !oi )tp_score++;
751 mocchiut 1.37 if( use_TOF && op && oi ){
752    
753     //
754     Float_t sen = 0.;
755     for (Int_t ih=0; ih < op->npmtadc; ih++){
756     Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
757     if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (op->dedx).At(ih);
758     };
759     for (Int_t ih=0; ih < oi->npmtadc; ih++){
760     Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
761     if ( pl == 2 || pl == 3 || pl == 4 || pl == 5 ) sen += (oi->dedx).At(ih);
762     };
763     //
764     if ( sen >= sortthr ){
765     //printf(" IS A NUCLEUS! en = %f \n",sen);
766     //
767     // is a nucleus use a different algorithm
768     //
769     Int_t nz = 6; Float_t zin[6]; // << define TOF z-coordinates
770     for(Int_t ip=0; ip<nz; ip++)
771     zin[ip] = tof_obj->ToFLevel2::GetZTOF(tof_obj->ToFLevel2::GetToFPlaneID(ip)); // << read ToF plane z-coordinates
772     Trajectory *tr = new Trajectory(nz,zin);
773     //
774     Int_t nphit_p =0;
775     Int_t nphit_i =0;
776     Float_t enhit_p = 0.;
777     Float_t enhit_i = 0.;
778     //
779     for (Int_t ih=0; ih < op->npmtadc; ih++){
780     Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
781     if(pl == 1 || pl == 2 || pl == 5){
782     nphit_p++;
783     enhit_p += (op->dedx).At(ih);
784     };
785     };
786     //
787     tp->DoTrack2(tr);
788     //
789     if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
790     for (Int_t ih=0; ih < op->npmtadc; ih++){
791     Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
792     if(pl == 0){
793     nphit_p++;
794     enhit_p += (op->dedx).At(ih);
795     };
796     };
797     };
798     if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
799     for (Int_t ih=0; ih < op->npmtadc; ih++){
800     Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
801     if(pl == 3){
802     nphit_p++;
803     enhit_p += (op->dedx).At(ih);
804     };
805     };
806     };
807     if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
808     for (Int_t ih=0; ih < op->npmtadc; ih++){
809     Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
810     if(pl == 4){
811     nphit_p++;
812     enhit_p += (op->dedx).At(ih);
813     };
814     };
815     };
816 pam-fi 1.9
817 mocchiut 1.37 for (Int_t ih=0; ih < oi->npmtadc; ih++){
818     Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
819     if(pl == 1 || pl == 2 || pl == 5){
820     nphit_i++;
821     enhit_i += (op->dedx).At(ih);
822     };
823     };
824     //
825     ti->DoTrack2(tr);
826     //
827     if ( fabs(tr->y[0]-oi->ytofpos[0]) < 2. ){
828     for (Int_t ih=0; ih < oi->npmtadc; ih++){
829     Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
830     if(pl == 0){
831     nphit_i++;
832     enhit_i += (op->dedx).At(ih);
833     };
834     };
835     };
836     if ( fabs(tr->y[3]-oi->ytofpos[1]) < 2. ){
837     for (Int_t ih=0; ih < oi->npmtadc; ih++){
838     Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
839     if(pl == 3){
840     nphit_i++;
841     enhit_i += (op->dedx).At(ih);
842     };
843 pam-fi 1.6 };
844 mocchiut 1.37 };
845     if ( fabs(tr->y[4]-oi->ytofpos[2]) < 2. ){
846     for (Int_t ih=0; ih < oi->npmtadc; ih++){
847     Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
848     if(pl == 4){
849     nphit_i++;
850     enhit_i += (op->dedx).At(ih);
851     };
852 pam-fi 1.13 };
853 mocchiut 1.37 };
854    
855 pam-fi 1.6
856 mocchiut 1.37 if(
857     use_TOF &&
858     (nphit_p+nphit_i) !=0 &&
859     true){
860    
861     // 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);
862     // printf(" score p %i score i %i \n",tp_score,ti_score);
863     // if( enhit_p > enhit_i ) tp_score++;
864     // if( nphit_p >= nphit_i && enhit_p > enhit_i ) tp_score++;
865     if ( nphit_p > nphit_i ) tp_score++;
866     if ( nphit_p < nphit_i ) ti_score++;
867     if ( nphit_p == nphit_i ){
868     if ( enhit_p > enhit_i ) tp_score++;
869     else ti_score++;
870     };
871     // printf(" dopo score p %i score i %i \n",tp_score,ti_score);
872     };
873     delete tr;
874     //
875     } else {
876     //
877     // NOT a NUCLEUS
878     //
879     //printf(" NOT a NUCLEUS! en = %f \n",sen);
880    
881     Int_t nphit_p =0;
882     Int_t nphit_i =0;
883 pam-fi 1.6
884    
885 mocchiut 1.37 /* cout << "track: npmtadc "<< op->npmtadc << endl;
886     cout << "track: npmttdc "<< op->npmttdc << endl;
887     cout << "image: npmtadc "<< oi->npmtadc << endl;
888     cout << "image: npmttdc "<< oi->npmttdc << endl;*/
889 pam-fi 1.6
890 pam-fi 1.41 // for (Int_t ih=0; ih < op->npmtadc; ih++){
891     // Int_t pl = tof_obj->GetPlaneIndex( (op->pmtadc).At(ih) );
892     // if(pl == 1 || pl == 2 || pl == 5)nphit_p++;
893     // };
894    
895     // for (Int_t ih=0; ih < oi->npmtadc; ih++){
896     // Int_t pl = tof_obj->GetPlaneIndex( (oi->pmtadc).At(ih) );
897     // if(pl == 1 || pl == 2 || pl == 5)nphit_i++;
898     // };
899     // --- modified to count tdc signals (more efficient?)
900     // --- and to implement check on tdcflag
901     for (Int_t ih=0; ih < op->npmttdc; ih++){
902 mocchiut 1.44 // Int_t pl = tof_obj->GetPlaneIndex( (op->pmttdc).At(ih) );
903 pam-fi 1.41 // if( (op->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_p++;
904     if( (op->tdcflag).At(ih)==0 )nphit_p++;
905 mocchiut 1.37 };
906 pam-fi 1.6
907 pam-fi 1.41 for (Int_t ih=0; ih < oi->npmttdc; ih++){
908 mocchiut 1.44 // Int_t pl = tof_obj->GetPlaneIndex( (oi->pmttdc).At(ih) );
909 pam-fi 1.41 // if( (oi->tdcflag).At(ih)==0 && (pl == 1 || pl == 2 || pl == 5) )nphit_i++;
910     if( (oi->tdcflag).At(ih)==0 )nphit_i++;
911 mocchiut 1.37 };
912 pam-fi 1.6
913 mocchiut 1.37 if(
914     (nphit_p+nphit_i) !=0 &&
915     true){
916 pam-fi 1.6
917 pam-fi 1.42 if ( nphit_p > nphit_i) tp_score++;
918     else if( nphit_p < nphit_i) ti_score++;
919     else ;//niente
920 mocchiut 1.37 };
921     };
922     // cout << "TOF "<<tp_score<<ti_score<<endl;
923     };
924 pam-fi 1.41
925    
926     // if(tp_score == ti_score) use_TRK = true;
927    
928    
929     // -----------------------------------------------------------------------------------------
930 mocchiut 1.37 // tracker check
931 pam-fi 1.41 // -----------------------------------------------------------------------------------------
932 mocchiut 1.37 // chi**2 difference is not always large enough to distinguish among
933     // the real track and its image.
934     // Tracker check will be applied always when calorimeter and tof information is ambiguous.
935 pam-fi 1.41 // *** MODIFIED ON AUGUST 2007 ***
936 mocchiut 1.37 if(use_TRK){
937 pam-fi 1.41 // if( tp->chi2 > 0 && tp->chi2 < ti->chi2 ) tp_score++ ;
938     // else if( ti->chi2 > 0 && ti->chi2 < tp->chi2 ) ti_score++ ;
939    
940     // CHECK 1 : number of points along X
941     if ( tp->GetNX() >= ti->GetNX() )tp_score++ ;
942     if ( tp->GetNX() <= ti->GetNX() )ti_score++ ;
943     // CHECK 2 : number of points along Y
944     if ( tp->GetNY() >= ti->GetNY() )tp_score++ ;
945     if ( tp->GetNY() <= ti->GetNY() )ti_score++ ;
946     // CHECK 3 : chi**2 along X
947     // if( tp->GetChi2X() > 0 && (tp->GetChi2X() < ti->GetChi2X() || ti->GetChi2X() <=0) ) tp_score++ ;
948     // if( ti->GetChi2X() > 0 && (ti->GetChi2X() < tp->GetChi2X() || tp->GetChi2X() <=0) ) ti_score++ ;
949     // CHECK 4 : chi**2 along Y
950     // if( tp->GetChi2Y() > 0 && (tp->GetChi2Y() < ti->GetChi2Y() || ti->GetChi2Y() <=0) ) tp_score++ ;
951     // if( ti->GetChi2Y() > 0 && (ti->GetChi2Y() < tp->GetChi2Y() || tp->GetChi2Y() <=0) ) ti_score++ ;
952     // CHECK 5 : total chi**2
953     // if( tp->chi2 > 0 && (tp->chi2 < ti->chi2 || ti->chi2 <=0) ) tp_score++ ;
954     // if( ti->chi2 > 0 && (ti->chi2 < tp->chi2 || tp->chi2 <=0) ) ti_score++ ;
955    
956 mocchiut 1.37 // cout << "TRK "<<tp_score<<ti_score<<endl;
957     };
958 pam-fi 1.6
959 pam-fi 1.41
960    
961     // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
962 mocchiut 1.37 // the winner is....
963 pam-fi 1.41 // *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
964 mocchiut 1.37 if (tp_score > ti_score) {
965 pam-fi 1.41
966 mocchiut 1.37
967     }else if (tp_score < ti_score) {
968    
969    
970     ts = ti;//its image!!
971     cs = ci;
972     os = oi;
973    
974     ti = tp;//its image!!
975     ci = cp;
976     oi = op;
977    
978     tp = ts;//its image!!
979     cp = cs;
980     op = os;
981 pam-fi 1.13
982    
983 mocchiut 1.37 }else {
984 pam-fi 1.41
985 pam-fi 1.39 // cout << "Warning - track image ambiguity not solved" << endl;
986 pam-fi 1.41
987 mocchiut 1.37 };
988 pam-fi 1.6
989 mocchiut 1.37 }else{
990     // ts = tp;
991     // cs = cp;
992     // os = op;
993     };
994    
995     // cout <<" SortTracks() "<<i<<" -- "<<ts<<endl;
996     // sorted_tracks->Add(ts);//save the track in the sorted array
997     // sorted_tracks.Add(ts);//save the track in the sorted array
998     // sorted_tracks.Add(tp);//save the track in the sorted array
999     // cout << "SortTracks:: sorted_tracks->Add(it) "<<i<<" "<<ts<<endl;
1000     // cout<<"o "<<tp<<endl;
1001     // cout<<"o "<<cp<<endl;
1002     // cout<<"o "<<op<<endl;
1003     new(ttsorted[i]) PamTrack(tp,cp,op);
1004     new(ttimage[i]) PamTrack(ti,ci,oi);
1005     };
1006    
1007     if( tsorted->GetEntries() != trk2_obj->GetNTracks() ){
1008     cout << "void PamLevel2::SortTracks(): tsorted->GetEntries() "<<tsorted->GetEntries()<<" != trk2_obj->GetNTracks() = "<<trk2_obj->GetNTracks() <<endl;
1009     tsorted->Delete(); tsorted=0;
1010     timage->Delete(); timage=0;
1011     }
1012    
1013     //Restore Object count
1014     //To save space in the table keeping track of all referenced objects
1015     //We reset the object count to what it was at the beginning of the event.
1016     TProcessID::SetObjectCount(ObjectNumber);
1017 pam-fi 1.6
1018 pam-fi 1.1 };
1019     //--------------------------------------
1020     //
1021     //
1022     //--------------------------------------
1023     /**
1024 pam-fi 1.2 * This method overrides TrkLevel2::GetTracks(), where sorting is done by decreasing number of fit points and increasing chi^2.
1025     * PamLevel2::GetTracks() keeps the same track order given by TrkLevel2::GetTracks(), but checks image selection by using calorimeter and ToF tracking information.
1026     */
1027 pam-fi 1.13 // TRefArray *PamLevel2::GetTracks(){
1028    
1029     // // *-*-*-*-*-*-*-*-*-*-*-*-*
1030     // SortTracks("+CAL+TOF");
1031     // // *-*-*-*-*-*-*-*-*-*-*-*-*
1032    
1033     // // return sorted_tracks;
1034     // return &sorted_tracks;
1035    
1036     // };
1037     TClonesArray *PamLevel2::GetTracks(){
1038 pam-fi 1.8
1039     // *-*-*-*-*-*-*-*-*-*-*-*-*
1040 mocchiut 1.37 SortTracks();
1041 pam-fi 1.8 // *-*-*-*-*-*-*-*-*-*-*-*-*
1042    
1043 pam-fi 1.13 return tsorted;
1044    
1045 pam-fi 1.2 };
1046     //--------------------------------------
1047 pam-fi 1.3 //
1048     //
1049 pam-fi 1.2 //--------------------------------------
1050     /**
1051 pam-fi 1.1 * Retrieves the it-th Pamela "physical" track.
1052     * It override TrkLevel2::GetTrack(int it).
1053     * @param it Track number, ranging from 0 to GetNTracks().
1054     */
1055     PamTrack *PamLevel2::GetTrack(int it){
1056 pam-fi 1.13
1057     // if(!trk2_obj) return 0;
1058 pam-fi 1.3
1059 mocchiut 1.37 // // *-*-*-*+-*-*-*-*-*-*-*-*-*
1060 pam-fi 1.13 // SortTracks("+CAL+TOF");
1061     // // *-*-*-*-*-*-*-*-*-*-*-*-*
1062     // // if(!sorted_tracks)return 0;
1063     // if(sorted_tracks.IsEmpty())return 0;
1064    
1065     // PamTrack *track = 0;
1066    
1067     // if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() && it<sorted_tracks.GetEntriesFast() ){
1068     // TrkTrack *t = (TrkTrack*)sorted_tracks.At(it);
1069     // track = GetPamTrackAlong(t);
1070     // }else{
1071     // cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1072     // };
1073    
1074     // return track;
1075    
1076     // cout << "PamLevel2::GetTrack(int it) "<<endl;
1077 pam-fi 1.8 // *-*-*-*-*-*-*-*-*-*-*-*-*
1078 mocchiut 1.37 SortTracks();
1079 pam-fi 1.8 // *-*-*-*-*-*-*-*-*-*-*-*-*
1080 pam-fi 1.13 if(!tsorted)return 0;
1081     if(!tsorted->GetEntries())return 0;
1082    
1083 pam-fi 1.6 PamTrack *track = 0;
1084    
1085 pam-fi 1.13 if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks()){
1086     // TrkTrack *t = (TrkTrack*)sorted_tracks.At(it);
1087     // track = GetPamTrackAlong(t);
1088     TClonesArray &t = *(tsorted);
1089     track = (PamTrack*)t[it];
1090    
1091 pam-fi 1.6 }else{
1092 pam-fi 1.13 cout << "PamLevel2::GetTrack(int) : tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1093 pam-fi 1.6 };
1094    
1095     return track;
1096    
1097 pam-fi 1.1 };
1098 pam-fi 1.3
1099 pam-fi 1.1 //--------------------------------------
1100     //
1101     //
1102     //--------------------------------------
1103     /**
1104 pam-fi 1.3 * Retrieves (if present) the image of the it-th Pamela "physical" track, sorted by the method PamLevel2::SortTracks().
1105 pam-fi 1.1 * @param it Track number, ranging from 0 to GetNTracks().
1106     */
1107     PamTrack *PamLevel2::GetTrackImage(int it){
1108    
1109 pam-fi 1.13 // if(!trk2_obj) return 0;
1110    
1111     // // *-*-*-*-*-*-*-*-*-*-*-*-*
1112     // SortTracks("+CAL+TOF");
1113     // // *-*-*-*-*-*-*-*-*-*-*-*-*
1114     // // if(!sorted_tracks)return 0;
1115     // if(sorted_tracks.IsEmpty())return 0;
1116    
1117     // PamTrack *image = 0;
1118    
1119     // if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() ){
1120     // TrkTrack *temp = (TrkTrack*)sorted_tracks.At(it);
1121     // if( temp->HasImage() ){
1122     // TrkTrack *t = trk2_obj->TrkLevel2::GetStoredTrack(temp->GetImageSeqNo());
1123     // image = GetPamTrackAlong(t);
1124     // }else{
1125     // cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;
1126     // };
1127     // }else{
1128     // cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1129     // };
1130    
1131     // return image;
1132    
1133    
1134 pam-fi 1.8 // *-*-*-*-*-*-*-*-*-*-*-*-*
1135 mocchiut 1.37 SortTracks();
1136 pam-fi 1.8 // *-*-*-*-*-*-*-*-*-*-*-*-*
1137 pam-fi 1.13 if(!timage)return 0;
1138     if(!timage->GetEntries())return 0;
1139 pam-fi 1.8
1140     PamTrack *image = 0;
1141    
1142 pam-fi 1.13 if( it >=0 && it < trk2_obj->TrkLevel2::GetNTracks() ){
1143     TClonesArray &t = *(tsorted);
1144     PamTrack *temp = (PamTrack*)t[it];
1145     if( temp->GetTrkTrack()->HasImage() ){
1146     TClonesArray &t = *(timage);
1147     image = (PamTrack*)t[it];
1148 pam-fi 1.2 }else{
1149 pam-fi 1.13 // cout <<"PamLevel2::GetTrackImage(int) : Track SeqNo "<<it<<" does not have image"<<endl;
1150 pam-fi 1.1 };
1151 pam-fi 1.8 }else{
1152 pam-fi 1.13 cout << "PamLevel2::GetTrackImage(int) : Tracker track SeqNo "<< it <<" does not exist (GetNTracks() = "<<trk2_obj->TrkLevel2::GetNTracks()<<")"<<endl;
1153 pam-fi 1.8 };
1154 pam-fi 1.1
1155 pam-fi 1.8 return image;
1156 pam-fi 1.1 }
1157    
1158     //--------------------------------------
1159     //
1160     //
1161     //--------------------------------------
1162     /**
1163 pam-fi 1.2 * Get the Pamela detector trees in a single file and make them friends.
1164     * @param f TFile pointer
1165     * @param detlist String to select trees to be included
1166     * @return Pointer to a TTree
1167 pam-fi 1.1 */
1168 pam-fi 1.11 TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ){
1169 pam-fi 1.4
1170 pam-fi 1.12 // if( !detlist.IsNull() ) SetWhichTrees(detlist);
1171     // else GetWhichTrees(f);
1172 mocchiut 1.35 if ( pam_tree ){
1173     printf("WARNING: TTree *PamLevel2::GetPamTree(TFile *fl, TString detlist) -- pam_tree already exists!\n ");
1174     return pam_tree;
1175     };
1176     //
1177    
1178 pam-fi 1.20 cout << "TTree *PamLevel2::GetPamTree(TFile *f, TString detlist ) -- obsolte "<<endl;
1179    
1180 pam-fi 1.12 SetWhichTrees(detlist);
1181 pam-fi 1.9
1182 pam-fi 1.6 TTree *Trout =0;
1183 pam-fi 1.9
1184     TString fname = f->GetName();
1185     if(!CheckLevel2File(fname))return NULL;
1186    
1187 pam-fi 1.20 // UInt_t *found=0;
1188 pam-fi 1.11
1189 pam-fi 1.12 cout<< "GetPamTree(TFile*,TString): detector list --> ";
1190     if(TRK1)cout<<"TRK1 ";
1191     if(TRK2)cout<<"TRK2 ";
1192     if(TRKh)cout<<"TRKH ";
1193     if(CAL1)cout<<"CAL1 ";
1194     if(CAL2)cout<<"CAL2 ";
1195     if(TOF)cout<<"TOF ";
1196     if(TRG)cout<<"TRG ";
1197     if(AC)cout<<"AC ";
1198     if(ND)cout<<"ND ";
1199     if(S4)cout<<"S4 ";
1200     if(ORB)cout<<"ORB ";
1201     cout << endl;
1202 pam-fi 1.20 if(SELLI)cout<<">>> Found selection-list <<<"<<endl;
1203 pam-fi 1.12
1204     f->cd();
1205    
1206 pam-fi 1.4 // Tracker
1207 pam-fi 1.1 TTree *T = (TTree*)f->Get("Tracker");
1208 pam-fi 1.8 if(T && (TRK2||TRK1||TRKh)) {
1209 pam-fi 1.9 if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
1210 pam-fi 1.20 // else T->SetBranchStatus("TrkLevel2",0,found);
1211 pam-fi 1.8 if(TRK2)cout << "Tracker : set branch address TrkLevel2"<<endl;
1212 pam-fi 1.9 if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
1213 pam-fi 1.20 // else T->SetBranchStatus("TrkLevel1",0,found);
1214 pam-fi 1.8 if(TRK1)cout << "Tracker : set branch address TrkLevel1"<<endl;
1215 pam-fi 1.9 if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1216 pam-fi 1.20 // else T->SetBranchStatus("TrkHough",0,found);
1217 pam-fi 1.9 if(TRKh)cout << "Tracker : set branch address TrkHough"<<endl;
1218     if(!Trout)Trout=T;
1219     else Trout->AddFriend(T);
1220 pam-fi 1.1 }else{
1221     cout << "Tracker : missing tree"<<endl;
1222     };
1223     // Calorimeter
1224     TTree *C = (TTree*)f->Get("Calorimeter");
1225 pam-fi 1.9 if(C && (CAL2||CAL1)) {
1226     if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1227 pam-fi 1.20 // else C->SetBranchStatus("CaloLevel2",0,found);
1228 pam-fi 1.9 if(CAL2)cout << "Calorimeter : set branch address CaloLevel2"<<endl;
1229     if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1230 pam-fi 1.20 // else C->SetBranchStatus("CaloLevel1",0,found);
1231 pam-fi 1.9 if(CAL1)cout << "Calorimeter : set branch address CaloLevel1"<<endl;
1232 pam-fi 1.4 if(!Trout)Trout=C;
1233     else Trout->AddFriend(C);
1234 pam-fi 1.1 }else{
1235     cout << "Calorimeter : missing tree"<<endl;
1236     };
1237 pam-fi 1.12
1238 pam-fi 1.1 // ToF
1239     TTree *O = (TTree*)f->Get("ToF");
1240 pam-fi 1.2 if(O && TOF) {
1241 pam-fi 1.9 O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
1242 pam-fi 1.1 cout << "ToF : set branch address ToFLevel2"<<endl;
1243 pam-fi 1.4 if(!Trout)Trout=O;
1244     else Trout->AddFriend(O);
1245 pam-fi 1.1 }else{
1246     cout << "ToF : missing tree"<<endl;
1247     };
1248     // Trigger
1249     TTree *R = (TTree*)f->Get("Trigger");
1250 pam-fi 1.2 if(R && TRG) {
1251 pam-fi 1.9 R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1252 pam-fi 1.1 cout << "Trigger : set branch address TrigLevel2"<<endl;
1253 mocchiut 1.35 if(!Trout)Trout=R;
1254 pam-fi 1.4 else Trout->AddFriend(R);
1255 pam-fi 1.1 }else{
1256     cout << "Trigger : missing tree"<<endl;
1257     };
1258     // S4
1259     TTree *S = (TTree*)f->Get("S4");
1260 pam-fi 1.2 if(S && S4) {
1261 pam-fi 1.9 S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1262 pam-fi 1.1 cout << "S4 : set branch address S4Level2"<<endl;
1263 mocchiut 1.35 if(!Trout)Trout=S;
1264 pam-fi 1.4 else Trout->AddFriend(S);
1265 pam-fi 1.1 }else{
1266     cout << "S4 : missing tree"<<endl;
1267     };
1268     // Neutron Detector
1269     TTree *N = (TTree*)f->Get("NeutronD");
1270 pam-fi 1.2 if(N && ND) {
1271 pam-fi 1.9 N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1272 pam-fi 1.1 cout << "NeutronD : set branch address NDLevel2"<<endl;
1273 mocchiut 1.35 if(!Trout)Trout=N;
1274 pam-fi 1.4 else Trout->AddFriend(N);
1275 pam-fi 1.1 }else{
1276     cout << "NeutronD : missing tree"<<endl;
1277     };
1278     // Anticounters
1279     TTree *A = (TTree*)f->Get("Anticounter");
1280 pam-fi 1.2 if(A && AC) {
1281 pam-fi 1.9 A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1282 pam-fi 1.1 cout << "Anticounter : set branch address AcLevel2"<<endl;
1283 mocchiut 1.35 if(!Trout)Trout=A;
1284 pam-fi 1.4 else Trout->AddFriend(A);
1285 pam-fi 1.1 }else{
1286     cout << "Anticounter : missing tree"<<endl;
1287     };
1288 pam-fi 1.2 // Orbital Info
1289 pam-fi 1.11 TTree *B = (TTree*)f->Get("OrbitalInfo");
1290     if(B && ORB) {
1291     B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1292     cout << "OrbitalInfo : set branch address OrbitalInfo"<<endl;
1293 mocchiut 1.35 if(!Trout)Trout=B;
1294 pam-fi 1.11 else Trout->AddFriend(B);
1295     }else{
1296     cout << "OrbitalInfo : missing tree"<<endl;
1297     };
1298 pam-fi 1.20
1299     TTree *L = (TTree*)f->Get("SelectionList");
1300     if(L && SELLI==1) {
1301     // L->SetBranchAddress("RunEntry",&irun);
1302     // cout << "SelectionList: set branch address RunEntry"<<endl;
1303     // L->SetBranchAddress("EventEntry",&irunentry);
1304     // cout << "SelectionList: set branch address EventEntry"<<endl;
1305     // if(!Trout)Trout=O;
1306     // else Trout->AddFriend("SelectionList");
1307     cout << " TTree *PamLevel2::GetPamTree(TFile, TString) >>> SelectionList not implemente!!!!"<<endl;
1308     sel_tree = 0;
1309     }else{
1310     cout << "SelectionList : missing tree"<<endl;
1311     };
1312 pam-fi 1.11
1313     cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;
1314 mocchiut 1.28
1315     pam_tree = (TChain*)Trout;
1316 pam-fi 1.1
1317 pam-fi 1.11 return Trout;
1318 pam-fi 1.1
1319     }
1320 pam-fi 1.2 //--------------------------------------
1321     //
1322     //
1323     //--------------------------------------
1324     /**
1325     * Get list of Level2 files.
1326     * @param ddir Level2 data directory.
1327     * @param flisttxt Name of txt file containing file list.
1328     * @return Pointer to a TList of TSystemFiles
1329     * If no input file list is given , all the Level2 files inside the directory are processed.
1330     */
1331     TList* PamLevel2::GetListOfLevel2Files(TString ddir, TString flisttxt = ""){
1332    
1333 pam-fi 1.6 TString wdir = gSystem->WorkingDirectory();
1334    
1335 mocchiut 1.35 // if(ddir=="")ddir = wdir;
1336 pam-fi 1.6 // TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1337 mocchiut 1.35 if ( ddir != ""){
1338     cout << "Level2 data directory : "<< ddir << endl;
1339     } else {
1340     cout << "Level2 data directory not given as input: trying to evaluate from list or taking working directory " << endl;
1341     };
1342 pam-fi 1.6 TList *contents = new TList; // create output list
1343     contents->SetOwner();
1344    
1345 pam-fi 1.16 // char *fullpath;
1346 pam-fi 1.18 // const char *fullpath;
1347 pam-fi 1.6
1348 pam-fi 1.26 // if no input file list is given:
1349 pam-fi 1.6 if ( flisttxt != "" ){
1350 pam-fi 1.2
1351 pam-fi 1.16 // if( !gSystem->IsFileInIncludePath(flisttxt,&fullpath) ){
1352 pam-fi 1.18 // if( !(fullpath = gSystem->FindFile("./",flisttxt)) ){
1353     // cout <<"File "<<flisttxt<<" not found"<<endl;
1354     // return 0;
1355     // }
1356     // flisttxt = fullpath;
1357 mocchiut 1.28 if ( !flisttxt.EndsWith(".root") ){
1358 pam-fi 1.18
1359     flisttxt = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1360 pam-fi 1.16
1361 pam-fi 1.11 if( !gSystem->ChangeDirectory(ddir) ){
1362     cout << "Cannot change directory : "<<ddir<<endl;
1363     return 0;
1364     }
1365 pam-fi 1.2
1366 pam-fi 1.6 cout <<"Input file list : " << flisttxt <<endl;
1367     ifstream in;
1368     in.open(flisttxt, ios::in); //open input file list
1369 pam-fi 1.18 if(!in.good()){
1370     cout <<" ERROR opening the file "<<endl;
1371     gSystem->ChangeDirectory(wdir); // back to the working directory
1372     return 0;
1373     }
1374 pam-fi 1.9 int line=0;
1375 pam-fi 1.6 while (1) {
1376     TString file;
1377     in >> file;
1378     if (!in.good()) break;
1379 pam-fi 1.9 line++;
1380 pam-fi 1.8 // cout <<"(1) " << file << endl;
1381 pam-fi 1.9 if(file.IsNull()){
1382     cout << "-- list interrupted at line "<<line <<endl;
1383     break;
1384     }
1385 pam-fi 1.10 if(file.Contains("#"))file = file(0,file.First("#"));
1386     // cout <<"(2) " << file << endl;
1387 pam-fi 1.16 // if( gSystem->IsFileInIncludePath(file,&fullpath) ){
1388 pam-fi 1.18 // if( (fullpath = gSystem->FindFile(ddir,file)) ){
1389 pam-fi 1.19 if( file.EndsWith(".root") ){
1390 mocchiut 1.35 TString filedir;
1391     if (ddir != ""){
1392     filedir = ddir; // take the input dir
1393     } else {
1394     gSystem->ChangeDirectory(wdir); // back to the working directory
1395     filedir = gSystem->DirName(file); // this will take the path if exist in the list otherwise it will return automatically the working dir
1396     };
1397     char *fullpath = gSystem->ConcatFileName(gSystem->DirName(filedir),gSystem->BaseName(file));
1398 pam-fi 1.19 contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list
1399     delete fullpath;
1400     }
1401 pam-fi 1.18 // }else{
1402 pam-fi 1.10 // if(file.Data()!="")cout << "File: "<<file<<" ---> missing "<< endl;
1403 pam-fi 1.18 // };
1404 pam-fi 1.6 };
1405     in.close();
1406 mocchiut 1.28 } else {
1407 mocchiut 1.35 if(flisttxt.Contains("#"))flisttxt = flisttxt(0,flisttxt.First("#"));
1408 mocchiut 1.28 char *fullpath = gSystem->ConcatFileName(gSystem->DirName(flisttxt),gSystem->BaseName(flisttxt));
1409     contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));// add file to the list
1410     delete fullpath;
1411     };
1412 pam-fi 1.6 }else{
1413 pam-fi 1.2
1414 pam-fi 1.6 cout << "No input file list given."<<endl;
1415     cout << "Check for existing root files."<<endl;
1416 pam-fi 1.2 // cout << "Warking directory: "<< gSystem->WorkingDirectory()<< endl;
1417 mocchiut 1.35 if ( ddir == "" ){
1418     ddir = wdir;
1419     cout << "Level2 data directory : "<< ddir << endl;
1420     };
1421    
1422 pam-fi 1.6 TSystemDirectory *datadir = new TSystemDirectory(gSystem->BaseName(ddir),ddir);
1423     TList *temp = datadir->GetListOfFiles();
1424 pam-fi 1.2 // temp->Print();
1425     // cout << "*************************************" << endl;
1426    
1427 pam-fi 1.6 TIter next(temp);
1428     TSystemFile *questo = 0;
1429    
1430    
1431     while ( (questo = (TSystemFile*) next()) ) {
1432     TString name = questo-> GetName();
1433     if( name.EndsWith(".root") ){
1434 pam-fi 1.18 // const char *fullpath = gSystem->FindFile(ddir,name);
1435 pam-fi 1.16 // char *fullpath;
1436     // gSystem->IsFileInIncludePath(name,&fullpath);
1437 pam-fi 1.18 char *fullpath = gSystem->ConcatFileName(gSystem->DirName(ddir),gSystem->BaseName(name));
1438 pam-fi 1.6 contents->Add(new TSystemFile(fullpath,gSystem->DirName(fullpath)));
1439 pam-fi 1.18 delete fullpath;
1440 pam-fi 1.6 };
1441     }
1442     delete temp;
1443     delete datadir;
1444    
1445     };
1446     gSystem->ChangeDirectory(wdir); // back to the working directory
1447 pam-fi 1.5 // cout << endl << "Selected files:" << endl;
1448     // contents->Print();
1449 pam-fi 1.18 cout << contents->GetEntries()<<" files \n";
1450 pam-fi 1.5 // cout << endl;
1451 pam-fi 1.4 // cout << "Working directory: "<< gSystem->WorkingDirectory()<< endl;
1452 pam-fi 1.6 return contents;
1453 pam-fi 1.2 };
1454     //--------------------------------------
1455     //
1456     //
1457     //--------------------------------------
1458     /**
1459     * Get the Pamela detector chains from a list of files and make them friends.
1460     * @param fl Pointer to a TList of TSystemFiles
1461     * @param detlist String to select trees to be included
1462     * @return Pointer to a TChain
1463     */
1464 pam-fi 1.11 TChain *PamLevel2::GetPamTree(TList *fl, TString detlist ){
1465 mocchiut 1.35 //
1466     //
1467     //
1468     if ( pam_tree ){
1469     printf("WARNING: TChain *PamLevel2::GetPamTree(TList *fl, TString detlist) -- pam_tree already exists!\n ");
1470     return pam_tree;
1471     };
1472     //
1473 pam-fi 1.11
1474     TChain *Trout =0;
1475 pam-fi 1.2
1476 pam-fi 1.12 // if( !detlist.IsNull() )SetWhichTrees(detlist);
1477     SetWhichTrees(detlist);
1478 pam-fi 1.6
1479 pam-fi 1.13 cout<< "GetPamTree(TList*,TString): input detector list --> ";
1480 pam-fi 1.12 if(TRK1)cout<<"TRK1 ";
1481     if(TRK2)cout<<"TRK2 ";
1482     if(TRKh)cout<<"TRKH ";
1483     if(CAL1)cout<<"CAL1 ";
1484     if(CAL2)cout<<"CAL2 ";
1485     if(TOF)cout<<"TOF ";
1486     if(TRG)cout<<"TRG ";
1487     if(AC)cout<<"AC ";
1488     if(ND)cout<<"ND ";
1489     if(S4)cout<<"S4 ";
1490     if(ORB)cout<<"ORB ";
1491     cout << endl;
1492    
1493 mocchiut 1.35 TChain *T = 0;
1494     TChain *C = 0;
1495     TChain *O = 0;
1496     TChain *R = 0;
1497     TChain *S = 0;
1498     TChain *N = 0;
1499     TChain *A = 0;
1500     TChain *B = 0;
1501 pam-fi 1.20
1502 mocchiut 1.35 TChain *L = 0;
1503 pam-fi 1.6
1504 pam-fi 1.8 if(TRK2||TRK1||TRKh) T = new TChain("Tracker");
1505 pam-fi 1.11 if(CAL2||CAL1) C = new TChain("Calorimeter");
1506     if(TOF) O = new TChain("ToF");
1507     if(TRG) R = new TChain("Trigger");
1508     if(S4) S = new TChain("S4");
1509     if(ND) N = new TChain("NeutronD");
1510     if(AC) A = new TChain("Anticounter");
1511     if(ORB) B = new TChain("OrbitalInfo");
1512 pam-fi 1.20 L = new TChain("SelectionList");
1513 pam-fi 1.6
1514     // loop over files and create chains
1515     TIter next(fl);
1516     TSystemFile *questo = 0;
1517     while ( (questo = (TSystemFile*) next()) ) {
1518     TString name = questo->GetName();
1519 pam-fi 1.13 cout << "File: "<< name << endl;
1520 pam-fi 1.6 if( CheckLevel2File(name) ){
1521 pam-fi 1.8 if(TRK2||TRK1||TRKh) T->Add(name);
1522 pam-fi 1.11 if(CAL1||CAL2) C->Add(name);
1523     if(TOF) O->Add(name);
1524     if(TRG) R->Add(name);
1525     if(S4) S->Add(name);
1526     if(ND) N->Add(name);
1527     if(AC) A->Add(name);
1528     if(ORB) B->Add(name);
1529 pam-fi 1.20 if(SELLI==1) L->Add(name);
1530 pam-fi 1.4 };
1531 mocchiut 1.35 };
1532 pam-fi 1.6
1533     cout << "done chain \n";
1534 pam-fi 1.36 cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
1535 pam-fi 1.11
1536 pam-fi 1.20 // UInt_t *found=0;
1537 pam-fi 1.11 // Tracker
1538     if(T && (TRK2||TRK1||TRKh)) {
1539     if(TRK2)T->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
1540 pam-fi 1.20 // else T->SetBranchStatus("TrkLevel2",0,found);
1541 pam-fi 1.11 if(TRK2)cout << "Tracker : set branch address TrkLevel2"<<endl;
1542     if(TRK1)T->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
1543 pam-fi 1.20 // else T->SetBranchStatus("TrkLevel1",0,found);
1544 pam-fi 1.11 if(TRK1)cout << "Tracker : set branch address TrkLevel1"<<endl;
1545     if(TRKh)T->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1546 pam-fi 1.20 // else T->SetBranchStatus("TrkHough",0,found);
1547 pam-fi 1.11 if(TRKh)cout << "Tracker : set branch address TrkHough"<<endl;
1548     if(!Trout)Trout=T;
1549 mocchiut 1.35 else Trout->AddFriend("Tracker");
1550 pam-fi 1.11 }else{
1551     cout << "Tracker : missing tree"<<endl;
1552     };
1553 pam-fi 1.6 // Calorimeter
1554 pam-fi 1.11 if(C && (CAL2||CAL1)) {
1555     if(CAL2)C->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1556 pam-fi 1.20 // else C->SetBranchStatus("CaloLevel2",0,found);
1557 pam-fi 1.11 if(CAL2)cout << "Calorimeter : set branch address CaloLevel2"<<endl;
1558     if(CAL1)C->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1559 pam-fi 1.20 // else C->SetBranchStatus("CaloLevel1",0,found);
1560 pam-fi 1.11 if(CAL1)cout << "Calorimeter : set branch address CaloLevel1"<<endl;
1561     if(!Trout)Trout=C;
1562     else Trout->AddFriend("Calorimeter");
1563     }else{
1564     cout << "Calorimeter : missing tree"<<endl;
1565 pam-fi 1.6 };
1566 pam-fi 1.4 // ToF
1567 pam-fi 1.11 if(O && TOF) {
1568     O->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
1569     cout << "ToF : set branch address ToFLevel2"<<endl;
1570     if(!Trout)Trout=O;
1571     else Trout->AddFriend("ToF");
1572     }else{
1573     cout << "ToF : missing tree"<<endl;
1574 pam-fi 1.6 };
1575 pam-fi 1.4 // Trigger
1576 pam-fi 1.11 if(R && TRG) {
1577     R->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1578     cout << "Trigger : set branch address TrigLevel2"<<endl;
1579     if(!Trout)Trout=O;
1580     else Trout->AddFriend("Trigger");
1581     }else{
1582     cout << "Trigger : missing tree"<<endl;
1583 pam-fi 1.6 };
1584 pam-fi 1.4 // S4
1585 pam-fi 1.11 if(S && S4) {
1586     S->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1587     cout << "S4 : set branch address S4Level2"<<endl;
1588     if(!Trout)Trout=O;
1589     else Trout->AddFriend("S4");
1590     }else{
1591     cout << "S4 : missing tree"<<endl;
1592 pam-fi 1.6 };
1593 pam-fi 1.4 // Neutron Detector
1594 pam-fi 1.11 if(N && ND) {
1595     N->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1596     cout << "NeutronD : set branch address NDLevel2"<<endl;
1597     if(!Trout)Trout=O;
1598     else Trout->AddFriend("NeutronD");
1599     }else{
1600     cout << "NeutronD : missing tree"<<endl;
1601 pam-fi 1.6 };
1602 pam-fi 1.4 // Anticounters
1603 pam-fi 1.11 if(A && AC) {
1604     A->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1605     cout << "Anticounter : set branch address AcLevel2"<<endl;
1606     if(!Trout)Trout=O;
1607     else Trout->AddFriend("Anticounter");
1608     }else{
1609     cout << "Anticounter : missing tree"<<endl;
1610 pam-fi 1.6 };
1611 pam-fi 1.11 // Orbital Info
1612     if(B && ORB) {
1613     B->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1614     cout << "OrbitalInfo : set branch address OrbitalInfo"<<endl;
1615     if(!Trout)Trout=O;
1616     else Trout->AddFriend("OrbitalInfo");
1617     }else{
1618     cout << "OrbitalInfo : missing tree"<<endl;
1619 pam-fi 1.6 };
1620 pam-fi 1.36 cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
1621 pam-fi 1.20
1622     // Selection List
1623     if(L && SELLI==1) {
1624     cout<<">>> Found selection-list <<<"<<endl;
1625     L->SetBranchAddress("RunEntry",&irun);
1626     cout << "SelectionList: set branch address RunEntry"<<endl;
1627     L->SetBranchAddress("EventEntry",&irunentry);
1628     cout << "SelectionList: set branch address EventEntry"<<endl;
1629     sel_tree = L;
1630     // if(!Trout)Trout=O;
1631     // else Trout->AddFriend("SelectionList");
1632 pam-fi 1.36 cout << "----------------------------------------------------" <<endl;
1633 pam-fi 1.20 }else{
1634     // cout << "SelectionList : missing tree"<<endl;
1635     if(L)L->Delete();
1636     };
1637 pam-fi 1.6
1638 pam-fi 1.11 // cout<<endl<<" Number of entries: "<<Trout->GetEntries()<<endl<<endl;
1639    
1640 pam-fi 1.20 pam_tree = Trout;
1641    
1642 pam-fi 1.11 return Trout;
1643 pam-fi 1.4 }
1644 pam-fi 1.8
1645    
1646    
1647 pam-fi 1.4 //--------------------------------------
1648     //
1649     //
1650     //--------------------------------------
1651     /**
1652     * Set branch addresses for Pamela friend trees
1653     */
1654 pam-fi 1.8 void PamLevel2::SetBranchAddress(TTree *t){
1655    
1656     TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");
1657     TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");
1658     TRKh = TRKh & t->GetBranchStatus("TrkHough");
1659     CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");
1660     CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");
1661     TOF = TOF & t->GetBranchStatus("ToFLevel2");
1662     TRG = TRG & t->GetBranchStatus("TrigLevel2");
1663     S4 = S4 & t->GetBranchStatus("S4Level2");
1664     ND = ND & t->GetBranchStatus("NDLevel2");
1665     AC = AC & t->GetBranchStatus("AcLevel2");
1666     ORB = ORB & t->GetBranchStatus("OrbitalInfo");
1667    
1668    
1669 pam-fi 1.4 // Tracker
1670 pam-fi 1.8 if(TRK1) {
1671 pam-fi 1.9 t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel2"));
1672 pam-fi 1.8 cout << "Tracker : set branch address TrkLevel1"<<endl;
1673     };
1674     if(TRK2) {
1675 pam-fi 1.9 t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel1"));
1676 pam-fi 1.6 cout << "Tracker : set branch address TrkLevel2"<<endl;
1677     };
1678 pam-fi 1.8 if(TRKh) {
1679 pam-fi 1.9 t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1680 pam-fi 1.8 cout << "Tracker : set branch address TrkHough"<<endl;
1681     };
1682 pam-fi 1.6
1683     // Calorimeter
1684 pam-fi 1.8 if(CAL1) {
1685 pam-fi 1.9 t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1686 pam-fi 1.8 cout << "Calorimeter : set branch address CaloLevel1"<<endl;
1687     };
1688     if(CAL2) {
1689 pam-fi 1.9 t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1690 pam-fi 1.6 cout << "Calorimeter : set branch address CaloLevel2"<<endl;
1691     };
1692    
1693 pam-fi 1.4 // ToF
1694 pam-fi 1.6 if(TOF) {
1695 pam-fi 1.9 t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
1696 pam-fi 1.6 cout << "ToF : set branch address ToFLevel2"<<endl;
1697     };
1698 pam-fi 1.4 // Trigger
1699 pam-fi 1.6 if(TRG) {
1700 pam-fi 1.9 t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1701 pam-fi 1.6 cout << "Trigger : set branch address TrigLevel2"<<endl;
1702     };
1703 pam-fi 1.4 // S4
1704 pam-fi 1.6 if(S4) {
1705 pam-fi 1.9 t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1706 pam-fi 1.6 cout << "S4 : set branch address S4Level2"<<endl;
1707     };
1708 pam-fi 1.4 // Neutron Detector
1709 pam-fi 1.6 if(ND) {
1710 pam-fi 1.9 t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1711 pam-fi 1.6 cout << "NeutronD : set branch address NDLevel2"<<endl;
1712     };
1713 pam-fi 1.4 // Anticounters
1714 pam-fi 1.6 if(AC) {
1715 pam-fi 1.9 t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1716 pam-fi 1.6 cout << "Anticounter : set branch address AcLevel2"<<endl;
1717     };
1718 pam-fi 1.4 // OrbitalInfo
1719 pam-fi 1.6 if(ORB) {
1720 pam-fi 1.9 t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1721 pam-fi 1.6 cout << "OrbitalInfo : set branch address OrbitalInfo"<<endl;
1722     };
1723 pam-fi 1.20 // SelectionList
1724     if(SELLI==1) {
1725     t->SetBranchAddress("RunEntry", &irun);
1726     cout << "SelectionList: set branch address RunEntry"<<endl;
1727     t->SetBranchAddress("EventEntry", &irunentry);
1728     cout << "SelectionList: set branch address EventEntry"<<endl;
1729     };
1730 pam-fi 1.6
1731 pam-fi 1.4 }
1732 pam-fi 1.8 /**
1733     * Set branch addresses for Pamela friend trees
1734     */
1735     void PamLevel2::SetBranchAddress(TChain *t){
1736 pam-fi 1.4
1737 pam-fi 1.8 TRK2 = TRK2 & t->GetBranchStatus("TrkLevel2");
1738     TRK1 = TRK1 & t->GetBranchStatus("TrkLevel1");
1739     TRKh = TRKh & t->GetBranchStatus("TrkHough");
1740     CAL1 = CAL1 & t->GetBranchStatus("CaloLevel1");
1741     CAL2 = CAL2 & t->GetBranchStatus("CaloLevel2");
1742     TOF = TOF & t->GetBranchStatus("ToFLevel2");
1743     TRG = TRG & t->GetBranchStatus("TrigLevel2");
1744     S4 = S4 & t->GetBranchStatus("S4Level2");
1745     ND = ND & t->GetBranchStatus("NDLevel2");
1746     AC = AC & t->GetBranchStatus("AcLevel2");
1747     ORB = ORB & t->GetBranchStatus("OrbitalInfo");
1748    
1749     // Tracker
1750 pam-fi 1.9 if(TRK2) {
1751 pam-fi 1.11 t->SetBranchAddress("TrkLevel2", GetPointerTo("TrkLevel2"));
1752 pam-fi 1.9 cout << "Tracker : set branch address TrkLevel2"<<endl;
1753     };
1754 pam-fi 1.8 if(TRK1) {
1755 pam-fi 1.9 t->SetBranchAddress("TrkLevel1", GetPointerTo("TrkLevel1"));
1756 pam-fi 1.8 cout << "Tracker : set branch address TrkLevel1"<<endl;
1757     };
1758     if(TRKh) {
1759 pam-fi 1.9 t->SetBranchAddress("TrkHough", GetPointerTo("TrkHough"));
1760 pam-fi 1.8 cout << "Tracker : set branch address TrkHough"<<endl;
1761     };
1762    
1763     // Calorimeter
1764 pam-fi 1.9 if(CAL2) {
1765     t->SetBranchAddress("CaloLevel2", GetPointerTo("CaloLevel2"));
1766     cout << "Calorimeter : set branch address CaloLevel2"<<endl;
1767     };
1768 pam-fi 1.8 if(CAL1) {
1769 pam-fi 1.9 t->SetBranchAddress("CaloLevel1", GetPointerTo("CaloLevel1"));
1770 pam-fi 1.8 cout << "Calorimeter : set branch address CaloLevel1"<<endl;
1771     };
1772    
1773     // ToF
1774     if(TOF) {
1775 pam-fi 1.9 t->SetBranchAddress("ToFLevel2", GetPointerTo("ToFLevel2"));
1776 pam-fi 1.8 cout << "ToF : set branch address ToFLevel2"<<endl;
1777     };
1778     // Trigger
1779     if(TRG) {
1780 pam-fi 1.9 t->SetBranchAddress("TrigLevel2", GetPointerTo("TrigLevel2"));
1781 pam-fi 1.8 cout << "Trigger : set branch address TrigLevel2"<<endl;
1782     };
1783     // S4
1784     if(S4) {
1785 pam-fi 1.9 t->SetBranchAddress("S4Level2", GetPointerTo("S4Level2"));
1786 pam-fi 1.8 cout << "S4 : set branch address S4Level2"<<endl;
1787     };
1788     // Neutron Detector
1789     if(ND) {
1790 pam-fi 1.9 t->SetBranchAddress("NDLevel2", GetPointerTo("NDLevel2"));
1791 pam-fi 1.8 cout << "NeutronD : set branch address NDLevel2"<<endl;
1792     };
1793     // Anticounters
1794     if(AC) {
1795 pam-fi 1.9 t->SetBranchAddress("AcLevel2", GetPointerTo("AcLevel2"));
1796 pam-fi 1.8 cout << "Anticounter : set branch address AcLevel2"<<endl;
1797     };
1798     // OrbitalInfo
1799     if(ORB) {
1800 pam-fi 1.9 t->SetBranchAddress("OrbitalInfo", GetPointerTo("OrbitalInfo"));
1801 pam-fi 1.8 cout << "OrbitalInfo : set branch address OrbitalInfo"<<endl;
1802     };
1803 pam-fi 1.20 // SelectionList
1804     if(SELLI==1) {
1805     t->SetBranchAddress("RunEntry", &irun);
1806     cout << "SelectionList: set branch address RunEntry"<<endl;
1807     t->SetBranchAddress("EventEntry", &irunentry);
1808     cout << "SelectionList: set branch address EventEntry"<<endl;
1809     };
1810 pam-fi 1.8
1811     }
1812 pam-fi 1.9
1813    
1814 pam-fi 1.4 //--------------------------------------
1815     //
1816     //
1817     //--------------------------------------
1818     /**
1819     * Get the Run tree chain from a list of files.
1820     * @param fl Pointer to a TList of TSystemFiles
1821     * @return Pointer to a TChain
1822     */
1823     TChain *PamLevel2::GetRunTree(TList *fl){
1824 mocchiut 1.35 //
1825     //
1826     //
1827     if ( run_tree ){
1828     printf("WARNING: TChain *PamLevel2::GetRunTree(TList *fl) -- run_tree already exists!\n ");
1829     return run_tree;
1830     };
1831     //
1832     TChain *R = new TChain("Run");
1833 pam-fi 1.6
1834     // loop over files and create chains
1835     TIter next(fl);
1836     TSystemFile *questo = 0;
1837     while ( (questo = (TSystemFile*) next()) ) {
1838     TString name = questo->GetName();
1839 pam-fi 1.4 // cout << "File: "<< name << endl;
1840 pam-fi 1.6 if( CheckLevel2File(name) ){
1841     R->Add(name);
1842     };
1843     }
1844 pam-fi 1.20
1845     if(R->GetNtrees()){
1846 pam-fi 1.6
1847 pam-fi 1.20 R->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
1848     cout << "Run : set branch address RunInfo"<<endl;
1849     R->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
1850     cout << "Software : set branch address SoftInfo"<<endl; // Emiliano
1851     }else{
1852     delete R;
1853     R=0;
1854     }
1855    
1856     run_tree = R;
1857    
1858 pam-fi 1.6 return R;
1859 pam-fi 1.4
1860     }
1861     //--------------------------------------
1862     //
1863     //
1864     //--------------------------------------
1865     /**
1866     * Get the Run tree from a file.
1867     * @param f Pointer to a TFile
1868     * @return Pointer to a TTree
1869     */
1870     TTree *PamLevel2::GetRunTree(TFile *f){
1871 mocchiut 1.35 if ( run_tree ){
1872     printf("WARNING: TTree *PamLevel2::GetRunTree(TFile *f) -- run_tree already exists!\n ");
1873     return run_tree;
1874     };
1875    
1876 pam-fi 1.4
1877 pam-fi 1.20 cout << "TTree *PamLevel2::GetRunTree(TFile *f) -- obsolte "<<endl;
1878 pam-fi 1.4
1879 mocchiut 1.35 TTree *T = (TTree*)f->Get("Run");
1880 pam-fi 1.6
1881 mocchiut 1.35 if(T){
1882     T->SetBranchAddress("RunInfo", GetPointerTo("RunInfo"));
1883 pam-fi 1.20 cout << "Run : set branch address RunInfo"<<endl;
1884 mocchiut 1.35 T->SetBranchAddress("SoftInfo", GetPointerTo("SoftInfo")); // Emiliano
1885 pam-fi 1.20 cout << "Software : set branch address SoftInfo"<<endl; // Emiliano
1886     }
1887 mocchiut 1.28
1888 mocchiut 1.35 run_tree = (TChain*)T;
1889 mocchiut 1.28
1890 mocchiut 1.35 return T;
1891 pam-fi 1.2
1892     }
1893 mocchiut 1.17 /**
1894     * Update the runinfo informations (to be used to have Run infos event by event basis)
1895     * @param run Pointer to the chain/tree which contains run infos
1896     * @return true if a new run has been read, false if it is still the same run
1897     */
1898 pam-fi 1.33 Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev){
1899 mocchiut 1.17 //
1900     // check if we have already called once GetEntry, if not call it
1901     //
1902 pam-fi 1.36 cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, Long64_t iev) --- NON FUNZIONA BENISSIMO.... "<<endl;
1903 pam-fi 1.34 if(!run){
1904     cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing RunInfo tree "<<endl;
1905     return(false);
1906     }
1907     if ( run->GetEntries() <= 0 ) return(false);
1908 mocchiut 1.17 //
1909 pam-fi 1.20
1910 mocchiut 1.35 // Int_t oldrun = irun;
1911 pam-fi 1.33 Long64_t oldrun = irun;
1912 mocchiut 1.35
1913 pam-fi 1.20 // --------------------------------------
1914     // if it is a full file (not preselected)
1915     // --------------------------------------
1916     if(SELLI==0){
1917 pam-fi 1.33
1918 pam-fi 1.36 //
1919     // the absolute time is necessary to relate the event with the run
1920     //
1921 pam-fi 1.43 if( !GetOrbitalInfo() && !ISGP){
1922 pam-fi 1.33 cout << "Bool_t PamLevel2::UpdateRunInfo(TChain *run, ULong64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
1923     return(false);
1924     }
1925    
1926 pam-fi 1.43 ULong64_t abstime = 0;
1927     if( GetOrbitalInfo() )abstime=GetOrbitalInfo()->absTime;
1928    
1929    
1930 pam-fi 1.36 //
1931     // the first time the routine is called, set run search from the beginning
1932     //
1933 mocchiut 1.35 if ( irun < 0LL ){
1934     irun = 0LL;
1935 pam-fi 1.26 run->GetEntry(irun);
1936 pam-fi 1.33 runfirstentry = 0LL;
1937     runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
1938     if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
1939 pam-fi 1.43
1940     if( ISGP && run->GetEntries()!=1 ){
1941     cout << "** WARNING ** simulated files are assumed to have 1 single run, not "<< run->GetEntries() << endl;
1942     cout << "** WARNING ** run will not be updated"<<endl;
1943     }
1944    
1945 pam-fi 1.24 };
1946 mocchiut 1.35 //
1947 pam-fi 1.43 if(ISGP)abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO
1948     //
1949     if ( irun == run->GetEntries()-1LL &&
1950     !(abstime >= GetRunInfo()->RUNHEADER_TIME &&
1951     abstime <= GetRunInfo()->RUNTRAILER_TIME)
1952 mocchiut 1.35 ){
1953     irun = -1LL;
1954     runfirstentry = 0LL;
1955     runlastentry = -1LL;
1956     };
1957 pam-fi 1.24 // modificato il controllo sull'aggiornamento del run, per evitare problemi
1958     // dovuti agli eventi annidati (NB! NEVENTS conta anche questi!!)
1959 mocchiut 1.31 //
1960     bool fromfirst = true;
1961     //
1962 pam-fi 1.43 while ( !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME) && irun < run->GetEntries()-1LL ){
1963 pam-fi 1.24 // while ( iev > (runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS-1)) && irun < run->GetEntries() ){
1964 pam-fi 1.20 irun++;
1965     run->GetEntry(irun);
1966 mocchiut 1.30 runfirstentry = runlastentry;
1967 pam-fi 1.33 if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runfirstentry += 1LL;
1968     runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
1969     // cout << " ))))) UPDATE RUN INFO ((((( @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
1970     // cout << "runfirstentry "<<runfirstentry<<endl;
1971 mocchiut 1.31 // printf(" iev %llu %u %llu \n",iev,this->GetRunInfo()->NEVENTS,(ULong64_t)(runfirstentry+(ULong64_t)(this->GetRunInfo()->NEVENTS)));
1972 pam-fi 1.43 // printf(" abstime %u trailertime %u \n",abstime,GetRunInfo()->RUNTRAILER_TIME);
1973 mocchiut 1.31 // printf(" IDRUN %u \n",GetRunInfo()->ID);
1974     //
1975 pam-fi 1.33 // prevshift = 0;
1976 mocchiut 1.31 //
1977 pam-fi 1.43 if ( irun == (Long64_t)(run->GetEntries()-1LL) && fromfirst && !(abstime >= GetRunInfo()->RUNHEADER_TIME && abstime <= GetRunInfo()->RUNTRAILER_TIME)){
1978 pam-fi 1.33 printf(" resetting irun (it should NOT happen!!!)\n");
1979 pam-fi 1.32 fromfirst = false;
1980     irun = 0;
1981     run->GetEntry(irun);
1982     runfirstentry = 0ULL;
1983 pam-fi 1.33 runlastentry += (Long64_t)(this->GetRunInfo()->NEVENTS);
1984     if ( (Long64_t)(this->GetRunInfo()->NEVENTS) > 0LL ) runlastentry -= 1LL;
1985 mocchiut 1.31 };
1986     //
1987 pam-fi 1.20 };
1988 mocchiut 1.31 //
1989 mocchiut 1.35 if (
1990 pam-fi 1.43 !(abstime >= GetRunInfo()->RUNHEADER_TIME &&
1991     abstime <= GetRunInfo()->RUNTRAILER_TIME)
1992 pam-fi 1.33 ) {
1993 mocchiut 1.44 printf(" Something very wrong here: cannot find RUN containing absolute time %llu \n",abstime);
1994 pam-fi 1.33 return false;
1995     }
1996 pam-fi 1.20 //
1997 mocchiut 1.30 if ( irun == oldrun || irun >= run->GetEntries() ) return(false);
1998 pam-fi 1.20 //
1999     // printf(" iev %llu irun %i nevents %u 1st %llu last %llu \n",iev,irun,this->GetRunInfo()->NEVENTS,(ULong64_t)runfirstentry,(ULong64_t)runlastentry);
2000     //
2001 pam-fi 1.33 prevshift = 0;
2002     cout << " ))))) UPDATE RUN INFO ((((( @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2003 pam-fi 1.34 // cout << "runfirstentry "<<runfirstentry<<endl;
2004 pam-fi 1.20 return(true);
2005 mocchiut 1.17 };
2006 pam-fi 1.20 // ----------------------------------------------------
2007     // if it is a preselected file (there is SelectionList)
2008     // NBNB - the event tree MUST be read first
2009     // ----------------------------------------------------
2010     if(SELLI==1){
2011     sel_tree->GetEntry(iev);
2012 pam-fi 1.34 // cout << irun << " "<< irunentry << endl;
2013 pam-fi 1.20 if(irun != oldrun){
2014     run->GetEntry(irun);
2015 pam-fi 1.33 cout << " ))))) UPDATE RUN INFO ((((( @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2016 pam-fi 1.32 prevshift = 0;
2017 pam-fi 1.20 return true;
2018     }
2019     return false;
2020     }
2021    
2022     return false;
2023 mocchiut 1.17 //
2024     };
2025 pam-fi 1.33
2026 pam-fi 1.36 Bool_t PamLevel2::UpdateRunInfo(Long64_t iev){
2027    
2028     if(!run_tree){
2029     cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree not loaded"<<endl;
2030     return false;
2031     }
2032     if ( run_tree->GetEntries() <= 0 ) {
2033     cout << " Bool_t PamLevel2::UpdateRunInfo(ULong64_t iev) -- ERROR -- run tree is empty"<<endl;
2034     return(false);
2035     }
2036 pam-fi 1.33
2037 pam-fi 1.36 Int_t oldrun = irun; // store current run index
2038    
2039     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2040     // if it is a full file (not preselected)
2041     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2042     if(SELLI==0){
2043    
2044 pam-fi 1.45 // ---------------------------------------------------------------
2045     // increment dead and live-time counters
2046     // ---------------------------------------------------------------
2047     if( GetTrigLevel2() ){
2048     totdltime[0]+=GetTrigLevel2()->dltime[0];
2049     totdltime[1]+=GetTrigLevel2()->dltime[1];
2050     }
2051     totdltime[2]++;
2052    
2053     // cout << setw(10)<<totdltime[0]<<setw(10)<<totdltime[1]<<setw(10)<<totdltime[2]<<endl;
2054    
2055     // ---------------------------------------------------------------
2056     // retrieve OBT and absolute time of the event
2057     // ---------------------------------------------------------------
2058 pam-fi 1.43 ULong64_t abstime = 0;
2059     ULong64_t obt = 0;
2060     if( GetOrbitalInfo() ){
2061     abstime = GetOrbitalInfo()->absTime;
2062     obt = GetOrbitalInfo()->OBT;
2063     }
2064 pam-fi 1.36 // ---------------------------------------------------------------
2065     // the absolute time is necessary to relate the event with the run
2066     // ---------------------------------------------------------------
2067 pam-fi 1.43 if( !GetOrbitalInfo() && !ISGP ){
2068 pam-fi 1.36 cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- missing OrbitalInfo "<<endl;
2069     return(false);
2070     }
2071    
2072     // -----------------------------------------------------------------------
2073     // the first time the routine is called, set run search from the beginning
2074     // -----------------------------------------------------------------------
2075     if ( irun < 0 ){
2076     irun = 0LL;
2077 pam-fi 1.39 irunentry = 0;
2078     prevshift = 0;
2079 pam-fi 1.36 run_tree->GetEntry(irun);
2080 pam-fi 1.43
2081     if( ISGP && run_tree->GetEntries()!=1 ){
2082     cout << "** WARNING ** simulated files are assumed to have 1 single run, not "<< run_tree->GetEntries() << endl;
2083     cout << "** WARNING ** run will not be updated"<<endl;
2084     }
2085 pam-fi 1.36 };
2086 pam-fi 1.43
2087     // -----------------------------------------------------------------------
2088 pam-fi 1.45 // if it is simulation, assigh abstime by hand (temporaneo!!!)
2089 pam-fi 1.43 // -----------------------------------------------------------------------
2090     if(ISGP){
2091     abstime = GetRunInfo()->RUNHEADER_TIME; // BARBATRUCCO
2092     obt = GetRunInfo()->RUNHEADER_OBT; // BARBATRUCCO
2093     }
2094 pam-fi 1.36 //
2095     bool fromfirst = true; // first loop over runs
2096 pam-fi 1.39
2097     // Bool_t hasfrag = false;
2098     // if( GetRunInfo()->ID_RUN_FRAG!=0 && GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID)hasfrag=true;
2099     // ULong64_t fragid = GetRunInfo()->ID_RUN_FRAG;
2100    
2101 pam-fi 1.36 // ------------------------------------------------------
2102     // loop over runs to find the one that contains the event
2103     // ------------------------------------------------------
2104 pam-fi 1.39 while (
2105     (
2106     (
2107 pam-fi 1.43 !(abstime >= GetRunInfo()->RUNHEADER_TIME && // check on absolute time (s)
2108     abstime <= GetRunInfo()->RUNTRAILER_TIME) &&
2109     !(obt >= GetRunInfo()->RUNHEADER_OBT && // additional check on OBT (ms)
2110     obt <= GetRunInfo()->RUNTRAILER_OBT)
2111 pam-fi 1.39 )
2112     || GetRunInfo()->NEVENTS==0
2113     || !(irunentry < GetRunInfo()->NEVENTS-1-prevshift)
2114     )
2115     && irun < run_tree->GetEntries() ){
2116    
2117 pam-fi 1.45
2118    
2119    
2120     // -----------------------------------------
2121     // store dead and live-time of previous run
2122     // -----------------------------------------
2123     if(fromfirst){
2124     if(oldrun==irun){
2125     /// decrement counters
2126     if( GetTrigLevel2()){
2127     totdltime[0]-=GetTrigLevel2()->dltime[0];//live-time
2128     totdltime[1]-=GetTrigLevel2()->dltime[1];//dead-time
2129     }
2130     totdltime[2]--; //event counter
2131     cout << endl;
2132     cout << "n.events : "<<totdltime[2]<<endl;
2133     cout << "RUN LIVE-TIME: "<<totdltime[0]*0.16<<" ms"<<endl;
2134     cout << "RUN DEAD-TIME: "<<totdltime[1]*0.01<<" ms"<<endl;
2135     }else{
2136     totdltime[0]=0;//live-time
2137     totdltime[1]=0;//dead-time
2138     totdltime[2]=0; //event counter
2139     cout << " *** JUMP RUN *** irun "<<irun<<endl;
2140     }
2141     /// add an entry
2142     if(run_tree_clone)
2143     if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
2144     run_tree_clone->GetBranch("DeadLiveTime")->Fill();
2145     /// reset counters
2146     if( GetTrigLevel2() ){
2147     totdltime[0]=GetTrigLevel2()->dltime[0];//live-time
2148     totdltime[1]=0; //dead-time
2149     }
2150     totdltime[2]=1; //event counter
2151     }
2152    
2153    
2154 pam-fi 1.36 irun++;
2155     // ------------------------------------
2156     // if the end of run tree is reached...
2157     // ------------------------------------
2158     if( irun == run_tree->GetEntries() ){
2159     if(!fromfirst){
2160     // -----------------------------------------------------
2161     // if it happened already once and the run was not found
2162     // ---> exit with error
2163     // -----------------------------------------------------
2164     cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- ERROR -- event entry #"<<iev<<" does not belong to any run (should not happen)" <<endl;
2165     return false;
2166     }
2167     // -----------------------------------------
2168     // ...otherwise repeat search from beginning
2169     // -----------------------------------------
2170     cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- reached end of run tree. searchin again from beginning " <<endl;
2171     fromfirst = false;
2172     irun = 0LL;
2173     runfirstentry = 0LL;
2174     }
2175     // -------------------------------------------------------------------
2176     // save the index of the first entry of the run, relative to pam_tree,
2177     // and read a new run
2178     // -------------------------------------------------------------------
2179 pam-fi 1.39 if(irun>0)runfirstentry += (GetRunInfo()->NEVENTS)-prevshift;
2180     irunentry = 0;
2181 pam-fi 1.45 prevshift = 0;
2182 pam-fi 1.36 run_tree->GetEntry(irun);
2183 pam-fi 1.39 if(GetRunInfo()->RUNHEADER_OBT>GetRunInfo()->RUNTRAILER_OBT ){
2184 pam-fi 1.36 cout << "Bool_t PamLevel2::UpdateRunInfo(Long64_t iev) -- WARNING -- irun "<<irun<<" has RUNHEADER_OBT>=RUNTRAILER_OBT " <<endl;
2185     cout << " (NB!! in this case some events are assigned to a wrong run)"<<endl;
2186     }
2187 pam-fi 1.39 // if(hasfrag && fragid != GetRunInfo()->ID){
2188     // cout << "... where is the next fragment ??"<<endl;
2189     // }
2190 pam-fi 1.36 };
2191 pam-fi 1.39
2192    
2193 pam-fi 1.36 // --------------------------------------
2194     // if there was no need to update the run
2195     // ---> exit with FALSE
2196     // --------------------------------------
2197     if ( irun == oldrun ) return(false);
2198    
2199     // --------------------------------------
2200     // ... otherwise
2201 pam-fi 1.45 // --------------------------------------
2202    
2203    
2204    
2205     // // -----------------------------------------
2206     // // store dead and live-time of previous run
2207     // // -----------------------------------------
2208     // // --------------------------------------------------------------
2209     // // if the run is empty, fill the dead-live time branch with zeros
2210     // // --------------------------------------------------------------
2211     // /// if some runs have been jumped, fill with zeros
2212     // if(irun-oldrun>1){
2213     // ULong64_t temp[3];
2214     // temp[0]=totdltime[0];
2215     // temp[1]=totdltime[1];
2216     // temp[2]=totdltime[2];
2217     // for(int i=oldrun+1; i<irun; irun++){
2218     // totdltime[0]=0;
2219     // totdltime[1]=0;
2220     // totdltime[2]=0;
2221     // cout << " ** JUMPED RUN **"<<endl;
2222     // if(run_tree_clone)
2223     // if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
2224     // run_tree_clone->GetBranch("DeadLiveTime")->Fill();
2225     // }
2226     // totdltime[0]=temp[0];
2227     // totdltime[1]=temp[1];
2228     // totdltime[2]=temp[2];
2229     // }
2230     // /// decrement counters
2231     // if( GetTrigLevel2() ){
2232     // totdltime[0]-=GetTrigLevel2()->dltime[0];//live-time
2233     // totdltime[1]-=GetTrigLevel2()->dltime[1];//dead-time
2234     // }
2235     // totdltime[2]--; //event counter
2236     // /// add an entry
2237     // if(irun>0 ){
2238     // cout << endl;
2239     // cout << "n.events : "<<totdltime[2]<<endl;
2240     // cout << "RUN LIVE-TIME: "<<totdltime[0]*0.16<<" ms"<<endl;
2241     // cout << "RUN DEAD-TIME: "<<totdltime[1]*0.01<<" ms"<<endl;
2242     // if(run_tree_clone)
2243     // if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
2244     // run_tree_clone->GetBranch("DeadLiveTime")->Fill();
2245     // }
2246     // /// reset counters
2247     // if( GetTrigLevel2() ){
2248     // totdltime[0]=GetTrigLevel2()->dltime[0];//live-time
2249     // totdltime[1]=0; //dead-time
2250     // }
2251     // totdltime[2]=1; //event counter
2252    
2253    
2254     // --------------------------------------
2255 pam-fi 1.36 // ---> exit with TRUE
2256     // --------------------------------------
2257     cout << endl << " ))))) UPDATE RUN INFO ((((( @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2258 pam-fi 1.39 // ----------------------------------------------------
2259     // then check if the run has a fragment
2260     // in this case we have to switch to the next fragment
2261     // when the end of the first fragment is reached
2262     // ----------------------------------------------------
2263     if(
2264     GetRunInfo()->ID_RUN_FRAG != 0 &&
2265     // GetRunInfo()->ID_RUN_FRAG != GetRunInfo()->ID &&
2266     true ){
2267     cout << "* fragment *"<<endl;
2268     }
2269 pam-fi 1.45
2270 pam-fi 1.36 return(true);
2271     };
2272     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2273     // if it is a preselected file (there is SelectionList)
2274     // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
2275     if(SELLI==1){
2276     sel_tree->GetEntry(iev);
2277     if(irun != oldrun){
2278     run_tree->GetEntry(irun);
2279     cout << endl << " ))))) UPDATE RUN INFO ((((( @iev "<<iev<<" run "<<GetRunInfo()->ID<<" irun "<<irun<<endl;
2280     prevshift = 0;
2281     return true;
2282     }
2283     return false;
2284     }
2285    
2286     return false;
2287     //
2288     };
2289 mocchiut 1.17 /**
2290     * Update the runinfo informations (to be used to have Run infos event by event basis)
2291     * @param run Pointer to the chain/tree which contains run infos
2292     * @return true if a new run has been read, false if it is still the same run
2293     */
2294 pam-fi 1.33 Bool_t PamLevel2::UpdateRunInfo(TTree *run, Long64_t iev){
2295 mocchiut 1.17 return(UpdateRunInfo((TChain*)run,iev));
2296     };
2297    
2298 pam-fi 1.2 //--------------------------------------
2299     //
2300     //
2301     //--------------------------------------
2302     /**
2303 pam-fi 1.8 * Set which trees shoul be analysed
2304 pam-fi 1.2 * @param detlist TString containing the sequence of trees required
2305     */
2306     void PamLevel2::SetWhichTrees(TString detlist){
2307    
2308 pam-fi 1.12 if(detlist.IsNull() || detlist.Contains("+ALL", TString::kIgnoreCase)){
2309 pam-fi 1.20 CAL0 = false;
2310 pam-fi 1.8 CAL1 = true;
2311     CAL2 = true;
2312     TRK2 = true;
2313     TRK1 = false;
2314     TRKh = false;
2315 pam-fi 1.20 TRK0 = false;
2316 pam-fi 1.6 TRG = true;
2317     TOF = true;
2318 pam-fi 1.20 TOF0 = false;
2319 pam-fi 1.6 S4 = true;
2320     ND = true;
2321     AC = true;
2322     ORB = true;
2323     }else if( detlist.Contains("-ALL", TString::kIgnoreCase) ){
2324 pam-fi 1.20 CAL0 = false;
2325 pam-fi 1.8 CAL1 = false;
2326     CAL2 = false;
2327     TRK2 = false;
2328     TRK1 = false;
2329     TRKh = false;
2330 pam-fi 1.20 TRK0 = false;
2331 pam-fi 1.6 TRG = false;
2332     TOF = false;
2333 pam-fi 1.20 TOF0 = false;
2334 pam-fi 1.6 S4 = false;
2335     ND = false;
2336     AC = false;
2337     ORB = false;
2338     };
2339    
2340 pam-fi 1.8 // -------------------------------------------------------------------------
2341     if( detlist.Contains("CAL1", TString::kIgnoreCase) ){
2342     if ( detlist.Contains("-CAL1", TString::kIgnoreCase) )CAL1=false;
2343     if ( detlist.Contains("+CAL1", TString::kIgnoreCase) )CAL1=true;
2344     };
2345 pam-fi 1.20
2346     if( detlist.Contains("CAL0", TString::kIgnoreCase) ){
2347     if ( detlist.Contains("-CAL0", TString::kIgnoreCase) )CAL0=false;
2348     if ( detlist.Contains("+CAL0", TString::kIgnoreCase) )CAL0=true;
2349     };
2350 pam-fi 1.8
2351     if( detlist.Contains("CAL2", TString::kIgnoreCase)){
2352     if ( detlist.Contains("-CAL2", TString::kIgnoreCase) )CAL2=false;
2353     if ( detlist.Contains("+CAL2", TString::kIgnoreCase) )CAL2=true;
2354     };
2355    
2356     if( detlist.Contains("+CAL", TString::kIgnoreCase) && !CAL1 && !CAL2 )CAL2=true;
2357     if( detlist.Contains("-CAL", TString::kIgnoreCase) && CAL1 && CAL2 ){
2358     CAL2=false;
2359     CAL1=false;
2360     }
2361     // -------------------------------------------------------------------------
2362 pam-fi 1.20 if( detlist.Contains("TRK0", TString::kIgnoreCase) ){
2363     if ( detlist.Contains("-TRK0", TString::kIgnoreCase) )TRK0=false;
2364     if ( detlist.Contains("+TRK0", TString::kIgnoreCase) )TRK0=true;
2365     };
2366    
2367 pam-fi 1.8 if( detlist.Contains("TRK1", TString::kIgnoreCase) ){
2368     if ( detlist.Contains("-TRK1", TString::kIgnoreCase) )TRK1=false;
2369     if ( detlist.Contains("+TRK1", TString::kIgnoreCase) )TRK1=true;
2370     };
2371    
2372     if( detlist.Contains("TRK2", TString::kIgnoreCase)){
2373     if ( detlist.Contains("-TRK2", TString::kIgnoreCase) )TRK2=false;
2374     if ( detlist.Contains("+TRK2", TString::kIgnoreCase) )TRK2=true;
2375     };
2376    
2377     if( detlist.Contains("TRKh", TString::kIgnoreCase)){
2378     if ( detlist.Contains("-TRKh", TString::kIgnoreCase) )TRKh=false;
2379     if ( detlist.Contains("+TRKh", TString::kIgnoreCase) )TRKh=true;
2380     };
2381    
2382     if( detlist.Contains("+TRK", TString::kIgnoreCase) && !TRK1 && !TRK2 && !TRKh )TRK2=true;
2383     if( detlist.Contains("-TRK", TString::kIgnoreCase) && TRK1 && TRK2 && TRKh){
2384     TRK2=false;
2385     TRK1=false;
2386     TRKh=false;
2387     }
2388     // -------------------------------------------------------------------------
2389 pam-fi 1.6
2390     if( detlist.Contains("-TRG", TString::kIgnoreCase) )TRG = false;
2391     else if( detlist.Contains("+TRG", TString::kIgnoreCase) )TRG = true;
2392    
2393     if( detlist.Contains("-TOF", TString::kIgnoreCase) )TOF = false;
2394     else if( detlist.Contains("+TOF", TString::kIgnoreCase) )TOF = true;
2395 pam-fi 1.20
2396     if( detlist.Contains("-TOF0", TString::kIgnoreCase) )TOF0 = false;
2397     else if( detlist.Contains("+TOF0", TString::kIgnoreCase) )TOF0 = true;
2398 pam-fi 1.6
2399     if( detlist.Contains("-S4", TString::kIgnoreCase) )S4 = false;
2400     else if( detlist.Contains("+S4", TString::kIgnoreCase) )S4 = true;
2401    
2402     if( detlist.Contains("-ND", TString::kIgnoreCase) )ND = false;
2403     else if( detlist.Contains("+ND", TString::kIgnoreCase) )ND = true;
2404    
2405     if( detlist.Contains("-AC", TString::kIgnoreCase) )AC = false;
2406     else if( detlist.Contains("+AC", TString::kIgnoreCase) )AC = true;
2407    
2408     if( detlist.Contains("-ORB", TString::kIgnoreCase) )ORB = false;
2409     else if( detlist.Contains("+ORB", TString::kIgnoreCase) )ORB = true;
2410 pam-fi 1.11
2411 pam-fi 1.12 // cout<< "Set detector list --> ";
2412     // if(TRK1)cout<<"TRK1 ";
2413     // if(TRK2)cout<<"TRK2 ";
2414     // if(TRKh)cout<<"TRKH ";
2415     // if(CAL1)cout<<"CAL1 ";
2416     // if(CAL2)cout<<"CAL2 ";
2417     // if(TOF)cout<<"TOF ";
2418     // if(TRG)cout<<"TRG ";
2419     // if(AC)cout<<"AC ";
2420     // if(ND)cout<<"ND ";
2421     // if(S4)cout<<"S4 ";
2422     // if(ORB)cout<<"ORB ";
2423     // cout << endl;
2424 pam-fi 1.6
2425 pam-fi 1.2 };
2426 pam-fi 1.11
2427    
2428     /**
2429     * Set tree/branch detector flags from the content of a tree
2430     */
2431     void PamLevel2::GetWhichTrees(TFile* f){
2432 pam-fi 1.20
2433    
2434    
2435     cout << "void PamLevel2::GetWhichTrees(TFile* f) --- WARNING!! --- ...potrebbe non funzionare "<<endl;
2436 pam-fi 1.11 // -----------
2437     // reset flags
2438     // -----------
2439 pam-fi 1.12 CAL1 = false;
2440     CAL2 = false;
2441     TRK2 = false;
2442     TRK1 = false;
2443     TRKh = false;
2444     TRG = false;
2445     TOF = false;
2446     S4 = false;
2447     ND = false;
2448     AC = false;
2449     ORB = false;
2450 pam-fi 1.11
2451 pam-fi 1.12 RUN = false;
2452 pam-fi 1.11
2453     cout << "Checking file: "<<f->GetName()<<endl;
2454     if( !f || f->IsZombie() ){
2455     cout << "File: "<< f->GetName() <<" Non valid root file"<< endl;
2456     return;
2457     }
2458    
2459     TList *lk = f->GetListOfKeys();
2460     TIter next(lk);
2461     TKey *key =0;
2462    
2463     Int_t nev = 0;
2464    
2465     while( (key = (TKey*)next()) ){
2466    
2467     if( !strcmp(key->GetName(),"Run" ) )RUN = true;
2468    
2469     //=========================================================
2470     if( !strcmp(key->GetName(),"Trigger" ) ){
2471     TRG = true;
2472     Int_t nevt = ((TTree*)f->Get("Trigger"))->GetEntries();
2473     if( nev && nevt!=nev){
2474     cout << "File: "<< f->GetName() <<" Trigger tree has "<<nevt<<" events instead of "<<nev<< endl;
2475     TRG = false;
2476     }else nev=nevt;
2477     }
2478     //=========================================================
2479     if( !strcmp(key->GetName(),"ToF" ) ){
2480     TOF = true;
2481     Int_t nevt = ((TTree*)f->Get("ToF"))->GetEntries();
2482     if( nev && nevt!=nev){
2483     cout << "File: "<< f->GetName() <<" ToF tree has "<<nevt<<" events instead of "<<nev<< endl;
2484     TOF = false;
2485     }else nev=nevt;
2486     }
2487     //=========================================================
2488     if( !strcmp(key->GetName(),"S4" ) ){
2489     S4 = true;
2490     Int_t nevt = ((TTree*)f->Get("S4"))->GetEntries();
2491     if( nev && nevt!=nev){
2492     cout << "File: "<< f->GetName() <<" S4 tree has "<<nevt<<" events instead of "<<nev<< endl;
2493     S4 = false;
2494     }else nev=nevt;
2495     }
2496     //=========================================================
2497    
2498     if( !strcmp(key->GetName(),"NeutronD" ) ){
2499     ND = true;
2500     Int_t nevt = ((TTree*)f->Get("NeutronD"))->GetEntries();
2501     if( nev && nevt!=nev){
2502     cout << "File: "<< f->GetName() <<"NeutronD tree has "<<nevt<<" events instead of "<<nev<< endl;
2503     ND =false;
2504     }else nev=nevt;
2505     }
2506     //=========================================================
2507     if( !strcmp(key->GetName(),"Anticounter") ){
2508     AC = true;
2509     Int_t nevt = ((TTree*)f->Get("Anticounter"))->GetEntries();
2510     if( nev && nevt!=nev){
2511     cout << "File: "<< f->GetName() <<" Anticounter tree has "<<nevt<<" events instead of "<<nev<< endl;
2512     AC =false;
2513     }else nev=nevt;
2514     }
2515     //=========================================================
2516     if( !strcmp(key->GetName(),"OrbitalInfo") ){
2517     ORB = true;
2518     Int_t nevt = ((TTree*)f->Get("OrbitalInfo"))->GetEntries();
2519     if( nev && nevt!=nev){
2520     cout << "File: "<< f->GetName() <<" OrbitalInfo tree has "<<nevt<<" events instead of "<<nev<< endl;
2521     ORB = false;
2522     }else nev=nevt;
2523     }
2524     //=========================================================
2525     if( !strcmp(key->GetName(),"Tracker" ) ){
2526     TTree *T = (TTree*)f->Get("Tracker");
2527     for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
2528     TString name = T->GetListOfBranches()->At(i)->GetName();
2529     if( !name.CompareTo("TrkLevel1") )TRK1=true;
2530     if( !name.CompareTo("TrkLevel2") )TRK2=true;
2531     if( !name.CompareTo("TrkHough") )TRKh=true;
2532     };
2533     Int_t nevt = T->GetEntries();
2534     if( nev && nevt!=nev){
2535     cout << "File: "<< f->GetName() <<" Tracker tree has "<<nevt<<" events instead of "<<nev<< endl;
2536     TRK1 = false;
2537     TRK2 = false;
2538     TRKh = false;
2539     }else nev=nevt;
2540     T->Delete();
2541     };
2542     //=========================================================
2543     if( !strcmp(key->GetName(),"Calorimeter" ) ){
2544     TTree *T = (TTree*)f->Get("Calorimeter");
2545     for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
2546     TString name = T->GetListOfBranches()->At(i)->GetName();
2547     if( !name.CompareTo("CaloLevel1") )CAL1=true;
2548     if( !name.CompareTo("CaloLevel2") )CAL2=true;
2549     };
2550     Int_t nevt = T->GetEntries();
2551     if( nev && nevt!=nev){
2552     cout << "File: "<< f->GetName() <<" Calorimeter tree has "<<nevt<<" events instead of "<<nev<< endl;
2553     CAL1 = false;
2554     CAL2 = false;
2555     }else nev=nevt;
2556     T->Delete();
2557     };
2558    
2559     };
2560    
2561 pam-fi 1.13 delete lk;
2562 pam-fi 1.12
2563     // cout<< "Get detector list from input file --> ";
2564     // if(TRK1)cout<<"TRK1 ";
2565     // if(TRK2)cout<<"TRK2 ";
2566     // if(TRKh)cout<<"TRKH ";
2567     // if(CAL1)cout<<"CAL1 ";
2568     // if(CAL2)cout<<"CAL2 ";
2569     // if(TOF)cout<<"TOF ";
2570     // if(TRG)cout<<"TRG ";
2571     // if(AC)cout<<"AC ";
2572     // if(ND)cout<<"ND ";
2573     // if(S4)cout<<"S4 ";
2574     // if(ORB)cout<<"ORB ";
2575     // cout << endl;
2576 pam-fi 1.11
2577     return ;
2578    
2579     };
2580    
2581    
2582 pam-fi 1.2 //--------------------------------------
2583     //
2584     //
2585     //--------------------------------------
2586     /**
2587 pam-fi 1.3 * Check if a file contains selected Pamela Level2 trees.
2588 pam-fi 1.2 * @param name File name
2589 pam-fi 1.4 * @return true if the file is ok.
2590 pam-fi 1.2 */
2591     Bool_t PamLevel2::CheckLevel2File(TString name){
2592    
2593 pam-fi 1.8 Bool_t CAL1__ok = false;
2594     Bool_t CAL2__ok = false;
2595     Bool_t TRK2__ok = false;
2596     Bool_t TRK1__ok = false;
2597     Bool_t TRKh__ok = false;
2598 pam-fi 1.7 Bool_t TRG__ok = false;
2599     Bool_t TOF__ok = false;
2600     Bool_t S4__ok = false;
2601     Bool_t ND__ok = false;
2602     Bool_t AC__ok = false;
2603     Bool_t ORB__ok = false;
2604 pam-fi 1.6
2605 pam-fi 1.7 Bool_t RUN__ok = false;
2606 pam-fi 1.6
2607 pam-fi 1.20 Bool_t SELLI__ok = false;
2608 pam-fi 1.8
2609     cout << "Checking file: "<<name<<endl;
2610 pam-fi 1.6 TFile *f = new TFile(name.Data());
2611     if( !f || f->IsZombie() ){
2612     cout << "File: "<< f->GetName() <<" discarded ---- Non valid root file"<< endl; return false;
2613     }
2614 pam-fi 1.7 // cout << "Get list of keys: "<<f<<endl;
2615 pam-fi 1.6 TList *lk = f->GetListOfKeys();
2616 pam-fi 1.8 // lk->Print();
2617 pam-fi 1.6 TIter next(lk);
2618     TKey *key =0;
2619 pam-fi 1.8
2620     Int_t nev = 0;
2621    
2622 pam-fi 1.6 while( (key = (TKey*)next()) ){
2623    
2624 pam-fi 1.20 // cout << key->GetName() << endl;
2625 pam-fi 1.12 // cout << key->GetName() << ""<<key->GetClassName()<<endl;
2626     // cout << " Get tree: " << f->Get(key->GetName())<<endl;
2627 pam-fi 1.7 // nev_previous = nev;
2628     // cout << " n.entries "<< nev <<endl;
2629     // if( key->GetClassName()=="TTree" && nev_previous && nev != nev_previous ){
2630     // nev = ((TTree*)f->Get(key->GetName()))->GetEntries();
2631     // cout << "File: "<< f->GetName() <<" discarded ---- "<< key->GetName() << " tree: n.entries does not match "<<nev<<" "<<nev_previous<< endl;
2632     // return false;
2633     // };
2634 pam-fi 1.6
2635 pam-fi 1.20
2636 pam-fi 1.6 if( !strcmp(key->GetName(),"Run" ) )RUN__ok = true;
2637 pam-fi 1.8
2638     //=========================================================
2639 pam-fi 1.20 if( !strcmp(key->GetName(),"SelectionList" ) ){
2640     SELLI__ok = true;
2641     if(SELLI==1){
2642     Int_t nevt = ((TTree*)f->Get("SelectionList"))->GetEntries();
2643     if( nev && nevt!=nev){
2644     cout << "File: "<< f->GetName() <<" discarded ---- SelectionList tree has "<<nevt<<" events instead of "<<nev<< endl;
2645     return false;
2646     }
2647     nev=nevt;
2648     }
2649     }
2650    
2651     //=========================================================
2652 pam-fi 1.8 if( !strcmp(key->GetName(),"Trigger" ) ){
2653     TRG__ok = true;
2654     if(TRG){
2655     Int_t nevt = ((TTree*)f->Get("Trigger"))->GetEntries();
2656     if( nev && nevt!=nev){
2657     cout << "File: "<< f->GetName() <<" discarded ---- Trigger tree has "<<nevt<<" events instead of "<<nev<< endl;
2658     return false;
2659     }
2660     nev=nevt;
2661     }
2662     }
2663     //=========================================================
2664     if( !strcmp(key->GetName(),"ToF" ) ){
2665     TOF__ok = true;
2666     if(TOF){
2667     Int_t nevt = ((TTree*)f->Get("ToF"))->GetEntries();
2668     if( nev && nevt!=nev){
2669     cout << "File: "<< f->GetName() <<" discarded ---- ToF tree has "<<nevt<<" events instead of "<<nev<< endl;
2670     return false;
2671     }
2672     nev=nevt;
2673     }
2674     }
2675     //=========================================================
2676     if( !strcmp(key->GetName(),"S4" ) ){
2677     S4__ok = true;
2678     if(S4){
2679     Int_t nevt = ((TTree*)f->Get("S4"))->GetEntries();
2680     if( nev && nevt!=nev){
2681     cout << "File: "<< f->GetName() <<" discarded ---- S4 tree has "<<nevt<<" events instead of "<<nev<< endl;
2682     return false;
2683     }
2684     nev=nevt;
2685     }
2686     }
2687     //=========================================================
2688    
2689     if( !strcmp(key->GetName(),"NeutronD" ) ){
2690     ND__ok = true;
2691     if(ND){
2692     Int_t nevt = ((TTree*)f->Get("NeutronD"))->GetEntries();
2693     if( nev && nevt!=nev){
2694     cout << "File: "<< f->GetName() <<" discarded ---- NeutronD tree has "<<nevt<<" events instead of "<<nev<< endl;
2695     return false;
2696     }
2697     nev=nevt;
2698     }
2699     }
2700     //=========================================================
2701     if( !strcmp(key->GetName(),"Anticounter") ){
2702     AC__ok = true;
2703     if(AC){
2704     Int_t nevt = ((TTree*)f->Get("Anticounter"))->GetEntries();
2705     if( nev && nevt!=nev){
2706     cout << "File: "<< f->GetName() <<" discarded ---- Anticounter tree has "<<nevt<<" events instead of "<<nev<< endl;
2707     return false;
2708     }
2709     nev=nevt;
2710     }
2711     }
2712     //=========================================================
2713     if( !strcmp(key->GetName(),"OrbitalInfo") ){
2714     ORB__ok = true;
2715     if(ORB){
2716     Int_t nevt = ((TTree*)f->Get("OrbitalInfo"))->GetEntries();
2717     if( nev && nevt!=nev){
2718     cout << "File: "<< f->GetName() <<" discarded ---- OrbitalInfo tree has "<<nevt<<" events instead of "<<nev<< endl;
2719     return false;
2720     }
2721     nev=nevt;
2722     }
2723     }
2724     //=========================================================
2725 pam-fi 1.6 if( !strcmp(key->GetName(),"Tracker" ) ){
2726     TTree *T = (TTree*)f->Get("Tracker");
2727 pam-fi 1.8 if(TRK1||TRK2||TRKh){
2728     Int_t nevt = T->GetEntries();
2729     if( nev && nevt!=nev){
2730     cout << "File: "<< f->GetName() <<" discarded ---- Tracker tree has "<<nevt<<" events instead of "<<nev<< endl;
2731     return false;
2732     }
2733     nev=nevt;
2734     }
2735 pam-fi 1.6 for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
2736     TString name = T->GetListOfBranches()->At(i)->GetName();
2737 pam-fi 1.8 if( !name.CompareTo("TrkLevel1") )TRK1__ok=true;
2738     if( !name.CompareTo("TrkLevel2") )TRK2__ok=true;
2739     if( !name.CompareTo("TrkHough") )TRKh__ok=true;
2740     };
2741     T->Delete();
2742 pam-fi 1.2 };
2743 pam-fi 1.8 //=========================================================
2744     if( !strcmp(key->GetName(),"Calorimeter" ) ){
2745     TTree *T = (TTree*)f->Get("Calorimeter");
2746     if(CAL1||CAL2){
2747     Int_t nevt = T->GetEntries();
2748     if( nev && nevt!=nev){
2749     cout << "File: "<< f->GetName() <<" discarded ---- Calorimeter tree has "<<nevt<<" events instead of "<<nev<< endl;
2750     return false;
2751     }
2752     nev=nevt;
2753     }
2754     for(Int_t i=0; i<T->GetListOfBranches()->GetEntries(); i++){
2755     TString name = T->GetListOfBranches()->At(i)->GetName();
2756     if( !name.CompareTo("CaloLevel1") )CAL1__ok=true;
2757     if( !name.CompareTo("CaloLevel2") )CAL2__ok=true;
2758     };
2759     T->Delete();
2760     };
2761    
2762 pam-fi 1.6 };
2763 pam-fi 1.20
2764     if( SELLI==-1 )SELLI = (Int_t)SELLI__ok;
2765     if( SELLI==0 && SELLI__ok ){
2766     cout << "File: "<< f->GetName() <<" discarded ---- found SelectionList (it is not a full-event file)" << endl;
2767     return false;
2768     }
2769     if( SELLI==1 && !SELLI__ok ){
2770     cout << "File: "<< f->GetName() <<" discarded ---- SelectionList missing" << endl;
2771     return false;
2772     }
2773 pam-fi 1.6
2774 pam-fi 1.20 // cout << "SELLI "<<SELLI<<endl;
2775    
2776 pam-fi 1.12 // cout<< "CheckLevel2File(TString): detector list --> ";
2777     // if(TRK1__ok)cout<<"TRK1 ";
2778     // if(TRK2__ok)cout<<"TRK2 ";
2779     // if(TRKh__ok)cout<<"TRKH ";
2780     // if(CAL1__ok)cout<<"CAL1 ";
2781     // if(CAL2__ok)cout<<"CAL2 ";
2782     // if(TOF__ok)cout<<"TOF ";
2783     // if(TRG__ok)cout<<"TRG ";
2784     // if(AC__ok)cout<<"AC ";
2785     // if(ND__ok)cout<<"ND ";
2786     // if(S4__ok)cout<<"S4 ";
2787     // if(ORB__ok)cout<<"ORB ";
2788     // cout << endl;
2789 pam-fi 1.8
2790    
2791 pam-fi 1.13 if(TRK2 && TRK1__ok)TRK1=1;
2792     // ----------------------------------------------------------------------------
2793     // NOTA
2794     // se c'e` il level1, lo devo necessarimente leggere.
2795     // infatti (non ho capito perche`) i cluster vengono letti e allocati in memoria
2796     // comunque, ma non vengono disallocati da PamLevel2::Clear()
2797     // ----------------------------------------------------------------------------
2798    
2799    
2800 pam-fi 1.6 if(!RUN__ok) {
2801 pam-fi 1.9 cout << "File: "<< f->GetName() <<" *WARNING* ---- Missing RunInfo tree"<< endl;
2802     // return false;
2803 pam-fi 1.8 };
2804    
2805     if(CAL1 && !CAL1__ok){
2806     cout << "File: "<< f->GetName() <<" discarded ---- Missing CaloLevel1 branch"<< endl;
2807     return false;
2808     };
2809     if(CAL2 && !CAL2__ok){
2810     cout << "File: "<< f->GetName() <<" discarded ---- Missing CaloLevel2 branch"<< endl;
2811     return false;
2812     };
2813     if(TRK2 && !TRK2__ok){
2814     cout << "File: "<< f->GetName() <<" discarded ---- Missing TrkLevel2 branch"<< endl;
2815     return false;
2816     };
2817     if(TRK1 && !TRK1__ok){
2818     cout << "File: "<< f->GetName() <<" discarded ---- Missing TrkLevel1 branch"<< endl;
2819     return false;
2820     };
2821     if(TRKh && !TRKh__ok){
2822     cout << "File: "<< f->GetName() <<" discarded ---- Missing TrkHough branch"<< endl;
2823     return false;
2824 pam-fi 1.6 };
2825 pam-fi 1.8 if(ORB && !ORB__ok){
2826     cout << "File: "<< f->GetName() <<" discarded ---- Missing ORB tree"<< endl;
2827     return false;
2828 pam-fi 1.6 };
2829 pam-fi 1.8 if(AC && !AC__ok){
2830     cout << "File: "<< f->GetName() <<" discarded ---- Missing AC tree"<< endl;
2831     return false;
2832 pam-fi 1.6 };
2833     if(S4 && !S4__ok){
2834 pam-fi 1.8 cout << "File: "<< f->GetName() <<" discarded ---- Missing S4 tree"<< endl;
2835     return false;
2836 pam-fi 1.6 };
2837 pam-fi 1.8 if(TOF && !TOF__ok){
2838     cout << "File: "<< f->GetName() <<" discarded ---- Missing ToF tree"<< endl;
2839     return false;
2840 pam-fi 1.6 };
2841 pam-fi 1.8
2842 pam-fi 1.12 if(ND && !ND__ok){
2843     cout << "File: "<< f->GetName() <<" discarded ---- Missing ND tree"<< endl;
2844     return false;
2845     };
2846     if(TRG && !TRG__ok){
2847     cout << "File: "<< f->GetName() <<" discarded ---- Missing Trigger tree"<< endl;
2848     return false;
2849     };
2850    
2851 pam-fi 1.8
2852 pam-fi 1.13 // lk->Delete();
2853     // delete lk;
2854 pam-fi 1.8 f->Close();
2855 pam-fi 1.12
2856     // cout<< "CheckLevel2File(TString): detector list --> ";
2857     // if(TRK1)cout<<"TRK1 ";
2858     // if(TRK2)cout<<"TRK2 ";
2859     // if(TRKh)cout<<"TRKH ";
2860     // if(CAL1)cout<<"CAL1 ";
2861     // if(CAL2)cout<<"CAL2 ";
2862     // if(TOF)cout<<"TOF ";
2863     // if(TRG)cout<<"TRG ";
2864     // if(AC)cout<<"AC ";
2865     // if(ND)cout<<"ND ";
2866     // if(S4)cout<<"S4 ";
2867     // if(ORB)cout<<"ORB ";
2868     // cout << endl;
2869 pam-fi 1.8
2870     return true;
2871 pam-fi 1.2
2872     };
2873    
2874    
2875 pam-fi 1.9 /**
2876     * Create clone-trees
2877     */
2878 pam-fi 1.11 void PamLevel2::CreateCloneTrees0( TChain *fChain, TFile *ofile ){
2879 pam-fi 1.9
2880     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2881     cout << "Create clones of PAMELA trees "<<endl;
2882    
2883     Int_t i=0;
2884 pam-fi 1.20 pam_tree_clone[i] = fChain->GetTree()->CloneTree(0);
2885     TString name = pam_tree_clone[i]->GetName();
2886 pam-fi 1.9 name.Append("_clone");
2887 pam-fi 1.20 // pam_tree_clone[i]->SetName(name.Data());
2888     cout << pam_tree_clone[i]->GetName() <<endl;
2889 pam-fi 1.9 i++;
2890    
2891     TList *li = fChain->GetListOfFriends();
2892     TIter next(li);
2893     TFriendElement* T_friend=0;
2894 pam-fi 1.11 ofile->cd();
2895 pam-fi 1.9 while( (T_friend = (TFriendElement*)next()) ){
2896     // cout<<T_friend->IsA()->GetName()<<" "<<T_friend->GetName()<<hex << T_friend->GetTree() << dec<<endl;
2897     // cout<<T_friend->GetTree()->GetName()<< endl;
2898 pam-fi 1.20 pam_tree_clone[i] = T_friend->GetTree()->CloneTree(0);
2899     pam_tree_clone[i]->SetAutoSave(1000000);
2900     name = pam_tree_clone[i]->GetName();
2901 pam-fi 1.9 name.Append("_clone");
2902 pam-fi 1.20 // pam_tree_clone[i]->SetName(name.Data());
2903     cout << pam_tree_clone[i]->GetName() << endl;
2904 pam-fi 1.9 i++;
2905     }
2906 pam-fi 1.13
2907     delete li;
2908 pam-fi 1.9
2909     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2910    
2911     }
2912    
2913 pam-fi 1.11 /**
2914     * Create clone-trees
2915     */
2916     void PamLevel2::CreateCloneTrees(TFile *ofile){
2917    
2918 pam-fi 1.39
2919     // if the pointer is null, create a default file
2920    
2921     if(!ofile){
2922     cout << "void PamLevel2::CreateCloneTrees(TFile*) -- WARNING -- Creating file: clone-tree.root "<<endl;
2923     ofile = new TFile("clone-tree.root","recreate");
2924     }
2925    
2926 pam-fi 1.11 ofile->cd();
2927 pam-fi 1.9
2928 pam-fi 1.11 cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
2929     cout << "Create new PAMELA trees "<<endl;
2930 pam-fi 1.9
2931 pam-fi 1.20
2932     run_tree_clone = new TTree("Run","PAMELA Level2 data from the GL_RUN table ");
2933     run_tree_clone->Branch("RunInfo","GL_RUN",GetPointerTo("RunInfo"));
2934     cout << "Run : branch RunInfo"<<endl;
2935     run_tree_clone->Branch("SoftInfo","SoftInfo",GetPointerTo("SoftInfo"));
2936     cout << "Run : branch SoftInfo"<<endl;
2937 pam-fi 1.36 // ------------------
2938     // replicate run tree
2939     // ------------------
2940     cout << "----------------------------------------------------"<<endl;
2941     cout << "irun\t | RUN\t NEVENTS\t absolute time"<<endl;
2942     for (Int_t i=0; i<run_tree->GetEntries(); i++){
2943     run_tree->GetEntry(i);
2944     cout << i<< "\t | "<<GetRunInfo()->ID<<"\t "<<GetRunInfo()->NEVENTS<< "\t "<<GetRunInfo()->RUNHEADER_TIME<<" <---> "<<GetRunInfo()->RUNTRAILER_TIME<<endl;
2945     run_tree_clone->Fill();
2946     }
2947     cout << "----------------------------------------------------"<<endl;
2948 pam-fi 1.20
2949 pam-fi 1.45 // ------------------------------------
2950     // add branch with dead and live times
2951     // ------------------------------------
2952     run_tree_clone->Branch("DeadLiveTime",totdltime,"dltime[3]/l");
2953     cout << "Run : branch DeadLiveTime"<<endl;
2954    
2955 pam-fi 1.20
2956     sel_tree_clone = new TTree("SelectionList","List of selected events ");
2957 pam-fi 1.34 sel_tree_clone->Branch("RunEntry",&irun,"runentry/L");
2958     sel_tree_clone->Branch("EventEntry",&irunentry,"eventry/L");
2959 pam-fi 1.20
2960    
2961 pam-fi 1.11 Int_t i=0;
2962     if(TRK1||TRK2||TRKh){
2963 pam-fi 1.20 pam_tree_clone[i] = new TTree("Tracker","PAMELA tracker level2 data ");
2964 pam-fi 1.11 if(TRK1) {
2965 pam-fi 1.20 pam_tree_clone[i]->Branch("TrkLevel1","TrkLevel1", GetPointerTo("TrkLevel1"));
2966     pam_tree_clone[i]->BranchRef();
2967 pam-fi 1.11 cout << "Tracker : branch TrkLevel1"<<endl;
2968 pam-fi 1.36 // cout << "CreateCloneTrees " << GetTrkLevel1()<<endl;
2969 pam-fi 1.11 };
2970     if(TRK2) {
2971 pam-fi 1.20 pam_tree_clone[i]->Branch("TrkLevel2", "TrkLevel2",GetPointerTo("TrkLevel2"));
2972 pam-fi 1.11 cout << "Tracker : branch TrkLevel2"<<endl;
2973     };
2974     if(TRKh) {
2975 pam-fi 1.20 pam_tree_clone[i]->Branch("TrkHough","TrkHough", GetPointerTo("TrkHough"));
2976 pam-fi 1.11 cout << "Tracker : branch TrkHough"<<endl;
2977     };
2978     i++;
2979     }
2980 pam-fi 1.9
2981 pam-fi 1.11 // Calorimeter
2982     if(CAL1||CAL2){
2983 pam-fi 1.20 pam_tree_clone[i] = new TTree("Calorimeter","PAMELA calorimeter level2 data ");
2984 pam-fi 1.11 if(CAL1) {
2985 pam-fi 1.20 pam_tree_clone[i]->Branch("CaloLevel1", "CaloLevel1", GetPointerTo("CaloLevel1"));
2986 pam-fi 1.11 cout << "Calorimeter : branch CaloLevel1"<<endl;
2987     };
2988     if(CAL2) {
2989 pam-fi 1.20 pam_tree_clone[i]->Branch("CaloLevel2","CaloLevel2", GetPointerTo("CaloLevel2"));
2990 pam-fi 1.11 cout << "Calorimeter : branch CaloLevel2"<<endl;
2991     };
2992     i++;
2993     }
2994 pam-fi 1.9
2995 pam-fi 1.11 // ToF
2996     if(TOF) {
2997 pam-fi 1.20 pam_tree_clone[i] = new TTree("ToF","PAMELA ToF level2 data ");
2998     pam_tree_clone[i]->Branch("ToFLevel2","ToFLevel2", GetPointerTo("ToFLevel2"));
2999 pam-fi 1.11 cout << "ToF : branch ToFLevel2"<<endl;
3000     i++;
3001     };
3002     // Trigger
3003     if(TRG) {
3004 pam-fi 1.20 pam_tree_clone[i] = new TTree("Trigger","PAMELA trigger level2 data ");
3005     pam_tree_clone[i]->Branch("TrigLevel2","TrigLevel2", GetPointerTo("TrigLevel2"));
3006 pam-fi 1.11 cout << "Trigger : branch TrigLevel2"<<endl;
3007     i++;
3008     };
3009     // S4
3010     if(S4) {
3011 pam-fi 1.20 pam_tree_clone[i] = new TTree("S4","PAMELA S4 level2 data ");
3012     pam_tree_clone[i]->Branch("S4Level2","S4Level2", GetPointerTo("S4Level2"));
3013 pam-fi 1.11 cout << "S4 : branch S4Level2"<<endl;
3014     i++;
3015     };
3016     // Neutron Detector
3017     if(ND) {
3018 pam-fi 1.20 pam_tree_clone[i] = new TTree("NeutronD","PAMELA neutron detector level2 data ");
3019     pam_tree_clone[i]->Branch("NDLevel2","NDLevel2", GetPointerTo("NDLevel2"));
3020 pam-fi 1.11 cout << "NeutronD : branch NDLevel2"<<endl;
3021     i++;
3022     };
3023     // Anticounters
3024     if(AC) {
3025 pam-fi 1.20 pam_tree_clone[i] = new TTree("Anticounter","PAMELA anticounter detector level2 data ");
3026     pam_tree_clone[i]->Branch("AcLevel2","AcLevel2", GetPointerTo("AcLevel2"));
3027 pam-fi 1.11 cout << "Anticounter : branch AcLevel2"<<endl;
3028     i++;
3029     };
3030     // OrbitalInfo
3031     if(ORB) {
3032 pam-fi 1.20 pam_tree_clone[i] = new TTree("OrbitalInfo","PAMELA oribital info ");
3033     pam_tree_clone[i]->Branch("OrbitalInfo","OrbitalInfo", GetPointerTo("OrbitalInfo"));
3034 pam-fi 1.11 cout << "OrbitalInfo : branch OrbitalInfo"<<endl;
3035     i++;
3036     };
3037     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
3038 pam-fi 1.9
3039 pam-fi 1.11 }
3040 pam-fi 1.9
3041    
3042     /**
3043     * Fill tree (created with CreateCloneTrees)
3044     *
3045     */
3046     //void PamLevel2::FillNewPamTree(TTree *T){
3047     void PamLevel2::FillCloneTrees(){
3048 pam-fi 1.20
3049     // cout << "PamLevel2::FillCloneTrees()" << irunentry << endl;
3050 pam-fi 1.24
3051 pam-fi 1.20 for(Int_t i=0; i<NCLONES; i++){
3052     if(pam_tree_clone[i])pam_tree_clone[i]->Fill();
3053     }
3054     if(sel_tree_clone)sel_tree_clone->Fill();
3055 pam-fi 1.21
3056 pam-fi 1.9 }
3057    
3058    
3059     TTree* PamLevel2::GetCloneTree(TString name){
3060    
3061 pam-fi 1.20 for(Int_t i=0; i<NCLONES; i++){
3062     if(pam_tree_clone[i]){
3063     TString na = pam_tree_clone[i]->GetName();
3064     if(!name.CompareTo(na))return pam_tree_clone[i];
3065 pam-fi 1.9 };
3066     }
3067 pam-fi 1.20 if(run_tree_clone){
3068     TString na = run_tree_clone->GetName();
3069     if(!name.CompareTo(na))return run_tree_clone;
3070     }
3071     if(sel_tree_clone){
3072     TString na = sel_tree_clone->GetName();
3073     if(!name.CompareTo(na))return sel_tree_clone;
3074     }
3075 pam-fi 1.9 return NULL;
3076    
3077     }
3078     void PamLevel2::WriteCloneTrees(){
3079     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
3080     cout << "Write clones of PAMELA trees "<<endl;
3081 pam-fi 1.45 cout << run_tree_clone->GetName()<<endl;
3082     if(run_tree_clone->GetBranch("DeadLiveTime")->GetEntries() < run_tree->GetEntries())
3083     run_tree_clone->GetBranch("DeadLiveTime")->Fill();
3084 pam-fi 1.20 run_tree_clone->Write();
3085     cout << sel_tree_clone->GetName()<<endl;
3086     sel_tree_clone->Write();
3087     for(Int_t i=0; i<NCLONES; i++){
3088     if(pam_tree_clone[i]){
3089     cout << pam_tree_clone[i]->GetName()<<endl;
3090     pam_tree_clone[i]->Write();
3091 pam-fi 1.9 };
3092     }
3093     cout << "+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+" <<endl;
3094    
3095     }
3096    
3097     /**
3098 pam-fi 1.24 * Method to get level2-trees entry, the corresponding run entry and (if required) the level0 entry.
3099 pam-fi 1.9 */
3100 pam-fi 1.33 //Int_t PamLevel2::GetEntry(Int_t iee){
3101     Int_t PamLevel2::GetEntry(Long64_t iee){
3102 pam-fi 1.9
3103 pam-fi 1.39 // cout << "-------------------------------------"<<endl;
3104     // cout << "Int_t PamLevel2::GetEntry("<<iee<<")"<<endl;
3105    
3106 pam-fi 1.20 if(!pam_tree){
3107 mocchiut 1.28 cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- level2 trees not loaded"<<endl;
3108 pam-fi 1.20 return 0;
3109     }
3110 mocchiut 1.28
3111    
3112     //
3113     // 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...
3114     //
3115     // if(!run_tree ){
3116     // cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loeaded"<<endl;
3117     // return 0;
3118     // }
3119 pam-fi 1.20
3120 pam-fi 1.33 Long64_t ii=0;
3121 pam-fi 1.20 //-------------------------------
3122     ii = iee;
3123 pam-fi 1.33 if( !pam_tree->GetEntry(ii) ){
3124     cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading pam tree"<<endl;
3125     return 0;
3126     }
3127 mocchiut 1.28 //
3128     // ... 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.
3129     // 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
3130     // a problem if you don't check the return code of getentry.
3131     //
3132     if(!run_tree ){
3133 mocchiut 1.29 if ( TRK0 || CAL0 || TOF0 || RUN ) { //forse cosi` va bene per tornare 1?
3134 pam-fi 1.36 cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- run tree not loaded"<<endl;
3135     return 0;
3136 mocchiut 1.29 } else {
3137 pam-fi 1.36 return 1; //cosi` se non c'e` run esce qua...
3138 mocchiut 1.29 }
3139 mocchiut 1.28 }
3140    
3141 pam-fi 1.20 //-------------------------------
3142     ii = iee;
3143 pam-fi 1.33 // Bool_t UPDATED = UpdateRunInfo(run_tree,ii);
3144 pam-fi 1.36 // Bool_t UPDATED = UpdateRunInfo(ii);
3145     UpdateRunInfo(ii);
3146 pam-fi 1.20 if(SELLI==0)irunentry = iee-runfirstentry;
3147 pam-fi 1.36 // if(UPDATED && run_tree_clone)run_tree_clone->Fill();
3148    
3149 pam-fi 1.20 // cout << "PamLevel2::GetEntry("<<iee<<") "<<irun<<" "<<runfirstentry<<" "<<irunentry<<endl;
3150    
3151 pam-fi 1.39 // cout << "irunentry "<<irunentry << endl;
3152     // cout << "runfirstentry "<<runfirstentry << endl;
3153     // cout << "nevents "<<GetRunInfo()->NEVENTS<< endl;
3154 pam-fi 1.40
3155     // if( TRK0 || CAL0 || TOF0 ){
3156     // if( !GetYodaEntry( ) ){
3157     // cout << " Int_t PamLevel2::GetEntry(Int_t) -- ERROR -- error reading level0 tree"<<endl;
3158     // return 0;
3159     // }
3160     // }
3161    
3162    
3163 pam-fi 1.20 return 1;
3164    
3165     }
3166    
3167 pam-fi 1.40 TrkLevel0 *PamLevel2::GetTrkLevel0(){
3168     if( !TRK0 )return NULL;
3169     if( !GetYodaEntry( ) ){
3170     cout << " Int_t PamLevel2::GetTrkLevel0() -- ERROR -- error reading level0 tree"<<endl;
3171     return 0;
3172     }
3173     return trk0_obj;
3174     };
3175     CaloLevel0 *PamLevel2::GetCaloLevel0(){
3176     if( !CAL0 )return NULL;
3177     if( !GetYodaEntry( ) ){
3178     cout << " Int_t PamLevel2::GetCaloLevel0() -- ERROR -- error reading level0 tree"<<endl;
3179     return 0;
3180     }
3181     return calo0_obj;
3182     };
3183    
3184    
3185 pam-fi 1.20 /**
3186     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
3187     * Given the run ID (...), if needed it query the DB and load the proper file.
3188     * @return Pointer to the tree
3189     */
3190    
3191    
3192     TTree* PamLevel2::GetYodaTree( ){
3193    
3194     // cout << "TTree* PamLevel2::GetYodaTree( )"<<endl;
3195     //===================================
3196     // check if iroot has changed
3197     //===================================
3198     if( irun<0 ){
3199 pam-fi 1.34 cout << "PamLevel2::GetYodaTree() -- ERROR -- irun = "<<irun<<endl;
3200     // cout << "In order to use this method you have to first load the RunInfo tree "<<endl;
3201 pam-fi 1.20 return NULL;
3202     }
3203     Int_t irootnew = run_obj->ID_ROOT_L0;
3204 pam-fi 1.39 // cout << "iroot "<<iroot<<endl;
3205     // cout << "irootnew "<<irootnew<<endl;
3206 pam-fi 1.20
3207     //===================================
3208     // load the level0 file
3209     // (if not already loaded)
3210     //===================================
3211     if( iroot != irootnew || !l0_tree){
3212     iroot = irootnew;
3213     //===================================
3214     // open the DB connection
3215     // (if not already opened)
3216     //===================================
3217     if(!dbc || (dbc && !dbc->IsConnected())){
3218     cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3219     cout<<"Connecting to DB"<<endl;
3220     cout<<"HOST "<<host<<endl;
3221     cout<<"USER "<<user<<endl;
3222     cout<<"PSW "<<psw<<endl;
3223     dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
3224     if( !dbc )return NULL;
3225     if( !dbc->IsConnected() )return NULL;
3226     // cout<<"...done"<<endl;
3227     cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3228     }else{
3229     // cout<<"DB already connected"<<endl;
3230     }
3231     GL_ROOT glroot = GL_ROOT();
3232 pam-fi 1.24 if( glroot.Query_GL_ROOT(iroot,dbc) ){
3233     cout << "TTree* PamLevel2::GetYodaTree( ) -- ERROR -- level0 file iroot = "<<iroot<< " does not exists"<<endl;
3234     return NULL;
3235     };
3236 pam-fi 1.20 TString filename = glroot.PATH + glroot.NAME;
3237     if(l0_file){
3238     l0_file->Close();
3239     l0_file->Delete();
3240     }
3241     cout << "Opening LEVEL0 file: "<< filename << endl;
3242     FileStat_t t;
3243     if( gSystem->GetPathInfo(filename.Data(),t) ){
3244     cout << " PamLevel2::GetYodaTree() -- ERROR opening file "<<endl;
3245     return NULL;
3246     }
3247     l0_file = new TFile(filename);
3248     if( !l0_file )return NULL;
3249     l0_tree = (TTree*)l0_file->Get("Physics");
3250     if(!h0_obj)h0_obj = new EventHeader();
3251     l0_tree->SetBranchAddress("Header" ,&h0_obj);
3252 mocchiut 1.30 prevshift = 0;
3253 pam-fi 1.20 //---------------------------------------------------
3254     // TRACKER:
3255     if(TRK0){
3256     if(!trk0_obj){
3257     trk0_obj = new TrkLevel0();
3258     trk0_obj->Set();
3259     };
3260     l0_tree->SetBranchAddress("Tracker" ,trk0_obj->GetPointerToTrackerEvent());
3261     }
3262 pam-fi 1.40 //--------------------------------------------------
3263 pam-fi 1.20 // CALORIMETER:
3264     if(CAL0){
3265 mocchiut 1.30 if(!calo0_obj){
3266     calo0_obj = new CaloLevel0();
3267     calo0_obj->Set();
3268     };
3269     l0_tree->SetBranchAddress("Calorimeter" ,calo0_obj->GetPointerToCalorimeterEvent());
3270     // cout << "PamLevel2::GetYodaTree() --- level0 calorimeter not implemented "<<endl;
3271 pam-fi 1.20 }
3272     //---------------------------------------------------
3273     // TOF:
3274     if(TOF0){
3275     cout << "PamLevel2::GetYodaTree() --- level0 TOF not implemented "<<endl;
3276     }
3277    
3278     };
3279    
3280 pam-fi 1.26 if(!dbc || (dbc && !dbc->IsConnected())){
3281     cout << " TTree* PamLevel2::GetYodaTree( ) -- no DB connected... hai fatto qualche cazzata "<<endl;
3282     }
3283    
3284 pam-fi 1.33 if ( TRK0 ){
3285     TrkParams::Load(6);
3286     if( !TrkParams::IsLoaded(6) ){
3287     cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- VK-mask not loaded"<<endl;
3288     };
3289     TrkParams::SetCalib(run_obj,dbc);
3290     TrkParams::LoadCalib( );
3291     if( !TrkParams::CalibIsLoaded() ){
3292     cout << " TTree* PamLevel2::GetYodaTree( ) -- WARNING -- Calibration not loaded"<<endl;
3293     };
3294     }
3295 pam-fi 1.26
3296 pam-fi 1.24 // cout << l0_tree << endl;
3297    
3298 pam-fi 1.20 return l0_tree;
3299 pam-fi 1.9
3300 pam-fi 1.20 }
3301 pam-fi 1.9
3302 pam-fi 1.20 /**
3303     * Method to retrieve the level0 tree (YODA tree) that contains the current event.
3304     */
3305     Int_t PamLevel2::GetYodaEntry(){
3306 pam-fi 1.9
3307 pam-fi 1.20 // cout << "Int_t PamLevel2::GetYodaEntry()"<<endl;
3308     if(!GetYodaTree())return 0;
3309    
3310 pam-fi 1.27 // patch
3311 pam-fi 1.33 if( irunentry < 0){
3312 pam-fi 1.36 // cout << "Int_t PamLevel2::GetYodaEntry() -- ATTENZIONE -- irunentry negativo?!?! "<<(Int_t)irunentry<<endl;
3313 pam-fi 1.33 irunentry=0LL;
3314 pam-fi 1.27 }
3315 pam-fi 1.20 // ---------------------------------
3316     // if file is NOT a preselected file
3317     // ---------------------------------
3318 pam-fi 1.33 Long64_t quellagiusta = irunentry + (Long64_t)(run_obj->EV_FROM);
3319 pam-fi 1.36 // cout << " irunentry "<<irunentry<<endl;
3320     // cout << " EV_FROM "<<run_obj->EV_FROM<<endl;
3321     // cout << " quellagiusta = irunentry + EV_FROM "<< quellagiusta << endl;
3322 pam-fi 1.24
3323 pam-fi 1.26 // cout << " irun "<< irun << " irunentry "<< irunentry<<" run_obj->EV_FROM "<<run_obj->EV_FROM <<" quella giusta "<<quellagiusta << endl;
3324     // cout << " iroot "<<iroot<<" run_obj->ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<endl;
3325 pam-fi 1.43 // cout << " time "<< abstime << endl;
3326 pam-fi 1.26 // cout << " trk_calib_used "<<run_obj->TRK_CALIB_USED<< endl;
3327    
3328 pam-fi 1.43 ULong64_t obt = 0;
3329     ULong64_t pktn = 0;
3330     if( GetOrbitalInfo() ){
3331     obt = GetOrbitalInfo()->OBT;
3332     pktn = GetOrbitalInfo()->pkt_num;
3333     }
3334    
3335     if( !GetOrbitalInfo() && !ISGP){
3336 pam-fi 1.24 cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing OrbitalInfo "<<endl;
3337     return 0;
3338     }
3339 pam-fi 1.43 if( obt==0 && pktn==0 && !ISGP ){
3340 pam-fi 1.34 cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- level2 event corrupted ?? "<<endl;
3341     return 0;
3342     }
3343 pam-fi 1.24
3344     // ---------------------------------------------------------------------
3345     // ATTENTION!!!
3346     // If data are not pre-processed with cleaner, the level0 tree may contain
3347     // spurious nested physics packets.
3348     // The GL_RUN variables EV_FROM, EV_TO and NEVENTS counts also these entries
3349     // while level2 tree DOES NOT!!
3350     // This means that "quellagiusta" in these cases is not correct.
3351     // In order to retrieve the correct level0 event, I implemented a check
3352     // of the OBT and pkt-number. In case of mismatch, the level0 entry number
3353     // is shift forward until when the packets match.
3354     // ---------------------------------------------------------------------
3355     Int_t answer = 0;
3356     Int_t shift =0;
3357 mocchiut 1.31 // printf(" siamo qui %i %i \n",shift,prevshift);
3358 pam-fi 1.39 Int_t maxshift = 10;
3359 pam-fi 1.24 do{
3360 mocchiut 1.30 if(shift>0){
3361 pam-fi 1.43 cout << " PKTNUM L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
3362 pam-fi 1.36 cout << " RUN: ID "<< GetRunInfo()->ID << " ID_ROOT_L0 "<<run_obj->ID_ROOT_L0<<" ID_RUN_FRAG "<<GetRunInfo()->ID_RUN_FRAG << " EV_FROM "<<GetRunInfo()->EV_FROM <<endl;
3363     cout << " L2 <--> L0 mismatch ( irun "<<irun<<" irunentry "<<irunentry<<" shift "<<shift<<" prevshift "<<prevshift<<" )"<<endl;
3364 pam-fi 1.26 }
3365 pam-fi 1.33 answer = l0_tree->GetEntry(quellagiusta+(Long64_t)shift+(Long64_t)prevshift);
3366 pam-fi 1.24 shift++;
3367     if( !GetEventHeader() ){
3368     cout << "Int_t PamLevel2::GetYodaEntry() -- ERROR -- missing EventHeader "<<endl;
3369     return 0;
3370     }
3371 pam-fi 1.43
3372     if(ISGP){
3373     obt = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()); //BARBATRUCCO
3374     pktn = (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()); //BARBATRUCCO
3375     }
3376    
3377     // cout << "PKTNUM "<<shift<<" == L2 --- "<< pktn << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetCounter()<<endl;
3378     // cout << " L2 --- "<< obt << " --- L0 --- "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime()<<endl;
3379 pam-fi 1.26 // if( (quellagiusta+shift) == l0_tree->GetEntries()+1 )cout << ">>> end of level0 tree <<<"<<endl;
3380 pam-fi 1.43 // cout << " obt "<< obt << endl;
3381 pam-fi 1.26 // cout << " GetEventHeader()->GetPscuHeader()->GetOrbitalTime() "<< GetEventHeader()->GetPscuHeader()->GetOrbitalTime() << endl;
3382 pam-fi 1.43 // cout << " pktn "<< pktn << endl;
3383 pam-fi 1.26 // cout << " GetEventHeader()->GetPscuHeader()->GetCounter() "<< GetEventHeader()->GetPscuHeader()->GetCounter() << endl;
3384 mocchiut 1.31 // printf(" IDRUN %u \n",GetRunInfo()->ID);
3385     //
3386 pam-fi 1.33 if ( prevshift != 0 && (quellagiusta+(Long64_t)shift) == GetYodaTree()->GetEntries() ){
3387 mocchiut 1.30 prevshift = 0;
3388     shift = -1;
3389     };
3390 pam-fi 1.26
3391 pam-fi 1.43 }while( ( obt != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetOrbitalTime()) ||
3392     pktn != (UInt_t)(GetEventHeader()->GetPscuHeader()->GetCounter()) )
3393     && (quellagiusta+(Long64_t)shift) < GetYodaTree()->GetEntries() && shift < maxshift);
3394 mocchiut 1.30
3395 pam-fi 1.39 if ( (quellagiusta+(Long64_t)shift+(Long64_t)prevshift) > GetYodaTree()->GetEntries() || shift == maxshift ) {
3396 pam-fi 1.36 cout << " Big trouble here, no such event in Level0 data! " <<endl;
3397     return 0;
3398     }
3399 pam-fi 1.20 // cout << "LA ENTRY GIUSTA E`: "<<quellagiusta<<" (spero...)"<<endl;
3400 pam-fi 1.24 // return GetYodaTree()->GetEntry(quellagiusta);
3401 mocchiut 1.30 if ( shift > 1 ) prevshift += (shift-1);
3402 pam-fi 1.20
3403 pam-fi 1.24 return answer;
3404 pam-fi 1.20
3405     }
3406 pam-fi 1.40 /**
3407     * \Brief Set DB connection
3408     */
3409     Bool_t PamLevel2::SetDBConnection(){
3410 mocchiut 1.30
3411 pam-fi 1.40 cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3412     cout<<"Connecting to DB"<<endl;
3413     cout<<"HOST "<<host<<endl;
3414     cout<<"USER "<<user<<endl;
3415     cout<<"PSW "<<psw<<endl;
3416     dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
3417     if( !dbc )return false;
3418     if( !dbc->IsConnected() )return false;
3419     cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
3420     return true;
3421 mocchiut 1.30
3422 pam-fi 1.40 }
3423 pam-fi 1.45
3424     /**
3425     * \Brief Add a friend to the pamela chain.
3426     * @param cname name of the chain to be added
3427     */
3428    
3429     TChain* PamLevel2::AddFriend(TString cname){
3430    
3431     if(!GetPamTree()){
3432     cout << " TChain* PamLevel2::AddFriend(TString cname) --- a pamela tree must be created first"<<endl;
3433     return NULL;
3434     }
3435    
3436    
3437     TChain *c = new TChain( cname.Data() );
3438    
3439     TIter next( GetPamTree()->GetListOfFiles() );
3440     Int_t nf = 0;
3441     TChainElement* element = 0;
3442     while ((element = (TChainElement*) next())) {
3443     c->Add(element->GetTitle());
3444     nf++;
3445     }
3446    
3447     GetPamTree()->AddFriend(cname.Data());
3448    
3449     cout << "external chain created and added to pamela friends :"<<cname<<endl;
3450     cout << "n.files "<<nf<<endl;
3451    
3452    
3453     return c;
3454    
3455     }

  ViewVC Help
Powered by ViewVC 1.1.23