Normalization for RAW and RECP histograms (Melinda)
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALDigitizer.cxx
CommitLineData
61e0abb5 1/**************************************************************************
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//_________________________________________________________________________
ffa6d63b 19//
61e0abb5 20//////////////////////////////////////////////////////////////////////////////
ab6a174f 21// Class performs digitization of Summable digits from simulated data
ffa6d63b 22//
61e0abb5 23// In addition it performs mixing of summable digits from different events.
24//
25// For each event two branches are created in TreeD:
26// "EMCAL" - list of digits
27// "AliEMCALDigitizer" - AliEMCALDigitizer with all parameters used in digitization
28//
29// Note, that one cset title for new digits branch, and repeat digitization with
30// another set of parameters.
31//
32// Examples of use:
33// root[0] AliEMCALDigitizer * d = new AliEMCALDigitizer() ;
34// root[1] d->ExecuteTask()
35// Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
36// //Digitizes SDigitis in all events found in file galice.root
37//
38// root[2] AliEMCALDigitizer * d1 = new AliEMCALDigitizer("galice1.root") ;
39// // Will read sdigits from galice1.root
40// root[3] d1->MixWith("galice2.root")
41// Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
42// // Reads another portion of sdigits from galice2.root
43// root[3] d1->MixWith("galice3.root")
44// // Reads another portion of sdigits from galice3.root
45// root[4] d->ExecuteTask("deb timing")
46// // Reads SDigits from files galice1.root, galice2.root ....
47// // mixes them and stores produced Digits in file galice1.root
48// // deb - prints number of produced digits
49// // deb all - prints list of produced digits
50// // timing - prints time used for digitization
ffa6d63b 51////////////////////////////////////////////////////////////////////////////////////
61e0abb5 52//
ffa6d63b 53//*-- Author: Sahal Yacoob (LBL)
814ad4bf 54// based on : AliEMCALDigitizer
05a92d59 55// Modif:
56// August 2002 Yves Schutz: clone PHOS as closely as possible and intoduction
57// of new IO (à la PHOS)
1963b290 58// November 2003 Aleksei Pavlinov : adopted for Shish-Kebab geometry
59//_________________________________________________________________________________
61e0abb5 60
61// --- ROOT system ---
e939a978 62#include <TROOT.h>
63#include <TTree.h>
64#include <TSystem.h>
65#include <TBenchmark.h>
e939a978 66#include <TBrowser.h>
67#include <TObjectTable.h>
68#include <TRandom.h>
21e7df44 69#include <cassert>
61e0abb5 70
71// --- AliRoot header files ---
b42d4197 72#include "AliLog.h"
e4dcc484 73#include "AliRun.h"
ea5f3389 74#include "AliRunDigitizer.h"
5dee926e 75#include "AliRunLoader.h"
53f1c378 76#include "AliCDBManager.h"
77#include "AliCDBEntry.h"
61e0abb5 78#include "AliEMCALDigit.h"
05a92d59 79#include "AliEMCAL.h"
5dee926e 80#include "AliEMCALLoader.h"
61e0abb5 81#include "AliEMCALDigitizer.h"
82#include "AliEMCALSDigitizer.h"
83#include "AliEMCALGeometry.h"
05a92d59 84#include "AliEMCALTick.h"
a435f763 85#include "AliEMCALCalibData.h"
33a52e55 86#include "AliEMCALSimParam.h"
05a92d59 87
61e0abb5 88ClassImp(AliEMCALDigitizer)
89
90
91//____________________________________________________________________________
18a21c7c 92AliEMCALDigitizer::AliEMCALDigitizer()
93 : AliDigitizer("",""),
94 fDefaultInit(kTRUE),
95 fDigitsInRun(0),
96 fInit(0),
97 fInput(0),
98 fInputFileNames(0x0),
99 fEventNames(0x0),
100 fDigitThreshold(0),
101 fMeanPhotonElectron(0),
33a52e55 102// fPedestal(0), //Not used, remove?
103// fSlope(0), //Not used, remove?
18a21c7c 104 fPinNoise(0),
105 fTimeResolution(0),
33a52e55 106// fTimeThreshold(0), //Not used, remove?
107// fTimeSignalLength(0), //Not used, remove?
18a21c7c 108 fADCchannelEC(0),
109 fADCpedestalEC(0),
110 fNADCEC(0),
111 fEventFolderName(""),
112 fFirstEvent(0),
113 fLastEvent(0),
7ea6391b 114 fCalibData(0x0)
61e0abb5 115{
116 // ctor
88cb7938 117 InitParameters() ;
ab6a174f 118 fManager = 0 ; // We work in the standalone mode
839828a6 119}
61e0abb5 120
839828a6 121//____________________________________________________________________________
18a21c7c 122AliEMCALDigitizer::AliEMCALDigitizer(TString alirunFileName, TString eventFolderName)
123 : AliDigitizer("EMCAL"+AliConfig::Instance()->GetDigitizerTaskName(), alirunFileName),
124 fDefaultInit(kFALSE),
125 fDigitsInRun(0),
126 fInit(0),
127 fInput(0),
128 fInputFileNames(0),
129 fEventNames(0),
130 fDigitThreshold(0),
131 fMeanPhotonElectron(0),
33a52e55 132// fPedestal(0),//Not used, remove?
133// fSlope(0), //Not used, remove?
18a21c7c 134 fPinNoise(0),
135 fTimeResolution(0),
33a52e55 136// fTimeThreshold(0), //Not used, remove?
137// fTimeSignalLength(0), //Not used, remove?
18a21c7c 138 fADCchannelEC(0),
139 fADCpedestalEC(0),
140 fNADCEC(0),
141 fEventFolderName(eventFolderName),
142 fFirstEvent(0),
143 fLastEvent(0),
7ea6391b 144 fCalibData(0x0)
839828a6 145{
05a92d59 146 // ctor
a9485c37 147 InitParameters() ;
88cb7938 148 Init() ;
ab6a174f 149 fManager = 0 ; // We work in the standalone mode
61e0abb5 150}
839828a6 151
61e0abb5 152//____________________________________________________________________________
18a21c7c 153AliEMCALDigitizer::AliEMCALDigitizer(const AliEMCALDigitizer & d)
154 : AliDigitizer(d.GetName(),d.GetTitle()),
155 fDefaultInit(d.fDefaultInit),
156 fDigitsInRun(d.fDigitsInRun),
157 fInit(d.fInit),
158 fInput(d.fInput),
159 fInputFileNames(d.fInputFileNames),
160 fEventNames(d.fEventNames),
161 fDigitThreshold(d.fDigitThreshold),
162 fMeanPhotonElectron(d.fMeanPhotonElectron),
33a52e55 163// fPedestal(d.fPedestal), //Not used, remove?
164// fSlope(d.fSlope), //Not used, remove?
18a21c7c 165 fPinNoise(d.fPinNoise),
166 fTimeResolution(d.fTimeResolution),
33a52e55 167// fTimeThreshold(d.fTimeThreshold), //Not used, remove?
168// fTimeSignalLength(d.fTimeSignalLength), //Not used, remove?
18a21c7c 169 fADCchannelEC(d.fADCchannelEC),
170 fADCpedestalEC(d.fADCpedestalEC),
171 fNADCEC(d.fNADCEC),
172 fEventFolderName(d.fEventFolderName),
173 fFirstEvent(d.fFirstEvent),
174 fLastEvent(d.fLastEvent),
7ea6391b 175 fCalibData(d.fCalibData)
88cb7938 176{
177 // copyy ctor
88cb7938 178 }
179
180//____________________________________________________________________________
18a21c7c 181AliEMCALDigitizer::AliEMCALDigitizer(AliRunDigitizer * rd)
182 : AliDigitizer(rd,"EMCAL"+AliConfig::Instance()->GetDigitizerTaskName()),
183 fDefaultInit(kFALSE),
184 fDigitsInRun(0),
185 fInit(0),
186 fInput(0),
187 fInputFileNames(0),
188 fEventNames(0),
189 fDigitThreshold(0.),
190 fMeanPhotonElectron(0),
33a52e55 191// fPedestal(0), //Not used, remove?
192// fSlope(0.), //Not used, remove?
18a21c7c 193 fPinNoise(0),
194 fTimeResolution(0.),
33a52e55 195// fTimeThreshold(0), //Not used, remove?
196// fTimeSignalLength(0), //Not used, remove?
18a21c7c 197 fADCchannelEC(0),
198 fADCpedestalEC(0),
199 fNADCEC(0),
200 fEventFolderName(0),
201 fFirstEvent(0),
202 fLastEvent(0),
7ea6391b 203 fCalibData(0x0)
61e0abb5 204{
45fa49ca 205 // ctor Init() is called by RunDigitizer
88cb7938 206 fManager = rd ;
e0e18b6d 207 fEventFolderName = fManager->GetInputFolderName(0) ;
88cb7938 208 SetTitle(dynamic_cast<AliStream*>(fManager->GetInputStream(0))->GetFileName(0));
05a92d59 209 InitParameters() ;
839828a6 210}
814ad4bf 211
839828a6 212//____________________________________________________________________________
213 AliEMCALDigitizer::~AliEMCALDigitizer()
214{
14ce0a6e 215 //dtor
33c3c91a 216 if (AliRunLoader::Instance()) {
5dee926e 217 AliLoader *emcalLoader=0;
33c3c91a 218 if ((emcalLoader = AliRunLoader::Instance()->GetDetectorLoader("EMCAL")))
5dee926e 219 emcalLoader->CleanDigitizer();
220 }
221 else
222 AliDebug(1," no runloader present");
f5f384f4 223 delete [] fInputFileNames ;
224 delete [] fEventNames ;
64942713 225
61e0abb5 226}
227
228//____________________________________________________________________________
09884213 229void AliEMCALDigitizer::Digitize(Int_t event)
05a92d59 230{
61e0abb5 231
232 // Makes the digitization of the collected summable digits
233 // for this it first creates the array of all EMCAL modules
ab6a174f 234 // filled with noise and after that adds contributions from
235 // SDigits. This design helps to avoid scanning over the
236 // list of digits to add contribution of any new SDigit.
237 //
238 // JLK 26-Jun-2008
239 // Note that SDigit energy info is stored as an amplitude, so we
240 // must call the Calibrate() method of the SDigitizer to convert it
241 // back to an energy in GeV before adding it to the Digit
242 //
1963b290 243 static int nEMC=0; //max number of digits possible
61e0abb5 244
33c3c91a 245 AliRunLoader *rl = AliRunLoader::Instance();
5dee926e 246 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
14ce0a6e 247 Int_t readEvent = event ;
1963b290 248 // fManager is data member from AliDigitizer
45fa49ca 249 if (fManager)
14ce0a6e 250 readEvent = dynamic_cast<AliStream*>(fManager->GetInputStream(0))->GetCurrentEventNumber() ;
cf24bdc4 251 AliDebug(1,Form("Adding event %d from input stream 0 %s %s",
14ce0a6e 252 readEvent, GetTitle(), fEventFolderName.Data())) ;
253 rl->GetEvent(readEvent);
5dee926e 254
255 TClonesArray * digits = emcalLoader->Digits() ;
ab6a174f 256 digits->Delete() ; //correct way to clear array when memory is
257 //allocated by objects stored in array
61e0abb5 258
e4dcc484 259 // Load Geometry
30bc5594 260 AliEMCALGeometry *geom = 0;
261 if (rl->GetAliRun()) {
262 AliEMCAL * emcal = (AliEMCAL*)rl->GetAliRun()->GetDetector("EMCAL");
263 geom = emcal->GetGeometry();
264 }
265 else
266 AliFatal("Could not get AliRun from runLoader");
e4dcc484 267
fe93d365 268 nEMC = geom->GetNCells();
269 AliDebug(1,Form("nEMC %i (number cells in EMCAL) | %s \n", nEMC, geom->GetName()));
270
88cb7938 271 Int_t absID ;
61e0abb5 272
88cb7938 273 digits->Expand(nEMC) ;
05a92d59 274
88cb7938 275 // get first the sdigitizer from the tasks list (must have same name as the digitizer)
5dee926e 276 if ( !emcalLoader->SDigitizer() )
277 emcalLoader->LoadSDigitizer();
278 AliEMCALSDigitizer * sDigitizer = dynamic_cast<AliEMCALSDigitizer*>(emcalLoader->SDigitizer());
88cb7938 279
280 if ( !sDigitizer )
281 Fatal("Digitize", "SDigitizer with name %s %s not found", fEventFolderName.Data(), GetTitle() ) ;
282
283 //take all the inputs to add together and load the SDigits
284 TObjArray * sdigArray = new TObjArray(fInput) ;
5dee926e 285 sdigArray->AddAt(emcalLoader->SDigits(), 0) ;
88cb7938 286 Int_t i ;
5dee926e 287
88cb7938 288 for(i = 1 ; i < fInput ; i++){
289 TString tempo(fEventNames[i]) ;
290 tempo += i ;
3880b0fb 291
292 AliRunLoader * rl2 = AliRunLoader::GetRunLoader(tempo) ;
293
294 if (rl2==0)
295 rl2 = AliRunLoader::Open(fInputFileNames[i], tempo) ;
296
45fa49ca 297 if (fManager)
14ce0a6e 298 readEvent = dynamic_cast<AliStream*>(fManager->GetInputStream(i))->GetCurrentEventNumber() ;
299 Info("Digitize", "Adding event %d from input stream %d %s %s", readEvent, i, fInputFileNames[i].Data(), tempo.Data()) ;
3880b0fb 300 rl2->LoadSDigits();
14ce0a6e 301 rl2->GetEvent(readEvent);
3880b0fb 302 AliEMCALLoader *emcalLoader2 = dynamic_cast<AliEMCALLoader*>(rl2->GetDetectorLoader("EMCAL"));
303 sdigArray->AddAt(emcalLoader2->SDigits(), i) ;
0c219753 304 }
5dee926e 305
d968cee0 306 //Find the first tower with signal
a9485c37 307 Int_t nextSig = nEMC + 1 ;
88cb7938 308 TClonesArray * sdigits ;
309 for(i = 0 ; i < fInput ; i++){
310 sdigits = dynamic_cast<TClonesArray *>(sdigArray->At(i)) ;
814ad4bf 311 if ( !sdigits->GetEntriesFast() )
312 continue ;
88cb7938 313 Int_t curNext = dynamic_cast<AliEMCALDigit *>(sdigits->At(0))->GetId() ;
814ad4bf 314 if(curNext < nextSig)
315 nextSig = curNext ;
cf24bdc4 316 AliDebug(1,Form("input %i : #sdigits %i \n",
317 i, sdigits->GetEntriesFast()));
814ad4bf 318 }
cf24bdc4 319 AliDebug(1,Form("FIRST tower with signal %i \n", nextSig));
61e0abb5 320
88cb7938 321 TArrayI index(fInput) ;
814ad4bf 322 index.Reset() ; //Set all indexes to zero
61e0abb5 323
05a92d59 324 AliEMCALDigit * digit ;
325 AliEMCALDigit * curSDigit ;
ffa6d63b 326
9517d886 327 // TClonesArray * ticks = new TClonesArray("AliEMCALTick",1000) ;
ffa6d63b 328
5dee926e 329 //Put Noise contribution
d25f2c54 330 for(absID = 0; absID < nEMC; absID++){ // Nov 30, 2006 by PAI; was from 1 to nEMC
ab6a174f 331 Float_t energy = 0 ;
a9485c37 332 // amplitude set to zero, noise will be added later
d25f2c54 333 new((*digits)[absID]) AliEMCALDigit( -1, -1, absID, 0, TimeOfNoise() ); // absID-1->absID
814ad4bf 334 //look if we have to add signal?
d25f2c54 335 digit = dynamic_cast<AliEMCALDigit *>(digits->At(absID)); // absID-1->absID
88cb7938 336
814ad4bf 337 if(absID==nextSig){
54cbf5a6 338 //Add SDigits from all inputs
9517d886 339 // ticks->Clear() ;
340 //Int_t contrib = 0 ;
341
342 //Follow PHOS and comment out this timing model til a better one
343 //can be developed - JLK 28-Apr-2008
344
345 //Float_t a = digit->GetAmp() ;
346 //Float_t b = TMath::Abs( a /fTimeSignalLength) ;
88cb7938 347 //Mark the beginning of the signal
9517d886 348 //new((*ticks)[contrib++]) AliEMCALTick(digit->GetTime(),0, b);
fdebddeb 349 //Mark the end of the signal
9517d886 350 //new((*ticks)[contrib++]) AliEMCALTick(digit->GetTime()+fTimeSignalLength, -a, -b);
351
352 // Calculate time as time of the largest digit
353 Float_t time = digit->GetTime() ;
ab6a174f 354 Float_t aTime= digit->GetAmp() ;
814ad4bf 355
88cb7938 356 // loop over input
357 for(i = 0; i< fInput ; i++){ //loop over (possible) merge sources
358 if(dynamic_cast<TClonesArray *>(sdigArray->At(i))->GetEntriesFast() > index[i] )
359 curSDigit = dynamic_cast<AliEMCALDigit*>(dynamic_cast<TClonesArray *>(sdigArray->At(i))->At(index[i])) ;
814ad4bf 360 else
361 curSDigit = 0 ;
362 //May be several digits will contribute from the same input
fdebddeb 363 while(curSDigit && (curSDigit->GetId() == absID)){
814ad4bf 364 //Shift primary to separate primaries belonging different inputs
365 Int_t primaryoffset ;
366 if(fManager)
367 primaryoffset = fManager->GetMask(i) ;
368 else
369 primaryoffset = i ;
370 curSDigit->ShiftPrimary(primaryoffset) ;
9517d886 371
372 //Remove old timing model - JLK 28-April-2008
373 //a = curSDigit->GetAmp() ;
374 //b = a /fTimeSignalLength ;
375 //new((*ticks)[contrib++]) AliEMCALTick(curSDigit->GetTime(),0, b);
376 //new((*ticks)[contrib++]) AliEMCALTick(curSDigit->GetTime()+fTimeSignalLength, -a, -b);
ab6a174f 377 if(curSDigit->GetAmp()>aTime) {
378 aTime = curSDigit->GetAmp();
9517d886 379 time = curSDigit->GetTime();
380 }
814ad4bf 381
ab6a174f 382 *digit = *digit + *curSDigit ; //adds amplitudes of each digit
814ad4bf 383
384 index[i]++ ;
88cb7938 385 if( dynamic_cast<TClonesArray *>(sdigArray->At(i))->GetEntriesFast() > index[i] )
1963b290 386 curSDigit = dynamic_cast<AliEMCALDigit*>(dynamic_cast<TClonesArray *>(sdigArray->At(i))->At(index[i])) ;
814ad4bf 387 else
388 curSDigit = 0 ;
389 }
390 }
ab6a174f 391 //Here we convert the summed amplitude to an energy in GeV
392 energy = sDigitizer->Calibrate(digit->GetAmp()) ; // GeV
a9485c37 393 // add fluctuations for photo-electron creation
ab6a174f 394 energy *= static_cast<Float_t>(gRandom->Poisson(fMeanPhotonElectron)) / static_cast<Float_t>(fMeanPhotonElectron) ;
88cb7938 395
05a92d59 396 //calculate and set time
9517d886 397 //New timing model needed - JLK 28-April-2008
398 //Float_t time = FrontEdgeTime(ticks) ;
814ad4bf 399 digit->SetTime(time) ;
400
401 //Find next signal module
a9485c37 402 nextSig = nEMC + 1 ;
88cb7938 403 for(i = 0 ; i < fInput ; i++){
404 sdigits = dynamic_cast<TClonesArray *>(sdigArray->At(i)) ;
814ad4bf 405 Int_t curNext = nextSig ;
406 if(sdigits->GetEntriesFast() > index[i] ){
88cb7938 407 curNext = dynamic_cast<AliEMCALDigit *>(sdigits->At(index[i]))->GetId() ;
814ad4bf 408 }
409 if(curNext < nextSig) nextSig = curNext ;
410 }
411 }
a9485c37 412 // add the noise now
ab6a174f 413 energy += TMath::Abs(gRandom->Gaus(0., fPinNoise)) ;
414 // JLK 26-June-2008
415 //Now digitize the energy according to the sDigitizer method,
416 //which merely converts the energy to an integer. Later we will
417 //check that the stored value matches our allowed dynamic ranges
418 digit->SetAmp(sDigitizer->Digitize(energy)) ;
419 AliDebug(10,Form(" absID %5i energy %f nextSig %5i\n",
420 absID, energy, nextSig));
421 } // for(absID = 0; absID < nEMC; absID++)
814ad4bf 422
9517d886 423 //ticks->Delete() ;
424 //delete ticks ;
61e0abb5 425
ab6a174f 426 //JLK is it better to call Clear() here?
05a92d59 427 delete sdigArray ; //We should not delete its contents
61e0abb5 428
814ad4bf 429 //remove digits below thresholds
4354827d 430 // until 10-02-2010 remove digits with energy smaller than fDigitThreshold 3*fPinNoise
431 // now, remove digits with Digitized ADC smaller than fDigitThreshold = 3
432 Float_t energy=0;
88cb7938 433 for(i = 0 ; i < nEMC ; i++){
434 digit = dynamic_cast<AliEMCALDigit*>( digits->At(i) ) ;
4354827d 435 //First get the energy in GeV units.
436 energy = sDigitizer->Calibrate(digit->GetAmp()) ;
437 //Then digitize using the calibration constants of the ocdb
438 Int_t ampADC = DigitizeEnergy(energy, digit->GetId()) ;
439 //if(ampADC>2)printf("Digit energy %f, amp %d, amp cal %d, threshold %d\n",energy,digit->GetAmp(),ampADC,fDigitThreshold);
440 if(ampADC < fDigitThreshold)
88cb7938 441 digits->RemoveAt(i) ;
442 else
814ad4bf 443 digit->SetTime(gRandom->Gaus(digit->GetTime(),fTimeResolution) ) ;
a9485c37 444 }
61e0abb5 445
814ad4bf 446 digits->Compress() ;
61e0abb5 447
05a92d59 448 Int_t ndigits = digits->GetEntriesFast() ;
4354827d 449
ab6a174f 450 //JLK 26-June-2008
451 //After we have done the summing and digitizing to create the
452 //digits, now we want to calibrate the resulting amplitude to match
453 //the dynamic range of our real data.
05a92d59 454 for (i = 0 ; i < ndigits ; i++) {
455 digit = dynamic_cast<AliEMCALDigit *>( digits->At(i) ) ;
814ad4bf 456 digit->SetIndexInList(i) ;
1963b290 457 energy = sDigitizer->Calibrate(digit->GetAmp()) ;
ab6a174f 458 digit->SetAmp(DigitizeEnergy(energy, digit->GetId()) ) ;
61e0abb5 459 }
ab6a174f 460
814ad4bf 461}
61e0abb5 462
d62a891a 463// //_____________________________________________________________________
e4dcc484 464Int_t AliEMCALDigitizer::DigitizeEnergy(Float_t energy, Int_t AbsId)
814ad4bf 465{
ab6a174f 466 // JLK 26-June-2008
e4dcc484 467 // Returns digitized value of the energy in a cell absId
ab6a174f 468 // using the calibration constants stored in the OCDB
469 // or default values if no CalibData object is found.
470 // This effectively converts everything to match the dynamic range
471 // of the real data we will collect
472 //
53f1c378 473 // Load Geometry
474 const AliEMCALGeometry * geom = AliEMCALGeometry::GetInstance();
e4dcc484 475
476 if (geom==0)
d25f2c54 477 AliFatal("Did not get geometry from EMCALLoader");
e4dcc484 478
479 Int_t iSupMod = -1;
2bb3725c 480 Int_t nModule = -1;
e4dcc484 481 Int_t nIphi = -1;
482 Int_t nIeta = -1;
483 Int_t iphi = -1;
484 Int_t ieta = -1;
fdebddeb 485 Int_t channel = -999;
e4dcc484 486
2bb3725c 487 Bool_t bCell = geom->GetCellIndex(AbsId, iSupMod, nModule, nIphi, nIeta) ;
e4dcc484 488 if(!bCell)
d25f2c54 489 Error("DigitizeEnergy","Wrong cell id number : AbsId %i ", AbsId) ;
2bb3725c 490 geom->GetCellPhiEtaIndexInSModule(iSupMod,nModule,nIphi, nIeta,iphi,ieta);
d62a891a 491
53f1c378 492 if(fCalibData) {
493 fADCpedestalEC = fCalibData->GetADCpedestal(iSupMod,ieta,iphi);
494 fADCchannelEC = fCalibData->GetADCchannel(iSupMod,ieta,iphi);
e4dcc484 495 }
5eb74a51 496
497 channel = static_cast<Int_t> (TMath::Ceil( (energy + fADCpedestalEC)/fADCchannelEC )) ;
498
fdebddeb 499 if(channel > fNADCEC )
7d74eaca 500 channel = fNADCEC ;
814ad4bf 501 return channel ;
d62a891a 502
61e0abb5 503}
504
505//____________________________________________________________________________
05a92d59 506void AliEMCALDigitizer::Exec(Option_t *option)
507{
45fa49ca 508 // Steering method to process digitization for events
509 // in the range from fFirstEvent to fLastEvent.
510 // This range is optionally set by SetEventRange().
511 // if fLastEvent=-1, then process events until the end.
512 // by default fLastEvent = fFirstEvent (process only one event)
05a92d59 513
88cb7938 514 if (!fInit) { // to prevent overwrite existing file
515 Error( "Exec", "Give a version name different from %s", fEventFolderName.Data() ) ;
516 return ;
517 }
518
814ad4bf 519 if (strstr(option,"print")) {
b42d4197 520
88cb7938 521 Print();
814ad4bf 522 return ;
523 }
524
61e0abb5 525 if(strstr(option,"tim"))
526 gBenchmark->Start("EMCALDigitizer");
5dee926e 527
33c3c91a 528 AliRunLoader *rl = AliRunLoader::Instance();
5dee926e 529 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
530
5b4d2c50 531 // Post Digitizer to the white board
5dee926e 532 emcalLoader->PostDigitizer(this) ;
5b4d2c50 533
30bc5594 534 if (fLastEvent == -1) {
5dee926e 535 fLastEvent = rl->GetNumberOfEvents() - 1 ;
30bc5594 536 }
396a348e 537 else if (fManager)
1963b290 538 fLastEvent = fFirstEvent ; // what is this ??
396a348e 539
45fa49ca 540 Int_t nEvents = fLastEvent - fFirstEvent + 1;
cf24bdc4 541 Int_t ievent;
542
5dee926e 543 rl->LoadSDigits("EMCAL");
cf24bdc4 544 for (ievent = fFirstEvent; ievent <= fLastEvent; ievent++) {
64942713 545
5dee926e 546 rl->GetEvent(ievent);
05a92d59 547
814ad4bf 548 Digitize(ievent) ; //Add prepared SDigits to digits and add the noise
88cb7938 549
9e5d2067 550 WriteDigits() ;
88cb7938 551
61e0abb5 552 if(strstr(option,"deb"))
553 PrintDigits(option);
1963b290 554 if(strstr(option,"table")) gObjectTable->Print();
88cb7938 555
814ad4bf 556 //increment the total number of Digits per run
5dee926e 557 fDigitsInRun += emcalLoader->Digits()->GetEntriesFast() ;
61e0abb5 558 }
64942713 559
5dee926e 560 emcalLoader->CleanDigitizer() ;
5b4d2c50 561
61e0abb5 562 if(strstr(option,"tim")){
563 gBenchmark->Stop("EMCALDigitizer");
b42d4197 564 AliInfo(Form("Exec: took %f seconds for Digitizing %f seconds per event",
565 gBenchmark->GetCpuTime("EMCALDigitizer"), gBenchmark->GetCpuTime("EMCALDigitizer")/nEvents )) ;
88cb7938 566 }
61e0abb5 567}
568
05a92d59 569//____________________________________________________________________________
33a52e55 570//Float_t AliEMCALDigitizer::FrontEdgeTime(TClonesArray * ticks)
571//{
572// // Returns the shortest time among all time ticks
573//
574// ticks->Sort() ; //Sort in accordance with times of ticks
575// TIter it(ticks) ;
576// AliEMCALTick * ctick = (AliEMCALTick *) it.Next() ;
577// Float_t time = ctick->CrossingTime(fTimeThreshold) ;
578//
579// AliEMCALTick * t ;
580// while((t=(AliEMCALTick*) it.Next())){
581// if(t->GetTime() < time) //This tick starts before crossing
582// *ctick+=*t ;
583// else
584// return time ;
585//
586// time = ctick->CrossingTime(fTimeThreshold) ;
587// }
588// return time ;
589//}
590//
05a92d59 591
592//____________________________________________________________________________
593Bool_t AliEMCALDigitizer::Init()
594{
595 // Makes all memory allocations
88cb7938 596 fInit = kTRUE ;
33c3c91a 597 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::Instance()->GetDetectorLoader("EMCAL"));
5dee926e 598
599 if ( emcalLoader == 0 ) {
600 Fatal("Init", "Could not obtain the AliEMCALLoader");
05a92d59 601 return kFALSE;
602 }
05a92d59 603
45fa49ca 604 fFirstEvent = 0 ;
605 fLastEvent = fFirstEvent ;
606
88cb7938 607 if(fManager)
608 fInput = fManager->GetNinputs() ;
609 else
610 fInput = 1 ;
611
612 fInputFileNames = new TString[fInput] ;
613 fEventNames = new TString[fInput] ;
614 fInputFileNames[0] = GetTitle() ;
615 fEventNames[0] = fEventFolderName.Data() ;
616 Int_t index ;
617 for (index = 1 ; index < fInput ; index++) {
618 fInputFileNames[index] = dynamic_cast<AliStream*>(fManager->GetInputStream(index))->GetFileName(0);
619 TString tempo = fManager->GetInputFolderName(index) ;
620 fEventNames[index] = tempo.Remove(tempo.Length()-1) ; // strip of the stream number added bt fManager
05a92d59 621 }
622
88cb7938 623 //to prevent cleaning of this object while GetEvent is called
5dee926e 624 emcalLoader->GetDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
e52475ed 625
53f1c378 626 //Calibration instance
627 fCalibData = emcalLoader->CalibData();
88cb7938 628 return fInit ;
05a92d59 629}
630
631//____________________________________________________________________________
632void AliEMCALDigitizer::InitParameters()
14ce0a6e 633{
29b5d492 634 // Parameter initialization for digitizer
6569f329 635
636 // Get the parameters from the OCDB via the loader
637 AliRunLoader *rl = AliRunLoader::Instance();
638 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
639 AliEMCALSimParam * simParam = 0x0;
640 if(emcalLoader) simParam = emcalLoader->SimulationParameters();
641
642 if(!simParam){
643 simParam = AliEMCALSimParam::GetInstance();
644 AliWarning("Simulation Parameters not available in OCDB?");
645 }
646
647 fMeanPhotonElectron = simParam->GetMeanPhotonElectron();//4400; // electrons per GeV
648 fPinNoise = simParam->GetPinNoise();//0.012; // pin noise in GeV from analysis test beam data
33a52e55 649 if (fPinNoise < 0.0001 )
88cb7938 650 Warning("InitParameters", "No noise added\n") ;
6569f329 651 fDigitThreshold = simParam->GetDigitThreshold(); //fPinNoise * 3; // 3 * sigma
652 fTimeResolution = simParam->GetTimeResolution(); //0.6e-9 ; // 600 psc
05a92d59 653
97075cd8 654 // These defaults are normally not used.
655 // Values are read from calibration database instead
33a52e55 656 fADCchannelEC = 0.0153; // Update 24 Apr 2007: 250./16/1024 - width of one ADC channel in GeV
657 fADCpedestalEC = 0.0 ; // GeV
658
6569f329 659 fNADCEC = simParam->GetNADCEC();//(Int_t) TMath::Power(2,16) ; // number of channels in Tower ADC - 65536
33a52e55 660
661 AliDebug(2,Form("Mean Photon Electron %d, Noise %f, E Threshold %f,Time Resolution %g,NADCEC %d",
662 fMeanPhotonElectron,fPinNoise,fDigitThreshold,fTimeResolution,fNADCEC));
a9485c37 663
33a52e55 664 // Not used anymore, remove?
665 // fTimeSignalLength = 1.0e-9 ;
666 // fTimeThreshold = 0.001*10000000 ; // Means 1 MeV in terms of SDigits amplitude ??
ab6a174f 667
05a92d59 668}
61e0abb5 669
61e0abb5 670//__________________________________________________________________
09884213 671void AliEMCALDigitizer::MixWith(TString alirunFileName, TString eventFolderName)
05a92d59 672{
673 // Allows to produce digits by superimposing background and signal event.
61e0abb5 674 // It is assumed, that headers file with SIGNAL events is opened in
05a92d59 675 // the constructor.
676 // Sets the BACKGROUND event, with which the SIGNAL event is to be mixed
677 // Thus we avoid writing (changing) huge and expensive
61e0abb5 678 // backgound files: all output will be writen into SIGNAL, i.e.
679 // opened in constructor file.
680 //
05a92d59 681 // One can open as many files to mix with as one needs.
682 // However only Sdigits with the same name (i.e. constructed with the same SDigitizer)
683 // can be mixed.
61e0abb5 684
05a92d59 685 if( strcmp(GetName(), "") == 0 )
61e0abb5 686 Init() ;
814ad4bf 687
05a92d59 688 if(fManager){
9859bfc0 689 Error("MixWith", "Cannot use this method under AliRunDigitizer") ;
61e0abb5 690 return ;
05a92d59 691 }
88cb7938 692 // looking for file which contains AliRun
693 if (gSystem->AccessPathName(alirunFileName)) {// file does not exist
694 Error("MixWith", "File %s does not exist!", alirunFileName.Data()) ;
695 return ;
61e0abb5 696 }
88cb7938 697 // looking for the file which contains SDigits
33c3c91a 698 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::Instance()->GetDetectorLoader("EMCAL"));
5dee926e 699 TString fileName( emcalLoader->GetSDigitsFileName() ) ;
e191bb57 700 if ( eventFolderName != AliConfig::GetDefaultEventFolderName()) // only if not the default folder name
88cb7938 701 fileName = fileName.ReplaceAll(".root", "") + "_" + eventFolderName + ".root" ;
702 if ( (gSystem->AccessPathName(fileName)) ) {
703 Error("MixWith", "The file %s does not exist!", fileName.Data()) ;
704 return ;
814ad4bf 705 }
88cb7938 706 // need to increase the arrays
30bc5594 707 // MvL: This code only works when fInput == 1, I think.
88cb7938 708 TString tempo = fInputFileNames[fInput-1] ;
709 delete [] fInputFileNames ;
710 fInputFileNames = new TString[fInput+1] ;
711 fInputFileNames[fInput-1] = tempo ;
712
713 tempo = fEventNames[fInput-1] ;
714 delete [] fEventNames ;
715 fEventNames = new TString[fInput+1] ;
716 fEventNames[fInput-1] = tempo ;
717
718 fInputFileNames[fInput] = alirunFileName ;
719 fEventNames[fInput] = eventFolderName ;
720 fInput++ ;
721}
1963b290 722
fe93d365 723//__________________________________________________________________
1963b290 724void AliEMCALDigitizer::Print1(Option_t * option)
725{ // 19-nov-04 - just for convinience
726 Print();
727 PrintDigits(option);
728}
729
61e0abb5 730//__________________________________________________________________
eb0b1051 731void AliEMCALDigitizer::Print(Option_t*)const
88cb7938 732{
733 // Print Digitizer's parameters
fdebddeb 734 printf("Print: \n------------------- %s -------------", GetName() ) ;
88cb7938 735 if( strcmp(fEventFolderName.Data(), "") != 0 ){
736 printf(" Writing Digits to branch with title %s\n", fEventFolderName.Data()) ;
737
738 Int_t nStreams ;
739 if (fManager)
740 nStreams = GetNInputStreams() ;
741 else
742 nStreams = fInput ;
743
744 Int_t index = 0 ;
5dee926e 745
746 AliRunLoader *rl=0;
747
88cb7938 748 for (index = 0 ; index < nStreams ; index++) {
749 TString tempo(fEventNames[index]) ;
750 tempo += index ;
5dee926e 751 if ((rl = AliRunLoader::GetRunLoader(tempo)) == 0)
752 rl = AliRunLoader::Open(fInputFileNames[index], tempo) ;
753 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
754 TString fileName( emcalLoader->GetSDigitsFileName() ) ;
e191bb57 755 if ( fEventNames[index] != AliConfig::GetDefaultEventFolderName()) // only if not the default folder name
88cb7938 756 fileName = fileName.ReplaceAll(".root", "") + "_" + fEventNames[index] + ".root" ;
757 printf ("Adding SDigits from %s %s\n", fInputFileNames[index].Data(), fileName.Data()) ;
758 }
5dee926e 759
33c3c91a 760 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::Instance()->GetDetectorLoader("EMCAL"));
5dee926e 761
762 printf("\nWriting digits to %s", emcalLoader->GetDigitsFileName().Data()) ;
88cb7938 763
764 printf("\nWith following parameters:\n") ;
765
766 printf(" Electronics noise in EMC (fPinNoise) = %f\n", fPinNoise) ;
4354827d 767 printf(" Threshold in Tower (fDigitThreshold) = %d\n", fDigitThreshold) ;
88cb7938 768 printf("---------------------------------------------------\n") ;
ea5f3389 769 }
770 else
fdebddeb 771 printf("Print: AliEMCALDigitizer not initialized") ;
61e0abb5 772}
814ad4bf 773
61e0abb5 774//__________________________________________________________________
14ce0a6e 775void AliEMCALDigitizer::PrintDigits(Option_t * option)
776{
777 //utility method for printing digit information
778
33c3c91a 779 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::Instance()->GetDetectorLoader("EMCAL"));
5dee926e 780 TClonesArray * digits = emcalLoader->Digits() ;
781 TClonesArray * sdigits = emcalLoader->SDigits() ;
88cb7938 782
1963b290 783 printf("\n #Digits: %d : sdigits %d ", digits->GetEntriesFast(), sdigits->GetEntriesFast()) ;
5dee926e 784 printf("\n event %d", emcalLoader->GetRunLoader()->GetEventNumber());
88cb7938 785
786 if(strstr(option,"all")){
61e0abb5 787 //loop over digits
788 AliEMCALDigit * digit;
88cb7938 789 printf("\nEMC digits (with primaries):\n") ;
790 printf("\n Id Amplitude Time Index Nprim: Primaries list \n") ;
61e0abb5 791 Int_t index ;
88cb7938 792 for (index = 0 ; index < digits->GetEntries() ; index++) {
793 digit = dynamic_cast<AliEMCALDigit *>(digits->At(index)) ;
794 printf("\n%6d %8d %6.5e %4d %2d : ",
11f9c5ff 795 digit->GetId(), digit->GetAmp(), digit->GetTime(), digit->GetIndexInList(), digit->GetNprimary()) ;
61e0abb5 796 Int_t iprimary;
9859bfc0 797 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++) {
88cb7938 798 printf("%d ",digit->GetPrimary(iprimary+1) ) ;
9859bfc0 799 }
800 }
61e0abb5 801 }
1963b290 802 printf("\n");
61e0abb5 803}
05a92d59 804
805//__________________________________________________________________
806Float_t AliEMCALDigitizer::TimeOfNoise(void)
04f0bda3 807{
808 // Calculates the time signal generated by noise
26a2ef9d 809 //PH Info("TimeOfNoise", "Change me") ;
04f0bda3 810 return gRandom->Rndm() * 1.28E-5;
05a92d59 811}
812
88cb7938 813//__________________________________________________________________
814void AliEMCALDigitizer::Unload()
815{
fdebddeb 816 // Unloads the SDigits and Digits
5dee926e 817 AliRunLoader *rl=0;
818
88cb7938 819 Int_t i ;
820 for(i = 1 ; i < fInput ; i++){
821 TString tempo(fEventNames[i]) ;
5dee926e 822 tempo += i;
823 if ((rl = AliRunLoader::GetRunLoader(tempo)))
824 rl->GetDetectorLoader("EMCAL")->UnloadSDigits() ;
88cb7938 825 }
33c3c91a 826 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::Instance()->GetDetectorLoader("EMCAL"));
5dee926e 827 emcalLoader->UnloadDigits() ;
88cb7938 828}
829
814ad4bf 830//_________________________________________________________________________________________
9e5d2067 831void AliEMCALDigitizer::WriteDigits()
814ad4bf 832{
61e0abb5 833
814ad4bf 834 // Makes TreeD in the output file.
835 // Check if branch already exists:
836 // if yes, exit without writing: ROOT TTree does not support overwriting/updating of
837 // already existing branches.
838 // else creates branch with Digits, named "EMCAL", title "...",
839 // and branch "AliEMCALDigitizer", with the same title to keep all the parameters
840 // and names of files, from which digits are made.
61e0abb5 841
33c3c91a 842 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::Instance()->GetDetectorLoader("EMCAL"));
cf24bdc4 843
5dee926e 844 const TClonesArray * digits = emcalLoader->Digits() ;
845 TTree * treeD = emcalLoader->TreeD();
846 if ( !treeD ) {
847 emcalLoader->MakeDigitsContainer();
848 treeD = emcalLoader->TreeD();
849 }
88cb7938 850
851 // -- create Digits branch
852 Int_t bufferSize = 32000 ;
5dee926e 853 TBranch * digitsBranch = 0;
30bc5594 854 if ((digitsBranch = treeD->GetBranch("EMCAL"))) {
5dee926e 855 digitsBranch->SetAddress(&digits);
30bc5594 856 AliWarning("Digits Branch already exists. Not all digits will be visible");
857 }
5dee926e 858 else
859 treeD->Branch("EMCAL","TClonesArray",&digits,bufferSize);
860 //digitsBranch->SetTitle(fEventFolderName);
861 treeD->Fill() ;
88cb7938 862
5dee926e 863 emcalLoader->WriteDigits("OVERWRITE");
864 emcalLoader->WriteDigitizer("OVERWRITE");
88cb7938 865
866 Unload() ;
867
814ad4bf 868}
61e0abb5 869
fe93d365 870//__________________________________________________________________
1963b290 871void AliEMCALDigitizer::Browse(TBrowser* b)
872{
1963b290 873 TTask::Browse(b);
874}