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