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