Move AliWarning and AliInfo to AliDebug to reduce prints during reconstruction
[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
ab6a174f 23//
24// JLK 26-Jun-2008 Added explanation:
25// SDigits need to hold the energy sum of the hits, but AliEMCALDigit
26// can (should) only store amplitude. Therefore, the SDigit energy is
27// "digitized" before being stored and must be "calibrated" back to an
28// energy before SDigits are summed to form true Digits
29//
61e0abb5 30// SDigits are written to TreeS, branch "EMCAL"
31// AliEMCALSDigitizer with all current parameters is written
32// to TreeS branch "AliEMCALSDigitizer".
33// Both branches have the same title. If necessary one can produce
34// another set of SDigits with different parameters. Two versions
35// can be distunguished using titles of the branches.
36// User case:
37// root [0] AliEMCALSDigitizer * s = new AliEMCALSDigitizer("galice.root")
38// Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
39// root [1] s->ExecuteTask()
40// // Makes SDigitis for all events stored in galice.root
41// root [2] s->SetPedestalParameter(0.001)
42// // One can change parameters of digitization
43// root [3] s->SetSDigitsBranch("Redestal 0.001")
44// // and write them into the new branch
45// root [4] s->ExeciteTask("deb all tim")
46// // available parameters:
47// deb - print # of produced SDigitis
48// deb all - print # and list of produced SDigits
49// tim - print benchmarking information
50//
ffa6d63b 51//*-- Author : Sahal Yacoob (LBL)
52// based on : AliPHOSSDigitzer
61e0abb5 53//////////////////////////////////////////////////////////////////////////////
54
61e0abb5 55// --- ROOT system ---
bda7cf56 56#include <TBenchmark.h>
bda7cf56 57#include <TBrowser.h>
d804d556 58//#include <Riostream.h>
bda7cf56 59#include <TMath.h>
a1e17193 60#include <TROOT.h>
05a92d59 61
61e0abb5 62// --- Standard library ---
d64c959b 63#include "stdlib.h"
61e0abb5 64
65// --- AliRoot header files ---
13e1db42 66#include "AliLog.h"
5dee926e 67#include "AliRunLoader.h"
68#include "AliStack.h"
61e0abb5 69#include "AliEMCALDigit.h"
5dee926e 70#include "AliEMCALLoader.h"
05a92d59 71#include "AliEMCALHit.h"
72#include "AliEMCALSDigitizer.h"
88cb7938 73#include "AliEMCALGeometry.h"
61e0abb5 74
75ClassImp(AliEMCALSDigitizer)
61e0abb5 76
77//____________________________________________________________________________
18a21c7c 78AliEMCALSDigitizer::AliEMCALSDigitizer()
79 : TTask("",""),
80 fA(0.),fB(0.),fECPrimThreshold(0.),
81 fDefaultInit(kTRUE),
82 fEventFolderName(0),
83 fInit(0),
84 fSDigitsInRun(0),
85 fFirstEvent(0),
86 fLastEvent(0),
7ea6391b 87 fSampling(0.)
61e0abb5 88{
89 // ctor
0a4cb131 90 InitParameters();
61e0abb5 91}
92
93//____________________________________________________________________________
e191bb57 94AliEMCALSDigitizer::AliEMCALSDigitizer(const char * alirunFileName,
18a21c7c 95 const char * eventFolderName)
96 : TTask("EMCAL"+AliConfig::Instance()->GetSDigitizerTaskName(), alirunFileName),
97 fA(0.),fB(0.),fECPrimThreshold(0.),
98 fDefaultInit(kFALSE),
99 fEventFolderName(eventFolderName),
100 fInit(0),
101 fSDigitsInRun(0),
102 fFirstEvent(0),
103 fLastEvent(0),
7ea6391b 104 fSampling(0.)
61e0abb5 105{
106 // ctor
814ad4bf 107 Init();
62741b41 108 InitParameters() ;
7ea6391b 109
61e0abb5 110}
111
88cb7938 112
61e0abb5 113//____________________________________________________________________________
18a21c7c 114AliEMCALSDigitizer::AliEMCALSDigitizer(const AliEMCALSDigitizer & sd)
115 : TTask(sd.GetName(),sd.GetTitle()),
116 fA(sd.fA),
117 fB(sd.fB),
118 fECPrimThreshold(sd.fECPrimThreshold),
119 fDefaultInit(sd.fDefaultInit),
120 fEventFolderName(sd.fEventFolderName),
121 fInit(sd.fInit),
122 fSDigitsInRun(sd.fSDigitsInRun),
123 fFirstEvent(sd.fFirstEvent),
124 fLastEvent(sd.fLastEvent),
7ea6391b 125 fSampling(sd.fSampling)
18a21c7c 126{
88cb7938 127 //cpy ctor
61e0abb5 128}
36657114 129
88cb7938 130
61e0abb5 131//____________________________________________________________________________
797e311f 132AliEMCALSDigitizer::~AliEMCALSDigitizer() {
14ce0a6e 133 //dtor
5dee926e 134 AliLoader *emcalLoader=0;
33c3c91a 135 if ((emcalLoader = AliRunLoader::Instance()->GetDetectorLoader("EMCAL")))
5dee926e 136 emcalLoader->CleanSDigitizer();
797e311f 137}
138
139//____________________________________________________________________________
61e0abb5 140void AliEMCALSDigitizer::Init(){
814ad4bf 141 // Initialization: open root-file, allocate arrays for hits and sdigits,
36657114 142 // attach task SDigitizer to the list of EMCAL tasks
814ad4bf 143 //
144 // Initialization can not be done in the default constructor
145 //============================================================= YS
146 // The initialisation is now done by the getter
61e0abb5 147
dc7da436 148 fInit = kTRUE;
afa0c708 149
33c3c91a 150 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::Instance()->GetDetectorLoader("EMCAL"));
5dee926e 151
152 if ( emcalLoader == 0 ) {
153 Fatal("Init", "Could not obtain the AliEMCALLoader");
814ad4bf 154 return ;
155 }
156
5dee926e 157 emcalLoader->PostSDigitizer(this);
158 emcalLoader->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
88cb7938 159
839828a6 160}
161
162//____________________________________________________________________________
afa0c708 163void AliEMCALSDigitizer::InitParameters()
164{
14ce0a6e 165 //initialize parameters for sdigitization
166
5dee926e 167 const AliEMCALGeometry * geom = AliEMCALGeometry::GetInstance();
62741b41 168 if (geom->GetSampling() == 0.) {
5082063d 169 Fatal("InitParameters", "Sampling factor not set !") ;
62741b41 170 }
ab6a174f 171
172 //
173 //JLK 26-Jun-2008 THIS SHOULD HAVE BEEN EXPLAINED AGES AGO:
174 //
175 //In order to be able to store SDigit Energy info into
176 //AliEMCALDigit, we need to convert it temporarily to an ADC amplitude
177 //and later when summing SDigits to form digits, convert it back to
178 //energy. These fA and fB parameters accomplish this through the
179 //Digitize() and Calibrate() methods
180 //
1963b290 181 // Initializes parameters
182 fA = 0;
63b36084 183 fB = 1.e+6; // Changed 24 Apr 2007. Dynamic range now 2 TeV
1963b290 184 fSampling = geom->GetSampling();
185
ff98b189 186 // threshold for deposit energy of hit
187 fECPrimThreshold = 0.05;// GeV // 22-may-07 was 0// 24-nov-04 - was 1.e-6;
188
b42d4197 189 AliDebug(2,Form("Print: \n------------------- %s -------------\n",GetName()));
190 AliDebug(2,Form(" fInit %i\n", int(fInit)));
191 AliDebug(2,Form(" fFirstEvent %i\n", fFirstEvent));
192 AliDebug(2,Form(" fLastEvent %i\n", fLastEvent));
193 AliDebug(2,Form(" Writing SDigits to branch with title %s\n", fEventFolderName.Data()));
194 AliDebug(2,Form(" with digitization parameters A = %f\n", fA));
195 AliDebug(2,Form(" B = %f\n", fB));
196 AliDebug(2,Form(" Threshold for EC Primary assignment = %f\n", fECPrimThreshold));
197 AliDebug(2,Form(" Sampling = %f\n", fSampling));
198 AliDebug(2,Form("---------------------------------------------------\n"));
199
b42d4197 200
839828a6 201}
202
61e0abb5 203//____________________________________________________________________________
afa0c708 204void AliEMCALSDigitizer::Exec(Option_t *option)
205{
fdebddeb 206 // Collects all hit of the same tower into digits
1963b290 207 TString o(option); o.ToUpper();
814ad4bf 208 if (strstr(option, "print") ) {
b42d4197 209
210 AliDebug(2,Form("Print: \n------------------- %s -------------\n",GetName()));
211 AliDebug(2,Form(" fInit %i\n", int(fInit)));
212 AliDebug(2,Form(" fFirstEvent %i\n", fFirstEvent));
213 AliDebug(2,Form(" fLastEvent %i\n", fLastEvent));
214 AliDebug(2,Form(" Writing SDigits to branch with title %s\n", fEventFolderName.Data()));
215 AliDebug(2,Form(" with digitization parameters A = %f\n", fA));
216 AliDebug(2,Form(" B = %f\n", fB));
217 AliDebug(2,Form(" Threshold for EC Primary assignment = %f\n", fECPrimThreshold));
218 AliDebug(2,Form(" Sampling = %f\n", fSampling));
219 AliDebug(2,Form("---------------------------------------------------\n"));
220
814ad4bf 221 return ;
222 }
61e0abb5 223
814ad4bf 224 if(strstr(option,"tim"))
225 gBenchmark->Start("EMCALSDigitizer");
97f3344e 226
33c3c91a 227 AliRunLoader *rl = AliRunLoader::Instance();
5dee926e 228 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
13e1db42 229
88cb7938 230 //switch off reloading of this task while getting event
231 if (!fInit) { // to prevent overwrite existing file
13e1db42 232 AliError( Form("Give a version name different from %s", fEventFolderName.Data()) ) ;
88cb7938 233 return ;
fdebddeb 234 }
235
5082063d 236 if (fLastEvent == -1)
5dee926e 237 fLastEvent = rl->GetNumberOfEvents() - 1 ;
1963b290 238 else {
5dee926e 239 fLastEvent = TMath::Min(fLastEvent, rl->GetNumberOfEvents()-1);
1963b290 240 }
5082063d 241 Int_t nEvents = fLastEvent - fFirstEvent + 1;
242
1963b290 243 Int_t ievent;
244 Float_t energy=0.; // de * fSampling - 23-nov-04
5dee926e 245 rl->LoadKinematics();
246 rl->LoadHits("EMCAL");
247
cf24bdc4 248 for (ievent = fFirstEvent; ievent <= fLastEvent; ievent++) {
5dee926e 249 rl->GetEvent(ievent);
250 TTree * treeS = emcalLoader->TreeS();
251 if ( !treeS ) {
252 emcalLoader->MakeSDigitsContainer();
253 treeS = emcalLoader->TreeS();
254 }
255 TClonesArray * hits = emcalLoader->Hits() ;
256 TClonesArray * sdigits = emcalLoader->SDigits() ;
814ad4bf 257 sdigits->Clear();
62741b41 258
eb73853b 259 Int_t nSdigits = 0 ;
260 Int_t i;
261 AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance();
262 for ( i = 0 ; i < hits->GetEntries() ; i++ ) {
263 AliEMCALHit * hit = dynamic_cast<AliEMCALHit*>(hits->At(i)) ;
264 AliEMCALDigit * curSDigit = 0 ;
265 AliEMCALDigit * sdigit = 0 ;
266 Bool_t newsdigit = kTRUE;
267
268 // hit->GetId() - Absolute Id number EMCAL segment
269 if(geom->CheckAbsCellId(hit->GetId())) { // was IsInECA(hit->GetId())
270 energy = hit->GetEnergy() * fSampling; // 23-nov-04
271 if(energy > fECPrimThreshold )
272 // Assign primary number only if deposited energy is significant
273 curSDigit = new AliEMCALDigit( hit->GetPrimary(),
274 hit->GetIparent(), hit->GetId(),
af5bdd85 275 Digitize(energy), hit->GetTime(),
276 -1, energy ) ;
62741b41 277 else
278 curSDigit = new AliEMCALDigit( -1 ,
279 -1 ,
280 hit->GetId(),
af5bdd85 281 Digitize(energy), hit->GetTime(),
282 -1, energy ) ;
eb73853b 283 } else {
284 Warning("Exec"," abs id %i is bad \n", hit->GetId());
285 newsdigit = kFALSE;
286 curSDigit = 0;
287 }
288
289 if(curSDigit != 0){
290 for(Int_t check= 0; check < nSdigits ; check++) {
291 sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(check)) ;
292
293 if( sdigit->GetId() == curSDigit->GetId()) { // Are we in the same ECAL tower ?
294 *sdigit = *sdigit + *curSDigit;
295 newsdigit = kFALSE;
afa0c708 296 }
afa0c708 297 }
eb73853b 298 }
299 if (newsdigit) {
300 new((*sdigits)[nSdigits]) AliEMCALDigit(*curSDigit);
301 nSdigits++ ;
302 }
303 delete curSDigit ;
304 } // loop over all hits (hit = deposited energy/entering particle)
62741b41 305 sdigits->Sort() ;
306
307 nSdigits = sdigits->GetEntriesFast() ;
308 fSDigitsInRun += nSdigits ;
ab6a174f 309
62741b41 310 for (i = 0 ; i < sdigits->GetEntriesFast() ; i++) {
311 AliEMCALDigit * sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(i)) ;
312 sdigit->SetIndexInList(i) ;
313 }
314
88cb7938 315 // Now write SDigits
eb73853b 316
62741b41 317 Int_t bufferSize = 32000 ;
5dee926e 318 TBranch * sdigitsBranch = treeS->GetBranch("EMCAL");
319 if (sdigitsBranch)
320 sdigitsBranch->SetAddress(&sdigits);
321 else
322 treeS->Branch("EMCAL",&sdigits,bufferSize);
eb73853b 323
5dee926e 324 treeS->Fill();
325
326 emcalLoader->WriteSDigits("OVERWRITE");
62741b41 327
328 //NEXT - SDigitizer
5dee926e 329 emcalLoader->WriteSDigitizer("OVERWRITE"); // why in event cycle ?
62741b41 330
331 if(strstr(option,"deb"))
332 PrintSDigits(option) ;
1963b290 333 }
eb73853b 334
88cb7938 335 Unload();
336
5dee926e 337 emcalLoader->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
88cb7938 338
61e0abb5 339 if(strstr(option,"tim")){
9859bfc0 340 gBenchmark->Stop("EMCALSDigitizer");
1963b290 341 printf("\n Exec: took %f seconds for SDigitizing %f seconds per event\n",
eb73853b 342 gBenchmark->GetCpuTime("EMCALSDigitizer"), gBenchmark->GetCpuTime("EMCALSDigitizer")/nEvents ) ;
62741b41 343 }
61e0abb5 344}
05a92d59 345
bda7cf56 346//__________________________________________________________________
bda7cf56 347Int_t AliEMCALSDigitizer::Digitize(Float_t energy)const {
348 // Digitize the energy
ab6a174f 349 //
350 //JLK 26-Jun-2008 EXPLANATION LONG OVERDUE:
351 //
352 //We have to digitize the SDigit energy so that it can be stored in
353 //AliEMCALDigit, which has only an ADC amplitude member and
354 //(rightly) no energy member. This method converts the energy to an
355 //integer which can be re-converted back to an energy with the
356 //Calibrate(energy) method when it is time to create Digits from SDigits
357 //
358 Double_t aSignal = fA + energy*fB;
359 if (TMath::Abs(aSignal)>2147483647.0) {
360 //PH 2147483647 is the max. integer
361 //PH This apparently is a problem which needs investigation
362 AliWarning(Form("Too big or too small energy %f",aSignal));
363 aSignal = TMath::Sign((Double_t)2147483647,aSignal);
bda7cf56 364 }
ab6a174f 365 return (Int_t ) aSignal;
366}
bda7cf56 367
368//__________________________________________________________________
ab6a174f 369Float_t AliEMCALSDigitizer::Calibrate(Int_t amp)const {
370 //
371 // Convert the amplitude back to energy in GeV
372 //
373 //JLK 26-Jun-2008 EXPLANATION LONG OVERDUE:
374 //
375 //We have to digitize the SDigit energy with the method Digitize()
376 //so that it can be stored in AliEMCALDigit, which has only an ADC
377 //amplitude member and (rightly) no energy member. This method is
378 //just the reverse of Digitize(): it converts the stored amplitude
379 //back to an energy value in GeV so that the SDigit energies can be
380 //summed before adding noise and creating digits out of them
381 //
382 return (Float_t)(amp - fA)/fB;
383
384}
385
106fc2fa 386
ab6a174f 387//__________________________________________________________________
1963b290 388void AliEMCALSDigitizer::Print1(Option_t * option)
389{
390 Print();
391 PrintSDigits(option);
392}
393
61e0abb5 394//__________________________________________________________________
5dee926e 395void AliEMCALSDigitizer::Print(Option_t *option) const
05a92d59 396{
397 // Prints parameters of SDigitizer
e52475ed 398 printf("Print: \n------------------- %s ------------- option %s\n", GetName() , option) ;
1963b290 399 printf(" fInit %i\n", int(fInit));
400 printf(" fFirstEvent %i\n", fFirstEvent);
401 printf(" fLastEvent %i\n", fLastEvent);
88cb7938 402 printf(" Writing SDigits to branch with title %s\n", fEventFolderName.Data()) ;
1963b290 403 printf(" with digitization parameters A = %f\n", fA) ;
404 printf(" B = %f\n", fB) ;
405 printf(" Threshold for EC Primary assignment = %f\n", fECPrimThreshold) ;
406 printf(" Sampling = %f\n", fSampling);
88cb7938 407 printf("---------------------------------------------------\n") ;
61e0abb5 408}
05a92d59 409
61e0abb5 410//__________________________________________________________________
05a92d59 411Bool_t AliEMCALSDigitizer::operator==( AliEMCALSDigitizer const &sd )const
412{
413 // Equal operator.
414 // SDititizers are equal if their pedestal, slope and threshold are equal
89e103bd 415 if( (fA==sd.fA)&&(fB==sd.fB)&&
fdebddeb 416 (fECPrimThreshold==sd.fECPrimThreshold))
61e0abb5 417 return kTRUE ;
418 else
419 return kFALSE ;
420}
173558f2 421
61e0abb5 422//__________________________________________________________________
d64c959b 423void AliEMCALSDigitizer::PrintSDigits(Option_t * option)
424{
61e0abb5 425 //Prints list of digits produced at the current pass of AliEMCALDigitizer
7ea6391b 426
33c3c91a 427 AliEMCALLoader *rl = dynamic_cast<AliEMCALLoader*>(AliRunLoader::Instance()->GetDetectorLoader("EMCAL"));
5dee926e 428 const TClonesArray * sdigits = rl->SDigits() ;
61e0abb5 429
fdebddeb 430 printf("\n") ;
5dee926e 431 printf("event %i", rl->GetRunLoader()->GetEventNumber());
1963b290 432 printf(" Number of entries in SDigits list %i", sdigits->GetEntriesFast());
814ad4bf 433 if(strstr(option,"all")||strstr(option,"EMC")){
9859bfc0 434
61e0abb5 435 //loop over digits
436 AliEMCALDigit * digit;
fdebddeb 437 printf("\n Id Amplitude Time Index Nprim: Primaries list \n") ;
1963b290 438 Int_t index, isum=0;
11f9c5ff 439 char * tempo = new char[8192];
814ad4bf 440 for (index = 0 ; index < sdigits->GetEntries() ; index++) {
05a92d59 441 digit = dynamic_cast<AliEMCALDigit *>( sdigits->At(index) ) ;
11f9c5ff 442 sprintf(tempo, "\n%6d %8d %6.5e %4d %2d :",
443 digit->GetId(), digit->GetAmp(), digit->GetTime(), digit->GetIndexInList(), digit->GetNprimary()) ;
d804d556 444 printf("%s",tempo);
1963b290 445 isum += digit->GetAmp();
61e0abb5 446
447 Int_t iprimary;
9859bfc0 448 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++) {
d804d556 449 sprintf(tempo, "%d ",digit->GetPrimary(iprimary+1) ) ;
450 printf("%s",tempo);
9859bfc0 451 }
61e0abb5 452 }
11f9c5ff 453 delete tempo ;
ab6a174f 454 printf("\n** Sum %i : %10.3f GeV/c **\n ", isum, Calibrate(isum));
1963b290 455 } else printf("\n");
61e0abb5 456}
05a92d59 457
05a92d59 458//____________________________________________________________________________
88cb7938 459void AliEMCALSDigitizer::Unload() const
05a92d59 460{
d64c959b 461 // Unload Hits and SDigits from the folder
33c3c91a 462 AliEMCALLoader *rl = dynamic_cast<AliEMCALLoader*>(AliRunLoader::Instance()->GetDetectorLoader("EMCAL"));
5dee926e 463 rl->UnloadHits() ;
464 rl->UnloadSDigits() ;
05a92d59 465}
1963b290 466
fe93d365 467//____________________________________________________________________________
1963b290 468void AliEMCALSDigitizer::Browse(TBrowser* b)
469{
1963b290 470 TTask::Browse(b);
471}