]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALSDigitizer.cxx
Tuning of EMCAL cluster finder for TRD1 geometry
[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 "TObjectTable.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():TTask("","") 
76 {
77   // ctor
78   fFirstEvent = fLastEvent  = fControlHists = 0;  
79   fDefaultInit = kTRUE ; 
80   fHists = 0;
81 }
82
83 //____________________________________________________________________________ 
84 AliEMCALSDigitizer::AliEMCALSDigitizer(const char * alirunFileName, 
85                                        const char * eventFolderName):
86   TTask("EMCAL"+AliConfig::Instance()->GetSDigitizerTaskName(), alirunFileName),
87   fEventFolderName(eventFolderName)
88 {
89   // ctor
90   fFirstEvent = fLastEvent  = fControlHists = 0 ; // runs one event by defaut  
91   Init();
92   InitParameters() ; 
93   fDefaultInit = kFALSE ; 
94   fHists = 0;
95   fControlHists = 1;
96   if(fControlHists) BookControlHists(1);
97 }
98
99
100 //____________________________________________________________________________ 
101 AliEMCALSDigitizer::AliEMCALSDigitizer(const AliEMCALSDigitizer & sd) : TTask(sd) {
102   //cpy ctor 
103
104   fFirstEvent    = sd.fFirstEvent ; 
105   fLastEvent     = sd.fLastEvent ;
106   fA             = sd.fA ;
107   fB             = sd.fB ;
108   fECPrimThreshold  = sd.fECPrimThreshold ;
109   fSDigitsInRun  = sd.fSDigitsInRun ;
110   SetName(sd.GetName()) ; 
111   SetTitle(sd.GetTitle()) ; 
112   fEventFolderName = sd.fEventFolderName;
113 }
114
115
116 //____________________________________________________________________________ 
117 AliEMCALSDigitizer::~AliEMCALSDigitizer() {
118   AliLoader *emcalLoader=0;
119   if ((emcalLoader = AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL")))
120       emcalLoader->CleanSDigitizer();
121 }
122
123 //____________________________________________________________________________ 
124 void AliEMCALSDigitizer::Init(){
125   // Initialization: open root-file, allocate arrays for hits and sdigits,
126   // attach task SDigitizer to the list of EMCAL tasks
127   // 
128   // Initialization can not be done in the default constructor
129   //============================================================= YS
130   //  The initialisation is now done by the getter
131
132   fInit = kTRUE ; 
133    
134   AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
135
136   if ( emcalLoader == 0 ) {
137     Fatal("Init", "Could not obtain the AliEMCALLoader");
138     return ;
139   } 
140   
141   emcalLoader->PostSDigitizer(this);
142   emcalLoader->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
143   
144 }
145
146 //____________________________________________________________________________ 
147 void AliEMCALSDigitizer::InitParameters()
148 {
149   const AliEMCALGeometry * geom = AliEMCALGeometry::GetInstance();
150   if (geom->GetSampling() == 0.) {
151     Fatal("InitParameters", "Sampling factor not set !") ; 
152   }
153   // Initializes parameters
154   fA         = 0;
155   fB         = 1.e+7;
156   fSampling  = geom->GetSampling();
157
158  // threshold for deposit energy of hit
159   fECPrimThreshold  = 0.; // 24-nov-04 - was 1.e-6;
160   Print("");
161 }
162
163 //____________________________________________________________________________
164 void AliEMCALSDigitizer::Exec(Option_t *option) 
165
166   // Collects all hit of the same tower into digits
167   TString o(option); o.ToUpper();
168   if (strstr(option, "print") ) {
169     Print() ; 
170     return ; 
171   }
172   
173   if(strstr(option,"tim"))
174     gBenchmark->Start("EMCALSDigitizer");
175
176   AliRunLoader *rl = AliRunLoader::GetRunLoader();
177   AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>(rl->GetDetectorLoader("EMCAL"));
178
179   //switch off reloading of this task while getting event
180   if (!fInit) { // to prevent overwrite existing file
181     AliError( Form("Give a version name different from %s", fEventFolderName.Data()) ) ;
182     return ;
183     }
184
185   if (fLastEvent == -1) 
186     fLastEvent = rl->GetNumberOfEvents() - 1 ;
187   else {
188     fLastEvent = TMath::Min(fLastEvent, rl->GetNumberOfEvents()-1);
189   }
190   Int_t nEvents   = fLastEvent - fFirstEvent + 1;
191
192   Int_t ievent;
193   Float_t energy=0.; // de * fSampling - 23-nov-04
194   rl->LoadKinematics();
195   rl->LoadHits("EMCAL");
196
197   for (ievent = fFirstEvent; ievent <= fLastEvent; ievent++) {
198     rl->GetEvent(ievent);
199     TTree * treeS = emcalLoader->TreeS();
200     if ( !treeS ) { 
201       emcalLoader->MakeSDigitsContainer();
202       treeS = emcalLoader->TreeS();
203     }
204     TClonesArray * hits = emcalLoader->Hits() ; 
205     TClonesArray * sdigits = emcalLoader->SDigits() ;
206     sdigits->Clear();
207     Int_t nSdigits = 0 ;
208     //Now make SDigits from hits, for EMCAL it is the same, so just copy    
209     Int_t nPrim =  static_cast<Int_t>((emcalLoader->TreeH())->GetEntries()) ; 
210     // This is not true: there is only one list of hits (MvL jan 2006)
211     // Attention nPrim is the number of primaries tracked by Geant 
212     // and this number could be different to the number of Primaries in TreeK; 
213     Int_t iprim ;
214     for ( iprim = 0 ; iprim < nPrim ; iprim++ ) { 
215       //=========== Get the EMCAL branch from Hits Tree for the Primary iprim
216       rl->Stack()->Particle(iprim) ;
217       Int_t i;
218       AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance(); 
219       for ( i = 0 ; i < hits->GetEntries() ; i++ ) {
220         AliEMCALHit * hit = dynamic_cast<AliEMCALHit*>(hits->At(i)) ;
221         AliEMCALDigit * curSDigit = 0 ;
222         AliEMCALDigit * sdigit = 0 ;
223         Bool_t newsdigit = kTRUE; 
224
225         // hit->GetId() - Absolute Id number EMCAL segment
226         if(geom->CheckAbsCellId(hit->GetId())) { // was IsInECA(hit->GetId())
227           energy = hit->GetEnergy() * fSampling; // 23-nov-04
228           if(energy >  fECPrimThreshold )
229         // Assign primary number only if deposited energy is significant
230             curSDigit =  new AliEMCALDigit( hit->GetPrimary(),
231                                             hit->GetIparent(), hit->GetId(), 
232                                             Digitize(energy), hit->GetTime() ) ;
233           else
234             curSDigit =  new AliEMCALDigit( -1               , 
235                                             -1               ,
236                                             hit->GetId(), 
237                                             Digitize(energy), hit->GetTime() ) ;
238         } else {
239           Warning("Exec"," abs id %i is bad \n", hit->GetId());
240           newsdigit = kFALSE;
241           curSDigit = 0;
242         }
243
244         if(curSDigit != 0){
245           for(Int_t check= 0; check < nSdigits ; check++) {
246             sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(check)) ;
247             
248             if( sdigit->GetId() == curSDigit->GetId()) { // Are we in the same ECAL tower ?              
249               *sdigit = *sdigit + *curSDigit;
250               newsdigit = kFALSE;
251             }
252           }
253         }
254         if (newsdigit) {
255           new((*sdigits)[nSdigits])  AliEMCALDigit(*curSDigit);
256           nSdigits++ ;  
257         }
258         delete curSDigit ; 
259       }  // loop over all hits (hit = deposited energy/entering particle)
260     } // loop over iprim
261     sdigits->Sort() ;
262     
263     nSdigits = sdigits->GetEntriesFast() ;
264     fSDigitsInRun += nSdigits ;  
265
266     Int_t nPrimarymax = -1 ; 
267     Int_t i ;
268     Double_t e=0.,esum=0.;
269     AliEMCALHistoUtilities::FillH1(fHists, 0, double(sdigits->GetEntriesFast()));
270     for (i = 0 ; i < sdigits->GetEntriesFast() ; i++) { 
271       AliEMCALDigit * sdigit = dynamic_cast<AliEMCALDigit *>(sdigits->At(i)) ;
272       sdigit->SetIndexInList(i) ;
273
274       AliEMCALHistoUtilities::FillH1(fHists, 2, double(sdigit->GetAmp()));
275       e = double(Calibrate(sdigit->GetAmp()));
276       esum += e;
277       AliEMCALHistoUtilities::FillH1(fHists, 3, e);
278       AliEMCALHistoUtilities::FillH1(fHists, 4, double(sdigit->GetId()));
279     }
280     if(esum>0.) AliEMCALHistoUtilities::FillH1(fHists, 1, esum);
281     
282     for (i = 0 ; i < sdigits->GetEntriesFast() ; i++) { // for what 
283       if (((dynamic_cast<AliEMCALDigit *>(sdigits->At(i)))->GetNprimary()) > nPrimarymax)
284         nPrimarymax = ((dynamic_cast<AliEMCALDigit *>(sdigits->At(i)))->GetNprimary()) ;
285     }
286
287     // Now write SDigits    
288
289     Int_t bufferSize = 32000 ;    
290     TBranch * sdigitsBranch = treeS->GetBranch("EMCAL");
291     if (sdigitsBranch)
292       sdigitsBranch->SetAddress(&sdigits);
293     else
294       treeS->Branch("EMCAL",&sdigits,bufferSize);
295  
296     treeS->Fill();
297     
298     emcalLoader->WriteSDigits("OVERWRITE");
299     
300     //NEXT - SDigitizer
301     emcalLoader->WriteSDigitizer("OVERWRITE");  // why in event cycle ?
302     
303     if(strstr(option,"deb"))
304       PrintSDigits(option) ;  
305   }
306
307   Unload();
308   
309   emcalLoader->GetSDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
310   
311   if(strstr(option,"tim")){
312     gBenchmark->Stop("EMCALSDigitizer"); 
313     printf("\n Exec: took %f seconds for SDigitizing %f seconds per event\n", 
314          gBenchmark->GetCpuTime("EMCALSDigitizer"), gBenchmark->GetCpuTime("EMCALSDigitizer")/nEvents ) ; 
315   }
316 }
317
318
319 void AliEMCALSDigitizer::Print1(Option_t * option)
320 {
321   Print(); 
322   PrintSDigits(option);
323 }
324
325 //__________________________________________________________________
326 void AliEMCALSDigitizer::Print(Option_t *option) const
327
328   // Prints parameters of SDigitizer
329   printf("Print: \n------------------- %s ------------- option %s\n", GetName() , option) ; 
330   printf("   fInit                                 %i\n", int(fInit));
331   printf("   fFirstEvent                           %i\n", fFirstEvent);
332   printf("   fLastEvent                            %i\n", fLastEvent);
333   printf("   Writing SDigits to branch with title  %s\n", fEventFolderName.Data()) ;
334   printf("   with digitization parameters       A = %f\n", fA) ; 
335   printf("                                      B = %f\n", fB) ;
336   printf("   Threshold for EC Primary assignment  = %f\n", fECPrimThreshold)  ;
337   printf("   Sampling                             = %f\n", fSampling);
338   printf("---------------------------------------------------\n") ;
339 }
340
341 //__________________________________________________________________
342 Bool_t AliEMCALSDigitizer::operator==( AliEMCALSDigitizer const &sd )const
343 {
344   // Equal operator.
345   // SDititizers are equal if their pedestal, slope and threshold are equal
346   if( (fA==sd.fA)&&(fB==sd.fB)&&
347       (fECPrimThreshold==sd.fECPrimThreshold))
348     return kTRUE ;
349   else
350     return kFALSE ;
351 }
352
353 //__________________________________________________________________
354 void AliEMCALSDigitizer::PrintSDigits(Option_t * option)
355 {
356   //Prints list of digits produced at the current pass of AliEMCALDigitizer
357   
358   
359   // AliEMCALGetter * gime = AliEMCALGetter::Instance() ; 
360   AliEMCALLoader *rl = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
361   const TClonesArray * sdigits = rl->SDigits() ; 
362   
363   printf("\n") ;  
364   printf("event %i", rl->GetRunLoader()->GetEventNumber());
365   printf(" Number of entries in SDigits list %i", sdigits->GetEntriesFast()); 
366   if(strstr(option,"all")||strstr(option,"EMC")){
367     
368     //loop over digits
369     AliEMCALDigit * digit;
370     printf("\n   Id  Amplitude    Time          Index Nprim: Primaries list \n") ;    
371     Int_t index, isum=0;
372     char * tempo = new char[8192]; 
373     for (index = 0 ; index < sdigits->GetEntries() ; index++) {
374       digit = dynamic_cast<AliEMCALDigit *>( sdigits->At(index) ) ;
375       sprintf(tempo, "\n%6d  %8d    %6.5e %4d      %2d :",
376               digit->GetId(), digit->GetAmp(), digit->GetTime(), digit->GetIndexInList(), digit->GetNprimary()) ;  
377       printf(tempo);
378       isum += digit->GetAmp();
379       
380       Int_t iprimary;
381       for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++) {
382         sprintf(tempo, "%d ",digit->GetPrimary(iprimary+1) ) ; 
383         printf(tempo); 
384       }          
385     }
386     delete tempo ;
387     printf("\n** Sum %i : %10.3f GeV/c **\n ", isum, double(isum)*1.e-6);
388   } else printf("\n");
389 }
390
391 //____________________________________________________________________________ 
392 void AliEMCALSDigitizer::Unload() const
393 {
394   // Unload Hits and SDigits from the folder
395   AliEMCALLoader *rl = dynamic_cast<AliEMCALLoader*>(AliRunLoader::GetRunLoader()->GetDetectorLoader("EMCAL"));
396   rl->UnloadHits() ; 
397   rl->UnloadSDigits() ; 
398 }
399
400 void AliEMCALSDigitizer::Browse(TBrowser* b)
401 {
402   if(fHists) b->Add(fHists);
403   TTask::Browse(b);
404 }
405
406 TList *AliEMCALSDigitizer::BookControlHists(int var)
407 { // 22-nov-04
408   gROOT->cd();
409   const AliEMCALGeometry *geom = AliEMCALGeometry::GetInstance() ;
410   if(var>=1){
411     printf(" AliEMCALSDigitizer::BookControlHists() in action \n");
412     new TH1F("HSDigiN",  "#EMCAL  sdigits ", 1001, -0.5, 1000.5);
413     new TH1F("HSDigiSumEnergy","Sum.EMCAL energy", 1000, 0.0, 100.);
414     new TH1F("HSDigiAmp",  "EMCAL sdigits amplitude", 1000, 0., 2.e+9);
415     new TH1F("HSDigiEnergy","EMCAL cell energy", 1000, 0.0, 100.);
416     new TH1F("HSDigiAbsId","EMCAL absID for sdigits",
417     geom->GetNCells(), 0.5, Double_t(geom->GetNCells())+0.5);
418   }
419
420   fHists = AliEMCALHistoUtilities::MoveHistsToList("EmcalSDigiControlHists", kFALSE);
421   fHists = 0;
422
423   return fHists;
424 }
425
426 void AliEMCALSDigitizer::SaveHists(const char* name, Bool_t kSingleKey, const char* opt)
427 {
428   AliEMCALHistoUtilities::SaveListOfHists(fHists, name, kSingleKey, opt); 
429 }