fCalcMass not copied in ctor. with TParticle parameter
[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 ---
52#include "TFile.h"
53#include "TTask.h"
54#include "TTree.h"
55#include "TSystem.h"
56#include "TROOT.h"
57#include "TFolder.h"
58#include "TBenchmark.h"
05a92d59 59#include "TGeometry.h"
60
61e0abb5 61// --- Standard library ---
70479d0e 62#include <Riostream.h>
61e0abb5 63
64// --- AliRoot header files ---
65#include "AliRun.h"
106fc2fa 66#include "AliHeader.h"
61e0abb5 67#include "AliEMCALDigit.h"
61e0abb5 68#include "AliEMCALGeometry.h"
814ad4bf 69#include "AliEMCALGetter.h"
05a92d59 70#include "AliEMCALHit.h"
71#include "AliEMCALSDigitizer.h"
61e0abb5 72
73ClassImp(AliEMCALSDigitizer)
61e0abb5 74
75//____________________________________________________________________________
76 AliEMCALSDigitizer::AliEMCALSDigitizer():TTask("AliEMCALSDigitizer","")
77{
78 // ctor
839828a6 79 InitParameters() ;
92f521a9 80 fDefaultInit = kTRUE ;
61e0abb5 81}
82
83//____________________________________________________________________________
05a92d59 84AliEMCALSDigitizer::AliEMCALSDigitizer(const char* headerFile, const char *sDigitsTitle, const Bool_t toSplit):
85TTask(sDigitsTitle, headerFile)
61e0abb5 86{
87 // ctor
839828a6 88 InitParameters() ;
05a92d59 89 fToSplit = toSplit ;
814ad4bf 90 Init();
92f521a9 91 fDefaultInit = kFALSE ;
61e0abb5 92}
93
94//____________________________________________________________________________
95AliEMCALSDigitizer::~AliEMCALSDigitizer()
96{
97 // dtor
839828a6 98 fSplitFile = 0 ;
61e0abb5 99}
36657114 100
61e0abb5 101//____________________________________________________________________________
102void AliEMCALSDigitizer::Init(){
814ad4bf 103 // Initialization: open root-file, allocate arrays for hits and sdigits,
36657114 104 // attach task SDigitizer to the list of EMCAL tasks
814ad4bf 105 //
106 // Initialization can not be done in the default constructor
107 //============================================================= YS
108 // The initialisation is now done by the getter
61e0abb5 109
36657114 110 if( strcmp(GetTitle(), "") == 0 )
814ad4bf 111 SetTitle("galice.root") ;
afa0c708 112
05a92d59 113 AliEMCALGetter * gime = AliEMCALGetter::GetInstance(GetTitle(), GetName(), fToSplit) ;
814ad4bf 114 if ( gime == 0 ) {
05a92d59 115 cerr << "ERROR: AliEMCALSDigitizer::Init -> Could not obtain the Getter object !"
116 << endl ;
814ad4bf 117 return ;
118 }
119
120 gime->PostSDigits( GetName(), GetTitle() ) ;
05a92d59 121
122 fSplitFile = 0 ;
123 if(fToSplit){
124 // construct the name of the file as /path/EMCAL.SDigits.root
125 // First - extract full path if necessary
126 TString sDigitsFileName(GetTitle()) ;
127 Ssiz_t islash = sDigitsFileName.Last('/') ;
128 if(islash<sDigitsFileName.Length())
129 sDigitsFileName.Remove(islash+1,sDigitsFileName.Length()) ;
130 else
131 sDigitsFileName="" ;
173558f2 132
05a92d59 133 // Next - append the file name
134 sDigitsFileName+="EMCAL.SDigits." ;
135 if((strcmp(GetName(),"Default")!=0)&&(strcmp(GetName(),"")!=0)){
136 sDigitsFileName+=GetName() ;
137 sDigitsFileName+="." ;
138 }
139 sDigitsFileName+="root" ;
173558f2 140
05a92d59 141 // Finally - check if the file already opened or open the file
142 fSplitFile = static_cast<TFile*>(gROOT->GetFile(sDigitsFileName.Data()));
143 if(!fSplitFile)
144 fSplitFile = TFile::Open(sDigitsFileName.Data(),"update") ;
145 }
61e0abb5 146
814ad4bf 147 TString sdname(GetName() );
148 sdname.Append(":") ;
149 sdname.Append(GetTitle() ) ;
05a92d59 150 SetName(sdname) ;
814ad4bf 151 gime->PostSDigitizer(this) ;
839828a6 152}
153
154//____________________________________________________________________________
afa0c708 155void AliEMCALSDigitizer::InitParameters()
156{
05a92d59 157 fA = 0;
158 fB = 10000000.;
159 fTowerPrimThreshold = 0.01 ;
839828a6 160 fPreShowerPrimThreshold = 0.0001 ;
05a92d59 161 fPhotonElectronFactor = 5000. ; // photoelectrons per GeV
162 fSplitFile = 0 ;
163 fToSplit = kFALSE ;
839828a6 164}
165
61e0abb5 166//____________________________________________________________________________
afa0c708 167void AliEMCALSDigitizer::Exec(Option_t *option)
168{
169 // Collects all hits in the same active volume into digit
173558f2 170
814ad4bf 171 if( strcmp(GetName(), "") == 0 )
172 Init() ;
61e0abb5 173
814ad4bf 174 if (strstr(option, "print") ) {
175 Print("") ;
176 return ;
177 }
61e0abb5 178
814ad4bf 179 if(strstr(option,"tim"))
180 gBenchmark->Start("EMCALSDigitizer");
97f3344e 181
814ad4bf 182 //Check, if this branch already exits
05a92d59 183 AliEMCALGetter * gime = AliEMCALGetter::GetInstance() ;
184 if(gime->BranchExists("SDigits") )
185 return;
186
97f3344e 187 TString sdname(GetName()) ;
188 sdname.Remove(sdname.Index(GetTitle())-1) ;
189
05a92d59 190 Int_t nevents = gime->MaxEvent() ;
814ad4bf 191 Int_t ievent ;
afa0c708 192
193 for(ievent = 0; ievent < nevents; ievent++){
194 gime->Event(ievent,"H") ;
195
196 const TClonesArray * hits = gime->Hits() ;
197
814ad4bf 198 TClonesArray * sdigits = gime->SDigits(sdname.Data()) ;
199 sdigits->Clear();
61e0abb5 200 Int_t nSdigits = 0 ;
814ad4bf 201
05a92d59 202 //Collects all hits in the same active volume into digit
61e0abb5 203
05a92d59 204 //Now make SDigits from hits, for EMCAL it is the same, so just copy
205 Int_t nPrim = static_cast<Int_t>((gAlice->TreeH())->GetEntries()) ;
206 // Attention nPrim is the number of primaries tracked by Geant
207 // and this number could be different to the number of Primaries in TreeK;
208 Int_t iprim ;
209 for ( iprim = 0 ; iprim < nPrim ; iprim++ ) {
210 //=========== Get the EMCAL branch from Hits Tree for the Primary iprim
211 gime->Track(iprim) ;
212 Int_t i;
213 for ( i = 0 ; i < hits->GetEntries() ; i++ ) {
214 AliEMCALHit * hit = dynamic_cast<AliEMCALHit*>(hits->At(i)) ;
215 AliEMCALDigit * curSDigit = 0 ;
216 AliEMCALDigit * sdigit = 0 ;
217 Bool_t newsdigit = kTRUE;
218 // Assign primary number only if deposited energy is significant
e1f60236 219
05a92d59 220 if( (!hit->IsInPreShower() && hit->GetEnergy() > fTowerPrimThreshold) ||
221 (hit->IsInPreShower() && hit->GetEnergy() > fPreShowerPrimThreshold))
222 curSDigit = new AliEMCALDigit( hit->GetPrimary(),
223 hit->GetIparent(),Layer2TowerID(hit->GetId(),hit->IsInPreShower()),
224 Digitize(hit->GetEnergy()), hit->GetTime() ) ;
225 else
226 curSDigit = new AliEMCALDigit( -1 ,
227 -1 ,
228 Layer2TowerID(hit->GetId(),hit->IsInPreShower()),
229 Digitize(hit->GetEnergy()), hit->GetTime() ) ;
230 Int_t check = 0 ;
231 for(check= 0; check < nSdigits ; check++) {
232 sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(check)) ;
233 if( sdigit->GetId() == curSDigit->GetId()) { // Are we in the same tower or the same preshower ?
234 *sdigit = *sdigit + *curSDigit;
e1f60236 235 newsdigit = kFALSE;
05a92d59 236 }
e1f60236 237 }
05a92d59 238 if (newsdigit) {
239 new((*sdigits)[nSdigits]) AliEMCALDigit(*curSDigit);
240 nSdigits++ ;
241 }
afa0c708 242 delete curSDigit ;
05a92d59 243 } // loop over all hits (hit = deposited energy/layer/entering particle)
244 } // loop over iprim
e1f60236 245
814ad4bf 246 sdigits->Sort() ;
61e0abb5 247
814ad4bf 248 nSdigits = sdigits->GetEntriesFast() ;
05a92d59 249 fSDigitsInRun += nSdigits ;
250 sdigits->Expand(nSdigits) ;
106fc2fa 251
05a92d59 252 const AliEMCALGeometry * geom = gime->EMCALGeometry() ;
253
afa0c708 254 if (nSdigits != 0 ) {
255 Int_t lastPreShowerIndex = nSdigits - 1 ;
256
257
258 if (!(dynamic_cast<AliEMCALDigit *>(sdigits->At(lastPreShowerIndex))->IsInPreShower()))
259
260 lastPreShowerIndex = -2;
261
262 Int_t firstPreShowerIndex = 100000 ;
263 Int_t index ;
264 AliEMCALDigit * sdigit = 0 ;
265
266 for ( index = 0; index < nSdigits ; index++) {
267 sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(index) ) ;
268 if (sdigit->IsInPreShower() ){
269 firstPreShowerIndex = index ;
270 break ;
271 }
106fc2fa 272 }
273
afa0c708 274 AliEMCALDigit * preshower ;
275 AliEMCALDigit * tower ;
276 Int_t lastIndex = lastPreShowerIndex +1 ;
277
278 for (index = firstPreShowerIndex ; index <= lastPreShowerIndex; index++) {
279 preshower = dynamic_cast<AliEMCALDigit *>(sdigits->At(index) );
280 Bool_t towerFound = kFALSE ;
281 Int_t jndex ;
282 for (jndex = 0; jndex < firstPreShowerIndex; jndex++) {
283 tower = dynamic_cast<AliEMCALDigit *>(sdigits->At(jndex) );
284 if ( (preshower->GetId() - (geom->GetNZ() * geom->GetNPhi()) ) == tower->GetId() ) {
285 Float_t towerEnergy = static_cast<Float_t>(tower->GetAmp()) ;
286 Float_t preshoEnergy = static_cast<Float_t>(preshower->GetAmp()) ;
287 towerEnergy +=preshoEnergy ;
288 *tower = *tower + *preshower ; // and add preshower multiplied by layer ratio to tower
289 tower->SetAmp(static_cast<Int_t>(TMath::Ceil(towerEnergy))) ;
290 towerFound = kTRUE ;
291 }
292 }
293 if ( !towerFound ) {
294 new((*sdigits)[lastIndex]) AliEMCALDigit(*preshower);
295 AliEMCALDigit * temp = dynamic_cast<AliEMCALDigit *>(sdigits->At(lastIndex)) ;
296 temp->SetId(temp->GetId() - (geom->GetNZ() * geom->GetNPhi()) ) ;
297 lastIndex++ ;
106fc2fa 298 }
299 }
afa0c708 300 sdigits->Sort() ;
301 Int_t NPrimarymax = -1 ;
302 Int_t i ;
303 for (i = 0 ; i < sdigits->GetEntriesFast() ; i++) {
304 sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(i)) ;
305 sdigit->SetIndexInList(i) ;
306 }
307
308 for (i = 0 ; i < sdigits->GetEntriesFast() ; i++) {
309 if (((dynamic_cast<AliEMCALDigit *>(sdigits->At(i)))->GetNprimary()) > NPrimarymax)
310 NPrimarymax = ((dynamic_cast<AliEMCALDigit *>(sdigits->At(i)))->GetNprimary()) ;
e1f60236 311 }
05a92d59 312 }
313
05a92d59 314 //Now write SDigits
315
316 if(gAlice->TreeS() == 0 || (fSplitFile)) //<--- To be checked: we should not create TreeS if it is already here
317 gAlice->MakeTree("S",fSplitFile);
afa0c708 318
05a92d59 319 if(fSplitFile)
320 fSplitFile->cd() ;
321
814ad4bf 322 //First list of sdigits
323 Int_t bufferSize = 32000 ;
324 TBranch * sdigitsBranch = gAlice->TreeS()->Branch("EMCAL",&sdigits,bufferSize);
325 sdigitsBranch->SetTitle(sdname);
814ad4bf 326
05a92d59 327 //NEXT - SDigitizer
814ad4bf 328 Int_t splitlevel = 0 ;
329 AliEMCALSDigitizer * sd = this ;
330 TBranch * sdigitizerBranch = gAlice->TreeS()->Branch("AliEMCALSDigitizer","AliEMCALSDigitizer",
331 &sd,bufferSize,splitlevel);
332 sdigitizerBranch->SetTitle(sdname);
814ad4bf 333
36657114 334 sdigitsBranch->Fill() ;
335 sdigitizerBranch->Fill() ;
106fc2fa 336 gAlice->TreeS()->AutoSave() ;
05a92d59 337
814ad4bf 338 if(strstr(option,"deb"))
339 PrintSDigits(option) ;
340
61e0abb5 341 }
05a92d59 342
61e0abb5 343 if(strstr(option,"tim")){
344 gBenchmark->Stop("EMCALSDigitizer");
345 cout << "AliEMCALSDigitizer:" << endl ;
346 cout << " took " << gBenchmark->GetCpuTime("EMCALSDigitizer") << " seconds for SDigitizing "
05a92d59 347 << gBenchmark->GetCpuTime("EMCALSDigitizer") << " seconds per event " << endl ;
61e0abb5 348 cout << endl ;
05a92d59 349 }
61e0abb5 350}
05a92d59 351
61e0abb5 352//__________________________________________________________________
353void AliEMCALSDigitizer::SetSDigitsBranch(const char * title ){
814ad4bf 354
355 // Setting title to branch SDigits
356
357 TString stitle(title) ;
358
359 // check if branch with title already exists
360 TBranch * sdigitsBranch =
361 static_cast<TBranch*>(gAlice->TreeS()->GetListOfBranches()->FindObject("EMCAL")) ;
362 TBranch * sdigitizerBranch =
363 static_cast<TBranch*>(gAlice->TreeS()->GetListOfBranches()->FindObject("AliEMCALSDigitizer")) ;
364 const char * sdigitsTitle = sdigitsBranch ->GetTitle() ;
365 const char * sdigitizerTitle = sdigitizerBranch ->GetTitle() ;
366 if ( stitle.CompareTo(sdigitsTitle)==0 || stitle.CompareTo(sdigitizerTitle)==0 ){
367 cerr << "ERROR: AliEMCALSdigitizer::SetSDigitsBranch -> Cannot overwrite existing branch with title " << title << endl ;
368 return ;
369 }
370
371 cout << "AliEMCALSdigitizer::SetSDigitsBranch -> Changing SDigits file from " << GetName() << " to " << title << endl ;
372
373 SetName(title) ;
374
375 // Post to the WhiteBoard
376 AliEMCALGetter * gime = AliEMCALGetter::GetInstance() ;
377 gime->PostSDigits( title, GetTitle()) ;
61e0abb5 378}
106fc2fa 379
106fc2fa 380
61e0abb5 381//__________________________________________________________________
05a92d59 382void AliEMCALSDigitizer::Print(Option_t* option)const
383{
384 // Prints parameters of SDigitizer
385
61e0abb5 386 cout << "------------------- "<< GetName() << " -------------" << endl ;
814ad4bf 387 cout << " Writing SDigitis to branch with title " << GetName() << endl ;
89e103bd 388 cout << " with digitization parameters A = " << fA << endl ;
389 cout << " B = " << fB << endl ;
390 cout << " Threshold for Primary assignment in Tower = " << fTowerPrimThreshold << endl ;
391 cout << " Threshold for Primary assignment in PreShower = " << fPreShowerPrimThreshold << endl ;
61e0abb5 392 cout << "---------------------------------------------------"<<endl ;
393
394}
05a92d59 395
61e0abb5 396//__________________________________________________________________
05a92d59 397Bool_t AliEMCALSDigitizer::operator==( AliEMCALSDigitizer const &sd )const
398{
399 // Equal operator.
400 // SDititizers are equal if their pedestal, slope and threshold are equal
401
89e103bd 402 if( (fA==sd.fA)&&(fB==sd.fB)&&
403 (fTowerPrimThreshold==sd.fTowerPrimThreshold) &&
404 (fPreShowerPrimThreshold==sd.fPreShowerPrimThreshold))
61e0abb5 405 return kTRUE ;
406 else
407 return kFALSE ;
408}
173558f2 409
61e0abb5 410//__________________________________________________________________
411void AliEMCALSDigitizer::PrintSDigits(Option_t * option){
412 //Prints list of digits produced at the current pass of AliEMCALDigitizer
413
814ad4bf 414 AliEMCALGetter * gime = AliEMCALGetter::GetInstance() ;
415 TString sdname(GetName()) ;
416 sdname.Remove(sdname.Index(GetTitle())-1) ;
417 const TClonesArray * sdigits = gime->SDigits(sdname.Data()) ;
61e0abb5 418
814ad4bf 419 cout << "AliEMCALSDigitiser: event " << gAlice->GetEvNumber() << endl ;
420 cout << " Number of entries in SDigits list " << sdigits->GetEntriesFast() << endl ;
421 cout << endl ;
422 if(strstr(option,"all")||strstr(option,"EMC")){
423
61e0abb5 424 //loop over digits
425 AliEMCALDigit * digit;
e1f60236 426 cout << "SDigit Id " << " Amplitude " << " Time " << " Index " << " Nprim " << " Primaries list " << endl;
61e0abb5 427 Int_t index ;
814ad4bf 428 for (index = 0 ; index < sdigits->GetEntries() ; index++) {
05a92d59 429 digit = dynamic_cast<AliEMCALDigit *>( sdigits->At(index) ) ;
814ad4bf 430 cout << setw(6) << digit->GetId() << " " << setw(10) << digit->GetAmp() << " " << digit->GetTime()
431 << setw(6) << digit->GetIndexInList() << " "
432 << setw(5) << digit->GetNprimary() <<" ";
61e0abb5 433
434 Int_t iprimary;
435 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++)
814ad4bf 436 cout << " " << digit->GetPrimary(iprimary+1) << " ";
61e0abb5 437 cout << endl;
438 }
814ad4bf 439 cout <<endl;
61e0abb5 440 }
441}
05a92d59 442
814ad4bf 443//________________________________________________________________________
05a92d59 444const Int_t AliEMCALSDigitizer::Layer2TowerID(Int_t ihit, Bool_t preshower)
445{
229f77c4 446 // Method to Transform from Hit Id to Digit Id
447 // This function should be one to one
814ad4bf 448 AliEMCALGetter * gime = AliEMCALGetter::GetInstance() ;
449 const AliEMCALGeometry * geom = gime->EMCALGeometry();
f063936c 450 Int_t ieta = ((ihit-1)/geom->GetNPhi())%geom->GetNZ(); // eta Tower Index
229f77c4 451 Int_t iphi = (ihit-1)%(geom->GetNPhi())+1; //phi Tower Index
452 Int_t it = -10;
453 Int_t ipre = 0;
814ad4bf 454
229f77c4 455 if (preshower)ipre = 1;
456 if (iphi > 0 && ieta >= 0){
457 it = iphi+ieta*geom->GetNPhi() + ipre*geom->GetNPhi()*geom->GetNZ();
458 return it;
459 }else{
460 cerr << " AliEMCALSDigitizer::Layer2TowerID() -- there is an error "<< endl << "Eta number = "
461 << ieta << "Phi number = " << iphi << endl ;
462 return it;
463 } // end if iphi>0 && ieta>0
814ad4bf 464}
229f77c4 465//_______________________________________________________________________________________
89e103bd 466// void AliEMCALSDigitizer::TestTowerID(void)
467// {
468// Int_t j;
229f77c4 469
89e103bd 470// Bool_t preshower = kFALSE;
471// for (j = 0 ; j < 10 ; j++){ // loop over hit id
472// Int_t i;
473// for (i = 0 ; i <= 2 ; i++){ // loop over
474// Int_t k = i*96*144+j*144+1;
475// cout << " Hit Index = " << k << " " << j*10 << " TOWERID = " << Layer2TowerID(k, preshower) << endl ;
476// }
477// }
478// }
05a92d59 479
480//____________________________________________________________________________
481void AliEMCALSDigitizer::UseHitsFrom(const char * filename)
482{
483 SetTitle(filename) ;
484 Init() ;
485}