AliFstream is moved to RAW package in order to avoid dependencies
[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 ---
bda7cf56 52#include <TBenchmark.h>
53#include <TH1.h>
54#include <TBrowser.h>
55#include <Riostream.h>
56#include <TMath.h>
05a92d59 57
61e0abb5 58// --- Standard library ---
d64c959b 59#include "stdlib.h"
61e0abb5 60
61// --- AliRoot header files ---
13e1db42 62#include "AliLog.h"
5dee926e 63#include "AliRunLoader.h"
64#include "AliStack.h"
61e0abb5 65#include "AliEMCALDigit.h"
5dee926e 66#include "AliEMCALLoader.h"
05a92d59 67#include "AliEMCALHit.h"
68#include "AliEMCALSDigitizer.h"
88cb7938 69#include "AliEMCALGeometry.h"
315d1c64 70#include "AliEMCALHistoUtilities.h"
61e0abb5 71
72ClassImp(AliEMCALSDigitizer)
61e0abb5 73
74//____________________________________________________________________________
88cb7938 75 AliEMCALSDigitizer::AliEMCALSDigitizer():TTask("","")
61e0abb5 76{
77 // ctor
1963b290 78 fFirstEvent = fLastEvent = fControlHists = 0;
92f521a9 79 fDefaultInit = kTRUE ;
1963b290 80 fHists = 0;
61e0abb5 81}
82
83//____________________________________________________________________________
e191bb57 84AliEMCALSDigitizer::AliEMCALSDigitizer(const char * alirunFileName,
85 const char * eventFolderName):
86 TTask("EMCAL"+AliConfig::Instance()->GetSDigitizerTaskName(), alirunFileName),
88cb7938 87 fEventFolderName(eventFolderName)
61e0abb5 88{
89 // ctor
1963b290 90 fFirstEvent = fLastEvent = fControlHists = 0 ; // runs one event by defaut
814ad4bf 91 Init();
62741b41 92 InitParameters() ;
92f521a9 93 fDefaultInit = kFALSE ;
1963b290 94 fHists = 0;
e52475ed 95 fControlHists = 1;
96 if(fControlHists) BookControlHists(1);
61e0abb5 97}
98
88cb7938 99
61e0abb5 100//____________________________________________________________________________
9e5d2067 101AliEMCALSDigitizer::AliEMCALSDigitizer(const AliEMCALSDigitizer & sd) : TTask(sd) {
88cb7938 102 //cpy ctor
103
5082063d 104 fFirstEvent = sd.fFirstEvent ;
105 fLastEvent = sd.fLastEvent ;
88cb7938 106 fA = sd.fA ;
107 fB = sd.fB ;
88cb7938 108 fECPrimThreshold = sd.fECPrimThreshold ;
88cb7938 109 fSDigitsInRun = sd.fSDigitsInRun ;
110 SetName(sd.GetName()) ;
111 SetTitle(sd.GetTitle()) ;
112 fEventFolderName = sd.fEventFolderName;
61e0abb5 113}
36657114 114
88cb7938 115
61e0abb5 116//____________________________________________________________________________
797e311f 117AliEMCALSDigitizer::~AliEMCALSDigitizer() {
14ce0a6e 118 //dtor
5dee926e 119 AliLoader *emcalLoader=0;
120 if ((emcalLoader = AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL")))
121 emcalLoader->CleanSDigitizer();
797e311f 122}
123
124//____________________________________________________________________________
61e0abb5 125void AliEMCALSDigitizer::Init(){
814ad4bf 126 // Initialization: open root-file, allocate arrays for hits and sdigits,
36657114 127 // attach task SDigitizer to the list of EMCAL tasks
814ad4bf 128 //
129 // Initialization can not be done in the default constructor
130 //============================================================= YS
131 // The initialisation is now done by the getter
61e0abb5 132
88cb7938 133 fInit = kTRUE ;
afa0c708 134
5dee926e 135 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
136
137 if ( emcalLoader == 0 ) {
138 Fatal("Init", "Could not obtain the AliEMCALLoader");
814ad4bf 139 return ;
140 }
141
5dee926e 142 emcalLoader->PostSDigitizer(this);
143 emcalLoader->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
88cb7938 144
839828a6 145}
146
147//____________________________________________________________________________
afa0c708 148void AliEMCALSDigitizer::InitParameters()
149{
14ce0a6e 150 //initialize parameters for sdigitization
151
5dee926e 152 const AliEMCALGeometry * geom = AliEMCALGeometry::GetInstance();
62741b41 153 if (geom->GetSampling() == 0.) {
5082063d 154 Fatal("InitParameters", "Sampling factor not set !") ;
62741b41 155 }
1963b290 156 // Initializes parameters
157 fA = 0;
158 fB = 1.e+7;
159 fSampling = geom->GetSampling();
160
161 // threshold for deposit energy of hit
162 fECPrimThreshold = 0.; // 24-nov-04 - was 1.e-6;
e52475ed 163 Print("");
839828a6 164}
165
61e0abb5 166//____________________________________________________________________________
afa0c708 167void AliEMCALSDigitizer::Exec(Option_t *option)
168{
fdebddeb 169 // Collects all hit of the same tower into digits
1963b290 170 TString o(option); o.ToUpper();
814ad4bf 171 if (strstr(option, "print") ) {
d64c959b 172 Print() ;
814ad4bf 173 return ;
174 }
61e0abb5 175
814ad4bf 176 if(strstr(option,"tim"))
177 gBenchmark->Start("EMCALSDigitizer");
97f3344e 178
5dee926e 179 AliRunLoader *rl = AliRunLoader::GetRunLoader();
180 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
13e1db42 181
88cb7938 182 //switch off reloading of this task while getting event
183 if (!fInit) { // to prevent overwrite existing file
13e1db42 184 AliError( Form("Give a version name different from %s", fEventFolderName.Data()) ) ;
88cb7938 185 return ;
fdebddeb 186 }
187
5082063d 188 if (fLastEvent == -1)
5dee926e 189 fLastEvent = rl->GetNumberOfEvents() - 1 ;
1963b290 190 else {
5dee926e 191 fLastEvent = TMath::Min(fLastEvent, rl->GetNumberOfEvents()-1);
1963b290 192 }
5082063d 193 Int_t nEvents = fLastEvent - fFirstEvent + 1;
194
1963b290 195 Int_t ievent;
196 Float_t energy=0.; // de * fSampling - 23-nov-04
5dee926e 197 rl->LoadKinematics();
198 rl->LoadHits("EMCAL");
199
cf24bdc4 200 for (ievent = fFirstEvent; ievent <= fLastEvent; ievent++) {
5dee926e 201 rl->GetEvent(ievent);
202 TTree * treeS = emcalLoader->TreeS();
203 if ( !treeS ) {
204 emcalLoader->MakeSDigitsContainer();
205 treeS = emcalLoader->TreeS();
206 }
207 TClonesArray * hits = emcalLoader->Hits() ;
208 TClonesArray * sdigits = emcalLoader->SDigits() ;
814ad4bf 209 sdigits->Clear();
61e0abb5 210 Int_t nSdigits = 0 ;
05a92d59 211 //Now make SDigits from hits, for EMCAL it is the same, so just copy
5dee926e 212 Int_t nPrim = static_cast<Int_t>((emcalLoader->TreeH())->GetEntries()) ;
213 // This is not true: there is only one list of hits (MvL jan 2006)
05a92d59 214 // Attention nPrim is the number of primaries tracked by Geant
5dee926e 215 // and this number could be different to the number of Primaries in TreeK;
05a92d59 216 Int_t iprim ;
62741b41 217 for ( iprim = 0 ; iprim < nPrim ; iprim++ ) {
218 //=========== Get the EMCAL branch from Hits Tree for the Primary iprim
5dee926e 219 rl->Stack()->Particle(iprim) ;
62741b41 220 Int_t i;
5dee926e 221 AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance();
62741b41 222 for ( i = 0 ; i < hits->GetEntries() ; i++ ) {
223 AliEMCALHit * hit = dynamic_cast<AliEMCALHit*>(hits->At(i)) ;
224 AliEMCALDigit * curSDigit = 0 ;
225 AliEMCALDigit * sdigit = 0 ;
226 Bool_t newsdigit = kTRUE;
227
1963b290 228 // hit->GetId() - Absolute Id number EMCAL segment
229 if(geom->CheckAbsCellId(hit->GetId())) { // was IsInECA(hit->GetId())
230 energy = hit->GetEnergy() * fSampling; // 23-nov-04
231 if(energy > fECPrimThreshold )
62741b41 232 // Assign primary number only if deposited energy is significant
62741b41 233 curSDigit = new AliEMCALDigit( hit->GetPrimary(),
234 hit->GetIparent(), hit->GetId(),
1963b290 235 Digitize(energy), hit->GetTime() ) ;
62741b41 236 else
237 curSDigit = new AliEMCALDigit( -1 ,
238 -1 ,
239 hit->GetId(),
1963b290 240 Digitize(energy), hit->GetTime() ) ;
241 } else {
242 Warning("Exec"," abs id %i is bad \n", hit->GetId());
243 newsdigit = kFALSE;
5dee926e 244 curSDigit = 0;
1963b290 245 }
fdebddeb 246
5dee926e 247 if(curSDigit != 0){
248 for(Int_t check= 0; check < nSdigits ; check++) {
249 sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(check)) ;
250
fdebddeb 251 if( sdigit->GetId() == curSDigit->GetId()) { // Are we in the same ECAL tower ?
5dee926e 252 *sdigit = *sdigit + *curSDigit;
253 newsdigit = kFALSE;
fdebddeb 254 }
afa0c708 255 }
afa0c708 256 }
fdebddeb 257 if (newsdigit) {
62741b41 258 new((*sdigits)[nSdigits]) AliEMCALDigit(*curSDigit);
259 nSdigits++ ;
e1f60236 260 }
62741b41 261 delete curSDigit ;
fdebddeb 262 } // loop over all hits (hit = deposited energy/entering particle)
62741b41 263 } // loop over iprim
62741b41 264 sdigits->Sort() ;
265
266 nSdigits = sdigits->GetEntriesFast() ;
267 fSDigitsInRun += nSdigits ;
62741b41 268
d64c959b 269 Int_t nPrimarymax = -1 ;
62741b41 270 Int_t i ;
1963b290 271 Double_t e=0.,esum=0.;
315d1c64 272 AliEMCALHistoUtilities::FillH1(fHists, 0, double(sdigits->GetEntriesFast()));
62741b41 273 for (i = 0 ; i < sdigits->GetEntriesFast() ; i++) {
274 AliEMCALDigit * sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(i)) ;
275 sdigit->SetIndexInList(i) ;
1963b290 276
315d1c64 277 AliEMCALHistoUtilities::FillH1(fHists, 2, double(sdigit->GetAmp()));
1963b290 278 e = double(Calibrate(sdigit->GetAmp()));
279 esum += e;
315d1c64 280 AliEMCALHistoUtilities::FillH1(fHists, 3, e);
281 AliEMCALHistoUtilities::FillH1(fHists, 4, double(sdigit->GetId()));
62741b41 282 }
315d1c64 283 if(esum>0.) AliEMCALHistoUtilities::FillH1(fHists, 1, esum);
62741b41 284
1963b290 285 for (i = 0 ; i < sdigits->GetEntriesFast() ; i++) { // for what
d64c959b 286 if (((dynamic_cast<AliEMCALDigit *>(sdigits->At(i)))->GetNprimary()) > nPrimarymax)
287 nPrimarymax = ((dynamic_cast<AliEMCALDigit *>(sdigits->At(i)))->GetNprimary()) ;
62741b41 288 }
5dee926e 289
88cb7938 290 // Now write SDigits
88cb7938 291
62741b41 292 Int_t bufferSize = 32000 ;
5dee926e 293 TBranch * sdigitsBranch = treeS->GetBranch("EMCAL");
294 if (sdigitsBranch)
295 sdigitsBranch->SetAddress(&sdigits);
296 else
297 treeS->Branch("EMCAL",&sdigits,bufferSize);
88cb7938 298
5dee926e 299 treeS->Fill();
300
301 emcalLoader->WriteSDigits("OVERWRITE");
62741b41 302
303 //NEXT - SDigitizer
5dee926e 304 emcalLoader->WriteSDigitizer("OVERWRITE"); // why in event cycle ?
62741b41 305
306 if(strstr(option,"deb"))
307 PrintSDigits(option) ;
1963b290 308 }
88cb7938 309
310 Unload();
311
5dee926e 312 emcalLoader->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
88cb7938 313
61e0abb5 314 if(strstr(option,"tim")){
9859bfc0 315 gBenchmark->Stop("EMCALSDigitizer");
1963b290 316 printf("\n Exec: took %f seconds for SDigitizing %f seconds per event\n",
5082063d 317 gBenchmark->GetCpuTime("EMCALSDigitizer"), gBenchmark->GetCpuTime("EMCALSDigitizer")/nEvents ) ;
62741b41 318 }
61e0abb5 319}
05a92d59 320
bda7cf56 321//__________________________________________________________________
322
323Int_t AliEMCALSDigitizer::Digitize(Float_t energy)const {
324 // Digitize the energy
325 Double_t aSignal = fA + energy*fB;
326 if (TMath::Abs(aSignal)>2147483647.0) {
327 //PH 2147483647 is the max. integer
328 //PH This apparently is a problem which needs investigation
329 AliWarning(Form("Too big or too small energy %f",aSignal));
330 aSignal = TMath::Sign((Double_t)2147483647,aSignal);
331 }
332 return (Int_t ) aSignal;
333 }
334
335
336//__________________________________________________________________
106fc2fa 337
1963b290 338void AliEMCALSDigitizer::Print1(Option_t * option)
339{
340 Print();
341 PrintSDigits(option);
342}
343
61e0abb5 344//__________________________________________________________________
5dee926e 345void AliEMCALSDigitizer::Print(Option_t *option) const
05a92d59 346{
347 // Prints parameters of SDigitizer
e52475ed 348 printf("Print: \n------------------- %s ------------- option %s\n", GetName() , option) ;
1963b290 349 printf(" fInit %i\n", int(fInit));
350 printf(" fFirstEvent %i\n", fFirstEvent);
351 printf(" fLastEvent %i\n", fLastEvent);
88cb7938 352 printf(" Writing SDigits to branch with title %s\n", fEventFolderName.Data()) ;
1963b290 353 printf(" with digitization parameters A = %f\n", fA) ;
354 printf(" B = %f\n", fB) ;
355 printf(" Threshold for EC Primary assignment = %f\n", fECPrimThreshold) ;
356 printf(" Sampling = %f\n", fSampling);
88cb7938 357 printf("---------------------------------------------------\n") ;
61e0abb5 358}
05a92d59 359
61e0abb5 360//__________________________________________________________________
05a92d59 361Bool_t AliEMCALSDigitizer::operator==( AliEMCALSDigitizer const &sd )const
362{
363 // Equal operator.
364 // SDititizers are equal if their pedestal, slope and threshold are equal
89e103bd 365 if( (fA==sd.fA)&&(fB==sd.fB)&&
fdebddeb 366 (fECPrimThreshold==sd.fECPrimThreshold))
61e0abb5 367 return kTRUE ;
368 else
369 return kFALSE ;
370}
173558f2 371
61e0abb5 372//__________________________________________________________________
d64c959b 373void AliEMCALSDigitizer::PrintSDigits(Option_t * option)
374{
61e0abb5 375 //Prints list of digits produced at the current pass of AliEMCALDigitizer
376
d64c959b 377
5dee926e 378 // AliEMCALGetter * gime = AliEMCALGetter::Instance() ;
379 AliEMCALLoader *rl = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
380 const TClonesArray * sdigits = rl->SDigits() ;
61e0abb5 381
fdebddeb 382 printf("\n") ;
5dee926e 383 printf("event %i", rl->GetRunLoader()->GetEventNumber());
1963b290 384 printf(" Number of entries in SDigits list %i", sdigits->GetEntriesFast());
814ad4bf 385 if(strstr(option,"all")||strstr(option,"EMC")){
9859bfc0 386
61e0abb5 387 //loop over digits
388 AliEMCALDigit * digit;
fdebddeb 389 printf("\n Id Amplitude Time Index Nprim: Primaries list \n") ;
1963b290 390 Int_t index, isum=0;
11f9c5ff 391 char * tempo = new char[8192];
814ad4bf 392 for (index = 0 ; index < sdigits->GetEntries() ; index++) {
05a92d59 393 digit = dynamic_cast<AliEMCALDigit *>( sdigits->At(index) ) ;
11f9c5ff 394 sprintf(tempo, "\n%6d %8d %6.5e %4d %2d :",
395 digit->GetId(), digit->GetAmp(), digit->GetTime(), digit->GetIndexInList(), digit->GetNprimary()) ;
1963b290 396 printf(tempo);
397 isum += digit->GetAmp();
61e0abb5 398
399 Int_t iprimary;
9859bfc0 400 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++) {
11f9c5ff 401 sprintf(tempo, "%d ",digit->GetPrimary(iprimary+1) ) ;
fdebddeb 402 printf(tempo);
9859bfc0 403 }
61e0abb5 404 }
11f9c5ff 405 delete tempo ;
1963b290 406 printf("\n** Sum %i : %10.3f GeV/c **\n ", isum, double(isum)*1.e-6);
407 } else printf("\n");
61e0abb5 408}
05a92d59 409
05a92d59 410//____________________________________________________________________________
88cb7938 411void AliEMCALSDigitizer::Unload() const
05a92d59 412{
d64c959b 413 // Unload Hits and SDigits from the folder
5dee926e 414 AliEMCALLoader *rl = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
415 rl->UnloadHits() ;
416 rl->UnloadSDigits() ;
05a92d59 417}
1963b290 418
419void AliEMCALSDigitizer::Browse(TBrowser* b)
420{
421 if(fHists) b->Add(fHists);
422 TTask::Browse(b);
423}
424
eb0b1051 425TList *AliEMCALSDigitizer::BookControlHists(int var)
14ce0a6e 426{
427 //book histograms for monitoring sdigitization
428 // 22-nov-04
1963b290 429 gROOT->cd();
5dee926e 430 const AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance() ;
1963b290 431 if(var>=1){
e52475ed 432 printf(" AliEMCALSDigitizer::BookControlHists() in action \n");
1963b290 433 new TH1F("HSDigiN", "#EMCAL sdigits ", 1001, -0.5, 1000.5);
434 new TH1F("HSDigiSumEnergy","Sum.EMCAL energy", 1000, 0.0, 100.);
435 new TH1F("HSDigiAmp", "EMCAL sdigits amplitude", 1000, 0., 2.e+9);
436 new TH1F("HSDigiEnergy","EMCAL cell energy", 1000, 0.0, 100.);
437 new TH1F("HSDigiAbsId","EMCAL absID for sdigits",
438 geom->GetNCells(), 0.5, Double_t(geom->GetNCells())+0.5);
439 }
e52475ed 440
315d1c64 441 fHists = AliEMCALHistoUtilities::MoveHistsToList("EmcalSDigiControlHists", kFALSE);
5dee926e 442 fHists = 0;
e52475ed 443
1963b290 444 return fHists;
445}
446
447void AliEMCALSDigitizer::SaveHists(const char* name, Bool_t kSingleKey, const char* opt)
448{
315d1c64 449 AliEMCALHistoUtilities::SaveListOfHists(fHists, name, kSingleKey, opt);
1963b290 450}