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