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