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