coding convention
[u/mrichter/AliRoot.git] / PHOS / AliPHOSSDigitizer.cxx
CommitLineData
990119d6 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
c093f031 16
990119d6 17/* $Id$ */
18
19//_________________________________________________________________________
7acf6008 20// This is a TTask that makes SDigits out of Hits
7b7c1533 21// The name of the TTask is also the title of the branch that will contain
22// the created SDigits
23// The title of the TTAsk is the name of the file that contains the hits from
24// which the SDigits are created
7acf6008 25// A Summable Digits is the sum of all hits originating
26// from one primary in one active cell
27// A threshold for assignment of the primary to SDigit is applied
28// SDigits are written to TreeS, branch "PHOS"
29// AliPHOSSDigitizer with all current parameters is written
30// to TreeS branch "AliPHOSSDigitizer".
f035f6ce 31// Both branches have the same title. If necessary one can produce
32// another set of SDigits with different parameters. Two versions
33// can be distunguished using titles of the branches.
34// User case:
a4e98857 35// root [0] AliPHOSSDigitizer * s = new AliPHOSSDigitizer("galice.root")
36// Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
37// root [1] s->ExecuteTask()
f035f6ce 38// // Makes SDigitis for all events stored in galice.root
a4e98857 39// root [2] s->SetPedestalParameter(0.001)
f035f6ce 40// // One can change parameters of digitization
3de072dc 41// root [3] s->SetSDigitsBranch("Pedestal 0.001")
f035f6ce 42// // and write them into the new branch
3de072dc 43// root [4] s->ExecuteTask("deb all tim")
f035f6ce 44// // available parameters:
45// deb - print # of produced SDigitis
46// deb all - print # and list of produced SDigits
47// tim - print benchmarking information
990119d6 48//
49//*-- Author : Dmitri Peressounko (SUBATECH & KI)
50//////////////////////////////////////////////////////////////////////////////
51
f035f6ce 52
990119d6 53// --- ROOT system ---
7acf6008 54#include "TBenchmark.h"
e957fea8 55#include "TRandom.h"
106fc2fa 56
990119d6 57// --- Standard library ---
58
59// --- AliRoot header files ---
e957fea8 60#include "AliPHOSGeometry.h"
990119d6 61#include "AliPHOSDigit.h"
7b7c1533 62#include "AliPHOSGetter.h"
990119d6 63#include "AliPHOSHit.h"
990119d6 64#include "AliPHOSSDigitizer.h"
65
990119d6 66ClassImp(AliPHOSSDigitizer)
67
68
69//____________________________________________________________________________
88cb7938 70 AliPHOSSDigitizer::AliPHOSSDigitizer():TTask("","")
548f0134 71{
990119d6 72 // ctor
92f521a9 73 InitParameters() ;
74 fDefaultInit = kTRUE ;
990119d6 75}
7acf6008 76
990119d6 77//____________________________________________________________________________
88cb7938 78AliPHOSSDigitizer::AliPHOSSDigitizer(const char * alirunFileName, const char * eventFolderName):
79 TTask("PHOS"+AliConfig::fgkSDigitizerTaskName, alirunFileName),
80 fEventFolderName(eventFolderName)
990119d6 81{
88cb7938 82
990119d6 83 // ctor
8d0f3f77 84 InitParameters() ;
2bd5457f 85 Init();
92f521a9 86 fDefaultInit = kFALSE ;
990119d6 87}
88
c093f031 89//____________________________________________________________________________
a8c47ab6 90AliPHOSSDigitizer::AliPHOSSDigitizer(const AliPHOSSDigitizer & sd)
91 : TTask(sd)
92{
bdc147a9 93 //cpy ctor
94
95 fA = sd.fA ;
96 fB = sd.fB ;
97 fPrimThreshold = sd.fPrimThreshold ;
98 fSDigitsInRun = sd.fSDigitsInRun ;
88cb7938 99 SetName(sd.GetName()) ;
100 SetTitle(sd.GetTitle()) ;
101 fEventFolderName = sd.fEventFolderName;
bdc147a9 102}
103
7b7c1533 104
7acf6008 105//____________________________________________________________________________
a4e98857 106void AliPHOSSDigitizer::Init()
107{
88cb7938 108 // Uses the getter to access the required files
7b7c1533 109
88cb7938 110 fInit = kTRUE ;
111
112 AliPHOSGetter * gime = AliPHOSGetter::Instance(GetTitle(), fEventFolderName.Data());
7b7c1533 113 if ( gime == 0 ) {
88cb7938 114 Fatal("Init" ,"Could not obtain the Getter object for file %s and event %s !", GetTitle(), fEventFolderName.Data()) ;
7b7c1533 115 return ;
116 }
117
88cb7938 118 TString opt("SDigits") ;
119 if(gime->VersionExists(opt) ) {
120 Error( "Init", "Give a version name different from %s", fEventFolderName.Data() ) ;
121 fInit = kFALSE ;
fbf811ec 122 }
123
88cb7938 124 gime->PostSDigitizer(this);
125 gime->PhosLoader()->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
126
7acf6008 127}
7b7c1533 128
8d0f3f77 129//____________________________________________________________________________
130void AliPHOSSDigitizer::InitParameters()
131{
88cb7938 132 // initializes the parameters for digitization
8d0f3f77 133 fA = 0;
134 fB = 10000000.;
135 fPrimThreshold = 0.01 ;
136 fSDigitsInRun = 0 ;
8d0f3f77 137}
138
990119d6 139//____________________________________________________________________________
a4e98857 140void AliPHOSSDigitizer::Exec(Option_t *option)
141{
142 // Collects all hits in the same active volume into digit
7b7c1533 143
144 if (strstr(option, "print") ) {
88cb7938 145 Print() ;
7b7c1533 146 return ;
147 }
990119d6 148
7acf6008 149 if(strstr(option,"tim"))
150 gBenchmark->Start("PHOSSDigitizer");
fbf811ec 151
88cb7938 152 AliPHOSGetter * gime = AliPHOSGetter::Instance() ;
153
154 //switch off reloading of this task while getting event
155 if (!fInit) { // to prevent overwrite existing file
156 Error( "Exec", "Give a version name different from %s", fEventFolderName.Data() ) ;
157 return ;
158 }
159
160
fbf811ec 161 Int_t nevents = gime->MaxEvent() ;
990119d6 162 Int_t ievent ;
7b7c1533 163 for(ievent = 0; ievent < nevents; ievent++){
88cb7938 164
5ad40cd6 165 gime->Event(ievent,"H") ;
88cb7938 166
167 TTree * treeS = gime->TreeS();
168 TClonesArray * hits = gime->Hits() ;
169 TClonesArray * sdigits = gime->SDigits() ;
7b7c1533 170 sdigits->Clear();
990119d6 171 Int_t nSdigits = 0 ;
7b7c1533 172 //Now make SDigits from hits, for PHOS it is the same, so just copy
88cb7938 173 Int_t nPrim = static_cast<Int_t>((gime->TreeH())->GetEntries()) ;
fbf811ec 174 // Attention nPrim is the number of primaries tracked by Geant
175 // and this number could be different to the number of Primaries in TreeK;
176 Int_t iprim ;
88cb7938 177
fbf811ec 178 for (iprim = 0 ; iprim < nPrim ; iprim ++) {
3962b6a1 179 //=========== Get the PHOS branch from Hits Tree for the Primary iprim
180 gime->Track(iprim) ;
990119d6 181 Int_t i;
7b7c1533 182 for ( i = 0 ; i < hits->GetEntries() ; i++ ) {
7a9d98f9 183 AliPHOSHit * hit = dynamic_cast<AliPHOSHit *>(hits->At(i)) ;
990119d6 184 // Assign primary number only if contribution is significant
9688c1dd 185
990119d6 186 if( hit->GetEnergy() > fPrimThreshold)
9688c1dd 187 new((*sdigits)[nSdigits]) AliPHOSDigit(hit->GetPrimary(),hit->GetId(),
fbf811ec 188 Digitize(hit->GetEnergy()), hit->GetTime()) ;
990119d6 189 else
9688c1dd 190 new((*sdigits)[nSdigits]) AliPHOSDigit( -1 , hit->GetId(),
fbf811ec 191 Digitize(hit->GetEnergy()), hit->GetTime()) ;
9688c1dd 192 nSdigits++ ;
990119d6 193
9688c1dd 194 }
88cb7938 195
3962b6a1 196 } // loop over iprim
88cb7938 197
7b7c1533 198 sdigits->Sort() ;
88cb7938 199
7b7c1533 200 nSdigits = sdigits->GetEntriesFast() ;
88cb7938 201
69866bef 202 fSDigitsInRun += nSdigits ;
7b7c1533 203 sdigits->Expand(nSdigits) ;
88cb7938 204
3962b6a1 205 Int_t i ;
990119d6 206 for (i = 0 ; i < nSdigits ; i++) {
7a9d98f9 207 AliPHOSDigit * digit = dynamic_cast<AliPHOSDigit *>(sdigits->At(i)) ;
990119d6 208 digit->SetIndexInList(i) ;
209 }
3962b6a1 210
fbf811ec 211 //Now write SDigits
fbf811ec 212
88cb7938 213
7acf6008 214 //First list of sdigits
88cb7938 215
fbf811ec 216 Int_t bufferSize = 32000 ;
88cb7938 217 TBranch * sdigitsBranch = treeS->Branch("PHOS",&sdigits,bufferSize);
218
fbf811ec 219 sdigitsBranch->Fill() ;
fbf811ec 220
88cb7938 221 gime->WriteSDigits("OVERWRITE");
222
223 //Next - SDigitizer
224
225 gime->WriteSDigitizer("OVERWRITE");
226
7acf6008 227 if(strstr(option,"deb"))
228 PrintSDigits(option) ;
990119d6 229 }
fbf811ec 230
88cb7938 231 Unload();
232
233 gime->PhosLoader()->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
234
7acf6008 235 if(strstr(option,"tim")){
236 gBenchmark->Stop("PHOSSDigitizer");
21cd0c07 237 Info("Exec"," took %f seconds for SDigitizing %f seconds per event",
238 gBenchmark->GetCpuTime("PHOSSDigitizer"), gBenchmark->GetCpuTime("PHOSSDigitizer")/nevents) ;
7acf6008 239 }
990119d6 240}
106fc2fa 241
990119d6 242//__________________________________________________________________
88cb7938 243void AliPHOSSDigitizer::Print()const
a4e98857 244{
245 // Prints parameters of SDigitizer
88cb7938 246 Info("Print", "\n------------------- %s -------------", GetName() ) ;
247 printf(" Writing SDigits to branch with title %s\n", fEventFolderName.Data()) ;
248 printf(" with digitization parameters A = %f\n", fA) ;
249 printf(" B = %f\n", fB) ;
250 printf(" Threshold for Primary assignment= %f\n", fPrimThreshold) ;
251 printf("---------------------------------------------------\n") ;
7acf6008 252
990119d6 253}
548f0134 254
990119d6 255//__________________________________________________________________
a4e98857 256Bool_t AliPHOSSDigitizer::operator==( AliPHOSSDigitizer const &sd )const
257{
3de072dc 258 // Equal operator.
259 // SDititizers are equal if their pedestal, slope and threshold are equal
260
990119d6 261 if( (fA==sd.fA)&&(fB==sd.fB)&&(fPrimThreshold==sd.fPrimThreshold))
262 return kTRUE ;
263 else
264 return kFALSE ;
265}
548f0134 266
a6eedfad 267//__________________________________________________________________
a4e98857 268void AliPHOSSDigitizer::PrintSDigits(Option_t * option)
269{
270 // Prints list of digits produced in the current pass of AliPHOSDigitizer
7b7c1533 271
a6eedfad 272
88cb7938 273 AliPHOSGetter * gime = AliPHOSGetter::Instance() ;
274 const TClonesArray * sdigits = gime->SDigits() ;
7b7c1533 275
21cd0c07 276 TString message ;
277 message = "\nAliPHOSSDigitiser: event " ;
278 message += gAlice->GetEvNumber();
279 message += "\n Number of entries in SDigits list " ;
280 message += sdigits->GetEntriesFast() ;
11f9c5ff 281 char * tempo = new char[8192];
21cd0c07 282
a6eedfad 283 if(strstr(option,"all")||strstr(option,"EMC")){
7acf6008 284
285 //loop over digits
286 AliPHOSDigit * digit;
21cd0c07 287 message += "\nEMC sdigits\n" ;
11f9c5ff 288 message += "\n Id Amplitude Time Index Nprim: Primaries list \n" ;
a6eedfad 289 Int_t maxEmc = gime->PHOSGeometry()->GetNModules()*gime->PHOSGeometry()->GetNCristalsInModule() ;
7acf6008 290 Int_t index ;
a6eedfad 291 for (index = 0 ; (index < sdigits->GetEntriesFast()) &&
fbf811ec 292 ((dynamic_cast<AliPHOSDigit *> (sdigits->At(index)))->GetId() <= maxEmc) ; index++) {
293 digit = dynamic_cast<AliPHOSDigit *>( sdigits->At(index) ) ;
21cd0c07 294 if(digit->GetNprimary() == 0)
295 continue;
11f9c5ff 296 sprintf(tempo, "\n%6d %8d %6.5e %4d %2d :",
297 digit->GetId(), digit->GetAmp(), digit->GetTime(), digit->GetIndexInList(), digit->GetNprimary()) ;
298 message += tempo ;
7acf6008 299 Int_t iprimary;
11f9c5ff 300 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++) {
301 sprintf(tempo, "%d ",digit->GetPrimary(iprimary+1) ) ;
302 message += tempo ;
303 }
a6eedfad 304 }
a6eedfad 305 }
306
307 if(strstr(option,"all")||strstr(option,"CPV")){
7acf6008 308
a6eedfad 309 //loop over CPV digits
310 AliPHOSDigit * digit;
21cd0c07 311
312 message += "CPV sdigits\n" ;
11f9c5ff 313 message += "\n Id Amplitude Index Nprim: Primaries list \n" ;
a6eedfad 314 Int_t maxEmc = gime->PHOSGeometry()->GetNModules()*gime->PHOSGeometry()->GetNCristalsInModule() ;
315 Int_t index ;
316 for (index = 0 ; index < sdigits->GetEntriesFast(); index++) {
fbf811ec 317 digit = dynamic_cast<AliPHOSDigit *>( sdigits->At(index) ) ;
a6eedfad 318 if(digit->GetId() > maxEmc){
11f9c5ff 319 sprintf(tempo, "\n%6d %8d %4d %2d :",
320 digit->GetId(), digit->GetAmp(), digit->GetIndexInList(), digit->GetNprimary()) ;
321 message += tempo ;
a6eedfad 322 Int_t iprimary;
21cd0c07 323 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++) {
11f9c5ff 324 sprintf(tempo, "%d ",digit->GetPrimary(iprimary+1) ) ;
325 message += tempo ;
21cd0c07 326 }
a6eedfad 327 }
328 }
7acf6008 329 }
88cb7938 330 delete []tempo ;
21cd0c07 331 Info("PrintSDigits", message.Data() ) ;
7acf6008 332}
7b7c1533 333
334//____________________________________________________________________________
88cb7938 335void AliPHOSSDigitizer::Unload() const
7b7c1533 336{
e957fea8 337 // Unloads the objects from the folder
88cb7938 338 AliPHOSGetter * gime = AliPHOSGetter::Instance() ;
339 AliPHOSLoader * loader = gime->PhosLoader() ;
340 loader->UnloadHits() ;
341 loader->UnloadSDigits() ;
7b7c1533 342}