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