messages moved to debug stream
[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 ---
bda7cf56 52#include <TBenchmark.h>
53#include <TH1.h>
54#include <TBrowser.h>
55#include <Riostream.h>
56#include <TMath.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//____________________________________________________________________________
18a21c7c 75AliEMCALSDigitizer::AliEMCALSDigitizer()
76 : TTask("",""),
77 fA(0.),fB(0.),fECPrimThreshold(0.),
78 fDefaultInit(kTRUE),
79 fEventFolderName(0),
80 fInit(0),
81 fSDigitsInRun(0),
82 fFirstEvent(0),
83 fLastEvent(0),
84 fSampling(0.),
85 fControlHists(0),
86 fHists(0)
61e0abb5 87{
88 // ctor
0a4cb131 89 InitParameters();
61e0abb5 90}
91
92//____________________________________________________________________________
e191bb57 93AliEMCALSDigitizer::AliEMCALSDigitizer(const char * alirunFileName,
18a21c7c 94 const char * eventFolderName)
95 : TTask("EMCAL"+AliConfig::Instance()->GetSDigitizerTaskName(), alirunFileName),
96 fA(0.),fB(0.),fECPrimThreshold(0.),
97 fDefaultInit(kFALSE),
98 fEventFolderName(eventFolderName),
99 fInit(0),
100 fSDigitsInRun(0),
101 fFirstEvent(0),
102 fLastEvent(0),
103 fSampling(0.),
104 fControlHists(1),
105 fHists(0)
61e0abb5 106{
107 // ctor
814ad4bf 108 Init();
62741b41 109 InitParameters() ;
e52475ed 110 if(fControlHists) BookControlHists(1);
61e0abb5 111}
112
88cb7938 113
61e0abb5 114//____________________________________________________________________________
18a21c7c 115AliEMCALSDigitizer::AliEMCALSDigitizer(const AliEMCALSDigitizer & sd)
116 : TTask(sd.GetName(),sd.GetTitle()),
117 fA(sd.fA),
118 fB(sd.fB),
119 fECPrimThreshold(sd.fECPrimThreshold),
120 fDefaultInit(sd.fDefaultInit),
121 fEventFolderName(sd.fEventFolderName),
122 fInit(sd.fInit),
123 fSDigitsInRun(sd.fSDigitsInRun),
124 fFirstEvent(sd.fFirstEvent),
125 fLastEvent(sd.fLastEvent),
126 fSampling(sd.fSampling),
127 fControlHists(sd.fControlHists),
128 fHists(sd.fHists)
129{
88cb7938 130 //cpy ctor
61e0abb5 131}
36657114 132
88cb7938 133
61e0abb5 134//____________________________________________________________________________
797e311f 135AliEMCALSDigitizer::~AliEMCALSDigitizer() {
14ce0a6e 136 //dtor
5dee926e 137 AliLoader *emcalLoader=0;
138 if ((emcalLoader = AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL")))
139 emcalLoader->CleanSDigitizer();
797e311f 140}
141
142//____________________________________________________________________________
61e0abb5 143void AliEMCALSDigitizer::Init(){
814ad4bf 144 // Initialization: open root-file, allocate arrays for hits and sdigits,
36657114 145 // attach task SDigitizer to the list of EMCAL tasks
814ad4bf 146 //
147 // Initialization can not be done in the default constructor
148 //============================================================= YS
149 // The initialisation is now done by the getter
61e0abb5 150
dc7da436 151 fInit = kTRUE;
afa0c708 152
5dee926e 153 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
154
155 if ( emcalLoader == 0 ) {
156 Fatal("Init", "Could not obtain the AliEMCALLoader");
814ad4bf 157 return ;
158 }
159
5dee926e 160 emcalLoader->PostSDigitizer(this);
161 emcalLoader->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
88cb7938 162
839828a6 163}
164
165//____________________________________________________________________________
afa0c708 166void AliEMCALSDigitizer::InitParameters()
167{
14ce0a6e 168 //initialize parameters for sdigitization
169
5dee926e 170 const AliEMCALGeometry * geom = AliEMCALGeometry::GetInstance();
62741b41 171 if (geom->GetSampling() == 0.) {
5082063d 172 Fatal("InitParameters", "Sampling factor not set !") ;
62741b41 173 }
1963b290 174 // Initializes parameters
175 fA = 0;
176 fB = 1.e+7;
177 fSampling = geom->GetSampling();
178
179 // threshold for deposit energy of hit
180 fECPrimThreshold = 0.; // 24-nov-04 - was 1.e-6;
e52475ed 181 Print("");
839828a6 182}
183
61e0abb5 184//____________________________________________________________________________
afa0c708 185void AliEMCALSDigitizer::Exec(Option_t *option)
186{
fdebddeb 187 // Collects all hit of the same tower into digits
1963b290 188 TString o(option); o.ToUpper();
814ad4bf 189 if (strstr(option, "print") ) {
d64c959b 190 Print() ;
814ad4bf 191 return ;
192 }
61e0abb5 193
814ad4bf 194 if(strstr(option,"tim"))
195 gBenchmark->Start("EMCALSDigitizer");
97f3344e 196
5dee926e 197 AliRunLoader *rl = AliRunLoader::GetRunLoader();
198 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
13e1db42 199
88cb7938 200 //switch off reloading of this task while getting event
201 if (!fInit) { // to prevent overwrite existing file
13e1db42 202 AliError( Form("Give a version name different from %s", fEventFolderName.Data()) ) ;
88cb7938 203 return ;
fdebddeb 204 }
205
5082063d 206 if (fLastEvent == -1)
5dee926e 207 fLastEvent = rl->GetNumberOfEvents() - 1 ;
1963b290 208 else {
5dee926e 209 fLastEvent = TMath::Min(fLastEvent, rl->GetNumberOfEvents()-1);
1963b290 210 }
5082063d 211 Int_t nEvents = fLastEvent - fFirstEvent + 1;
212
1963b290 213 Int_t ievent;
214 Float_t energy=0.; // de * fSampling - 23-nov-04
5dee926e 215 rl->LoadKinematics();
216 rl->LoadHits("EMCAL");
217
cf24bdc4 218 for (ievent = fFirstEvent; ievent <= fLastEvent; ievent++) {
5dee926e 219 rl->GetEvent(ievent);
220 TTree * treeS = emcalLoader->TreeS();
221 if ( !treeS ) {
222 emcalLoader->MakeSDigitsContainer();
223 treeS = emcalLoader->TreeS();
224 }
225 TClonesArray * hits = emcalLoader->Hits() ;
226 TClonesArray * sdigits = emcalLoader->SDigits() ;
814ad4bf 227 sdigits->Clear();
62741b41 228
eb73853b 229 Int_t nSdigits = 0 ;
230 Int_t i;
231 AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance();
232 for ( i = 0 ; i < hits->GetEntries() ; i++ ) {
233 AliEMCALHit * hit = dynamic_cast<AliEMCALHit*>(hits->At(i)) ;
234 AliEMCALDigit * curSDigit = 0 ;
235 AliEMCALDigit * sdigit = 0 ;
236 Bool_t newsdigit = kTRUE;
237
238 // hit->GetId() - Absolute Id number EMCAL segment
239 if(geom->CheckAbsCellId(hit->GetId())) { // was IsInECA(hit->GetId())
240 energy = hit->GetEnergy() * fSampling; // 23-nov-04
241 if(energy > fECPrimThreshold )
242 // Assign primary number only if deposited energy is significant
243 curSDigit = new AliEMCALDigit( hit->GetPrimary(),
244 hit->GetIparent(), hit->GetId(),
245 Digitize(energy), hit->GetTime() ) ;
62741b41 246 else
247 curSDigit = new AliEMCALDigit( -1 ,
248 -1 ,
249 hit->GetId(),
1963b290 250 Digitize(energy), hit->GetTime() ) ;
eb73853b 251 } else {
252 Warning("Exec"," abs id %i is bad \n", hit->GetId());
253 newsdigit = kFALSE;
254 curSDigit = 0;
255 }
256
257 if(curSDigit != 0){
258 for(Int_t check= 0; check < nSdigits ; check++) {
259 sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(check)) ;
260
261 if( sdigit->GetId() == curSDigit->GetId()) { // Are we in the same ECAL tower ?
262 *sdigit = *sdigit + *curSDigit;
263 newsdigit = kFALSE;
afa0c708 264 }
afa0c708 265 }
eb73853b 266 }
267 if (newsdigit) {
268 new((*sdigits)[nSdigits]) AliEMCALDigit(*curSDigit);
269 nSdigits++ ;
270 }
271 delete curSDigit ;
272 } // loop over all hits (hit = deposited energy/entering particle)
62741b41 273 sdigits->Sort() ;
274
275 nSdigits = sdigits->GetEntriesFast() ;
276 fSDigitsInRun += nSdigits ;
eb73853b 277
1963b290 278 Double_t e=0.,esum=0.;
315d1c64 279 AliEMCALHistoUtilities::FillH1(fHists, 0, double(sdigits->GetEntriesFast()));
62741b41 280 for (i = 0 ; i < sdigits->GetEntriesFast() ; i++) {
281 AliEMCALDigit * sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(i)) ;
282 sdigit->SetIndexInList(i) ;
eb73853b 283
315d1c64 284 AliEMCALHistoUtilities::FillH1(fHists, 2, double(sdigit->GetAmp()));
1963b290 285 e = double(Calibrate(sdigit->GetAmp()));
286 esum += e;
315d1c64 287 AliEMCALHistoUtilities::FillH1(fHists, 3, e);
288 AliEMCALHistoUtilities::FillH1(fHists, 4, double(sdigit->GetId()));
62741b41 289 }
315d1c64 290 if(esum>0.) AliEMCALHistoUtilities::FillH1(fHists, 1, esum);
62741b41 291
88cb7938 292 // Now write SDigits
eb73853b 293
62741b41 294 Int_t bufferSize = 32000 ;
5dee926e 295 TBranch * sdigitsBranch = treeS->GetBranch("EMCAL");
296 if (sdigitsBranch)
297 sdigitsBranch->SetAddress(&sdigits);
298 else
299 treeS->Branch("EMCAL",&sdigits,bufferSize);
eb73853b 300
5dee926e 301 treeS->Fill();
302
303 emcalLoader->WriteSDigits("OVERWRITE");
62741b41 304
305 //NEXT - SDigitizer
5dee926e 306 emcalLoader->WriteSDigitizer("OVERWRITE"); // why in event cycle ?
62741b41 307
308 if(strstr(option,"deb"))
309 PrintSDigits(option) ;
1963b290 310 }
eb73853b 311
88cb7938 312 Unload();
313
5dee926e 314 emcalLoader->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
88cb7938 315
61e0abb5 316 if(strstr(option,"tim")){
9859bfc0 317 gBenchmark->Stop("EMCALSDigitizer");
1963b290 318 printf("\n Exec: took %f seconds for SDigitizing %f seconds per event\n",
eb73853b 319 gBenchmark->GetCpuTime("EMCALSDigitizer"), gBenchmark->GetCpuTime("EMCALSDigitizer")/nEvents ) ;
62741b41 320 }
61e0abb5 321}
05a92d59 322
bda7cf56 323//__________________________________________________________________
324
325Int_t AliEMCALSDigitizer::Digitize(Float_t energy)const {
326 // Digitize the energy
327 Double_t aSignal = fA + energy*fB;
328 if (TMath::Abs(aSignal)>2147483647.0) {
329 //PH 2147483647 is the max. integer
330 //PH This apparently is a problem which needs investigation
331 AliWarning(Form("Too big or too small energy %f",aSignal));
332 aSignal = TMath::Sign((Double_t)2147483647,aSignal);
333 }
334 return (Int_t ) aSignal;
335 }
336
337
338//__________________________________________________________________
106fc2fa 339
1963b290 340void AliEMCALSDigitizer::Print1(Option_t * option)
341{
342 Print();
343 PrintSDigits(option);
344}
345
61e0abb5 346//__________________________________________________________________
5dee926e 347void AliEMCALSDigitizer::Print(Option_t *option) const
05a92d59 348{
349 // Prints parameters of SDigitizer
e52475ed 350 printf("Print: \n------------------- %s ------------- option %s\n", GetName() , option) ;
1963b290 351 printf(" fInit %i\n", int(fInit));
352 printf(" fFirstEvent %i\n", fFirstEvent);
353 printf(" fLastEvent %i\n", fLastEvent);
88cb7938 354 printf(" Writing SDigits to branch with title %s\n", fEventFolderName.Data()) ;
1963b290 355 printf(" with digitization parameters A = %f\n", fA) ;
356 printf(" B = %f\n", fB) ;
357 printf(" Threshold for EC Primary assignment = %f\n", fECPrimThreshold) ;
358 printf(" Sampling = %f\n", fSampling);
88cb7938 359 printf("---------------------------------------------------\n") ;
61e0abb5 360}
05a92d59 361
61e0abb5 362//__________________________________________________________________
05a92d59 363Bool_t AliEMCALSDigitizer::operator==( AliEMCALSDigitizer const &sd )const
364{
365 // Equal operator.
366 // SDititizers are equal if their pedestal, slope and threshold are equal
89e103bd 367 if( (fA==sd.fA)&&(fB==sd.fB)&&
fdebddeb 368 (fECPrimThreshold==sd.fECPrimThreshold))
61e0abb5 369 return kTRUE ;
370 else
371 return kFALSE ;
372}
173558f2 373
61e0abb5 374//__________________________________________________________________
d64c959b 375void AliEMCALSDigitizer::PrintSDigits(Option_t * option)
376{
61e0abb5 377 //Prints list of digits produced at the current pass of AliEMCALDigitizer
378
d64c959b 379
5dee926e 380 // AliEMCALGetter * gime = AliEMCALGetter::Instance() ;
381 AliEMCALLoader *rl = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
382 const TClonesArray * sdigits = rl->SDigits() ;
61e0abb5 383
fdebddeb 384 printf("\n") ;
5dee926e 385 printf("event %i", rl->GetRunLoader()->GetEventNumber());
1963b290 386 printf(" Number of entries in SDigits list %i", sdigits->GetEntriesFast());
814ad4bf 387 if(strstr(option,"all")||strstr(option,"EMC")){
9859bfc0 388
61e0abb5 389 //loop over digits
390 AliEMCALDigit * digit;
fdebddeb 391 printf("\n Id Amplitude Time Index Nprim: Primaries list \n") ;
1963b290 392 Int_t index, isum=0;
11f9c5ff 393 char * tempo = new char[8192];
814ad4bf 394 for (index = 0 ; index < sdigits->GetEntries() ; index++) {
05a92d59 395 digit = dynamic_cast<AliEMCALDigit *>( sdigits->At(index) ) ;
11f9c5ff 396 sprintf(tempo, "\n%6d %8d %6.5e %4d %2d :",
397 digit->GetId(), digit->GetAmp(), digit->GetTime(), digit->GetIndexInList(), digit->GetNprimary()) ;
1963b290 398 printf(tempo);
399 isum += digit->GetAmp();
61e0abb5 400
401 Int_t iprimary;
9859bfc0 402 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++) {
11f9c5ff 403 sprintf(tempo, "%d ",digit->GetPrimary(iprimary+1) ) ;
fdebddeb 404 printf(tempo);
9859bfc0 405 }
61e0abb5 406 }
11f9c5ff 407 delete tempo ;
1963b290 408 printf("\n** Sum %i : %10.3f GeV/c **\n ", isum, double(isum)*1.e-6);
409 } else printf("\n");
61e0abb5 410}
05a92d59 411
05a92d59 412//____________________________________________________________________________
88cb7938 413void AliEMCALSDigitizer::Unload() const
05a92d59 414{
d64c959b 415 // Unload Hits and SDigits from the folder
5dee926e 416 AliEMCALLoader *rl = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
417 rl->UnloadHits() ;
418 rl->UnloadSDigits() ;
05a92d59 419}
1963b290 420
421void AliEMCALSDigitizer::Browse(TBrowser* b)
422{
423 if(fHists) b->Add(fHists);
424 TTask::Browse(b);
425}
426
eb0b1051 427TList *AliEMCALSDigitizer::BookControlHists(int var)
14ce0a6e 428{
429 //book histograms for monitoring sdigitization
430 // 22-nov-04
1963b290 431 gROOT->cd();
5dee926e 432 const AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance() ;
1963b290 433 if(var>=1){
e52475ed 434 printf(" AliEMCALSDigitizer::BookControlHists() in action \n");
1963b290 435 new TH1F("HSDigiN", "#EMCAL sdigits ", 1001, -0.5, 1000.5);
436 new TH1F("HSDigiSumEnergy","Sum.EMCAL energy", 1000, 0.0, 100.);
437 new TH1F("HSDigiAmp", "EMCAL sdigits amplitude", 1000, 0., 2.e+9);
438 new TH1F("HSDigiEnergy","EMCAL cell energy", 1000, 0.0, 100.);
439 new TH1F("HSDigiAbsId","EMCAL absID for sdigits",
440 geom->GetNCells(), 0.5, Double_t(geom->GetNCells())+0.5);
441 }
e52475ed 442
315d1c64 443 fHists = AliEMCALHistoUtilities::MoveHistsToList("EmcalSDigiControlHists", kFALSE);
5dee926e 444 fHists = 0;
e52475ed 445
1963b290 446 return fHists;
447}
448
449void AliEMCALSDigitizer::SaveHists(const char* name, Bool_t kSingleKey, const char* opt)
450{
315d1c64 451 AliEMCALHistoUtilities::SaveListOfHists(fHists, name, kSingleKey, opt);
1963b290 452}