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