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