]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALSDigitizer.cxx
updates for Effective C++ compiler flags
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALSDigitizer.cxx
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 //_________________________________________________________________________
19 // This is a TTask that makes SDigits out of Hits
20 // A Summable Digits is the sum of all hits originating 
21 // from one in one tower of the EMCAL 
22 // A threshold for assignment of the primary to SDigit is applied 
23 // SDigits are written to TreeS, branch "EMCAL"
24 // AliEMCALSDigitizer with all current parameters is written 
25 // to TreeS branch "AliEMCALSDigitizer".
26 // Both branches have the same title. If necessary one can produce 
27 // another set of SDigits with different parameters. Two versions
28 // can be distunguished using titles of the branches.
29 // User case:
30 // root [0] AliEMCALSDigitizer * s = new AliEMCALSDigitizer("galice.root")
31 // Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
32 // root [1] s->ExecuteTask()
33 //             // Makes SDigitis for all events stored in galice.root
34 // root [2] s->SetPedestalParameter(0.001)
35 //             // One can change parameters of digitization
36 // root [3] s->SetSDigitsBranch("Redestal 0.001")
37 //             // and write them into the new branch
38 // root [4] s->ExeciteTask("deb all tim")
39 //             // available parameters:
40 //             deb - print # of produced SDigitis
41 //             deb all  - print # and list of produced SDigits
42 //             tim - print benchmarking information
43 //
44 //*-- Author : Sahal Yacoob (LBL)
45 // based on  : AliPHOSSDigitzer 
46 // Modif: 
47 //  August 2002 Yves Schutz: clone PHOS as closely as possible and intoduction
48 //                           of new  IO (à la PHOS)
49 //////////////////////////////////////////////////////////////////////////////
50
51 // --- ROOT system ---
52 #include <TBenchmark.h>
53 #include <TH1.h>
54 #include <TBrowser.h>
55 #include <Riostream.h>
56 #include <TMath.h>
57
58 // --- Standard library ---
59 #include "stdlib.h"
60
61 // --- AliRoot header files ---
62 #include "AliLog.h"
63 #include "AliRunLoader.h"
64 #include "AliStack.h"
65 #include "AliEMCALDigit.h"
66 #include "AliEMCALLoader.h"
67 #include "AliEMCALHit.h"
68 #include "AliEMCALSDigitizer.h"
69 #include "AliEMCALGeometry.h"
70 #include "AliEMCALHistoUtilities.h"
71
72 ClassImp(AliEMCALSDigitizer)
73            
74 //____________________________________________________________________________ 
75 AliEMCALSDigitizer::AliEMCALSDigitizer()
76   : TTask("",""),
77     fA(0.),fB(0.),fECPrimThreshold(0.),
78     fDefaultInit(kTRUE),
79     fEventFolderName(0),
80     fInit(0),
81     fSDigitsInRun(0),
82     fFirstEvent(0),
83     fLastEvent(0),
84     fSampling(0.),
85     fControlHists(0),
86     fHists(0)
87 {
88   // ctor
89   InitParameters();
90 }
91
92 //____________________________________________________________________________ 
93 AliEMCALSDigitizer::AliEMCALSDigitizer(const char * alirunFileName, 
94                                        const char * eventFolderName)
95   : TTask("EMCAL"+AliConfig::Instance()->GetSDigitizerTaskName(), alirunFileName),
96     fA(0.),fB(0.),fECPrimThreshold(0.),
97     fDefaultInit(kFALSE),
98     fEventFolderName(eventFolderName),
99     fInit(0),
100     fSDigitsInRun(0),
101     fFirstEvent(0),
102     fLastEvent(0),
103     fSampling(0.),
104     fControlHists(1),
105     fHists(0)
106 {
107   // ctor
108   Init();
109   InitParameters() ; 
110   if(fControlHists) BookControlHists(1);
111 }
112
113
114 //____________________________________________________________________________ 
115 AliEMCALSDigitizer::AliEMCALSDigitizer(const AliEMCALSDigitizer & sd) 
116   : TTask(sd.GetName(),sd.GetTitle()),
117     fA(sd.fA),
118     fB(sd.fB),
119     fECPrimThreshold(sd.fECPrimThreshold),
120     fDefaultInit(sd.fDefaultInit),
121     fEventFolderName(sd.fEventFolderName),
122     fInit(sd.fInit),
123     fSDigitsInRun(sd.fSDigitsInRun),
124     fFirstEvent(sd.fFirstEvent),
125     fLastEvent(sd.fLastEvent),
126     fSampling(sd.fSampling),
127     fControlHists(sd.fControlHists),
128     fHists(sd.fHists)
129 {
130   //cpy ctor 
131 }
132
133
134 //____________________________________________________________________________ 
135 AliEMCALSDigitizer::~AliEMCALSDigitizer() {
136   //dtor
137   AliLoader *emcalLoader=0;
138   if ((emcalLoader = AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL")))
139       emcalLoader->CleanSDigitizer();
140 }
141
142 //____________________________________________________________________________ 
143 void AliEMCALSDigitizer::Init(){
144   // Initialization: open root-file, allocate arrays for hits and sdigits,
145   // attach task SDigitizer to the list of EMCAL tasks
146   // 
147   // Initialization can not be done in the default constructor
148   //============================================================= YS
149   //  The initialisation is now done by the getter
150
151   fInit = kTRUE; 
152    
153   AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
154
155   if ( emcalLoader == 0 ) {
156     Fatal("Init", "Could not obtain the AliEMCALLoader");
157     return ;
158   } 
159   
160   emcalLoader->PostSDigitizer(this);
161   emcalLoader->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
162   
163 }
164
165 //____________________________________________________________________________ 
166 void AliEMCALSDigitizer::InitParameters()
167 {
168   //initialize parameters for sdigitization
169
170   const AliEMCALGeometry * geom = AliEMCALGeometry::GetInstance();
171   if (geom->GetSampling() == 0.) {
172     Fatal("InitParameters", "Sampling factor not set !") ; 
173   }
174   // Initializes parameters
175   fA         = 0;
176   fB         = 1.e+7;
177   fSampling  = geom->GetSampling();
178
179  // threshold for deposit energy of hit
180   fECPrimThreshold  = 0.; // 24-nov-04 - was 1.e-6;
181   Print("");
182 }
183
184 //____________________________________________________________________________
185 void AliEMCALSDigitizer::Exec(Option_t *option) 
186
187   // Collects all hit of the same tower into digits
188   TString o(option); o.ToUpper();
189   if (strstr(option, "print") ) {
190     Print() ; 
191     return ; 
192   }
193   
194   if(strstr(option,"tim"))
195     gBenchmark->Start("EMCALSDigitizer");
196
197   AliRunLoader *rl = AliRunLoader::GetRunLoader();
198   AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
199
200   //switch off reloading of this task while getting event
201   if (!fInit) { // to prevent overwrite existing file
202     AliError( Form("Give a version name different from %s", fEventFolderName.Data()) ) ;
203     return ;
204     }
205
206   if (fLastEvent == -1) 
207     fLastEvent = rl->GetNumberOfEvents() - 1 ;
208   else {
209     fLastEvent = TMath::Min(fLastEvent, rl->GetNumberOfEvents()-1);
210   }
211   Int_t nEvents   = fLastEvent - fFirstEvent + 1;
212
213   Int_t ievent;
214   Float_t energy=0.; // de * fSampling - 23-nov-04
215   rl->LoadKinematics();
216   rl->LoadHits("EMCAL");
217
218   for (ievent = fFirstEvent; ievent <= fLastEvent; ievent++) {
219     rl->GetEvent(ievent);
220     TTree * treeS = emcalLoader->TreeS();
221     if ( !treeS ) { 
222       emcalLoader->MakeSDigitsContainer();
223       treeS = emcalLoader->TreeS();
224     }
225     TClonesArray * hits = emcalLoader->Hits() ; 
226     TClonesArray * sdigits = emcalLoader->SDigits() ;
227     sdigits->Clear();
228
229     Int_t nSdigits = 0 ;
230     Int_t i;
231     AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance(); 
232     for ( i = 0 ; i < hits->GetEntries() ; i++ ) {
233       AliEMCALHit * hit = dynamic_cast<AliEMCALHit*>(hits->At(i)) ;
234       AliEMCALDigit * curSDigit = 0 ;
235       AliEMCALDigit * sdigit = 0 ;
236       Bool_t newsdigit = kTRUE; 
237       
238       // hit->GetId() - Absolute Id number EMCAL segment
239       if(geom->CheckAbsCellId(hit->GetId())) { // was IsInECA(hit->GetId())
240         energy = hit->GetEnergy() * fSampling; // 23-nov-04
241         if(energy >  fECPrimThreshold )
242           // Assign primary number only if deposited energy is significant
243           curSDigit =  new AliEMCALDigit( hit->GetPrimary(),
244                                           hit->GetIparent(), hit->GetId(), 
245                                           Digitize(energy), hit->GetTime() ) ;
246           else
247             curSDigit =  new AliEMCALDigit( -1               , 
248                                             -1               ,
249                                             hit->GetId(), 
250                                             Digitize(energy), hit->GetTime() ) ;
251       } else {
252         Warning("Exec"," abs id %i is bad \n", hit->GetId());
253         newsdigit = kFALSE;
254         curSDigit = 0;
255       }
256       
257       if(curSDigit != 0){
258         for(Int_t check= 0; check < nSdigits ; check++) {
259           sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(check)) ;
260           
261           if( sdigit->GetId() == curSDigit->GetId()) { // Are we in the same ECAL tower ?              
262             *sdigit = *sdigit + *curSDigit;
263             newsdigit = kFALSE;
264           }
265         }
266       }
267       if (newsdigit) {
268         new((*sdigits)[nSdigits])  AliEMCALDigit(*curSDigit);
269         nSdigits++ ;  
270       }
271       delete curSDigit ; 
272     }  // loop over all hits (hit = deposited energy/entering particle)
273     sdigits->Sort() ;
274     
275     nSdigits = sdigits->GetEntriesFast() ;
276     fSDigitsInRun += nSdigits ;  
277     
278     Double_t e=0.,esum=0.;
279     AliEMCALHistoUtilities::FillH1(fHists, 0, double(sdigits->GetEntriesFast()));
280     for (i = 0 ; i < sdigits->GetEntriesFast() ; i++) { 
281       AliEMCALDigit * sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(i)) ;
282       sdigit->SetIndexInList(i) ;
283       
284       AliEMCALHistoUtilities::FillH1(fHists, 2, double(sdigit->GetAmp()));
285       e = double(Calibrate(sdigit->GetAmp()));
286       esum += e;
287       AliEMCALHistoUtilities::FillH1(fHists, 3, e);
288       AliEMCALHistoUtilities::FillH1(fHists, 4, double(sdigit->GetId()));
289     }
290     if(esum>0.) AliEMCALHistoUtilities::FillH1(fHists, 1, esum);
291     
292     // Now write SDigits    
293     
294     Int_t bufferSize = 32000 ;    
295     TBranch * sdigitsBranch = treeS->GetBranch("EMCAL");
296     if (sdigitsBranch)
297       sdigitsBranch->SetAddress(&sdigits);
298     else
299       treeS->Branch("EMCAL",&sdigits,bufferSize);
300     
301     treeS->Fill();
302     
303     emcalLoader->WriteSDigits("OVERWRITE");
304     
305     //NEXT - SDigitizer
306     emcalLoader->WriteSDigitizer("OVERWRITE");  // why in event cycle ?
307     
308     if(strstr(option,"deb"))
309       PrintSDigits(option) ;  
310   }
311   
312   Unload();
313   
314   emcalLoader->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
315   
316   if(strstr(option,"tim")){
317     gBenchmark->Stop("EMCALSDigitizer"); 
318     printf("\n Exec: took %f seconds for SDigitizing %f seconds per event\n", 
319            gBenchmark->GetCpuTime("EMCALSDigitizer"), gBenchmark->GetCpuTime("EMCALSDigitizer")/nEvents ) ; 
320   }
321 }
322
323 //__________________________________________________________________
324
325 Int_t AliEMCALSDigitizer::Digitize(Float_t energy)const {
326   // Digitize the energy
327     Double_t aSignal = fA + energy*fB;
328     if (TMath::Abs(aSignal)>2147483647.0) { 
329       //PH 2147483647 is the max. integer
330       //PH This apparently is a problem which needs investigation
331       AliWarning(Form("Too big or too small energy %f",aSignal));
332       aSignal = TMath::Sign((Double_t)2147483647,aSignal);
333     }
334     return (Int_t ) aSignal;
335   }
336  
337
338 //__________________________________________________________________
339
340 void AliEMCALSDigitizer::Print1(Option_t * option)
341 {
342   Print(); 
343   PrintSDigits(option);
344 }
345
346 //__________________________________________________________________
347 void AliEMCALSDigitizer::Print(Option_t *option) const
348
349   // Prints parameters of SDigitizer
350   printf("Print: \n------------------- %s ------------- option %s\n", GetName() , option) ; 
351   printf("   fInit                                 %i\n", int(fInit));
352   printf("   fFirstEvent                           %i\n", fFirstEvent);
353   printf("   fLastEvent                            %i\n", fLastEvent);
354   printf("   Writing SDigits to branch with title  %s\n", fEventFolderName.Data()) ;
355   printf("   with digitization parameters       A = %f\n", fA) ; 
356   printf("                                      B = %f\n", fB) ;
357   printf("   Threshold for EC Primary assignment  = %f\n", fECPrimThreshold)  ;
358   printf("   Sampling                             = %f\n", fSampling);
359   printf("---------------------------------------------------\n") ;
360 }
361
362 //__________________________________________________________________
363 Bool_t AliEMCALSDigitizer::operator==( AliEMCALSDigitizer const &sd )const
364 {
365   // Equal operator.
366   // SDititizers are equal if their pedestal, slope and threshold are equal
367   if( (fA==sd.fA)&&(fB==sd.fB)&&
368       (fECPrimThreshold==sd.fECPrimThreshold))
369     return kTRUE ;
370   else
371     return kFALSE ;
372 }
373
374 //__________________________________________________________________
375 void AliEMCALSDigitizer::PrintSDigits(Option_t * option)
376 {
377   //Prints list of digits produced at the current pass of AliEMCALDigitizer
378   
379   
380   // AliEMCALGetter * gime = AliEMCALGetter::Instance() ; 
381   AliEMCALLoader *rl = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
382   const TClonesArray * sdigits = rl->SDigits() ; 
383   
384   printf("\n") ;  
385   printf("event %i", rl->GetRunLoader()->GetEventNumber());
386   printf(" Number of entries in SDigits list %i", sdigits->GetEntriesFast()); 
387   if(strstr(option,"all")||strstr(option,"EMC")){
388     
389     //loop over digits
390     AliEMCALDigit * digit;
391     printf("\n   Id  Amplitude    Time          Index Nprim: Primaries list \n") ;    
392     Int_t index, isum=0;
393     char * tempo = new char[8192]; 
394     for (index = 0 ; index < sdigits->GetEntries() ; index++) {
395       digit = dynamic_cast<AliEMCALDigit *>( sdigits->At(index) ) ;
396       sprintf(tempo, "\n%6d  %8d    %6.5e %4d      %2d :",
397               digit->GetId(), digit->GetAmp(), digit->GetTime(), digit->GetIndexInList(), digit->GetNprimary()) ;  
398       printf(tempo);
399       isum += digit->GetAmp();
400       
401       Int_t iprimary;
402       for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++) {
403         sprintf(tempo, "%d ",digit->GetPrimary(iprimary+1) ) ; 
404         printf(tempo); 
405       }          
406     }
407     delete tempo ;
408     printf("\n** Sum %i : %10.3f GeV/c **\n ", isum, double(isum)*1.e-6);
409   } else printf("\n");
410 }
411
412 //____________________________________________________________________________ 
413 void AliEMCALSDigitizer::Unload() const
414 {
415   // Unload Hits and SDigits from the folder
416   AliEMCALLoader *rl = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
417   rl->UnloadHits() ; 
418   rl->UnloadSDigits() ; 
419 }
420
421 void AliEMCALSDigitizer::Browse(TBrowser* b)
422 {
423   if(fHists) b->Add(fHists);
424   TTask::Browse(b);
425 }
426
427 TList *AliEMCALSDigitizer::BookControlHists(int var)
428
429   //book histograms for monitoring sdigitization
430   // 22-nov-04
431   gROOT->cd();
432   const AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance() ;
433   if(var>=1){
434     printf(" AliEMCALSDigitizer::BookControlHists() in action \n");
435     new TH1F("HSDigiN",  "#EMCAL  sdigits ", 1001, -0.5, 1000.5);
436     new TH1F("HSDigiSumEnergy","Sum.EMCAL energy", 1000, 0.0, 100.);
437     new TH1F("HSDigiAmp",  "EMCAL sdigits amplitude", 1000, 0., 2.e+9);
438     new TH1F("HSDigiEnergy","EMCAL cell energy", 1000, 0.0, 100.);
439     new TH1F("HSDigiAbsId","EMCAL absID for sdigits",
440     geom->GetNCells(), 0.5, Double_t(geom->GetNCells())+0.5);
441   }
442
443   fHists = AliEMCALHistoUtilities::MoveHistsToList("EmcalSDigiControlHists", kFALSE);
444   fHists = 0;
445
446   return fHists;
447 }
448
449 void AliEMCALSDigitizer::SaveHists(const char* name, Bool_t kSingleKey, const char* opt)
450 {
451   AliEMCALHistoUtilities::SaveListOfHists(fHists, name, kSingleKey, opt); 
452 }