Minor changes to make code more efficient
[u/mrichter/AliRoot.git] / PHOS / AliPHOSDigitizer.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
16/* $Id$ */
17
18//_________________________________________________________________________
990119d6 19//*-- Author : Dmitri Peressounko (SUBATECH & Kurchatov Institute)
20//////////////////////////////////////////////////////////////////////////////
a4e98857 21// This TTask performs digitization of Summable digits (in the PHOS case it is just
22// the sum of contributions from all primary particles into a given cell).
990119d6 23// In addition it performs mixing of summable digits from different events.
7b7c1533 24// The name of the TTask is also the title of the branch that will contain
25// the created SDigits
26// The title of the TTAsk is the name of the file that contains the hits from
27// which the SDigits are created
bca3b32a 28//
29// For each event two branches are created in TreeD:
30// "PHOS" - list of digits
31// "AliPHOSDigitizer" - AliPHOSDigitizer with all parameters used in digitization
32//
a4e98857 33// Note, that one can set a title for new digits branch, and repeat digitization with
bca3b32a 34// another set of parameters.
35//
a4e98857 36// Use case:
990119d6 37// root[0] AliPHOSDigitizer * d = new AliPHOSDigitizer() ;
38// root[1] d->ExecuteTask()
39// Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
40// //Digitizes SDigitis in all events found in file galice.root
bca3b32a 41//
8cb3533f 42// root[2] AliPHOSDigitizer * d1 = new AliPHOSDigitizer("galice1.root") ;
43// // Will read sdigits from galice1.root
44// root[3] d1->MixWith("galice2.root")
990119d6 45// Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
a4e98857 46// // Reads another set of sdigits from galice2.root
8cb3533f 47// root[3] d1->MixWith("galice3.root")
a4e98857 48// // Reads another set of sdigits from galice3.root
8cb3533f 49// root[4] d->ExecuteTask("deb timing")
50// // Reads SDigits from files galice1.root, galice2.root ....
51// // mixes them and stores produced Digits in file galice1.root
52// // deb - prints number of produced digits
53// // deb all - prints list of produced digits
54// // timing - prints time used for digitization
990119d6 55//
990119d6 56
57// --- ROOT system ---
8cb3533f 58#include "TFile.h"
990119d6 59#include "TTree.h"
60#include "TSystem.h"
8cb3533f 61#include "TROOT.h"
62#include "TFolder.h"
63#include "TObjString.h"
64#include "TBenchmark.h"
ba54256b 65
990119d6 66// --- Standard library ---
8cb3533f 67#include <iomanip.h>
990119d6 68
69// --- AliRoot header files ---
70
8cb3533f 71#include "AliRun.h"
3f81a70b 72#include "AliRunDigitizer.h"
990119d6 73#include "AliPHOSDigit.h"
dd5c4038 74#include "AliPHOS.h"
7b7c1533 75#include "AliPHOSGetter.h"
990119d6 76#include "AliPHOSDigitizer.h"
77#include "AliPHOSSDigitizer.h"
8cb3533f 78#include "AliPHOSGeometry.h"
7437a0f7 79#include "AliPHOSTick.h"
990119d6 80
81ClassImp(AliPHOSDigitizer)
82
83
84//____________________________________________________________________________
3f81a70b 85 AliPHOSDigitizer::AliPHOSDigitizer()
990119d6 86{
87 // ctor
88
2bd5457f 89 fPinNoise = 0.01 ;
90 fEMCDigitThreshold = 0.01 ;
91 fCPVNoise = 0.01;
92 fCPVDigitThreshold = 0.09 ;
aaf8a71c 93 fTimeResolution = 0.5e-9 ;
7437a0f7 94 fTimeSignalLength = 1.0e-9 ;
2b60655b 95 fDigitsInRun = 0 ;
3758d9fc 96 fADCchanelEmc = 0.0015; // width of one ADC channel in GeV
97 fADCpedestalEmc = 0.005 ; //
98 fNADCemc = (Int_t) TMath::Power(2,16) ; // number of channels in EMC ADC
99
100 fADCchanelCpv = 0.0012 ; // width of one ADC channel in CPV 'popugais'
101 fADCpedestalCpv = 0.012 ; //
102 fNADCcpv = (Int_t) TMath::Power(2,12); // number of channels in CPV ADC
103
104 fTimeThreshold = 0.001*10000000 ; //Means 1 MeV in terms of SDigits amplitude
105 fARD = 0 ; // We work in the standalong mode
3f81a70b 106}
107
108//____________________________________________________________________________
109AliPHOSDigitizer::AliPHOSDigitizer(const char *headerFile,const char * name)
110{
111 // ctor
112 SetName(name) ;
113 SetTitle(headerFile) ;
3f81a70b 114 fARD = 0 ; // We work in the standalong mode
3f81a70b 115 Init() ;
116
990119d6 117}
118
990119d6 119//____________________________________________________________________________
3f81a70b 120AliPHOSDigitizer::AliPHOSDigitizer(AliRunDigitizer * ard)
990119d6 121{
122 // ctor
3f81a70b 123 fARD = ard ;
124 SetName("Default");
125 SetTitle("aliroot") ;
8cb3533f 126
990119d6 127}
128
129//____________________________________________________________________________
130 AliPHOSDigitizer::~AliPHOSDigitizer()
131{
132 // dtor
8cb3533f 133
8cb3533f 134
990119d6 135}
136
137//____________________________________________________________________________
7b7c1533 138void AliPHOSDigitizer::Digitize(const Int_t event)
a4e98857 139{
140
141 // Makes the digitization of the collected summable digits.
142 // It first creates the array of all PHOS modules
143 // filled with noise (different for EMC, CPV and PPSD) and
144 // then adds contributions from SDigits.
145 // This design avoids scanning over the list of digits to add
146 // contribution to new SDigits only.
990119d6 147
253788f7 148 if( !AliPHOSGetter::GetInstance())
8cb3533f 149 Init() ;
990119d6 150
7b7c1533 151 AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ;
3f81a70b 152 TClonesArray * digits = gime->Digits(GetName()) ;
3758d9fc 153 AliPHOSSDigitizer * sdiz = gime->SDigitizer(GetName()) ;
7b7c1533 154
155 digits->Clear() ;
990119d6 156
7b7c1533 157 const AliPHOSGeometry *geom = gime->PHOSGeometry() ;
990119d6 158
159 //Making digits with noise, first EMC
160 Int_t nEMC = geom->GetNModules()*geom->GetNPhi()*geom->GetNZ();
161
162 Int_t nCPV ;
990119d6 163 Int_t absID ;
164 TString name = geom->GetName() ;
165
9688c1dd 166 nCPV = nEMC + geom->GetNumberOfCPVPadsZ()*geom->GetNumberOfCPVPadsPhi()*
167 geom->GetNModules() ;
8cb3533f 168
9688c1dd 169 digits->Expand(nCPV) ;
8cb3533f 170
7b7c1533 171
172 // sdigitize random gaussian noise and add it to all cells (EMCA+CPV+PPSD)
173 // get first the sdigitizer from the tasks list (must have same name as the digitizer)
174 const AliPHOSSDigitizer * sDigitizer = gime->SDigitizer(GetName());
175 if ( !sDigitizer) {
176 cerr << "ERROR: AliPHOSDigitizer::Digitize -> SDigitizer with name " << GetName() << " not found " << endl ;
177 abort() ;
178 }
9688c1dd 179
7b7c1533 180 // loop through the sdigits posted to the White Board and add them to the noise
7a9d98f9 181 TCollection * folderslist = gime->SDigitsFolder()->GetListOfFolders() ;
7b7c1533 182 TIter next(folderslist) ;
183 TFolder * folder = 0 ;
3f81a70b 184 TClonesArray * sdigits = 0 ;
185 Int_t input = 0 ;
9688c1dd 186 TObjArray * sdigArray = new TObjArray(2) ;
187 while ( (folder = (TFolder*)next()) )
3f81a70b 188 if ( (sdigits = (TClonesArray*)folder->FindObject(GetName()) ) ) {
189 cout << "INFO: AliPHOSDigitizer::Digitize -> Adding SDigits "
190 << GetName() << " from " << folder->GetName() << endl ;
9688c1dd 191 sdigArray->AddAt(sdigits, input) ;
192 input++ ;
193 }
194
7a9d98f9 195 //Find the first crystall with signal
9688c1dd 196 Int_t nextSig = 200000 ;
197 Int_t i;
198 for(i=0; i<input; i++){
199 sdigits = (TClonesArray *)sdigArray->At(i) ;
7a9d98f9 200 if ( !sdigits->GetEntries() )
201 continue ;
9688c1dd 202 Int_t curNext = ((AliPHOSDigit *)sdigits->At(0))->GetId() ;
7a9d98f9 203 if(curNext < nextSig)
204 nextSig = curNext ;
9688c1dd 205 }
7437a0f7 206
9688c1dd 207 TArrayI index(input) ;
208 index.Reset() ; //Set all indexes to zero
209
210 AliPHOSDigit * digit ;
211 AliPHOSDigit * curSDigit ;
212
7437a0f7 213 TClonesArray * ticks = new TClonesArray("AliPHOSTick",1000) ;
214
9688c1dd 215 //Put Noise contribution
216 for(absID = 1; absID <= nEMC; absID++){
217 Float_t noise = gRandom->Gaus(0., fPinNoise) ;
218 new((*digits)[absID-1]) AliPHOSDigit( -1,absID,sDigitizer->Digitize(noise), TimeOfNoise() ) ;
219 //look if we have to add signal?
220 if(absID==nextSig){
221 //Add SDigits from all inputs
222 digit = (AliPHOSDigit *) digits->At(absID-1) ;
7437a0f7 223
224 ticks->Clear() ;
9688c1dd 225 Int_t contrib = 0 ;
7437a0f7 226 Float_t a = digit->GetAmp() ;
227 Float_t b = TMath::Abs( a /fTimeSignalLength) ;
228 //Mark the beginnign of the signal
229 new((*ticks)[contrib++]) AliPHOSTick(digit->GetTime(),0, b);
230 //Mark the end of the ignal
231 new((*ticks)[contrib++]) AliPHOSTick(digit->GetTime()+fTimeSignalLength, -a, -b);
232
9688c1dd 233 //loop over inputs
234 for(i=0; i<input; i++){
5c9dfca0 235 if(((TClonesArray *)sdigArray->At(i))->GetEntriesFast() > index[i] )
236 curSDigit = (AliPHOSDigit*)((TClonesArray *)sdigArray->At(i))->At(index[i]) ;
237 else
238 curSDigit = 0 ;
9688c1dd 239 //May be several digits will contribute from the same input
240 while(curSDigit && curSDigit->GetId() == absID){
241 //Shift primary to separate primaries belonging different inputs
242 Int_t primaryoffset ;
243 if(fARD)
244 primaryoffset = fARD->GetMask(i) ;
245 else
246 primaryoffset = 10000000*i ;
247 curSDigit->ShiftPrimary(primaryoffset) ;
7437a0f7 248
249 a = curSDigit->GetAmp() ;
250 b = a /fTimeSignalLength ;
251 new((*ticks)[contrib++]) AliPHOSTick(curSDigit->GetTime(),0, b);
252 new((*ticks)[contrib++]) AliPHOSTick(curSDigit->GetTime()+fTimeSignalLength, -a, -b);
9688c1dd 253
254 *digit = *digit + *curSDigit ; //add energies
7437a0f7 255
9688c1dd 256 index[i]++ ;
5c9dfca0 257 if(((TClonesArray *)sdigArray->At(i))->GetEntriesFast() > index[i] )
258 curSDigit = (AliPHOSDigit*)((TClonesArray *)sdigArray->At(i))->At(index[i]) ;
259 else
260 curSDigit = 0 ;
9688c1dd 261 }
262 }
263
264 //calculate and set time
7437a0f7 265 Float_t time = FrontEdgeTime(ticks) ;
9688c1dd 266 digit->SetTime(time) ;
7437a0f7 267
9688c1dd 268 //Find next signal module
7437a0f7 269 nextSig = 200000 ;
9688c1dd 270 for(i=0; i<input; i++){
271 sdigits = ((TClonesArray *)sdigArray->At(i)) ;
5c9dfca0 272 Int_t curNext = nextSig ;
273 if(sdigits->GetEntriesFast() > index[i] ){
274 curNext = ((AliPHOSDigit *) sdigits->At(index[i]))->GetId() ;
275
276 }
9688c1dd 277 if(curNext < nextSig) nextSig = curNext ;
7b7c1533 278 }
279 }
990119d6 280 }
3f81a70b 281
7437a0f7 282 ticks->Delete() ;
283 delete ticks ;
9688c1dd 284
285
286 //Now CPV digits (different noise and no timing)
287 for(absID = nEMC+1; absID <= nCPV; absID++){
288 Float_t noise = gRandom->Gaus(0., fCPVNoise) ;
289 new((*digits)[absID-1]) AliPHOSDigit( -1,absID,sDigitizer->Digitize(noise), TimeOfNoise() ) ;
290 //look if we have to add signal?
291 if(absID==nextSig){
292 digit = (AliPHOSDigit *) digits->At(absID-1) ;
293 //Add SDigits from all inputs
294 for(i=0; i<input; i++){
5c9dfca0 295 if(((TClonesArray *)sdigArray->At(i))->GetEntriesFast() > index[i] )
296 curSDigit = (AliPHOSDigit*)((TClonesArray *)sdigArray->At(i))->At(index[i]) ;
297 else
298 curSDigit = 0 ;
9688c1dd 299 //May be several digits will contribute from the same input
300 while(curSDigit && curSDigit->GetId() == absID){
301 //Shift primary to separate primaries belonging different inputs
302 Int_t primaryoffset ;
303 if(fARD)
304 primaryoffset = fARD->GetMask(i) ;
305 else
306 primaryoffset = 10000000*i ;
307 curSDigit->ShiftPrimary(primaryoffset) ;
308
309 //add energies
310 *digit = *digit + *curSDigit ;
311 index[i]++ ;
5c9dfca0 312 if(((TClonesArray *)sdigArray->At(i))->GetEntriesFast() > index[i] )
313 curSDigit = (AliPHOSDigit*)((TClonesArray *)sdigArray->At(i))->At(index[i]) ;
314 else
315 curSDigit = 0 ;
9688c1dd 316 }
317 }
318
319 //Find next signal module
320 for(i=0; i<input; i++){
321 sdigits = (TClonesArray *)sdigArray->At(i) ;
5c9dfca0 322 Int_t curNext = nextSig ;
323 if(sdigits->GetEntriesFast() > index[i] )
324 curNext = ((AliPHOSDigit *) sdigits->At(index[i]))->GetId() ;
9688c1dd 325 if(curNext < nextSig) nextSig = curNext ;
326 }
327
328 }
329 }
9688c1dd 330 delete sdigArray ; //We should not delete its contents
331
332
333
990119d6 334 //remove digits below thresholds
aaf8a71c 335 for(absID = 0; absID < nEMC ; absID++){
336 digit = (AliPHOSDigit*) digits->At(absID) ;
337 if(sDigitizer->Calibrate( digit->GetAmp() ) < fEMCDigitThreshold)
7b7c1533 338 digits->RemoveAt(absID) ;
aaf8a71c 339 else
340 digit->SetTime(gRandom->Gaus(digit->GetTime(),fTimeResolution) ) ;
341 }
342
990119d6 343 for(absID = nEMC; absID < nCPV ; absID++)
7b7c1533 344 if(sDigitizer->Calibrate(((AliPHOSDigit*)digits->At(absID))->GetAmp()) < fCPVDigitThreshold)
345 digits->RemoveAt(absID) ;
9688c1dd 346
7b7c1533 347 digits->Compress() ;
990119d6 348
7b7c1533 349 Int_t ndigits = digits->GetEntriesFast() ;
7b7c1533 350 digits->Expand(ndigits) ;
990119d6 351
3758d9fc 352 //Set indexes in list of digits and make true digitization of the energy
990119d6 353 for (i = 0 ; i < ndigits ; i++) {
7b7c1533 354 AliPHOSDigit * digit = (AliPHOSDigit *) digits->At(i) ;
990119d6 355 digit->SetIndexInList(i) ;
3758d9fc 356 Float_t energy = sdiz->Calibrate(digit->GetAmp()) ;
357 digit->SetAmp(DigitizeEnergy(energy,digit->GetId()) ) ;
990119d6 358 }
990119d6 359
7b7c1533 360}
3758d9fc 361//____________________________________________________________________________
362Int_t AliPHOSDigitizer::DigitizeEnergy(Float_t energy, Int_t absId)
363{
364 Int_t chanel ;
365 if(absId <= fEmcCrystals){ //digitize as EMC
366 chanel = (Int_t) TMath::Ceil((energy - fADCpedestalEmc)/fADCchanelEmc) ;
367 if(chanel > fNADCemc ) chanel = fNADCemc ;
368 }
369 else{ //Digitize as CPV
370 chanel = (Int_t) TMath::Ceil((energy - fADCpedestalCpv)/fADCchanelCpv) ;
371 if(chanel > fNADCcpv ) chanel = fNADCcpv ;
372 }
373 return chanel ;
374}
7b7c1533 375//____________________________________________________________________________
376void AliPHOSDigitizer::Exec(Option_t *option)
377{
378 // Managing method
990119d6 379
7b7c1533 380 if( strcmp(GetName(), "") == 0 )
381 Init() ;
8cb3533f 382
7b7c1533 383 if (strstr(option,"print")) {
384 Print("");
385 return ;
8cb3533f 386 }
990119d6 387
7b7c1533 388 if(strstr(option,"tim"))
389 gBenchmark->Start("PHOSDigitizer");
8cb3533f 390
3f81a70b 391 AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ;
392
393 Int_t nevents ;
394
395 TTree * treeD ;
396
397 if(fARD){
398 treeD = fARD->GetTreeD() ;
399 nevents = 1 ; // Will process only one event
400 }
401 else {
402 gAlice->GetEvent(0) ;
403 nevents = (Int_t) gAlice->TreeE()->GetEntries() ;
404 treeD=gAlice->TreeD() ;
405 }
406 if(treeD == 0 ){
407 cerr << " AliPHOSDigitizer :: Can not find TreeD " << endl ;
408 return ;
409 }
410
7b7c1533 411 //Check, if this branch already exits
3f81a70b 412 TObjArray * lob = (TObjArray*)treeD->GetListOfBranches() ;
7b7c1533 413 TIter next(lob) ;
414 TBranch * branch = 0 ;
415 Bool_t phosfound = kFALSE, digitizerfound = kFALSE ;
8cb3533f 416
7b7c1533 417 while ( (branch = (TBranch*)next()) && (!phosfound || !digitizerfound) ) {
3f81a70b 418 if ( (strcmp(branch->GetName(), "PHOS")==0) &&
419 (strcmp(branch->GetTitle(), GetName())==0) )
7b7c1533 420 phosfound = kTRUE ;
8cb3533f 421
3f81a70b 422 else if ( (strcmp(branch->GetName(), "AliPHOSDigitizer")==0) &&
423 (strcmp(branch->GetTitle(), GetName())==0) )
7b7c1533 424 digitizerfound = kTRUE ;
8cb3533f 425 }
990119d6 426
3f81a70b 427 if ( phosfound ) {
428 cerr << "WARNING: AliPHOSDigitizer -> Digits branch with name " << GetName()
429 << " already exits" << endl ;
430 return ;
431 }
432 if ( digitizerfound ) {
433 cerr << "WARNING: AliPHOSDigitizer -> Digitizer branch with name " << GetName()
7b7c1533 434 << " already exits" << endl ;
435 return ;
436 }
eec3ac52 437
7b7c1533 438 Int_t ievent ;
bca3b32a 439
7b7c1533 440 for(ievent = 0; ievent < nevents; ievent++){
3f81a70b 441
442 if(fARD){
443 Int_t input ;
444 for(input = 0 ; input < fARD->GetNinputs(); input ++){
445 TTree * treeS = fARD->GetInputTreeS(input) ;
446 if(!treeS){
447 cerr << "AliPHOSDigitizer -> No Input " << endl ;
448 return ;
449 }
450 gime->ReadTreeS(treeS,input) ;
451 }
452 }
453 else
454 gime->Event(ievent,"S") ;
990119d6 455
7b7c1533 456 Digitize(ievent) ; //Add prepared SDigits to digits and add the noise
3f81a70b 457
7b7c1533 458 WriteDigits(ievent) ;
3f81a70b 459
01a599c9 460 if(strstr(option,"deb"))
461 PrintDigits(option);
94de8339 462
463 //increment the total number of Digits per run
464 fDigitsInRun += gime->Digits()->GetEntriesFast() ;
01a599c9 465 }
3f81a70b 466
8cb3533f 467 if(strstr(option,"tim")){
468 gBenchmark->Stop("PHOSDigitizer");
469 cout << "AliPHOSDigitizer:" << endl ;
ba54256b 470 cout << " took " << gBenchmark->GetCpuTime("PHOSDigitizer") << " seconds for Digitizing "
471 << gBenchmark->GetCpuTime("PHOSDigitizer")/nevents << " seconds per event " << endl ;
8cb3533f 472 cout << endl ;
473 }
990119d6 474
475}
476
7b7c1533 477//____________________________________________________________________________
7437a0f7 478Float_t AliPHOSDigitizer::FrontEdgeTime(TClonesArray * ticks)
9688c1dd 479{ //
7437a0f7 480 ticks->Sort() ; //Sort in accordance with times of ticks
481 TIter it(ticks) ;
482 AliPHOSTick * ctick = (AliPHOSTick *) it.Next() ;
483 Float_t time = ctick->CrossingTime(fTimeThreshold) ;
484
485 AliPHOSTick * t ;
486 while((t=(AliPHOSTick*) it.Next())){
487 if(t->GetTime() < time) //This tick starts before crossing
488 *ctick+=*t ;
489 else
490 return time ;
491
492 time = ctick->CrossingTime(fTimeThreshold) ;
9688c1dd 493 }
494 return time ;
9688c1dd 495}
496//____________________________________________________________________________
3f81a70b 497Bool_t AliPHOSDigitizer::Init()
a4e98857 498{
3758d9fc 499 fPinNoise = 0.01 ;
500 fEMCDigitThreshold = 0.01 ;
501 fCPVNoise = 0.01;
502 fCPVDigitThreshold = 0.09 ;
aaf8a71c 503 fTimeResolution = 0.5e-9 ;
3758d9fc 504 fTimeSignalLength = 1.0e-9 ;
505 fDigitsInRun = 0 ;
506 fADCchanelEmc = 0.0015; // width of one ADC channel in GeV
507 fADCpedestalEmc = 0.005 ; //
508 fNADCemc = (Int_t) TMath::Power(2,16) ; // number of channels in EMC ADC
509
510 fADCchanelCpv = 0.0012 ; // width of one ADC channel in CPV 'popugais'
511 fADCpedestalCpv = 0.012 ; //
512 fNADCcpv = (Int_t) TMath::Power(2,12); // number of channels in CPV ADC
513
514 fTimeThreshold = 0.001*10000000 ; //Means 1 MeV in terms of SDigits amplitude
515
7b7c1533 516 // Makes all memory allocations
517 // Adds Digitizer task to the folder of PHOS tasks
3758d9fc 518
7b7c1533 519 //============================================================= YS
520 // The initialisation is now done by AliPHOSGetter
8cb3533f 521
7b7c1533 522 if( strcmp(GetTitle(), "") == 0 )
523 SetTitle("galice.root") ;
524
7b7c1533 525 // the SDigits name is stored by AliPHOSGetter as the name of the TClones Array
526 // //YSAlice/WhiteBoard/SDigits/PHOS/headerFile/branchname and has branchTitle as title.
990119d6 527
7b7c1533 528 AliPHOSGetter * gime = AliPHOSGetter::GetInstance(GetTitle(), GetName()) ;
529 if ( gime == 0 ) {
530 cerr << "ERROR: AliPHOSDigitizer::Init -> Could not obtain the Getter object !" << endl ;
3f81a70b 531 return kFALSE;
7b7c1533 532 }
3758d9fc 533
534 const AliPHOSGeometry * geom = gime->PHOSGeometry() ;
535 fEmcCrystals = geom->GetNModules() * geom->GetNCristalsInModule() ;
3f81a70b 536
537 // create a folder on the white board //YSAlice/WhiteBoard/Digits/PHOS/headerFile/digitsTitle
538 gime->PostDigits(GetName() ) ;
7b7c1533 539
540 //add Task to //YSAlice/tasks/Digitizer/PHOS
3f81a70b 541 gime->PostDigitizer(this) ;
542
543 //Mark that we will use current header file
544 if(!fARD){
545 gime->PostSDigits(GetName(),GetTitle()) ;
546 gime->PostSDigitizer(GetName(),GetTitle()) ;
547 }
548 return kTRUE ;
990119d6 549}
7b7c1533 550
990119d6 551//__________________________________________________________________
7b7c1533 552void AliPHOSDigitizer::MixWith(const char* headerFile)
a4e98857 553{
ba54256b 554 // Allows to produce digits by superimposing background and signal event.
bca3b32a 555 // It is assumed, that headers file with SIGNAL events is opened in
a4e98857 556 // the constructor.
557 // Sets the BACKGROUND event, with which the SIGNAL event is to be mixed
558 // Thus we avoid writing (changing) huge and expensive
bca3b32a 559 // backgound files: all output will be writen into SIGNAL, i.e.
560 // opened in constructor file.
561 //
a4e98857 562 // One can open as many files to mix with as one needs.
7b7c1533 563 // However only Sdigits with the same name (i.e. constructed with the same SDigitizer)
564 // can be mixed.
bca3b32a 565
7b7c1533 566 if( strcmp(GetName(), "") == 0 )
8cb3533f 567 Init() ;
568
3f81a70b 569 if(fARD){
570 cout << "Can not use this method under AliRunDigitizer " << endl ;
571 return ;
572 }
7b7c1533 573
574 // check if the specified SDigits do not already exist on the White Board:
575 // //YSAlice/WhiteBoard/SDigits/PHOS/headerFile/sDigitsTitle
990119d6 576
7b7c1533 577 TString path = "YSAlice/WhiteBoard/SDigits/PHOS/" ;
578 path += headerFile ;
579 path += "/" ;
3f81a70b 580 path += GetName() ;
7b7c1533 581 if ( gROOT->FindObjectAny(path.Data()) ) {
582 cerr << "WARNING: AliPHOSDigitizer::MixWith -> Entry already exists, do not add" << endl ;
583 return;
990119d6 584 }
3f81a70b 585
586 AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ;
587 gime->PostSDigits(GetName(),headerFile) ;
588
7b7c1533 589 // check if the requested file is already open or exist and if SDigits Branch exist
590 TFile * file = (TFile*)gROOT->FindObject(headerFile);
591 if ( !file ) {
592 file = new TFile(headerFile, "READ") ;
593 if (!file) {
594 cerr << "ERROR: AliPHOSDigitizer::MixWith -> File " << headerFile << " does not exist!" << endl ;
595 return ;
596 }
597 }
3f81a70b 598
990119d6 599}
7b7c1533 600
990119d6 601//__________________________________________________________________
dd5c4038 602void AliPHOSDigitizer::Print(Option_t* option)const {
603 // Print Digitizer's parameters
7b7c1533 604 if( strcmp(GetName(), "") != 0 ){
8cb3533f 605
606 cout << "------------------- "<< GetName() << " -------------" << endl ;
607 cout << "Digitizing sDigits from file(s): " <<endl ;
7b7c1533 608
609 TCollection * folderslist = ((TFolder*)gROOT->FindObjectAny("YSAlice/WhiteBoard/SDigits/PHOS"))->GetListOfFolders() ;
610 TIter next(folderslist) ;
611 TFolder * folder = 0 ;
612
613 while ( (folder = (TFolder*)next()) ) {
614 if ( folder->FindObject(GetName()) )
615 cout << "Adding SDigits " << GetName() << " from " << folder->GetName() << endl ;
8cb3533f 616 }
617 cout << endl ;
7b7c1533 618 cout << "Writing digits to " << GetTitle() << endl ;
8cb3533f 619
620 cout << endl ;
621 cout << "With following parameters: " << endl ;
622 cout << " Electronics noise in EMC (fPinNoise) = " << fPinNoise << endl ;
623 cout << " Threshold in EMC (fEMCDigitThreshold) = " << fEMCDigitThreshold << endl ; ;
624 cout << " Noise in CPV (fCPVNoise) = " << fCPVNoise << endl ;
625 cout << " Threshold in CPV (fCPVDigitThreshold) = " << fCPVDigitThreshold << endl ;
8cb3533f 626 cout << "---------------------------------------------------" << endl ;
990119d6 627 }
8cb3533f 628 else
629 cout << "AliPHOSDigitizer not initialized " << endl ;
630
631}
9688c1dd 632
8cb3533f 633//__________________________________________________________________
dd5c4038 634void AliPHOSDigitizer::PrintDigits(Option_t * option){
635 // Print a table of digits
a4e98857 636
7b7c1533 637 AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ;
638 TClonesArray * digits = gime->Digits() ;
639
01a599c9 640 cout << "AliPHOSDigitiser: event " << gAlice->GetEvNumber() << endl ;
7b7c1533 641 cout << " Number of entries in Digits list " << digits->GetEntriesFast() << endl ;
990119d6 642 cout << endl ;
9688c1dd 643 if(strstr(option,"all")||strstr(option,"EMC")){
8cb3533f 644
645 //loop over digits
646 AliPHOSDigit * digit;
9688c1dd 647 cout << "Digit Id Amplitude Index " << " Nprim " << " Primaries list " << endl;
648 Int_t maxEmc = gime->PHOSGeometry()->GetNModules()*gime->PHOSGeometry()->GetNCristalsInModule() ;
8cb3533f 649 Int_t index ;
9688c1dd 650 for (index = 0 ; (index < digits->GetEntries()) &&
651 (((AliPHOSDigit * ) digits->At(index))->GetId() <= maxEmc) ; index++) {
7b7c1533 652 digit = (AliPHOSDigit * ) digits->At(index) ;
9688c1dd 653 if(digit->GetNprimary() == 0) continue;
654 cout << setw(6) << digit->GetId() << " " << setw(10) << digit->GetAmp() << " "
655 << setw(6) << digit->GetIndexInList() << " "
656 << setw(5) << digit->GetNprimary() <<" ";
8cb3533f 657
658 Int_t iprimary;
659 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++)
9688c1dd 660 cout << setw(5) << digit->GetPrimary(iprimary+1) << " ";
8cb3533f 661 cout << endl;
9688c1dd 662 }
663 cout << endl;
664 }
665
666 if(strstr(option,"all")||strstr(option,"CPV")){
8cb3533f 667
9688c1dd 668 //loop over CPV digits
669 AliPHOSDigit * digit;
670 cout << "Digit Id " << " Amplitude " << " Index " << " Nprim " << " Primaries list " << endl;
671 Int_t maxEmc = gime->PHOSGeometry()->GetNModules()*gime->PHOSGeometry()->GetNCristalsInModule() ;
672 Int_t index ;
673 for (index = 0 ; index < digits->GetEntries(); index++) {
674 digit = (AliPHOSDigit * ) digits->At(index) ;
675 if(digit->GetId() > maxEmc){
676 cout << setw(6) << digit->GetId() << " " << setw(10) << digit->GetAmp() << " "
677 << setw(6) << digit->GetIndexInList() << " "
678 << setw(5) << digit->GetNprimary() <<" ";
679
680 Int_t iprimary;
681 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++)
682 cout << setw(5) << digit->GetPrimary(iprimary+1) << " ";
683 cout << endl;
684 }
685 }
8cb3533f 686 }
9688c1dd 687
8cb3533f 688}
7b7c1533 689
8cb3533f 690//__________________________________________________________________
a4e98857 691void AliPHOSDigitizer::SetSDigitsBranch(const char* title)
692{
bca3b32a 693 // we set title (comment) of the SDigits branch in the first! header file
7b7c1533 694 if( strcmp(GetName(), "") == 0 )
695 Init() ;
990119d6 696
7b7c1533 697 AliPHOSGetter::GetInstance()->SDigits()->SetName(title) ;
698
8cb3533f 699}
9688c1dd 700//__________________________________________________________________
701Float_t AliPHOSDigitizer::TimeOfNoise(void)
702{ // Calculates the time signal generated by noise
703 //to be rewritten, now returns just big number
704 return 1. ;
705
706}
7b7c1533 707//____________________________________________________________________________
708void AliPHOSDigitizer::Reset()
709{
710 // sets current event number to the first simulated event
711
712 if( strcmp(GetName(), "") == 0 )
713 Init() ;
714
715 // Int_t inputs ;
716// for(inputs = 0; inputs < fNinputs ;inputs++)
717// fIevent->AddAt(-1, inputs ) ;
718
719}
720
721//____________________________________________________________________________
722void AliPHOSDigitizer::WriteDigits(Int_t event)
723{
724
725 // Makes TreeD in the output file.
726 // Check if branch already exists:
727 // if yes, exit without writing: ROOT TTree does not support overwriting/updating of
728 // already existing branches.
729 // else creates branch with Digits, named "PHOS", title "...",
730 // and branch "AliPHOSDigitizer", with the same title to keep all the parameters
731 // and names of files, from which digits are made.
732
733 AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ;
3f81a70b 734 TClonesArray * digits = gime->Digits(GetName()) ;
ba54256b 735
3f81a70b 736 TTree * treeD ;
7b7c1533 737
3f81a70b 738 if(fARD)
739 treeD = fARD->GetTreeD() ;
740 else
741 treeD = gAlice->TreeD();
742
7b7c1533 743 // create new branches
744 // -- generate file name if necessary
745 char * file =0;
746 if(gSystem->Getenv("CONFIG_SPLIT_FILE")){ //generating file name
747 file = new char[strlen(gAlice->GetBaseFile())+20] ;
748 sprintf(file,"%s/PHOS.Digits.root",gAlice->GetBaseFile()) ;
749 }
750
751 TDirectory *cwd = gDirectory;
7b7c1533 752 // -- create Digits branch
753 Int_t bufferSize = 32000 ;
ba54256b 754 TBranch * digitsBranch = treeD->Branch("PHOS",&digits,bufferSize);
7b7c1533 755 digitsBranch->SetTitle(GetName());
756 if (file) {
757 digitsBranch->SetFile(file);
758 TIter next( digitsBranch->GetListOfBranches());
759 TBranch * sbr ;
760 while ((sbr=(TBranch*)next())) {
761 sbr->SetFile(file);
762 }
763 cwd->cd();
764 }
765
766 // -- Create Digitizer branch
767 Int_t splitlevel = 0 ;
768 AliPHOSDigitizer * d = gime->Digitizer(GetName()) ;
ba54256b 769 TBranch * digitizerBranch = treeD->Branch("AliPHOSDigitizer", "AliPHOSDigitizer", &d,bufferSize,splitlevel);
7b7c1533 770 digitizerBranch->SetTitle(GetName());
771 if (file) {
772 digitizerBranch->SetFile(file);
773 TIter next( digitizerBranch->GetListOfBranches());
774 TBranch * sbr;
775 while ((sbr=(TBranch*)next())) {
776 sbr->SetFile(file);
777 }
778 cwd->cd();
779 }
3f81a70b 780
7b7c1533 781 digitsBranch->Fill() ;
782 digitizerBranch->Fill() ;
783
ba54256b 784 treeD->Write(0,kOverwrite) ;
3f81a70b 785
7b7c1533 786}