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