]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSGetter.cxx
cosmetics
[u/mrichter/AliRoot.git] / PHOS / AliPHOSGetter.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 //_________________________________________________________________________
19 //  A singleton. This class should be used in the analysis stage to get 
20 //  reconstructed objects: Digits, RecPoints, TrackSegments and RecParticles,
21 //  instead of directly reading them from galice.root file. This container 
22 //  ensures, that one reads Digits, made of these particular digits, RecPoints, 
23 //  made of these particular RecPoints, TrackSegments and RecParticles. 
24 //  This becomes non trivial if there are several identical branches, produced with
25 //  different set of parameters. 
26 //
27 //  An example of how to use (see also class AliPHOSAnalyser):
28 //  AliPHOSGetter * gime = AliPHOSGetter::GetInstance("galice.root","test") ;
29 //  for(Int_t irecp = 0; irecp < gime->NRecParticles() ; irecp++)
30 //     AliPHOSRecParticle * part = gime->RecParticle(1) ;
31 //     ................
32 //  gime->Event(event) ;    // reads new event from galice.root
33 //                  
34 //*-- Author: Yves Schutz (SUBATECH) & Dmitri Peressounko (RRC KI & SUBATECH)
35 //*--         Completely redesigned by Dmitri Peressounko March 2001  
36 //
37 //*-- YS June 2001 : renamed the original AliPHOSIndexToObject and make
38 //*--         systematic usage of TFolders without changing the interface        
39 //////////////////////////////////////////////////////////////////////////////
40
41 // --- ROOT system ---
42
43 #include <TFile.h>
44 #include <TROOT.h>
45 #include <TSystem.h>
46 #include <TParticle.h>
47 #include <TH1D.h>
48 #include <TF1.h>
49
50 // --- Standard library ---
51
52 // --- AliRoot header files ---
53 #include "AliESD.h"
54 #include "AliHeader.h"  
55 #include "AliMC.h"
56 #include "AliPHOS.h"
57 #include "AliPHOSBeamTestEvent.h"
58 #include "AliPHOSGetter.h"
59 #include "AliPHOSLoader.h"
60 #include "AliRunLoader.h"
61 #include "AliStack.h"  
62 #include "AliPHOSRawStream.h"
63 #include "AliRawReaderFile.h"
64
65 ClassImp(AliPHOSGetter)
66   
67 AliPHOSGetter * AliPHOSGetter::fgObjGetter = 0 ; 
68 AliPHOSLoader * AliPHOSGetter::fgPhosLoader = 0;
69 Int_t AliPHOSGetter::fgDebug = 0;
70
71 //  TFile * AliPHOSGetter::fgFile = 0 ; 
72
73 //____________________________________________________________________________ 
74 AliPHOSGetter::AliPHOSGetter(const char* headerFile, const char* version, Option_t * openingOption)
75 {
76   // ctor only called by Instance()
77
78   AliRunLoader* rl = AliRunLoader::GetRunLoader(version) ; 
79   if (!rl) {
80     rl = AliRunLoader::Open(headerFile, version, openingOption);
81     if (!rl) {
82       Fatal("AliPHOSGetter", "Could not find the Run Loader for %s - %s",headerFile, version) ; 
83       return ;
84     } 
85     if (rl->GetAliRun() == 0x0) {
86       rl->LoadgAlice();
87       gAlice = rl->GetAliRun(); // should be removed
88     }
89   }
90   fgPhosLoader = dynamic_cast<AliPHOSLoader*>(rl->GetLoader("PHOSLoader"));
91   if ( !fgPhosLoader ) 
92     Error("AliPHOSGetter", "Could not find PHOSLoader") ; 
93   else 
94     fgPhosLoader->SetTitle(version);
95   
96   
97   // initialize data members
98   SetDebug(0) ; 
99   fBTE = 0 ; 
100   fPrimaries = 0 ; 
101   fLoadingStatus = "" ; 
102  
103   fESDFileName = rl->GetFileName()  ; // this should be the galice.root file
104   fESDFileName.ReplaceAll("galice.root", "AliESDs.root") ;  
105   fESDFile = 0 ; 
106   fESD = 0 ; 
107   fESDTree = 0 ; 
108 }
109
110 //____________________________________________________________________________ 
111 AliPHOSGetter::~AliPHOSGetter()
112 {
113   // dtor
114   delete fgPhosLoader ;
115   fgPhosLoader = 0 ;
116   delete fBTE ; 
117   fBTE = 0 ; 
118   fPrimaries->Delete() ; 
119   delete fPrimaries ; 
120   fgObjGetter = 0;
121   if (fESD) 
122     delete fESD ; 
123   if (fESDTree) 
124     delete fESDTree ; 
125 }
126
127 //____________________________________________________________________________ 
128 void AliPHOSGetter::Reset()
129 {
130   // resets things in case the getter is called consecutively with different files
131   // the PHOS Loader is already deleted by the Run Loader
132
133   if (fPrimaries) { 
134     fPrimaries->Delete() ; 
135     delete fPrimaries ;
136   } 
137   fgPhosLoader = 0; 
138   fgObjGetter = 0; 
139 }
140
141 //____________________________________________________________________________ 
142 AliPHOSClusterizer * AliPHOSGetter::Clusterizer()
143
144   // Returns pointer to the Clusterizer task 
145   AliPHOSClusterizer * rv ; 
146   rv =  dynamic_cast<AliPHOSClusterizer *>(PhosLoader()->Reconstructioner()) ;
147   if (!rv) {
148     Event(0, "R") ; 
149     rv =  dynamic_cast<AliPHOSClusterizer*>(PhosLoader()->Reconstructioner()) ;
150   }
151   return rv ; 
152 }
153
154 //____________________________________________________________________________ 
155 TObjArray * AliPHOSGetter::CpvRecPoints() 
156 {
157   // asks the Loader to return the CPV RecPoints container 
158
159   TObjArray * rv = 0 ; 
160   
161   rv = PhosLoader()->CpvRecPoints() ; 
162   if (!rv) {
163     PhosLoader()->MakeRecPointsArray() ;
164     rv = PhosLoader()->CpvRecPoints() ; 
165   }
166   return rv ; 
167 }
168
169 //____________________________________________________________________________ 
170 TClonesArray * AliPHOSGetter::Digits() 
171 {
172   // asks the Loader to return the Digits container 
173
174   TClonesArray * rv = 0 ; 
175   rv = PhosLoader()->Digits() ; 
176
177   if( !rv ) {
178     PhosLoader()->MakeDigitsArray() ; 
179     rv = PhosLoader()->Digits() ;
180   }
181   return rv ; 
182 }
183
184 //____________________________________________________________________________ 
185 AliPHOSDigitizer * AliPHOSGetter::Digitizer() 
186
187   // Returns pointer to the Digitizer task 
188   AliPHOSDigitizer * rv ; 
189   rv =  dynamic_cast<AliPHOSDigitizer *>(PhosLoader()->Digitizer()) ;
190   if (!rv) {
191     Event(0, "D") ; 
192     rv =  dynamic_cast<AliPHOSDigitizer *>(PhosLoader()->Digitizer()) ;
193   }
194   return rv ; 
195 }
196
197
198 //____________________________________________________________________________ 
199 TObjArray * AliPHOSGetter::EmcRecPoints() 
200 {
201   // asks the Loader to return the EMC RecPoints container 
202
203   TObjArray * rv = 0 ; 
204   
205   rv = PhosLoader()->EmcRecPoints() ; 
206   if (!rv) {
207     PhosLoader()->MakeRecPointsArray() ;
208     rv = PhosLoader()->EmcRecPoints() ; 
209   }
210   return rv ; 
211 }
212
213 //____________________________________________________________________________ 
214 TClonesArray * AliPHOSGetter::TrackSegments() 
215 {
216   // asks the Loader to return the TrackSegments container 
217
218   TClonesArray * rv = 0 ; 
219   
220   rv = PhosLoader()->TrackSegments() ; 
221   if (!rv) {
222     PhosLoader()->MakeTrackSegmentsArray() ;
223     rv = PhosLoader()->TrackSegments() ; 
224   }
225   return rv ; 
226 }
227
228 //____________________________________________________________________________ 
229 AliPHOSTrackSegmentMaker * AliPHOSGetter::TrackSegmentMaker()
230
231   // Returns pointer to the TrackSegmentMaker task 
232   AliPHOSTrackSegmentMaker * rv ; 
233   rv =  dynamic_cast<AliPHOSTrackSegmentMaker *>(PhosLoader()->TrackSegmentMaker()) ;
234   if (!rv) {
235     Event(0, "T") ; 
236     rv =  dynamic_cast<AliPHOSTrackSegmentMaker *>(PhosLoader()->TrackSegmentMaker()) ;
237   }
238   return rv ; 
239 }
240
241 //____________________________________________________________________________ 
242 TClonesArray * AliPHOSGetter::RecParticles() 
243 {
244   // asks the Loader to return the TrackSegments container 
245
246   TClonesArray * rv = 0 ; 
247   
248   rv = PhosLoader()->RecParticles() ; 
249   if (!rv) {
250     PhosLoader()->MakeRecParticlesArray() ;
251     rv = PhosLoader()->RecParticles() ; 
252   }
253   return rv ; 
254 }
255 //____________________________________________________________________________ 
256 void AliPHOSGetter::Event(Int_t event, const char* opt) 
257 {
258   // Reads the content of all Tree's S, D and R
259
260   if ( event >= MaxEvent() ) {
261     Error("Event", "%d not found in TreeE !", event) ; 
262     return ; 
263   }
264
265   AliRunLoader * rl = AliRunLoader::GetRunLoader(PhosLoader()->GetTitle());
266
267   // checks if we are dealing with test-beam data
268   TBranch * btb = rl->TreeE()->GetBranch("AliPHOSBeamTestEvent") ;
269   if(btb){
270     if(!fBTE)
271       fBTE = new AliPHOSBeamTestEvent() ;
272     btb->SetAddress(&fBTE) ;
273     btb->GetEntry(event) ;
274   }
275   else{
276     if(fBTE){
277       delete fBTE ;
278       fBTE = 0 ;
279     }
280   }
281
282   // Loads the type of object(s) requested
283   
284   rl->GetEvent(event) ;
285
286   if( strstr(opt,"X") || (strcmp(opt,"")==0) )
287     ReadPrimaries() ;
288
289   if(strstr(opt,"H") )
290     ReadTreeH();
291
292   if(strstr(opt,"S") )
293     ReadTreeS() ;
294
295   if( strstr(opt,"D") )
296     ReadTreeD() ;
297
298   if( strstr(opt,"R") )
299     ReadTreeR() ;
300
301   if( strstr(opt,"T") )
302     ReadTreeT() ;
303
304   if( strstr(opt,"P") )
305     ReadTreeP() ;
306
307   if( strstr(opt,"E") )
308     ReadTreeE(event) ;
309
310   if( strstr(opt,"W") )
311     ReadRaw(event) ;
312  
313
314 //   if( strstr(opt,"Q") )
315 //     ReadTreeQA() ;
316  
317 }
318
319
320 //____________________________________________________________________________ 
321 Int_t AliPHOSGetter::EventNumber() const
322   {
323   // return the current event number
324   AliRunLoader * rl = AliRunLoader::GetRunLoader(PhosLoader()->GetTitle());
325   return static_cast<Int_t>(rl->GetEventNumber()) ;   
326 }
327
328 //____________________________________________________________________________ 
329   TClonesArray * AliPHOSGetter::Hits()  
330 {
331   // asks the loader to return  the Hits container 
332   
333   TClonesArray * rv = 0 ; 
334   
335   rv = PhosLoader()->Hits() ; 
336   if ( !rv ) {
337     PhosLoader()->LoadHits("read"); 
338     rv = PhosLoader()->Hits() ; 
339   }
340   return rv ; 
341 }
342
343 //____________________________________________________________________________ 
344 AliPHOSGetter * AliPHOSGetter::Instance(const char* alirunFileName, const char* version, Option_t * openingOption) 
345 {
346   // Creates and returns the pointer of the unique instance
347   // Must be called only when the environment has changed
348   
349   //::Info("Instance","alirunFileName=%s version=%s openingOption=%s",alirunFileName,version,openingOption);
350   
351   if(!fgObjGetter){ // first time the getter is called 
352     fgObjGetter = new AliPHOSGetter(alirunFileName, version, openingOption) ;
353   }
354   else { // the getter has been called previously
355     AliRunLoader * rl = AliRunLoader::GetRunLoader(fgPhosLoader->GetTitle());
356     if ( rl->GetFileName() == alirunFileName ) {// the alirunFile has the same name
357       // check if the file is already open
358       TFile * galiceFile = dynamic_cast<TFile *>(gROOT->FindObject(rl->GetFileName()) ) ; 
359       
360       if ( !galiceFile ) 
361         fgObjGetter = new AliPHOSGetter(alirunFileName, version, openingOption) ;
362       
363       else {  // the file is already open check the version name
364         TString currentVersionName = rl->GetEventFolder()->GetName() ; 
365         TString newVersionName(version) ; 
366         if (currentVersionName == newVersionName) 
367           if(fgDebug)
368             ::Warning( "Instance", "Files with version %s already open", currentVersionName.Data() ) ;  
369         else {
370           fgObjGetter = new AliPHOSGetter(alirunFileName, version, openingOption) ;      
371         }
372       }
373     }
374     else {
375       AliRunLoader * rl = AliRunLoader::GetRunLoader(fgPhosLoader->GetTitle()) ; 
376       if ( strstr(version, AliConfig::GetDefaultEventFolderName()) ) // false in case of merging
377         delete rl ; 
378       fgObjGetter = new AliPHOSGetter(alirunFileName, version, openingOption) ;      
379     }
380   }
381   if (!fgObjGetter) 
382     ::Error("AliPHOSGetter::Instance", "Failed to create the PHOS Getter object") ;
383   else 
384     if (fgDebug)
385       Print() ;
386   
387   return fgObjGetter ;
388 }
389
390 //____________________________________________________________________________ 
391 AliPHOSGetter *  AliPHOSGetter::Instance()
392 {
393   // Returns the pointer of the unique instance already defined
394   
395   if(!fgObjGetter && fgDebug)
396      ::Warning("AliPHOSGetter::Instance", "Getter not initialized") ;
397
398    return fgObjGetter ;
399            
400 }
401
402 //____________________________________________________________________________ 
403 Int_t AliPHOSGetter::MaxEvent() const 
404 {
405   // returns the number of events in the run (from TE)
406
407   AliRunLoader * rl = AliRunLoader::GetRunLoader(PhosLoader()->GetTitle());
408   return static_cast<Int_t>(rl->GetNumberOfEvents()) ; 
409 }
410
411 //____________________________________________________________________________ 
412 TParticle * AliPHOSGetter::Primary(Int_t index) const
413 {
414   AliRunLoader * rl = AliRunLoader::GetRunLoader(PhosLoader()->GetTitle());
415   return rl->Stack()->Particle(index) ; 
416
417
418 //____________________________________________________________________________ 
419 AliPHOS * AliPHOSGetter:: PHOS() const  
420 {
421   // returns the PHOS object 
422   AliPHOS * phos = dynamic_cast<AliPHOS*>(PhosLoader()->GetModulesFolder()->FindObject("PHOS")) ;  
423   if (!phos) 
424     if (fgDebug)
425       Warning("PHOS", "PHOS module not found in module folders: %s", PhosLoader()->GetModulesFolder()->GetName() ) ; 
426   return phos ; 
427 }  
428
429
430
431 //____________________________________________________________________________ 
432 AliPHOSPID * AliPHOSGetter::PID()
433
434   // Returns pointer to the PID task 
435   AliPHOSPID * rv ; 
436   rv =  dynamic_cast<AliPHOSPID *>(PhosLoader()->PIDTask()) ;
437   if (!rv) {
438     Event(0, "P") ; 
439     rv =  dynamic_cast<AliPHOSPID *>(PhosLoader()->PIDTask()) ;
440   }
441   return rv ; 
442 }
443
444 //____________________________________________________________________________ 
445 AliPHOSGeometry * AliPHOSGetter::PHOSGeometry() const 
446 {
447   // Returns PHOS geometry
448
449   AliPHOSGeometry * rv = 0 ; 
450   if (PHOS() )
451     rv =  PHOS()->GetGeometry() ;
452   return rv ; 
453
454
455 //____________________________________________________________________________ 
456 TClonesArray * AliPHOSGetter::Primaries()  
457 {
458   // creates the Primaries container if needed
459   if ( !fPrimaries ) {
460     if (fgDebug) 
461       Info("Primaries", "Creating a new TClonesArray for primaries") ; 
462     fPrimaries = new TClonesArray("TParticle", 1000) ;
463   } 
464   return fPrimaries ; 
465 }
466
467 //____________________________________________________________________________ 
468 void  AliPHOSGetter::Print() 
469 {
470   // Print usefull information about the getter
471     
472   AliRunLoader * rl = AliRunLoader::GetRunLoader(fgPhosLoader->GetTitle());
473   ::Info( "Print", "gAlice file is %s -- version name is %s", (rl->GetFileName()).Data(), rl->GetEventFolder()->GetName() ) ; 
474 }
475
476 //____________________________________________________________________________ 
477 void AliPHOSGetter::ReadPrimaries()  
478 {
479   // Read Primaries from Kinematics.root
480   
481   AliRunLoader * rl = AliRunLoader::GetRunLoader(PhosLoader()->GetTitle());
482   
483   // gets kine tree from the root file (Kinematics.root)
484   if ( ! rl->TreeK() ) { // load treeK the first time
485     rl->LoadKinematics() ;
486   }
487   
488   fNPrimaries = (rl->GetHeader())->GetNtrack(); 
489   if (fgDebug) 
490     Info( "ReadTreeK", "Found %d particles in event # %d", fNPrimaries, EventNumber() ) ; 
491
492
493   // first time creates the container
494   if ( Primaries() ) 
495     fPrimaries->Clear() ; 
496   
497   Int_t index = 0 ; 
498   for (index = 0 ; index < fNPrimaries; index++) { 
499     new ((*fPrimaries)[index]) TParticle(*(Primary(index)));
500   }
501 }
502
503 //____________________________________________________________________________ 
504 Bool_t AliPHOSGetter::OpenESDFile() 
505 {
506   //Open the ESD file    
507   Bool_t rv = kTRUE ; 
508   if (!fESDFile) {
509     fESDFile = TFile::Open(fESDFileName) ;
510     if (!fESDFile ) 
511       return kFALSE ; 
512   }
513   else if (fESDFile->IsOpen()) {
514     fESDFile->Close() ; 
515     fESDFile = TFile::Open(fESDFileName) ;
516   }
517   if (!fESDFile->IsOpen())
518     rv = kFALSE ; 
519   return rv ; 
520 }
521
522 //____________________________________________________________________________ 
523 Int_t AliPHOSGetter::ReadRaw(Int_t event)
524 {
525   // reads the raw format data, converts it into digits format and store digits in Digits()
526   // container.
527
528   AliRawReaderFile rawReader(event) ; 
529   AliPHOSRawStream in(&rawReader);
530   
531   const Int_t kHighGainIdOffset = PHOSGeometry()->GetNModules()
532     * PHOSGeometry()->GetNPhi()
533     * PHOSGeometry()->GetNZ() 
534     * 2 ;
535
536   Bool_t first = kTRUE ;
537   
538   TH1D hLowGain("hLowGain", "Low Gain", 1000, 0., PHOS()->GetRawFormatTimeMax()) ; 
539   TH1D hHighGain("hHighGain", "High Gain", 1000, 0., PHOS()->GetRawFormatTimeMax()) ; 
540   
541   // fit half the gaussian decay rather than AliPHOS::RawResponseFunction because thiswould give a floating point
542   // exception during error calculation ... To solve... 
543   TF1 * gauss = new TF1("gauss", "gaus", 
544                          PHOS()->GetRawFormatTimePeak(), 
545                          PHOS()->GetRawFormatTimeMax() ) ;   
546
547   Int_t relId[4], id ;
548   Bool_t hgflag = kFALSE ; 
549  
550   TClonesArray * digits = Digits() ;
551   digits->Clear() ; 
552   Int_t idigit = 0 ; 
553   
554   while ( in.Next() ) { // PHOS entries loop 
555         
556     if ( (in.IsNewRow() || in.IsNewColumn() || in.IsNewModule()) ) {
557       if (!first) {
558         hLowGain.Fit(gauss, "QRON") ; 
559         Int_t ampL =  static_cast<Int_t>(gauss->Eval(gauss->GetParameter(2)) + 0.5) ; 
560         Double_t timeL  = PHOS()->GetRawFormatTimePeak() - gauss->GetParameter(2) ;
561         if (timeL < 0 ) // happens with noise 
562           timeL =  PHOS()->GetRawFormatTimePeak() ;  
563         if (hgflag) {
564           hHighGain.Fit(gauss, "QRON") ; 
565           Int_t ampH =  static_cast<Int_t>(gauss->Eval(gauss->GetParameter(2)) + 0.5) ; 
566           Double_t timeH  = PHOS()->GetRawFormatTimePeak() - gauss->GetParameter(2) ; 
567           if (timeH < 0 ) // happens with noise 
568             timeH =  PHOS()->GetRawFormatTimePeak() ;  
569           new((*digits)[idigit]) AliPHOSDigit( -1, id+kHighGainIdOffset, ampH, timeH) ;
570           idigit++ ; 
571         }
572         else {
573           new((*digits)[idigit]) AliPHOSDigit( -1, id, ampL, timeL) ;
574           idigit++ ; 
575         }
576       }
577       first = kFALSE ; 
578       hLowGain.Reset() ; 
579       hHighGain.Reset() ; 
580       relId[0] = in.GetModule() ;
581       if ( relId[0] >= PHOS()->GetRawFormatHighGainOffset() ) { 
582         relId[0] -=  PHOS()->GetRawFormatHighGainOffset() ; 
583         hgflag = kTRUE ; 
584       } else 
585           hgflag = kFALSE ; 
586       relId[1] = 0 ; 
587       relId[2] = in.GetRow() ; 
588       relId[3] = in.GetColumn() ; 
589       PHOSGeometry()->RelToAbsNumbering(relId, id) ;    
590     }
591     if (hgflag)
592       hHighGain.Fill(
593                      in.GetTime() * PHOS()->GetRawFormatTimeMax() / PHOS()->GetRawFormatTimeBins(), 
594                      in. GetSignal() * PHOS()->GetRawFormatHighGainFactor() ) ;
595     else 
596       hLowGain.Fill(
597                     in.GetTime() * PHOS()->GetRawFormatTimeMax() / PHOS()->GetRawFormatTimeBins(), 
598                     in. GetSignal() ) ;
599   } // PHOS entries loop
600   
601   delete gauss ; 
602   
603   return Digits()->GetEntriesFast() ; 
604 }
605
606 //____________________________________________________________________________ 
607 Int_t AliPHOSGetter::ReadTreeD()
608 {
609   // Read the Digits
610   
611   PhosLoader()->CleanDigits() ;    
612   // gets TreeD from the root file (PHOS.Digits.root)
613   // if ( !IsLoaded("D") ) {
614     PhosLoader()->LoadDigits("UPDATE") ;
615     PhosLoader()->LoadDigitizer("UPDATE") ;
616     //  SetLoaded("D") ; 
617     //} 
618   return Digits()->GetEntries() ; 
619 }
620
621 //____________________________________________________________________________ 
622 Int_t AliPHOSGetter::ReadTreeH()
623 {
624   // Read the Hits
625   PhosLoader()->CleanHits() ;
626   // gets TreeH from the root file (PHOS.Hit.root)
627   //if ( !IsLoaded("H") ) {
628     PhosLoader()->LoadHits("UPDATE") ;
629   //  SetLoaded("H") ; 
630   //}  
631   return Hits()->GetEntries() ; 
632 }
633
634 //____________________________________________________________________________ 
635 Int_t AliPHOSGetter::ReadTreeR()
636 {
637   // Read the RecPoints
638   
639   PhosLoader()->CleanRecPoints() ;
640   // gets TreeR from the root file (PHOS.RecPoints.root)
641   //if ( !IsLoaded("R") ) {
642     PhosLoader()->LoadRecPoints("UPDATE") ;
643     PhosLoader()->LoadClusterizer("UPDATE") ;
644     //  SetLoaded("R") ; 
645     //}
646
647   return EmcRecPoints()->GetEntries() ; 
648 }
649
650 //____________________________________________________________________________ 
651 Int_t AliPHOSGetter::ReadTreeT()
652 {
653   // Read the TrackSegments
654   
655   PhosLoader()->CleanTracks() ; 
656   // gets TreeT from the root file (PHOS.TrackSegments.root)
657   //if ( !IsLoaded("T") ) {
658     PhosLoader()->LoadTracks("UPDATE") ;
659     PhosLoader()->LoadTrackSegmentMaker("UPDATE") ;
660     //    SetLoaded("T") ; 
661     //}
662
663   return TrackSegments()->GetEntries() ; 
664 }
665 //____________________________________________________________________________ 
666 Int_t AliPHOSGetter::ReadTreeP()
667 {
668   // Read the RecParticles
669   
670   PhosLoader()->CleanRecParticles() ; 
671
672   // gets TreeT from the root file (PHOS.TrackSegments.root)
673   //  if ( !IsLoaded("P") ) {
674     PhosLoader()->LoadRecParticles("UPDATE") ;
675     PhosLoader()->LoadPID("UPDATE") ;
676     //  SetLoaded("P") ; 
677     //}
678
679   return RecParticles()->GetEntries() ; 
680 }
681 //____________________________________________________________________________ 
682 Int_t AliPHOSGetter::ReadTreeS()
683 {
684   // Read the SDigits
685   
686   PhosLoader()->CleanSDigits() ; 
687   // gets TreeS from the root file (PHOS.SDigits.root)
688   //if ( !IsLoaded("S") ) {
689     PhosLoader()->LoadSDigits("READ") ;
690     PhosLoader()->LoadSDigitizer("READ") ;
691     //  SetLoaded("S") ; 
692     //}
693
694   return SDigits()->GetEntries() ; 
695 }
696
697 //____________________________________________________________________________ 
698 Int_t AliPHOSGetter::ReadTreeE(Int_t event)
699 {
700   // Read the ESD
701   
702   // gets esdTree from the root file (AliESDs.root)
703   if (!fESDFile)
704     if ( !OpenESDFile() ) 
705       return -1 ; 
706
707   fESDTree = static_cast<TTree*>(fESDFile->Get("esdTree")) ; 
708   fESD = new AliESD;
709    if (!fESDTree) {
710
711      Error("ReadTreeE", "no ESD tree found");
712      return -1;
713    }
714    fESDTree->SetBranchAddress("ESD", &fESD);
715    fESDTree->GetEvent(event);
716
717    return event ; 
718 }
719
720 //____________________________________________________________________________ 
721 TClonesArray * AliPHOSGetter::SDigits() 
722 {
723   // asks the Loader to return the Digits container 
724
725   TClonesArray * rv = 0 ; 
726   
727   rv = PhosLoader()->SDigits() ; 
728   if (!rv) {
729     PhosLoader()->MakeSDigitsArray() ;
730     rv = PhosLoader()->SDigits() ; 
731   }
732   return rv ; 
733 }
734
735 //____________________________________________________________________________ 
736 AliPHOSSDigitizer * AliPHOSGetter::SDigitizer()
737
738   // Returns pointer to the SDigitizer task 
739   AliPHOSSDigitizer * rv ; 
740   rv =  dynamic_cast<AliPHOSSDigitizer *>(PhosLoader()->SDigitizer()) ;
741   if (!rv) {
742     Event(0, "S") ; 
743     rv =  dynamic_cast<AliPHOSSDigitizer *>(PhosLoader()->SDigitizer()) ;
744   }
745   return rv ; 
746 }
747
748 //____________________________________________________________________________ 
749 TParticle * AliPHOSGetter::Secondary(const TParticle* p, Int_t index) const
750 {
751   // Return first (index=1) or second (index=2) secondary particle of primary particle p 
752
753   if(index <= 0) 
754     return 0 ;
755   if(index > 2)
756     return 0 ;
757
758   if(p) {
759   Int_t daughterIndex = p->GetDaughter(index-1) ; 
760   AliRunLoader * rl = AliRunLoader::GetRunLoader(PhosLoader()->GetTitle());
761   return  rl->GetAliRun()->GetMCApp()->Particle(daughterIndex) ; 
762   }
763   else
764     return 0 ;
765 }
766
767 //____________________________________________________________________________ 
768 void AliPHOSGetter::Track(Int_t itrack) 
769 {
770   // Read the first entry of PHOS branch in hit tree gAlice->TreeH()
771  
772  AliRunLoader * rl = AliRunLoader::GetRunLoader(PhosLoader()->GetTitle());
773
774   if( !TreeH() ) // load treeH the first time
775     rl->LoadHits() ;
776
777   // first time create the container
778   TClonesArray * hits = Hits() ; 
779   if ( hits ) 
780     hits->Clear() ; 
781
782   TBranch * phosbranch = dynamic_cast<TBranch*>(TreeH()->GetBranch("PHOS")) ; 
783   phosbranch->SetAddress(&hits) ;
784   phosbranch->GetEntry(itrack) ;
785 }
786
787 //____________________________________________________________________________ 
788 TTree * AliPHOSGetter::TreeD() const 
789 {
790   // Returns pointer to the Digits Tree
791   TTree * rv = 0 ; 
792   rv = PhosLoader()->TreeD() ; 
793   if ( !rv ) {
794     PhosLoader()->MakeTree("D");
795     rv = PhosLoader()->TreeD() ;
796   } 
797   
798   return rv ; 
799 }
800
801 //____________________________________________________________________________ 
802 TTree * AliPHOSGetter::TreeH() const 
803 {
804   // Returns pointer to the Hits Tree
805   TTree * rv = 0 ; 
806   rv = PhosLoader()->TreeH() ; 
807   if ( !rv ) {
808     PhosLoader()->MakeTree("H");
809     rv = PhosLoader()->TreeH() ;
810   } 
811   
812   return rv ; 
813 }
814
815 //____________________________________________________________________________ 
816 TTree * AliPHOSGetter::TreeR() const 
817 {
818   // Returns pointer to the RecPoints Tree
819   TTree * rv = 0 ; 
820   rv = PhosLoader()->TreeR() ; 
821   if ( !rv ) {
822     PhosLoader()->MakeTree("R");
823     rv = PhosLoader()->TreeR() ;
824   } 
825   
826   return rv ; 
827 }
828
829 //____________________________________________________________________________ 
830 TTree * AliPHOSGetter::TreeT() const 
831 {
832   // Returns pointer to the TrackSegments Tree
833   TTree * rv = 0 ; 
834   rv = PhosLoader()->TreeT() ; 
835   if ( !rv ) {
836     PhosLoader()->MakeTree("T");
837     rv = PhosLoader()->TreeT() ;
838   } 
839   
840   return rv ; 
841 }
842 //____________________________________________________________________________ 
843 TTree * AliPHOSGetter::TreeP() const 
844 {
845   // Returns pointer to the RecParticles  Tree
846   TTree * rv = 0 ; 
847   rv = PhosLoader()->TreeP() ; 
848   if ( !rv ) {
849     PhosLoader()->MakeTree("P");
850     rv = PhosLoader()->TreeP() ;
851   } 
852   
853   return rv ; 
854 }
855
856 //____________________________________________________________________________ 
857 TTree * AliPHOSGetter::TreeS() const 
858
859  // Returns pointer to the SDigits Tree
860   TTree * rv = 0 ; 
861   rv = PhosLoader()->TreeS() ; 
862   if ( !rv ) {
863     PhosLoader()->MakeTree("S");
864     rv = PhosLoader()->TreeS() ;
865   } 
866   
867   return rv ; 
868 }
869
870 //____________________________________________________________________________ 
871 Bool_t AliPHOSGetter::VersionExists(TString & opt) const
872 {
873   // checks if the version with the present name already exists in the same directory
874
875   Bool_t rv = kFALSE ;
876  
877   AliRunLoader * rl = AliRunLoader::GetRunLoader(PhosLoader()->GetTitle());
878   TString version( rl->GetEventFolder()->GetName() ) ; 
879
880   opt.ToLower() ; 
881   
882   if ( opt == "sdigits") {
883     // add the version name to the root file name
884     TString fileName( PhosLoader()->GetSDigitsFileName() ) ; 
885     if (version != AliConfig::GetDefaultEventFolderName()) // only if not the default folder name 
886       fileName = fileName.ReplaceAll(".root", "") + "_" + version + ".root" ;
887     if ( !(gSystem->AccessPathName(fileName)) ) { 
888       Warning("VersionExists", "The file %s already exists", fileName.Data()) ;
889       rv = kTRUE ; 
890     }
891     PhosLoader()->SetSDigitsFileName(fileName) ;
892   }
893
894   if ( opt == "digits") {
895     // add the version name to the root file name
896     TString fileName( PhosLoader()->GetDigitsFileName() ) ; 
897     if (version != AliConfig::GetDefaultEventFolderName()) // only if not the default folder name 
898       fileName = fileName.ReplaceAll(".root", "") + "_" + version + ".root" ;
899     if ( !(gSystem->AccessPathName(fileName)) ) {
900       Warning("VersionExists", "The file %s already exists", fileName.Data()) ;  
901       rv = kTRUE ; 
902     }
903   }
904
905   return rv ;
906
907 }
908
909 //____________________________________________________________________________ 
910 UShort_t AliPHOSGetter::EventPattern(void) const
911 {
912   // Return the pattern (trigger bit register) of the beam-test event
913   if(fBTE)
914     return fBTE->GetPattern() ;
915   else
916     return 0 ;
917 }
918 //____________________________________________________________________________ 
919 Float_t AliPHOSGetter::BeamEnergy(void) const
920 {
921   // Return the beam energy of the beam-test event
922   if(fBTE)
923     return fBTE->GetBeamEnergy() ;
924   else
925     return 0 ;
926 }