Fixing memory leak (L.Gaudichet)
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALSDigitizer.cxx
CommitLineData
ffa6d63b 1/*************************************************************************
61e0abb5 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// This is a TTask that makes SDigits out of Hits
20// A Summable Digits is the sum of all hits originating
ffa6d63b 21// from one in one tower of the EMCAL
61e0abb5 22// A threshold for assignment of the primary to SDigit is applied
23// SDigits are written to TreeS, branch "EMCAL"
24// AliEMCALSDigitizer with all current parameters is written
25// to TreeS branch "AliEMCALSDigitizer".
26// Both branches have the same title. If necessary one can produce
27// another set of SDigits with different parameters. Two versions
28// can be distunguished using titles of the branches.
29// User case:
30// root [0] AliEMCALSDigitizer * s = new AliEMCALSDigitizer("galice.root")
31// Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
32// root [1] s->ExecuteTask()
33// // Makes SDigitis for all events stored in galice.root
34// root [2] s->SetPedestalParameter(0.001)
35// // One can change parameters of digitization
36// root [3] s->SetSDigitsBranch("Redestal 0.001")
37// // and write them into the new branch
38// root [4] s->ExeciteTask("deb all tim")
39// // available parameters:
40// deb - print # of produced SDigitis
41// deb all - print # and list of produced SDigits
42// tim - print benchmarking information
43//
ffa6d63b 44//*-- Author : Sahal Yacoob (LBL)
45// based on : AliPHOSSDigitzer
05a92d59 46// Modif:
47// August 2002 Yves Schutz: clone PHOS as closely as possible and intoduction
48// of new IO (à la PHOS)
61e0abb5 49//////////////////////////////////////////////////////////////////////////////
50
61e0abb5 51// --- ROOT system ---
61e0abb5 52#include "TBenchmark.h"
1963b290 53#include "TH1.h"
54#include "TBrowser.h"
5dee926e 55#include "Riostream.h"
1963b290 56//#include "TObjectTable.h"
05a92d59 57
61e0abb5 58// --- Standard library ---
d64c959b 59#include "stdlib.h"
61e0abb5 60
61// --- AliRoot header files ---
13e1db42 62#include "AliLog.h"
5dee926e 63#include "AliRunLoader.h"
64#include "AliStack.h"
61e0abb5 65#include "AliEMCALDigit.h"
5dee926e 66#include "AliEMCALLoader.h"
05a92d59 67#include "AliEMCALHit.h"
68#include "AliEMCALSDigitizer.h"
88cb7938 69#include "AliEMCALGeometry.h"
315d1c64 70#include "AliEMCALHistoUtilities.h"
61e0abb5 71
72ClassImp(AliEMCALSDigitizer)
61e0abb5 73
74//____________________________________________________________________________
88cb7938 75 AliEMCALSDigitizer::AliEMCALSDigitizer():TTask("","")
61e0abb5 76{
77 // ctor
1963b290 78 fFirstEvent = fLastEvent = fControlHists = 0;
92f521a9 79 fDefaultInit = kTRUE ;
1963b290 80 fHists = 0;
61e0abb5 81}
82
83//____________________________________________________________________________
e191bb57 84AliEMCALSDigitizer::AliEMCALSDigitizer(const char * alirunFileName,
85 const char * eventFolderName):
86 TTask("EMCAL"+AliConfig::Instance()->GetSDigitizerTaskName(), alirunFileName),
88cb7938 87 fEventFolderName(eventFolderName)
61e0abb5 88{
89 // ctor
1963b290 90 fFirstEvent = fLastEvent = fControlHists = 0 ; // runs one event by defaut
814ad4bf 91 Init();
62741b41 92 InitParameters() ;
92f521a9 93 fDefaultInit = kFALSE ;
1963b290 94 fHists = 0;
e52475ed 95 fControlHists = 1;
96 if(fControlHists) BookControlHists(1);
61e0abb5 97}
98
88cb7938 99
61e0abb5 100//____________________________________________________________________________
9e5d2067 101AliEMCALSDigitizer::AliEMCALSDigitizer(const AliEMCALSDigitizer & sd) : TTask(sd) {
88cb7938 102 //cpy ctor
103
5082063d 104 fFirstEvent = sd.fFirstEvent ;
105 fLastEvent = sd.fLastEvent ;
88cb7938 106 fA = sd.fA ;
107 fB = sd.fB ;
88cb7938 108 fECPrimThreshold = sd.fECPrimThreshold ;
88cb7938 109 fSDigitsInRun = sd.fSDigitsInRun ;
110 SetName(sd.GetName()) ;
111 SetTitle(sd.GetTitle()) ;
112 fEventFolderName = sd.fEventFolderName;
61e0abb5 113}
36657114 114
88cb7938 115
61e0abb5 116//____________________________________________________________________________
797e311f 117AliEMCALSDigitizer::~AliEMCALSDigitizer() {
5dee926e 118 AliLoader *emcalLoader=0;
119 if ((emcalLoader = AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL")))
120 emcalLoader->CleanSDigitizer();
797e311f 121}
122
123//____________________________________________________________________________
61e0abb5 124void AliEMCALSDigitizer::Init(){
814ad4bf 125 // Initialization: open root-file, allocate arrays for hits and sdigits,
36657114 126 // attach task SDigitizer to the list of EMCAL tasks
814ad4bf 127 //
128 // Initialization can not be done in the default constructor
129 //============================================================= YS
130 // The initialisation is now done by the getter
61e0abb5 131
88cb7938 132 fInit = kTRUE ;
afa0c708 133
5dee926e 134 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
135
136 if ( emcalLoader == 0 ) {
137 Fatal("Init", "Could not obtain the AliEMCALLoader");
814ad4bf 138 return ;
139 }
140
5dee926e 141 emcalLoader->PostSDigitizer(this);
142 emcalLoader->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
88cb7938 143
839828a6 144}
145
146//____________________________________________________________________________
afa0c708 147void AliEMCALSDigitizer::InitParameters()
148{
5dee926e 149 const AliEMCALGeometry * geom = AliEMCALGeometry::GetInstance();
62741b41 150 if (geom->GetSampling() == 0.) {
5082063d 151 Fatal("InitParameters", "Sampling factor not set !") ;
62741b41 152 }
1963b290 153 // Initializes parameters
154 fA = 0;
155 fB = 1.e+7;
156 fSampling = geom->GetSampling();
157
158 // threshold for deposit energy of hit
159 fECPrimThreshold = 0.; // 24-nov-04 - was 1.e-6;
e52475ed 160 Print("");
839828a6 161}
162
61e0abb5 163//____________________________________________________________________________
afa0c708 164void AliEMCALSDigitizer::Exec(Option_t *option)
165{
fdebddeb 166 // Collects all hit of the same tower into digits
1963b290 167 TString o(option); o.ToUpper();
814ad4bf 168 if (strstr(option, "print") ) {
d64c959b 169 Print() ;
814ad4bf 170 return ;
171 }
61e0abb5 172
814ad4bf 173 if(strstr(option,"tim"))
174 gBenchmark->Start("EMCALSDigitizer");
97f3344e 175
5dee926e 176 AliRunLoader *rl = AliRunLoader::GetRunLoader();
177 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
13e1db42 178
88cb7938 179 //switch off reloading of this task while getting event
180 if (!fInit) { // to prevent overwrite existing file
13e1db42 181 AliError( Form("Give a version name different from %s", fEventFolderName.Data()) ) ;
88cb7938 182 return ;
fdebddeb 183 }
184
5082063d 185 if (fLastEvent == -1)
5dee926e 186 fLastEvent = rl->GetNumberOfEvents() - 1 ;
1963b290 187 else {
5dee926e 188 fLastEvent = TMath::Min(fLastEvent, rl->GetNumberOfEvents()-1);
1963b290 189 }
5082063d 190 Int_t nEvents = fLastEvent - fFirstEvent + 1;
191
1963b290 192 Int_t ievent;
193 Float_t energy=0.; // de * fSampling - 23-nov-04
5dee926e 194 rl->LoadKinematics();
195 rl->LoadHits("EMCAL");
196
cf24bdc4 197 for (ievent = fFirstEvent; ievent <= fLastEvent; ievent++) {
5dee926e 198 rl->GetEvent(ievent);
199 TTree * treeS = emcalLoader->TreeS();
200 if ( !treeS ) {
201 emcalLoader->MakeSDigitsContainer();
202 treeS = emcalLoader->TreeS();
203 }
204 TClonesArray * hits = emcalLoader->Hits() ;
205 TClonesArray * sdigits = emcalLoader->SDigits() ;
814ad4bf 206 sdigits->Clear();
61e0abb5 207 Int_t nSdigits = 0 ;
05a92d59 208 //Now make SDigits from hits, for EMCAL it is the same, so just copy
5dee926e 209 Int_t nPrim = static_cast<Int_t>((emcalLoader->TreeH())->GetEntries()) ;
210 // This is not true: there is only one list of hits (MvL jan 2006)
05a92d59 211 // Attention nPrim is the number of primaries tracked by Geant
5dee926e 212 // and this number could be different to the number of Primaries in TreeK;
05a92d59 213 Int_t iprim ;
62741b41 214 for ( iprim = 0 ; iprim < nPrim ; iprim++ ) {
215 //=========== Get the EMCAL branch from Hits Tree for the Primary iprim
5dee926e 216 rl->Stack()->Particle(iprim) ;
62741b41 217 Int_t i;
5dee926e 218 AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance();
62741b41 219 for ( i = 0 ; i < hits->GetEntries() ; i++ ) {
220 AliEMCALHit * hit = dynamic_cast<AliEMCALHit*>(hits->At(i)) ;
221 AliEMCALDigit * curSDigit = 0 ;
222 AliEMCALDigit * sdigit = 0 ;
223 Bool_t newsdigit = kTRUE;
224
1963b290 225 // hit->GetId() - Absolute Id number EMCAL segment
226 if(geom->CheckAbsCellId(hit->GetId())) { // was IsInECA(hit->GetId())
227 energy = hit->GetEnergy() * fSampling; // 23-nov-04
228 if(energy > fECPrimThreshold )
62741b41 229 // Assign primary number only if deposited energy is significant
62741b41 230 curSDigit = new AliEMCALDigit( hit->GetPrimary(),
231 hit->GetIparent(), hit->GetId(),
1963b290 232 Digitize(energy), hit->GetTime() ) ;
62741b41 233 else
234 curSDigit = new AliEMCALDigit( -1 ,
235 -1 ,
236 hit->GetId(),
1963b290 237 Digitize(energy), hit->GetTime() ) ;
238 } else {
239 Warning("Exec"," abs id %i is bad \n", hit->GetId());
240 newsdigit = kFALSE;
5dee926e 241 curSDigit = 0;
1963b290 242 }
fdebddeb 243
5dee926e 244 if(curSDigit != 0){
245 for(Int_t check= 0; check < nSdigits ; check++) {
246 sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(check)) ;
247
fdebddeb 248 if( sdigit->GetId() == curSDigit->GetId()) { // Are we in the same ECAL tower ?
5dee926e 249 *sdigit = *sdigit + *curSDigit;
250 newsdigit = kFALSE;
fdebddeb 251 }
afa0c708 252 }
afa0c708 253 }
fdebddeb 254 if (newsdigit) {
62741b41 255 new((*sdigits)[nSdigits]) AliEMCALDigit(*curSDigit);
256 nSdigits++ ;
e1f60236 257 }
62741b41 258 delete curSDigit ;
fdebddeb 259 } // loop over all hits (hit = deposited energy/entering particle)
62741b41 260 } // loop over iprim
62741b41 261 sdigits->Sort() ;
262
263 nSdigits = sdigits->GetEntriesFast() ;
264 fSDigitsInRun += nSdigits ;
62741b41 265
d64c959b 266 Int_t nPrimarymax = -1 ;
62741b41 267 Int_t i ;
1963b290 268 Double_t e=0.,esum=0.;
315d1c64 269 AliEMCALHistoUtilities::FillH1(fHists, 0, double(sdigits->GetEntriesFast()));
62741b41 270 for (i = 0 ; i < sdigits->GetEntriesFast() ; i++) {
271 AliEMCALDigit * sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(i)) ;
272 sdigit->SetIndexInList(i) ;
1963b290 273
315d1c64 274 AliEMCALHistoUtilities::FillH1(fHists, 2, double(sdigit->GetAmp()));
1963b290 275 e = double(Calibrate(sdigit->GetAmp()));
276 esum += e;
315d1c64 277 AliEMCALHistoUtilities::FillH1(fHists, 3, e);
278 AliEMCALHistoUtilities::FillH1(fHists, 4, double(sdigit->GetId()));
62741b41 279 }
315d1c64 280 if(esum>0.) AliEMCALHistoUtilities::FillH1(fHists, 1, esum);
62741b41 281
1963b290 282 for (i = 0 ; i < sdigits->GetEntriesFast() ; i++) { // for what
d64c959b 283 if (((dynamic_cast<AliEMCALDigit *>(sdigits->At(i)))->GetNprimary()) > nPrimarymax)
284 nPrimarymax = ((dynamic_cast<AliEMCALDigit *>(sdigits->At(i)))->GetNprimary()) ;
62741b41 285 }
5dee926e 286
88cb7938 287 // Now write SDigits
88cb7938 288
62741b41 289 Int_t bufferSize = 32000 ;
5dee926e 290 TBranch * sdigitsBranch = treeS->GetBranch("EMCAL");
291 if (sdigitsBranch)
292 sdigitsBranch->SetAddress(&sdigits);
293 else
294 treeS->Branch("EMCAL",&sdigits,bufferSize);
88cb7938 295
5dee926e 296 treeS->Fill();
297
298 emcalLoader->WriteSDigits("OVERWRITE");
62741b41 299
300 //NEXT - SDigitizer
5dee926e 301 emcalLoader->WriteSDigitizer("OVERWRITE"); // why in event cycle ?
62741b41 302
303 if(strstr(option,"deb"))
304 PrintSDigits(option) ;
1963b290 305 }
88cb7938 306
307 Unload();
308
5dee926e 309 emcalLoader->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
88cb7938 310
61e0abb5 311 if(strstr(option,"tim")){
9859bfc0 312 gBenchmark->Stop("EMCALSDigitizer");
1963b290 313 printf("\n Exec: took %f seconds for SDigitizing %f seconds per event\n",
5082063d 314 gBenchmark->GetCpuTime("EMCALSDigitizer"), gBenchmark->GetCpuTime("EMCALSDigitizer")/nEvents ) ;
62741b41 315 }
61e0abb5 316}
05a92d59 317
106fc2fa 318
1963b290 319void AliEMCALSDigitizer::Print1(Option_t * option)
320{
321 Print();
322 PrintSDigits(option);
323}
324
61e0abb5 325//__________________________________________________________________
5dee926e 326void AliEMCALSDigitizer::Print(Option_t *option) const
05a92d59 327{
328 // Prints parameters of SDigitizer
e52475ed 329 printf("Print: \n------------------- %s ------------- option %s\n", GetName() , option) ;
1963b290 330 printf(" fInit %i\n", int(fInit));
331 printf(" fFirstEvent %i\n", fFirstEvent);
332 printf(" fLastEvent %i\n", fLastEvent);
88cb7938 333 printf(" Writing SDigits to branch with title %s\n", fEventFolderName.Data()) ;
1963b290 334 printf(" with digitization parameters A = %f\n", fA) ;
335 printf(" B = %f\n", fB) ;
336 printf(" Threshold for EC Primary assignment = %f\n", fECPrimThreshold) ;
337 printf(" Sampling = %f\n", fSampling);
88cb7938 338 printf("---------------------------------------------------\n") ;
61e0abb5 339}
05a92d59 340
61e0abb5 341//__________________________________________________________________
05a92d59 342Bool_t AliEMCALSDigitizer::operator==( AliEMCALSDigitizer const &sd )const
343{
344 // Equal operator.
345 // SDititizers are equal if their pedestal, slope and threshold are equal
89e103bd 346 if( (fA==sd.fA)&&(fB==sd.fB)&&
fdebddeb 347 (fECPrimThreshold==sd.fECPrimThreshold))
61e0abb5 348 return kTRUE ;
349 else
350 return kFALSE ;
351}
173558f2 352
61e0abb5 353//__________________________________________________________________
d64c959b 354void AliEMCALSDigitizer::PrintSDigits(Option_t * option)
355{
61e0abb5 356 //Prints list of digits produced at the current pass of AliEMCALDigitizer
357
d64c959b 358
5dee926e 359 // AliEMCALGetter * gime = AliEMCALGetter::Instance() ;
360 AliEMCALLoader *rl = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
361 const TClonesArray * sdigits = rl->SDigits() ;
61e0abb5 362
fdebddeb 363 printf("\n") ;
5dee926e 364 printf("event %i", rl->GetRunLoader()->GetEventNumber());
1963b290 365 printf(" Number of entries in SDigits list %i", sdigits->GetEntriesFast());
814ad4bf 366 if(strstr(option,"all")||strstr(option,"EMC")){
9859bfc0 367
61e0abb5 368 //loop over digits
369 AliEMCALDigit * digit;
fdebddeb 370 printf("\n Id Amplitude Time Index Nprim: Primaries list \n") ;
1963b290 371 Int_t index, isum=0;
11f9c5ff 372 char * tempo = new char[8192];
814ad4bf 373 for (index = 0 ; index < sdigits->GetEntries() ; index++) {
05a92d59 374 digit = dynamic_cast<AliEMCALDigit *>( sdigits->At(index) ) ;
11f9c5ff 375 sprintf(tempo, "\n%6d %8d %6.5e %4d %2d :",
376 digit->GetId(), digit->GetAmp(), digit->GetTime(), digit->GetIndexInList(), digit->GetNprimary()) ;
1963b290 377 printf(tempo);
378 isum += digit->GetAmp();
61e0abb5 379
380 Int_t iprimary;
9859bfc0 381 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++) {
11f9c5ff 382 sprintf(tempo, "%d ",digit->GetPrimary(iprimary+1) ) ;
fdebddeb 383 printf(tempo);
9859bfc0 384 }
61e0abb5 385 }
11f9c5ff 386 delete tempo ;
1963b290 387 printf("\n** Sum %i : %10.3f GeV/c **\n ", isum, double(isum)*1.e-6);
388 } else printf("\n");
61e0abb5 389}
05a92d59 390
05a92d59 391//____________________________________________________________________________
88cb7938 392void AliEMCALSDigitizer::Unload() const
05a92d59 393{
d64c959b 394 // Unload Hits and SDigits from the folder
5dee926e 395 AliEMCALLoader *rl = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
396 rl->UnloadHits() ;
397 rl->UnloadSDigits() ;
05a92d59 398}
1963b290 399
400void AliEMCALSDigitizer::Browse(TBrowser* b)
401{
402 if(fHists) b->Add(fHists);
403 TTask::Browse(b);
404}
405
eb0b1051 406TList *AliEMCALSDigitizer::BookControlHists(int var)
1963b290 407{ // 22-nov-04
408 gROOT->cd();
5dee926e 409 const AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance() ;
1963b290 410 if(var>=1){
e52475ed 411 printf(" AliEMCALSDigitizer::BookControlHists() in action \n");
1963b290 412 new TH1F("HSDigiN", "#EMCAL sdigits ", 1001, -0.5, 1000.5);
413 new TH1F("HSDigiSumEnergy","Sum.EMCAL energy", 1000, 0.0, 100.);
414 new TH1F("HSDigiAmp", "EMCAL sdigits amplitude", 1000, 0., 2.e+9);
415 new TH1F("HSDigiEnergy","EMCAL cell energy", 1000, 0.0, 100.);
416 new TH1F("HSDigiAbsId","EMCAL absID for sdigits",
417 geom->GetNCells(), 0.5, Double_t(geom->GetNCells())+0.5);
418 }
e52475ed 419
315d1c64 420 fHists = AliEMCALHistoUtilities::MoveHistsToList("EmcalSDigiControlHists", kFALSE);
5dee926e 421 fHists = 0;
e52475ed 422
1963b290 423 return fHists;
424}
425
426void AliEMCALSDigitizer::SaveHists(const char* name, Bool_t kSingleKey, const char* opt)
427{
315d1c64 428 AliEMCALHistoUtilities::SaveListOfHists(fHists, name, kSingleKey, opt);
1963b290 429}