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