Removing overlaps. Using assemblies for the alignable voulmes (L.Gaudichet)
[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//////////////////////////////////////////////////////////////////////////////
ffa6d63b 21// Class performs digitization of Summable digits
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 ---
1963b290 62#include "TROOT.h"
61e0abb5 63#include "TTree.h"
64#include "TSystem.h"
61e0abb5 65#include "TBenchmark.h"
1963b290 66#include "TList.h"
67#include "TH1.h"
68#include "TBrowser.h"
69#include "TObjectTable.h"
61e0abb5 70
71// --- AliRoot header files ---
e4dcc484 72#include "AliRun.h"
ea5f3389 73#include "AliRunDigitizer.h"
5dee926e 74#include "AliRunLoader.h"
61e0abb5 75#include "AliEMCALDigit.h"
05a92d59 76#include "AliEMCAL.h"
5dee926e 77#include "AliEMCALLoader.h"
61e0abb5 78#include "AliEMCALDigitizer.h"
79#include "AliEMCALSDigitizer.h"
80#include "AliEMCALGeometry.h"
05a92d59 81#include "AliEMCALTick.h"
315d1c64 82#include "AliEMCALHistoUtilities.h"
05a92d59 83
61e0abb5 84ClassImp(AliEMCALDigitizer)
85
86
87//____________________________________________________________________________
88cb7938 88 AliEMCALDigitizer::AliEMCALDigitizer():AliDigitizer("",""),
89 fInput(0),
90 fInputFileNames(0x0),
91 fEventNames(0x0)
61e0abb5 92{
93 // ctor
88cb7938 94 InitParameters() ;
92f521a9 95 fDefaultInit = kTRUE ;
05a92d59 96 fManager = 0 ; // We work in the standalong mode
88cb7938 97 fEventFolderName = "" ;
839828a6 98}
61e0abb5 99
839828a6 100//____________________________________________________________________________
09884213 101AliEMCALDigitizer::AliEMCALDigitizer(TString alirunFileName, TString eventFolderName):
e191bb57 102 AliDigitizer("EMCAL"+AliConfig::Instance()->GetDigitizerTaskName(), alirunFileName),
88cb7938 103 fInputFileNames(0), fEventNames(0), fEventFolderName(eventFolderName)
839828a6 104{
05a92d59 105 // ctor
106
a9485c37 107 InitParameters() ;
88cb7938 108 Init() ;
92f521a9 109 fDefaultInit = kFALSE ;
88cb7938 110 fManager = 0 ; // We work in the standalong mode
61e0abb5 111}
839828a6 112
61e0abb5 113//____________________________________________________________________________
9e5d2067 114AliEMCALDigitizer::AliEMCALDigitizer(const AliEMCALDigitizer & d) : AliDigitizer(d)
88cb7938 115{
116 // copyy ctor
117
118 SetName(d.GetName()) ;
119 SetTitle(d.GetTitle()) ;
120 fDigitThreshold = d.fDigitThreshold ;
121 fMeanPhotonElectron = d.fMeanPhotonElectron ;
122 fPedestal = d.fPedestal ;
123 fSlope = d.fSlope ;
124 fPinNoise = d.fPinNoise ;
125 fTimeResolution = d.fTimeResolution ;
126 fTimeThreshold = d.fTimeThreshold ;
127 fTimeSignalLength = d.fTimeSignalLength ;
128 fADCchannelEC = d.fADCchannelEC ;
129 fADCpedestalEC = d.fADCpedestalEC ;
130 fNADCEC = d.fNADCEC ;
88cb7938 131 fEventFolderName = d.fEventFolderName;
132 }
133
134//____________________________________________________________________________
135AliEMCALDigitizer::AliEMCALDigitizer(AliRunDigitizer * rd):
e191bb57 136 AliDigitizer(rd,"EMCAL"+AliConfig::Instance()->GetDigitizerTaskName()),
88cb7938 137 fEventFolderName(0)
61e0abb5 138{
45fa49ca 139 // ctor Init() is called by RunDigitizer
88cb7938 140 fManager = rd ;
e0e18b6d 141 fEventFolderName = fManager->GetInputFolderName(0) ;
88cb7938 142 SetTitle(dynamic_cast<AliStream*>(fManager->GetInputStream(0))->GetFileName(0));
05a92d59 143 InitParameters() ;
dafd64d0 144 fDefaultInit = kFALSE ;
839828a6 145}
814ad4bf 146
839828a6 147//____________________________________________________________________________
148 AliEMCALDigitizer::~AliEMCALDigitizer()
149{
5dee926e 150 if (AliRunLoader::GetRunLoader()) {
151 AliLoader *emcalLoader=0;
152 if ((emcalLoader = AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL")))
153 emcalLoader->CleanDigitizer();
154 }
155 else
156 AliDebug(1," no runloader present");
f5f384f4 157 delete [] fInputFileNames ;
158 delete [] fEventNames ;
61e0abb5 159}
160
161//____________________________________________________________________________
09884213 162void AliEMCALDigitizer::Digitize(Int_t event)
05a92d59 163{
61e0abb5 164
165 // Makes the digitization of the collected summable digits
166 // for this it first creates the array of all EMCAL modules
167 // filled with noise (different for EMC, CPV and PPSD) and
168 // after that adds contributions from SDigits. This design
169 // helps to avoid scanning over the list of digits to add
170 // contribution of any new SDigit.
1963b290 171 static int isTrd1Geom = -1; // -1 - mean undefined
172 static int nEMC=0; //max number of digits possible
61e0abb5 173
5dee926e 174 AliRunLoader *rl = AliRunLoader::GetRunLoader();
175 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
45fa49ca 176 Int_t ReadEvent = event ;
1963b290 177 // fManager is data member from AliDigitizer
45fa49ca 178 if (fManager)
179 ReadEvent = dynamic_cast<AliStream*>(fManager->GetInputStream(0))->GetCurrentEventNumber() ;
cf24bdc4 180 AliDebug(1,Form("Adding event %d from input stream 0 %s %s",
181 ReadEvent, GetTitle(), fEventFolderName.Data())) ;
5dee926e 182 rl->GetEvent(ReadEvent);
183
184 TClonesArray * digits = emcalLoader->Digits() ;
814ad4bf 185 digits->Clear() ;
61e0abb5 186
e4dcc484 187 // Load Geometry
188 // const AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance();
189 rl->LoadgAlice();
190 AliRun * gAlice = rl->GetAliRun();
191 AliEMCAL * emcal = (AliEMCAL*)gAlice->GetDetector("EMCAL");
192 AliEMCALGeometry * geom = emcal->GetGeometry();
193
1963b290 194 if(isTrd1Geom < 0) {
195 TString ng(geom->GetName());
196 isTrd1Geom = 0;
197 if(ng.Contains("SHISH") && ng.Contains("TRD1")) isTrd1Geom = 1;
fdebddeb 198
1963b290 199 if(isTrd1Geom == 0) nEMC = geom->GetNPhi()*geom->GetNZ();
200 else nEMC = geom->GetNCells();
5dee926e 201 AliDebug(1,Form("nEMC %i (number cells in EMCAL) | %s | isTrd1Geom %i\n", nEMC, geom->GetName(), isTrd1Geom));
1963b290 202 }
88cb7938 203 Int_t absID ;
61e0abb5 204
88cb7938 205 digits->Expand(nEMC) ;
05a92d59 206
88cb7938 207 // get first the sdigitizer from the tasks list (must have same name as the digitizer)
5dee926e 208 if ( !emcalLoader->SDigitizer() )
209 emcalLoader->LoadSDigitizer();
210 AliEMCALSDigitizer * sDigitizer = dynamic_cast<AliEMCALSDigitizer*>(emcalLoader->SDigitizer());
88cb7938 211
212 if ( !sDigitizer )
213 Fatal("Digitize", "SDigitizer with name %s %s not found", fEventFolderName.Data(), GetTitle() ) ;
214
215 //take all the inputs to add together and load the SDigits
216 TObjArray * sdigArray = new TObjArray(fInput) ;
5dee926e 217 sdigArray->AddAt(emcalLoader->SDigits(), 0) ;
88cb7938 218 Int_t i ;
5dee926e 219
88cb7938 220 for(i = 1 ; i < fInput ; i++){
221 TString tempo(fEventNames[i]) ;
222 tempo += i ;
5dee926e 223 rl = AliRunLoader::Open(fInputFileNames[i], tempo) ;
45fa49ca 224 if (fManager)
225 ReadEvent = dynamic_cast<AliStream*>(fManager->GetInputStream(i))->GetCurrentEventNumber() ;
b67f6b2d 226 Info("Digitize", "Adding event %d from input stream %d %s %s", ReadEvent, i, fInputFileNames[i].Data(), tempo.Data()) ;
5dee926e 227 rl->LoadSDigits();
228 rl->GetEvent(ReadEvent);
229 sdigArray->AddAt(emcalLoader->SDigits(), i) ;
0c219753 230 }
5dee926e 231
d968cee0 232 //Find the first tower with signal
a9485c37 233 Int_t nextSig = nEMC + 1 ;
88cb7938 234 TClonesArray * sdigits ;
235 for(i = 0 ; i < fInput ; i++){
236 sdigits = dynamic_cast<TClonesArray *>(sdigArray->At(i)) ;
814ad4bf 237 if ( !sdigits->GetEntriesFast() )
238 continue ;
88cb7938 239 Int_t curNext = dynamic_cast<AliEMCALDigit *>(sdigits->At(0))->GetId() ;
814ad4bf 240 if(curNext < nextSig)
241 nextSig = curNext ;
cf24bdc4 242 AliDebug(1,Form("input %i : #sdigits %i \n",
243 i, sdigits->GetEntriesFast()));
814ad4bf 244 }
cf24bdc4 245 AliDebug(1,Form("FIRST tower with signal %i \n", nextSig));
61e0abb5 246
88cb7938 247 TArrayI index(fInput) ;
814ad4bf 248 index.Reset() ; //Set all indexes to zero
61e0abb5 249
05a92d59 250 AliEMCALDigit * digit ;
251 AliEMCALDigit * curSDigit ;
ffa6d63b 252
814ad4bf 253 TClonesArray * ticks = new TClonesArray("AliEMCALTick",1000) ;
ffa6d63b 254
5dee926e 255 //Put Noise contribution
814ad4bf 256 for(absID = 1; absID <= nEMC; absID++){
88cb7938 257 Float_t amp = 0 ;
a9485c37 258 // amplitude set to zero, noise will be added later
259 new((*digits)[absID-1]) AliEMCALDigit( -1, -1, absID, 0, TimeOfNoise() ) ;
814ad4bf 260 //look if we have to add signal?
88cb7938 261 digit = dynamic_cast<AliEMCALDigit *>(digits->At(absID-1)) ;
262
814ad4bf 263 if(absID==nextSig){
54cbf5a6 264 //Add SDigits from all inputs
814ad4bf 265 ticks->Clear() ;
266 Int_t contrib = 0 ;
267 Float_t a = digit->GetAmp() ;
268 Float_t b = TMath::Abs( a /fTimeSignalLength) ;
88cb7938 269 //Mark the beginning of the signal
814ad4bf 270 new((*ticks)[contrib++]) AliEMCALTick(digit->GetTime(),0, b);
fdebddeb 271 //Mark the end of the signal
814ad4bf 272 new((*ticks)[contrib++]) AliEMCALTick(digit->GetTime()+fTimeSignalLength, -a, -b);
273
88cb7938 274 // loop over input
275 for(i = 0; i< fInput ; i++){ //loop over (possible) merge sources
276 if(dynamic_cast<TClonesArray *>(sdigArray->At(i))->GetEntriesFast() > index[i] )
277 curSDigit = dynamic_cast<AliEMCALDigit*>(dynamic_cast<TClonesArray *>(sdigArray->At(i))->At(index[i])) ;
814ad4bf 278 else
279 curSDigit = 0 ;
280 //May be several digits will contribute from the same input
fdebddeb 281 while(curSDigit && (curSDigit->GetId() == absID)){
814ad4bf 282 //Shift primary to separate primaries belonging different inputs
283 Int_t primaryoffset ;
284 if(fManager)
285 primaryoffset = fManager->GetMask(i) ;
286 else
287 primaryoffset = i ;
288 curSDigit->ShiftPrimary(primaryoffset) ;
289
290 a = curSDigit->GetAmp() ;
291 b = a /fTimeSignalLength ;
292 new((*ticks)[contrib++]) AliEMCALTick(curSDigit->GetTime(),0, b);
293 new((*ticks)[contrib++]) AliEMCALTick(curSDigit->GetTime()+fTimeSignalLength, -a, -b);
294
295 *digit = *digit + *curSDigit ; //add energies
296
297 index[i]++ ;
88cb7938 298 if( dynamic_cast<TClonesArray *>(sdigArray->At(i))->GetEntriesFast() > index[i] )
1963b290 299 curSDigit = dynamic_cast<AliEMCALDigit*>(dynamic_cast<TClonesArray *>(sdigArray->At(i))->At(index[i])) ;
814ad4bf 300 else
301 curSDigit = 0 ;
302 }
303 }
a9485c37 304 // add fluctuations for photo-electron creation
305 amp = sDigitizer->Calibrate(digit->GetAmp()) ; // GeV
a9485c37 306 amp *= static_cast<Float_t>(gRandom->Poisson(fMeanPhotonElectron)) / static_cast<Float_t>(fMeanPhotonElectron) ;
88cb7938 307
05a92d59 308 //calculate and set time
814ad4bf 309 Float_t time = FrontEdgeTime(ticks) ;
310 digit->SetTime(time) ;
311
312 //Find next signal module
a9485c37 313 nextSig = nEMC + 1 ;
88cb7938 314 for(i = 0 ; i < fInput ; i++){
315 sdigits = dynamic_cast<TClonesArray *>(sdigArray->At(i)) ;
814ad4bf 316 Int_t curNext = nextSig ;
317 if(sdigits->GetEntriesFast() > index[i] ){
88cb7938 318 curNext = dynamic_cast<AliEMCALDigit *>(sdigits->At(index[i]))->GetId() ;
814ad4bf 319 }
320 if(curNext < nextSig) nextSig = curNext ;
321 }
322 }
a9485c37 323 // add the noise now
fdebddeb 324 amp += TMath::Abs(gRandom->Gaus(0., fPinNoise)) ;
a9485c37 325 digit->SetAmp(sDigitizer->Digitize(amp)) ;
cf24bdc4 326 AliDebug(10,Form(" absID %5i amp %f nextSig %5i\n",
327 absID, amp, nextSig));
1963b290 328 } // for(absID = 1; absID <= nEMC; absID++)
814ad4bf 329
330 ticks->Delete() ;
331 delete ticks ;
61e0abb5 332
05a92d59 333 delete sdigArray ; //We should not delete its contents
61e0abb5 334
814ad4bf 335 //remove digits below thresholds
88cb7938 336 for(i = 0 ; i < nEMC ; i++){
337 digit = dynamic_cast<AliEMCALDigit*>( digits->At(i) ) ;
fdebddeb 338 Float_t threshold = fDigitThreshold ;
88cb7938 339 if(sDigitizer->Calibrate( digit->GetAmp() ) < threshold)
340 digits->RemoveAt(i) ;
341 else
814ad4bf 342 digit->SetTime(gRandom->Gaus(digit->GetTime(),fTimeResolution) ) ;
a9485c37 343 }
61e0abb5 344
814ad4bf 345 digits->Compress() ;
61e0abb5 346
05a92d59 347 Int_t ndigits = digits->GetEntriesFast() ;
88cb7938 348 digits->Expand(ndigits) ;
61e0abb5 349
1963b290 350 //Set indexes in list of digits and fill hists.
315d1c64 351 AliEMCALHistoUtilities::FillH1(fHists, 0, Double_t(ndigits));
1963b290 352 Float_t energy=0., esum=0.;
05a92d59 353 for (i = 0 ; i < ndigits ; i++) {
354 digit = dynamic_cast<AliEMCALDigit *>( digits->At(i) ) ;
814ad4bf 355 digit->SetIndexInList(i) ;
1963b290 356 energy = sDigitizer->Calibrate(digit->GetAmp()) ;
357 esum += energy;
e4dcc484 358 digit->SetAmp(DigitizeEnergy(energy, digit->GetId()) ) ; // for what ??
315d1c64 359 AliEMCALHistoUtilities::FillH1(fHists, 2, double(digit->GetAmp()));
360 AliEMCALHistoUtilities::FillH1(fHists, 3, double(energy));
361 AliEMCALHistoUtilities::FillH1(fHists, 4, double(digit->GetId()));
61e0abb5 362 }
315d1c64 363 AliEMCALHistoUtilities::FillH1(fHists, 1, esum);
814ad4bf 364}
61e0abb5 365
814ad4bf 366//____________________________________________________________________________
e4dcc484 367Int_t AliEMCALDigitizer::DigitizeEnergy(Float_t energy, Int_t AbsId)
814ad4bf 368{
e4dcc484 369 // Returns digitized value of the energy in a cell absId
370 // Loader
371 AliRunLoader *rl = AliRunLoader::GetRunLoader();
372 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>
373 (rl->GetDetectorLoader("EMCAL"));
374
375 // Load EMCAL Geometry
376 rl->LoadgAlice();
377 AliRun * gAlice = rl->GetAliRun();
378 AliEMCAL * emcal = (AliEMCAL*)gAlice->GetDetector("EMCAL");
379 AliEMCALGeometry * geom = emcal->GetGeometry();
380
381 if (geom==0)
382 AliFatal("Did not get geometry from EMCALLoader") ;
383
384 Int_t iSupMod = -1;
385 Int_t nTower = -1;
386 Int_t nIphi = -1;
387 Int_t nIeta = -1;
388 Int_t iphi = -1;
389 Int_t ieta = -1;
fdebddeb 390 Int_t channel = -999;
e4dcc484 391
392 Bool_t bCell = geom->GetCellIndex(AbsId, iSupMod, nTower, nIphi, nIeta) ;
393 if(!bCell)
394 Error("DigitizeEnergy","Wrong cell id number") ;
395 geom->GetCellPhiEtaIndexInSModule(iSupMod,nTower,nIphi, nIeta,iphi,ieta);
396
397 if(emcalLoader->CalibData()) {
398 fADCpedestalEC = emcalLoader->CalibData()
399 ->GetADCpedestal(iSupMod,ieta,iphi);
400 fADCchannelEC = emcalLoader->CalibData()
401 ->GetADCchannel(iSupMod,ieta,iphi);
402
403 channel = static_cast<Int_t> (TMath::Ceil( (energy + fADCpedestalEC)/fADCchannelEC )) ;
404 }
fdebddeb 405 if(channel > fNADCEC )
7d74eaca 406 channel = fNADCEC ;
814ad4bf 407 return channel ;
61e0abb5 408}
409
410//____________________________________________________________________________
05a92d59 411void AliEMCALDigitizer::Exec(Option_t *option)
412{
45fa49ca 413 // Steering method to process digitization for events
414 // in the range from fFirstEvent to fLastEvent.
415 // This range is optionally set by SetEventRange().
416 // if fLastEvent=-1, then process events until the end.
417 // by default fLastEvent = fFirstEvent (process only one event)
05a92d59 418
88cb7938 419 if (!fInit) { // to prevent overwrite existing file
420 Error( "Exec", "Give a version name different from %s", fEventFolderName.Data() ) ;
421 return ;
422 }
423
814ad4bf 424 if (strstr(option,"print")) {
88cb7938 425 Print();
814ad4bf 426 return ;
427 }
428
61e0abb5 429 if(strstr(option,"tim"))
430 gBenchmark->Start("EMCALDigitizer");
5dee926e 431
432 AliRunLoader *rl = AliRunLoader::GetRunLoader();
433 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
434
5b4d2c50 435 // Post Digitizer to the white board
5dee926e 436 emcalLoader->PostDigitizer(this) ;
5b4d2c50 437
45fa49ca 438 if (fLastEvent == -1)
5dee926e 439 fLastEvent = rl->GetNumberOfEvents() - 1 ;
396a348e 440 else if (fManager)
1963b290 441 fLastEvent = fFirstEvent ; // what is this ??
396a348e 442
45fa49ca 443 Int_t nEvents = fLastEvent - fFirstEvent + 1;
cf24bdc4 444 Int_t ievent;
445
5dee926e 446 rl->LoadSDigits("EMCAL");
cf24bdc4 447 for (ievent = fFirstEvent; ievent <= fLastEvent; ievent++) {
5dee926e 448 //gime->Event(ievent,"S") ;
449 rl->GetEvent(ievent);
05a92d59 450
814ad4bf 451 Digitize(ievent) ; //Add prepared SDigits to digits and add the noise
88cb7938 452
9e5d2067 453 WriteDigits() ;
88cb7938 454
61e0abb5 455 if(strstr(option,"deb"))
456 PrintDigits(option);
1963b290 457 if(strstr(option,"table")) gObjectTable->Print();
88cb7938 458
814ad4bf 459 //increment the total number of Digits per run
5dee926e 460 fDigitsInRun += emcalLoader->Digits()->GetEntriesFast() ;
61e0abb5 461 }
1963b290 462 // gime->WriteDigitizer("OVERWRITE");
463
5dee926e 464 emcalLoader->CleanDigitizer() ;
5b4d2c50 465
61e0abb5 466 if(strstr(option,"tim")){
467 gBenchmark->Stop("EMCALDigitizer");
fdebddeb 468 printf("Exec: took %f seconds for Digitizing %f seconds per event",
45fa49ca 469 gBenchmark->GetCpuTime("EMCALDigitizer"), gBenchmark->GetCpuTime("EMCALDigitizer")/nEvents ) ;
88cb7938 470 }
61e0abb5 471}
472
05a92d59 473//____________________________________________________________________________
474Float_t AliEMCALDigitizer::FrontEdgeTime(TClonesArray * ticks)
88cb7938 475{
476 // Returns the shortest time among all time ticks
477
05a92d59 478 ticks->Sort() ; //Sort in accordance with times of ticks
479 TIter it(ticks) ;
480 AliEMCALTick * ctick = (AliEMCALTick *) it.Next() ;
481 Float_t time = ctick->CrossingTime(fTimeThreshold) ;
482
483 AliEMCALTick * t ;
484 while((t=(AliEMCALTick*) it.Next())){
485 if(t->GetTime() < time) //This tick starts before crossing
486 *ctick+=*t ;
487 else
488 return time ;
489
490 time = ctick->CrossingTime(fTimeThreshold) ;
491 }
492 return time ;
493}
494
495//____________________________________________________________________________
496Bool_t AliEMCALDigitizer::Init()
497{
498 // Makes all memory allocations
88cb7938 499 fInit = kTRUE ;
5dee926e 500 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
501
502 if ( emcalLoader == 0 ) {
503 Fatal("Init", "Could not obtain the AliEMCALLoader");
05a92d59 504 return kFALSE;
505 }
05a92d59 506
45fa49ca 507 fFirstEvent = 0 ;
508 fLastEvent = fFirstEvent ;
509
88cb7938 510 if(fManager)
511 fInput = fManager->GetNinputs() ;
512 else
513 fInput = 1 ;
514
515 fInputFileNames = new TString[fInput] ;
516 fEventNames = new TString[fInput] ;
517 fInputFileNames[0] = GetTitle() ;
518 fEventNames[0] = fEventFolderName.Data() ;
519 Int_t index ;
520 for (index = 1 ; index < fInput ; index++) {
521 fInputFileNames[index] = dynamic_cast<AliStream*>(fManager->GetInputStream(index))->GetFileName(0);
522 TString tempo = fManager->GetInputFolderName(index) ;
523 fEventNames[index] = tempo.Remove(tempo.Length()-1) ; // strip of the stream number added bt fManager
05a92d59 524 }
525
88cb7938 526 //to prevent cleaning of this object while GetEvent is called
5dee926e 527 emcalLoader->GetDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
e52475ed 528
529 Print();
88cb7938 530
531 return fInit ;
05a92d59 532}
533
534//____________________________________________________________________________
535void AliEMCALDigitizer::InitParameters()
1963b290 536{ // Tune parameters - 24-nov-04
537
538 fMeanPhotonElectron = 3300 ; // electrons per GeV
539 fPinNoise = 0.004;
88cb7938 540 if (fPinNoise == 0. )
541 Warning("InitParameters", "No noise added\n") ;
1963b290 542 fDigitThreshold = fPinNoise * 3; // 3 * sigma
543 fTimeResolution = 0.3e-9 ; // 300 psc
05a92d59 544 fTimeSignalLength = 1.0e-9 ;
05a92d59 545
1963b290 546 fADCchannelEC = 0.00305; // 200./65536 - width of one ADC channel in GeV
547 fADCpedestalEC = 0.009 ; // GeV
548 fNADCEC = (Int_t) TMath::Power(2,16) ; // number of channels in Tower ADC - 65536
a9485c37 549
1963b290 550 fTimeThreshold = 0.001*10000000 ; // Means 1 MeV in terms of SDigits amplitude ??
551 // hists. for control; no hists on default
552 fControlHists = 0;
553 fHists = 0;
05a92d59 554}
61e0abb5 555
61e0abb5 556//__________________________________________________________________
09884213 557void AliEMCALDigitizer::MixWith(TString alirunFileName, TString eventFolderName)
05a92d59 558{
559 // Allows to produce digits by superimposing background and signal event.
61e0abb5 560 // It is assumed, that headers file with SIGNAL events is opened in
05a92d59 561 // the constructor.
562 // Sets the BACKGROUND event, with which the SIGNAL event is to be mixed
563 // Thus we avoid writing (changing) huge and expensive
61e0abb5 564 // backgound files: all output will be writen into SIGNAL, i.e.
565 // opened in constructor file.
566 //
05a92d59 567 // One can open as many files to mix with as one needs.
568 // However only Sdigits with the same name (i.e. constructed with the same SDigitizer)
569 // can be mixed.
61e0abb5 570
05a92d59 571 if( strcmp(GetName(), "") == 0 )
61e0abb5 572 Init() ;
814ad4bf 573
05a92d59 574 if(fManager){
9859bfc0 575 Error("MixWith", "Cannot use this method under AliRunDigitizer") ;
61e0abb5 576 return ;
05a92d59 577 }
88cb7938 578 // looking for file which contains AliRun
579 if (gSystem->AccessPathName(alirunFileName)) {// file does not exist
580 Error("MixWith", "File %s does not exist!", alirunFileName.Data()) ;
581 return ;
61e0abb5 582 }
88cb7938 583 // looking for the file which contains SDigits
5dee926e 584 //AliEMCALGetter * gime = AliEMCALGetter::Instance() ;
585 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
586 TString fileName( emcalLoader->GetSDigitsFileName() ) ;
e191bb57 587 if ( eventFolderName != AliConfig::GetDefaultEventFolderName()) // only if not the default folder name
88cb7938 588 fileName = fileName.ReplaceAll(".root", "") + "_" + eventFolderName + ".root" ;
589 if ( (gSystem->AccessPathName(fileName)) ) {
590 Error("MixWith", "The file %s does not exist!", fileName.Data()) ;
591 return ;
814ad4bf 592 }
88cb7938 593 // need to increase the arrays
594 TString tempo = fInputFileNames[fInput-1] ;
595 delete [] fInputFileNames ;
596 fInputFileNames = new TString[fInput+1] ;
597 fInputFileNames[fInput-1] = tempo ;
598
599 tempo = fEventNames[fInput-1] ;
600 delete [] fEventNames ;
601 fEventNames = new TString[fInput+1] ;
602 fEventNames[fInput-1] = tempo ;
603
604 fInputFileNames[fInput] = alirunFileName ;
605 fEventNames[fInput] = eventFolderName ;
606 fInput++ ;
607}
1963b290 608
609void AliEMCALDigitizer::Print1(Option_t * option)
610{ // 19-nov-04 - just for convinience
611 Print();
612 PrintDigits(option);
613}
614
61e0abb5 615//__________________________________________________________________
eb0b1051 616void AliEMCALDigitizer::Print(Option_t*)const
88cb7938 617{
618 // Print Digitizer's parameters
fdebddeb 619 printf("Print: \n------------------- %s -------------", GetName() ) ;
88cb7938 620 if( strcmp(fEventFolderName.Data(), "") != 0 ){
621 printf(" Writing Digits to branch with title %s\n", fEventFolderName.Data()) ;
622
623 Int_t nStreams ;
624 if (fManager)
625 nStreams = GetNInputStreams() ;
626 else
627 nStreams = fInput ;
628
629 Int_t index = 0 ;
5dee926e 630
631 AliRunLoader *rl=0;
632
88cb7938 633 for (index = 0 ; index < nStreams ; index++) {
634 TString tempo(fEventNames[index]) ;
635 tempo += index ;
5dee926e 636 if ((rl = AliRunLoader::GetRunLoader(tempo)) == 0)
637 rl = AliRunLoader::Open(fInputFileNames[index], tempo) ;
638 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
639 TString fileName( emcalLoader->GetSDigitsFileName() ) ;
e191bb57 640 if ( fEventNames[index] != AliConfig::GetDefaultEventFolderName()) // only if not the default folder name
88cb7938 641 fileName = fileName.ReplaceAll(".root", "") + "_" + fEventNames[index] + ".root" ;
642 printf ("Adding SDigits from %s %s\n", fInputFileNames[index].Data(), fileName.Data()) ;
643 }
5dee926e 644
645 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
646
647 printf("\nWriting digits to %s", emcalLoader->GetDigitsFileName().Data()) ;
88cb7938 648
649 printf("\nWith following parameters:\n") ;
650
651 printf(" Electronics noise in EMC (fPinNoise) = %f\n", fPinNoise) ;
652 printf(" Threshold in EMC (fDigitThreshold) = %f\n", fDigitThreshold) ;
653 printf("---------------------------------------------------\n") ;
ea5f3389 654 }
655 else
fdebddeb 656 printf("Print: AliEMCALDigitizer not initialized") ;
61e0abb5 657}
814ad4bf 658
61e0abb5 659//__________________________________________________________________
660void AliEMCALDigitizer::PrintDigits(Option_t * option){
5dee926e 661 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
662 TClonesArray * digits = emcalLoader->Digits() ;
663 TClonesArray * sdigits = emcalLoader->SDigits() ;
88cb7938 664
1963b290 665 printf("\n #Digits: %d : sdigits %d ", digits->GetEntriesFast(), sdigits->GetEntriesFast()) ;
5dee926e 666 printf("\n event %d", emcalLoader->GetRunLoader()->GetEventNumber());
88cb7938 667
668 if(strstr(option,"all")){
61e0abb5 669 //loop over digits
670 AliEMCALDigit * digit;
88cb7938 671 printf("\nEMC digits (with primaries):\n") ;
672 printf("\n Id Amplitude Time Index Nprim: Primaries list \n") ;
61e0abb5 673 Int_t index ;
88cb7938 674 for (index = 0 ; index < digits->GetEntries() ; index++) {
675 digit = dynamic_cast<AliEMCALDigit *>(digits->At(index)) ;
676 printf("\n%6d %8d %6.5e %4d %2d : ",
11f9c5ff 677 digit->GetId(), digit->GetAmp(), digit->GetTime(), digit->GetIndexInList(), digit->GetNprimary()) ;
61e0abb5 678 Int_t iprimary;
9859bfc0 679 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++) {
88cb7938 680 printf("%d ",digit->GetPrimary(iprimary+1) ) ;
9859bfc0 681 }
682 }
61e0abb5 683 }
1963b290 684 printf("\n");
61e0abb5 685}
05a92d59 686
687//__________________________________________________________________
688Float_t AliEMCALDigitizer::TimeOfNoise(void)
04f0bda3 689{
690 // Calculates the time signal generated by noise
26a2ef9d 691 //PH Info("TimeOfNoise", "Change me") ;
04f0bda3 692 return gRandom->Rndm() * 1.28E-5;
05a92d59 693}
694
88cb7938 695//__________________________________________________________________
696void AliEMCALDigitizer::Unload()
697{
fdebddeb 698 // Unloads the SDigits and Digits
5dee926e 699 AliRunLoader *rl=0;
700
88cb7938 701 Int_t i ;
702 for(i = 1 ; i < fInput ; i++){
703 TString tempo(fEventNames[i]) ;
5dee926e 704 tempo += i;
705 if ((rl = AliRunLoader::GetRunLoader(tempo)))
706 rl->GetDetectorLoader("EMCAL")->UnloadSDigits() ;
88cb7938 707 }
5dee926e 708 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
709 emcalLoader->UnloadDigits() ;
88cb7938 710}
711
814ad4bf 712//_________________________________________________________________________________________
9e5d2067 713void AliEMCALDigitizer::WriteDigits()
814ad4bf 714{
61e0abb5 715
814ad4bf 716 // Makes TreeD in the output file.
717 // Check if branch already exists:
718 // if yes, exit without writing: ROOT TTree does not support overwriting/updating of
719 // already existing branches.
720 // else creates branch with Digits, named "EMCAL", title "...",
721 // and branch "AliEMCALDigitizer", with the same title to keep all the parameters
722 // and names of files, from which digits are made.
61e0abb5 723
5dee926e 724 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
cf24bdc4 725
5dee926e 726 const TClonesArray * digits = emcalLoader->Digits() ;
727 TTree * treeD = emcalLoader->TreeD();
728 if ( !treeD ) {
729 emcalLoader->MakeDigitsContainer();
730 treeD = emcalLoader->TreeD();
731 }
88cb7938 732
733 // -- create Digits branch
734 Int_t bufferSize = 32000 ;
5dee926e 735 TBranch * digitsBranch = 0;
736 if ((digitsBranch = treeD->GetBranch("EMCAL")))
737 digitsBranch->SetAddress(&digits);
738 else
739 treeD->Branch("EMCAL","TClonesArray",&digits,bufferSize);
740 //digitsBranch->SetTitle(fEventFolderName);
741 treeD->Fill() ;
88cb7938 742
5dee926e 743 emcalLoader->WriteDigits("OVERWRITE");
744 emcalLoader->WriteDigitizer("OVERWRITE");
88cb7938 745
746 Unload() ;
747
814ad4bf 748}
61e0abb5 749
1963b290 750void AliEMCALDigitizer::Browse(TBrowser* b)
751{
752 if(fHists) b->Add(fHists);
753 TTask::Browse(b);
754}
755
eb0b1051 756TList *AliEMCALDigitizer::BookControlHists(int var)
1963b290 757{ // 22-nov-04
758 Info("BookControlHists"," started ");
759 gROOT->cd();
5dee926e 760 const AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance();
1963b290 761 if(var>=1){
762 new TH1F("hDigiN", "#EMCAL digits with fAmp > fDigitThreshold",
763 fNADCEC+1, -0.5, Double_t(fNADCEC));
5dee926e 764 new TH1F("HDigiSumEnergy","Sum.EMCAL energy from digi", 1000, 0.0, 200.);
1963b290 765 new TH1F("hDigiAmp", "EMCAL digital amplitude", fNADCEC+1, -0.5, Double_t(fNADCEC));
766 new TH1F("hDigiEnergy","EMCAL cell energy", 2000, 0.0, 200.);
767 new TH1F("hDigiAbsId","EMCAL absId cells with fAmp > fDigitThreshold ",
768 geom->GetNCells(), 0.5, Double_t(geom->GetNCells())+0.5);
769 }
315d1c64 770 fHists = AliEMCALHistoUtilities::MoveHistsToList("EmcalDigiControlHists", kFALSE);
5dee926e 771 fHists = 0;
1963b290 772 return fHists;
773}
774
775void AliEMCALDigitizer::SaveHists(const char* name, Bool_t kSingleKey, const char* opt)
776{
315d1c64 777 AliEMCALHistoUtilities::SaveListOfHists(fHists, name, kSingleKey, opt);
1963b290 778}