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