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