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