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