Made more robust
[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"
05a92d59 53
61e0abb5 54// --- Standard library ---
d64c959b 55#include "stdlib.h"
61e0abb5 56
57// --- AliRoot header files ---
61e0abb5 58#include "AliEMCALDigit.h"
814ad4bf 59#include "AliEMCALGetter.h"
05a92d59 60#include "AliEMCALHit.h"
61#include "AliEMCALSDigitizer.h"
88cb7938 62#include "AliEMCALGeometry.h"
61e0abb5 63
64ClassImp(AliEMCALSDigitizer)
61e0abb5 65
66//____________________________________________________________________________
88cb7938 67 AliEMCALSDigitizer::AliEMCALSDigitizer():TTask("","")
61e0abb5 68{
69 // ctor
5082063d 70 fFirstEvent = fLastEvent = 0 ;
92f521a9 71 fDefaultInit = kTRUE ;
61e0abb5 72}
73
74//____________________________________________________________________________
88cb7938 75AliEMCALSDigitizer::AliEMCALSDigitizer(const char * alirunFileName, const char * eventFolderName):
76 TTask("EMCAL"+AliConfig::fgkSDigitizerTaskName, alirunFileName),
77 fEventFolderName(eventFolderName)
61e0abb5 78{
79 // ctor
5082063d 80 fFirstEvent = fLastEvent = 0 ; // runs one event by defaut
814ad4bf 81 Init();
62741b41 82 InitParameters() ;
92f521a9 83 fDefaultInit = kFALSE ;
61e0abb5 84}
85
88cb7938 86
61e0abb5 87//____________________________________________________________________________
9e5d2067 88AliEMCALSDigitizer::AliEMCALSDigitizer(const AliEMCALSDigitizer & sd) : TTask(sd) {
88cb7938 89 //cpy ctor
90
5082063d 91 fFirstEvent = sd.fFirstEvent ;
92 fLastEvent = sd.fLastEvent ;
88cb7938 93 fA = sd.fA ;
94 fB = sd.fB ;
88cb7938 95 fECPrimThreshold = sd.fECPrimThreshold ;
88cb7938 96 fSDigitsInRun = sd.fSDigitsInRun ;
97 SetName(sd.GetName()) ;
98 SetTitle(sd.GetTitle()) ;
99 fEventFolderName = sd.fEventFolderName;
61e0abb5 100}
36657114 101
88cb7938 102
61e0abb5 103//____________________________________________________________________________
797e311f 104AliEMCALSDigitizer::~AliEMCALSDigitizer() {
105 // dtor
106 AliEMCALGetter * gime =
107 AliEMCALGetter::Instance(GetTitle(), fEventFolderName.Data());
108 gime->EmcalLoader()->CleanSDigitizer();
109}
110
111//____________________________________________________________________________
61e0abb5 112void AliEMCALSDigitizer::Init(){
814ad4bf 113 // Initialization: open root-file, allocate arrays for hits and sdigits,
36657114 114 // attach task SDigitizer to the list of EMCAL tasks
814ad4bf 115 //
116 // Initialization can not be done in the default constructor
117 //============================================================= YS
118 // The initialisation is now done by the getter
61e0abb5 119
88cb7938 120 fInit = kTRUE ;
afa0c708 121
88cb7938 122 AliEMCALGetter * gime = AliEMCALGetter::Instance(GetTitle(), fEventFolderName.Data());
814ad4bf 123 if ( gime == 0 ) {
88cb7938 124 Fatal("Init", "Could not obtain the Getter objectfor file %s and event %s !", GetTitle(), fEventFolderName.Data()) ;
814ad4bf 125 return ;
126 }
127
88cb7938 128 TString opt("SDigits") ;
129 if(gime->VersionExists(opt) ) {
130 Error( "Init", "Give a version name different from %s", fEventFolderName.Data() ) ;
131 fInit = kFALSE ;
05a92d59 132 }
88cb7938 133
134 gime->PostSDigitizer(this);
135 gime->EmcalLoader()->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
136
839828a6 137}
138
139//____________________________________________________________________________
afa0c708 140void AliEMCALSDigitizer::InitParameters()
141{
d64c959b 142 // Initializes parameters
05a92d59 143 fA = 0;
144 fB = 10000000.;
62741b41 145
88cb7938 146 AliEMCALGetter * gime = AliEMCALGetter::Instance() ;
62741b41 147 const AliEMCALGeometry * geom = gime->EMCALGeometry() ;
148 if (geom->GetSampling() == 0.) {
5082063d 149 Fatal("InitParameters", "Sampling factor not set !") ;
62741b41 150 }
f540341d 151// else
152// Info("InitParameters", "Sampling factor set to %f", geom->GetSampling()) ;
62741b41 153
154 // this threshold corresponds approximately to 100 MeV
7d74eaca 155 fECPrimThreshold = 100E-3;
839828a6 156}
157
61e0abb5 158//____________________________________________________________________________
afa0c708 159void AliEMCALSDigitizer::Exec(Option_t *option)
160{
fdebddeb 161 // Collects all hit of the same tower into digits
814ad4bf 162 if (strstr(option, "print") ) {
d64c959b 163 Print() ;
814ad4bf 164 return ;
165 }
61e0abb5 166
814ad4bf 167 if(strstr(option,"tim"))
168 gBenchmark->Start("EMCALSDigitizer");
97f3344e 169
5082063d 170 AliEMCALGetter * gime = AliEMCALGetter::Instance(GetTitle()) ;
88cb7938 171
172 //switch off reloading of this task while getting event
173 if (!fInit) { // to prevent overwrite existing file
174 Error( "Exec", "Give a version name different from %s", fEventFolderName.Data() ) ;
175 return ;
fdebddeb 176 }
177
5082063d 178 if (fLastEvent == -1)
179 fLastEvent = gime->MaxEvent() - 1 ;
180 else
181 fLastEvent = TMath::Min(fFirstEvent, gime->MaxEvent()); // only ine event at the time
182 Int_t nEvents = fLastEvent - fFirstEvent + 1;
183
62741b41 184 Int_t ievent ;
5082063d 185 for (ievent = fFirstEvent; ievent <= fLastEvent; ievent++) {
62741b41 186 gime->Event(ievent,"H") ;
88cb7938 187 TTree * treeS = gime->TreeS();
188 TClonesArray * hits = gime->Hits() ;
189 TClonesArray * sdigits = gime->SDigits() ;
814ad4bf 190 sdigits->Clear();
61e0abb5 191 Int_t nSdigits = 0 ;
05a92d59 192 //Now make SDigits from hits, for EMCAL it is the same, so just copy
88cb7938 193 Int_t nPrim = static_cast<Int_t>((gime->TreeH())->GetEntries()) ;
05a92d59 194 // Attention nPrim is the number of primaries tracked by Geant
195 // and this number could be different to the number of Primaries in TreeK;
196 Int_t iprim ;
62741b41 197 for ( iprim = 0 ; iprim < nPrim ; iprim++ ) {
198 //=========== Get the EMCAL branch from Hits Tree for the Primary iprim
199 gime->Track(iprim) ;
200 Int_t i;
201 for ( i = 0 ; i < hits->GetEntries() ; i++ ) {
202 AliEMCALHit * hit = dynamic_cast<AliEMCALHit*>(hits->At(i)) ;
203 AliEMCALDigit * curSDigit = 0 ;
204 AliEMCALDigit * sdigit = 0 ;
205 Bool_t newsdigit = kTRUE;
206
207 // Assign primary number only if deposited energy is significant
88cb7938 208 AliEMCALGeometry * geom = gime->EMCALGeometry() ;
fdebddeb 209 if( geom->IsInECA(hit->GetId()) ){
62741b41 210 if( hit->GetEnergy() > fECPrimThreshold )
211 curSDigit = new AliEMCALDigit( hit->GetPrimary(),
212 hit->GetIparent(), hit->GetId(),
213 Digitize(hit->GetEnergy()), hit->GetTime() ) ;
214 else
215 curSDigit = new AliEMCALDigit( -1 ,
216 -1 ,
217 hit->GetId(),
218 Digitize(hit->GetEnergy()), hit->GetTime() ) ;
fdebddeb 219 }
220 else{
221 newsdigit = kFALSE;
222 }
223
62741b41 224 Int_t check = 0 ;
fdebddeb 225
62741b41 226 for(check= 0; check < nSdigits ; check++) {
227 sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(check)) ;
fdebddeb 228 if(curSDigit != 0){
229 if( sdigit->GetId() == curSDigit->GetId()) { // Are we in the same ECAL tower ?
62741b41 230 *sdigit = *sdigit + *curSDigit;
231 newsdigit = kFALSE;
fdebddeb 232 }
afa0c708 233 }
afa0c708 234 }
fdebddeb 235 if (newsdigit) {
62741b41 236 new((*sdigits)[nSdigits]) AliEMCALDigit(*curSDigit);
237 nSdigits++ ;
e1f60236 238 }
62741b41 239 delete curSDigit ;
fdebddeb 240 } // loop over all hits (hit = deposited energy/entering particle)
62741b41 241 } // loop over iprim
62741b41 242 sdigits->Sort() ;
243
244 nSdigits = sdigits->GetEntriesFast() ;
245 fSDigitsInRun += nSdigits ;
246 sdigits->Expand(nSdigits) ;
247
d64c959b 248 Int_t nPrimarymax = -1 ;
62741b41 249 Int_t i ;
250 for (i = 0 ; i < sdigits->GetEntriesFast() ; i++) {
251 AliEMCALDigit * sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(i)) ;
252 sdigit->SetIndexInList(i) ;
253 }
254
255 for (i = 0 ; i < sdigits->GetEntriesFast() ; i++) {
d64c959b 256 if (((dynamic_cast<AliEMCALDigit *>(sdigits->At(i)))->GetNprimary()) > nPrimarymax)
257 nPrimarymax = ((dynamic_cast<AliEMCALDigit *>(sdigits->At(i)))->GetNprimary()) ;
62741b41 258 }
259
88cb7938 260 // Now write SDigits
62741b41 261 //First list of sdigits
88cb7938 262
62741b41 263 Int_t bufferSize = 32000 ;
88cb7938 264 TBranch * sdigitsBranch = treeS->Branch("EMCAL",&sdigits,bufferSize);
265
266 sdigitsBranch->Fill() ;
267
268 gime->WriteSDigits("OVERWRITE");
62741b41 269
270 //NEXT - SDigitizer
88cb7938 271
272 gime->WriteSDigitizer("OVERWRITE");
62741b41 273
274 if(strstr(option,"deb"))
275 PrintSDigits(option) ;
61e0abb5 276 }
88cb7938 277
278 Unload();
279
280 gime->EmcalLoader()->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
281
61e0abb5 282 if(strstr(option,"tim")){
9859bfc0 283 gBenchmark->Stop("EMCALSDigitizer");
fdebddeb 284 printf("Exec: took %f seconds for SDigitizing %f seconds per event",
5082063d 285 gBenchmark->GetCpuTime("EMCALSDigitizer"), gBenchmark->GetCpuTime("EMCALSDigitizer")/nEvents ) ;
62741b41 286 }
61e0abb5 287}
05a92d59 288
106fc2fa 289
61e0abb5 290//__________________________________________________________________
d64c959b 291void AliEMCALSDigitizer::Print()const
05a92d59 292{
293 // Prints parameters of SDigitizer
fdebddeb 294 printf("Print: \n------------------- %s -------------", GetName() ) ;
88cb7938 295 printf(" Writing SDigits to branch with title %s\n", fEventFolderName.Data()) ;
296 printf(" with digitization parameters A = %f\n", fA) ;
297 printf(" B = %f\n", fB) ;
88cb7938 298 printf(" Threshold for EC Primary assignment= %f\n", fECPrimThreshold) ;
88cb7938 299 printf("---------------------------------------------------\n") ;
05a92d59 300
61e0abb5 301}
05a92d59 302
61e0abb5 303//__________________________________________________________________
05a92d59 304Bool_t AliEMCALSDigitizer::operator==( AliEMCALSDigitizer const &sd )const
305{
306 // Equal operator.
307 // SDititizers are equal if their pedestal, slope and threshold are equal
89e103bd 308 if( (fA==sd.fA)&&(fB==sd.fB)&&
fdebddeb 309 (fECPrimThreshold==sd.fECPrimThreshold))
61e0abb5 310 return kTRUE ;
311 else
312 return kFALSE ;
313}
173558f2 314
61e0abb5 315//__________________________________________________________________
d64c959b 316void AliEMCALSDigitizer::PrintSDigits(Option_t * option)
317{
61e0abb5 318 //Prints list of digits produced at the current pass of AliEMCALDigitizer
319
d64c959b 320
88cb7938 321 AliEMCALGetter * gime = AliEMCALGetter::Instance() ;
322 const TClonesArray * sdigits = gime->SDigits() ;
61e0abb5 323
fdebddeb 324 printf("\n") ;
325 printf("event %i", gAlice->GetEvNumber()) ;
326 printf("\n Number of entries in SDigits list %i", sdigits->GetEntriesFast());
814ad4bf 327 if(strstr(option,"all")||strstr(option,"EMC")){
9859bfc0 328
61e0abb5 329 //loop over digits
330 AliEMCALDigit * digit;
fdebddeb 331 printf("\n Id Amplitude Time Index Nprim: Primaries list \n") ;
61e0abb5 332 Int_t index ;
11f9c5ff 333 char * tempo = new char[8192];
814ad4bf 334 for (index = 0 ; index < sdigits->GetEntries() ; index++) {
05a92d59 335 digit = dynamic_cast<AliEMCALDigit *>( sdigits->At(index) ) ;
11f9c5ff 336 sprintf(tempo, "\n%6d %8d %6.5e %4d %2d :",
337 digit->GetId(), digit->GetAmp(), digit->GetTime(), digit->GetIndexInList(), digit->GetNprimary()) ;
fdebddeb 338 printf(tempo);
61e0abb5 339
340 Int_t iprimary;
9859bfc0 341 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++) {
11f9c5ff 342 sprintf(tempo, "%d ",digit->GetPrimary(iprimary+1) ) ;
fdebddeb 343 printf(tempo);
9859bfc0 344 }
61e0abb5 345 }
11f9c5ff 346 delete tempo ;
61e0abb5 347 }
348}
05a92d59 349
05a92d59 350//____________________________________________________________________________
88cb7938 351void AliEMCALSDigitizer::Unload() const
05a92d59 352{
d64c959b 353 // Unload Hits and SDigits from the folder
88cb7938 354 AliEMCALGetter * gime = AliEMCALGetter::Instance() ;
355 AliEMCALLoader * loader = gime->EmcalLoader() ;
356 loader->UnloadHits() ;
357 loader->UnloadSDigits() ;
05a92d59 358}