1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 //_________________________________________________________________________
20 //////////////////////////////////////////////////////////////////////////////
21 // Class performs digitization of Summable digits
23 // In addition it performs mixing of summable digits from different events.
25 // For each event two branches are created in TreeD:
26 // "EMCAL" - list of digits
27 // "AliEMCALDigitizer" - AliEMCALDigitizer with all parameters used in digitization
29 // Note, that one cset title for new digits branch, and repeat digitization with
30 // another set of parameters.
33 // root[0] AliEMCALDigitizer * d = new AliEMCALDigitizer() ;
34 // root[1] d->ExecuteTask()
35 // Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
36 // //Digitizes SDigitis in all events found in file galice.root
38 // root[2] AliEMCALDigitizer * d1 = new AliEMCALDigitizer("galice1.root") ;
39 // // Will read sdigits from galice1.root
40 // root[3] d1->MixWith("galice2.root")
41 // Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
42 // // Reads another portion of sdigits from galice2.root
43 // root[3] d1->MixWith("galice3.root")
44 // // Reads another portion of sdigits from galice3.root
45 // root[4] d->ExecuteTask("deb timing")
46 // // Reads SDigits from files galice1.root, galice2.root ....
47 // // mixes them and stores produced Digits in file galice1.root
48 // // deb - prints number of produced digits
49 // // deb all - prints list of produced digits
50 // // timing - prints time used for digitization
51 ////////////////////////////////////////////////////////////////////////////////////
53 //*-- Author: Sahal Yacoob (LBL)
54 // based on : AliPHOSDigitizer
55 //_________________________________________________________________________________
57 // --- ROOT system ---
63 #include "TObjString.h"
64 #include "TBenchmark.h"
65 // --- Standard library ---
68 // --- AliRoot header files ---
71 #include "AliEMCALDigit.h"
72 #include "AliEMCALHit.h"
73 #include "AliEMCALv1.h"
74 #include "AliEMCALDigitizer.h"
75 #include "AliEMCALSDigitizer.h"
76 #include "AliEMCALGeometry.h"
78 ClassImp(AliEMCALDigitizer)
81 //____________________________________________________________________________
82 AliEMCALDigitizer::AliEMCALDigitizer():TTask("AliEMCALDigitizer","")
89 fEMCDigitThreshold = 0.00 ;
90 fInitialized = kFALSE ;
98 //____________________________________________________________________________
99 void AliEMCALDigitizer::Init(){
100 // Makes all memory allocations
104 fHeaderFiles = new TClonesArray("TObjString",1) ;
105 new((*fHeaderFiles)[0]) TObjString("galice.root") ;
107 //Test, if this file already open
109 TFile *file = (TFile*) gROOT->GetFile(((TObjString *) fHeaderFiles->At(0))->GetString() ) ;
112 file = new TFile(((TObjString *) fHeaderFiles->At(0))->GetString(),"update") ;
113 gAlice = (AliRun *) file->Get("gAlice") ;
116 file = new TFile(((TObjString *) fHeaderFiles->At(0))->GetString()) ;
120 fSDigitsTitles = new TClonesArray("TObjString",1);
121 new((*fSDigitsTitles)[0]) TObjString("Default") ;
123 fSDigits = new TClonesArray("TClonesArray",1) ;
124 new((*fSDigits)[0]) TClonesArray("AliEMCALDigit",1000) ;
128 fDigitsTitle = "Default" ;
130 fDigits = new TClonesArray("AliEMCALDigit",200000) ;
132 fIevent = new TArrayI(1) ;
133 fIevent->AddAt(-1,0 ) ;
134 fIeventMax = new TArrayI(1) ;
136 fIeventMax->AddAt((Int_t) gAlice->TreeE()->GetEntries(), 0 );
138 // add Task to //root/Tasks folder
139 TTask * roottasks = (TTask*)gROOT->GetRootFolder()->FindObject("Tasks") ;
140 roottasks->Add(this) ;
142 fInitialized = kTRUE ;
147 //____________________________________________________________________________
148 AliEMCALDigitizer::AliEMCALDigitizer(const char *headerFile,const char *sDigitsTitle):
149 TTask("AliEMCALDigitizer","")
152 fHeaderFiles = new TClonesArray("TObjString",1) ;
153 new((*fHeaderFiles)[0]) TObjString(headerFile) ;
155 // Header file, where result will be stored
156 TFile * file = (TFile*) gROOT->GetFile(((TObjString *) fHeaderFiles->At(0))->GetString() ) ;
158 if(((TObjString *) fHeaderFiles->At(0))->GetString().Contains("rfio"))
159 file = TFile::Open(((TObjString *) fHeaderFiles->At(0))->GetString(),"update") ;
161 file = new TFile(((TObjString *) fHeaderFiles->At(0))->GetString(),"update") ;
162 gAlice = (AliRun *) file->Get("gAlice") ; //If not read yet
167 fSDigitsTitles = new TClonesArray("TObjString",1); // Title name of the SDigits branch
168 new((*fSDigitsTitles)[0]) TObjString(sDigitsTitle) ;
170 fSDigits = new TClonesArray("TClonesArray",1) ; // here list of SDigits wil be stored
171 new((*fSDigits)[0]) TClonesArray("AliEMCALDigit",1000) ;
173 fDigits = new TClonesArray("AliEMCALDigit",200000) ;
175 fDigitsTitle = "Default" ;
180 fIevent = new TArrayI(1) ;
181 fIevent->AddAt(-1,0 ) ;
182 fIeventMax = new TArrayI(1) ;
184 // Get number of events to process
185 fIeventMax->AddAt((Int_t) gAlice->TreeE()->GetEntries(), 0 );
190 fEMCDigitThreshold = 0.001 ;
192 // add Task to //root/Tasks folder
193 TTask * roottasks = (TTask*)gROOT->GetRootFolder()->FindObject("Tasks") ;
194 roottasks->Add(this) ;
195 fInitialized = kTRUE ;
199 //____________________________________________________________________________
200 AliEMCALDigitizer::~AliEMCALDigitizer()
204 if(fHeaderFiles) delete fHeaderFiles ;
205 if(fSDigitsTitles) delete fSDigitsTitles ;
206 if(fSDigits) delete fSDigits ;
207 if(fDigits) delete fDigits ;
209 //____________________________________________________________________________
210 void AliEMCALDigitizer::Reset() {
211 //sets current event number to the first simulated event
217 for(inputs = 0; inputs < fNinputs ;inputs++)
218 fIevent->AddAt(-1, inputs ) ;
221 //____________________________________________________________________________
222 Bool_t AliEMCALDigitizer::Combinator() {
224 //Makes all desirable combinations Signal+Background,
225 // returns kFALSE when all combinations are made
226 // May be useful to introduce some options like "One-to-One", "All-to-One" and "All-to-All" ?
228 //realizing "One-to-One" option...
234 Bool_t endNotReached = kTRUE ;
236 for(inputs = 0; (inputs < fNinputs) && endNotReached ;inputs++){
237 if(fIevent->At(inputs)+1 < fIeventMax->At(inputs))
238 fIevent->AddAt(fIevent->At(inputs)+1, inputs ) ;
241 endNotReached = kFALSE ;
242 else //for inputs other than base one start from the beginning
243 fIevent->AddAt(0, inputs ) ;
246 return endNotReached ;
250 //____________________________________________________________________________
251 void AliEMCALDigitizer::Digitize(Option_t *option) {
253 // Makes the digitization of the collected summable digits
254 // for this it first creates the array of all EMCAL modules
255 // filled with noise (different for EMC, CPV and PPSD) and
256 // after that adds contributions from SDigits. This design
257 // helps to avoid scanning over the list of digits to add
258 // contribution of any new SDigit.
265 AliEMCALv0 * EMCAL = (AliEMCALv0 *) gAlice->GetDetector("EMCAL") ;
266 AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance( EMCAL->GetGeometry()->GetName(), EMCAL->GetGeometry()->GetTitle() );
268 //Making digits with noise, first EMC
269 Int_t nEMC = 2*geom->GetNPhi()*geom->GetNZ();
271 TString name = geom->GetName() ;
273 for(absID = 1; absID <= nEMC; absID++){
274 Float_t noise = gRandom->Gaus(0., fPinNoise) ;
275 new((*fDigits)[absID-1]) AliEMCALDigit( -1,-1, absID,fSDigitizer->Digitize(noise) ) ;
279 // Now look throught (unsorted) list of SDigits and add corresponding digits
280 AliEMCALDigit *curSDigit ;
281 AliEMCALDigit *digit ;
284 for(inputs = 0; inputs< fNinputs ; inputs++){ //loop over (possible) merge sources
286 TClonesArray * sdigits= (TClonesArray *)fSDigits->At(inputs) ;
289 Int_t nSDigits = sdigits->GetEntries() ;
290 for(isdigit=0;isdigit< nSDigits; isdigit++){
291 curSDigit = (AliEMCALDigit *)sdigits->At(isdigit) ;
292 if(inputs) //Shift primaries for non-background sdigits
293 curSDigit->ShiftPrimary(inputs) ;
294 digit = (AliEMCALDigit *)fDigits->At(curSDigit->GetId() - 1);
295 cout << curSDigit->GetAmp() << " and " << digit->GetAmp() << endl ;
296 *digit = *digit + *curSDigit ;
301 //remove digits below thresholds
302 for(absID = 0; absID < nEMC ; absID++)
303 if(fSDigitizer->Calibrate(((AliEMCALDigit*)fDigits->At(absID))->GetAmp()) < fEMCDigitThreshold)
304 fDigits->RemoveAt(absID) ;
306 fDigits->Compress() ;
308 Int_t ndigits = fDigits->GetEntriesFast() ;
310 fDigits->Expand(ndigits) ;
313 //Set indexes in list of digits
315 for (i = 0 ; i < ndigits ; i++) {
316 AliEMCALDigit * digit = (AliEMCALDigit *) fDigits->At(i) ;
317 digit->SetIndexInList(i) ;
318 cout << digit->GetAmp() << "is amp" << endl;
319 Float_t energy = fSDigitizer->Calibrate(digit->GetAmp()) ;
320 cout << energy << " = energy " << endl;
321 energy = energy*1000 ;
322 cout << energy << " = energy " << endl;
323 digit->SetAmp(DigitizeEnergy(energy,digit->GetId()) ) ;
324 cout << digit->GetAmp() << "is amp" << endl;
325 cout << 100*energy/12.9 << endl ;
328 //____________________________________________________________________________
330 Int_t AliEMCALDigitizer::DigitizeEnergy(Float_t energy, Int_t absId)
332 //if(absId <= fEmcCrystals){ //digitize as EMC
333 chanel = (Int_t) TMath::Ceil(energy) ;
334 //if(chanel > fNADCemc ) chanel = fNADCemc ;
338 //____________________________________________________________________________
341 void AliEMCALDigitizer::WriteDigits(){
343 // Made TreeD in the output file. Check if branch already exists: if yes, exits
344 // without writing: ROOT TTree does not suppert overwriting/updating of the
345 // already existing branches. Creates branch with Digits, named "EMCAL", title "...",
346 // and branch "AliEMCALDigitizer", with the same title to keep all the parameters
347 // and names of files, from which digits are made.
349 gAlice->GetEvent(fIevent->At(0)) ; // Suitable only for One-To-One mixing
350 gAlice->SetEvent(fIevent->At(0)) ; // for all-to-all will produce a lot of branches in TreeD
352 if(gAlice->TreeD()==0)
353 gAlice->MakeTree("D") ;
355 //Check, if this branch already exits?
356 TBranch * digitsBranch = 0;
357 TBranch * digitizerBranch = 0;
359 TObjArray * branches = gAlice->TreeD()->GetListOfBranches() ;
361 Bool_t emcalNotFound = kTRUE ;
362 Bool_t digitizerNotFound = kTRUE ;
364 for(ibranch = 0;ibranch <branches->GetEntries();ibranch++){
367 digitsBranch=(TBranch *) branches->At(ibranch) ;
368 if( (strcmp("EMCAL",digitsBranch->GetName())==0 ) &&
369 (fDigitsTitle.CompareTo(digitsBranch->GetTitle()) == 0) )
370 emcalNotFound = kFALSE ;
372 if(digitizerNotFound){
373 digitizerBranch = (TBranch *) branches->At(ibranch) ;
374 if( (strcmp(digitizerBranch->GetName(),"AliEMCALDigitizer") == 0) &&
375 (fDigitsTitle.CompareTo(digitizerBranch->GetTitle()) == 0))
376 digitizerNotFound = kFALSE ;
381 if(!(digitizerNotFound && emcalNotFound)){
382 cout << "AliEMCALDigitizer error: " << endl ;
383 cout << " can not update/overwrite existing branches "<< endl ;
384 cout << " do not write " << endl ;
388 // create new branches
390 //First generate file name
392 if(gSystem->Getenv("CONFIG_SPLIT_FILE")){ //generating file name
393 file = new char[strlen(gAlice->GetBaseFile())+20] ;
394 sprintf(file,"%s/EMCAL.Digits.root",gAlice->GetBaseFile()) ;
397 TDirectory *cwd = gDirectory;
399 //First create list of sdigits
400 Int_t bufferSize = 32000 ;
401 digitsBranch = gAlice->TreeD()->Branch("EMCAL",&fDigits,bufferSize);
402 digitsBranch->SetTitle(fDigitsTitle.Data());
404 digitsBranch->SetFile(file);
405 TIter next( digitsBranch->GetListOfBranches());
407 while ((sbr=(TBranch*)next())) {
413 //second - create Digitizer
414 Int_t splitlevel = 0 ;
415 AliEMCALDigitizer * d = this ;
416 digitizerBranch = gAlice->TreeD()->Branch("AliEMCALDigitizer","AliEMCALDigitizer",
417 &d,bufferSize,splitlevel);
418 digitizerBranch->SetTitle(fDigitsTitle.Data());
420 digitizerBranch->SetFile(file);
421 TIter next( digitizerBranch->GetListOfBranches());
423 while ((sbr=(TBranch*)next())) {
429 digitsBranch->Fill() ;
430 digitizerBranch->Fill() ;
432 gAlice->TreeD()->Write(0,kOverwrite) ;
434 //remove fSDigitizer before new event.
443 //____________________________________________________________________________
444 void AliEMCALDigitizer::Exec(Option_t *option) {
447 if(!fInitialized) Init() ;
449 if(strstr(option,"tim"))
450 gBenchmark->Start("EMCALDigitizer");
452 //reset events numbers to start from the beginnig
457 if(!ReadSDigits()) //read sdigits event(s) evaluated by Combinator() from file(s)
460 Digitize(option) ; //Add prepared SDigits to digits and add the noise
463 if(strstr(option,"deb"))
468 if(strstr(option,"tim")){
469 gBenchmark->Stop("EMCALDigitizer");
470 cout << "AliEMCALDigitizer:" << endl ;
471 cout << " took " << gBenchmark->GetCpuTime("EMCALDigitizer") << " seconds for SDigitizing "
472 << gBenchmark->GetCpuTime("EMCALDigitizer")/(fIeventMax->At(0)) << " seconds per event " << endl ;
478 //__________________________________________________________________
479 Bool_t AliEMCALDigitizer::ReadSDigits(){
480 // Reads summable digits from the opened files for the particular set of events given by fIevent
482 if(!fInitialized) Init() ;
486 for(inputs = fNinputs-1; inputs >= 0; inputs --){
488 Int_t event = fIevent->At(inputs) ;
490 TFile * file = (TFile*) gROOT->GetFile(((TObjString *) fHeaderFiles->At(inputs))->GetString() ) ;
493 // Get SDigits Tree header from file
495 sprintf(treeName,"TreeS%d",event);
496 TTree * treeS = (TTree*)file->Get(treeName);
499 cout << "Error at AliEMCALDigitizer: no "<<treeName << " in file " << file->GetName() << endl ;
500 cout << "Do nothing " << endl ;
504 TBranch * sdigitsBranch = 0;
505 TBranch * sdigitizerBranch = 0;
507 TObjArray * branches = treeS->GetListOfBranches() ;
509 Bool_t emcalNotFound = kTRUE ;
510 Bool_t sdigitizerNotFound = kTRUE ;
512 for(ibranch = 0;ibranch <branches->GetEntries();ibranch++){
515 sdigitsBranch=(TBranch *) branches->At(ibranch) ;
516 if(( strcmp("EMCAL",sdigitsBranch->GetName())==0 ) &&
517 ((TObjString*) fSDigitsTitles->At(inputs))->GetString().CompareTo(sdigitsBranch->GetTitle())== 0 )
518 emcalNotFound = kFALSE ;
522 if(sdigitizerNotFound){
523 sdigitizerBranch = (TBranch *) branches->At(ibranch) ;
524 if(( strcmp(sdigitizerBranch->GetName(),"AliEMCALSDigitizer") == 0) &&
525 ((TObjString*) fSDigitsTitles->At(inputs))->GetString().CompareTo(sdigitizerBranch->GetTitle())== 0 )
526 sdigitizerNotFound = kFALSE ;
531 if(sdigitizerNotFound || emcalNotFound){
532 cout << "Can't find Branch with sdigits or SDigitizer in the file " ;
533 if( ((TObjString*)fSDigitsTitles->At(inputs))->GetString().IsNull() )
534 cout << file->GetName() << endl ;
536 cout << ((TObjString*)fSDigitsTitles->At(inputs))->GetString().Data() << endl ;
537 cout << "Do nothing" <<endl ;
541 TClonesArray * sdigits = (TClonesArray*) fSDigits->At(inputs) ;
542 sdigitsBranch->SetAddress(&sdigits) ;
544 AliEMCALSDigitizer *sDigitizer = new AliEMCALSDigitizer();
545 sdigitizerBranch->SetAddress(&sDigitizer) ;
547 sdigitsBranch->GetEntry(0) ;
548 sdigitizerBranch->GetEntry(0) ;
551 fSDigitizer = sDigitizer ;
553 if(!((*fSDigitizer)==(*sDigitizer)) ){
554 cout << "AliEMCALDigitizer ERROR:" << endl ;
555 cout << " you are using sdigits made with different SDigitizers" << endl ;
556 cout << "fSD " << fSDigitizer << " SD" << sDigitizer << endl ;
557 fSDigitizer->Print("") ;
558 sDigitizer->Print("") ;
559 cout << "Do Nothing " << endl ;
564 fPedestal = fSDigitizer->GetPedestalParameter() ;
565 fSlope = fSDigitizer->GetCalibrationParameter() ;
570 //__________________________________________________________________
571 void AliEMCALDigitizer::MixWith(char* HeaderFile, char* sDigitsTitle){
572 // Alows produce digits by superimposing background and signal event.
573 // It is assumed, that headers file with SIGNAL events is opened in
574 // constructor, and now we set the BACKGROUND event, with which we
575 // will mix. Thus we avoid writing (changing) huge and expencive
576 // backgound files: all output will be writen into SIGNAL, i.e.
577 // opened in constructor file.
579 // One can open as many files to mix with as one wants.
587 cout << "Specify at least header file to merge"<< endl ;
592 for(inputs = 0; inputs < fNinputs ; inputs++){
593 if(strcmp(((TObjString *)fHeaderFiles->At(inputs))->GetString(),HeaderFile) == 0 ){
594 if(sDigitsTitle == 0){
595 if(((TObjString*)fSDigitsTitles->At(inputs))->GetString().CompareTo("") == 0){
596 cout << "Entry already exists, do not add" << endl ;
601 if(((TObjString*)fSDigitsTitles->At(inputs))->GetString().CompareTo(sDigitsTitle)){
602 cout << "Entry already exists, do not add" << endl ;
608 fHeaderFiles->Expand(fNinputs+1) ;
609 new((*fHeaderFiles)[fNinputs]) TObjString(HeaderFile) ;
612 TFile * file = new TFile(((TObjString *) fHeaderFiles->At(fNinputs))->GetString()) ;
616 fSDigitsTitles->Expand(fNinputs+1) ;
617 new((*fSDigitsTitles)[fNinputs]) TObjString(sDigitsTitle) ;
619 fSDigits->Expand(fNinputs+1) ;
620 new((*fSDigits)[fNinputs]) TClonesArray("AliEMCALDigit",1000) ;
622 fIevent->Set(fNinputs+1) ;
623 fIevent->AddAt(-1, fNinputs) ;
625 fIeventMax->Set(fNinputs+1) ;
627 TTree * te = (TTree *) file->Get("TE") ;
628 fIeventMax->AddAt((Int_t) te->GetEntries(), fNinputs );
633 //__________________________________________________________________
634 void AliEMCALDigitizer::Print(Option_t* option)const {
638 cout << "------------------- "<< GetName() << " -------------" << endl ;
639 cout << "Digitizing sDigits from file(s): " <<endl ;
641 for(input = 0; input < fNinputs ; input++) {
642 cout << " " << ((TObjString *) fHeaderFiles->At(input))->GetString() <<
643 " Branch title:" << ((TObjString *) fSDigitsTitles->At(input))->GetString() << endl ;
646 cout << "Writing digits to " << ((TObjString *) fHeaderFiles->At(0))->GetString() << endl ;
649 cout << "With following parameters: " << endl ;
650 cout << " Electronics noise in EMC (fPinNoise) = " << fPinNoise << endl ;
651 cout << " Threshold in EMC (fEMCDigitThreshold) = " << fEMCDigitThreshold << endl ; ;
652 cout << "---------------------------------------------------" << endl ;
655 cout << "AliEMCALDigitizer not initialized " << endl ;
658 //__________________________________________________________________
659 void AliEMCALDigitizer::PrintDigits(Option_t * option){
661 cout << "AliEMCALDigitiser:"<< endl ;
662 cout << " Number of entries in Digits list " << fDigits->GetEntriesFast() << endl ;
664 if(strstr(option,"all")){
667 AliEMCALDigit * digit;
668 cout << "Digit Id " << " Amplitude " << " Index " << " Nprim " << " Primaries list " << endl;
670 for (index = 0 ; index < fDigits->GetEntries() ; index++) {
671 digit = (AliEMCALDigit * ) fDigits->At(index) ;
672 cout << setw(8) << digit->GetId() << " " << setw(3) << digit->GetAmp() << " "
673 << setw(6) << digit->GetIndexInList() << " "
674 << setw(5) << digit->GetNprimary() <<" ";
677 for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++)
678 cout << setw(5) << digit->GetPrimary(iprimary+1) << " ";
684 //__________________________________________________________________
685 void AliEMCALDigitizer::SetSDigitsBranch(const char* title){
686 // we set title (comment) of the SDigits branch in the first! header file
687 if(!fInitialized) Init() ;
689 ((TObjString*) fSDigitsTitles->At(0) )->SetString((char*)title) ;
692 //__________________________________________________________________
693 void AliEMCALDigitizer::SetDigitsBranch(const char* title){
694 //Sets the title (comment) of the branch to which Digits branch
695 if(!fInitialized) Init() ;
697 fDigitsTitle = title ;