Added a protection in the dtor. When the tasks is created by default ctor (to access...
[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 "TFile.h"
990119d6 55#include "TTask.h"
56#include "TTree.h"
57#include "TSystem.h"
7acf6008 58#include "TROOT.h"
59#include "TFolder.h"
60#include "TBenchmark.h"
106fc2fa 61#include "TGeometry.h"
62
990119d6 63// --- Standard library ---
7acf6008 64#include <iomanip.h>
990119d6 65
66// --- AliRoot header files ---
7acf6008 67#include "AliRun.h"
106fc2fa 68#include "AliHeader.h"
990119d6 69#include "AliPHOSDigit.h"
a6eedfad 70#include "AliPHOSGeometry.h"
7b7c1533 71#include "AliPHOSGetter.h"
990119d6 72#include "AliPHOSHit.h"
990119d6 73#include "AliPHOSSDigitizer.h"
74
990119d6 75
76ClassImp(AliPHOSSDigitizer)
77
78
79//____________________________________________________________________________
8d0f3f77 80 AliPHOSSDigitizer::AliPHOSSDigitizer():TTask("","") {
990119d6 81 // ctor
8d0f3f77 82 InitParameters() ;
990119d6 83}
7acf6008 84
990119d6 85//____________________________________________________________________________
7b7c1533 86AliPHOSSDigitizer::AliPHOSSDigitizer(const char * headerFile, const char * sDigitsTitle):TTask(sDigitsTitle, headerFile)
990119d6 87{
88 // ctor
8d0f3f77 89 InitParameters() ;
2bd5457f 90 Init();
990119d6 91}
92
c093f031 93//____________________________________________________________________________
94AliPHOSSDigitizer::~AliPHOSSDigitizer()
95{
79bb1b62 96 // dtor
97 // gime=0 if Digitizer created by default ctor (to get just the parameters)
6f51f90b 98
8d0f3f77 99 AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ;
79bb1b62 100
8d0f3f77 101 if (gime) {
102 // remove the task from the folder list
103 gime->RemoveTask("S",GetName()) ;
104
105 TString name(GetName()) ;
106 if (! name.IsNull() )
107 name.Remove(name.Index(":")) ;
108
109 // remove the Hits from the folder list
110 gime->RemoveObjects("H",name) ;
111
112 // remove the SDigits from the folder list
113 gime->RemoveObjects("S", name) ;
114
115 // Delete gAlice
116 gime->CloseFile() ;
117
118 }
119 fSplitFile = 0 ;
c093f031 120}
7b7c1533 121
7acf6008 122//____________________________________________________________________________
a4e98857 123void AliPHOSSDigitizer::Init()
124{
2bd5457f 125 // Initialization: open root-file, allocate arrays for hits and sdigits,
126 // attach task SDigitizer to the list of PHOS tasks
127 //
a4e98857 128 // Initialization can not be done in the default constructor
7b7c1533 129 //============================================================= YS
130 // The initialisation is now done by AliPHOSGetter
131
132 if( strcmp(GetTitle(), "") == 0 )
133 SetTitle("galice.root") ;
85f78bbc 134
7b7c1533 135 AliPHOSGetter * gime = AliPHOSGetter::GetInstance(GetTitle(), GetName()) ;
136 if ( gime == 0 ) {
137 cerr << "ERROR: AliPHOSSDigitizer::Init -> Could not obtain the Getter object !" << endl ;
138 return ;
139 }
140
5ad40cd6 141 gime->PostSDigits( GetName(), GetTitle() ) ;
142
143 TString sdname(GetName() );
144 sdname.Append(":") ;
145 sdname.Append(GetTitle() ) ;
146 SetName(sdname) ;
147 gime->PostSDigitizer(this) ;
7acf6008 148}
7b7c1533 149
8d0f3f77 150//____________________________________________________________________________
151void AliPHOSSDigitizer::InitParameters()
152{
153 fA = 0;
154 fB = 10000000.;
155 fPrimThreshold = 0.01 ;
156 fSDigitsInRun = 0 ;
157 fSplitFile = 0 ;
158}
159
990119d6 160//____________________________________________________________________________
a4e98857 161void AliPHOSSDigitizer::Exec(Option_t *option)
162{
163 // Collects all hits in the same active volume into digit
7b7c1533 164 if( strcmp(GetName(), "") == 0 )
7acf6008 165 Init() ;
7b7c1533 166
167 if (strstr(option, "print") ) {
168 Print("") ;
169 return ;
170 }
990119d6 171
7acf6008 172 if(strstr(option,"tim"))
173 gBenchmark->Start("PHOSSDigitizer");
7b7c1533 174
175 //Check, if this branch already exits
5ad40cd6 176 gAlice->GetEvent(0) ;
a2d8b8a4 177
178 TString sdname(GetName()) ;
179 sdname.Remove(sdname.Index(GetTitle())-1) ;
180
5ad40cd6 181 if(gAlice->TreeS() ) {
7a9d98f9 182 TObjArray * lob = static_cast<TObjArray*>(gAlice->TreeS()->GetListOfBranches()) ;
5ad40cd6 183 TIter next(lob) ;
184 TBranch * branch = 0 ;
185 Bool_t phosfound = kFALSE, sdigitizerfound = kFALSE ;
7b7c1533 186
7a9d98f9 187 while ( (branch = (static_cast<TBranch*>(next()))) && (!phosfound || !sdigitizerfound) ) {
c093f031 188 TString thisName( GetName() ) ;
189 TString branchName( branch->GetTitle() ) ;
190 branchName.Append(":") ;
191 if ( (strcmp(branch->GetName(), "PHOS")==0) && thisName.BeginsWith(branchName) )
5ad40cd6 192 phosfound = kTRUE ;
c093f031 193 else if ( (strcmp(branch->GetName(), "AliPHOSSDigitizer")==0) && thisName.BeginsWith(branchName) )
5ad40cd6 194 sdigitizerfound = kTRUE ;
195 }
196
197 if ( phosfound || sdigitizerfound ) {
198 cerr << "WARNING: AliPHOSSDigitizer::Exec -> SDigits and/or SDigitizer branch with name " << GetName()
199 << " already exits" << endl ;
200 return ;
201 }
202 }
7b7c1533 203
7b7c1533 204 AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ;
205
7b7c1533 206 Int_t nevents = (Int_t) gAlice->TreeE()->GetEntries() ;
990119d6 207
208 Int_t ievent ;
7b7c1533 209 for(ievent = 0; ievent < nevents; ievent++){
5ad40cd6 210 gime->Event(ievent,"H") ;
017addb3 211 const TClonesArray * hits = gime->Hits() ;
5ad40cd6 212 TClonesArray * sdigits = gime->SDigits(sdname.Data()) ;
7b7c1533 213 sdigits->Clear();
990119d6 214 Int_t nSdigits = 0 ;
215
990119d6 216
7b7c1533 217 //Now make SDigits from hits, for PHOS it is the same, so just copy
c093f031 218
3962b6a1 219 Int_t Nprim = (Int_t) (gAlice->TreeH())->GetEntries();
220 // Attention Nprim is the number of primaries tracked by Geant and this number could be different to the number of Primaries in TreeK;
221 Int_t iprim;
222 for (iprim=0; iprim<Nprim; iprim++) {
223 //=========== Get the PHOS branch from Hits Tree for the Primary iprim
224 gime->Track(iprim) ;
990119d6 225 Int_t i;
7b7c1533 226 for ( i = 0 ; i < hits->GetEntries() ; i++ ) {
7a9d98f9 227 AliPHOSHit * hit = dynamic_cast<AliPHOSHit *>(hits->At(i)) ;
990119d6 228 // Assign primary number only if contribution is significant
9688c1dd 229
990119d6 230 if( hit->GetEnergy() > fPrimThreshold)
9688c1dd 231 new((*sdigits)[nSdigits]) AliPHOSDigit(hit->GetPrimary(),hit->GetId(),
232 Digitize(hit->GetEnergy()), hit->GetTime()) ;
990119d6 233 else
9688c1dd 234 new((*sdigits)[nSdigits]) AliPHOSDigit( -1 , hit->GetId(),
235 Digitize(hit->GetEnergy()), hit->GetTime()) ;
236 nSdigits++ ;
990119d6 237
9688c1dd 238 }
c093f031 239
3962b6a1 240 } // loop over iprim
990119d6 241
7b7c1533 242 sdigits->Sort() ;
990119d6 243
7b7c1533 244 nSdigits = sdigits->GetEntriesFast() ;
69866bef 245 fSDigitsInRun += nSdigits ;
7b7c1533 246 sdigits->Expand(nSdigits) ;
c093f031 247
3962b6a1 248 Int_t i ;
990119d6 249 for (i = 0 ; i < nSdigits ; i++) {
7a9d98f9 250 AliPHOSDigit * digit = dynamic_cast<AliPHOSDigit *>(sdigits->At(i)) ;
990119d6 251 digit->SetIndexInList(i) ;
252 }
3962b6a1 253
106fc2fa 254 if(gAlice->TreeS() == 0)
85f78bbc 255 gAlice->MakeTree("S", fSplitFile);
c093f031 256
7acf6008 257 //First list of sdigits
258 Int_t bufferSize = 32000 ;
7b7c1533 259 TBranch * sdigitsBranch = gAlice->TreeS()->Branch("PHOS",&sdigits,bufferSize);
5ad40cd6 260 sdigitsBranch->SetTitle(sdname);
7acf6008 261
7b7c1533 262 //Next - SDigitizer
7acf6008 263 Int_t splitlevel = 0 ;
264 AliPHOSSDigitizer * sd = this ;
7b7c1533 265 TBranch * sdigitizerBranch = gAlice->TreeS()->Branch("AliPHOSSDigitizer","AliPHOSSDigitizer",
7acf6008 266 &sd,bufferSize,splitlevel);
5ad40cd6 267 sdigitizerBranch->SetTitle(sdname);
c093f031 268
42d2745f 269 sdigitsBranch->Fill() ;
c093f031 270 sdigitizerBranch->Fill() ;
42d2745f 271 gAlice->TreeS()->AutoSave() ;
7acf6008 272
273 if(strstr(option,"deb"))
274 PrintSDigits(option) ;
c093f031 275
990119d6 276 }
c093f031 277
7acf6008 278 if(strstr(option,"tim")){
279 gBenchmark->Stop("PHOSSDigitizer");
280 cout << "AliPHOSSDigitizer:" << endl ;
281 cout << " took " << gBenchmark->GetCpuTime("PHOSSDigitizer") << " seconds for SDigitizing "
7b7c1533 282 << gBenchmark->GetCpuTime("PHOSSDigitizer")/nevents << " seconds per event " << endl ;
7acf6008 283 cout << endl ;
284 }
285
286
990119d6 287}
106fc2fa 288
990119d6 289//__________________________________________________________________
a4e98857 290void AliPHOSSDigitizer::SetSDigitsBranch(const char * title )
291{
292 // Setting title to branch SDigits
7b7c1533 293
294 TString stitle(title) ;
295
296 // check if branch with title already exists
7a9d98f9 297 TBranch * sdigitsBranch =
298 static_cast<TBranch*>(gAlice->TreeS()->GetListOfBranches()->FindObject("PHOS")) ;
299 TBranch * sdigitizerBranch =
300 static_cast<TBranch*>(gAlice->TreeS()->GetListOfBranches()->FindObject("AliPHOSSDigitizer")) ;
7b7c1533 301 const char * sdigitsTitle = sdigitsBranch ->GetTitle() ;
302 const char * sdigitizerTitle = sdigitizerBranch ->GetTitle() ;
303 if ( stitle.CompareTo(sdigitsTitle)==0 || stitle.CompareTo(sdigitizerTitle)==0 ){
304 cerr << "ERROR: AliPHOSSdigitizer::SetSDigitsBranch -> Cannot overwrite existing branch with title " << title << endl ;
305 return ;
306 }
307
308 cout << "AliPHOSSdigitizer::SetSDigitsBranch -> Changing SDigits file from " << GetName() << " to " << title << endl ;
309
310 SetName(title) ;
311
312 // Post to the WhiteBoard
313 AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ;
5ad40cd6 314 gime->PostSDigits( title, GetTitle()) ;
990119d6 315}
7b7c1533 316
106fc2fa 317//__________________________________________________________________
c093f031 318void AliPHOSSDigitizer::SetSplitFile(const TString splitFileName)
106fc2fa 319{
320 // Diverts the SDigits in a file separate from the hits file
321
322 TDirectory * cwd = gDirectory ;
c093f031 323
324 if ( !(gAlice->GetTreeSFileName() == splitFileName) ) {
325 if (gAlice->GetTreeSFile() )
326 gAlice->GetTreeSFile()->Close() ;
327 }
328
329 fSplitFile = gAlice->InitTreeFile("S",splitFileName.Data());
330 fSplitFile->cd() ;
8d0f3f77 331 gAlice->Write(0, TObject::kOverwrite);
106fc2fa 332
333 TTree *treeE = gAlice->TreeE();
334 if (!treeE) {
c093f031 335 cerr << "ERROR: AliPHOSSDigitizer::SetSPlitFile -> No TreeE found "<<endl;
106fc2fa 336 abort() ;
337 }
338
339 // copy TreeE
c093f031 340 AliHeader *header = new AliHeader();
341 treeE->SetBranchAddress("Header", &header);
342 treeE->SetBranchStatus("*",1);
343 TTree *treeENew = treeE->CloneTree();
8d0f3f77 344 treeENew->Write(0, TObject::kOverwrite);
345
c093f031 346
106fc2fa 347 // copy AliceGeom
c093f031 348 TGeometry *AliceGeom = static_cast<TGeometry*>(cwd->Get("AliceGeom"));
349 if (!AliceGeom) {
350 cerr << "ERROR: AliPHOSSDigitizer::SetSPlitFile -> AliceGeom was not found in the input file "<<endl;
351 abort() ;
352 }
8d0f3f77 353 AliceGeom->Write(0, TObject::kOverwrite);
c093f031 354
c093f031 355 gAlice->MakeTree("S",fSplitFile);
85f78bbc 356 cwd->cd() ;
6f51f90b 357 cout << "INFO: AliPHOSSDigitizer::SetSPlitMode -> SDigits will be stored in " << splitFileName.Data() << endl ;
8d0f3f77 358
106fc2fa 359}
360
990119d6 361//__________________________________________________________________
a4e98857 362void AliPHOSSDigitizer::Print(Option_t* option)const
363{
364 // Prints parameters of SDigitizer
990119d6 365 cout << "------------------- "<< GetName() << " -------------" << endl ;
7a9d98f9 366 cout << " Writing SDigits to branch with title " << GetName() << endl ;
7acf6008 367 cout << " with digitization parameters A = " << fA << endl ;
368 cout << " B = " << fB << endl ;
369 cout << " Threshold for Primary assignment= " << fPrimThreshold << endl ;
990119d6 370 cout << "---------------------------------------------------"<<endl ;
7acf6008 371
990119d6 372}
373//__________________________________________________________________
a4e98857 374Bool_t AliPHOSSDigitizer::operator==( AliPHOSSDigitizer const &sd )const
375{
3de072dc 376 // Equal operator.
377 // SDititizers are equal if their pedestal, slope and threshold are equal
378
990119d6 379 if( (fA==sd.fA)&&(fB==sd.fB)&&(fPrimThreshold==sd.fPrimThreshold))
380 return kTRUE ;
381 else
382 return kFALSE ;
383}
7acf6008 384//__________________________________________________________________
a6eedfad 385//__________________________________________________________________
a4e98857 386void AliPHOSSDigitizer::PrintSDigits(Option_t * option)
387{
388 // Prints list of digits produced in the current pass of AliPHOSDigitizer
7b7c1533 389
a6eedfad 390
7b7c1533 391 AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ;
5ad40cd6 392 TString sdname(GetName()) ;
393 sdname.Remove(sdname.Index(GetTitle())-1) ;
017addb3 394 const TClonesArray * sdigits = gime->SDigits(sdname.Data()) ;
7b7c1533 395
a6eedfad 396 cout << "AliPHOSSDigitiser: event " << gAlice->GetEvNumber() << endl ;
397 cout << " Number of entries in SDigits list " << sdigits->GetEntriesFast() << endl ;
7acf6008 398 cout << endl ;
a6eedfad 399 if(strstr(option,"all")||strstr(option,"EMC")){
7acf6008 400
401 //loop over digits
402 AliPHOSDigit * digit;
a6eedfad 403 cout << "EMC sdigits " << endl ;
404 cout << "Digit Id Amplitude Index Nprim Primaries list " << endl;
405 Int_t maxEmc = gime->PHOSGeometry()->GetNModules()*gime->PHOSGeometry()->GetNCristalsInModule() ;
7acf6008 406 Int_t index ;
a6eedfad 407 for (index = 0 ; (index < sdigits->GetEntriesFast()) &&
408 (((AliPHOSDigit * ) sdigits->At(index))->GetId() <= maxEmc) ; index++) {
7b7c1533 409 digit = (AliPHOSDigit * ) sdigits->At(index) ;
a6eedfad 410 if(digit->GetNprimary() == 0) continue;
411 cout << setw(6) << digit->GetId() << " " << setw(10) << digit->GetAmp() << " "
412 << setw(6) << digit->GetIndexInList() << " "
413 << setw(5) << digit->GetNprimary() <<" ";
7acf6008 414
415 Int_t iprimary;
416 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++)
a6eedfad 417 cout << setw(5) << digit->GetPrimary(iprimary+1) << " ";
7acf6008 418 cout << endl;
a6eedfad 419 }
420 cout << endl;
421 }
422
423 if(strstr(option,"all")||strstr(option,"CPV")){
7acf6008 424
a6eedfad 425 //loop over CPV digits
426 AliPHOSDigit * digit;
427 cout << "CPV sdigits " << endl ;
428 cout << "Digit Id Amplitude Index Nprim Primaries list " << endl;
429 Int_t maxEmc = gime->PHOSGeometry()->GetNModules()*gime->PHOSGeometry()->GetNCristalsInModule() ;
430 Int_t index ;
431 for (index = 0 ; index < sdigits->GetEntriesFast(); index++) {
432 digit = (AliPHOSDigit * ) sdigits->At(index) ;
433 if(digit->GetId() > maxEmc){
434 cout << setw(6) << digit->GetId() << " " << setw(10) << digit->GetAmp() << " "
435 << setw(6) << digit->GetIndexInList() << " "
436 << setw(5) << digit->GetNprimary() <<" ";
437
438 Int_t iprimary;
439 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++)
440 cout << setw(5) << digit->GetPrimary(iprimary+1) << " ";
441 cout << endl;
442 }
443 }
7acf6008 444 }
a6eedfad 445
7acf6008 446}
7b7c1533 447
448//____________________________________________________________________________
449void AliPHOSSDigitizer::UseHitsFrom(const char * filename)
450{
451 SetTitle(filename) ;
452 Init() ;
453}