removing jetfinder classes from main body of EMCAL code to subdirectory
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALSDigitizer.cxx
CommitLineData
ffa6d63b 1/*************************************************************************
61e0abb5 2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16/* $Id$ */
17
18//_________________________________________________________________________
19// This is a TTask that makes SDigits out of Hits
20// A Summable Digits is the sum of all hits originating
ffa6d63b 21// from one in one tower of the EMCAL
61e0abb5 22// A threshold for assignment of the primary to SDigit is applied
23// SDigits are written to TreeS, branch "EMCAL"
24// AliEMCALSDigitizer with all current parameters is written
25// to TreeS branch "AliEMCALSDigitizer".
26// Both branches have the same title. If necessary one can produce
27// another set of SDigits with different parameters. Two versions
28// can be distunguished using titles of the branches.
29// User case:
30// root [0] AliEMCALSDigitizer * s = new AliEMCALSDigitizer("galice.root")
31// Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
32// root [1] s->ExecuteTask()
33// // Makes SDigitis for all events stored in galice.root
34// root [2] s->SetPedestalParameter(0.001)
35// // One can change parameters of digitization
36// root [3] s->SetSDigitsBranch("Redestal 0.001")
37// // and write them into the new branch
38// root [4] s->ExeciteTask("deb all tim")
39// // available parameters:
40// deb - print # of produced SDigitis
41// deb all - print # and list of produced SDigits
42// tim - print benchmarking information
43//
ffa6d63b 44//*-- Author : Sahal Yacoob (LBL)
45// based on : AliPHOSSDigitzer
05a92d59 46// Modif:
47// August 2002 Yves Schutz: clone PHOS as closely as possible and intoduction
48// of new IO (à la PHOS)
61e0abb5 49//////////////////////////////////////////////////////////////////////////////
50
61e0abb5 51// --- ROOT system ---
61e0abb5 52#include "TBenchmark.h"
1963b290 53#include "TH1.h"
54#include "TBrowser.h"
5dee926e 55#include "Riostream.h"
1963b290 56//#include "TObjectTable.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"
1963b290 70#include "AliEMCALJetMicroDst.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;
61e0abb5 95}
96
88cb7938 97
61e0abb5 98//____________________________________________________________________________
9e5d2067 99AliEMCALSDigitizer::AliEMCALSDigitizer(const AliEMCALSDigitizer & sd) : TTask(sd) {
88cb7938 100 //cpy ctor
101
5082063d 102 fFirstEvent = sd.fFirstEvent ;
103 fLastEvent = sd.fLastEvent ;
88cb7938 104 fA = sd.fA ;
105 fB = sd.fB ;
88cb7938 106 fECPrimThreshold = sd.fECPrimThreshold ;
88cb7938 107 fSDigitsInRun = sd.fSDigitsInRun ;
108 SetName(sd.GetName()) ;
109 SetTitle(sd.GetTitle()) ;
110 fEventFolderName = sd.fEventFolderName;
61e0abb5 111}
36657114 112
88cb7938 113
61e0abb5 114//____________________________________________________________________________
797e311f 115AliEMCALSDigitizer::~AliEMCALSDigitizer() {
5dee926e 116 AliLoader *emcalLoader=0;
117 if ((emcalLoader = AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL")))
118 emcalLoader->CleanSDigitizer();
797e311f 119}
120
121//____________________________________________________________________________
61e0abb5 122void AliEMCALSDigitizer::Init(){
814ad4bf 123 // Initialization: open root-file, allocate arrays for hits and sdigits,
36657114 124 // attach task SDigitizer to the list of EMCAL tasks
814ad4bf 125 //
126 // Initialization can not be done in the default constructor
127 //============================================================= YS
128 // The initialisation is now done by the getter
61e0abb5 129
88cb7938 130 fInit = kTRUE ;
afa0c708 131
5dee926e 132 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
133
134 if ( emcalLoader == 0 ) {
135 Fatal("Init", "Could not obtain the AliEMCALLoader");
814ad4bf 136 return ;
137 }
138
5dee926e 139 emcalLoader->PostSDigitizer(this);
140 emcalLoader->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
88cb7938 141
839828a6 142}
143
144//____________________________________________________________________________
afa0c708 145void AliEMCALSDigitizer::InitParameters()
146{
5dee926e 147 const AliEMCALGeometry * geom = AliEMCALGeometry::GetInstance();
62741b41 148 if (geom->GetSampling() == 0.) {
5082063d 149 Fatal("InitParameters", "Sampling factor not set !") ;
62741b41 150 }
1963b290 151 // Initializes parameters
152 fA = 0;
153 fB = 1.e+7;
154 fSampling = geom->GetSampling();
155
156 // threshold for deposit energy of hit
157 fECPrimThreshold = 0.; // 24-nov-04 - was 1.e-6;
839828a6 158}
159
61e0abb5 160//____________________________________________________________________________
afa0c708 161void AliEMCALSDigitizer::Exec(Option_t *option)
162{
fdebddeb 163 // Collects all hit of the same tower into digits
1963b290 164 TString o(option); o.ToUpper();
814ad4bf 165 if (strstr(option, "print") ) {
d64c959b 166 Print() ;
814ad4bf 167 return ;
168 }
61e0abb5 169
814ad4bf 170 if(strstr(option,"tim"))
171 gBenchmark->Start("EMCALSDigitizer");
97f3344e 172
5dee926e 173 AliRunLoader *rl = AliRunLoader::GetRunLoader();
174 AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
13e1db42 175
88cb7938 176 //switch off reloading of this task while getting event
177 if (!fInit) { // to prevent overwrite existing file
13e1db42 178 AliError( Form("Give a version name different from %s", fEventFolderName.Data()) ) ;
88cb7938 179 return ;
fdebddeb 180 }
181
5082063d 182 if (fLastEvent == -1)
5dee926e 183 fLastEvent = rl->GetNumberOfEvents() - 1 ;
1963b290 184 else {
5dee926e 185 fLastEvent = TMath::Min(fLastEvent, rl->GetNumberOfEvents()-1);
1963b290 186 }
5082063d 187 Int_t nEvents = fLastEvent - fFirstEvent + 1;
188
1963b290 189 Int_t ievent;
190 Float_t energy=0.; // de * fSampling - 23-nov-04
5dee926e 191 rl->LoadKinematics();
192 rl->LoadHits("EMCAL");
193
cf24bdc4 194 for (ievent = fFirstEvent; ievent <= fLastEvent; ievent++) {
5dee926e 195 rl->GetEvent(ievent);
196 TTree * treeS = emcalLoader->TreeS();
197 if ( !treeS ) {
198 emcalLoader->MakeSDigitsContainer();
199 treeS = emcalLoader->TreeS();
200 }
201 TClonesArray * hits = emcalLoader->Hits() ;
202 TClonesArray * sdigits = emcalLoader->SDigits() ;
814ad4bf 203 sdigits->Clear();
61e0abb5 204 Int_t nSdigits = 0 ;
05a92d59 205 //Now make SDigits from hits, for EMCAL it is the same, so just copy
5dee926e 206 Int_t nPrim = static_cast<Int_t>((emcalLoader->TreeH())->GetEntries()) ;
207 // This is not true: there is only one list of hits (MvL jan 2006)
05a92d59 208 // Attention nPrim is the number of primaries tracked by Geant
5dee926e 209 // and this number could be different to the number of Primaries in TreeK;
05a92d59 210 Int_t iprim ;
62741b41 211 for ( iprim = 0 ; iprim < nPrim ; iprim++ ) {
212 //=========== Get the EMCAL branch from Hits Tree for the Primary iprim
5dee926e 213 rl->Stack()->Particle(iprim) ;
62741b41 214 Int_t i;
5dee926e 215 AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance();
62741b41 216 for ( i = 0 ; i < hits->GetEntries() ; i++ ) {
217 AliEMCALHit * hit = dynamic_cast<AliEMCALHit*>(hits->At(i)) ;
218 AliEMCALDigit * curSDigit = 0 ;
219 AliEMCALDigit * sdigit = 0 ;
220 Bool_t newsdigit = kTRUE;
221
1963b290 222 // hit->GetId() - Absolute Id number EMCAL segment
223 if(geom->CheckAbsCellId(hit->GetId())) { // was IsInECA(hit->GetId())
224 energy = hit->GetEnergy() * fSampling; // 23-nov-04
225 if(energy > fECPrimThreshold )
62741b41 226 // Assign primary number only if deposited energy is significant
62741b41 227 curSDigit = new AliEMCALDigit( hit->GetPrimary(),
228 hit->GetIparent(), hit->GetId(),
1963b290 229 Digitize(energy), hit->GetTime() ) ;
62741b41 230 else
231 curSDigit = new AliEMCALDigit( -1 ,
232 -1 ,
233 hit->GetId(),
1963b290 234 Digitize(energy), hit->GetTime() ) ;
235 } else {
236 Warning("Exec"," abs id %i is bad \n", hit->GetId());
237 newsdigit = kFALSE;
5dee926e 238 curSDigit = 0;
1963b290 239 }
fdebddeb 240
5dee926e 241 if(curSDigit != 0){
242 for(Int_t check= 0; check < nSdigits ; check++) {
243 sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(check)) ;
244
fdebddeb 245 if( sdigit->GetId() == curSDigit->GetId()) { // Are we in the same ECAL tower ?
5dee926e 246 *sdigit = *sdigit + *curSDigit;
247 newsdigit = kFALSE;
fdebddeb 248 }
afa0c708 249 }
afa0c708 250 }
fdebddeb 251 if (newsdigit) {
62741b41 252 new((*sdigits)[nSdigits]) AliEMCALDigit(*curSDigit);
253 nSdigits++ ;
e1f60236 254 }
62741b41 255 delete curSDigit ;
fdebddeb 256 } // loop over all hits (hit = deposited energy/entering particle)
62741b41 257 } // loop over iprim
62741b41 258 sdigits->Sort() ;
259
260 nSdigits = sdigits->GetEntriesFast() ;
261 fSDigitsInRun += nSdigits ;
62741b41 262
d64c959b 263 Int_t nPrimarymax = -1 ;
62741b41 264 Int_t i ;
1963b290 265 Double_t e=0.,esum=0.;
266 sv::FillH1(fHists, 0, double(sdigits->GetEntriesFast()));
62741b41 267 for (i = 0 ; i < sdigits->GetEntriesFast() ; i++) {
268 AliEMCALDigit * sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(i)) ;
269 sdigit->SetIndexInList(i) ;
1963b290 270
271 sv::FillH1(fHists, 2, double(sdigit->GetAmp()));
272 e = double(Calibrate(sdigit->GetAmp()));
273 esum += e;
274 sv::FillH1(fHists, 3, e);
275 sv::FillH1(fHists, 4, double(sdigit->GetId()));
62741b41 276 }
1963b290 277 if(esum>0.) sv::FillH1(fHists, 1, esum);
62741b41 278
1963b290 279 for (i = 0 ; i < sdigits->GetEntriesFast() ; i++) { // for what
d64c959b 280 if (((dynamic_cast<AliEMCALDigit *>(sdigits->At(i)))->GetNprimary()) > nPrimarymax)
281 nPrimarymax = ((dynamic_cast<AliEMCALDigit *>(sdigits->At(i)))->GetNprimary()) ;
62741b41 282 }
5dee926e 283
88cb7938 284 // Now write SDigits
88cb7938 285
62741b41 286 Int_t bufferSize = 32000 ;
5dee926e 287 TBranch * sdigitsBranch = treeS->GetBranch("EMCAL");
288 if (sdigitsBranch)
289 sdigitsBranch->SetAddress(&sdigits);
290 else
291 treeS->Branch("EMCAL",&sdigits,bufferSize);
88cb7938 292
5dee926e 293 treeS->Fill();
294
295 emcalLoader->WriteSDigits("OVERWRITE");
62741b41 296
297 //NEXT - SDigitizer
5dee926e 298 emcalLoader->WriteSDigitizer("OVERWRITE"); // why in event cycle ?
62741b41 299
300 if(strstr(option,"deb"))
301 PrintSDigits(option) ;
1963b290 302 }
88cb7938 303
304 Unload();
305
5dee926e 306 emcalLoader->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
88cb7938 307
61e0abb5 308 if(strstr(option,"tim")){
9859bfc0 309 gBenchmark->Stop("EMCALSDigitizer");
1963b290 310 printf("\n Exec: took %f seconds for SDigitizing %f seconds per event\n",
5082063d 311 gBenchmark->GetCpuTime("EMCALSDigitizer"), gBenchmark->GetCpuTime("EMCALSDigitizer")/nEvents ) ;
62741b41 312 }
61e0abb5 313}
05a92d59 314
106fc2fa 315
1963b290 316void AliEMCALSDigitizer::Print1(Option_t * option)
317{
318 Print();
319 PrintSDigits(option);
320}
321
61e0abb5 322//__________________________________________________________________
5dee926e 323void AliEMCALSDigitizer::Print(Option_t *option) const
05a92d59 324{
325 // Prints parameters of SDigitizer
1963b290 326 printf("Print: \n------------------- %s -------------\n", GetName() ) ;
327 printf(" fInit %i\n", int(fInit));
328 printf(" fFirstEvent %i\n", fFirstEvent);
329 printf(" fLastEvent %i\n", fLastEvent);
88cb7938 330 printf(" Writing SDigits to branch with title %s\n", fEventFolderName.Data()) ;
1963b290 331 printf(" with digitization parameters A = %f\n", fA) ;
332 printf(" B = %f\n", fB) ;
333 printf(" Threshold for EC Primary assignment = %f\n", fECPrimThreshold) ;
334 printf(" Sampling = %f\n", fSampling);
88cb7938 335 printf("---------------------------------------------------\n") ;
61e0abb5 336}
05a92d59 337
61e0abb5 338//__________________________________________________________________
05a92d59 339Bool_t AliEMCALSDigitizer::operator==( AliEMCALSDigitizer const &sd )const
340{
341 // Equal operator.
342 // SDititizers are equal if their pedestal, slope and threshold are equal
89e103bd 343 if( (fA==sd.fA)&&(fB==sd.fB)&&
fdebddeb 344 (fECPrimThreshold==sd.fECPrimThreshold))
61e0abb5 345 return kTRUE ;
346 else
347 return kFALSE ;
348}
173558f2 349
61e0abb5 350//__________________________________________________________________
d64c959b 351void AliEMCALSDigitizer::PrintSDigits(Option_t * option)
352{
61e0abb5 353 //Prints list of digits produced at the current pass of AliEMCALDigitizer
354
d64c959b 355
5dee926e 356 // AliEMCALGetter * gime = AliEMCALGetter::Instance() ;
357 AliEMCALLoader *rl = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
358 const TClonesArray * sdigits = rl->SDigits() ;
61e0abb5 359
fdebddeb 360 printf("\n") ;
5dee926e 361 printf("event %i", rl->GetRunLoader()->GetEventNumber());
1963b290 362 printf(" Number of entries in SDigits list %i", sdigits->GetEntriesFast());
814ad4bf 363 if(strstr(option,"all")||strstr(option,"EMC")){
9859bfc0 364
61e0abb5 365 //loop over digits
366 AliEMCALDigit * digit;
fdebddeb 367 printf("\n Id Amplitude Time Index Nprim: Primaries list \n") ;
1963b290 368 Int_t index, isum=0;
11f9c5ff 369 char * tempo = new char[8192];
814ad4bf 370 for (index = 0 ; index < sdigits->GetEntries() ; index++) {
05a92d59 371 digit = dynamic_cast<AliEMCALDigit *>( sdigits->At(index) ) ;
11f9c5ff 372 sprintf(tempo, "\n%6d %8d %6.5e %4d %2d :",
373 digit->GetId(), digit->GetAmp(), digit->GetTime(), digit->GetIndexInList(), digit->GetNprimary()) ;
1963b290 374 printf(tempo);
375 isum += digit->GetAmp();
61e0abb5 376
377 Int_t iprimary;
9859bfc0 378 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++) {
11f9c5ff 379 sprintf(tempo, "%d ",digit->GetPrimary(iprimary+1) ) ;
fdebddeb 380 printf(tempo);
9859bfc0 381 }
61e0abb5 382 }
11f9c5ff 383 delete tempo ;
1963b290 384 printf("\n** Sum %i : %10.3f GeV/c **\n ", isum, double(isum)*1.e-6);
385 } else printf("\n");
61e0abb5 386}
05a92d59 387
05a92d59 388//____________________________________________________________________________
88cb7938 389void AliEMCALSDigitizer::Unload() const
05a92d59 390{
d64c959b 391 // Unload Hits and SDigits from the folder
5dee926e 392 AliEMCALLoader *rl = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
393 rl->UnloadHits() ;
394 rl->UnloadSDigits() ;
05a92d59 395}
1963b290 396
397void AliEMCALSDigitizer::Browse(TBrowser* b)
398{
399 if(fHists) b->Add(fHists);
400 TTask::Browse(b);
401}
402
eb0b1051 403TList *AliEMCALSDigitizer::BookControlHists(int var)
1963b290 404{ // 22-nov-04
405 gROOT->cd();
5dee926e 406 const AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance() ;
1963b290 407 if(var>=1){
408 new TH1F("HSDigiN", "#EMCAL sdigits ", 1001, -0.5, 1000.5);
409 new TH1F("HSDigiSumEnergy","Sum.EMCAL energy", 1000, 0.0, 100.);
410 new TH1F("HSDigiAmp", "EMCAL sdigits amplitude", 1000, 0., 2.e+9);
411 new TH1F("HSDigiEnergy","EMCAL cell energy", 1000, 0.0, 100.);
412 new TH1F("HSDigiAbsId","EMCAL absID for sdigits",
413 geom->GetNCells(), 0.5, Double_t(geom->GetNCells())+0.5);
414 }
415 fHists = sv::MoveHistsToList("EmcalSDigiControlHists", kFALSE);
5dee926e 416 fHists = 0;
1963b290 417 return fHists;
418}
419
420void AliEMCALSDigitizer::SaveHists(const char* name, Bool_t kSingleKey, const char* opt)
421{
422 sv::SaveListOfHists(fHists, name, kSingleKey, opt);
423}