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